[project @ 1999-05-07 14:52:49 by simonm]
[ghc-hetmet.git] / ghc / tests / lib / should_run / dynamic001.hs
1 -- !!! Dynamic library regression tests
2 module Main(main) where
3
4 import Dynamic
5
6 main :: IO ()
7 main = do
8    test   "toDyn"   toDyn_list
9    testIO "fromDyn" fromDyn_test
10
11 toDyn_list :: [Dynamic]
12 toDyn_list =
13     [ toDyn (1::Int)
14     , toDyn ('a')
15     , toDyn False
16     , toDyn ((-1.0)::Float)
17     , toDyn (0.0::Double)
18     , toDyn (1394::Integer)
19     , toDyn (print "hello")
20     , toDyn toDyn_list
21     , toDyn ([]::[Int])
22     , toDyn (Nothing  :: Maybe Int)
23     , toDyn ((Just 2) :: Maybe Int)
24     , toDyn ((Just 2) :: Maybe Int)
25     , toDyn ((Left 3) :: Either Int Bool)
26     , toDyn ((Right 3) :: Either Char Int)
27     , toDyn ()
28     , toDyn LT
29     , toDyn ((),2::Int)
30     , toDyn ((),2::Int,'a')
31     , toDyn ((),2::Int,'a',1.0::Double)
32     , toDyn ((),2::Int,'a',1.0::Double,Nothing::Maybe Bool)
33     , toDyn ((+) :: Int -> Int -> Int)
34     , toDyn ((+) :: Integer -> Integer -> Integer)
35     , toDyn ((++) :: [Char] -> [Char] -> [Char])
36     ]
37
38 -- Testing the conversion from Dynamic values:
39 fromDyn_test :: IO ()
40 fromDyn_test = do
41    print (fromDyn (toDyn (1::Int)) (0::Int))
42    print (fromDyn (toDyn ('a'::Char)) (0::Int))
43    print (fromDyn (toDyn 'a') 'b')
44    print (fromDyn (toDyn (1::Float)) (0::Float))
45    print (fromDyn (toDyn (2::Float)) (0::Int))
46    print (fromDyn (toDyn (3::Double)) (0::Double))
47    print (fromDyn (toDyn (4::Double)) (0::Int))
48    print (fromDyn (toDyn (5::Integer)) (0::Integer))
49    print (fromDyn (toDyn (6::Integer)) False)
50    print (fromDyn (toDyn [1,3,5::Integer]) ([]::[Integer]))
51    print (fromDyn (toDyn (Just True)) (Nothing::Maybe Bool))
52    print (fromDyn (toDyn (Left True::Either Bool Bool)) (Right False :: Either Bool Bool))
53    print (fromDyn (toDyn LT) GT)
54    print (fromDyn (toDyn ((+1)::Int->Int)) False)
55    print ((fromDyn (toDyn ((+1)::Int->Int)) ((+2)::Int->Int)) 3)
56    print ((fromDyn (toDyn ((++)::[Int]->[Int]->[Int])) ((undefined)::[Int]->[Int]->[Int])) [1] [2])
57
58     
59 -- Misc test utilities:
60 test :: Show a => String -> [a] -> IO ()
61 test str ls = do
62   putStrLn ("*** Testing: " ++ str ++ " ***")
63   putStrLn (showListLn ls)
64
65 testIO :: String -> IO () -> IO ()
66 testIO str tst = do
67   putStrLn ("*** Testing: " ++ str ++ " ***")
68   tst
69
70
71 -- showListLn presents a list in a diff-friendly format.
72 -- showListLn [a1,..an]
73 --  =>
74 --      [ a1
75 --      , a2
76 --      ..
77 --      , an
78 --      ]
79 --   
80 showListLn :: Show a => [a] -> String
81 showListLn [] = ""
82 showListLn ls = '[' : ' ' : go ls
83  where
84    go    [x] = show x ++ "\n]"
85    go (x:xs) = show x ++ '\n':',':' ':go xs
86
87 {-
88 test8 = toDyn (mkAppTy listTc)
89 test9 :: Float
90 test9 = fromDyn test8 0
91
92 printf :: String -> [Dynamic] -> IO ()
93 printf str args = putStr (decode str args)
94  where
95   decode [] [] = []
96   decode ('%':'n':cs) (d:ds) =
97     (\ v -> show v++decode cs ds) (fromDyn  d (0::Int))
98   decode ('%':'c':cs) (d:ds) =
99     (\ v -> show v++decode cs ds) (fromDyn  d ('\0'))
100   decode ('%':'b':cs) (d:ds) =
101     (\ v -> show v++decode cs ds) (fromDyn  d (False::Bool))
102   decode (x:xs) ds = x:decode xs ds
103
104 test10 :: IO ()
105 test10 = printf "%n = %c, that much is %b\n" [toDyn (3::Int),toDyn 'a', toDyn False]
106
107 -}