summaryrefslogtreecommitdiff
path: root/libraries/base/Debug/QuickCheck/Utils.hs
blob: 112c3ca06006be0d440aaa39220f0c12dfd201d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
-----------------------------------------------------------------------------
-- |
-- Module      :  Debug.QuickCheck.Utils
-- Copyright   :  (c) Andy Gill 2001
-- License     :  BSD-style (see the file libraries/base/LICENSE)
-- 
-- Maintainer  :  libraries@haskell.org
-- Stability   :  experimental
-- Portability :  portable
--
-- These are some general purpose utilities for use with QuickCheck.
--
-----------------------------------------------------------------------------

module Debug.QuickCheck.Utils
  ( isAssociativeBy
  , isAssociative
  , isCommutableBy
  , isCommutable
  , isTotalOrder
  ) where

import Prelude

import Debug.QuickCheck

isAssociativeBy :: (Show a,Testable prop) 
		=> (a -> a -> prop) -> Gen a -> (a -> a -> a) -> Property
isAssociativeBy (===) src (**) = 
     	forAll src $ \ a ->
     	forAll src $ \ b ->
     	forAll src $ \ c ->
	((a ** b) ** c) === (a ** (b ** c))

isAssociative :: (Arbitrary a,Show a,Eq a) => (a -> a -> a) -> Property
isAssociative = isAssociativeBy (==) arbitrary

isCommutableBy :: (Show a,Testable prop) 
	       => (b -> b -> prop) -> Gen a -> (a -> a -> b) -> Property
isCommutableBy (===) src (**) =
	forAll src $ \ a ->
	forAll src $ \ b ->
	(a ** b) === (b ** a)

isCommutable :: (Arbitrary a,Show a,Eq b) => (a -> a -> b) -> Property
isCommutable = isCommutableBy (==) arbitrary

isTotalOrder :: (Arbitrary a,Show a,Ord a) => a -> a -> Property
isTotalOrder x y = 
    classify (x > y)  "less than" $
    classify (x == y) "equals" $
    classify (x < y)  "greater than" $
    x < y || x == y || x > y