f9f9ec778e8da91267983605559b9a57e5919ee6
[ghc.git] / testsuite / tests / numeric / should_run / arith012.hs
1 -- !!! Testing NumExts
2 module Main(main) where
3
4 import Numeric
5 import Data.Char
6
7 main :: IO ()
8 main = tst
9
10 tst :: IO ()
11 tst = do
12 test_doubleToFloat
13 test_floatToDouble
14 test_showHex
15 test_showOct
16 test_showBin
17
18 ----
19 -- Test data:
20 doubles :: [Double]
21 doubles = [ -1.2 , 0, 0.1, 0.5, 1.0, 1234.45454,
22 1.6053e4, 1.64022e12, 6.894e-4, 6.34543455634582173,
23 5342413403.40540423255]
24 ints :: [Int]
25 ints = [ 0, 1, 255, 65513, 6029, 1024, 256, 201357245]
26
27 integers :: [Integer]
28 integers = [ 0, 1, 255, 65513, 6029, 1024, 256,
29 2343243543500233, 656194962055457832]
30 ---
31
32 test_doubleToFloat :: IO ()
33 test_doubleToFloat = do
34 test_banner "doubleToFloat"
35 putStrLn (show doubles)
36 putStrLn (show $ map doubleToFloat doubles)
37
38 doubleToFloat :: Double -> Float
39 doubleToFloat = realToFrac
40
41 floatToDouble :: Float -> Double
42 floatToDouble = realToFrac
43
44 test_floatToDouble :: IO ()
45 test_floatToDouble = do
46 test_banner "doubleToFloat"
47 putStrLn (show doubles)
48 putStrLn (show $ map doubleToFloat doubles)
49 putStrLn (show $ map (floatToDouble.doubleToFloat) doubles)
50
51 test_showHex :: IO ()
52 test_showHex = do
53 test_banner "showHex"
54 putStrLn (show ints)
55 putStrLn (showList' (map showHex ints))
56 putStrLn (show integers)
57 putStrLn (showList' (map showHex integers))
58
59 test_showBin :: IO ()
60 test_showBin = do
61 test_banner "showBin"
62 putStrLn (show ints)
63 putStrLn (showList' (map showBin ints))
64 putStrLn (show integers)
65 putStrLn (showList' (map showBin integers))
66
67 showBin i = showIntAtBase 2 intToDigit i
68
69 showList' :: [ShowS] -> String
70 showList' [] = "[]"
71 showList' (x:xs) = showChar '[' . x $ showl xs ""
72 where
73 showl [] = showChar ']'
74 showl (x:xs) = showChar ',' . x . showl xs
75
76
77 test_showOct :: IO ()
78 test_showOct = do
79 test_banner "showOct"
80 putStrLn (show ints)
81 putStrLn (showList' (map showOct ints))
82 putStrLn (show integers)
83 putStrLn (showList' (map showOct integers))
84
85 ----
86 test_banner :: String -> IO ()
87 test_banner tst = do
88 putStrLn $ "--------------------------------"
89 putStrLn $ "--Testing " ++ tst
90 putStrLn $ "--------------------------------"