Skip to content

Instantly share code, notes, and snippets.

@scsibug
Created December 14, 2011 23:10
Show Gist options
  • Save scsibug/1479019 to your computer and use it in GitHub Desktop.
Save scsibug/1479019 to your computer and use it in GitHub Desktop.
QuickCheck.Function
import Test.QuickCheck
import Test.QuickCheck.Function
-- mapping any function over reverse of a list ==
-- reversing the map of that function of a list
prop_maprev ∷ [Int] → Fun Int String → Bool
prop_maprev i (Fun _ f) = map f (reverse i) == reverse (map f i)
-- map over any function preserves list size
prop_mapsize ∷ [String] → Fun String String → Bool
prop_mapsize xs (Fun _ f) = length (map f xs) == length xs
-- map preserves order
prop_maporder ∷ [String] → Bool
prop_maporder xs = map id xs == xs
-- filter function == comprehension guard with predicate
prop_filter ∷ [Int] → Fun Int Bool → Bool
prop_filter xs (Fun _ p) = filter p xs == [x | x ← xs, p x]
-- identity applied to any function == that function
prop_id ∷ Int → Fun Int Int → Bool
prop_id i (Fun _ f) = (id ○ f) i == f i
-- any function applied to tuple == that function, curried, with two args
prop_curry ∷ Int → Int → Fun (Int,Int) Int → Bool
prop_curry a b (Fun _ f) = f (a,b) == (curry f) a b
-- example of a failing test (curry doesn't reverse a & b)
prop_curry_fail ∷ Int → Int → Fun (Int,Int) Int → Bool
prop_curry_fail a b (Fun _ f) = f (a,b) == (curry f) b a
main = do
quickCheck prop_maprev
quickCheck prop_mapsize
quickCheck prop_maporder
quickCheck prop_filter
quickCheck prop_id
quickCheck prop_curry
-- quickCheck prop_curry_fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment