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
24 PS
1994-12-10, 1995-06-14, 1996-05-16 *)
26 (*KILL
05/11/1997 11:02. tho
.:
30 val _
= print
"Testing String...\n";
37 val s1
= "" (* size s1
= 0 *)
38 and s2
= "ABCDE\tFGHI"; (* size s2
= 10 *)
39 val ABCDE
= List.map chr
[65,66,67,68,69];
42 val test1
= tst
' "test1" (fn _
=> (size s1
= 0 andalso size s2
= 10));
43 val test2
= tst
' "test2" (fn _
=> (sub(s2
,6) = chr
70 andalso sub(s2
,9) = chr
73));
44 val test3
= tst0
"test3" ((sub(s1
, 0) seq
"WRONG") handle Subscript
=> "OK" | _
=> "WRONG");
45 val test4
= tst0
"test4" ((sub(s2
, ~
1) seq
"WRONG") handle Subscript
=> "OK" | _
=> "WRONG");
46 val test5
= tst0
"test5" ((sub(s2
, 10) seq
"WRONG") handle Subscript
=> "OK" | _
=> "WRONG");
50 "" = concat
[] andalso "" = concat
[s1
]
51 andalso s2
= concat
[s2
] andalso s2^s2
= concat
[s2
,s2
]
52 andalso "ABCD" = concat
["A","B","C","D"]);
54 val test7
= tst
' "test7" (fn _
=> "A" = str(chr
65));
57 tst
' "test8" (fn _
=> "" = implode
[] andalso "ABCDE" = implode ABCDE
);
60 tst
' "test9" (fn _
=> [] = explode
"" andalso ABCDE
= explode
"ABCDE");
63 tst
' "test10" (fn _
=>
64 s1
< s2
andalso s1
<= s1
65 andalso s2
> s1
andalso s2
>=s2
);
68 tst
' "test11a" (fn _
=>
69 s2
= extract(s2
, 0, SOME (size s2
))
70 andalso s2
= extract(s2
, 0, NONE
)
71 andalso "" = extract(s2
, size s2
, SOME
0)
72 andalso "" = extract(s2
, size s2
, NONE
)
73 andalso "" = extract(s1
, 0, SOME
0)
74 andalso "" = extract(s1
, 0, NONE
));
76 val test11b
= tst0
"test11b" ((extract(s2
, ~
1, SOME
0) seq
"WRONG")
77 handle Subscript
=> "OK" | _
=> "WRONG");
78 val test11c
= tst0
"test11c" ((extract(s2
, 11, SOME
0) seq
"WRONG")
79 handle Subscript
=> "OK" | _
=> "WRONG");
80 val test11d
= tst0
"test11d" ((extract(s2
, 0, SOME
11) seq
"WRONG")
81 handle Subscript
=> "OK" | _
=> "WRONG");
82 val test11e
= tst0
"test11e" ((extract(s2
, 10, SOME
1) seq
"WRONG")
83 handle Subscript
=> "OK" | _
=> "WRONG");
84 val test11f
= tst0
"test11f" ((extract(s2
, ~
1, NONE
) seq
"WRONG")
85 handle Subscript
=> "OK" | _
=> "WRONG");
86 val test11g
= tst0
"test11g" ((extract(s2
, 11, NONE
) seq
"WRONG")
87 handle Subscript
=> "OK" | _
=> "WRONG");
90 tst
' "test11h" (fn _
=>
91 "ABCDE" = extract(s2
, 0, SOME
5)
92 andalso "FGHI" = extract(s2
, 6, SOME
4)
93 andalso "FGHI" = extract(s2
, 6, NONE
));
96 tst
' "test12a" (fn _
=>
97 s2
= substring(s2
, 0, size s2
)
98 andalso "" = substring(s2
, size s2
, 0)
99 andalso "" = substring(s1
, 0, 0));
101 val test12b
= tst0
"test12b" ((substring(s2
, ~
1, 0) seq
"WRONG")
102 handle Subscript
=> "OK" | _
=> "WRONG");
103 val test12c
= tst0
"test12c" ((substring(s2
, 11, 0) seq
"WRONG")
104 handle Subscript
=> "OK" | _
=> "WRONG");
105 val test12d
= tst0
"test12d" ((substring(s2
, 0, 11) seq
"WRONG")
106 handle Subscript
=> "OK" | _
=> "WRONG");
107 val test12e
= tst0
"test12e" ((substring(s2
, 10, 1) seq
"WRONG")
108 handle Subscript
=> "OK" | _
=> "WRONG");
110 tst
' "test12f" (fn _
=>
111 "ABCDE" = substring(s2
, 0, 5)
112 andalso "FGHI" = substring(s2
, 6, 4));
114 tst0
"test12g" (case (Int.minInt
, Int.maxInt
) of
115 (SOME min
, SOME max
) =>
116 ((substring("", max
, max
); substring("", min
, min
); "WRONG")
117 handle Subscript
=> "OK" | _
=> "WRONG")
121 tst
' "test13a" (fn _
=>
122 (translate (fn _
=> "") s2
= ""
123 andalso translate (fn x
=> str x
) "" = ""
124 andalso translate (fn x
=> str x
) s2
= s2
));
127 tst
' "test13b" (fn _
=>
128 (translate (fn c
=> if c
= #
"\t" then "XYZ " else str c
) s2
132 tst
' "test14" (fn _
=>
133 (tokens isSpace
"" = []
134 andalso tokens isSpace
" \t \n" = []
135 andalso tokens (fn c
=> c
= #
",") ",asd,,def,fgh"
136 = ["asd","def","fgh"]));
139 tst
' "test15" (fn _
=>
140 (fields isSpace
"" = [""]
141 andalso fields isSpace
" \t \n" = ["","","","","","",""]
142 andalso fields (fn c
=> c
= #
",") ",asd,,def,fgh"
143 = ["","asd","","def","fgh"]));
146 tst
' "test16a" (fn _
=>
147 EQUAL
= compare(s1
,s1
) andalso EQUAL
= compare(s2
,s2
)
148 andalso LESS
= compare("A", "B")
149 andalso GREATER
= compare("B", "A")
150 andalso LESS
= compare("ABCD", "ABCDE")
151 andalso GREATER
= compare("ABCDE", "ABCD"));
154 tst
' "test16b" (fn _
=>
155 EQUAL
= compare(s1
,s1
) andalso EQUAL
= compare(s2
,s2
)
156 andalso LESS
= compare("A", "a")
157 andalso GREATER
= compare("b", "B")
158 andalso LESS
= compare("abcd", "abcde")
159 andalso GREATER
= compare("abcde", "abcd"));
162 (* Test cases for SML
string escape functions
. *)
165 let fun chk (arg
, res
) = toString (str arg
) = res
187 val (arg
, res
) = (implode (map #
1 argResList
),
188 concat (map #
2 argResList
))
189 in tst
' "test17" (fn _
=> List.all chk argResList
190 andalso toString arg
= res
)
194 let val chars
= CharVector
.tabulate(256, chr
)
195 val l
= CharVector
.foldl (fn (e
,l
) => e
:: l
) [] chars
196 val chars
= implode l
197 in tst
' "test18" (fn _
=> fromString(toString chars
) = SOME chars
) end
200 let fun chkFromString (arg
, res
) = fromString arg
= SOME (str res
)
222 ("\\ \t\n\n \\A", #
"A"),
223 ("\\ \t\n\n \\z", #
"z"),
224 ("\\ \t\n\n \\@", #
"@"),
225 ("\\ \t\n\n \\~", #
"~"),
226 ("\\ \t\n\n \\\\n", #
"\n"),
227 ("\\ \t\n\n \\\\t", #
"\t"),
228 ("\\ \t\n\n \\\\\\", #
"\\"),
229 ("\\ \t\n\n \\\\\"", #
"\""),
230 ("\\ \t\n\n \\\\^@", #
"\000"),
231 ("\\ \t\n\n \\\\^A", #
"\001"),
232 ("\\ \t\n\n \\\\^Z", #
"\026"),
233 ("\\ \t\n\n \\\\^_", #
"\031"),
234 ("\\ \t\n\n \\\\000", #
"\000"),
235 ("\\ \t\n\n \\\\097", #
"a"),
236 ("\\ \t\n\n \\\\255", #
"\255")]
237 val (arg
, res
) = (concat (map #
1 argResList
),
238 implode (map #
2 argResList
))
240 tst
' "test19" (fn _
=> List.all chkFromString argResList
241 andalso fromString arg
= SOME res
)
245 tst
' "test20" (fn _
=> List.all (fn arg
=> isSome (fromString arg
))
267 (* Test cases for C
string escape functions
*)
270 let val chars
= CharVector
.tabulate(256, chr
)
271 val l
= CharVector
.foldl (fn (e
,l
) => e
:: l
) [] chars
272 val chars
= implode l
274 in tst
' "test21" (fn _
=> fromCString(toCString chars
) = SOME chars
) end;
289 val (arg
, res
) = (concat (map #
1 argResList
),
290 concat (map #
2 argResList
))
292 tst
' "test22" (fn _
=>
293 List.all (fn (arg
, res
) => toCString arg
= res
) argResList
294 andalso toCString arg
= res
)
298 let fun checkFromCStringSucc (arg
, res
) = fromCString arg
= SOME res
329 ("\\x0000000a", "\010"),
330 ("\\x0000000a2", "\162"),
331 ("\\x0000000ag", "\010g"),
332 ("\\x0000000A", "\010"),
333 ("\\x0000000A2", "\162"),
334 ("\\x0000000Ag", "\010g"),
335 ("\\x00000000000000000000000000000000000000000000000000000000000000011+",
337 val (arg
, res
) = (concat (map #
1 argResList
),
338 concat (map #
2 argResList
))
340 tst
' "test23" (fn _
=> List.all checkFromCStringSucc argResList
341 andalso fromCString arg
= SOME res
)
345 let fun checkFromCStringFail arg
= isSome (fromCString arg
)
347 tst
' "test24" (fn _
=> List.all checkFromCStringFail
363 tst
' "test25" (fn _
=>
365 andalso isPrefix
"" "abcde"
366 andalso isPrefix
"a" "abcde"
367 andalso isPrefix
"abcd" "abcde"
368 andalso isPrefix
"abcde" "abcde"
369 andalso not (isPrefix
"abcde" "")
370 andalso not (isPrefix
"abcdef" "abcde")
371 andalso not (isPrefix
"Abcde" "abcde")
372 andalso not (isPrefix
"abcdE" "abcde"))