]> code.delx.au - offlineimap/blobdiff - testsrc/TestInfrastructure.hs
Test checkpointing
[offlineimap] / testsrc / TestInfrastructure.hs
index 41e6ad580357eff74672e0feb8a89bb64b49c7b7..9e230c6d377c44535b19545eec96934cc1441b90 100644 (file)
@@ -23,6 +23,8 @@ import qualified Test.HUnit as HU
 import qualified Data.Map as Map
 import System.IO
 import Text.Printf
+import System.Random
+import Data.Word
 
 (@=?) :: (Eq a, Show a) => a -> a -> Result
 expected @=? actual = 
@@ -36,15 +38,30 @@ expected @=? actual =
 keysToMap :: Ord k => [k] -> Map.Map k ()
 keysToMap = foldl (\map k -> Map.insert k () map) Map.empty
 
-emptymap :: Map.Map Int ()
+emptymap :: (Eq k, Ord k, Show v) => Map.Map k v
 emptymap = Map.empty
 
-instance (Arbitrary k, Eq k, Ord k) => Arbitrary (Map.Map k ()) where
+instance (Arbitrary k, Arbitrary v, Eq k, Ord k) => Arbitrary (Map.Map k v) where
     arbitrary = 
         do items <- arbitrary
-           return $ keysToMap items
+           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
+
+instance Arbitrary Char where
+    arbitrary = sized $ \n -> choose (toEnum 0, min (toEnum n) maxBound)
+    coarbitrary n = variant (if (fromEnum n) >= 0 then toEnum (2 * x) else toEnum (2 * x + 1))
+                where x = (abs . fromEnum $ n)::Int
+
 -- Modified from HUnit
 runVerbTestText :: HU.PutText st -> HU.Test -> IO (HU.Counts, st)
 runVerbTestText (HU.PutText put us) t = do