Code Contracts: Contract.ForAll() (7 of n)

Posted on Monday, July 19, 2010 4:59 PM

This post in the series on Code.Contracts is all about the Contract.ForAll method. It comes in two flavors, one that iterates in a for-loop way, and a generic form that iterates over an IEnumerable of T in a more foreach kind of way.

In the example I have used so far, I included at one point the check with Contract.ForAll<char> to ensure all the characters in the result must be equal to the first character of the input string. Unfortunately the static checker of Code Contracts is unable to ensure the correctness of this for all test. It probably has got to do something with the fact that the current System.String implementation does not include the contract checks on the constructor we’re using. Hence the checker presents you with a warning like this:

CodeContracts: ensures unproven: Contract.ForAll(0, count, p => Contract.Result<string>()[p].Equals(input[0]))

I’ve tried a few things to work around this warning, but haven’t had any luck so far. So if anyone has a brilliant idea how to get rid of it, be my guest.

So I created a different example to explain the Contract.ForAll method, which will be proven nicely by the static checker. Here goes:

    public bool FirstTenPrimesFollowingX(int x, List<int> primeList)
    {
      Contract.Requires(x > 0);
      Contract.Requires(primeList != null);
      Contract.Requires(primeList.Count == 10);
      Contract.Requires(Contract.ForAll(0, primeList.Count, p => primeList[p] > x));
      Contract.Requires(Contract.ForAll<int>(primeList, p => p.IsPrime()));

      return (from p in primeList
              where !p.IsPrime()
              select p).Count() == 0;
    }

This example tests whether the list of primes in the second parameter will only contain the next ten primes greater than the value in the x input parameter. There is a number of preconditions present, all of them checking a bit of the contract. The first ensures the value of x is greater than 0, the second makes sure the list of primes is not null, the third ensures the count of the given list is 10. The fourth is a call to Contract.ForAll, this is the first flavor mentioned above. It iterates from 0 to the count of the primeList, and ensures each value is greater than x. The fifth and last check is using the second flavor of Contract.ForAll, which is the generic form. In this particular case, the type of int could have been inferred from the usage, but I’ve chosen to put it here explicitly to make it clear we’re calling the generic form of Contract.ForAll. It validates that each entry in the list is indeed prime.

Let me note that there is no real world purpose to the example’s method body. It simply confirms that there is no non-prime value in the list, which is already ensured by the method’s contract. As may have become clear from the example, the Contract.ForAll is not restricted to Contract.Ensures calls. It’s not even restricted to any Contract calls. However, be aware of how and when to use this.

The next post I will take the Contract.OldValue<T>() method as the subject. This is a method that is restricted to usage in post conditions. And there is still some ground to cover on the end of the post conditions, as there is also the Contract.ValueAtReturn<T>() method and the other post condition Contract.EnsuresOnThrow<TException> method as well.

That’s it for now
Meile

Feedback

# re: Code Contracts: Contract.ForAll() (7 of n)

2/25/2012 3:51 AM by discount oakley sunglasses
<a href="http://www.sunglasseshop2012.com/">discount oakley sunglasses</a> is a sport and lifestyle brand, driven to ignite the imagination through the fusion of art and science. The essence of the brand is communicated through hundreds of professional and amateur athletes who depend on <a href="http://www.sunglasseshop2012.com/">oakley">http://www.sunglasseshop2012.com/">oakley sunglasses outlet</a> products to provide them with the very best while they redefine what is physically possible.The <a href="http://www.sunglasseshop2012.com/">oakley">http://www.sunglasseshop2012.com/">oakley sunglasses</a> within our Era category represent the most recent technology and freshest new looks to fit your way of life.Since the 1940s sunglasses have been popular as a fashion accessory, especially on the beach.<a href="http://www.sunglasseshop2012.com/oakley-active-sunglasses-c-2.html">Oakley Active Sunglasses</a> that change colours with the ease of a chameleon could become the hottest fashion accessory.

<a href="http://www.sunglasseshop2012.com/new-oakley-sunglasses-c-1.html">Oakley Sunglasses 2012</a> has now been recognized as one of the most coveted brands in performance technology and fashion.<a href="http://www.sunglasseshop2012.com/oakley-lifestyle-sunglasses-c-3.html">Oakley Lifestyle Sunglasses</a> are famous for exceptional top quality technologies and a innovative range of <a href="http://www.sunglasseshop2012.com/oakley-sport-sunglasses-c-6.html">Oakley Sport Sunglasses</a>. But referring to the original one, the price is very high, some of us can not afford it, so <a href="http://www.sunglasseshop2012.com/oakley-multi-lens-sunglasses-c-4.html">Oakley Multi Lens Sunglasses</a> are you the best choice. We have been in wholesale Oakley sunglasses for 6 years, we always offer excellent quality and cheapest <a href="http://www.sunglasseshop2012.com/oakley-polarized-sunglasses-c-5.html">Oakley Polarized Sunglasses</a>.

# sac Lancel

3/2/2012 3:23 AM by sac Lancel
JTYYLHHKXOZDM
Trouver Sacs à main Lancel bon marché fabriqués en cuir de Lancel

# re: Code Contracts: Contract.ForAll() (7 of n)

3/5/2012 9:26 PM by wriying services
There're no doubts that to vuy essay of supreme quality from specialists seems to be great thought for college students who do not ideas how complete their research papers.

# re: Code Contracts: Contract.ForAll() (7 of n)

3/7/2012 9:41 AM by blog commenting services
I used to be frustrated just because of ranking of my site. It used to be really low. Fortunately I found very good blog commenting services that assisted my website a lot. I try to utilize seo continuously now!

# longchamp outlet

3/28/2012 8:18 AM by longchamp wallet
For longchamp online details on the core papers and other statistical characteristics of each Front, see the Research Fronts section of the Essential Science Indicators from Thomson Reuters. Some papers have comments sent in by the author(s) of the paper which may include Longchamp luxury bags images and descriptions of their work. http://www.longchampparis1948.com/

# re: Code Contracts: Contract.ForAll() (7 of n)

4/22/2012 5:07 PM by term paper online
I don’t listen to those who state that it is extremely unsafe to write my research paper in cooperation with services that propose papers from organizations that are ready for use on the Web.

# re: Code Contracts: Contract.ForAll() (7 of n)

5/5/2012 12:11 PM by writing service
I have been worked for the essays online service for several years. I'm a a great paper writer and can help you guys getting term papers of very high quality!

# abercrombie paris

5/9/2012 10:19 AM by abercrombie paris
abercrombie and fitch paris

# re: Code Contracts: Contract.ForAll() (7 of n)

5/10/2012 8:17 PM by research papers buy
Din’t you know that we would provide with students smashing theme. We give you a word that people buy essays for cheap and have the experienced writing services to buy research papers at.

# re: Code Contracts: Contract.ForAll() (7 of n)

5/25/2012 9:46 AM by custom writing
The students all over the world buy the research paper and custom essays at the paper writing service just about this good topic. The students know about the essay writing from the custom writing services.

Post Comment

Title  
Name  
Url
Comment   

ATTENTION: the code you need to copy is CaSe SeNsItIvE and is required to prevent spam.
Enter the code you see:

posts - 24, comments - 125, trackbacks - 0, articles - 0

Copyright © Meile Zetstra