File: time.sml

package info (click to toggle)
mlton 20100608-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 34,980 kB
  • ctags: 69,089
  • sloc: ansic: 18,421; lisp: 2,879; makefile: 1,570; sh: 1,325; pascal: 256; asm: 97
file content (117 lines) | stat: -rw-r--r-- 3,797 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(* Auxiliary functions for test cases *)

infix 1 seq
fun e1 seq e2 = e2;
fun check b = if b then "OK" else "WRONG";
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";

fun range (from, to) p = 
    let open Int 
    in
        (from > to) orelse (p from) andalso (range (from+1, to) p)
    end;

fun checkrange bounds = check o range bounds;

fun tst0 s s' = print (s ^ "    \t" ^ s' ^ "\n");
fun tst  s b = tst0 s (check  b);
fun tst' s f = tst0 s (check' f);

fun tstrange s bounds = (tst s) o range bounds  

(* test/time.sml
   PS 1995-03-23
*)

val _ = print "\nFile time.sml: Testing structure Time...\n"

local 
    fun fib n = if n<2 then 1 else fib(n-1) + fib(n-2);
    open Time
    val bigt = fromSeconds 987654321 + fromMicroseconds 500012;
    val litt = fromSeconds 454 + fromMicroseconds 501701

    val test1 = 
    tst' "test1" (fn _ => zeroTime + bigt = bigt andalso bigt - zeroTime = bigt);

val test2a = 
    tst' "test2a" (fn _ => toSeconds zeroTime = 0
           andalso zeroTime = fromSeconds 0
           andalso zeroTime = fromMilliseconds 0
           andalso zeroTime = fromMicroseconds 0);
val test2b = 
    tst' "test2b" (fn _ => toSeconds bigt = 987654321
           andalso toSeconds litt = 454
           andalso toMilliseconds litt = 454501
           andalso toMicroseconds litt = 454501701);
val test2c = tst0 "test2c" ((fromSeconds ~1 seq "OK")
                           handle _ => "WRONG")
val test2d = tst0 "test2d" ((fromMilliseconds ~1 seq "OK")
                           handle _ => "WRONG")
val test2e = tst0 "test2e" ((fromMicroseconds ~1 seq "OK")
                           handle _ => "WRONG")

val test3a = 
    tst' "test3a" (fn _ => fromReal 0.0 = zeroTime
                   andalso fromReal 10.25 = fromSeconds 10 + fromMilliseconds 250);
val test3b = tst0 "test3b" ((fromReal ~1.0 seq "OK")
                           handle _ => "WRONG")
val test3c = tst0 "test3c" ((fromReal 1E300 seq "OK")
                           handle Time => "OK" | _ => "WRONG") 

val test4a = 
    tst' "test4a" (fn _ => Real.==(toReal (fromReal 100.25), 100.25));

val test6a = 
    tst' "test6a" (fn _ => bigt + litt = litt + bigt
                   andalso (bigt + litt) - litt = bigt
                   andalso (bigt - litt) + litt = bigt);

val test7a = 
    tst' "test7a" (fn _ => litt <= litt andalso litt >= litt
           andalso zeroTime < litt andalso litt > zeroTime
           andalso litt < bigt andalso bigt > litt
           andalso not (litt > bigt) 
           andalso not (bigt < litt) 
           andalso not(litt < litt)
           andalso not(litt > litt));

val test8a = 
    tst' "test8a" (fn _ => now() <= now() 
           andalso (now () before fib 27 seq ()) <= now());

val test9a = 
    tst' "test9a" (fn _ => fmt 0 litt = "455")

val test9b = 
    tst' "test9b" (fn _ => fmt 1 litt = "454.5"
           andalso fmt 2 litt = "454.50"
           andalso fmt 3 litt = "454.502"
           andalso fmt 4 litt = "454.5017"
           andalso fmt 5 litt = "454.50170"
           andalso fmt 6 litt = "454.501701");
    
fun chk (s, r) = 
    tst' "test10a" (fn _ => 
           case fromString s of
               SOME res => res = fromMicroseconds r
             | NONE     => false)

val test10a = 
    List.map chk
         [("189", 189000000),
          ("189.1", 189100000),
          ("189.125125", 189125125),
          (".1", 100000),
          (".125125", 125125),
          (" \n\t189crap", 189000000),
          (" \n\t189.1crap", 189100000),
          (" \n\t189.125125crap", 189125125),
          (" \n\t.1crap", 100000),
          (" \n\t.125125crap", 125125)];

val test10b = 
    List.app (fn s => tst0 "test10b" (case fromString s of NONE => "OK" | _ => "WRONG"))
         ["", "now", "Monday"];
in
end