8d22c272b81274956c03c7f268dec83ea40a1a97
[packages/base.git] / Debug / QuickCheck / Utils.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Debug.QuickCheck.Utils
4 -- Copyright : (c) Andy Gill 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
6 --
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : portable
10 --
11 -- These are some general purpose utilities for use with QuickCheck.
12 --
13 -----------------------------------------------------------------------------
14
15 module Debug.QuickCheck.Utils
16 ( isAssociativeBy
17 , isAssociative
18 , isCommutableBy
19 , isCommutable
20 , isTotalOrder
21 ) where
22
23 import Prelude
24
25 import Debug.QuickCheck
26
27 isAssociativeBy :: (Show a,Testable prop)
28 => (a -> a -> prop) -> Gen a -> (a -> a -> a) -> Property
29 isAssociativeBy (===) src (**) =
30 forAll src $ \ a ->
31 forAll src $ \ b ->
32 forAll src $ \ c ->
33 ((a ** b) ** c) === (a ** (b ** c))
34
35 isAssociative :: (Arbitrary a,Show a,Eq a) => (a -> a -> a) -> Property
36 isAssociative = isAssociativeBy (==) arbitrary
37
38 isCommutableBy :: (Show a,Testable prop)
39 => (b -> b -> prop) -> Gen a -> (a -> a -> b) -> Property
40 isCommutableBy (===) src (**) =
41 forAll src $ \ a ->
42 forAll src $ \ b ->
43 (a ** b) === (b ** a)
44
45 isCommutable :: (Arbitrary a,Show a,Eq b) => (a -> a -> b) -> Property
46 isCommutable = isCommutableBy (==) arbitrary
47
48 isTotalOrder :: (Arbitrary a,Show a,Ord a) => a -> a -> Property
49 isTotalOrder x y =
50 classify (x > y) "less than" $
51 classify (x == y) "equals" $
52 classify (x < y) "greater than" $
53 x < y || x == y || x > y