]> code.delx.au - offlineimap/commitdiff
Support Word8 in tests
authorJohn Goerzen <jgoerzen@complete.org>
Thu, 29 May 2008 06:18:11 +0000 (01:18 -0500)
committerJohn Goerzen <jgoerzen@complete.org>
Thu, 29 May 2008 06:18:11 +0000 (01:18 -0500)
testsrc/TestInfrastructure.hs

index 45d0f0a18d43733855e04d6f9337c1ad5807a8cf..83b0b84a9b4fdb8e3e41e575fecc242d187e7f1b 100644 (file)
@@ -23,6 +23,7 @@ import qualified Test.HUnit as HU
 import qualified Data.Map as Map
 import System.IO
 import Text.Printf
+import System.Random
 
 (@=?) :: (Eq a, Show a) => a -> a -> Result
 expected @=? actual = 
@@ -45,6 +46,16 @@ instance (Arbitrary k, Arbitrary v, Eq k, Ord k) => Arbitrary (Map.Map k v) wher
            return $ Map.fromList items
     coarbitrary = coarbitrary . Map.keys
 
+instance Arbitrary Word8 where
+    arbitrary = sized $ \n -> choose (0, min (fromIntegral n) maxBound)
+    coarbitrary n = variant (if n >= 0 then 2 * x else 2 * x + 1)
+                where x = abs . fromIntegral $ n
+
+instance Random Word8 where
+    randomR (a, b) g = (\(x, y) -> (fromInteger x, y)) $
+                       randomR (toInteger a, toInteger b) g
+    random g = randomR (minBound, maxBound) g
+
 -- Modified from HUnit
 runVerbTestText :: HU.PutText st -> HU.Test -> IO (HU.Counts, st)
 runVerbTestText (HU.PutText put us) t = do