1 -- !!! Dynamic library regression tests
2 module Main(main) where
8 test "toDyn" toDyn_list
9 testIO "fromDyn" fromDyn_test
11 toDyn_list :: [Dynamic]
16 , toDyn ((-1.0)::Float)
18 , toDyn (1394::Integer)
19 , toDyn (print "hello")
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)
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])
38 -- Testing the conversion from Dynamic values:
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])
59 -- Misc test utilities:
60 test :: Show a => String -> [a] -> IO ()
62 putStrLn ("*** Testing: " ++ str ++ " ***")
63 putStrLn (showListLn ls)
65 testIO :: String -> IO () -> IO ()
67 putStrLn ("*** Testing: " ++ str ++ " ***")
71 -- showListLn presents a list in a diff-friendly format.
72 -- showListLn [a1,..an]
80 showListLn :: Show a => [a] -> String
82 showListLn ls = '[' : ' ' : go ls
84 go [x] = show x ++ "\n]"
85 go (x:xs) = show x ++ '\n':',':' ':go xs
88 test8 = toDyn (mkAppTy listTc)
90 test9 = fromDyn test8 0
92 printf :: String -> [Dynamic] -> IO ()
93 printf str args = putStr (decode str args)
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
105 test10 = printf "%n = %c, that much is %b\n" [toDyn (3::Int),toDyn 'a', toDyn False]