[Haskell-beginners] Quickcheck Preconditions

Daniel Fischer daniel.is.fischer at googlemail.com
Wed May 25 13:04:05 CEST 2011


On Wednesday 25 May 2011 12:40:20, Guy wrote:
> I have some functions with preconditions. For example, foo::[a]->IO Bool
> requires a list of at least three elements, and bar::[a]->Int->IO ()
> requires that its second argument is some position within the list.
> 
> How can I test these with quickcheck? If I use ==>, quickcheck gives up
> with very few test cases.
> 

You could increase the maxDiscard value,

ghci> quickCheckWith stdArgs{ maxDiscard = 5000 } (forAll arbitrary (\xs -> 
(length xs > 2) ==> whatever))

or create a generator that produces only output fulfilling the 
preconditions:

atLeast3 :: Arbitrary a => Gen [a]
atLeast3 = do
    x <- arbitrary
    y <- arbitrary
    z <- arbitrary
    ws <- arbitrary
    return (x:y:z:ws)

ghci> quickCheck (forAll atLeast3 whatever)



More information about the Beginners mailing list