Search code examples
haskellquickcheck

How to quickCheck all possible cases for a type that is both Enum and Bounded?


I have a quickCheck property which involves generating elements for a sum type which has only two elements.

Clearly the default number of test cases, 100, is too many for this case, and so I've used withMaxSuccess to reduce the number of cases to 3. This runs quickly but is not ideal for two reasons. Firstly, the three test cases run are more than the two required. And secondly, the three cases are not comprehensive due to the 1-in-4 chance that all three involve the same element, to the exclusion of the other one.

I have tried QuickCheck's forAll modifier, which seemed like it might do what I am looking for, but the number of test cases run was still 100.

If I have a type with a finite number of elements to be the generator for a QuickCheck test, is there a way to set QuickCheck to test the property comprehensively over the type by running it with each element once?

To qualify whether the type has a finite number of elements, perhaps it can be qualified by both the Enum and Bounded typeclasses.


Solution

  • How about just not using quickcheck?

    > myNeatProperty x = (x == GT) || (x <= EQ)
    > all myNeatProperty [minBound .. maxBound]
    True
    > lessNeatProperty x = x <= EQ
    > all lessNeatProperty [minBound .. maxBound]
    False
    > -- Oh no! Can we have some counterexamples, please?
    > filter (not . lessNeatProperty) [minBound .. maxBound]
    [GT]
    

    For those situations where your space is finite but not Enum and Bounded, consider using universeF in place of [minBound .. maxBound].