Typo
[packages/base.git] / tests / enum03.hs
1 -- !!! Testing the Word Enum instances.
2 {-# LANGUAGE CPP #-}
3 module Main(main) where
4
5 #if __GLASGOW_HASKELL__ < 705
6 import Prelude hiding (catch)
7 #endif
8 import Control.Exception
9 import Data.Word
10 import Data.Int
11
12 main = do
13 putStrLn "Testing Enum Word8:"
14 testEnumWord8
15 putStrLn "Testing Enum Word16:"
16 testEnumWord16
17 putStrLn "Testing Enum Word32:"
18 testEnumWord32
19 putStrLn "Testing Enum Word64:"
20 testEnumWord64
21
22
23 #define printTest(x) (do{ putStr ( " " ++ "x" ++ " = " ) ; print (x) })
24
25 testEnumWord8 :: IO ()
26 testEnumWord8 = do
27 -- succ
28 printTest ((succ (0::Word8)))
29 printTest ((succ (minBound::Word8)))
30 mayBomb (printTest ((succ (maxBound::Word8))))
31
32 -- pred
33 printTest (pred (1::Word8))
34 printTest (pred (maxBound::Word8))
35 mayBomb (printTest (pred (minBound::Word8)))
36
37 -- toEnum
38 printTest ((map (toEnum::Int->Word8) [1, fromIntegral (minBound::Word8)::Int, fromIntegral (maxBound::Word8)::Int]))
39 mayBomb (printTest ((toEnum (maxBound::Int))::Word8))
40
41 -- fromEnum
42 printTest ((map fromEnum [(1::Word8),minBound,maxBound]))
43
44 -- [x..] aka enumFrom
45 printTest ((take 7 [(1::Word8)..]))
46 printTest ((take 7 [((maxBound::Word8)-5)..])) -- just in case it doesn't catch the upper bound..
47
48 -- [x,y..] aka enumFromThen
49 printTest ((take 7 [(1::Word8),2..]))
50 printTest ((take 7 [(1::Word8),7..]))
51 printTest ((take 7 [(1::Word8),1..]))
52 printTest ((take 7 [(1::Word8),0..]))
53 printTest ((take 7 [(5::Word8),2..]))
54 let x = (minBound::Word8) + 1
55 printTest ((take 7 [x, x-1 ..]))
56 let x = (minBound::Word8) + 5
57 printTest ((take 7 [x, x-1 ..]))
58 let x = (maxBound::Word8) - 5
59 printTest ((take 7 [x, (x+1) ..]))
60
61 -- [x..y] aka enumFromTo
62 printTest ((take 7 ([(1::Word8) .. 5])))
63 printTest ((take 4 ([(1::Word8) .. 1])))
64 printTest ((take 7 ([(1::Word8) .. 0])))
65 printTest ((take 7 ([(5::Word8) .. 0])))
66 printTest ((take 7 ([(maxBound-(5::Word8)) .. maxBound])))
67 printTest ((take 7 ([(minBound+(5::Word8)) .. minBound])))
68
69 -- [x,y..z] aka enumFromThenTo
70 printTest ((take 7 [(5::Word8),4..1]))
71 printTest ((take 7 [(5::Word8),3..1]))
72 printTest ((take 7 [(5::Word8),3..2]))
73 printTest ((take 7 [(1::Word8),2..1]))
74 printTest ((take 7 [(2::Word8),1..2]))
75 printTest ((take 7 [(2::Word8),1..1]))
76 printTest ((take 7 [(2::Word8),3..1]))
77
78 let x = (maxBound::Word8) - 4
79 printTest ((take 7 [x,(x+1)..maxBound]))
80 let x = (minBound::Word8) + 5
81 printTest ((take 7 [x,(x-1)..minBound]))
82
83 testEnumWord16 :: IO ()
84 testEnumWord16 = do
85 -- succ
86 printTest ((succ (0::Word16)))
87 printTest ((succ (minBound::Word16)))
88 mayBomb (printTest ((succ (maxBound::Word16))))
89
90 -- pred
91 printTest (pred (1::Word16))
92 printTest (pred (maxBound::Word16))
93 mayBomb (printTest (pred (minBound::Word16)))
94
95 -- toEnum
96 printTest ((map (toEnum::Int->Word16) [1, fromIntegral (minBound::Word16)::Int, fromIntegral (maxBound::Word16)::Int]))
97 mayBomb (printTest ((toEnum (maxBound::Int))::Word16))
98
99
100 -- fromEnum
101 printTest ((map fromEnum [(1::Word16),minBound,maxBound]))
102
103 -- [x..] aka enumFrom
104 printTest ((take 7 [(1::Word16)..]))
105 printTest ((take 7 [((maxBound::Word16)-5)..])) -- just in case it doesn't catch the upper bound..
106
107 -- [x,y..] aka enumFromThen
108 printTest ((take 7 [(1::Word16),2..]))
109 printTest ((take 7 [(1::Word16),7..]))
110 printTest ((take 7 [(1::Word16),1..]))
111 printTest ((take 7 [(1::Word16),0..]))
112 printTest ((take 7 [(5::Word16),2..]))
113 let x = (minBound::Word16) + 1
114 printTest ((take 7 [x, x-1 ..]))
115 let x = (minBound::Word16) + 5
116 printTest ((take 7 [x, x-1 ..]))
117 let x = (maxBound::Word16) - 5
118 printTest ((take 7 [x, (x+1) ..]))
119
120 -- [x..y] aka enumFromTo
121 printTest ((take 7 ([(1::Word16) .. 5])))
122 printTest ((take 4 ([(1::Word16) .. 1])))
123 printTest ((take 7 ([(1::Word16) .. 0])))
124 printTest ((take 7 ([(5::Word16) .. 0])))
125 printTest ((take 7 ([(maxBound-(5::Word16)) .. maxBound])))
126 printTest ((take 7 ([(minBound+(5::Word16)) .. minBound])))
127
128 -- [x,y..z] aka enumFromThenTo
129 printTest ((take 7 [(5::Word16),4..1]))
130 printTest ((take 7 [(5::Word16),3..1]))
131 printTest ((take 7 [(5::Word16),3..2]))
132 printTest ((take 7 [(1::Word16),2..1]))
133 printTest ((take 7 [(2::Word16),1..2]))
134 printTest ((take 7 [(2::Word16),1..1]))
135 printTest ((take 7 [(2::Word16),3..1]))
136
137 let x = (maxBound::Word16) - 4
138 printTest ((take 7 [x,(x+1)..maxBound]))
139 let x = (minBound::Word16) + 5
140 printTest ((take 7 [x,(x-1)..minBound]))
141
142 testEnumWord32 :: IO ()
143 testEnumWord32 = do
144 -- succ
145 printTest ((succ (0::Word32)))
146 printTest ((succ (minBound::Word32)))
147 mayBomb (printTest ((succ (maxBound::Word32))))
148
149 -- pred
150 printTest (pred (1::Word32))
151 printTest (pred (maxBound::Word32))
152 mayBomb (printTest (pred (minBound::Word32)))
153
154 -- toEnum
155 printTest ((map (toEnum::Int->Word32) [1, fromIntegral (minBound::Word32)::Int, fromIntegral (maxBound::Int32)::Int]))
156 mayBomb (printTest ((toEnum (maxBound::Int))::Word32))
157
158 -- fromEnum
159 printTest ((map fromEnum [(1::Word32),minBound,fromIntegral (maxBound::Int)]))
160 mayBomb (printTest (fromEnum (maxBound::Word32)))
161
162 -- [x..] aka enumFrom
163 printTest ((take 7 [(1::Word32)..]))
164 printTest ((take 7 [((maxBound::Word32)-5)..])) -- just in case it doesn't catch the upper bound..
165
166 -- [x,y..] aka enumFromThen
167 printTest ((take 7 [(1::Word32),2..]))
168 printTest ((take 7 [(1::Word32),7..]))
169 printTest ((take 7 [(1::Word32),1..]))
170 printTest ((take 7 [(1::Word32),0..]))
171 printTest ((take 7 [(5::Word32),2..]))
172 let x = (minBound::Word32) + 1
173 printTest ((take 7 [x, x-1 ..]))
174 let x = (minBound::Word32) + 5
175 printTest ((take 7 [x, x-1 ..]))
176 let x = (maxBound::Word32) - 5
177 printTest ((take 7 [x, (x+1) ..]))
178
179 -- [x..y] aka enumFromTo
180 printTest ((take 7 ([(1::Word32) .. 5])))
181 printTest ((take 4 ([(1::Word32) .. 1])))
182 printTest ((take 7 ([(1::Word32) .. 0])))
183 printTest ((take 7 ([(5::Word32) .. 0])))
184 printTest ((take 7 ([(maxBound-(5::Word32)) .. maxBound])))
185 printTest ((take 7 ([(minBound+(5::Word32)) .. minBound])))
186
187 -- [x,y..z] aka enumFromThenTo
188 printTest ((take 7 [(5::Word32),4..1]))
189 printTest ((take 7 [(5::Word32),3..1]))
190 printTest ((take 7 [(5::Word32),3..2]))
191 printTest ((take 7 [(1::Word32),2..1]))
192 printTest ((take 7 [(2::Word32),1..2]))
193 printTest ((take 7 [(2::Word32),1..1]))
194 printTest ((take 7 [(2::Word32),3..1]))
195
196 let x = (maxBound::Word32) - 4
197 printTest ((take 7 [x,(x+1)..maxBound]))
198 let x = (minBound::Word32) + 5
199 printTest ((take 7 [x,(x-1)..minBound]))
200
201 testEnumWord64 :: IO ()
202 testEnumWord64 = do
203 -- succ
204 printTest ((succ (0::Word64)))
205 printTest ((succ (minBound::Word64)))
206 mayBomb (printTest ((succ (maxBound::Word64))))
207
208 -- pred
209 printTest (pred (1::Word64))
210 printTest (pred (maxBound::Word64))
211 mayBomb (printTest (pred (minBound::Word64)))
212
213 -- toEnum
214 mayBomb (printTest ((map (toEnum::Int->Word64) [1, fromIntegral (minBound::Word64)::Int, maxBound::Int])))
215 mayBomb (printTest ((toEnum (maxBound::Int))::Word64))
216
217 -- fromEnum
218 printTest ((map fromEnum [(1::Word64),minBound,fromIntegral (maxBound::Int)]))
219 mayBomb (printTest (fromEnum (maxBound::Word64)))
220
221 -- [x..] aka enumFrom
222 printTest ((take 7 [(1::Word64)..]))
223 printTest ((take 7 [((maxBound::Word64)-5)..])) -- just in case it doesn't catch the upper bound..
224
225 -- [x,y..] aka enumFromThen
226 printTest ((take 7 [(1::Word64),2..]))
227 printTest ((take 7 [(1::Word64),7..]))
228 printTest ((take 7 [(1::Word64),1..]))
229 printTest ((take 7 [(1::Word64),0..]))
230 printTest ((take 7 [(5::Word64),2..]))
231 let x = (minBound::Word64) + 1
232 printTest ((take 7 [x, x-1 ..]))
233 let x = (minBound::Word64) + 5
234 printTest ((take 7 [x, x-1 ..]))
235 let x = (maxBound::Word64) - 5
236 printTest ((take 7 [x, (x+1) ..]))
237
238 -- [x..y] aka enumFromTo
239 printTest ((take 7 ([(1::Word64) .. 5])))
240 printTest ((take 4 ([(1::Word64) .. 1])))
241 printTest ((take 7 ([(1::Word64) .. 0])))
242 printTest ((take 7 ([(5::Word64) .. 0])))
243 printTest ((take 7 ([(maxBound-(5::Word64)) .. maxBound])))
244 printTest ((take 7 ([(minBound+(5::Word64)) .. minBound])))
245
246 -- [x,y..z] aka enumFromThenTo
247 printTest ((take 7 [(5::Word64),4..1]))
248 printTest ((take 7 [(5::Word64),3..1]))
249 printTest ((take 7 [(5::Word64),3..2]))
250 printTest ((take 7 [(1::Word64),2..1]))
251 printTest ((take 7 [(2::Word64),1..2]))
252 printTest ((take 7 [(2::Word64),1..1]))
253 printTest ((take 7 [(2::Word64),3..1]))
254
255 let x = (maxBound::Word64) - 4
256 printTest ((take 7 [x,(x+1)..maxBound]))
257 let x = (minBound::Word64) + 5
258 printTest ((take 7 [x,(x-1)..minBound]))
259
260
261 --
262 --
263 -- Utils
264 --
265 --
266
267
268 mayBomb x = catch x (\(ErrorCall e) -> putStrLn ("error " ++ show e))
269 `catch` (\e -> putStrLn ("Fail: " ++ show (e :: SomeException)))