@@ -5,6 +5,69 @@ import Prelude
55type AlmostEff = Unit -> Unit
66
77main :: AlmostEff
8- main = mainImpl show
8+ main = do
9+ testNumberShow show
10+ testOrderings
11+
12+ foreign import testNumberShow :: (Number -> String ) -> AlmostEff
13+ foreign import throwErr :: String -> AlmostEff
14+
15+
16+ assert :: String -> Boolean -> AlmostEff
17+ assert msg condition = if condition then const unit else throwErr msg
18+
19+ testOrd :: forall a . (Ord a , Show a ) => a -> a -> Ordering -> AlmostEff
20+ testOrd x y ord =
21+ assert
22+ (" (compare " <> show x <> " " <> show y <> " ) is not equal to " <> show ord)
23+ $ (compare x y) == ord
24+
25+ nan :: Number
26+ nan = 0.0 /0.0
27+
28+ -- Unfortunately, NaN inhabits our Int
29+ intNan :: Int
30+ intNan = mod 1 0
31+
32+ plusInfinity :: Number
33+ plusInfinity = 1.0 /0.0
34+
35+ minusInfinity :: Number
36+ minusInfinity = -1.0 /0.0
37+
38+ testOrderings :: AlmostEff
39+ testOrderings = do
40+ assert " NaN shouldn't be equal to itself" $ nan /= nan
41+ assert " NaN shouldn't be equal to itself" $ (compare nan nan) /= EQ
42+ testOrd 1.0 2.0 LT
43+ testOrd 2.0 1.0 GT
44+ testOrd 1.0 (-2.0 ) GT
45+ testOrd (-2.0 ) 1.0 LT
46+ testOrd minusInfinity plusInfinity LT
47+ testOrd minusInfinity 0.0 LT
48+ testOrd plusInfinity 0.0 GT
49+ testOrd plusInfinity minusInfinity GT
50+ testOrd 1.0 nan GT
51+ testOrd nan 1.0 GT
52+ testOrd nan plusInfinity GT
53+ testOrd plusInfinity nan GT
54+ assert " 1 > NaN should be false" $ (1.0 > nan) == false
55+ assert " 1 < NaN should be false" $ (1.0 < nan) == false
56+ assert " NaN > 1 should be false" $ (nan > 1.0 ) == false
57+ assert " NaN < 1 should be false" $ (nan < 1.0 ) == false
58+ assert " NaN == 1 should be false" $ nan /= 1.0
59+ testOrd intNan 2147483647 GT
60+ testOrd ' a' ' b' LT
61+ testOrd ' b' ' A' GT
62+ testOrd " 10" " 0" GT
63+ testOrd " 10" " 2" LT
64+ testOrd true true EQ
65+ testOrd false false EQ
66+ testOrd false true LT
67+ testOrd true false GT
68+ testOrd ([] :: Array Int ) [] EQ
69+ testOrd [1 , 0 ] [1 ] GT
70+ testOrd [1 ] [1 , 0 ] LT
71+ testOrd [1 , 1 ] [1 , 0 ] GT
72+ testOrd [1 , -1 ] [1 , 0 ] LT
973
10- foreign import mainImpl :: (Number -> String ) -> AlmostEff
0 commit comments