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 (* 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 *)
25 (*KILL
05/11/1997 10:55. tho
.:
29 val _
= print
"\nFile bytechar.sml: Testing structures Byte and Char...\n"
35 val test1
= tstrange
"test1" (0,255) (fn i
=>
36 (Word8.toInt
o Byte
.charToByte
o Byte
.byteToChar
o Word8.fromInt
) i
= i
);
38 val test2
= tstrange
"test2" (0,Char.maxOrd
) (fn i
=>
39 (Word8.toInt
o Byte
.charToByte
o Char.chr
) i
= i
);
41 val test3
= tstrange
"test3" (0,255)
42 (fn i
=> (Char.ord
o Byte
.byteToChar
o Word8.fromInt
) i
= i
);
44 val test4
= tstrange
"test4" (0, Char.maxOrd
)
45 (fn i
=> (Char.ord
o Char.chr
) i
= i
);
47 val test5
= tst0
"test5" ((Char.chr ~
1 seq
"WRONG") handle Chr
=> "OK" | _
=> "WRONG")
49 val test6
= tst0
"test6" ((Char.chr (Char.maxOrd
+1) seq
"WRONG")
50 handle Chr
=> "OK" | _
=> "WRONG")
52 val test7
= tst
"test7" ("" = Byte
.bytesToString (Word8Vector
.fromList
[]));
56 (Byte
.bytesToString
o Word8Vector
.fromList
o map
Word8.fromInt
)
59 val unpackString
= Byte
.unpackString
o Word8ArraySlice
.slice
61 val test9
= tst
"test9" ("" = unpackString (Word8Array
.fromList
[], 0, SOME
0))
64 val arr
= Word8Array
.tabulate(10, fn i
=> Word8.fromInt(i
+65))
66 val test10a
= tst
"test10a" ("" = unpackString(arr
, 0, SOME
0));
67 val test10b
= tst
"test10b" ("" = unpackString(arr
, 10, SOME
0)
68 andalso "" = unpackString(arr
, 10, NONE
));
69 val test10c
= tst
"test10c" ("BCDE" = unpackString(arr
, 1, SOME
4));
70 val test10d
= tst0
"test10d" ((unpackString(arr
, ~
1, SOME
0) seq
"WRONG")
71 handle Subscript
=> "OK" | _
=> "WRONG")
72 val test10e
= tst0
"test10e" ((unpackString(arr
, 11, SOME
0) seq
"WRONG")
73 handle Subscript
=> "OK" | _
=> "WRONG")
74 val test10f
= tst0
"test10f" ((unpackString(arr
, 0, SOME ~
1) seq
"WRONG")
75 handle Subscript
=> "OK" | _
=> "WRONG")
76 val test10g
= tst0
"test10g" ((unpackString(arr
, 0, SOME
11) seq
"WRONG")
77 handle Subscript
=> "OK" | _
=> "WRONG")
78 val test10h
= tst0
"test10h" ((unpackString(arr
, 10, SOME
1) seq
"WRONG")
79 handle Subscript
=> "OK" | _
=> "WRONG")
80 val test10i
= tst0
"test10i" ((unpackString(arr
, ~
1, NONE
) seq
"WRONG")
81 handle Subscript
=> "OK" | _
=> "WRONG")
82 val test10j
= tst0
"test10j" ((unpackString(arr
, 11, NONE
) seq
"WRONG")
83 handle Subscript
=> "OK" | _
=> "WRONG")
86 val unpackStringVec
= Byte
.unpackStringVec
o Word8VectorSlice
.slice
88 val vec
= Word8Vector
.tabulate(10, fn i
=> Word8.fromInt(i
+65))
90 val test11a
= tst
"test11a" ("" = unpackStringVec(vec
, 0, SOME
0));
91 val test11b
= tst
"test11b" ("" = unpackStringVec(vec
, 10, SOME
0)
92 andalso "" = unpackStringVec(vec
, 10, NONE
));
93 val test11c
= tst
"test11c" ("BCDE" = unpackStringVec(vec
, 1, SOME
4));
94 val test11d
= tst0
"test11d" ((unpackStringVec(vec
, ~
1, SOME
0) seq
"WRONG")
95 handle Subscript
=> "OK" | _
=> "WRONG")
96 val test11e
= tst0
"test11e" ((unpackStringVec(vec
, 11, SOME
0) seq
"WRONG")
97 handle Subscript
=> "OK" | _
=> "WRONG")
98 val test11f
= tst0
"test11f" ((unpackStringVec(vec
, 0, SOME ~
1) seq
"WRONG")
99 handle Subscript
=> "OK" | _
=> "WRONG")
100 val test11g
= tst0
"test11g" ((unpackStringVec(vec
, 0, SOME
11) seq
"WRONG")
101 handle Subscript
=> "OK" | _
=> "WRONG")
102 val test11h
= tst0
"test11h" ((unpackStringVec(vec
, 10, SOME
1) seq
"WRONG")
103 handle Subscript
=> "OK" | _
=> "WRONG")
104 val test11i
= tst0
"test11i" ((unpackStringVec(vec
, ~
1, NONE
) seq
"WRONG")
105 handle Subscript
=> "OK" | _
=> "WRONG")
106 val test11j
= tst0
"test11j" ((unpackStringVec(vec
, 11, NONE
) seq
"WRONG")
107 handle Subscript
=> "OK" | _
=> "WRONG")
110 val 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"));
118 val 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");
126 val test20
= tst
"test20" (Char.ord
Char.maxChar
= Char.maxOrd
);
130 let val stop
= String.size s
131 fun h i
= i
< stop
andalso (c
= String.sub(s
, i
) orelse h(i
+1))
134 (* Check that
p(c
) = (mycontains s c
) for all characters
: *)
138 (p (chr n
) = mycontains
s (chr n
)) andalso h(n
+1)
141 fun checkset p s
= tst
' "checkset" (fn _
=> equivalent p s
);
143 val graphchars
= "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\
144 \[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
146 val 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"
151 val 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"
156 val 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"
162 let fun h
0 res
= chr
0 :: res
163 | h n res
= h (n
-1) (chr n
:: res
)
170 checkset isLower
"abcdefghijklmnopqrstuvwxyz";
172 checkset isUpper
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
174 checkset isDigit
"0123456789";
176 checkset isAlpha
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
178 checkset isHexDigit
"0123456789abcdefABCDEF";
181 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
183 checkset
isPrint (" " ^ graphchars
)
185 checkset isSpace
" \009\010\011\012\013";
187 checkset isGraph graphchars
189 checkset isAscii ascii
192 tst
' "test31" (fn _
=> map
toLower (explode ascii
) = explode lowerascii
)
194 tst
' "test32" (fn _
=> map
toUpper (explode ascii
) = explode upperascii
)
196 tst
' "test33" (fn _
=>
197 map
toUpper (explode graphchars
)
198 seq map
toLower (explode graphchars
)
202 tst
' "test34a" (fn _
=>
203 map
pred (List.drop(allchars
, 1)) = List.take(allchars
, 255));
204 val test34b
= tst0
"test34b" ((pred minChar seq
"WRONG")
205 handle Chr
=> "OK" | _
=> "WRONG")
207 tst
' "test35a" (fn _
=>
208 map
succ (List.take(allchars
, 255)) = List.drop(allchars
, 1));
209 val test35b
= tst0
"test35b" ((succ maxChar seq
"WRONG")
210 handle Chr
=> "OK" | _
=> "WRONG")
214 (* Test cases for SML character escape functions
. *)
217 let fun chk (arg
, res
) = Char.toString arg
= res
218 in tst
' "test36" (fn _
=> List.all chk
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
247 let fun chkFromString (arg
, res
) = Char.fromString arg
= SOME res
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")]
285 tst
' "test38" (fn _
=> List.all chkFromString argResList
)
289 tst
' "test39" (fn _
=> List.all (fn arg
=> Char.fromString arg
= NONE
)
310 (* Test cases for C
string escape functions
*)
313 let val chars
= List.tabulate(256, chr
)
314 in tst
' "test40" (fn _
=>
316 = List.map
Char.fromCString (List.map
Char.toCString chars
))
333 tst
' "test41" (fn _
=>
334 List.all (fn (arg
, res
) => Char.toCString arg
= res
) argResList
)
338 let fun checkFromCStringSucc (arg
, res
) =
339 str (valOf (Char.fromCString arg
)) = res
370 ("\\x0000000a", "\010"),
371 ("\\x0000000a2", "\162"),
372 ("\\x0000000ag", "\010"),
373 ("\\x0000000A", "\010"),
374 ("\\x0000000A2", "\162"),
375 ("\\x0000000Ag", "\010"),
376 ("\\x00000000000000000000000000000000000000000000000000000000000000011+",
380 tst
' "test42" (fn _
=> List.all checkFromCStringSucc argResList
)
384 let fun checkFromCStringFail arg
= Char.fromCString arg
= NONE
386 tst
' "test43" (fn _
=> List.all checkFromCStringFail