@@ -2,6 +2,7 @@
22
33module QuickCheckUtils where
44
5+import Control.Arrow (first)
56import Data.Int (Int64)
67import Data.Word (Word8, Word16, Word32)
78import qualified Data.Text as T
@@ -10,11 +11,6 @@ import System.Random (Random(..), RandomGen)
1011import Test.QuickCheck (Arbitrary(..), choose, oneof, sized, variant, vector)
1112import qualified Data.ByteString as B
1213
13−integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
14−integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
15− fromIntegral b :: Integer) g of
16− (x,h) -> (fromIntegral x, h)
17−
1814instance Random Int64 where
1915 randomR = integralRandomR
2016 random = randomR (minBound,maxBound)
@@ -84,3 +80,49 @@ instance Arbitrary (NotEmpty TL.Text) where
8480instance Arbitrary (NotEmpty B.ByteString) where
8581 arbitrary = (fmap B.pack) `fmap` arbitrary
8682 coarbitrary = coarbitrary . notEmpty
83+
84+data 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)
89+
90+small :: Small -> Int
91+small = fromEnum
92+
93+intf f a b = toEnum ((fromEnum a `f` fromEnum b) `mod` 32)
94+
95+instance Show Small where
96+ show = show . fromEnum
97+
98+instance Read Small where
99+ readsPrec n = map (first toEnum) . readsPrec n
100+
101+instance Num Small where
102+ fromInteger = toEnum . fromIntegral
103+ signum _ = 1
104+ abs = id
105+ (+) = intf (+)
106+ (-) = intf (-)
107+ (*) = intf (*)
108+
109+instance Real Small where
110+ toRational = toRational . fromEnum
111+
112+instance Integral Small where
113+ toInteger = toInteger . fromEnum
114+ quotRem a b = (toEnum x, toEnum y)
115+ where (x, y) = fromEnum a `quotRem` fromEnum b
116+
117+instance Random Small where
118+ randomR = integralRandomR
119+ random = randomR (minBound,maxBound)
120+
121+instance Arbitrary Small where
122+ arbitrary = choose (minBound,maxBound)
123+ coarbitrary c = variant (fromEnum c `rem` 4)
124+
125+integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
126+integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
127+ fromIntegral b :: Integer) g of
128+ (x,h) -> (fromIntegral x, h)