私は Microsoft DevLabs Code Contracts 静的アナライザーを採用しようとしましたが、それが自分なのかそれとも彼らなのか実際にはわからない状況に直面しました。コードは次のとおりです。
public static int GenerateInBetween(int min, int max)
{
Contract.Requires(min < max);
Contract.Requires((long)(max - min) <= (long)(Int32.MaxValue));
Contract.Ensures(Contract.Result<int>() >= min);
Contract.Ensures(Contract.Result<int>() <= max); // Unpvoven!
long range = max - min;
double basicRandom = new Random().NextDouble();
Contract.Assert(basicRandom >= 0.0);
Contract.Assert(basicRandom <= 1.0); // Unpvoven!
double randomDouble = basicRandom * range;
Contract.Assert(randomDouble >= 0.0);
Contract.Assert(randomDouble <= (double)range); // Unpvoven!
int randomInt32 = (int)randomDouble;
Contract.Assert(randomInt32 >= 0);
Contract.Assert(randomInt32 <= range);
return min + randomInt32;
}
静的アナライザーは、コメントされた事後条件とアサートを証明できなかったと主張します。いつそれが間違っているのかわかりませんでした。
編集asserts を asserts に置き換えても、事後条件はまだ証明されていません。