Debian Codegraph @

regression/widechar.sml

raw
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 (* test/widechar.sml -- test cases WideChar, 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.:
26 use "auxil.sml";
27 *)
28
29 val _ = print "File widechar.sml: Testing structure WideChar...\n"
30
31 val test4 = tstrange "test4" (0, WideChar.maxOrd)
32 (fn i => (WideChar.ord o WideChar.chr) i = i);
33
34 val test5 = tst0 "test5" ((WideChar.chr ~1 seq "WRONG") handle Chr => "OK" | _ => "WRONG")
35
36 val test6 = tst0 "test6" ((WideChar.chr (WideChar.maxOrd+1) seq "WRONG")
37 handle Chr => "OK" | _ => "WRONG")
38
39 val test18 = tst "test18" (not (WideChar.contains "" (WideChar.chr 65))
40 andalso not (WideChar.contains "aBCDE" (WideChar.chr 65))
41 andalso (WideChar.contains "ABCD" (WideChar.chr 67))
42 andalso not (WideChar.contains "" #"\000")
43 andalso not (WideChar.contains "" #"\255")
44 andalso not (WideChar.contains "azAZ09" #"\000")
45 andalso not (WideChar.contains "azAZ09" #"\255"));
46
47 val test19 = tst "test19" (WideChar.notContains "" (WideChar.chr 65)
48 andalso WideChar.notContains "aBCDE" (WideChar.chr 65)
49 andalso not (WideChar.notContains "ABCD" (WideChar.chr 67))
50 andalso WideChar.notContains "" #"\000"
51 andalso WideChar.notContains "" #"\255"
52 andalso WideChar.notContains "azAZ09" #"\000"
53 andalso WideChar.notContains "azAZ09" #"\255");
54
55 val test20 = tst "test20" (WideChar.ord WideChar.maxChar = WideChar.maxOrd);
56
57 local
58 fun mycontains s c =
59 let val stop = WideString.size s
60 fun h i = i < stop andalso (c = WideString.sub(s, i) orelse h(i+1))
61 in h 0 end;
62
63 (* Check that p(c) = (mycontains s c) for all characters: *)
64 fun equivalent p s =
65 let fun h n =
66 n > 255 orelse
67 (p (WideChar.chr n) = mycontains s (WideChar.chr n)) andalso h(n+1)
68 in h 0 end
69
70 fun checkset p s = tst' "checkset" (fn _ => equivalent p s);
71
72 val graphchars : WideString.string
73 = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\
74 \[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
75
76 val ascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
77 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
78 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
79 \ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\127"
80
81 val lowerascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
82 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
83 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
84 \abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\127"
85
86 val upperascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
87 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
88 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
89 \ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\127"
90
91 val allchars =
92 let fun h 0 res = WideChar.chr 0 :: res
93 | h n res = h (n-1) (WideChar.chr n :: res)
94 in h 255 [] end
95
96 open WideChar
97 in
98
99 val test21 =
100 checkset isLower "abcdefghijklmnopqrstuvwxyz";
101 val test22 =
102 checkset isUpper "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
103 val test23 =
104 checkset isDigit "0123456789";
105 val test24 =
106 checkset isAlpha "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
107 val test25 =
108 checkset isHexDigit "0123456789abcdefABCDEF";
109 val test26 =
110 checkset isAlphaNum
111 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
112 val test27 =
113 checkset isPrint (WideString.^ (" ", graphchars))
114 val test28 =
115 checkset isSpace " \009\010\011\012\013";
116 val test29 =
117 checkset isGraph graphchars
118 val test30 =
119 checkset isAscii ascii
120
121 val test31 =
122 tst' "test31" (fn _ => map toLower (WideString.explode ascii) =
123 WideString.explode lowerascii)
124 val test32 =
125 tst' "test32" (fn _ => map toUpper (WideString.explode ascii) =
126 WideString.explode upperascii)
127 val test33 =
128 tst' "test33" (fn _ =>
129 map toUpper (WideString.explode graphchars)
130 seq map toLower (WideString.explode graphchars)
131 seq true)
132
133 val test34a =
134 tst' "test34a" (fn _ =>
135 map pred (List.drop(allchars, 1)) = List.take(allchars, 255));
136 val test34b = tst0 "test34b" ((pred minChar seq "WRONG")
137 handle Chr => "OK" | _ => "WRONG")
138 val test35a =
139 tst' "test35a" (fn _ =>
140 map succ (List.take(allchars, 255)) = List.drop(allchars, 1));
141 val test35b = tst0 "test35b" ((succ maxChar seq "WRONG")
142 handle Chr => "OK" | _ => "WRONG")
143 end
144
145
146 (* Test cases for SML character escape functions. *)
147
148 val test36 =
149 let fun chk (arg, res) = WideChar.toString arg = res
150 in tst' "test36" (fn _ => List.all chk
151 [(#"\000", "\\^@"),
152 (#"\001", "\\^A"),
153 (#"\006", "\\^F"),
154 (#"\007", "\\a"),
155 (#"\008", "\\b"),
156 (#"\009", "\\t"),
157 (#"\010", "\\n"),
158 (#"\011", "\\v"),
159 (#"\012", "\\f"),
160 (#"\013", "\\r"),
161 (#"\014", "\\^N"),
162 (#"\031", "\\^_"),
163 (#"\032", " "),
164 (#"\126", "~"),
165 (#"\\", "\\\\"),
166 (#"\"", "\\\""),
167 (#"A", "A"),
168 (#"\127", "\\127"),
169 (#"\128", "\\128"),
170 (#"\255", "\\255")])
171 end;
172
173 val test37 =
174 let val chars = List.tabulate(256, WideChar.chr)
175 fun chk c = WideChar.fromString(WideChar.toString c) = SOME c
176 in tst' "test37" (fn _ => List.all chk chars) end
177
178 val test38 =
179 let fun chkFromString (arg, res) = WideChar.fromString arg = SOME res
180 val argResList =
181 [("A", #"A"),
182 ("z", #"z"),
183 ("@", #"@"),
184 ("~", #"~"),
185 ("\\a", #"\007"),
186 ("\\b", #"\008"),
187 ("\\t", #"\009"),
188 ("\\n", #"\010"),
189 ("\\v", #"\011"),
190 ("\\f", #"\012"),
191 ("\\r", #"\013"),
192 ("\\\\", #"\\"),
193 ("\\\"", #"\""),
194 ("\\^@", #"\000"),
195 ("\\^A", #"\001"),
196 ("\\^Z", #"\026"),
197 ("\\^_", #"\031"),
198 ("\\000", #"\000"),
199 ("\\097", #"a"),
200 ("\\255", #"\255"),
201 ("\\256", #"\256"),
202 ("\\999", #"\999"),
203 ("\\u0000", #"\000"),
204 ("\\u67ab", #"\u67ab"),
205 ("\\U001067ab", #"\U001067ab"),
206 ("\\U0010ffff", #"\U0010ffff"),
207 ("\\ \t\n\n \\A", #"A"),
208 ("\\ \t\n\n \\z", #"z"),
209 ("\\ \t\n\n \\@", #"@"),
210 ("\\ \t\n\n \\~", #"~"),
211 ("\\ \t\n\n \\\\n", #"\n"),
212 ("\\ \t\n\n \\\\t", #"\t"),
213 ("\\ \t\n\n \\\\\\", #"\\"),
214 ("\\ \t\n\n \\\\\"", #"\""),
215 ("\\ \t\n\n \\\\^@", #"\000"),
216 ("\\ \t\n\n \\\\^A", #"\001"),
217 ("\\ \t\n\n \\\\^Z", #"\026"),
218 ("\\ \t\n\n \\\\^_", #"\031"),
219 ("\\ \t\n\n \\\\000", #"\000"),
220 ("\\ \t\n\n \\\\097", #"a"),
221 ("\\ \t\n\n \\\\255", #"\255")]
222 in
223 tst' "test38" (fn _ => List.all chkFromString argResList)
224 end;
225
226 val test39 =
227 tst' "test39" (fn _ => List.all (fn arg => WideChar.fromString arg = NONE)
228 ["\\",
229 "\\c",
230 "\\F",
231 "\\e",
232 "\\g",
233 "\\N",
234 "\\T",
235 "\\1",
236 "\\11",
237 "\\-65",
238 "\\~65",
239 "\\?",
240 "\\^`",
241 "\\^a",
242 "\\^z",
243 "\\U00110000", (* outside the range of Unicode *)
244 "\\ a",
245 "\\ a\\B",
246 "\\ \\"]);
247
248 (* Test cases for C string escape functions *)
249
250 val test40 =
251 let val chars = List.tabulate(256, WideChar.chr)
252 in tst' "test40" (fn _ =>
253 List.map SOME chars
254 = List.map WideChar.fromCString (List.map WideChar.toCString chars))
255 end;
256
257 val test41 =
258 let val argResList =
259 [(#"\010", "\\n"),
260 (#"\009", "\\t"),
261 (#"\011", "\\v"),
262 (#"\008", "\\b"),
263 (#"\013", "\\r"),
264 (#"\012", "\\f"),
265 (#"\007", "\\a"),
266 (#"\\", "\\\\"),
267 (#"?", "\\?"),
268 (#"'", "\\'"),
269 (#"\"", "\\\"")]
270 in
271 tst' "test41" (fn _ =>
272 List.all (fn (arg, res) => WideChar.toCString arg = res) argResList)
273 end;
274
275 val test42 =
276 let fun checkFromCStringSucc (arg, res) =
277 WideString.str (valOf (WideChar.fromCString arg)) = res
278 val argResList =
279 [("\\n", "\010"),
280 ("\\t", "\009"),
281 ("\\v", "\011"),
282 ("\\b", "\008"),
283 ("\\r", "\013"),
284 ("\\f", "\012"),
285 ("\\a", "\007"),
286 ("\\\\", "\\"),
287 ("\\?", "?"),
288 ("\\'", "'"),
289 ("\\\"", "\""),
290 ("\\1", "\001"),
291 ("\\11", "\009"),
292 ("\\111", "\073"),
293 ("\\1007", "\064"),
294 ("\\100A", "\064"),
295 ("\\0", "\000"),
296 ("\\377", "\255"),
297 ("\\18", "\001"),
298 ("\\178", "\015"),
299 ("\\1C", "\001"),
300 ("\\17C", "\015"),
301 ("\\x0", "\000"),
302 ("\\xff", "\255"),
303 ("\\xFF", "\255"),
304 ("\\x1", "\001"),
305 ("\\x11", "\017"),
306 ("\\xag", "\010"),
307 ("\\xAAg", "\170"),
308 ("\\u0000", "\000"),
309 ("\\x67ab", "\u67ab"),
310 ("\\u67ab", "\u67ab"),
311 ("\\uffff", "\uffff"),
312 ("\\U001067ab", "\U001067ab"),
313 ("\\U0010ffff", "\U0010ffff"),
314 ("\\x0000000a", "\010"),
315 ("\\x0000000a2", "\162"),
316 ("\\x0000000ag", "\010"),
317 ("\\x0000000A", "\010"),
318 ("\\x0000000A2", "\162"),
319 ("\\x0000000Ag", "\010"),
320 ("\\x00000000000000000000000000000000000000000000000000000000000000011+",
321 "\017")
322 ]
323 in
324 tst' "test42" (fn _ => List.all checkFromCStringSucc argResList)
325 end;
326
327 val test43 =
328 let fun checkFromCStringFail arg = WideChar.fromCString arg = NONE
329 in
330 tst' "test43" (fn _ => List.all checkFromCStringFail
331 ["\\",
332 "\\X",
333 "\\=",
334 "\\8",
335 "\\9",
336 "\\c",
337 "\\d",
338 "\\x",
339 "\\U00110000", (* outside the range of Unicode *)
340 "\\xG"])
341 end;