1 --!!! Testing Read (assuming that Eq, Show and Enum work!)
5 import Ratio(Ratio,(%),Rational)
6 import List(zip4,zip5,zip6,zip7)
8 -- test that expected equality holds
9 tst :: (Read a, Show a, Eq a) => a -> Bool
10 tst x = read (show x) == x
12 -- measure degree of error
13 diff :: (Read a, Show a, Num a) => a -> a
14 diff x = read (show x) - x
16 ----------------------------------------------------------------
17 -- test for hand-written instances
18 ----------------------------------------------------------------
21 test2 = all tst [False,True]
22 test3 = all tst [minBound::Char ..]
23 test4 = all tst [Nothing, Just (Just True)]
24 test5 = all tst [Left True, Right (Just True)]
25 test6 = all tst [LT .. GT]
26 test7 = all tst [[],['a'..'z'],['A'..'Z']]
27 test8 = all tst $ [minBound,maxBound]
29 test9 = all tst $ [(fromInt minBound)-1, (fromInt maxBound)+1]
30 ++ [-100..100 :: Integer]
32 -- we don't test fractional Floats/Doubles because they don't work
33 test10 = all tst $ [-100..100 :: Float]
34 test11 = all tst $ [-100..100 :: Double]
36 test12 = all tst $ [-2%2,-1%2,0%2,1%2,2%2]
37 ++ [-10.0,-9.9..10.0 :: Ratio Int]
38 test13 = all tst $ [-2%2,-1%2,0%2,1%2,2%2]
39 ++ [-10.0,-9.9..10.0 :: Rational]
41 ----------------------------------------------------------------
42 -- test for derived instances
43 ----------------------------------------------------------------
47 test21 = all tst $ [-1..1]
48 test22 = all tst $ zip [-1..1] [-1..1]
49 test23 = all tst $ zip3 [-1..1] [-1..1] [-1..1]
50 test24 = all tst $ zip4 [-1..1] [-1..1] [-1..1] [-1..1]
51 test25 = all tst $ zip5 [-1..1] [-1..1] [-1..1] [-1..1] [-1..1]
52 {- Not derived automatically
53 test26 = all tst $ zip6 [-1..1] [-1..1] [-1..1] [-1..1] [-1..1] [-1..1]
54 test27 = all tst $ zip7 [-1..1] [-1..1] [-1..1] [-1..1] [-1..1] [-1..1] [-1..1]
59 data T1 = C1 | C2 | C3 | C4 | C5 | C6 | C7
60 deriving (Eq, Ord, Enum, Read, Show)
62 test30 = all tst [C1 .. C7]
66 data T2 = A Int | B {x,y::Int, z::Bool} | C Bool
67 deriving (Eq, Read, Show)
69 test31 = all tst [A 1, B 1 2 True, C True]
74 deriving (Eq, Read, Show)
76 test32 = all tst [ T3 i | i <- [-10..10] ]
78 ----------------------------------------------------------------
79 -- Random tests for things which have failed in the past
80 ----------------------------------------------------------------
82 test100 = read "(True)" :: Bool
84 test101 = tst (pi :: Float)
85 test102 = diff (pi :: Float)
87 test103 = tst (pi :: Double)
88 test104 = diff (pi :: Double)