]> code.delx.au - offlineimap/blob - testsrc/TestInfrastructure.hs
Switched tests to Word8
[offlineimap] / testsrc / TestInfrastructure.hs
1 {-
2 Copyright (C) 2002-2008 John Goerzen <jgoerzen@complete.org>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 -}
18
19 module TestInfrastructure where
20 import Test.QuickCheck
21 import Test.QuickCheck.Batch
22 import qualified Test.HUnit as HU
23 import qualified Data.Map as Map
24 import System.IO
25 import Text.Printf
26 import System.Random
27 import Data.Word
28
29 (@=?) :: (Eq a, Show a) => a -> a -> Result
30 expected @=? actual =
31 Result {ok = Just (expected == actual),
32 arguments = ["Result: expected " ++ show expected ++ ", got " ++ show actual],
33 stamp = []}
34
35 (@?=) :: (Eq a, Show a) => a -> a -> Result
36 (@?=) = flip (@=?)
37
38 keysToMap :: Ord k => [k] -> Map.Map k ()
39 keysToMap = foldl (\map k -> Map.insert k () map) Map.empty
40
41 emptymap :: (Eq k, Ord k, Show v) => Map.Map k v
42 emptymap = Map.empty
43
44 instance (Arbitrary k, Arbitrary v, Eq k, Ord k) => Arbitrary (Map.Map k v) where
45 arbitrary =
46 do items <- arbitrary
47 return $ Map.fromList items
48 coarbitrary = coarbitrary . Map.keys
49
50 instance Arbitrary Word8 where
51 arbitrary = sized $ \n -> choose (0, min (fromIntegral n) maxBound)
52 coarbitrary n = variant (if n >= 0 then 2 * x else 2 * x + 1)
53 where x = abs . fromIntegral $ n
54
55 instance Random Word8 where
56 randomR (a, b) g = (\(x, y) -> (fromInteger x, y)) $
57 randomR (toInteger a, toInteger b) g
58 random g = randomR (minBound, maxBound) g
59
60 -- Modified from HUnit
61 runVerbTestText :: HU.PutText st -> HU.Test -> IO (HU.Counts, st)
62 runVerbTestText (HU.PutText put us) t = do
63 (counts, us') <- HU.performTest reportStart reportError reportFailure us t
64 us'' <- put (HU.showCounts counts) True us'
65 return (counts, us'')
66 where
67 reportStart ss us = do hPrintf stderr "\rTesting %-68s\n" (HU.showPath (HU.path ss))
68 put (HU.showCounts (HU.counts ss)) False us
69 reportError = reportProblem "Error:" "Error in: "
70 reportFailure = reportProblem "Failure:" "Failure in: "
71 reportProblem p0 p1 msg ss us = put line True us
72 where line = "### " ++ kind ++ path' ++ '\n' : msg
73 kind = if null path' then p0 else p1
74 path' = HU.showPath (HU.path ss)
75