File: check_arrays.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (236 lines) | stat: -rw-r--r-- 6,077 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
(* Auxiliary functions for test cases *)

infix 1 seq
fun e1 seq e2 = e2;
fun check b = if b then "OK" else "WRONG";
fun check' f = (if f () then "OK" else "WRONG") handle _ => "EXN";

fun range (from, to) p = 
    let open Int 
    in
        (from > to) orelse (p from) andalso (range (from+1, to) p)
    end;

fun checkrange bounds = check o range bounds;

fun tst0 s s' = print (s ^ "    \t" ^ s' ^ "\n");
fun tst  s b = tst0 s (check  b);
fun tst' s f = tst0 s (check' f);

fun tstrange s bounds = (tst s) o range bounds  

(*check_arrays.sml  13/10/1997 22:13. tho.*)

fun impossible s = (print "ERROR : "; print s; print "\n")

val Char_prim_array_maxLen = 200*4-42;
val Poly_prim_array_maxLen = 200-42;

(*test Word8Array structure*)

val _ =
   (print "\nTesting structure Word8Array\n";
let
fun dot () = {}(*pr "."*)
fun phase s = {}(*pr s*)
fun try_with n =
(print ("\nNow I will try with a " ^ Int.toString n ^ "-array.");
 let val a = Word8Array.array (n, 0w42);
     val i = ref 0
 in
   phase "\ncheck 1:";
   i := 0;
   while (!i < n) do
     (dot ();
      if Word8Array.sub (a, !i) <> 0w42
      then impossible ("check 1 failed: it is "
                       ^ Int.toString (Word8.toInt (Word8Array.sub (a, !i))))
      else ();
      i := !i + 1);
   phase "\ncheck length:";
   if Word8Array.length a <> n then
     impossible ("length was "
                 ^ Int.toString (Word8Array.length a)
                 ^ " and not "
                 ^ Int.toString n)
   else ();
   phase "\ncheck foldr:";
   if (Word8Array.foldr (fn (e,a) => Word8.toInt e + a) 0 a) <> Word8Array.length a * 42 then
     impossible ("foldr check failed: it was "
                 ^ Int.toString (Word8Array.foldr (fn (e,a) => Word8.toInt e + a) 0 a)
                 ^ " and not "
                 ^ Int.toString (Word8Array.length a * 42))
   else ();
   phase "\ninit:";
   i := 0;
   while (!i < n) do
     (dot ();
      Word8Array.update (a, !i, 0w2 * (Word8.fromInt (!i) mod 0w20));
      i := !i + 1);
   phase "\ncheck 2:";
   i := n-1;
   while (!i >= 0) do
     (dot ();
      if Word8Array.sub (a, !i) <> (0w2 * (Word8.fromInt (!i) mod 0w20))
      then impossible (concat["check 2 failed: found ",
                              (Int.toString o Word8.toInt)(Word8Array.sub (a, !i)),
                              " and not ",
                              (Int.toString o Word8.toInt)(0w2 * (Word8.fromInt (!i) mod 0w20))])
      else ();
      i := !i - 1);
   print "    \tok"
 end);
in
(try_with 119;
 try_with 13;
 try_with 130;
 try_with 10000;
 try_with 0;
 try_with 1;
 try_with Poly_prim_array_maxLen;
 try_with (2 * Poly_prim_array_maxLen);
 try_with (Poly_prim_array_maxLen + 1);
 try_with (20 * Poly_prim_array_maxLen + 1);
 try_with (20 * Char_prim_array_maxLen + 1);
 print "\n")
end

)

val _ =
   (
(*test Array structure*)
print "\nTesting structure Array\n";
let
fun dot () = () (*pr ".";*)
fun phase s = () (*pr s;*)
fun try_with n =
(print ("\nNow I will try with a " ^ Int.toString n ^ "-array.");
 let val a = Array.array (n, 42)
     val i = ref 0
 in
   phase "\ncheck 1:";
   i := 0;
   while (!i < n) do
     (dot ();
      if Array.sub (a, !i) <> 42 then impossible "check 1 failed"
      else ();
      i := !i + 1);
   phase "\ncheck length:";
   if Array.length a <> n then
     impossible ("length was "
                 ^ Int.toString (Array.length a)
                 ^ " and not "
                 ^ Int.toString n)
   else ();
   phase "\ncheck foldr:";
   if Array.foldr (op +) 0 a <> Array.length a * 42 then
     impossible ("foldr check failed: it was "
                 ^ Int.toString (Array.foldr (op +) 0 a)
                 ^ " and not "
                 ^ Int.toString (Array.length a * 42))
   else ();
   phase "\ninit:";
   i := 0;
   while (!i < n) do
     (dot ();
      Array.update (a, !i, !i * !i);
      i := !i + 1);
   phase "\ncheck 2:";
   i := n-1;
   while (!i >= 0) do
     (dot ();
      if Array.sub (a, !i) <> !i * !i then impossible "check 2 failed"
      else ();
      i := !i - 1);
   print "    \tok"
 end);
in
(try_with 119;
 try_with 13;
 try_with 130;
 try_with 10000;
 try_with 0;
 try_with 1;
 try_with Poly_prim_array_maxLen;
 try_with (2 * Poly_prim_array_maxLen);
 try_with (Poly_prim_array_maxLen + 1);
 try_with (20 * Poly_prim_array_maxLen + 1);
 try_with (20 * Char_prim_array_maxLen + 1);
 print "\n")
end
)



   val _ = (
(*test CharArray structure*)

print "\nTesting structure CharArray\n";
let
fun dot () = () (*pr ".";*)
fun phase s = () (*pr s*)
val x = #"*"
fun f (* : (elem * 'b) -> 'b *)  (x', b) = x = x' andalso b
val b_init = true
fun repeat x 0 = []
  | repeat x n = x :: repeat x (n-1)
fun try_with n =
(print ("\nNow I will try with a " ^ Int.toString n ^ "-array.");
 let val a = CharArray.array (n, x)
     val x_summasumarum = true
     val i = ref 0
 in
   phase "\ncheck 1:";
   i := 0;
   while (!i < n) do
     (dot ();
      if CharArray.sub (a, !i) <> x then impossible "check 1 failed"
      else ();
      i := !i + 1);
   phase "\ncheck length:";
   if CharArray.length a <> n then
     impossible ("length was "
                 ^ Int.toString (CharArray.length a)
                 ^ " and not "
                 ^ Int.toString n)
   else ();
   phase "\ncheck foldr:";
   if CharArray.foldr f b_init a <> x_summasumarum then
     impossible "foldr check failed"
   else ();
   phase "\ninit:";
   i := 0;
   while (!i < n) do
     (dot ();
      CharArray.update (a, !i, chr ((!i mod (127-34)) + 34) );
      i := !i + 1);
   phase "\ncheck 2:";
   i := n-1;
   while (!i >= 0) do
     (dot ();
      if CharArray.sub (a, !i) <> chr ((!i mod (127-34)) + 34)
      then impossible "check 2 failed"
      else ();
      i := !i - 1);
   print "    \tok"
 end);
in
(try_with 119;
 try_with 13;
 try_with 130;
 try_with 10000;
 try_with 0;
 try_with 1;
 try_with Char_prim_array_maxLen;
 try_with (2 * Char_prim_array_maxLen);
 try_with (Char_prim_array_maxLen + 1);
 try_with (20 * Char_prim_array_maxLen + 1);
 print "\n")
end

;
print "\ncheck done\n"
)