Import Upstream version 20180207
[hcoop/debian/mlton.git] / regression / vector.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/vector.sml -- some test cases for Vector
23 PS 1994-12-10, 1995-06-14, 1997-03-07 *)
24
25 (*KILL 05/11/1997 11:04. tho.:
26 use "auxil.sml";
27 *)
28
29 val _ = print "Testing Vector...\n";
30 fun prtest (s, s') = print(s ^ ": " ^ s' ^ "\n")
31 local
32 open Vector;
33 infix 9 sub;
34 fun extract (vec, s, l) = VectorSlice.vector (VectorSlice.slice (vec, s, l))
35 fun mapi f (vec, s, l) =
36 VectorSlice.mapi (fn (i,x) => f (i+s,x)) (VectorSlice.slice (vec, s, l))
37 in
38
39 val a = fromList [0,1,2,3,4,5,6];
40 val b = fromList [44,55,66];
41 val c = fromList [0,1,2,3,4,5,6];
42
43 val test1 = check'(fn _ => a<>b);
44 val _ = prtest("test1", test1);
45 val test2 = check'(fn _ => a=c);
46 val _ = prtest("test2", test2);
47 val d = tabulate(100, fn i => i mod 7);
48
49 val test3 = check'(fn _ => d sub 27 = 6);
50 val _ = prtest("test3", test3);
51 val test4a = (tabulate(maxLen+1, fn i => i) seq "WRONG")
52 handle Overflow => "OK" | Size => "OK" | _ => "WRONG";
53 val _ = prtest("test4a", test4a);
54 val test4b = (tabulate(~1, fn i => i) seq "WRONG")
55 handle Size => "OK" | _ => "WRONG";
56 val _ = prtest("test4b", test4b);
57 val test4c = check'(fn _ => length (tabulate(0, fn i => i div 0)) = 0);
58 val _ = prtest("test4c", test4c);
59 val test5 = check'(fn _ => length (fromList []) = 0 andalso length a = 7);
60 val _ = prtest("test5", test5);
61 val test6a = (c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
62 val _ = prtest("test6a", test6a);
63 val test6b = (c sub 7 seq "WRONG") handle Subscript => "OK" | _ => "WRONG";
64 val _ = prtest("test6b", test6b);
65 val test6c = check'(fn _ => c sub 0 = 0);
66 val _ = prtest("test6c", test6c);
67 val e = concat [d, b, d];
68
69 val test7 = check'(fn _ => length e = 203);
70 val _ = prtest("test7", test7);
71 val test8 = check'(fn _ => length (concat []) = 0);
72 val _ = prtest("test8", test8);
73 val f = extract (e, 100, SOME 3);
74
75 val test9 = check'(fn _ => f = b);
76 val _ = prtest("test9", test9);
77 val test9a = check'(fn _ => e = extract(e, 0, SOME (length e))
78 andalso e = extract(e, 0, NONE));
79 val _ = prtest("test9a", test9a);
80 val test9b = check'(fn _ => fromList [] = extract(e, 100, SOME 0));
81 val _ = prtest("test9b", test9b);
82 val test9c = (extract(e, ~1, SOME (length e)) seq "WRONG")
83 handle Subscript => "OK" | _ => "WRONG"
84 val _ = prtest("test9c", test9c);
85 val test9d = (extract(e, length e + 1, SOME 0) seq "WRONG")
86 handle Subscript => "OK" | _ => "WRONG"
87 val _ = prtest("test9d", test9d);
88 val test9e = (extract(e, 0, SOME (length e+1)) seq "WRONG")
89 handle Subscript => "OK" | _ => "WRONG"
90 val _ = prtest("test9e", test9e);
91 val test9f = (extract(e, 20, SOME ~1) seq "WRONG")
92 handle Subscript => "OK" | _ => "WRONG"
93 val _ = prtest("test9f", test9f);
94 val test9g = (extract(e, ~1, NONE) seq "WRONG")
95 handle Subscript => "OK" | _ => "WRONG"
96 val _ = prtest("test9g", test9g);
97 val test9h = (extract(e, length e + 1, NONE) seq "WRONG")
98 handle Subscript => "OK" | _ => "WRONG"
99 val _ = prtest("test9h", test9h);
100 val test9i = check'(fn _ => fromList [] = extract(e, length e, SOME 0)
101 andalso fromList [] = extract(e, length e, NONE));
102 val _ = prtest("test9i", test9i);
103 fun chkiter iter f vec reslast =
104 check'(fn _ =>
105 let val last = ref ~1
106 val res = iter (fn x => (last := x; f x)) vec
107 in (res, !last) = reslast end)
108
109 fun chkiteri iter f vec reslast =
110 check'(fn _ =>
111 let val last = ref ~1
112 val res = iter (fn (i, x) => (last := i; f x)) vec
113 in (res, !last) = reslast end)
114
115 val test10a =
116 chkiter map (fn x => 2*x) b (fromList [88,110,132], 66)
117 val _ = prtest("test10a", test10a);
118 val test11a =
119 chkiteri mapi (fn x => 2*x) (b, 0, NONE) (fromList [88,110,132], 2)
120 val _ = prtest("test11a", test11a);
121 val test11b =
122 chkiteri mapi (fn x => 2*x) (b, 1, NONE) (fromList [110,132], 2)
123 val _ = prtest("test11b", test11b);
124 val test11c =
125 chkiteri mapi (fn x => 2*x) (b, 1, SOME 0) (fromList [], ~1)
126 val _ = prtest("test11c", test11c);
127 val test11d =
128 chkiteri mapi (fn x => 2*x) (b, 1, SOME 1) (fromList [110], 1)
129 val _ = prtest("test11d", test11d);
130 val test11e =
131 chkiteri mapi (fn x => 2*x) (b, 3, NONE) (fromList [], ~1)
132 val _ = prtest("test11e", test11e);
133 val test11f =
134 (mapi #2 (b, 0, SOME 4) seq "WRONG")
135 handle Subscript => "OK" | _ => "WRONG";
136 val _ = prtest("test11f", test11f);
137 val test11g =
138 (mapi #2 (b, 3, SOME 1) seq "WRONG")
139 handle Subscript => "OK" | _ => "WRONG";
140 val _ = prtest("test11g", test11g);
141 val test11h =
142 (mapi #2 (b, 4, SOME 0) seq "WRONG")
143 handle Subscript => "OK" | _ => "WRONG";
144 val _ = prtest("test11h", test11h);
145 val test11i =
146 (mapi #2 (b, 4, NONE) seq "WRONG")
147 handle Subscript => "OK" | _ => "WRONG";
148 val _ = prtest("test11i", test11i);
149 end;
150
151