Backport from sid to buster
[hcoop/debian/mlton.git] / regression / bytechar.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/bytechar.sml -- test cases for Byte and Char, suitable for ASCII
23 PS 1994-12-10, 1995-05-11, 1995-11-10, 1996-09-30 *)
24
25(*KILL 05/11/1997 10:55. tho.:
26use "auxil.sml";
27*)
28
29val _ = print "\nFile bytechar.sml: Testing structures Byte and Char...\n"
30
31local
32
33in
34
35val test1 = tstrange "test1" (0,255) (fn i =>
36 (Word8.toInt o Byte.charToByte o Byte.byteToChar o Word8.fromInt) i = i);
37
38val test2 = tstrange "test2" (0,Char.maxOrd) (fn i =>
39 (Word8.toInt o Byte.charToByte o Char.chr) i = i);
40
41val test3 = tstrange "test3" (0,255)
42 (fn i => (Char.ord o Byte.byteToChar o Word8.fromInt) i = i);
43
44val test4 = tstrange "test4" (0, Char.maxOrd)
45 (fn i => (Char.ord o Char.chr) i = i);
46
47val test5 = tst0 "test5" ((Char.chr ~1 seq "WRONG") handle Chr => "OK" | _ => "WRONG")
48
49val test6 = tst0 "test6" ((Char.chr (Char.maxOrd+1) seq "WRONG")
50 handle Chr => "OK" | _ => "WRONG")
51
52val test7 = tst "test7" ("" = Byte.bytesToString (Word8Vector.fromList []));
53
54val test8 =
55 tst "test8" ("ABDC" =
56 (Byte.bytesToString o Word8Vector.fromList o map Word8.fromInt)
57 [65, 66, 68, 67]);
58
59val unpackString = Byte.unpackString o Word8ArraySlice.slice
60
61val test9 = tst "test9" ("" = unpackString (Word8Array.fromList [], 0, SOME 0))
62
63local
64 val arr = Word8Array.tabulate(10, fn i => Word8.fromInt(i+65))
65in
66val test10a = tst "test10a" ("" = unpackString(arr, 0, SOME 0));
67val test10b = tst "test10b" ("" = unpackString(arr, 10, SOME 0)
68 andalso "" = unpackString(arr, 10, NONE));
69val test10c = tst "test10c" ("BCDE" = unpackString(arr, 1, SOME 4));
70val test10d = tst0 "test10d" ((unpackString(arr, ~1, SOME 0) seq "WRONG")
71 handle Subscript => "OK" | _ => "WRONG")
72val test10e = tst0 "test10e" ((unpackString(arr, 11, SOME 0) seq "WRONG")
73 handle Subscript => "OK" | _ => "WRONG")
74val test10f = tst0 "test10f" ((unpackString(arr, 0, SOME ~1) seq "WRONG")
75 handle Subscript => "OK" | _ => "WRONG")
76val test10g = tst0 "test10g" ((unpackString(arr, 0, SOME 11) seq "WRONG")
77 handle Subscript => "OK" | _ => "WRONG")
78val test10h = tst0 "test10h" ((unpackString(arr, 10, SOME 1) seq "WRONG")
79 handle Subscript => "OK" | _ => "WRONG")
80val test10i = tst0 "test10i" ((unpackString(arr, ~1, NONE) seq "WRONG")
81 handle Subscript => "OK" | _ => "WRONG")
82val test10j = tst0 "test10j" ((unpackString(arr, 11, NONE) seq "WRONG")
83 handle Subscript => "OK" | _ => "WRONG")
84end
85
86val unpackStringVec = Byte.unpackStringVec o Word8VectorSlice.slice
87local
88 val vec = Word8Vector.tabulate(10, fn i => Word8.fromInt(i+65))
89in
90val test11a = tst "test11a" ("" = unpackStringVec(vec, 0, SOME 0));
91val test11b = tst "test11b" ("" = unpackStringVec(vec, 10, SOME 0)
92 andalso "" = unpackStringVec(vec, 10, NONE));
93val test11c = tst "test11c" ("BCDE" = unpackStringVec(vec, 1, SOME 4));
94val test11d = tst0 "test11d" ((unpackStringVec(vec, ~1, SOME 0) seq "WRONG")
95 handle Subscript => "OK" | _ => "WRONG")
96val test11e = tst0 "test11e" ((unpackStringVec(vec, 11, SOME 0) seq "WRONG")
97 handle Subscript => "OK" | _ => "WRONG")
98val test11f = tst0 "test11f" ((unpackStringVec(vec, 0, SOME ~1) seq "WRONG")
99 handle Subscript => "OK" | _ => "WRONG")
100val test11g = tst0 "test11g" ((unpackStringVec(vec, 0, SOME 11) seq "WRONG")
101 handle Subscript => "OK" | _ => "WRONG")
102val test11h = tst0 "test11h" ((unpackStringVec(vec, 10, SOME 1) seq "WRONG")
103 handle Subscript => "OK" | _ => "WRONG")
104val test11i = tst0 "test11i" ((unpackStringVec(vec, ~1, NONE) seq "WRONG")
105 handle Subscript => "OK" | _ => "WRONG")
106val test11j = tst0 "test11j" ((unpackStringVec(vec, 11, NONE) seq "WRONG")
107 handle Subscript => "OK" | _ => "WRONG")
108end
109
110val test18 = tst "test18" (not (Char.contains "" (Char.chr 65))
111 andalso not (Char.contains "aBCDE" (Char.chr 65))
112 andalso (Char.contains "ABCD" (Char.chr 67))
113 andalso not (Char.contains "" #"\000")
114 andalso not (Char.contains "" #"\255")
115 andalso not (Char.contains "azAZ09" #"\000")
116 andalso not (Char.contains "azAZ09" #"\255"));
117
118val test19 = tst "test19" (Char.notContains "" (Char.chr 65)
119 andalso Char.notContains "aBCDE" (Char.chr 65)
120 andalso not (Char.notContains "ABCD" (Char.chr 67))
121 andalso Char.notContains "" #"\000"
122 andalso Char.notContains "" #"\255"
123 andalso Char.notContains "azAZ09" #"\000"
124 andalso Char.notContains "azAZ09" #"\255");
125
126val test20 = tst "test20" (Char.ord Char.maxChar = Char.maxOrd);
127
128local
129fun mycontains s c =
130 let val stop = String.size s
131 fun h i = i < stop andalso (c = String.sub(s, i) orelse h(i+1))
132 in h 0 end;
133
134(* Check that p(c) = (mycontains s c) for all characters: *)
135fun equivalent p s =
136 let fun h n =
137 n > 255 orelse
138 (p (chr n) = mycontains s (chr n)) andalso h(n+1)
139 in h 0 end
140
141fun checkset p s = tst' "checkset" (fn _ => equivalent p s);
142
143val graphchars = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\
144 \[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
145
146val ascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
147 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
148 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
149 \ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\127"
150
151val lowerascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
152 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
153 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
154 \abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\127"
155
156val upperascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
157 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
158 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
159 \ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\127"
160
161val allchars =
162 let fun h 0 res = chr 0 :: res
163 | h n res = h (n-1) (chr n :: res)
164 in h 255 [] end
165
166open Char
167in
168
169val test21 =
170 checkset isLower "abcdefghijklmnopqrstuvwxyz";
171val test22 =
172 checkset isUpper "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
173val test23 =
174 checkset isDigit "0123456789";
175val test24 =
176 checkset isAlpha "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
177val test25 =
178 checkset isHexDigit "0123456789abcdefABCDEF";
179val test26 =
180 checkset isAlphaNum
181 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
182val test27 =
183 checkset isPrint (" " ^ graphchars)
184val test28 =
185 checkset isSpace " \009\010\011\012\013";
186val test29 =
187 checkset isGraph graphchars
188val test30 =
189 checkset isAscii ascii
190
191val test31 =
192 tst' "test31" (fn _ => map toLower (explode ascii) = explode lowerascii)
193val test32 =
194 tst' "test32" (fn _ => map toUpper (explode ascii) = explode upperascii)
195val test33 =
196 tst' "test33" (fn _ =>
197 map toUpper (explode graphchars)
198 seq map toLower (explode graphchars)
199 seq true)
200
201val test34a =
202 tst' "test34a" (fn _ =>
203 map pred (List.drop(allchars, 1)) = List.take(allchars, 255));
204val test34b = tst0 "test34b" ((pred minChar seq "WRONG")
205 handle Chr => "OK" | _ => "WRONG")
206val test35a =
207 tst' "test35a" (fn _ =>
208 map succ (List.take(allchars, 255)) = List.drop(allchars, 1));
209val test35b = tst0 "test35b" ((succ maxChar seq "WRONG")
210 handle Chr => "OK" | _ => "WRONG")
211end
212
213
214(* Test cases for SML character escape functions. *)
215
216val test36 =
217 let fun chk (arg, res) = Char.toString arg = res
218 in tst' "test36" (fn _ => List.all chk
219 [(#"\000", "\\^@"),
220 (#"\001", "\\^A"),
221 (#"\006", "\\^F"),
222 (#"\007", "\\a"),
223 (#"\008", "\\b"),
224 (#"\009", "\\t"),
225 (#"\010", "\\n"),
226 (#"\011", "\\v"),
227 (#"\012", "\\f"),
228 (#"\013", "\\r"),
229 (#"\014", "\\^N"),
230 (#"\031", "\\^_"),
231 (#"\032", " "),
232 (#"\126", "~"),
233 (#"\\", "\\\\"),
234 (#"\"", "\\\""),
235 (#"A", "A"),
236 (#"\127", "\\127"),
237 (#"\128", "\\128"),
238 (#"\255", "\\255")])
239 end;
240
241val test37 =
242 let val chars = List.tabulate(256, chr)
243 fun chk c = Char.fromString(Char.toString c) = SOME c
244 in tst' "test37" (fn _ => List.all chk chars) end
245
246val test38 =
247 let fun chkFromString (arg, res) = Char.fromString arg = SOME res
248 val argResList =
249 [("A", #"A"),
250 ("z", #"z"),
251 ("@", #"@"),
252 ("~", #"~"),
253 ("\\a", #"\007"),
254 ("\\b", #"\008"),
255 ("\\t", #"\009"),
256 ("\\n", #"\010"),
257 ("\\v", #"\011"),
258 ("\\f", #"\012"),
259 ("\\r", #"\013"),
260 ("\\\\", #"\\"),
261 ("\\\"", #"\""),
262 ("\\^@", #"\000"),
263 ("\\^A", #"\001"),
264 ("\\^Z", #"\026"),
265 ("\\^_", #"\031"),
266 ("\\000", #"\000"),
267 ("\\097", #"a"),
268 ("\\255", #"\255"),
269 ("\\ \t\n\n \\A", #"A"),
270 ("\\ \t\n\n \\z", #"z"),
271 ("\\ \t\n\n \\@", #"@"),
272 ("\\ \t\n\n \\~", #"~"),
273 ("\\ \t\n\n \\\\n", #"\n"),
274 ("\\ \t\n\n \\\\t", #"\t"),
275 ("\\ \t\n\n \\\\\\", #"\\"),
276 ("\\ \t\n\n \\\\\"", #"\""),
277 ("\\ \t\n\n \\\\^@", #"\000"),
278 ("\\ \t\n\n \\\\^A", #"\001"),
279 ("\\ \t\n\n \\\\^Z", #"\026"),
280 ("\\ \t\n\n \\\\^_", #"\031"),
281 ("\\ \t\n\n \\\\000", #"\000"),
282 ("\\ \t\n\n \\\\097", #"a"),
283 ("\\ \t\n\n \\\\255", #"\255")]
284 in
285 tst' "test38" (fn _ => List.all chkFromString argResList)
286 end;
287
288val test39 =
289 tst' "test39" (fn _ => List.all (fn arg => Char.fromString arg = NONE)
290 ["\\",
291 "\\c",
292 "\\F",
293 "\\e",
294 "\\g",
295 "\\N",
296 "\\T",
297 "\\1",
298 "\\11",
299 "\\256",
300 "\\-65",
301 "\\~65",
302 "\\?",
303 "\\^`",
304 "\\^a",
305 "\\^z",
306 "\\ a",
307 "\\ a\\B",
308 "\\ \\"]);
309
310(* Test cases for C string escape functions *)
311
312val test40 =
313 let val chars = List.tabulate(256, chr)
314 in tst' "test40" (fn _ =>
315 List.map SOME chars
316 = List.map Char.fromCString (List.map Char.toCString chars))
317 end;
318
319val test41 =
320 let val argResList =
321 [(#"\010", "\\n"),
322 (#"\009", "\\t"),
323 (#"\011", "\\v"),
324 (#"\008", "\\b"),
325 (#"\013", "\\r"),
326 (#"\012", "\\f"),
327 (#"\007", "\\a"),
328 (#"\\", "\\\\"),
329 (#"?", "\\?"),
330 (#"'", "\\'"),
331 (#"\"", "\\\"")]
332 in
333 tst' "test41" (fn _ =>
334 List.all (fn (arg, res) => Char.toCString arg = res) argResList)
335 end;
336
337val test42 =
338 let fun checkFromCStringSucc (arg, res) =
339 str (valOf (Char.fromCString arg)) = res
340 val argResList =
341 [("\\n", "\010"),
342 ("\\t", "\009"),
343 ("\\v", "\011"),
344 ("\\b", "\008"),
345 ("\\r", "\013"),
346 ("\\f", "\012"),
347 ("\\a", "\007"),
348 ("\\\\", "\\"),
349 ("\\?", "?"),
350 ("\\'", "'"),
351 ("\\\"", "\""),
352 ("\\1", "\001"),
353 ("\\11", "\009"),
354 ("\\111", "\073"),
355 ("\\1007", "\064"),
356 ("\\100A", "\064"),
357 ("\\0", "\000"),
358 ("\\377", "\255"),
359 ("\\18", "\001"),
360 ("\\178", "\015"),
361 ("\\1C", "\001"),
362 ("\\17C", "\015"),
363 ("\\x0", "\000"),
364 ("\\xff", "\255"),
365 ("\\xFF", "\255"),
366 ("\\x1", "\001"),
367 ("\\x11", "\017"),
368 ("\\xag", "\010"),
369 ("\\xAAg", "\170"),
370 ("\\x0000000a", "\010"),
371 ("\\x0000000a2", "\162"),
372 ("\\x0000000ag", "\010"),
373 ("\\x0000000A", "\010"),
374 ("\\x0000000A2", "\162"),
375 ("\\x0000000Ag", "\010"),
376 ("\\x00000000000000000000000000000000000000000000000000000000000000011+",
377 "\017")
378 ]
379 in
380 tst' "test42" (fn _ => List.all checkFromCStringSucc argResList)
381 end;
382
383val test43 =
384 let fun checkFromCStringFail arg = Char.fromCString arg = NONE
385 in
386 tst' "test43" (fn _ => List.all checkFromCStringFail
387 ["\\",
388 "\\X",
389 "\\=",
390 "\\400",
391 "\\777",
392 "\\8",
393 "\\9",
394 "\\c",
395 "\\d",
396 "\\x",
397 "\\x100",
398 "\\xG"])
399 end;
400end