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 | ||
23 | (* test/list.sml PS 1994-12-10; Martin-11/03/1998 *) | |
24 | ||
25 | val _ = print "\nFile list.sml: Testing structure List...\n"; | |
26 | ||
27 | local | |
28 | open List | |
29 | in | |
30 | val v123 = [1,2,3]; | |
31 | fun even i = i mod 2 = 0; | |
32 | ||
33 | val test1 = tst "test1" (null [] andalso not (null [[]])); | |
34 | ||
35 | val test2 = tst "test2" (1 = hd v123 andalso [2,3] = tl v123 andalso 3 = last v123) | |
36 | ||
37 | val test3 = tst0 "test3" ((hd [] seq "WRONG") handle Empty => "OK" | _ => "WRONG") | |
38 | val test4 = tst0 "test4" ((tl [] seq "WRONG") handle Empty => "OK" | _ => "WRONG") | |
39 | val test5 = tst0 "test5" ((last [] seq "WRONG") handle Empty => "OK" | _ => "WRONG") | |
40 | ||
41 | val test6 = tst "test6" (1 = nth(v123,0) andalso 3 = nth(v123,2)) | |
42 | ||
43 | val test7 = tst0 "test7" ((nth(v123,~1) seq "WRONG") handle Subscript => "OK" | _ => "WRONG") | |
44 | val test8 = tst0 "test8" ((nth(v123,3) seq "WRONG") handle Subscript => "OK" | _ => "WRONG") | |
45 | ||
46 | val test9 = tst "test9" (3 = length v123); | |
47 | ||
48 | val test10 = tst "test10" ([3,2,1] = rev [1,2,3]); | |
49 | ||
50 | val v16 = v123 @ [4,5,6]; | |
51 | ||
52 | val test11 = tst "test11" ([1,2,3,4,5,6] = v16); | |
53 | ||
54 | val test12 = tst "test12" (concat [] = [] andalso concat [v16] = v16 | |
55 | andalso concat [v123, [4,5,6]] = v16); | |
56 | ||
57 | val test13 = tst "test13"(rev v16 = revAppend([4,5,6], [3,2,1])); | |
58 | ||
59 | local | |
60 | val v = ref 0 | |
61 | fun h [] r = r | h (x::xr) r = h xr (r+r+x): int; | |
62 | val isum = h v16 0 | |
63 | in | |
64 | fun reset () = v := 0; | |
65 | fun incrv i = v := 2 * !v + i; | |
66 | fun checkv () = tst "checkv" (!v = isum); | |
67 | end; | |
68 | ||
69 | val test14 = (reset (); app incrv v16; checkv); | |
70 | ||
71 | val test15 = tst "test15" ([2,4,6,8,10,12] = map (fn i=>i*2) v16); | |
72 | ||
73 | val test16 = | |
74 | tst "test16" ([3,9,15] = | |
75 | mapPartial (fn i => if even i then NONE else SOME (3*i)) v16); | |
76 | ||
77 | val test17 = tst "test17" (NONE = find (fn i => i>7) v16); | |
78 | ||
79 | val test18 = tst "test18" (SOME 5 = find (fn i => i>4) v16); | |
80 | ||
81 | val test19 = tst "test19" (NONE = find (fn _ => true) []); | |
82 | ||
83 | val test20 = tst "test20" ([2,4,6] = filter even v16); | |
84 | ||
85 | val test21 = (reset (); filter (fn i => (incrv i; true)) v16 seq checkv()); | |
86 | ||
87 | val test22 = tst "test22" (([2,4,6], [1,3,5]) = partition even v16); | |
88 | ||
89 | val test23 = (reset (); partition (fn i => (incrv i; true)) v16 seq checkv()); | |
90 | ||
91 | val test24 = tst "test24" (v16 = foldr op:: [] v16); | |
92 | val test25 = tst "test25" (rev v16 = foldl op:: [] v16); | |
93 | ||
94 | val test26 = (reset(); foldr (fn (i,r) => incrv i) () (rev v16); checkv()); | |
95 | val test27 = (reset(); foldl (fn (i,r) => incrv i) () v16; checkv()); | |
96 | ||
97 | val test28 = tst "test28" (21 = foldr op+ 0 v16 andalso 21 = foldl op+ 0 v16); | |
98 | ||
99 | val test29 = tst "test29" (all (fn _ => false) [] | |
100 | andalso not (exists (fn _ => true) [])); | |
101 | ||
102 | val test30 = tst "test30" (exists even [1,1,1,1,1,1,2,1] | |
103 | andalso all even [6,6,6,6,6,6,6,6]); | |
104 | ||
105 | val test31 = tst "test31" (v16 = tabulate (6, fn i => i+1)); | |
106 | ||
107 | val test32 = (reset(); tabulate (6, fn i => (incrv (i+1); 127)) seq checkv()); | |
108 | ||
109 | val test33 = tst "test33" ([] = tabulate (0, fn i => 1 div i)); | |
110 | ||
111 | val test34 = tst0 "test36b" ((tabulate(~1, fn _ => raise Div) seq "WRONG") | |
112 | handle Size => "OK" | _ => "WRONG") | |
113 | ||
114 | val test35a = tst "test35a" (drop([], 0) = [] | |
115 | andalso drop(v123, 0) = v123 | |
116 | andalso drop(v123, 3) = []); | |
117 | val test35b = tst0 "test36b" ((drop(v123, ~1) seq "WRONG") | |
118 | handle Subscript => "OK" | _ => "WRONG") | |
119 | val test35c = tst0 "test35c" ((drop(v123, 4) seq "WRONG") | |
120 | handle Subscript => "OK" | _ => "WRONG") | |
121 | ||
122 | val test36a = tst "test36a" (take([], 0) = [] | |
123 | andalso take(v123, 3) = v123 | |
124 | andalso take(v123, 0) = []); | |
125 | val test36b = tst0 "test36b" ((take(v123, ~1) seq "WRONG") | |
126 | handle Subscript => "OK" | _ => "WRONG") | |
127 | val test36c = tst0 "test36c" ((take(v123, 4) seq "WRONG") | |
128 | handle Subscript => "OK" | _ => "WRONG") | |
129 | ||
130 | val test37a = | |
131 | tst' "test37a" (fn _ => getItem [] = NONE | |
132 | andalso getItem [#"A"] = SOME(#"A", []) | |
133 | andalso getItem [#"B", #"C"] = SOME(#"B", [#"C"])); | |
134 | end; |