Tutorial by Examples

At its simplest, a property is a function which returns a Bool. prop_reverseDoesNotChangeLength xs = length (reverse xs) == length xs A property declares a high-level invariant of a program. The QuickCheck test runner will evaluate the function with 100 random inputs and check that the result is...
The quickCheck function tests a property on 100 random inputs. ghci> quickCheck prop_reverseDoesNotChangeLength +++ OK, passed 100 tests. If a property fails for some input, quickCheck prints out a counterexample. prop_reverseIsAlwaysEmpty xs = reverse xs == [] -- plainly not true for all ...
quickCheckAll is a Template Haskell helper which finds all the definitions in the current file whose name begins with prop_ and tests them. {-# LANGUAGE TemplateHaskell #-} import Test.QuickCheck (quickCheckAll) import Data.List (sort) idempotent :: Eq a => (a -> a) -> a -> Bool ...
The Arbitrary class is for types that can be randomly generated by QuickCheck. The minimal implementation of Arbitrary is the arbitrary method, which runs in the Gen monad to produce a random value. Here is an instance of Arbitrary for the following datatype of non-empty lists. import Test.QuickC...
prop_evenNumberPlusOneIsOdd :: Integer -> Property prop_evenNumberPlusOneIsOdd x = even x ==> odd (x + 1) If you want to check that a property holds given that a precondition holds, you can use the ==> operator. Note that if it's very unlikely for arbitrary inputs to match the precondit...
It can be difficult to test functions with poor asymptotic complexity using quickcheck as the random inputs are not usually size bounded. By adding an upper bound on the size of the input we can still test these expensive functions. import Data.List(permutations) import Test.QuickCheck longRunn...

Page 1 of 1