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)
21 testIntlikeNoBits :: (Bounded a, Integral a, Ix a, Read a) => String -> a -> IO ()
22 testIntlikeNoBits name zero = do
23 putStrLn $ "--------------------------------"
24 putStrLn $ "--Testing " ++ name
25 putStrLn $ "--------------------------------"
36 testIntlike :: (Bounded a, Integral a, Ix a, Read a, Bits a) => String -> a -> IO ()
37 testIntlike name zero = do
38 testIntlikeNoBits name zero
42 -- In all these tests, zero is a dummy element used to get
43 -- the overloading to work
46 putStrLn "testBounded"
47 print $ (minBound-1, minBound, minBound+1) `asTypeOf` (zero,zero,zero)
48 print $ (maxBound-1, maxBound, maxBound+1) `asTypeOf` (zero,zero,zero)
52 print $ take 10 [zero .. ] -- enumFrom
53 print $ take 10 [zero, toEnum 2 .. ] -- enumFromThen
54 print [zero .. toEnum 20] -- enumFromTo
55 print [zero, toEnum 2 .. toEnum 20] -- enumFromThenTo
57 samples :: (Num a, Enum a) => a -> ([a], [a])
58 samples zero = ([-3 .. -1]++[0 .. 3], [-3 .. -1]++[1 .. 3])
60 table1 :: (Show a, Show b) => String -> (a -> b) -> [a] -> IO ()
62 sequence [ f' x | x <- xs ]
65 f' x = putStrLn (nm ++ " " ++ show x ++ " = " ++ show (f x))
67 table2 :: (Show a, Show b, Show c) => String -> (a -> b -> c) -> [a] -> [b] -> IO ()
68 table2 nm op xs ys = do
69 sequence [ sequence [ op' x y | y <- ys ] >> putStrLn " "
74 op' x y = putStrLn (show x ++ " " ++ nm ++ " " ++ show y
75 ++ " = " ++ show (op x y))
77 testReadShow zero = do
78 putStrLn "testReadShow"
80 print (map read_show xs)
82 (xs,zs) = samples zero
83 read_show x = (read (show x) `asTypeOf` zero)
87 table2 "==" (==) xs xs
88 table2 "/=" (/=) xs xs
90 (xs,ys) = samples zero
94 table2 "<=" (<=) xs xs
97 table2 ">=" (>=) xs xs
98 table2 "`compare`" compare xs xs
100 (xs,ys) = samples zero
107 table1 "negate" negate xs
109 (xs,ys) = samples zero
113 table1 "toRational" toRational xs
115 (xs,ys) = samples zero
117 testIntegral zero = do
118 putStrLn "testIntegral"
119 table2 "`divMod` " divMod xs ys
120 table2 "`div` " div xs ys
121 table2 "`mod` " mod xs ys
122 table2 "`quotRem`" quotRem xs ys
123 table2 "`quot` " quot xs ys
124 table2 "`rem` " rem xs ys
126 (xs,ys) = samples zero
130 table2 ".&. " (.&.) xs ys
131 table2 ".|. " (.|.) xs ys
132 table2 "`xor`" xor xs ys
133 table1 "complement" complement xs
134 table2 "`shiftL`" shiftL xs ([0..3] ++ [32])
135 table2 "`shiftR`" shiftR xs ([0..3] ++ [32])
136 table2 "`rotate`" rotate xs ([-3..3])
137 table1 "bit" (\ x -> (bit x) `asTypeOf` zero) [(0::Int)..3]
138 table2 "`setBit`" setBit xs ([0..3] ++ [32])
139 table2 "`clearBit`" clearBit xs ([0..3] ++ [32])
140 table2 "`complementBit`" complementBit xs ([0..3] ++ [32])
141 table2 "`testBit`" testBit xs ([0..3] ++ [32])
142 table1 "bitSize" bitSize xs
143 table1 "isSigned" isSigned xs
145 (xs,ys) = samples zero