Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 886381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:03:28+00:00 2026-05-15T13:03:28+00:00

I’ve been playing with Code Contracts and I really like what I’ve seen so

  • 0

I’ve been playing with Code Contracts and I really like what I’ve seen so far. They encourage me to evaluate and explicitly declare my assumptions, which has already helped me to identify a few corner cases I hadn’t considered in the code to which I’m adding contracts. Right now I’m playing with trying to enforce more sophisticated invariants. I have one case that currently fails proving and I’m curious if there is a way I can fix this besides simply adding Contract.Assume calls. Here is the class in question, stripped down for ease of reading:

public abstract class MemoryEncoder
{
    private const int CapacityDelta = 16;

    private int _currentByte;

    /// <summary>
    ///   The current byte index in the encoding stream.
    ///   This should not need to be modified, under typical usage,
    ///   but can be used to randomly access the encoding region.
    /// </summary>
    public int CurrentByte
    {
        get
        {
            Contract.Ensures(Contract.Result<int>() >= 0);
            Contract.Ensures(Contract.Result<int>() <= Length);
            return _currentByte;
        }
        set
        {
            Contract.Requires(value >= 0);
            Contract.Requires(value <= Length);
            _currentByte = value;
        }
    }

    /// <summary>
    ///   Current number of bytes encoded in the buffer.
    ///   This may be less than the size of the buffer (capacity).
    /// </summary>
    public int Length { get; private set; }

    /// <summary>
    /// The raw buffer encapsulated by the encoder.
    /// </summary>
    protected internal Byte[] Buffer { get; private set; }

    /// <summary>
    /// Reserve space in the encoder buffer for the specified number of new bytes
    /// </summary>
    /// <param name="bytesRequired">The number of bytes required</param>
    protected void ReserveSpace(int bytesRequired)
    {
        Contract.Requires(bytesRequired > 0);
        Contract.Ensures((Length - CurrentByte) >= bytesRequired);

        //Check if these bytes would overflow the current buffer););
        if ((CurrentByte + bytesRequired) > Buffer.Length)
        {
            //Create a new buffer with at least enough space for the additional bytes required
            var newBuffer = new Byte[Buffer.Length + Math.Max(bytesRequired, CapacityDelta)];

            //Copy the contents of the previous buffer and replace the original buffer reference
            Buffer.CopyTo(newBuffer, 0);
            Buffer = newBuffer;
        }

        //Check if the total length of written bytes has increased
        if ((CurrentByte + bytesRequired) > Length)
        {
            Length = CurrentByte + bytesRequired;
        }
    }

    [ContractInvariantMethod]
    private void GlobalRules()
    {
        Contract.Invariant(Buffer != null);
        Contract.Invariant(Length <= Buffer.Length);
        Contract.Invariant(CurrentByte >= 0);
        Contract.Invariant(CurrentByte <= Length);
    }
}

I’m interested in how I can structure the Contract calls in ReserveSpace so that the class invariants are provable. In particular, it complains about (Length <= Buffer.Length) and (CurrentByte <= Length). It’s reasonable to me that it can’t see that (Length <= Buffer.Length) is satisfied, since it’s creating a new buffer and reassigning the reference. Is my only option to add an Assume that the invariants are satisfied?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:03:29+00:00Added an answer on May 15, 2026 at 1:03 pm

    After fighting with this for a while, I came up with this provable solution (constructor is a dummy to allow for isolated testing):

    public abstract class MemoryEncoder
    {
        private const int CapacityDelta = 16;
    
        private byte[] _buffer;
        private int _currentByte;
        private int _length;
    
        protected MemoryEncoder()
        {
            Buffer = new byte[500];
            Length = 0;
            CurrentByte = 0;
        }
    
        /// <summary>
        ///   The current byte index in the encoding stream.
        ///   This should not need to be modified, under typical usage,
        ///   but can be used to randomly access the encoding region.
        /// </summary>
        public int CurrentByte
        {
            get
            {
                return _currentByte;
            }
            set
            {
                Contract.Requires(value >= 0);
                Contract.Requires(value <= Length);
                _currentByte = value;
            }
        }
    
    
        /// <summary>
        ///   Current number of bytes encoded in the buffer.
        ///   This may be less than the size of the buffer (capacity).
        /// </summary>
        public int Length
        {
            get { return _length; }
            private set
            {
                Contract.Requires(value >= 0);
                Contract.Requires(value <= _buffer.Length);
                Contract.Requires(value >= CurrentByte);
                Contract.Ensures(_length <= _buffer.Length);
                _length = value;
            }
        }
    
        /// <summary>
        /// The raw buffer encapsulated by the encoder.
        /// </summary>
        protected internal Byte[] Buffer
        {
            get { return _buffer; }
            private set
            {
                Contract.Requires(value != null);
                Contract.Requires(value.Length >= _length);
                _buffer = value;
            }
        }
    
        /// <summary>
        /// Reserve space in the encoder buffer for the specified number of new bytes
        /// </summary>
        /// <param name="bytesRequired">The number of bytes required</param>
        protected void ReserveSpace(int bytesRequired)
        {
            Contract.Requires(bytesRequired > 0);
            Contract.Ensures((Length - CurrentByte) >= bytesRequired);
    
            //Check if these bytes would overflow the current buffer););
            if ((CurrentByte + bytesRequired) > Buffer.Length)
            {
                //Create a new buffer with at least enough space for the additional bytes required
                var newBuffer = new Byte[Buffer.Length + Math.Max(bytesRequired, CapacityDelta)];
    
                //Copy the contents of the previous buffer and replace the original buffer reference
                Buffer.CopyTo(newBuffer, 0);
                Buffer = newBuffer;
            }
    
            //Check if the total length of written bytes has increased
            if ((CurrentByte + bytesRequired) > Length)
            {
                Contract.Assume(CurrentByte + bytesRequired <= _buffer.Length);
                Length = CurrentByte + bytesRequired;
            }
        }
    
        [ContractInvariantMethod]
        private void GlobalRules()
        {
            Contract.Invariant(_buffer != null);
            Contract.Invariant(_length <= _buffer.Length);
            Contract.Invariant(_currentByte >= 0);
            Contract.Invariant(_currentByte <= _length);
        }
    }
    

    The main thing I noticed is that placing invariants on properties gets messy, but seems to solve more easily with invariants on fields. It was also important to place appropriate contractual obligations in the property accessors. I’ll have to keep experimenting and see what works and what doesn’t. It’s an interesting system, but I’d definitely like to know more if anybody has a good ‘cheat sheet’ on how the prover works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.