Import Upstream version 20180207
[hcoop/debian/mlton.git] / regression / time.sml
1 (* Auxiliary functions for test cases *)
2
3 infix 1 seq
4 fun e1 seq e2 = e2;
5 fun check b = if b then "OK" else "WRONG";
6 fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";
7
8 fun range (from, to) p =
9 let open Int
10 in
11 (from > to) orelse (p from) andalso (range (from+1, to) p)
12 end;
13
14 fun checkrange bounds = check o range bounds;
15
16 fun tst0 s s' = print (s ^ " \t" ^ s' ^ "\n");
17 fun tst s b = tst0 s (check b);
18 fun tst' s f = tst0 s (check' f);
19
20 fun tstrange s bounds = (tst s) o range bounds
21
22 (* test/time.sml
23 PS 1995-03-23
24 *)
25
26 val _ = print "\nFile time.sml: Testing structure Time...\n"
27
28 local
29 fun fib n = if n<2 then 1 else fib(n-1) + fib(n-2);
30 open Time
31 val bigt = fromSeconds 987654321 + fromMicroseconds 500012;
32 val litt = fromSeconds 454 + fromMicroseconds 501701
33
34 val test1 =
35 tst' "test1" (fn _ => zeroTime + bigt = bigt andalso bigt - zeroTime = bigt);
36
37 val test2a =
38 tst' "test2a" (fn _ => toSeconds zeroTime = 0
39 andalso zeroTime = fromSeconds 0
40 andalso zeroTime = fromMilliseconds 0
41 andalso zeroTime = fromMicroseconds 0);
42 val test2b =
43 tst' "test2b" (fn _ => toSeconds bigt = 987654321
44 andalso toSeconds litt = 454
45 andalso toMilliseconds litt = 454501
46 andalso toMicroseconds litt = 454501701);
47 val test2c = tst0 "test2c" ((fromSeconds ~1 seq "OK")
48 handle _ => "WRONG")
49 val test2d = tst0 "test2d" ((fromMilliseconds ~1 seq "OK")
50 handle _ => "WRONG")
51 val test2e = tst0 "test2e" ((fromMicroseconds ~1 seq "OK")
52 handle _ => "WRONG")
53
54 val test3a =
55 tst' "test3a" (fn _ => fromReal 0.0 = zeroTime
56 andalso fromReal 10.25 = fromSeconds 10 + fromMilliseconds 250);
57 val test3b = tst0 "test3b" ((fromReal ~1.0 seq "OK")
58 handle _ => "WRONG")
59 val test3c = tst0 "test3c" ((fromReal 1E300 seq "OK")
60 handle Time => "OK" | _ => "WRONG")
61
62 val test4a =
63 tst' "test4a" (fn _ => Real.==(toReal (fromReal 100.25), 100.25));
64
65 val test6a =
66 tst' "test6a" (fn _ => bigt + litt = litt + bigt
67 andalso (bigt + litt) - litt = bigt
68 andalso (bigt - litt) + litt = bigt);
69
70 val test7a =
71 tst' "test7a" (fn _ => litt <= litt andalso litt >= litt
72 andalso zeroTime < litt andalso litt > zeroTime
73 andalso litt < bigt andalso bigt > litt
74 andalso not (litt > bigt)
75 andalso not (bigt < litt)
76 andalso not(litt < litt)
77 andalso not(litt > litt));
78
79 val test8a =
80 tst' "test8a" (fn _ => now() <= now()
81 andalso (now () before fib 27 seq ()) <= now());
82
83 val test9a =
84 tst' "test9a" (fn _ => fmt 0 litt = "455")
85
86 val test9b =
87 tst' "test9b" (fn _ => fmt 1 litt = "454.5"
88 andalso fmt 2 litt = "454.50"
89 andalso fmt 3 litt = "454.502"
90 andalso fmt 4 litt = "454.5017"
91 andalso fmt 5 litt = "454.50170"
92 andalso fmt 6 litt = "454.501701");
93
94 fun chk (s, r) =
95 tst' "test10a" (fn _ =>
96 case fromString s of
97 SOME res => res = fromMicroseconds r
98 | NONE => false)
99
100 val test10a =
101 List.map chk
102 [("189", 189000000),
103 ("189.1", 189100000),
104 ("189.125125", 189125125),
105 (".1", 100000),
106 (".125125", 125125),
107 (" \n\t189crap", 189000000),
108 (" \n\t189.1crap", 189100000),
109 (" \n\t189.125125crap", 189125125),
110 (" \n\t.1crap", 100000),
111 (" \n\t.125125crap", 125125)];
112
113 val test10b =
114 List.app (fn s => tst0 "test10b" (case fromString s of NONE => "OK" | _ => "WRONG"))
115 ["", "now", "Monday"];
116 in
117 end