-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathTestInstance.hs
49 lines (34 loc) · 1.21 KB
/
TestInstance.hs
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
-- Define a set of test instances of common types
-- Portions of this code is taken from "Real World Haskell"
module TestInstance
()
where
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Test.QuickCheck
{-# LANGUAGE CPP #-}
#if MIN_VERSION_random(1,0,1)
-- random>=1.0.1 is exporting these instances, so don't need to redefine it
#else
import Data.Word
import System.Random
integralRandomR :: (Integral a, Integral b, RandomGen g, Num b) => (a, b) -> g -> (b, g)
integralRandomR (a,b) g = case randomR (c,d) g of
(x,h) -> (fromIntegral x, h)
where (c,d) = (fromIntegral a :: Integer,
fromIntegral b :: Integer)
instance Random Word32 where
randomR = integralRandomR
random = randomR (minBound, maxBound)
instance Random Word8 where
randomR = integralRandomR
random = randomR (minBound, maxBound)
#endif
instance Arbitrary L.ByteString where
arbitrary = L.pack `fmap` arbitrary
instance CoArbitrary L.ByteString where
coarbitrary = coarbitrary . L.unpack
instance Arbitrary B.ByteString where
arbitrary = B.pack `fmap` arbitrary
instance CoArbitrary B.ByteString where
coarbitrary = coarbitrary . B.unpack