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