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