1 (* Auxiliary functions for test cases
*)
5 fun check b
= if b
then "OK" else "WRONG";
6 fun check
' f
= (if f () then "OK" else "WRONG") handle _
=> "EXN";
8 fun range (from
, to
) p
=
11 (from
> to
) orelse (p from
) andalso (range (from
+1, to
) p
)
14 fun checkrange bounds
= check
o range bounds
;
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
);
20 fun tstrange s bounds
= (tst s
) o range bounds
22 (*check_arrays
.sml
13/10/1997 22:13. tho
.*)
24 fun impossible s
= (print
"ERROR : "; print s
; print
"\n")
26 val Char_prim_array_maxLen
= 200*4-42;
27 val Poly_prim_array_maxLen
= 200-42;
29 (*test Word8Array
structure*)
32 (print
"\nTesting structure Word8Array\n";
34 fun dot () = {}(*pr
"."*)
35 fun phase s
= {}(*pr s
*)
37 (print ("\nNow I will try with a " ^
Int.toString n ^
"-array.");
38 let val a
= Word8Array
.array (n
, 0w42
);
45 if Word8Array
.sub (a
, !i
) <> 0w42
46 then impossible ("check 1 failed: it is "
47 ^
Int.toString (Word8.toInt (Word8Array
.sub (a
, !i
))))
50 phase
"\ncheck length:";
51 if Word8Array
.length a
<> n
then
52 impossible ("length was "
53 ^
Int.toString (Word8Array
.length a
)
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
)
62 ^
Int.toString (Word8Array
.length a
* 42))
68 Word8Array
.update (a
, !i
, 0w2
* (Word8.fromInt (!i
) mod 0w20
));
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
)),
78 (Int.toString
o Word8.toInt
)(0w2
* (Word8.fromInt (!i
) mod 0w20
))])
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);
102 (*test Array
structure*)
103 print
"\nTesting structure Array\n";
105 fun dot () = () (*pr
".";*)
106 fun phase s
= () (*pr s
;*)
108 (print ("\nNow I will try with a " ^
Int.toString n ^
"-array.");
109 let val a
= Array
.array (n
, 42)
116 if Array
.sub (a
, !i
) <> 42 then impossible
"check 1 failed"
119 phase
"\ncheck length:";
120 if Array
.length a
<> n
then
121 impossible ("length was "
122 ^
Int.toString (Array
.length a
)
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
)
131 ^
Int.toString (Array
.length a
* 42))
137 Array
.update (a
, !i
, !i
* !i
);
143 if Array
.sub (a
, !i
) <> !i
* !i
then impossible
"check 2 failed"
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);
167 (*test CharArray
structure*)
169 print
"\nTesting structure CharArray\n";
171 fun dot () = () (*pr
".";*)
172 fun phase s
= () (*pr s
*)
174 fun f (* : (elem
* 'b
) -> 'b
*) (x
', b
) = x
= x
' andalso b
177 | repeat x n
= x
:: repeat
x (n
-1)
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
188 if CharArray
.sub (a
, !i
) <> x
then impossible
"check 1 failed"
191 phase
"\ncheck length:";
192 if CharArray
.length a
<> n
then
193 impossible ("length was "
194 ^
Int.toString (CharArray
.length a
)
198 phase
"\ncheck foldr:";
199 if CharArray
.foldr f b_init a
<> x_summasumarum
then
200 impossible
"foldr check failed"
206 CharArray
.update (a
, !i
, chr ((!i
mod (127-34)) + 34) );
212 if CharArray
.sub (a
, !i
) <> chr ((!i
mod (127-34)) + 34)
213 then impossible
"check 2 failed"
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);
233 print
"\ncheck done\n"