on e91c827 help sign in
text/tests/QuickCheckUtils.hs
128 lines 4.0 kB history raw
1{-# LANGUAGE FlexibleInstances #-}
3module QuickCheckUtils where
5import Control.Arrow (first)
6import Data.Int (Int64)
7import Data.Word (Word8, Word16, Word32)
8import qualified Data.Text as T
9import qualified Data.Text.Lazy as TL
10import System.Random (Random(..), RandomGen)
11import Test.QuickCheck (Arbitrary(..), choose, oneof, sized, variant, vector)
12import qualified Data.ByteString as B
14instance Random Int64 where
15 randomR = integralRandomR
16 random = randomR (minBound,maxBound)
18instance Arbitrary Int64 where
19 arbitrary = choose (minBound,maxBound)
20 coarbitrary c = variant (fromEnum c `rem` 4)
22instance Random Word8 where
23 randomR = integralRandomR
24 random = randomR (minBound,maxBound)
26instance Arbitrary Word8 where
27 arbitrary = choose (minBound,maxBound)
28 coarbitrary c = variant (fromEnum c `rem` 4)
30instance Arbitrary B.ByteString where
31 arbitrary = B.pack `fmap` arbitrary
32 coarbitrary s = coarbitrary (B.unpack s)
34instance Random Word16 where
35 randomR = integralRandomR
36 random = randomR (minBound,maxBound)
38instance Arbitrary Word16 where
39 arbitrary = choose (minBound,maxBound)
40 coarbitrary c = variant (fromEnum c `rem` 4)
42instance Random Word32 where
43 randomR = integralRandomR
44 random = randomR (minBound,maxBound)
46instance Arbitrary Word32 where
47 arbitrary = choose (minBound,maxBound)
48 coarbitrary c = variant (fromEnum c `rem` 4)
50instance Arbitrary Char where
51 arbitrary = oneof [choose ('\0','\55295'), choose ('\57344','\1114111')]
52 coarbitrary c = variant (fromEnum c `rem` 4)
54instance Arbitrary T.Text where
55 arbitrary = T.pack `fmap` arbitrary
56 coarbitrary s = coarbitrary (T.unpack s)
58instance Arbitrary TL.Text where
59 arbitrary = TL.pack `fmap` arbitrary
60 coarbitrary s = coarbitrary (TL.unpack s)
62newtype NotEmpty a = NotEmpty { notEmpty :: a }
63 deriving (Eq, Ord, Show)
65instance Functor NotEmpty where
66 fmap f (NotEmpty a) = NotEmpty (f a)
68instance Arbitrary a => Arbitrary (NotEmpty [a]) where
69 arbitrary = sized (\n -> NotEmpty `fmap` (choose (1,n+1) >>= vector))
70 coarbitrary = coarbitrary . notEmpty
72instance Arbitrary (NotEmpty T.Text) where
73 arbitrary = (fmap T.pack) `fmap` arbitrary
74 coarbitrary = coarbitrary . notEmpty
76instance Arbitrary (NotEmpty TL.Text) where
77 arbitrary = (fmap TL.pack) `fmap` arbitrary
78 coarbitrary = coarbitrary . notEmpty
80instance Arbitrary (NotEmpty B.ByteString) where
81 arbitrary = (fmap B.pack) `fmap` arbitrary
82 coarbitrary = coarbitrary . notEmpty
84data Small = S0 | S1 | S2 | S3 | S4 | S5 | S6 | S7
85 | S8 | S9 | S10 | S11 | S12 | S13 | S14 | S15
86 | S16 | S17 | S18 | S19 | S20 | S21 | S22 | S23
87 | S24 | S25 | S26 | S27 | S28 | S29 | S30 | S31
88 deriving (Eq, Ord, Enum, Bounded)
90small :: Small -> Int
91small = fromEnum
93intf f a b = toEnum ((fromEnum a `f` fromEnum b) `mod` 32)
95instance Show Small where
96 show = show . fromEnum
98instance Read Small where
99 readsPrec n = map (first toEnum) . readsPrec n
101instance Num Small where
102 fromInteger = toEnum . fromIntegral
103 signum _ = 1
104 abs = id
105 (+) = intf (+)
106 (-) = intf (-)
107 (*) = intf (*)
109instance Real Small where
110 toRational = toRational . fromEnum
112instance Integral Small where
113 toInteger = toInteger . fromEnum
114 quotRem a b = (toEnum x, toEnum y)
115 where (x, y) = fromEnum a `quotRem` fromEnum b
117instance Random Small where
118 randomR = integralRandomR
119 random = randomR (minBound,maxBound)
121instance Arbitrary Small where
122 arbitrary = choose (minBound,maxBound)
123 coarbitrary c = variant (fromEnum c `rem` 4)
125integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
126integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
127 fromIntegral b :: Integer) g of
128 (x,h) -> (fromIntegral x, h)