1 -- !!! Testing Int and Word
2 module Main(main) where
13 testIntlikeNoBits "Int" (0::Int)
14 testIntlike "Int8" (0::Int8)
15 testIntlike "Int16" (0::Int16)
16 testIntlike "Int32" (0::Int32)
17 testIntlike "Word8" (0::Word8)
18 testIntlike "Word16" (0::Word16)
19 testIntlike "Word32" (0::Word32)
22 testIntlikeNoBits :: (Bounded a, Integral a, Ix a, Read a) => String -> a -> IO ()
23 testIntlikeNoBits name zero = do
24 putStrLn $ "--------------------------------"
25 putStrLn $ "--Testing " ++ name
26 putStrLn $ "--------------------------------"
37 let zero = 0 :: Integer
38 putStrLn $ "--------------------------------"
39 putStrLn $ "--Testing Integer
40 putStrLn $ "--------------------------------"
50 testIntlike :: (Bounded a, Integral a, Ix a, Read a, Bits a) => String -> a -> IO ()
51 testIntlike name zero = do
52 testIntlikeNoBits name zero
56 -- In all these tests, zero is a dummy element used to get
57 -- the overloading to work
60 putStrLn "testBounded"
61 print $ (minBound-1, minBound, minBound+1) `asTypeOf` (zero,zero,zero)
62 print $ (maxBound-1, maxBound, maxBound+1) `asTypeOf` (zero,zero,zero)
66 print $ take 10 [zero .. ] -- enumFrom
67 print $ take 10 [zero, toEnum 2 .. ] -- enumFromThen
68 print [zero .. toEnum 20] -- enumFromTo
69 print [zero, toEnum 2 .. toEnum 20] -- enumFromThenTo
71 samples :: (Num a, Enum a) => a -> ([a], [a])
72 samples zero = ([-3 .. -1]++[0 .. 3], [-3 .. -1]++[1 .. 3])
74 table1 :: (Show a, Show b) => String -> (a -> b) -> [a] -> IO ()
76 sequence [ f' x | x <- xs ]
79 f' x = putStrLn (nm ++ " " ++ show x ++ " = " ++ show (f x))
81 table2 :: (Show a, Show b, Show c) => String -> (a -> b -> c) -> [a] -> [b] -> IO ()
82 table2 nm op xs ys = do
83 sequence [ sequence [ op' x y | y <- ys ] >> putStrLn " "
88 op' x y = putStrLn (show x ++ " " ++ nm ++ " " ++ show y
89 ++ " = " ++ show (op x y))
91 testReadShow zero = do
92 putStrLn "testReadShow"
94 print (map read_show xs)
96 (xs,zs) = samples zero
97 read_show x = (read (show x) `asTypeOf` zero)
101 table2 "==" (==) xs xs
102 table2 "/=" (/=) xs xs
104 (xs,ys) = samples zero
108 table2 "<=" (<=) xs xs
109 table2 "< " (<) xs xs
110 table2 "> " (>) xs xs
111 table2 ">=" (>=) xs xs
112 table2 "`compare`" compare xs xs
114 (xs,ys) = samples zero
121 table1 "negate" negate xs
123 (xs,ys) = samples zero
127 table1 "toRational" toRational xs
129 (xs,ys) = samples zero
131 testIntegral zero = do
132 putStrLn "testIntegral"
133 table2 "`divMod` " divMod xs ys
134 table2 "`div` " div xs ys
135 table2 "`mod` " mod xs ys
136 table2 "`quotRem`" quotRem xs ys
137 table2 "`quot` " quot xs ys
138 table2 "`rem` " rem xs ys
140 (xs,ys) = samples zero
142 testBits zero do_bitsize = do
144 table2 ".&. " (.&.) xs ys
145 table2 ".|. " (.|.) xs ys
146 table2 "`xor`" xor xs ys
147 table1 "complement" complement xs
148 table2 "`shiftL`" shiftL xs ([0..3] ++ [32])
149 table2 "`shiftR`" shiftR xs ([0..3] ++ [32])
150 table2 "`rotate`" rotate xs ([-3..3])
151 table1 "bit" (\ x -> (bit x) `asTypeOf` zero) [(0::Int)..3]
152 table2 "`setBit`" setBit xs ([0..3] ++ [32])
153 table2 "`clearBit`" clearBit xs ([0..3] ++ [32])
154 table2 "`complementBit`" complementBit xs ([0..3] ++ [32])
155 table2 "`testBit`" testBit xs ([0..3] ++ [32])
156 if do_bitsize then table1 "bitSize" bitSize xs else return ()
157 table1 "isSigned" isSigned xs
159 (xs,ys) = samples zero