| 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 |
|
| 23 |
(* test/string.sml |
| 24 |
PS 1994-12-10, 1995-06-14, 1996-05-16 *) |
| 25 |
|
| 26 |
(*KILL 05/11/1997 11:02. tho.: |
| 27 |
use "auxil.sml"; |
| 28 |
*) |
| 29 |
|
| 30 |
val _ = print "Testing String...\n"; |
| 31 |
|
| 32 |
local |
| 33 |
val map' = map |
| 34 |
open Char String |
| 35 |
val map = map' |
| 36 |
|
| 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]; |
| 40 |
in |
| 41 |
|
| 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"); |
| 47 |
|
| 48 |
val test6 = |
| 49 |
tst' "test6" (fn _ => |
| 50 |
"" = concat [] andalso "" = concat [s1] |
| 51 |
andalso s2 = concat [s2] andalso s2^s2 = concat [s2,s2] |
| 52 |
andalso "ABCD" = concat ["A","B","C","D"]); |
| 53 |
|
| 54 |
val test7 = tst' "test7" (fn _ => "A" = str(chr 65)); |
| 55 |
|
| 56 |
val test8 = |
| 57 |
tst' "test8" (fn _ => "" = implode [] andalso "ABCDE" = implode ABCDE); |
| 58 |
|
| 59 |
val test9 = |
| 60 |
tst' "test9" (fn _ => [] = explode "" andalso ABCDE = explode "ABCDE"); |
| 61 |
|
| 62 |
val test10 = |
| 63 |
tst' "test10" (fn _ => |
| 64 |
s1 < s2 andalso s1 <= s1 |
| 65 |
andalso s2 > s1 andalso s2 >=s2); |
| 66 |
|
| 67 |
val test11a = |
| 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)); |
| 75 |
|
| 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"); |
| 88 |
|
| 89 |
val test11h = |
| 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)); |
| 94 |
|
| 95 |
val test12a = |
| 96 |
tst' "test12a" (fn _ => |
| 97 |
s2 = substring(s2, 0, size s2) |
| 98 |
andalso "" = substring(s2, size s2, 0) |
| 99 |
andalso "" = substring(s1, 0, 0)); |
| 100 |
|
| 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"); |
| 109 |
val test12f = |
| 110 |
tst' "test12f" (fn _ => |
| 111 |
"ABCDE" = substring(s2, 0, 5) |
| 112 |
andalso "FGHI" = substring(s2, 6, 4)); |
| 113 |
val test12g = |
| 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") |
| 118 |
| _ => "OK"); |
| 119 |
|
| 120 |
val test13a = |
| 121 |
tst' "test13a" (fn _ => |
| 122 |
(translate (fn _ => "") s2 = "" |
| 123 |
andalso translate (fn x => str x) "" = "" |
| 124 |
andalso translate (fn x => str x) s2 = s2)); |
| 125 |
|
| 126 |
val test13b = |
| 127 |
tst' "test13b" (fn _ => |
| 128 |
(translate (fn c => if c = #"\t" then "XYZ " else str c) s2 |
| 129 |
= "ABCDEXYZ FGHI")); |
| 130 |
|
| 131 |
val test14 = |
| 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"])); |
| 137 |
|
| 138 |
val test15 = |
| 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"])); |
| 144 |
|
| 145 |
val test16a = |
| 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")); |
| 152 |
|
| 153 |
val test16b = |
| 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")); |
| 160 |
|
| 161 |
|
| 162 |
(* Test cases for SML string escape functions. *) |
| 163 |
|
| 164 |
val test17 = |
| 165 |
let fun chk (arg, res) = toString (str arg) = res |
| 166 |
val argResList = |
| 167 |
[(#"\000", "\\^@"), |
| 168 |
(#"\001", "\\^A"), |
| 169 |
(#"\006", "\\^F"), |
| 170 |
(#"\007", "\\a"), |
| 171 |
(#"\008", "\\b"), |
| 172 |
(#"\009", "\\t"), |
| 173 |
(#"\010", "\\n"), |
| 174 |
(#"\011", "\\v"), |
| 175 |
(#"\012", "\\f"), |
| 176 |
(#"\013", "\\r"), |
| 177 |
(#"\014", "\\^N"), |
| 178 |
(#"\031", "\\^_"), |
| 179 |
(#"\032", " "), |
| 180 |
(#"\126", "~"), |
| 181 |
(#"\\", "\\\\"), |
| 182 |
(#"\"", "\\\""), |
| 183 |
(#"A", "A"), |
| 184 |
(#"\127", "\\127"), |
| 185 |
(#"\128", "\\128"), |
| 186 |
(#"\255", "\\255")] |
| 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) |
| 191 |
end; |
| 192 |
|
| 193 |
val test18 = |
| 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 |
| 198 |
|
| 199 |
val test19 = |
| 200 |
let fun chkFromString (arg, res) = fromString arg = SOME (str res) |
| 201 |
val argResList = |
| 202 |
[("A", #"A"), |
| 203 |
("z", #"z"), |
| 204 |
("@", #"@"), |
| 205 |
("~", #"~"), |
| 206 |
("\\a", #"\007"), |
| 207 |
("\\b", #"\008"), |
| 208 |
("\\t", #"\009"), |
| 209 |
("\\n", #"\010"), |
| 210 |
("\\v", #"\011"), |
| 211 |
("\\f", #"\012"), |
| 212 |
("\\r", #"\013"), |
| 213 |
("\\\\", #"\\"), |
| 214 |
("\\\"", #"\""), |
| 215 |
("\\^@", #"\000"), |
| 216 |
("\\^A", #"\001"), |
| 217 |
("\\^Z", #"\026"), |
| 218 |
("\\^_", #"\031"), |
| 219 |
("\\000", #"\000"), |
| 220 |
("\\097", #"a"), |
| 221 |
("\\255", #"\255"), |
| 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)) |
| 239 |
in |
| 240 |
tst' "test19" (fn _ => List.all chkFromString argResList |
| 241 |
andalso fromString arg = SOME res) |
| 242 |
end; |
| 243 |
|
| 244 |
val test20 = |
| 245 |
tst' "test20" (fn _ => List.all (fn arg => not (isSome (fromString arg))) |
| 246 |
["\\", |
| 247 |
"\\c", |
| 248 |
"\\F", |
| 249 |
"\\e", |
| 250 |
"\\g", |
| 251 |
"\\N", |
| 252 |
"\\T", |
| 253 |
"\\1", |
| 254 |
"\\11", |
| 255 |
"\\256", |
| 256 |
"\\-65", |
| 257 |
"\\~65", |
| 258 |
"\\?", |
| 259 |
"\\^`", |
| 260 |
"\\^a", |
| 261 |
"\\^z", |
| 262 |
"\\ a", |
| 263 |
"\\ a\\B"]); |
| 264 |
|
| 265 |
|
| 266 |
(* Test cases for C string escape functions *) |
| 267 |
|
| 268 |
val test21 = |
| 269 |
let val chars = CharVector.tabulate(256, chr) |
| 270 |
val l = CharVector.foldl (fn (e,l) => e :: l) [] chars |
| 271 |
val chars = implode l |
| 272 |
|
| 273 |
in tst' "test21" (fn _ => fromCString(toCString chars) = SOME chars) end; |
| 274 |
|
| 275 |
val test22 = |
| 276 |
let val argResList = |
| 277 |
[("\010", "\\n"), |
| 278 |
("\009", "\\t"), |
| 279 |
("\011", "\\v"), |
| 280 |
("\008", "\\b"), |
| 281 |
("\013", "\\r"), |
| 282 |
("\012", "\\f"), |
| 283 |
("\007", "\\a"), |
| 284 |
("\\", "\\\\"), |
| 285 |
("?", "\\?"), |
| 286 |
("'", "\\'"), |
| 287 |
("\"", "\\\"")] |
| 288 |
val (arg, res) = (concat (map #1 argResList), |
| 289 |
concat (map #2 argResList)) |
| 290 |
in |
| 291 |
tst' "test22" (fn _ => |
| 292 |
List.all (fn (arg, res) => toCString arg = res) argResList |
| 293 |
andalso toCString arg = res) |
| 294 |
end; |
| 295 |
|
| 296 |
val test23 = |
| 297 |
let fun checkFromCStringSucc (arg, res) = fromCString arg = SOME res |
| 298 |
val argResList = |
| 299 |
[("\\n", "\010"), |
| 300 |
("\\t", "\009"), |
| 301 |
("\\v", "\011"), |
| 302 |
("\\b", "\008"), |
| 303 |
("\\r", "\013"), |
| 304 |
("\\f", "\012"), |
| 305 |
("\\a", "\007"), |
| 306 |
("\\\\", "\\"), |
| 307 |
("\\?", "?"), |
| 308 |
("\\'", "'"), |
| 309 |
("\\\"", "\""), |
| 310 |
("\\1", "\001"), |
| 311 |
("\\11", "\009"), |
| 312 |
("\\111", "\073"), |
| 313 |
("\\1007", "\0647"), |
| 314 |
("\\100A", "\064A"), |
| 315 |
("\\0", "\000"), |
| 316 |
("\\377", "\255"), |
| 317 |
("\\18", "\0018"), |
| 318 |
("\\178", "\0158"), |
| 319 |
("\\1C", "\001C"), |
| 320 |
("\\17C", "\015C"), |
| 321 |
("\\x0", "\000"), |
| 322 |
("\\xff", "\255"), |
| 323 |
("\\xFF", "\255"), |
| 324 |
("\\x1", "\001"), |
| 325 |
("\\x11", "\017"), |
| 326 |
("\\xag", "\010g"), |
| 327 |
("\\xAAg", "\170g"), |
| 328 |
("\\x0000000a", "\010"), |
| 329 |
("\\x0000000a2", "\162"), |
| 330 |
("\\x0000000ag", "\010g"), |
| 331 |
("\\x0000000A", "\010"), |
| 332 |
("\\x0000000A2", "\162"), |
| 333 |
("\\x0000000Ag", "\010g"), |
| 334 |
("\\x00000000000000000000000000000000000000000000000000000000000000011+", |
| 335 |
"\017+")] |
| 336 |
val (arg, res) = (concat (map #1 argResList), |
| 337 |
concat (map #2 argResList)) |
| 338 |
in |
| 339 |
tst' "test23" (fn _ => List.all checkFromCStringSucc argResList |
| 340 |
andalso fromCString arg = SOME res) |
| 341 |
end; |
| 342 |
|
| 343 |
val test24 = |
| 344 |
let fun checkFromCStringFail arg = isSome (fromCString arg) |
| 345 |
in |
| 346 |
tst' "test24" (fn _ => List.all checkFromCStringFail |
| 347 |
["\\", |
| 348 |
"\\X", |
| 349 |
"\\=", |
| 350 |
"\\400", |
| 351 |
"\\777", |
| 352 |
"\\8", |
| 353 |
"\\9", |
| 354 |
"\\c", |
| 355 |
"\\d", |
| 356 |
"\\x", |
| 357 |
"\\x100", |
| 358 |
"\\xG"]) |
| 359 |
end; |
| 360 |
|
| 361 |
val test25 = |
| 362 |
tst' "test25" (fn _ => |
| 363 |
isPrefix "" "" |
| 364 |
andalso isPrefix "" "abcde" |
| 365 |
andalso isPrefix "a" "abcde" |
| 366 |
andalso isPrefix "abcd" "abcde" |
| 367 |
andalso isPrefix "abcde" "abcde" |
| 368 |
andalso not (isPrefix "abcde" "") |
| 369 |
andalso not (isPrefix "abcdef" "abcde") |
| 370 |
andalso not (isPrefix "Abcde" "abcde") |
| 371 |
andalso not (isPrefix "abcdE" "abcde")) |
| 372 |
|
| 373 |
end |