enum02.hs 8.24 KB
Newer Older
1 2 3
-- !!! Testing the Int Enum instances.
module Main(main) where

4
import Control.Exception
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
import Int

main = do
  putStrLn "Testing Enum Int8:"
  testEnumInt8
  putStrLn "Testing Enum Int16:"
  testEnumInt16
  putStrLn "Testing Enum Int32:"
  testEnumInt32
  putStrLn "Testing Enum Int64:"
  testEnumInt64

#define printTest(x) (do{ putStr ( "    " ++ "x" ++ " = " ) ; print (x) })

testEnumInt8 :: IO ()
testEnumInt8 = do
     -- succ
  printTest ((succ (0::Int8)))
  printTest ((succ (minBound::Int8)))
  mayBomb   (printTest ((succ (maxBound::Int8))))

     -- pred
  printTest (pred (1::Int8))
  printTest (pred (maxBound::Int8))
  mayBomb   (printTest (pred (minBound::Int8))) 

     -- toEnum
32
  printTest ((map (toEnum::Int->Int8) [1, fromIntegral (minBound::Int8), fromIntegral (maxBound::Int8)]))
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  mayBomb   (printTest ((toEnum (maxBound::Int))::Int8))

     -- fromEnum
  printTest ((map fromEnum [(1::Int8),minBound,maxBound]))

     -- [x..] aka enumFrom
  printTest ((take 7 [(1::Int8)..]))
  printTest ((take 7 [((maxBound::Int8)-5)..])) -- just in case it doesn't catch the upper bound..
  
     -- [x,y..] aka enumFromThen
  printTest ((take 7 [(1::Int8),2..]))
  printTest ((take 7 [(1::Int8),7..]))
  printTest ((take 7 [(1::Int8),1..]))
  printTest ((take 7 [(1::Int8),0..]))
  printTest ((take 7 [(5::Int8),2..]))
  let x = (minBound::Int8) + 1
  printTest ((take 7 [x, x-1 ..]))
  let x = (minBound::Int8) + 5
  printTest ((take 7 [x, x-1 ..]))
  let x = (maxBound::Int8) - 5
  printTest ((take 7 [x, (x+1) ..]))

     -- [x..y] aka enumFromTo
  printTest ((take 7 ([(1::Int8) .. 5])))
  printTest ((take 4 ([(1::Int8) .. 1])))
  printTest ((take 7 ([(1::Int8) .. 0])))
  printTest ((take 7 ([(5::Int8) .. 0])))
  printTest ((take 7 ([(maxBound-(5::Int8)) .. maxBound])))
  printTest ((take 7 ([(minBound+(5::Int8)) .. minBound])))

     -- [x,y..z] aka enumFromThenTo
  printTest ((take 7 [(5::Int8),4..1]))
  printTest ((take 7 [(5::Int8),3..1]))
  printTest ((take 7 [(5::Int8),3..2]))
  printTest ((take 7 [(1::Int8),2..1]))
  printTest ((take 7 [(2::Int8),1..2]))
  printTest ((take 7 [(2::Int8),1..1]))
  printTest ((take 7 [(2::Int8),3..1]))

  let x = (maxBound::Int8) - 4
  printTest ((take 7 [x,(x+1)..maxBound]))
  let x = (minBound::Int8) + 5
  printTest ((take 7 [x,(x-1)..minBound]))

testEnumInt16 :: IO ()
testEnumInt16 = do
     -- succ
  printTest ((succ (0::Int16)))
  printTest ((succ (minBound::Int16)))
  mayBomb   (printTest ((succ (maxBound::Int16))))

     -- pred
  printTest (pred (1::Int16))
  printTest (pred (maxBound::Int16))
  mayBomb   (printTest (pred (minBound::Int16))) 

     -- toEnum
90
  printTest ((map (toEnum::Int->Int16) [1, fromIntegral (minBound::Int16), fromIntegral (maxBound::Int16)]))
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  mayBomb   (printTest ((toEnum (maxBound::Int))::Int16))


     -- fromEnum
  printTest ((map fromEnum [(1::Int16),minBound,maxBound]))

     -- [x..] aka enumFrom
  printTest ((take 7 [(1::Int16)..]))
  printTest ((take 7 [((maxBound::Int16)-5)..])) -- just in case it doesn't catch the upper bound..
  
     -- [x,y..] aka enumFromThen
  printTest ((take 7 [(1::Int16),2..]))
  printTest ((take 7 [(1::Int16),7..]))
  printTest ((take 7 [(1::Int16),1..]))
  printTest ((take 7 [(1::Int16),0..]))
  printTest ((take 7 [(5::Int16),2..]))
  let x = (minBound::Int16) + 1
  printTest ((take 7 [x, x-1 ..]))
  let x = (minBound::Int16) + 5
  printTest ((take 7 [x, x-1 ..]))
  let x = (maxBound::Int16) - 5
  printTest ((take 7 [x, (x+1) ..]))

     -- [x..y] aka enumFromTo
  printTest ((take 7 ([(1::Int16) .. 5])))
  printTest ((take 4 ([(1::Int16) .. 1])))
  printTest ((take 7 ([(1::Int16) .. 0])))
  printTest ((take 7 ([(5::Int16) .. 0])))
  printTest ((take 7 ([(maxBound-(5::Int16)) .. maxBound])))
  printTest ((take 7 ([(minBound+(5::Int16)) .. minBound])))

     -- [x,y..z] aka enumFromThenTo
  printTest ((take 7 [(5::Int16),4..1]))
  printTest ((take 7 [(5::Int16),3..1]))
  printTest ((take 7 [(5::Int16),3..2]))
  printTest ((take 7 [(1::Int16),2..1]))
  printTest ((take 7 [(2::Int16),1..2]))
  printTest ((take 7 [(2::Int16),1..1]))
  printTest ((take 7 [(2::Int16),3..1]))

  let x = (maxBound::Int16) - 4
  printTest ((take 7 [x,(x+1)..maxBound]))
  let x = (minBound::Int16) + 5
  printTest ((take 7 [x,(x-1)..minBound]))

testEnumInt32 :: IO ()
testEnumInt32 = do
     -- succ
  printTest ((succ (0::Int32)))
  printTest ((succ (minBound::Int32)))
  mayBomb   (printTest ((succ (maxBound::Int32))))

     -- pred
  printTest (pred (1::Int32))
  printTest (pred (maxBound::Int32))
  mayBomb   (printTest (pred (minBound::Int32))) 

     -- toEnum
149
  printTest ((map (toEnum::Int->Int32) [1, fromIntegral (minBound::Int32), fromIntegral (maxBound::Int32)]))
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
  mayBomb   (printTest ((toEnum (maxBound::Int))::Int32))

     -- fromEnum
  printTest ((map fromEnum [(1::Int32),minBound,maxBound]))

     -- [x..] aka enumFrom
  printTest ((take 7 [(1::Int32)..]))
  printTest ((take 7 [((maxBound::Int32)-5)..])) -- just in case it doesn't catch the upper bound..
  
     -- [x,y..] aka enumFromThen
  printTest ((take 7 [(1::Int32),2..]))
  printTest ((take 7 [(1::Int32),7..]))
  printTest ((take 7 [(1::Int32),1..]))
  printTest ((take 7 [(1::Int32),0..]))
  printTest ((take 7 [(5::Int32),2..]))
  let x = (minBound::Int32) + 1
  printTest ((take 7 [x, x-1 ..]))
  let x = (minBound::Int32) + 5
  printTest ((take 7 [x, x-1 ..]))
  let x = (maxBound::Int32) - 5
  printTest ((take 7 [x, (x+1) ..]))

     -- [x..y] aka enumFromTo
  printTest ((take 7 ([(1::Int32) .. 5])))
  printTest ((take 4 ([(1::Int32) .. 1])))
  printTest ((take 7 ([(1::Int32) .. 0])))
  printTest ((take 7 ([(5::Int32) .. 0])))
  printTest ((take 7 ([(maxBound-(5::Int32)) .. maxBound])))
  printTest ((take 7 ([(minBound+(5::Int32)) .. minBound])))

     -- [x,y..z] aka enumFromThenTo
  printTest ((take 7 [(5::Int32),4..1]))
  printTest ((take 7 [(5::Int32),3..1]))
  printTest ((take 7 [(5::Int32),3..2]))
  printTest ((take 7 [(1::Int32),2..1]))
  printTest ((take 7 [(2::Int32),1..2]))
  printTest ((take 7 [(2::Int32),1..1]))
  printTest ((take 7 [(2::Int32),3..1]))

  let x = (maxBound::Int32) - 4
  printTest ((take 7 [x,(x+1)..maxBound]))
  let x = (minBound::Int32) + 5
  printTest ((take 7 [x,(x-1)..minBound]))

testEnumInt64 :: IO ()
testEnumInt64 = do
     -- succ
  printTest ((succ (0::Int64)))
  printTest ((succ (minBound::Int64)))
  mayBomb   (printTest ((succ (maxBound::Int64))))

     -- pred
  printTest (pred (1::Int64))
  printTest (pred (maxBound::Int64))
  mayBomb   (printTest (pred (minBound::Int64))) 

     -- toEnum
207
  printTest ((map (toEnum::Int->Int64) [1, fromIntegral (minBound::Int64), fromIntegral (maxBound::Int64)]))
208 209 210
  mayBomb   (printTest ((toEnum (maxBound::Int))::Int64))

     -- fromEnum
211
  printTest ((map fromEnum [(1::Int64),fromIntegral (minBound::Int) ,fromIntegral (maxBound::Int)]))
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  mayBomb   (printTest (fromEnum (maxBound::Int64)))

     -- [x..] aka enumFrom
  printTest ((take 7 [(1::Int64)..]))
  printTest ((take 7 [((maxBound::Int64)-5)..])) -- just in case it doesn't catch the upper bound..

     -- [x,y..] aka enumFromThen
  printTest ((take 7 [(1::Int64),2..]))
  printTest ((take 7 [(1::Int64),7..]))
  printTest ((take 7 [(1::Int64),1..]))
  printTest ((take 7 [(1::Int64),0..]))
  printTest ((take 7 [(5::Int64),2..]))
  let x = (minBound::Int64) + 1
  printTest ((take 7 [x, x-1 ..]))
  let x = (minBound::Int64) + 5
  printTest ((take 7 [x, x-1 ..]))
  let x = (maxBound::Int64) - 5
  printTest ((take 7 [x, (x+1) ..]))

     -- [x..y] aka enumFromTo
  printTest ((take 7 ([(1::Int64) .. 5])))
  printTest ((take 4 ([(1::Int64) .. 1])))
  printTest ((take 7 ([(1::Int64) .. 0])))
  printTest ((take 7 ([(5::Int64) .. 0])))
  printTest ((take 7 ([(maxBound-(5::Int64)) .. maxBound])))
  printTest ((take 7 ([(minBound+(5::Int64)) .. minBound])))

     -- [x,y..z] aka enumFromThenTo
  printTest ((take 7 [(5::Int64),4..1]))
  printTest ((take 7 [(5::Int64),3..1]))
  printTest ((take 7 [(5::Int64),3..2]))
  printTest ((take 7 [(1::Int64),2..1]))
  printTest ((take 7 [(2::Int64),1..2]))
  printTest ((take 7 [(2::Int64),1..1]))
  printTest ((take 7 [(2::Int64),3..1]))

  let x = (maxBound::Int64) - 4
  printTest ((take 7 [x,(x+1)..maxBound]))
  let x = (minBound::Int64) + 5
  printTest ((take 7 [x,(x-1)..minBound]))


--
--
--  Utils
--
--


mayBomb x = catchJust errorCalls x (\e -> putStrLn ("error " ++ show e))