Import Debian changes 20180207-1
[hcoop/debian/mlton.git] / regression / word8vector.sml
CommitLineData
7f918cf1
CE
1type word8 = Word8.word
2
3(* Auxiliary functions for test cases *)
4
5infix 1 seq
6fun e1 seq e2 = e2;
7fun check b = if b then "OK" else "WRONG";
8fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";
9
10fun range (from, to) p =
11 let open Int
12 in
13 (from > to) orelse (p from) andalso (range (from+1, to) p)
14 end;
15
16fun checkrange bounds = check o range bounds;
17
18fun tst0 s s' = print (s ^ " \t" ^ s' ^ "\n");
19fun tst s b = tst0 s (check b);
20fun tst' s f = tst0 s (check' f);
21
22fun tstrange s bounds = (tst s) o range bounds
23
24(* test/vector.sml -- some test cases for Vector
25 PS 1994-12-10, 1995-06-14 *)
26
27(*KILL 05/11/1997 11:05. tho.:
28use "auxil.sml";
29*)
30
31local
32 open Word8Vector;
33 fun extract (vec, s, l) =
34 Word8VectorSlice.vector (Word8VectorSlice.slice (vec, s, l))
35 fun mapi f (vec, s, l) =
36 Word8VectorSlice.mapi (fn (i,x) => f (i+s,x)) (Word8VectorSlice.slice (vec, s, l))
37 val i2w = Word8.fromInt;
38 infix 9 sub;
39in
40
41val a = fromList (List.map i2w [0,1,2,3,4,5,6]);
42val b = fromList (List.map i2w [44,55,66]);
43val c = fromList (List.map i2w [0,1,2,3,4,5,6]);
44
45fun equal (v, v') =
46 let
47 val n = Word8Vector.length v
48 val n' = Word8Vector.length v'
49 fun loop i =
50 i = n
51 orelse (Word8Vector.sub (v, i) = Word8Vector.sub (v', i)
52 andalso loop (i + 1))
53 in
54 n = n' andalso loop 0
55 end
56
57val test1:unit = tst' "test1" (fn _ => not (equal (a,b)));
58val test2:unit = tst' "test2" (fn _ => equal (a, c));
59
60val d = tabulate(100, fn i => i2w (i mod 7));
61
62val test3:unit = tst' "test3" (fn _ => d sub 27 = i2w 6);
63
64val test4a:unit = tst0 "test4a" ((tabulate(maxLen+1, i2w) seq "WRONG")
65 handle Overflow => "OK" | Size => "OK" | _ => "WRONG")
66
67val test4b:unit = tst0 "test4b" ((tabulate(~1, i2w) seq "WRONG")
68 handle Size => "OK" | _ => "WRONG")
69
70val test4c:unit = tst' "test4c" (fn _ => length (tabulate(0, fn i => i2w (i div 0))) = 0);
71
72val test5:unit = tst' "test5" (fn _ => length (fromList []) = 0 andalso length a = 7);
73
74val test6a:unit = tst0 "test6a" ((c sub ~1 seq "WRONG") handle Subscript => "OK" | _ => "WRONG")
75val test6b:unit = tst0 "test6b" ((c sub 7 seq "WRONG") handle Subscript => "OK" | _ => "WRONG")
76val test6c:unit = tst' "test6c" (fn _ => c sub 0 = i2w 0);
77
78val e = concat [d, b, d];
79
80val test7:unit = tst' "test7" (fn _ => length e = 203);
81
82val test8:unit = tst' "test8" (fn _ => length (concat []) = 0);
83
84val f = extract (e, 100, SOME 3);
85
86val test9:unit = tst' "test9" (fn _ => equal (f, b));
87
88val test9a:unit = tst' "test9a" (fn _ => equal (e, extract(e, 0, SOME (length e)))
89 andalso equal (e, extract(e, 0, NONE)));
90val test9b:unit = tst' "test9b" (fn _ => equal (fromList [],
91 extract(e, 100, SOME 0)));
92val test9c:unit = tst0 "test9c" ((extract(e, ~1, SOME (length e)) seq "WRONG")
93 handle Subscript => "OK" | _ => "WRONG")
94val test9d:unit = tst0 "test9d" ((extract(e, length e + 1, SOME 0) seq "WRONG")
95 handle Subscript => "OK" | _ => "WRONG")
96val test9e:unit = tst0 "test9e" ((extract(e, 0, SOME (length e+1)) seq "WRONG")
97 handle Subscript => "OK" | _ => "WRONG")
98val test9f:unit = tst0 "test9f" ((extract(e, 20, SOME ~1) seq "WRONG")
99 handle Subscript => "OK" | _ => "WRONG")
100val test9g:unit = tst0 "test9g" ((extract(e, ~1, NONE) seq "WRONG")
101 handle Subscript => "OK" | _ => "WRONG")
102val test9h:unit = tst0 "test9h" ((extract(e, length e + 1, NONE) seq "WRONG")
103 handle Subscript => "OK" | _ => "WRONG")
104val test9i:unit = tst' "test9i" (fn _ => equal (fromList [], extract (e, length e, SOME 0))
105 andalso equal (fromList [], extract(e, length e, NONE)));
106
107fun chkiter iter f vec (res', last') =
108 tst' "test_chkiter" (fn _ =>
109 let val last = ref (0w255:word8)
110 val res = iter (fn x => (last := x; f x)) vec
111 in equal (res, res') andalso !last = last' end)
112
113fun chkiteri iter f vec (res', last') =
114 tst' "test_chkiteri" (fn _ =>
115 let val last = ref ~1
116 val res = iter (fn (i, x) => (last := i; f x)) vec
117 in equal (res, res') andalso !last = last' end)
118
119val test10a:unit =
120 chkiter map (fn x => 0w2*x) b (fromList [0w88,0w110,0w132], 0w66)
121
122val test11a:unit =
123 chkiteri mapi (fn x => 0w2*x) (b, 0, NONE) (fromList [0w88,0w110,0w132], 2)
124val test11b:unit =
125 chkiteri mapi (fn x => 0w2*x) (b, 1, NONE) (fromList [0w110,0w132], 2)
126val test11c:unit =
127 chkiteri mapi (fn x => 0w2*x) (b, 1, SOME 0) (fromList [], ~1)
128val test11d:unit =
129 chkiteri mapi (fn x => 0w2*x) (b, 1, SOME 1) (fromList [0w110], 1)
130val test11e:unit =
131 chkiteri mapi (fn x => 0w2*x) (b, 3, NONE) (fromList [], ~1)
132
133val test11f:unit =
134 tst0 "test11f" ((mapi #2 (b, 0, SOME 4) seq "WRONG")
135 handle Subscript => "OK" | _ => "WRONG")
136val test11g:unit =
137 tst0 "test11g" ((mapi #2 (b, 3, SOME 1) seq "WRONG")
138 handle Subscript => "OK" | _ => "WRONG")
139val test11h:unit =
140 tst0 "test11h" ((mapi #2 (b, 4, SOME 0) seq "WRONG")
141 handle Subscript => "OK" | _ => "WRONG")
142val test11i:unit =
143 tst0 "test11i" ((mapi #2 (b, 4, NONE) seq "WRONG")
144 handle Subscript => "OK" | _ => "WRONG")
145end;