[Haskell-cafe] quickCheck problem

Rogan Creswick creswick at gmail.com
Tue Oct 6 20:31:27 UTC 2015


On Tue, Oct 6, 2015 at 1:24 PM, Roelof Wobben <r.wobben at home.nl> wrote:
> Hello,
>
> I have written a function to test if three numbers are the same.
>
> Now I want to test my functions by using quickCheck.
>
> So I thought to test first if all three are the same.
>
> but how can I tell quickcheck that all numbers are the same.

Just have quickcheck generate one number, and use it for all three
arguments, eg:

prop_allSame :: Int -> Bool
prop_allSame x = yourFn x x x

then test the other cases:

prop_different :: Int -> Int -> Int -> Property
prop_different x y z = x /= y && y /= z && x /= z ==> not $ yourFn x y z

That's probably OK for ints, but generally the guard in that style
isn't a great solution, since quickcheck will just keep generating
inputs until the predicate is satisfied.  That can take a while, so
you could also manually offset the first input in various ways:

prop_different :: Int -> Int -> Bool
prop_different x y = not $ yourFn x (x + y) (x - (y + 1)) -- I put a
+1 in here to avoid returning true where y = 0,

There are probably other ways to mutate a randomly generated input for
your problem that may work better, but that's the general idea.

--Rogan

>
> and second question :  hwo do the test likeif two numbers are the same.
>
> I ask this because I try to solve a exercise of the programming Haskell
> book,
> IM have read chapter 3 now.
>
> Roelof
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe


More information about the Haskell-Cafe mailing list