[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / interpreter / test / runtime / r004.hs
1 --!!! Testing Read (assuming that Eq, Show and Enum work!)
2
3 module TestRead where
4
5 import Ratio(Ratio,(%),Rational)
6 import List(zip4,zip5,zip6,zip7)
7
8 -- test that expected equality holds
9 tst :: (Read a, Show a, Eq a) => a -> Bool
10 tst x = read (show x) == x
11
12 -- measure degree of error
13 diff :: (Read a, Show a, Num a) => a -> a
14 diff x = read (show x) - x
15
16 ----------------------------------------------------------------
17 -- test for hand-written instances
18 ----------------------------------------------------------------
19
20 test1 = tst ()
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] 
28                   ++ [-100..100 :: Int]
29 test9 = all tst $ [(fromInt minBound)-1, (fromInt maxBound)+1]
30                   ++ [-100..100 :: Integer]
31
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]
35
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]
40
41 ----------------------------------------------------------------
42 -- test for derived instances
43 ----------------------------------------------------------------
44
45 -- Tuples
46
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]
55 -}
56
57 -- Enumeration
58
59 data T1 = C1 | C2 | C3 | C4 | C5 | C6 | C7 
60   deriving (Eq, Ord, Enum, Read, Show)
61
62 test30 = all tst [C1 .. C7]
63
64 -- Records
65
66 data T2 = A Int | B {x,y::Int, z::Bool} | C Bool
67   deriving (Eq, Read, Show)
68
69 test31 = all tst [A 1, B 1 2 True, C True]
70
71 -- newtype
72
73 newtype T3 = T3 Int
74   deriving (Eq, Read, Show)
75
76 test32 = all tst [ T3 i | i <- [-10..10] ]
77
78 ----------------------------------------------------------------
79 -- Random tests for things which have failed in the past
80 ----------------------------------------------------------------
81
82 test100 = read "(True)" :: Bool
83
84 test101 = tst  (pi :: Float)
85 test102 = diff (pi :: Float)
86
87 test103 = tst  (pi :: Double)
88 test104 = diff (pi :: Double)
89
90
91