Commit | Line | Data |
---|---|---|
7f918cf1 CE |
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/date.sml | |
23 | PS 1995-03-20, 1995-05-12, 1996-07-05, 1998-04-07 | |
24 | *) | |
25 | ||
26 | (* MosML test file ported to the ML Kit; ME 1998-07-17 *) | |
27 | ||
28 | val _ = print "\nFile date.sml: Testing structure Date...\n" | |
29 | ||
30 | local | |
31 | open Time Date | |
32 | fun later h = | |
33 | toString(fromTimeLocal(now() + fromReal (3600.0 * real h))) ^ "\n"; | |
34 | fun nowdate () = Date.fromTimeLocal(now()); | |
35 | fun mkdate(y,mo,d,h,mi,s) = | |
36 | date {year=y, month=mo, day=d, hour=h, minute=mi, second=s, | |
37 | offset = NONE} | |
38 | fun cmp(dt1, dt2) = compare(mkdate dt1, mkdate dt2) | |
39 | ||
40 | fun fromto dt = | |
41 | toString (valOf (fromString (toString dt))) = toString dt | |
42 | ||
43 | fun tofrom s = | |
44 | toString (valOf (fromString s)) = s | |
45 | ||
46 | val y2k = | |
47 | date {year=2000, month=Jan, day=1, hour=0, minute=0, second=0, | |
48 | offset = SOME Time.zeroTime} | |
49 | ||
50 | val y2kE1 = | |
51 | date {year=2000, month=Jan, day=1, hour=0, minute=0, second=0, | |
52 | offset = SOME (Time.fromSeconds 82800) } | |
53 | ||
54 | val y2kW1 = | |
55 | date {year=2000, month=Jan, day=1, hour=0, minute=0, second=0, | |
56 | offset = SOME (Time.fromSeconds 3600) } | |
57 | ||
58 | val _ = | |
59 | ((*print "This is (local time) now: "; print (later 0); | |
60 | print "This is UTC now: "; | |
61 | print (toString (fromTimeUniv(now()))); print "\n"; | |
62 | print "This is an hour from now: "; print (later 1); | |
63 | print "This is a day from now: "; print (later 24); | |
64 | print "This is a week from now: "; print (later 168); | |
65 | print "This is 120 days from now: "; print (later (24 * 120)); | |
66 | print "This is 160 days from now: "; print (later (24 * 160)); | |
67 | print "This is 200 days from now: "; print (later (24 * 200)); | |
68 | print "This is 240 days from now: "; print (later (24 * 240)); | |
69 | *) | |
70 | print "This is the epoch (UTC): "; | |
71 | print (toString(fromTimeUniv zeroTime) ^ "\n"); | |
72 | print "The UTC millenium (UTC time): "; | |
73 | print (toString y2k ^ "\n"); | |
74 | print "The UTC millenium minus 5 sec: "; | |
75 | print (toString (date {year=2000, month=Jan, day=1, hour=0, | |
76 | minute=0, second= ~5, offset = SOME Time.zeroTime}) | |
77 | ^ "\n") | |
78 | (* print "The UTC millenium (local time): "; | |
79 | print (toString (fromTimeLocal (toTime y2k)) ^ "\n"); | |
80 | print "The local millenium (UTC time): "; | |
81 | print (toString (fromTimeUniv (toTime (mkdate(2000, Jan, 1, 0, 0, 0)))) | |
82 | ^ "\n"); | |
83 | print "The UTC+01 millenium (UTC): "; | |
84 | print (toString (fromTimeUniv (toTime y2kE1)) ^ "\n"); | |
85 | print "The UTC-01 millenium (UTC): "; | |
86 | print (toString (fromTimeUniv (toTime y2kW1)) ^ "\n"); | |
87 | print "This is today's number: "; | |
88 | print (fmt "%j" (nowdate()) ^ " (internally: "); | |
89 | print (Int.toString (yearDay (nowdate())) ^ ")\n"); | |
90 | print "This is today's weekday: "; | |
91 | print (fmt "%A" (nowdate()) ^ "\n"); | |
92 | print "This is the name of this month: "; | |
93 | print (fmt "%B" (nowdate()) ^ "\n"); | |
94 | print "Today's ISO date: "; | |
95 | print (fmt "%Y-%m-%d" (nowdate ()) ^ "\n")*)) | |
96 | ||
97 | ||
98 | val test1 = | |
99 | tst' "test1" (fn _ => | |
100 | cmp((1993,Jul,25,16,12,18), (1994,Jun,25,16,12,18)) = LESS | |
101 | andalso cmp((1995,May,25,16,12,18), (1994,Jun,25,16,12,18)) = GREATER | |
102 | andalso cmp((1994,May,26,16,12,18), (1994,Jun,25,16,12,18)) = LESS | |
103 | andalso cmp((1994,Jul,24,16,12,18), (1994,Jun,25,16,12,18)) = GREATER | |
104 | andalso cmp((1994,Jun,24,17,12,18), (1994,Jun,25,16,12,18)) = LESS | |
105 | andalso cmp((1994,Jun,26,15,12,18), (1994,Jun,25,16,12,18)) = GREATER | |
106 | andalso cmp((1994,Jun,25,15,13,18), (1994,Jun,25,16,12,18)) = LESS | |
107 | andalso cmp((1994,Jun,25,17,11,18), (1994,Jun,25,16,12,18)) = GREATER | |
108 | andalso cmp((1994,Jun,25,16,11,19), (1994,Jun,25,16,12,18)) = LESS | |
109 | andalso cmp((1994,Jun,25,16,13,17), (1994,Jun,25,16,12,18)) = GREATER | |
110 | andalso cmp((1994,Jun,25,16,12,17), (1994,Jun,25,16,12,18)) = LESS | |
111 | andalso cmp((1994,Jun,25,16,12,19), (1994,Jun,25,16,12,18)) = GREATER | |
112 | andalso cmp((1994,Jun,25,16,12,18), (1994,Jun,25,16,12,18)) = EQUAL); | |
113 | ||
114 | val test2 = | |
115 | tst' "test2" (fn _ => | |
116 | fmt "%A" (mkdate(1995,May,22,4,0,1)) = "Monday"); | |
117 | ||
118 | val test3 = | |
119 | tst' "test3" (fn _ => | |
120 | List.all fromto | |
121 | [mkdate(1995,Aug,22,4,0,1), | |
122 | mkdate(1996,Apr, 5, 0, 7, 21), | |
123 | mkdate(1996,Mar, 5, 6, 13, 58)]); | |
124 | ||
125 | val test4 = | |
126 | tst' "test4" (fn _ => | |
127 | List.all tofrom | |
128 | ["Fri Jul 05 14:25:16 1996", | |
129 | "Mon Feb 05 04:25:16 1996", | |
130 | "Sat Jan 06 04:25:16 1996"]) | |
131 | ||
132 | val test5 = | |
133 | tst' "test5" (fn _ => | |
134 | weekDay(mkdate(1962, Jun, 25, 1, 2, 3)) = Mon | |
135 | andalso weekDay(mkdate(1998, Mar, 6, 1, 2, 3)) = Fri | |
136 | andalso weekDay(mkdate(1998, Apr, 6, 1, 2, 3)) = Mon | |
137 | andalso weekDay(mkdate(1900, Feb, 28, 1, 2, 3)) = Wed | |
138 | andalso weekDay(mkdate(1900, Mar, 1, 1, 2, 3)) = Thu | |
139 | andalso weekDay(mkdate(1850, Feb, 28, 1, 2, 3)) = Thu | |
140 | andalso weekDay(mkdate(1850, Mar, 1, 1, 2, 3)) = Fri | |
141 | andalso weekDay(mkdate(1860, Feb, 28, 1, 2, 3)) = Tue | |
142 | andalso weekDay(mkdate(1860, Feb, 29, 1, 2, 3)) = Wed | |
143 | andalso weekDay(mkdate(1860, Mar, 1, 1, 2, 3)) = Thu | |
144 | andalso weekDay(mkdate(2000, Feb, 28, 1, 2, 3)) = Mon | |
145 | andalso weekDay(mkdate(2000, Feb, 29, 1, 2, 3)) = Tue | |
146 | andalso weekDay(mkdate(2000, Mar, 1, 1, 2, 3)) = Wed) | |
147 | ||
148 | val test6 = | |
149 | tst' "test6" (fn _ => | |
150 | yearDay(mkdate(1962, Jan, 1, 1, 2, 3)) = 0 | |
151 | andalso yearDay(mkdate(1998, Mar, 6, 1, 2, 3)) = 64 | |
152 | andalso yearDay(mkdate(1900, Feb, 28, 1, 2, 3)) = 58 | |
153 | andalso yearDay(mkdate(1900, Mar, 1, 1, 2, 3)) = 59 | |
154 | andalso yearDay(mkdate(1900, Dec, 31, 1, 2, 3)) = 364 | |
155 | andalso yearDay(mkdate(1850, Feb, 28, 1, 2, 3)) = 58 | |
156 | andalso yearDay(mkdate(1850, Mar, 1, 1, 2, 3)) = 59 | |
157 | andalso yearDay(mkdate(1850, Dec, 31, 1, 2, 3)) = 364 | |
158 | andalso yearDay(mkdate(1860, Feb, 28, 1, 2, 3)) = 58 | |
159 | andalso yearDay(mkdate(1860, Feb, 29, 1, 2, 3)) = 59 | |
160 | andalso yearDay(mkdate(1860, Mar, 1, 1, 2, 3)) = 60 | |
161 | andalso yearDay(mkdate(1860, Dec, 31, 1, 2, 3)) = 365 | |
162 | andalso yearDay(mkdate(2000, Feb, 28, 1, 2, 3)) = 58 | |
163 | andalso yearDay(mkdate(2000, Feb, 29, 1, 2, 3)) = 59 | |
164 | andalso yearDay(mkdate(2000, Mar, 1, 1, 2, 3)) = 60 | |
165 | andalso yearDay(mkdate(2000, Dec, 31, 1, 2, 3)) = 365 | |
166 | andalso yearDay(mkdate(1959, Feb, 28, 1, 2, 3)) = 58 | |
167 | andalso yearDay(mkdate(1959, Mar, 1, 1, 2, 3)) = 59 | |
168 | andalso yearDay(mkdate(1959, Dec, 31, 1, 2, 3)) = 364 | |
169 | andalso yearDay(mkdate(1960, Feb, 28, 1, 2, 3)) = 58 | |
170 | andalso yearDay(mkdate(1960, Feb, 29, 1, 2, 3)) = 59 | |
171 | andalso yearDay(mkdate(1960, Mar, 1, 1, 2, 3)) = 60 | |
172 | andalso yearDay(mkdate(1960, Dec, 31, 1, 2, 3)) = 365) | |
173 | ||
174 | fun addh h = | |
175 | let val dt = mkdate(1998, Apr, 6, h, 0, 0) | |
176 | in (month dt, day dt, hour dt) end | |
177 | ||
178 | val test7 = | |
179 | tst' "test7" (fn _ => | |
180 | addh 0 = (Apr, 6, 0) | |
181 | andalso addh 23 = (Apr, 6, 23) | |
182 | andalso addh 24 = (Apr, 7, 0) | |
183 | andalso addh 36 = (Apr, 7, 12) | |
184 | andalso addh 600 = (May, 1, 0) | |
185 | andalso addh 610 = (May, 1, 10) | |
186 | andalso addh 625 = (May, 2, 1)) | |
187 | ||
188 | val test8 = | |
189 | tst' "test8" (fn _ => | |
190 | hour (mkdate(1998, Mar, 28, 12, 0, 0)) = 12 | |
191 | andalso hour (mkdate(1998, Mar, 28, 36, 0, 0)) = 12) | |
192 | ||
193 | in | |
194 | end |