Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* test/substring.sml 1995-04-27, 1997-06-03 *) |
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 tst0 s s' = print (s ^ " \t" ^ s' ^ "\n"); | |
15 | fun tst s b = tst0 s (check b); | |
16 | fun tst' s f = tst0 s (check' f); | |
17 | ||
18 | fun checkrange bounds = check o range bounds; | |
19 | ||
20 | ||
21 | local | |
22 | open Char Substring | |
23 | val all = full | |
24 | fun base2 (a, b) = (base a, base b) | |
25 | ||
26 | val s1 = "" (* String.size s1 = 0 *) | |
27 | and s2 = "ABCDE\tFGHI"; (* String.size s2 = 10 *) | |
28 | val ss1 = all s1 (* size s1 = 0 *) | |
29 | and ss2 = all s2; (* size s2 = 10 *) | |
30 | ||
31 | val sa = "AAAAaAbAABBBB"; (* String.size sa = 14 *) | |
32 | (* 45678 *) | |
33 | ||
34 | val ssa1 = extract(sa, 4, SOME 0) (* size ssa1 = 0 *) | |
35 | val ssa2 = extract(sa, 4, SOME 5) (* size ssa2 = 5 *) | |
36 | ||
37 | val ss3 = extract("junk this is a (clear)textjunk", 4, SOME 24); | |
38 | (* 456789012345678901234567 *) | |
39 | ||
40 | in | |
41 | ||
42 | ||
43 | val test1a = tst' "test1a" | |
44 | (fn _ => | |
45 | (s2, 10, 0) = base(extract(s2, 10, SOME 0)) | |
46 | andalso (s2, 10, 0) = base(extract(s2, 10, NONE)) | |
47 | andalso (s2, 0, 0) = base(extract(s2, 0, SOME 0)) | |
48 | andalso (s2, 4, 3) = base(extract(s2, 4, SOME 3)) | |
49 | andalso (s2, 4, 6) = base(extract(s2, 4, SOME 6)) | |
50 | andalso (s2, 4, 6) = base(extract(s2, 4, NONE)) | |
51 | andalso (s2, 0, 10) = base(extract(s2, 0, SOME 10)) | |
52 | andalso (s2, 0, 10) = base(extract(s2, 0, NONE))); | |
53 | ||
54 | val test1b = tst0 "test1b" ((extract(s2, ~1, SOME 0) seq "WRONG") | |
55 | handle Subscript => "OK" | _ => "WRONG") | |
56 | val test1c = tst0 "test1c" ((extract(s2, 11, SOME 0) seq "WRONG") | |
57 | handle Subscript => "OK" | _ => "WRONG") | |
58 | val test1d = tst0 "test1d" ((extract(s2, 0, SOME 11) seq "WRONG") | |
59 | handle Subscript => "OK" | _ => "WRONG") | |
60 | val test1e = tst0 "test1e" ((extract(s2, 10, SOME 1) seq "WRONG") | |
61 | handle Subscript => "OK" | _ => "WRONG") | |
62 | val test1f = tst0 "test1f" ((extract(s2, ~1, NONE) seq "WRONG") | |
63 | handle Subscript => "OK" | _ => "WRONG") | |
64 | val test1g = tst0 "test1g" ((extract(s2, 11, NONE) seq "WRONG") | |
65 | handle Subscript => "OK" | _ => "WRONG") | |
66 | ||
67 | val test1h = tst' "test1h" | |
68 | (fn _ => | |
69 | string ssa1 = "" | |
70 | andalso string ssa2 = "aAbAA" | |
71 | andalso s1 = string (all s1) | |
72 | andalso s2 = string (all s2)); | |
73 | ||
74 | val test2a = tst' "test2a" | |
75 | (fn _ => | |
76 | string(triml 6 ss2) = "FGHI" | |
77 | andalso s2 = string(triml 0 ss2) | |
78 | andalso s1 = string(triml 0 ss1) | |
79 | andalso (s2, 10, 0) = base(triml 10 ss2) | |
80 | andalso (s2, 10, 0) = base(triml 11 ss2) | |
81 | andalso (sa, 6, 3) = base(triml 2 ssa2) | |
82 | andalso (sa, 9, 0) = base(triml 5 ssa2) | |
83 | andalso (sa, 9, 0) = base(triml 6 ssa2)); | |
84 | ||
85 | val test2b = tst0 "test2b" ((triml ~1 ss2 seq "WRONG") | |
86 | handle Subscript => "OK" | _ => "WRONG") | |
87 | val test2c = tst0 "test2c" ((triml ~1 ssa2 seq "WRONG") | |
88 | handle Subscript => "OK" | _ => "WRONG") | |
89 | ||
90 | val test3a = tst' "test3a" | |
91 | (fn _ => | |
92 | string(trimr 6 ss2) = "ABCD" | |
93 | andalso s2 = string(trimr 0 ss2) | |
94 | andalso s1 = string(trimr 0 ss1) | |
95 | andalso (s2, 0, 0) = base(trimr 10 ss2) | |
96 | andalso (s2, 0, 0) = base(trimr 11 ss2) | |
97 | andalso (sa, 4, 3) = base(trimr 2 ssa2) | |
98 | andalso (sa, 4, 0) = base(trimr 5 ssa2) | |
99 | andalso (sa, 4, 0) = base(trimr 6 ssa2)); | |
100 | ||
101 | val test3b = tst0 "test3b" ((trimr ~1 ss2 seq "WRONG") | |
102 | handle Subscript => "OK" | _ => "WRONG") | |
103 | val test3c = tst0 "test3c" ((trimr ~1 ssa2 seq "WRONG") | |
104 | handle Subscript => "OK" | _ => "WRONG") | |
105 | ||
106 | val test4 = tst' "test4" | |
107 | (fn _ => | |
108 | isEmpty ss1 | |
109 | andalso not (isEmpty ss2) | |
110 | andalso isEmpty ssa1 | |
111 | andalso not (isEmpty ssa2)); | |
112 | ||
113 | val test5a = tst' "test5a" | |
114 | (fn _ => | |
115 | case getc ssa1 of NONE => true | _ => false); | |
116 | ||
117 | val test5b = tst' "test5b" | |
118 | (fn _ => | |
119 | case getc ssa2 of | |
120 | NONE => false | |
121 | | SOME(#"a", rest) => "AbAA" = string rest | |
122 | | _ => false); | |
123 | ||
124 | val test6 = tst' "test6" | |
125 | (fn _ => | |
126 | first ssa1 = NONE | |
127 | andalso first ssa2 = SOME #"a") | |
128 | ||
129 | val test7 = tst' "test7" | |
130 | (fn _ => (size ss1 = 0 andalso size ss2 = 10 | |
131 | andalso size ssa1 = 0 andalso size ssa2 = 5)); | |
132 | ||
133 | val test8a = tst' "test8a" | |
134 | (fn _ => (sub(ss2,6) = chr 70 andalso sub(ss2,9) = chr 73 | |
135 | andalso sub(ssa2, 1) = chr 65)); | |
136 | val test8b = tst0 "test8b" | |
137 | ((sub(ss1, 0) seq "WRONG") handle Subscript => "OK" | _ => "WRONG") | |
138 | val test8c = tst0 "test8c" | |
139 | ((sub(ss2, ~1) seq "WRONG") handle Subscript => "OK" | _ => "WRONG") | |
140 | val test8d = tst0 "test8d" | |
141 | ((sub(ss2, 10) seq "WRONG") handle Subscript => "OK" | _ => "WRONG") | |
142 | val test8e = tst0 "test8e" | |
143 | ((sub(ssa2, ~1) seq "WRONG") handle Subscript => "OK" | _ => "WRONG") | |
144 | val test8f = tst0 "test8f" | |
145 | ((sub(ssa2, 5) seq "WRONG") handle Subscript => "OK" | _ => "WRONG") | |
146 | ||
147 | val test9a = tst' "test9a" | |
148 | (fn _ => | |
149 | base ss2 = base(slice(ss2, 0, SOME (size ss2))) | |
150 | andalso base ss2 = base(slice(ss2, 0, NONE)) | |
151 | andalso (s2, 10, 0) = base(slice(ss2, size ss2, SOME 0)) | |
152 | andalso (s2, 10, 0) = base(slice(ss2, size ss2, NONE)) | |
153 | andalso base ss1 = base(slice(ss1, 0, SOME 0)) | |
154 | andalso base ss1 = base(slice(ss1, 0, NONE))); | |
155 | ||
156 | val test9b = tst' "test9b" | |
157 | (fn _ => | |
158 | (sa, 4, 5) = base(slice(ssa2, 0, SOME 5)) | |
159 | andalso (sa, 4, 5) = base(slice(ssa2, 0, NONE)) | |
160 | andalso (sa, 4, 0) = base(slice(ssa2, 0, SOME 0)) | |
161 | andalso (sa, 9, 0) = base(slice(ssa2, 5, SOME 0)) | |
162 | andalso (sa, 9, 0) = base(slice(ssa2, 5, NONE)) | |
163 | andalso (sa, 5, 3) = base(slice(ssa2, 1, SOME 3)) | |
164 | andalso (sa, 5, 4) = base(slice(ssa2, 1, SOME 4)) | |
165 | andalso (sa, 5, 4) = base(slice(ssa2, 1, NONE))); | |
166 | ||
167 | val test9c = tst0 "test9c" ((slice(ssa2, ~1, SOME 0) seq "WRONG") | |
168 | handle Subscript => "OK" | _ => "WRONG") | |
169 | val test9d = tst0 "test9d" ((slice(ssa2, 6, SOME 0) seq "WRONG") | |
170 | handle Subscript => "OK" | _ => "WRONG") | |
171 | val test9e = tst0 "test9e" ((slice(ssa2, 0, SOME 6) seq "WRONG") | |
172 | handle Subscript => "OK" | _ => "WRONG") | |
173 | val test9f = tst0 "test9f" ((slice(ssa2, 5, SOME 1) seq "WRONG") | |
174 | handle Subscript => "OK" | _ => "WRONG") | |
175 | val test9g = tst0 "test9g" ((slice(ssa2, ~1, NONE) seq "WRONG") | |
176 | handle Subscript => "OK" | _ => "WRONG") | |
177 | val test9h = tst0 "test9h" ((slice(ssa2, 6, NONE) seq "WRONG") | |
178 | handle Subscript => "OK" | _ => "WRONG") | |
179 | ||
180 | val test12 = tst' "test12" | |
181 | (fn _ => | |
182 | concat [] = "" | |
183 | andalso concat [ssa1, ssa1, ssa1] = "" | |
184 | andalso concat [ssa2, ssa2, ssa2] = "aAbAAaAbAAaAbAA" | |
185 | andalso concat [ssa2, ssa1, ss2, ss1] = "aAbAAABCDE\tFGHI"); | |
186 | ||
187 | val test13 = tst' "test13" | |
188 | (fn _ => | |
189 | explode ss1 = [] | |
190 | andalso explode ssa1 = [] | |
191 | andalso explode ssa2 = [#"a", #"A", #"b", #"A", #"A"]); | |
192 | ||
193 | val test14 = tst' "test14" | |
194 | (fn _ => | |
195 | EQUAL = compare(ssa1,ssa1) andalso EQUAL = compare(ssa2,ssa2) | |
196 | andalso LESS = compare(triml 1 ssa2, ssa2) | |
197 | andalso GREATER = compare(ssa2, triml 1 ssa2) | |
198 | andalso LESS = compare(trimr 1 ssa2, ssa2) | |
199 | andalso GREATER = compare(ssa2, trimr 1 ssa2) | |
200 | andalso LESS = compare(all "AB", ssa2) | |
201 | andalso GREATER = compare(ssa2, all "AB")); | |
202 | ||
203 | fun finda c = c <> #"A"; | |
204 | fun findb c = c <> #"B"; | |
205 | ||
206 | val test15 = tst' "test15" | |
207 | (fn _ => | |
208 | (sa, 5, 4) = base(dropl finda ssa2) | |
209 | andalso (sa, 9, 0) = base(dropl findb ssa2) | |
210 | andalso base ssa1 = base(dropl finda ssa1)); | |
211 | ||
212 | val test16 = tst' "test16" | |
213 | (fn _ => | |
214 | (sa, 4, 5) = base(dropr finda ssa2) | |
215 | andalso (sa, 4, 0) = base(dropr findb ssa2) | |
216 | andalso base ssa1 = base(dropr finda ssa1)); | |
217 | ||
218 | val test17 = tst' "test17" | |
219 | (fn _ => | |
220 | (sa, 4, 1) = base(takel finda ssa2) | |
221 | andalso (sa, 4, 5) = base(takel findb ssa2) | |
222 | andalso base ssa1 = base(takel finda ssa1)); | |
223 | ||
224 | val test18 = tst' "test18" | |
225 | (fn _ => | |
226 | (sa, 9, 0) = base(taker finda ssa2) | |
227 | andalso (sa, 4, 5) = base(taker findb ssa2) | |
228 | andalso base ssa1 = base(taker finda ssa1)); | |
229 | ||
230 | val test19 = tst' "test19" | |
231 | (fn _ => | |
232 | ((sa, 4, 1), (sa, 5, 4)) = base2(splitl finda ssa2) | |
233 | andalso ((sa, 4, 5), (sa, 9, 0)) = base2(splitl findb ssa2) | |
234 | andalso base2(ssa1, ssa1) = base2(splitl finda ssa1)); | |
235 | ||
236 | val test20 = tst' "test20" | |
237 | (fn _ => | |
238 | ((sa, 4, 5), (sa, 9, 0)) = base2(splitr finda ssa2) | |
239 | andalso ((sa, 4, 0), (sa, 4, 5)) = base2(splitr findb ssa2) | |
240 | andalso base2(ssa1, ssa1) = base2 (splitr finda ssa1)); | |
241 | ||
242 | val test21 = tst' "test21" | |
243 | (fn _ => | |
244 | ((sa, 4, 0), (sa, 4, 5)) = base2(position "" ssa2) | |
245 | andalso ((sa, 4, 1), (sa, 5, 4)) = base2(position "Ab" ssa2) | |
246 | andalso ((sa, 4, 5), (sa, 9, 0)) = base2(position "B" ssa2) | |
247 | andalso ((sa, 4, 5), (sa, 9, 0)) = base2(position "AAB" ssa2) | |
248 | andalso ((sa, 4, 0), (sa, 4, 5)) = base2(position "aA" ssa2) | |
249 | andalso ((sa, 4, 2), (sa, 6, 3)) = base2(position "bAA" ssa2) | |
250 | andalso (base ssa1, base ssa1) = base2(position "A" ssa1) | |
251 | andalso (base ssa1, base ssa1) = base2(position "" ssa1)); | |
252 | ||
253 | (* For the pre-November 1995 version of position: | |
254 | val test21 = tst' "test21" | |
255 | (fn _ => | |
256 | (sa, 4, 5) = base(position "" ssa2) | |
257 | andalso (sa, 5, 4) = base(position "Ab" ssa2) | |
258 | andalso (sa, 9, 0) = base(position "B" ssa2) | |
259 | andalso (sa, 9, 0) = base(position "AAB" ssa2) | |
260 | andalso (sa, 4, 5) = base(position "aA" ssa2) | |
261 | andalso (sa, 6, 3) = base(position "bAA" ssa2) | |
262 | andalso base ssa1 = base(position "A" ssa1) | |
263 | andalso base ssa1 = base(position "" ssa1)); | |
264 | *) | |
265 | ||
266 | val test22a = tst' "test22a" | |
267 | (fn _ => | |
268 | (translate (fn _ => "") ssa2 = "" | |
269 | andalso translate (fn x => str x) ssa1 = "" | |
270 | andalso translate (fn x => str x) ssa2 = string ssa2)); | |
271 | ||
272 | val test22b = tst' "test22b" | |
273 | (fn _ => | |
274 | (translate (fn c => if c = #"b" then "XYZ " else str c) ssa2 | |
275 | = "aAXYZ AA")); | |
276 | ||
277 | val test23 = tst' "test23" | |
278 | (fn _ => | |
279 | (null(tokens isSpace ssa1) | |
280 | andalso null(tokens (Char.contains "Aab") ssa2) | |
281 | andalso map string (tokens (fn c => c = #"A") ssa2) = ["a","b"])); | |
282 | ||
283 | val test24 = tst' "test24" | |
284 | (fn _ => | |
285 | (map base (fields isSpace ssa1) = [base ssa1] | |
286 | andalso map base (fields (contains "Aab") ssa2) | |
287 | = [(sa,4,0),(sa,5,0),(sa,6,0),(sa,7,0),(sa,8,0),(sa,9,0)] | |
288 | andalso map string (fields (fn c => c = #"A") ssa2) | |
289 | = ["a","b","",""])); | |
290 | ||
291 | val test25 = tst' "test25" | |
292 | (fn _ => | |
293 | null(tokens (fn _ => true) ss3) | |
294 | andalso null(tokens (fn _ => false) (all "")) | |
295 | andalso null(tokens (contains " ()") (all "(()())(( ()")) | |
296 | andalso ["this","is","a","clear","text"] = | |
297 | map string (tokens (contains " ()") ss3)); | |
298 | ||
299 | local | |
300 | val v = ref 0 | |
301 | fun setv c = v := ord c; | |
302 | in | |
303 | ||
304 | val test26a = tst' "test26a" | |
305 | (fn _ => | |
306 | (v := 0; | |
307 | foldl (fn (x, _) => setv x) () ssa2; | |
308 | !v = 65)); | |
309 | ||
310 | val test26b = tst' "test26b" | |
311 | (fn _ => | |
312 | implode(foldl (op ::) [] ssa2) = "AAbAa"); | |
313 | ||
314 | val test27a = tst' "test27a" | |
315 | (fn _ => | |
316 | (v := 0; | |
317 | foldr (fn (x, _) => setv x) () ssa2; | |
318 | !v = 97)); | |
319 | ||
320 | val test27b = tst' "test27b" | |
321 | (fn _ => | |
322 | implode(foldr (op ::) [] ssa2) = "aAbAA"); | |
323 | ||
324 | val test28 = tst' "test28" | |
325 | (fn _ => | |
326 | (v := 0; | |
327 | app setv ssa2; | |
328 | !v = 65)); | |
329 | end | |
330 | ||
331 | val test29a = tst' "test29a" | |
332 | (fn _ => | |
333 | base2(splitAt(ssa1, 0)) = ((sa, 4, 0), (sa, 4, 0)) | |
334 | andalso base2(splitAt(ssa2, 0)) = ((sa, 4, 0), (sa, 4, 5)) | |
335 | andalso base2(splitAt(ssa2, 1)) = ((sa, 4, 1), (sa, 5, 4)) | |
336 | andalso base2(splitAt(ssa2, 4)) = ((sa, 4, 4), (sa, 8, 1)) | |
337 | andalso base2(splitAt(ssa2, 5)) = ((sa, 4, 5), (sa, 9, 0))); | |
338 | val test29b = tst0 "test29b" ((splitAt(ssa2, ~1) seq "WRONG") | |
339 | handle Subscript => "OK" | _ => "WRONG") | |
340 | val test29c = tst0 "test29c" ((splitAt(ssa2, 6) seq "WRONG") | |
341 | handle Subscript => "OK" | _ => "WRONG") | |
342 | ||
343 | val test30a = tst' "test30a" | |
344 | (fn _ => | |
345 | (s2, 10, 0) = base(substring(s2, 10, 0)) | |
346 | andalso (s2, 0, 0) = base(substring(s2, 0, 0)) | |
347 | andalso (s2, 4, 3) = base(substring(s2, 4, 3)) | |
348 | andalso (s2, 4, 6) = base(substring(s2, 4, 6)) | |
349 | andalso (s2, 0, 10) = base(substring(s2, 0, 10))); | |
350 | ||
351 | val test30b = tst0 "test30b" ((substring(s2, ~1, 0) seq "WRONG") | |
352 | handle Subscript => "OK" | _ => "WRONG") | |
353 | val test30c = tst0 "test30c" ((substring(s2, 11, 0) seq "WRONG") | |
354 | handle Subscript => "OK" | _ => "WRONG") | |
355 | val test30d = tst0 "test30d" ((substring(s2, 0, 11) seq "WRONG") | |
356 | handle Subscript => "OK" | _ => "WRONG") | |
357 | val test30e = tst0 "test30e" ((substring(s2, 10, 1) seq "WRONG") | |
358 | handle Subscript => "OK" | _ => "WRONG") | |
359 | val test30f = tst0 "test30f" | |
360 | ((case (Int.minInt, Int.maxInt) of | |
361 | (SOME min, SOME max) => | |
362 | ((substring("", max, max); "WRONG") | |
363 | handle Subscript => | |
364 | ((substring("", min, min); "WRONG") | |
365 | handle Subscript => "OK" | _ => "WRONG")) | |
366 | | _ => "OK") | |
367 | handle _ => "EXN") | |
368 | ||
369 | (* val sa = "AAAAaAbAABBBB"; *) | |
370 | val test31 = tst' "test31" | |
371 | (fn _ => | |
372 | isPrefix "" (substring(sa, 0, 0)) | |
373 | andalso isPrefix "" (substring(sa, 13, 0)) | |
374 | andalso isPrefix "" ssa1 | |
375 | andalso isPrefix "aAbAA" ssa2 | |
376 | andalso isPrefix "aAbA" ssa2 | |
377 | andalso not (isPrefix "aAbAAB" ssa2) | |
378 | andalso not (isPrefix "aAbAAB" ssa1)) | |
379 | ||
380 | fun eqspan(sus1, sus2, res) = base(span(sus1, sus2)) = base res | |
381 | ||
382 | val test32a = tst' "test32a" (fn _ => | |
383 | eqspan(substring(sa, 0, 0), substring(sa, 0, 13), all sa) | |
384 | andalso eqspan(substring(sa, 0, 13), substring(sa, 13, 0), all sa) | |
385 | andalso eqspan(substring(sa, 5, 0), substring(sa, 5, 0), substring(sa, 5,0)) | |
386 | andalso eqspan(substring(sa, 0, 5), substring(sa, 5, 8), all sa) | |
387 | andalso eqspan(substring(sa, 0, 13), substring(sa, 0, 13), all sa) | |
388 | andalso eqspan(substring(sa, 5, 4), substring(sa, 2, 4), substring(sa,5,1)) | |
389 | andalso eqspan(substring(sa, 2, 5), substring(sa, 6, 3), substring(sa, 2,7)) | |
390 | andalso eqspan(substring("abcd", 1, 0), substring("abcd", 1, 2), | |
391 | substring("abcd", 1, 2)) | |
392 | andalso eqspan(substring("", 0, 0), substring("", 0, 0), all "")) | |
393 | ||
394 | val test32b = tst0 "test32b" ((span(substring("a", 0, 0), substring("b", 0, 0)) seq "WRONG") | |
395 | handle Span => "OK" | _ => "WRONG") | |
396 | ||
397 | val test32c = tst0 "test32c" ((span(substring(sa, 1, 0), substring(sa, 0, 0)) seq "WRONG") | |
398 | handle Span => "OK" | _ => "WRONG") | |
399 | ||
400 | val test32d = tst0 "test32d" ((span(substring(sa, 3, 2), substring("abcd", 2, 1)) seq "WRONG") | |
401 | handle Span => "OK" | _ => "WRONG") | |
402 | ||
403 | val test32e = tst0 "test32e" ((span(substring("a", 0, 0), substring("b", 0, 0)) seq "WRONG") | |
404 | handle Span => "OK" | _ => "WRONG") | |
405 | ||
406 | end; |