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