Backport from sid to buster
[hcoop/debian/mlton.git] / regression / check_arrays.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 (*check_arrays.sml 13/10/1997 22:13. tho.*)
23
24 fun impossible s = (print "ERROR : "; print s; print "\n")
25
26 val Char_prim_array_maxLen = 200*4-42;
27 val Poly_prim_array_maxLen = 200-42;
28
29 (*test Word8Array structure*)
30
31 val _ =
32 (print "\nTesting structure Word8Array\n";
33 let
34 fun dot () = {}(*pr "."*)
35 fun phase s = {}(*pr s*)
36 fun try_with n =
37 (print ("\nNow I will try with a " ^ Int.toString n ^ "-array.");
38 let val a = Word8Array.array (n, 0w42);
39 val i = ref 0
40 in
41 phase "\ncheck 1:";
42 i := 0;
43 while (!i < n) do
44 (dot ();
45 if Word8Array.sub (a, !i) <> 0w42
46 then impossible ("check 1 failed: it is "
47 ^ Int.toString (Word8.toInt (Word8Array.sub (a, !i))))
48 else ();
49 i := !i + 1);
50 phase "\ncheck length:";
51 if Word8Array.length a <> n then
52 impossible ("length was "
53 ^ Int.toString (Word8Array.length a)
54 ^ " and not "
55 ^ Int.toString n)
56 else ();
57 phase "\ncheck foldr:";
58 if (Word8Array.foldr (fn (e,a) => Word8.toInt e + a) 0 a) <> Word8Array.length a * 42 then
59 impossible ("foldr check failed: it was "
60 ^ Int.toString (Word8Array.foldr (fn (e,a) => Word8.toInt e + a) 0 a)
61 ^ " and not "
62 ^ Int.toString (Word8Array.length a * 42))
63 else ();
64 phase "\ninit:";
65 i := 0;
66 while (!i < n) do
67 (dot ();
68 Word8Array.update (a, !i, 0w2 * (Word8.fromInt (!i) mod 0w20));
69 i := !i + 1);
70 phase "\ncheck 2:";
71 i := n-1;
72 while (!i >= 0) do
73 (dot ();
74 if Word8Array.sub (a, !i) <> (0w2 * (Word8.fromInt (!i) mod 0w20))
75 then impossible (concat["check 2 failed: found ",
76 (Int.toString o Word8.toInt)(Word8Array.sub (a, !i)),
77 " and not ",
78 (Int.toString o Word8.toInt)(0w2 * (Word8.fromInt (!i) mod 0w20))])
79 else ();
80 i := !i - 1);
81 print " \tok"
82 end);
83 in
84 (try_with 119;
85 try_with 13;
86 try_with 130;
87 try_with 10000;
88 try_with 0;
89 try_with 1;
90 try_with Poly_prim_array_maxLen;
91 try_with (2 * Poly_prim_array_maxLen);
92 try_with (Poly_prim_array_maxLen + 1);
93 try_with (20 * Poly_prim_array_maxLen + 1);
94 try_with (20 * Char_prim_array_maxLen + 1);
95 print "\n")
96 end
97
98 )
99
100 val _ =
101 (
102 (*test Array structure*)
103 print "\nTesting structure Array\n";
104 let
105 fun dot () = () (*pr ".";*)
106 fun phase s = () (*pr s;*)
107 fun try_with n =
108 (print ("\nNow I will try with a " ^ Int.toString n ^ "-array.");
109 let val a = Array.array (n, 42)
110 val i = ref 0
111 in
112 phase "\ncheck 1:";
113 i := 0;
114 while (!i < n) do
115 (dot ();
116 if Array.sub (a, !i) <> 42 then impossible "check 1 failed"
117 else ();
118 i := !i + 1);
119 phase "\ncheck length:";
120 if Array.length a <> n then
121 impossible ("length was "
122 ^ Int.toString (Array.length a)
123 ^ " and not "
124 ^ Int.toString n)
125 else ();
126 phase "\ncheck foldr:";
127 if Array.foldr (op +) 0 a <> Array.length a * 42 then
128 impossible ("foldr check failed: it was "
129 ^ Int.toString (Array.foldr (op +) 0 a)
130 ^ " and not "
131 ^ Int.toString (Array.length a * 42))
132 else ();
133 phase "\ninit:";
134 i := 0;
135 while (!i < n) do
136 (dot ();
137 Array.update (a, !i, !i * !i);
138 i := !i + 1);
139 phase "\ncheck 2:";
140 i := n-1;
141 while (!i >= 0) do
142 (dot ();
143 if Array.sub (a, !i) <> !i * !i then impossible "check 2 failed"
144 else ();
145 i := !i - 1);
146 print " \tok"
147 end);
148 in
149 (try_with 119;
150 try_with 13;
151 try_with 130;
152 try_with 10000;
153 try_with 0;
154 try_with 1;
155 try_with Poly_prim_array_maxLen;
156 try_with (2 * Poly_prim_array_maxLen);
157 try_with (Poly_prim_array_maxLen + 1);
158 try_with (20 * Poly_prim_array_maxLen + 1);
159 try_with (20 * Char_prim_array_maxLen + 1);
160 print "\n")
161 end
162 )
163
164
165
166 val _ = (
167 (*test CharArray structure*)
168
169 print "\nTesting structure CharArray\n";
170 let
171 fun dot () = () (*pr ".";*)
172 fun phase s = () (*pr s*)
173 val x = #"*"
174 fun f (* : (elem * 'b) -> 'b *) (x', b) = x = x' andalso b
175 val b_init = true
176 fun repeat x 0 = []
177 | repeat x n = x :: repeat x (n-1)
178 fun try_with n =
179 (print ("\nNow I will try with a " ^ Int.toString n ^ "-array.");
180 let val a = CharArray.array (n, x)
181 val x_summasumarum = true
182 val i = ref 0
183 in
184 phase "\ncheck 1:";
185 i := 0;
186 while (!i < n) do
187 (dot ();
188 if CharArray.sub (a, !i) <> x then impossible "check 1 failed"
189 else ();
190 i := !i + 1);
191 phase "\ncheck length:";
192 if CharArray.length a <> n then
193 impossible ("length was "
194 ^ Int.toString (CharArray.length a)
195 ^ " and not "
196 ^ Int.toString n)
197 else ();
198 phase "\ncheck foldr:";
199 if CharArray.foldr f b_init a <> x_summasumarum then
200 impossible "foldr check failed"
201 else ();
202 phase "\ninit:";
203 i := 0;
204 while (!i < n) do
205 (dot ();
206 CharArray.update (a, !i, chr ((!i mod (127-34)) + 34) );
207 i := !i + 1);
208 phase "\ncheck 2:";
209 i := n-1;
210 while (!i >= 0) do
211 (dot ();
212 if CharArray.sub (a, !i) <> chr ((!i mod (127-34)) + 34)
213 then impossible "check 2 failed"
214 else ();
215 i := !i - 1);
216 print " \tok"
217 end);
218 in
219 (try_with 119;
220 try_with 13;
221 try_with 130;
222 try_with 10000;
223 try_with 0;
224 try_with 1;
225 try_with Char_prim_array_maxLen;
226 try_with (2 * Char_prim_array_maxLen);
227 try_with (Char_prim_array_maxLen + 1);
228 try_with (20 * Char_prim_array_maxLen + 1);
229 print "\n")
230 end
231
232 ;
233 print "\ncheck done\n"
234 )
235
236