0

I am playing with FsCheck. And it fails on

0:
[|-1; 1|]
shrink:
[|-1; 0|]
shrink:
[|1; 0|]
shrink:
[|0; 0|]
Falsifiable, after 1 test (3 shrinks) (StdGen (1052297207,296308070)):
Label of failing property: original (0,0) first sum 0, second 0
Original:
[|-1; 1|]
Shrunk:
[|0; 0|]

Why? Two operations ArrayHelpers.SumOfElements(original) and original.Sum() return the same result but iy fails.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Prop.ForAll<int[]>(original =>
            {
                var firstSum = ArrayHelpers.SumOfElements(original);
                return (!(original.Sum() == firstSum)).When(original.Count() > 1).Label($"original ({string.Join(",", original)}) first sum {firstSum}, second {original.Sum()}");
            }).VerboseCheck();
        }
    }


    public static class ArrayHelpers
    {
        public static int SumOfElements(int[] array)
        {
            int sum = 0;
            for (int i = 0; i < array.Count(); i++)
            {
                sum += array[i];
            }
            return sum;
        }
    }
}
A191919
  • 3,422
  • 7
  • 49
  • 93
  • Your property asserts they should be different: `!(original.Sum() == firstSum)`. – Lee May 29 '17 at 13:18
  • @Lee, I thought that Prop should find cases when sum's is differrent. But "sum 0, second 0" they are the same. – A191919 May 29 '17 at 13:32
  • Your property should hold for arbitrary `int[]` values generated by the generator. FsCheck generates random values to check the property with and fails if it fails to hold for any of them. The property you're trying to check is `original.Sum() == ArrayHelpers.SumOfElements(orginal)`, but your code negates the condition. – Lee May 29 '17 at 13:41

0 Answers0