]> code.delx.au - offlineimap/blobdiff - testsrc/TestInfrastructure.hs
Restrict character generation to first 255
[offlineimap] / testsrc / TestInfrastructure.hs
index 0cd54100010dc7923f8612a9e3cf03309596f270..41419bdddb9150b09afb03fba3c6ebe2fdd8d16d 100644 (file)
@@ -26,6 +26,7 @@ import Text.Printf
 import System.Random
 import Data.Word
 import Test.HUnit.Utils
+import Text.ParserCombinators.Parsec
 
 (@=?) :: (Eq a, Show a) => a -> a -> Result
 expected @=? actual = 
@@ -59,8 +60,8 @@ instance Random Word8 where
     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))
+    arbitrary = sized $ \n -> choose (toEnum 0, min (toEnum (n * 0x50)) '\xFF')
+    coarbitrary n = variant (toEnum (2 * x + 1))
                 where x = (abs . fromEnum $ n)::Int
 
 -- Modified from HUnit
@@ -81,10 +82,24 @@ runVerbTestText (HU.PutText put us) t = do
          path' = HU.showPath (HU.path ss)
 
 q :: Testable a => String -> a -> HU.Test
-q = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 10000,
+q = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 20000,
                             configEvery = \_ _ -> ""})
     -- configEvery = testCount for displaying a running test counter
     where testCountBase n = " (test " ++ show n ++ "/250)"
           testCount n _ = testCountBase n ++ 
                           replicate (length (testCountBase n)) '\b'
 
+qverbose :: Testable a => String -> a -> HU.Test
+qverbose = qccheck (defaultConfig {configMaxTest = 250, configMaxFail = 20000,
+                            configEvery = \n args -> show n ++ ":\n" ++ unlines args})
+
+
+{- | Test a parser, forcing it to apply to all input. -}
+p parser input = 
+    case parse parseTest "(none)" input of
+      Left _ -> Nothing
+      Right y -> Just y
+    where parseTest = do r <- parser
+                         eof
+                         return r
+