~ubuntu-branches/ubuntu/saucy/haskell-quickcheck/saucy

« back to all changes in this revision

Viewing changes to Test/QuickCheck/Function.hs

  • Committer: Bazaar Package Importer
  • Author(s): Giovanni Mascellani
  • Date: 2011-05-03 19:38:47 UTC
  • mfrom: (15.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110503193847-1nqtw3k2ekhla2aw
Tags: 2.4.1.1-1
* Team upload.
* New upstream release
* Using Template Haskell only on archtectures that support it.
* Standards-Version bumped to 3.9.1 (no changes required).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
{-# LANGUAGE TypeOperators, GADTs #-}
 
2
-- | Generation of random shrinkable, showable functions.
 
3
-- Not really documented at the moment!
 
4
--
 
5
-- Example of use:
 
6
-- 
 
7
-- >>> :{
 
8
-- >>> let prop :: Fun String Integer -> Bool
 
9
-- >>>     prop (Fun _ f) = f "monkey" == f "banana" || f "banana" == f "elephant"
 
10
-- >>> :}
 
11
-- >>> quickCheck prop
 
12
-- *** Failed! Falsifiable (after 3 tests and 134 shrinks):     
 
13
-- {"elephant"->1, "monkey"->1, _->0}
 
14
--
 
15
-- To generate random values of type @'Fun' a b@,
 
16
-- you must have an instance @'Function' a@.
 
17
-- If your type has a 'Show' instance, you can use 'functionShow' to write the instance; otherwise,
 
18
-- use 'functionMap' to give a bijection between your type and a type that is already an instance of 'Function'.
 
19
-- See the @'Function' [a]@ instance for an example of the latter.
2
20
module Test.QuickCheck.Function
3
21
  ( Fun(..)
4
22
  , apply
12
30
--------------------------------------------------------------------------
13
31
-- imports
14
32
 
15
 
import Test.QuickCheck.Gen
16
33
import Test.QuickCheck.Arbitrary
17
 
import Test.QuickCheck.Property
18
34
import Test.QuickCheck.Poly
19
 
import Test.QuickCheck.Modifiers
20
35
 
21
36
import Data.Char
22
37
import Data.Word
49
64
-- only use this on finite functions
50
65
showFunction :: (Show a, Show b) => (a :-> b) -> Maybe b -> String
51
66
showFunction p md =
52
 
  "{" ++ concat (intersperse "," ( [ show x ++ "->" ++ show c
53
 
                                   | (x,c) <- table p
54
 
                                   ]
55
 
                                ++ [ "_->" ++ show d
56
 
                                   | Just d <- [md]
57
 
                                   ] )) ++ "}"
 
67
  "{" ++ concat (intersperse ", " ( [ show x ++ "->" ++ show c
 
68
                                    | (x,c) <- table p
 
69
                                    ]
 
70
                                 ++ [ "_->" ++ show d
 
71
                                    | Just d <- [md]
 
72
                                    ] )) ++ "}"
58
73
 
59
74
-- turning a concrete function into an abstract function (with a default result)
60
75
abstract :: (a :-> c) -> c -> (a -> c)