I am working on an object that encapsulates a bitmap as an array of pixels. The array is one dimensional, and I store the width of the image in a readonly field. I also added a property that calculates the height based the pixel array length and the width. I have the following invariants regarding the height:
- The pixel array (Data property, using the private data field) is not null, and has at least one element.
- The width is larger than zero
The code of the height property:
public int Height
{
[Pure]
get
{
Contract.Requires(Data != null);
Contract.Requires(Width > 0);
Contract.Requires(Data.Length > 0);
Contract.Ensures(Contract.Result<int>() > 0);
Contract.EndContractBlock();
Contract.Assume(Data.Length > Width);
return Data.Length / Width;
}
}
But I can't get the static checker to prove the ensure. The problem might be (as of my understanding), that there is no requirement that Data.Length % Width == 0
, so because of the integer division Data.Length / Width
could be zero; hence I added the assumption with no luck. I am not sure how to hint the static checker to accept my ensure.