File: widechar.sml

package info (click to toggle)
mlton 20130715-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 60,900 kB
  • ctags: 69,386
  • sloc: xml: 34,418; ansic: 17,399; lisp: 2,879; makefile: 1,605; sh: 1,254; pascal: 256; python: 143; asm: 97
file content (341 lines) | stat: -rw-r--r-- 11,441 bytes parent folder | download | duplicates (8)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
(* 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  

(* test/widechar.sml -- test cases WideChar, suitable for ASCII
   PS 1994-12-10, 1995-05-11, 1995-11-10, 1996-09-30 *)

(*KILL 05/11/1997 10:55. tho.:
use "auxil.sml";
*)

val _ = print "File widechar.sml: Testing structure WideChar...\n"

val test4 = tstrange "test4" (0, WideChar.maxOrd) 
    (fn i => (WideChar.ord o WideChar.chr) i = i);

val test5 = tst0 "test5" ((WideChar.chr ~1 seq "WRONG") handle Chr => "OK" | _ => "WRONG")

val test6 = tst0 "test6" ((WideChar.chr (WideChar.maxOrd+1) seq "WRONG") 
                          handle Chr => "OK" | _ => "WRONG")
        
val test18 = tst "test18" (not (WideChar.contains "" (WideChar.chr 65))
                   andalso not (WideChar.contains "aBCDE" (WideChar.chr 65))
                   andalso (WideChar.contains "ABCD" (WideChar.chr 67))
                   andalso not (WideChar.contains "" #"\000")
                   andalso not (WideChar.contains "" #"\255")
                   andalso not (WideChar.contains "azAZ09" #"\000")
                   andalso not (WideChar.contains "azAZ09" #"\255"));

val test19 = tst "test19" (WideChar.notContains "" (WideChar.chr 65)
                   andalso WideChar.notContains "aBCDE" (WideChar.chr 65)
                   andalso not (WideChar.notContains "ABCD" (WideChar.chr 67))
                   andalso WideChar.notContains "" #"\000"
                   andalso WideChar.notContains "" #"\255"
                   andalso WideChar.notContains "azAZ09" #"\000"
                   andalso WideChar.notContains "azAZ09" #"\255");

val test20 = tst "test20" (WideChar.ord WideChar.maxChar = WideChar.maxOrd);

local 
fun mycontains s c = 
    let val stop = WideString.size s
        fun h i = i < stop andalso (c = WideString.sub(s, i) orelse h(i+1))
    in h 0 end;

(* Check that p(c) = (mycontains s c) for all characters: *)
fun equivalent p s = 
    let fun h n =
        n > 255 orelse 
        (p (WideChar.chr n) = mycontains s (WideChar.chr n)) andalso h(n+1)
    in h 0 end

fun checkset p s = tst' "checkset" (fn _ => equivalent p s);

val graphchars : WideString.string 
               = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\
                 \[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

val ascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
             \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
             \ !\"#$%&'()*+,-./0123456789:;<=>?@\
             \ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\127" 

val lowerascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
 \abcdefghijklmnopqrstuvwxyz[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\127" 

val upperascii = "\^@\^A\^B\^C\^D\^E\^F\^G\^H\t\n\^K\^L\^M\^N\^O\^P\
 \\^Q\^R\^S\^T\^U\^V\^W\^X\^Y\^Z\^[\^\\^]\^^\^_\
 \ !\"#$%&'()*+,-./0123456789:;<=>?@\
 \ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\127" 

val allchars = 
    let fun h 0 res = WideChar.chr 0 :: res
          | h n res = h (n-1) (WideChar.chr n :: res)
    in h 255 [] end

open WideChar
in

val test21 = 
    checkset isLower "abcdefghijklmnopqrstuvwxyz";
val test22 = 
    checkset isUpper "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
val test23 = 
    checkset isDigit "0123456789";
val test24 = 
    checkset isAlpha "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
val test25 = 
    checkset isHexDigit "0123456789abcdefABCDEF";
val test26 = 
    checkset isAlphaNum 
       "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
val test27 = 
    checkset isPrint (WideString.^ (" ", graphchars))
val test28 = 
    checkset isSpace " \009\010\011\012\013";
val test29 = 
    checkset isGraph graphchars
val test30 = 
    checkset isAscii ascii

val test31 = 
    tst' "test31" (fn _ => map toLower (WideString.explode ascii) = 
                           WideString.explode lowerascii)
val test32 = 
    tst' "test32" (fn _ => map toUpper (WideString.explode ascii) = 
                           WideString.explode upperascii)
val test33 = 
    tst' "test33" (fn _ => 
           map toUpper (WideString.explode graphchars)
           seq map toLower (WideString.explode graphchars)
           seq true)

val test34a =
    tst' "test34a" (fn _ => 
           map pred (List.drop(allchars, 1)) = List.take(allchars, 255));
val test34b = tst0 "test34b" ((pred minChar seq "WRONG")
                              handle Chr => "OK" | _ => "WRONG")
val test35a =
    tst' "test35a" (fn _ => 
           map succ (List.take(allchars, 255)) = List.drop(allchars, 1));
val test35b = tst0 "test35b" ((succ maxChar seq "WRONG")
                              handle Chr => "OK" | _ => "WRONG")
end


(* Test cases for SML character escape functions. *)

val test36 = 
    let fun chk (arg, res) = WideChar.toString arg = res
    in tst' "test36" (fn _ => List.all chk 
               [(#"\000", "\\^@"),
               (#"\001", "\\^A"),
               (#"\006", "\\^F"),
               (#"\007", "\\a"),
               (#"\008", "\\b"),
               (#"\009", "\\t"),
               (#"\010", "\\n"),
               (#"\011", "\\v"),
               (#"\012", "\\f"),
               (#"\013", "\\r"),
               (#"\014", "\\^N"),
               (#"\031", "\\^_"),
               (#"\032", " "),
               (#"\126", "~"),
               (#"\\", "\\\\"),
               (#"\"", "\\\""),
               (#"A", "A"),
               (#"\127", "\\127"),
               (#"\128", "\\128"),
               (#"\255", "\\255")])
    end;

val test37 = 
    let val chars = List.tabulate(256, WideChar.chr)
        fun chk c = WideChar.fromString(WideChar.toString c) = SOME c
    in tst' "test37" (fn _ => List.all chk chars) end

val test38 =                 
    let fun chkFromString (arg, res) = WideChar.fromString arg = SOME res
        val argResList = 
            [("A", #"A"),
             ("z", #"z"),
             ("@", #"@"),
             ("~", #"~"),
             ("\\a", #"\007"),
             ("\\b", #"\008"),
             ("\\t", #"\009"),
             ("\\n", #"\010"),
             ("\\v", #"\011"),
             ("\\f", #"\012"),
             ("\\r", #"\013"),
             ("\\\\", #"\\"),
             ("\\\"", #"\""),
             ("\\^@", #"\000"),
             ("\\^A", #"\001"),
             ("\\^Z", #"\026"),
             ("\\^_", #"\031"), 
             ("\\000", #"\000"),
             ("\\097", #"a"),
             ("\\255", #"\255"),
             ("\\256", #"\256"),
             ("\\999", #"\999"),
             ("\\u0000", #"\000"),
             ("\\u67ab", #"\u67ab"),
             ("\\U001067ab", #"\U001067ab"),
             ("\\U0010ffff", #"\U0010ffff"),
             ("\\   \t\n\n \\A", #"A"),
             ("\\   \t\n\n \\z", #"z"),
             ("\\   \t\n\n \\@", #"@"),
             ("\\   \t\n\n \\~", #"~"),
             ("\\   \t\n\n \\\\n", #"\n"),
             ("\\   \t\n\n \\\\t", #"\t"),
             ("\\   \t\n\n \\\\\\", #"\\"),
             ("\\   \t\n\n \\\\\"", #"\""),
             ("\\   \t\n\n \\\\^@", #"\000"),
             ("\\   \t\n\n \\\\^A", #"\001"),
             ("\\   \t\n\n \\\\^Z", #"\026"),
             ("\\   \t\n\n \\\\^_", #"\031"), 
             ("\\   \t\n\n \\\\000", #"\000"),
             ("\\   \t\n\n \\\\097", #"a"),
             ("\\   \t\n\n \\\\255", #"\255")]
    in 
        tst' "test38" (fn _ => List.all chkFromString argResList)
    end;

val test39 = 
    tst' "test39" (fn _ => List.all (fn arg => WideChar.fromString arg = NONE)
           ["\\",
            "\\c",
            "\\F",
            "\\e",
            "\\g",
            "\\N",
            "\\T",
            "\\1",
            "\\11",
            "\\-65",
            "\\~65",
            "\\?",
            "\\^`",
            "\\^a",
            "\\^z",
            "\\U00110000", (* outside the range of Unicode *)
            "\\   a",
            "\\   a\\B",
            "\\   \\"]);

(* Test cases for C string escape functions *)

val test40 = 
    let val chars = List.tabulate(256, WideChar.chr)
    in tst' "test40" (fn _ => 
              List.map SOME chars 
              = List.map WideChar.fromCString (List.map WideChar.toCString chars))
    end;

val test41 = 
    let val argResList = 
            [(#"\010", "\\n"),
             (#"\009", "\\t"),
             (#"\011", "\\v"),
             (#"\008", "\\b"),
             (#"\013", "\\r"),
             (#"\012", "\\f"),
             (#"\007", "\\a"),
             (#"\\", "\\\\"),
             (#"?", "\\?"),
             (#"'", "\\'"),
             (#"\"", "\\\"")]
    in
        tst' "test41" (fn _ => 
               List.all (fn (arg, res) => WideChar.toCString arg = res) argResList)
    end;

val test42 = 
    let fun checkFromCStringSucc (arg, res) = 
            WideString.str (valOf (WideChar.fromCString arg)) = res
        val argResList = 
            [("\\n", "\010"),
             ("\\t", "\009"),
             ("\\v", "\011"),
             ("\\b", "\008"),
             ("\\r", "\013"),
             ("\\f", "\012"),
             ("\\a", "\007"),
             ("\\\\",  "\\"),
             ("\\?", "?"),
             ("\\'", "'"),
             ("\\\"", "\""),
             ("\\1", "\001"),
             ("\\11", "\009"),
             ("\\111", "\073"),
             ("\\1007", "\064"),
             ("\\100A", "\064"),
             ("\\0",   "\000"),
             ("\\377", "\255"),
             ("\\18", "\001"),
             ("\\178", "\015"),
             ("\\1C", "\001"),
             ("\\17C", "\015"),
             ("\\x0", "\000"),
             ("\\xff", "\255"),
             ("\\xFF", "\255"),
             ("\\x1", "\001"),
             ("\\x11", "\017"),
             ("\\xag", "\010"),
             ("\\xAAg", "\170"),
             ("\\u0000", "\000"),
             ("\\x67ab", "\u67ab"),
             ("\\u67ab", "\u67ab"),
             ("\\uffff", "\uffff"),
             ("\\U001067ab", "\U001067ab"),
             ("\\U0010ffff", "\U0010ffff"),
             ("\\x0000000a", "\010"),
             ("\\x0000000a2", "\162"),
             ("\\x0000000ag", "\010"),
             ("\\x0000000A", "\010"),
             ("\\x0000000A2", "\162"),
             ("\\x0000000Ag", "\010"),
             ("\\x00000000000000000000000000000000000000000000000000000000000000011+",
              "\017")
             ]
    in 
        tst' "test42" (fn _ => List.all checkFromCStringSucc argResList)
    end;

val test43 = 
    let fun checkFromCStringFail arg = WideChar.fromCString arg = NONE
    in
        tst' "test43" (fn _ => List.all checkFromCStringFail 
               ["\\",
                "\\X",
                "\\=",
                "\\8",
                "\\9",
                "\\c",
                "\\d",
                "\\x",
                "\\U00110000", (* outside the range of Unicode *)
                "\\xG"])
    end;