*** empty log message ***
[bpt/guile.git] / test-suite / tests / srfi-13.test
CommitLineData
df937d20
MG
1;;;; srfi-13.test --- Test suite for Guile's SRFI-13 functions. -*- scheme -*-
2;;;; Martin Grabmueller, 2001-05-07
3;;;;
4c6563e1 4;;;; Copyright (C) 2001, 2004 Free Software Foundation, Inc.
e5c5ac92 5;;;;
df937d20
MG
6;;;; This program is free software; you can redistribute it and/or modify
7;;;; it under the terms of the GNU General Public License as published by
8;;;; the Free Software Foundation; either version 2, or (at your option)
9;;;; any later version.
e5c5ac92 10;;;;
df937d20
MG
11;;;; This program is distributed in the hope that it will be useful,
12;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;;; GNU General Public License for more details.
e5c5ac92 15;;;;
df937d20
MG
16;;;; You should have received a copy of the GNU General Public License
17;;;; along with this software; see the file COPYING. If not, write to
18;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19;;;; Boston, MA 02111-1307 USA
20
80fdeb4e 21(use-modules (srfi srfi-13) (srfi srfi-14) (test-suite lib))
df937d20
MG
22
23(define exception:strict-infix-grammar
24 (cons 'misc-error "^strict-infix"))
25
967c0904
KR
26;;;
27;;; string-any
28;;;
29
df937d20
MG
30(with-test-prefix "string-any"
31
967c0904
KR
32 (with-test-prefix "bad char_pred"
33
34 (pass-if-exception "integer" exception:wrong-type-arg
35 (string-any 123 "abcde"))
36
37 (pass-if-exception "string" exception:wrong-type-arg
38 (string-any "zzz" "abcde")))
39
40 (with-test-prefix "char"
41
42 (pass-if "no match"
43 (not (string-any #\C "abcde")))
44
45 (pass-if "one match"
46 (string-any #\C "abCde"))
47
48 (pass-if "more than one match"
49 (string-any #\X "abXXX"))
50
51 (pass-if "no match, start index"
52 (not (string-any #\A "Abcde" 1)))
53
54 (pass-if "one match, start index"
55 (string-any #\C "abCde" 1))
56
57 (pass-if "more than one match, start index"
58 (string-any #\X "abXXX" 1))
59
60 (pass-if "no match, start and end index"
61 (not (string-any #\X "XbcdX" 1 4)))
62
63 (pass-if "one match, start and end index"
64 (string-any #\C "abCde" 1 4))
65
66 (pass-if "more than one match, start and end index"
67 (string-any #\X "abXXX" 1 4)))
68
69 (with-test-prefix "charset"
70
71 (pass-if "no match"
72 (not (string-any char-set:upper-case "abcde")))
73
74 (pass-if "one match"
75 (string-any char-set:upper-case "abCde"))
76
77 (pass-if "more than one match"
78 (string-any char-set:upper-case "abCDE"))
79
80 (pass-if "no match, start index"
81 (not (string-any char-set:upper-case "Abcde" 1)))
82
83 (pass-if "one match, start index"
84 (string-any char-set:upper-case "abCde" 1))
85
86 (pass-if "more than one match, start index"
87 (string-any char-set:upper-case "abCDE" 1))
88
89 (pass-if "no match, start and end index"
90 (not (string-any char-set:upper-case "AbcdE" 1 4)))
91
92 (pass-if "one match, start and end index"
93 (string-any char-set:upper-case "abCde" 1 4))
94
95 (pass-if "more than one match, start and end index"
96 (string-any char-set:upper-case "abCDE" 1 4)))
97
98 (with-test-prefix "pred"
df937d20 99
967c0904
KR
100 (pass-if "no match"
101 (not (string-any char-upper-case? "abcde")))
df937d20 102
967c0904
KR
103 (pass-if "one match"
104 (string-any char-upper-case? "abCde"))
df937d20 105
967c0904
KR
106 (pass-if "more than one match"
107 (string-any char-upper-case? "abCDE"))
df937d20 108
967c0904
KR
109 (pass-if "no match, start index"
110 (not (string-any char-upper-case? "Abcde" 1)))
df937d20 111
967c0904
KR
112 (pass-if "one match, start index"
113 (string-any char-upper-case? "abCde" 1))
df937d20 114
967c0904
KR
115 (pass-if "more than one match, start index"
116 (string-any char-upper-case? "abCDE" 1))
df937d20 117
967c0904
KR
118 (pass-if "no match, start and end index"
119 (not (string-any char-upper-case? "AbcdE" 1 4)))
df937d20 120
967c0904
KR
121 (pass-if "one match, start and end index"
122 (string-any char-upper-case? "abCde" 1 4))
123
124 (pass-if "more than one match, start and end index"
125 (string-any char-upper-case? "abCDE" 1 4))))
df937d20 126
4c6563e1
KR
127;;;
128;;; string-every
129;;;
130
df937d20
MG
131(with-test-prefix "string-every"
132
967c0904
KR
133 (with-test-prefix "char"
134
135 (pass-if "empty string"
136 (string-every #\X ""))
137
138 (pass-if "empty substring"
139 (string-every #\X "abc" 1 1))
140
141 (pass-if "no match at all"
142 (not (string-every #\X "abcde")))
143
144 (pass-if "not all match"
145 (not (string-every #\X "abXXX")))
146
147 (pass-if "all match"
148 (string-every #\X "XXXXX"))
149
150 (pass-if "no match at all, start index"
151 (not (string-every #\X "Xbcde" 1)))
152
153 (pass-if "not all match, start index"
154 (not (string-every #\X "XXcde" 1)))
155
156 (pass-if "all match, start index"
157 (string-every #\X "aXXXX" 1))
158
159 (pass-if "no match at all, start and end index"
160 (not (string-every #\X "XbcdX" 1 4)))
161
162 (pass-if "not all match, start and end index"
163 (not (string-every #\X "XXcde" 1 4)))
164
165 (pass-if "all match, start and end index"
166 (string-every #\X "aXXXe" 1 4)))
167
168 (with-test-prefix "charset"
169
170 (pass-if "empty string"
171 (string-every char-set:upper-case ""))
172
173 (pass-if "empty substring"
174 (string-every char-set:upper-case "abc" 1 1))
175
176 (pass-if "no match at all"
177 (not (string-every char-set:upper-case "abcde")))
178
179 (pass-if "not all match"
180 (not (string-every char-set:upper-case "abCDE")))
181
182 (pass-if "all match"
183 (string-every char-set:upper-case "ABCDE"))
184
185 (pass-if "no match at all, start index"
186 (not (string-every char-set:upper-case "Abcde" 1)))
187
188 (pass-if "not all match, start index"
189 (not (string-every char-set:upper-case "ABcde" 1)))
190
191 (pass-if "all match, start index"
192 (string-every char-set:upper-case "aBCDE" 1))
193
194 (pass-if "no match at all, start and end index"
195 (not (string-every char-set:upper-case "AbcdE" 1 4)))
196
197 (pass-if "not all match, start and end index"
198 (not (string-every char-set:upper-case "ABcde" 1 4)))
199
200 (pass-if "all match, start and end index"
201 (string-every char-set:upper-case "aBCDe" 1 4)))
202
203 (with-test-prefix "pred"
204
205 ;; in guile 1.6.4 and earlier string-every incorrectly returned #f on an
206 ;; empty string
207 (pass-if "empty string"
208 (string-every char-upper-case? ""))
209 (pass-if "empty substring"
210 (string-every char-upper-case? "abc" 1 1))
4c6563e1 211
967c0904
KR
212 (pass-if "no match at all"
213 (not (string-every char-upper-case? "abcde")))
df937d20 214
967c0904
KR
215 (pass-if "not all match"
216 (not (string-every char-upper-case? "abCDE")))
df937d20 217
967c0904
KR
218 (pass-if "all match"
219 (string-every char-upper-case? "ABCDE"))
df937d20 220
967c0904
KR
221 (pass-if "no match at all, start index"
222 (not (string-every char-upper-case? "Abcde" 1)))
df937d20 223
967c0904
KR
224 (pass-if "not all match, start index"
225 (not (string-every char-upper-case? "ABcde" 1)))
df937d20 226
967c0904
KR
227 (pass-if "all match, start index"
228 (string-every char-upper-case? "aBCDE" 1))
df937d20 229
967c0904
KR
230 (pass-if "no match at all, start and end index"
231 (not (string-every char-upper-case? "AbcdE" 1 4)))
df937d20 232
967c0904
KR
233 (pass-if "not all match, start and end index"
234 (not (string-every char-upper-case? "ABcde" 1 4)))
df937d20 235
967c0904
KR
236 (pass-if "all match, start and end index"
237 (string-every char-upper-case? "aBCDe" 1 4))))
df937d20
MG
238
239(with-test-prefix "string-tabulate"
240
241 (pass-if "static fill-char"
242 (string=? (string-tabulate (lambda (idx) #\!) 10) "!!!!!!!!!!"))
243
244 (pass-if "variable fill-char"
245 (string=? (string-tabulate
246 (lambda (idx) (integer->char (+ idx 32))) 10) " !\"#$%&'()")))
247
df937d20
MG
248(with-test-prefix "string->list"
249
250 (pass-if "empty"
251 (zero? (length (string->list ""))))
252
253 (pass-if "nonempty"
254 (= (length (string->list "foo")) 3))
255
7cfbc4f7
MG
256 (pass-if "empty, start index"
257 (zero? (length (string->list "foo" 3 3))))
df937d20 258
7cfbc4f7
MG
259 (pass-if "nonempty, start index"
260 (= (length (string->list "foo" 1 3)) 2))
df937d20
MG
261 )
262
263(with-test-prefix "reverse-list->string"
264
265 (pass-if "empty"
266 (string-null? (reverse-list->string '())))
267
268 (pass-if "nonempty"
269 (string=? "foo" (reverse-list->string '(#\o #\o #\f)))))
270
271
272(with-test-prefix "string-join"
273
274 (pass-if "empty list, no delimiter, implicit infix, empty 1"
275 (string=? "" (string-join '())))
276
277 (pass-if "empty string, no delimiter, implicit infix, empty 2"
278 (string=? "" (string-join '(""))))
279
280 (pass-if "non-empty, no delimiter, implicit infix"
281 (string=? "bla" (string-join '("bla"))))
282
283 (pass-if "empty list, implicit infix, empty 1"
284 (string=? "" (string-join '() "|delim|")))
285
286 (pass-if "empty string, implicit infix, empty 2"
287 (string=? "" (string-join '("") "|delim|")))
288
289 (pass-if "non-empty, implicit infix"
290 (string=? "bla" (string-join '("bla") "|delim|")))
291
292 (pass-if "non-empty, implicit infix"
293 (string=? "bla" (string-join '("bla") "|delim|")))
294
295 (pass-if "two strings, implicit infix"
296 (string=? "bla|delim|fasel" (string-join '("bla" "fasel") "|delim|")))
297
298 (pass-if "empty, explicit infix"
299 (string=? "" (string-join '("") "|delim|" 'infix)))
300
301 (pass-if "empty list, explicit infix"
302 (string=? "" (string-join '() "|delim|" 'infix)))
303
304 (pass-if "non-empty, explicit infix"
305 (string=? "bla" (string-join '("bla") "|delim|" 'infix)))
306
307 (pass-if "two strings, explicit infix"
308 (string=? "bla|delim|fasel" (string-join '("bla" "fasel") "|delim|"
309 'infix)))
310
e5c5ac92 311 (pass-if-exception "empty list, strict infix"
df937d20
MG
312 exception:strict-infix-grammar
313 (string-join '() "|delim|" 'strict-infix))
314
315 (pass-if "empty, strict infix"
316 (string=? "" (string-join '("") "|delim|" 'strict-infix)))
317
318 (pass-if "non-empty, strict infix"
319 (string=? "foo" (string-join '("foo") "|delim|" 'strict-infix)))
320
321 (pass-if "two strings, strict infix"
322 (string=? "foo|delim|bar" (string-join '("foo" "bar") "|delim|"
323 'strict-infix)))
324
325 (pass-if "empty list, prefix"
326 (string=? "" (string-join '() "|delim|" 'prefix)))
327
328 (pass-if "empty, prefix"
329 (string=? "|delim|" (string-join '("") "|delim|" 'prefix)))
330
331 (pass-if "non-empty, prefix"
332 (string=? "|delim|foo" (string-join '("foo") "|delim|" 'prefix)))
333
334 (pass-if "two strings, prefix"
335 (string=? "|delim|foo|delim|bar" (string-join '("foo" "bar") "|delim|"
336 'prefix)))
337
338 (pass-if "empty list, suffix"
339 (string=? "" (string-join '() "|delim|" 'suffix)))
340
341 (pass-if "empty, suffix"
342 (string=? "|delim|" (string-join '("") "|delim|" 'suffix)))
343
344 (pass-if "non-empty, suffix"
345 (string=? "foo|delim|" (string-join '("foo") "|delim|" 'suffix)))
346
347 (pass-if "two strings, suffix"
348 (string=? "foo|delim|bar|delim|" (string-join '("foo" "bar") "|delim|"
349 'suffix))))
350
df937d20
MG
351(with-test-prefix "string-copy"
352
353 (pass-if "empty string"
354 (string=? "" (string-copy "")))
355
356 (pass-if "full string"
357 (string=? "foo-bar" (string-copy "foo-bar")))
358
7cfbc4f7
MG
359 (pass-if "start index"
360 (string=? "o-bar" (string-copy "foo-bar" 2)))
df937d20 361
7cfbc4f7
MG
362 (pass-if "start and end index"
363 (string=? "o-ba" (string-copy "foo-bar" 2 6)))
df937d20
MG
364)
365
366(with-test-prefix "substring/shared"
367
368 (pass-if "empty string"
369 (eq? "" (substring/shared "" 0)))
370
371 (pass-if "non-empty string"
372 (string=? "foo" (substring/shared "foo-bar" 0 3)))
373
374 (pass-if "non-empty string, not eq?"
375 (string=? "foo-bar" (substring/shared "foo-bar" 0 7))))
376
377(with-test-prefix "string-copy!"
378
379 (pass-if "non-empty string"
380 (string=? "welld, oh yeah!"
381 (let* ((s "hello")
d7e4c2da 382 (t (string-copy "world, oh yeah!")))
df937d20
MG
383 (string-copy! t 1 s 1 3)
384 t))))
385
386(with-test-prefix "string-take"
387
388 (pass-if "empty string"
389 (string=? "" (string-take "foo bar braz" 0)))
390
391 (pass-if "non-empty string"
392 (string=? "foo " (string-take "foo bar braz" 4)))
393
394 (pass-if "full string"
395 (string=? "foo bar braz" (string-take "foo bar braz" 12))))
396
397(with-test-prefix "string-take-right"
398
399 (pass-if "empty string"
400 (string=? "" (string-take-right "foo bar braz" 0)))
401
402 (pass-if "non-empty string"
403 (string=? "braz" (string-take-right "foo bar braz" 4)))
404
405 (pass-if "full string"
406 (string=? "foo bar braz" (string-take-right "foo bar braz" 12))))
407
408(with-test-prefix "string-drop"
409
410 (pass-if "empty string"
411 (string=? "" (string-drop "foo bar braz" 12)))
412
413 (pass-if "non-empty string"
414 (string=? "braz" (string-drop "foo bar braz" 8)))
415
416 (pass-if "full string"
417 (string=? "foo bar braz" (string-drop "foo bar braz" 0))))
418
419(with-test-prefix "string-drop-right"
420
421 (pass-if "empty string"
422 (string=? "" (string-drop-right "foo bar braz" 12)))
423
424 (pass-if "non-empty string"
425 (string=? "foo " (string-drop-right "foo bar braz" 8)))
426
427 (pass-if "full string"
428 (string=? "foo bar braz" (string-drop-right "foo bar braz" 0))))
429
430(with-test-prefix "string-pad"
431
432 (pass-if "empty string, zero pad"
433 (string=? "" (string-pad "" 0)))
434
435 (pass-if "empty string, zero pad, pad char"
436 (string=? "" (string-pad "" 0)))
437
438 (pass-if "empty pad string, 2 pad "
439 (string=? " " (string-pad "" 2)))
440
441 (pass-if "empty pad string, 2 pad, pad char"
442 (string=? "!!" (string-pad "" 2 #\!)))
443
444 (pass-if "empty pad string, 2 pad, pad char, start index"
445 (string=? "!c" (string-pad "abc" 2 #\! 2)))
446
447 (pass-if "empty pad string, 2 pad, pad char, start and end index"
448 (string=? "!c" (string-pad "abcd" 2 #\! 2 3)))
449
450 (pass-if "freestyle 1"
451 (string=? "32" (string-pad (number->string 532) 2 #\!)))
452
453 (pass-if "freestyle 2"
454 (string=? "!532" (string-pad (number->string 532) 4 #\!))))
455
456(with-test-prefix "string-pad-right"
457
458 (pass-if "empty string, zero pad"
459 (string=? "" (string-pad-right "" 0)))
460
461 (pass-if "empty string, zero pad, pad char"
462 (string=? "" (string-pad-right "" 0)))
463
464 (pass-if "empty pad string, 2 pad "
465 (string=? " " (string-pad-right "" 2)))
466
467 (pass-if "empty pad string, 2 pad, pad char"
468 (string=? "!!" (string-pad-right "" 2 #\!)))
469
470 (pass-if "empty pad string, 2 pad, pad char, start index"
471 (string=? "c!" (string-pad-right "abc" 2 #\! 2)))
472
473 (pass-if "empty pad string, 2 pad, pad char, start and end index"
474 (string=? "c!" (string-pad-right "abcd" 2 #\! 2 3)))
475
476 (pass-if "freestyle 1"
477 (string=? "53" (string-pad-right (number->string 532) 2 #\!)))
478
479 (pass-if "freestyle 2"
480 (string=? "532!" (string-pad-right (number->string 532) 4 #\!))))
481
f764e6d1
MG
482(with-test-prefix "string-trim"
483
484 (pass-if "empty string"
485 (string=? "" (string-trim "")))
486
487 (pass-if "no char/pred"
488 (string=? "foo " (string-trim " \tfoo ")))
489
490 (pass-if "start index, pred"
491 (string=? "foo " (string-trim " \tfoo " char-whitespace? 1)))
492
493 (pass-if "start and end index, pred"
494 (string=? "f" (string-trim " \tfoo " char-whitespace? 1 3)))
495
496 (pass-if "start index, char"
497 (string=? "\tfoo " (string-trim " \tfoo " #\space 1)))
498
499 (pass-if "start and end index, char"
500 (string=? "\tf" (string-trim " \tfoo " #\space 1 3)))
501
502 (pass-if "start index, charset"
503 (string=? "foo " (string-trim " \tfoo " char-set:whitespace 1)))
504
505 (pass-if "start and end index, charset"
506 (string=? "f" (string-trim " \tfoo " char-set:whitespace 1 3))))
507
508(with-test-prefix "string-trim-right"
509
510 (pass-if "empty string"
511 (string=? "" (string-trim-right "")))
512
513 (pass-if "no char/pred"
514 (string=? " \tfoo" (string-trim-right " \tfoo ")))
515
516 (pass-if "start index, pred"
517 (string=? "\tfoo" (string-trim-right " \tfoo " char-whitespace? 1)))
518
519 (pass-if "start and end index, pred"
520 (string=? "\tf" (string-trim-right " \tfoo " char-whitespace? 1 3)))
521
522 (pass-if "start index, char"
523 (string=? "\tfoo" (string-trim-right " \tfoo " #\space 1)))
524
525 (pass-if "start and end index, char"
526 (string=? "\tf" (string-trim-right " \tfoo " #\space 1 3)))
527
528 (pass-if "start index, charset"
529 (string=? "\tfoo" (string-trim-right " \tfoo " char-set:whitespace 1)))
530
531 (pass-if "start and end index, charset"
532 (string=? "\tf" (string-trim-right " \tfoo " char-set:whitespace 1 3))))
533
534(with-test-prefix "string-trim-both"
535
536 (pass-if "empty string"
537 (string=? "" (string-trim-both "")))
538
539 (pass-if "no char/pred"
540 (string=? "foo" (string-trim-both " \tfoo ")))
541
542 (pass-if "start index, pred"
543 (string=? "foo" (string-trim-both " \tfoo " char-whitespace? 1)))
544
545 (pass-if "start and end index, pred"
546 (string=? "f" (string-trim-both " \tfoo " char-whitespace? 1 3)))
547
548 (pass-if "start index, char"
549 (string=? "\tfoo" (string-trim-both " \tfoo " #\space 1)))
550
551 (pass-if "start and end index, char"
552 (string=? "\tf" (string-trim-both " \tfoo " #\space 1 3)))
553
554 (pass-if "start index, charset"
555 (string=? "foo" (string-trim-both " \tfoo " char-set:whitespace 1)))
556
557 (pass-if "start and end index, charset"
558 (string=? "f" (string-trim-both " \tfoo " char-set:whitespace 1 3))))
559
f764e6d1
MG
560(define s0 (make-string 200 #\!))
561(define s1 (make-string 0 #\!))
562
563(with-test-prefix "string-fill!"
564
565 (pass-if "empty string, no indices"
566 (string-fill! s1 #\*)
567 (= (string-length s1) 0))
568
569 (pass-if "empty string, start index"
570 (string-fill! s1 #\* 0)
571 (= (string-length s1) 0))
572
573 (pass-if "empty string, start and end index"
574 (string-fill! s1 #\* 0 0)
575 (= (string-length s1) 0))
576
577 (pass-if "no indices"
578 (string-fill! s0 #\*)
579 (char=? (string-ref s0 0) #\*))
580
581 (pass-if "start index"
582 (string-fill! s0 #\+ 10)
583 (char=? (string-ref s0 11) #\+))
584
585 (pass-if "start and end index"
586 (string-fill! s0 #\| 12 20)
587 (char=? (string-ref s0 13) #\|)))
588
5f5850b3
MG
589(with-test-prefix "string-prefix-length"
590
591 (pass-if "empty prefix"
592 (= 0 (string-prefix-length "" "foo bar")))
593
594 (pass-if "non-empty prefix - match"
595 (= 3 (string-prefix-length "foo" "foo bar")))
596
597 (pass-if "non-empty prefix - no match"
598 (= 0 (string-prefix-length "bar" "foo bar"))))
599
600(with-test-prefix "string-prefix-length-ci"
601
602 (pass-if "empty prefix"
603 (= 0 (string-prefix-length-ci "" "foo bar")))
604
605 (pass-if "non-empty prefix - match"
606 (= 3 (string-prefix-length-ci "fOo" "foo bar")))
607
608 (pass-if "non-empty prefix - no match"
609 (= 0 (string-prefix-length-ci "bAr" "foo bar"))))
610
611(with-test-prefix "string-suffix-length"
612
613 (pass-if "empty suffix"
614 (= 0 (string-suffix-length "" "foo bar")))
615
616 (pass-if "non-empty suffix - match"
617 (= 3 (string-suffix-length "bar" "foo bar")))
618
619 (pass-if "non-empty suffix - no match"
620 (= 0 (string-suffix-length "foo" "foo bar"))))
621
622(with-test-prefix "string-suffix-length-ci"
623
624 (pass-if "empty suffix"
625 (= 0 (string-suffix-length-ci "" "foo bar")))
626
627 (pass-if "non-empty suffix - match"
628 (= 3 (string-suffix-length-ci "bAr" "foo bar")))
629
630 (pass-if "non-empty suffix - no match"
631 (= 0 (string-suffix-length-ci "fOo" "foo bar"))))
632
633(with-test-prefix "string-prefix?"
634
635 (pass-if "empty prefix"
636 (string-prefix? "" "foo bar"))
637
638 (pass-if "non-empty prefix - match"
639 (string-prefix? "foo" "foo bar"))
640
641 (pass-if "non-empty prefix - no match"
642 (not (string-prefix? "bar" "foo bar"))))
643
644(with-test-prefix "string-prefix-ci?"
645
646 (pass-if "empty prefix"
647 (string-prefix-ci? "" "foo bar"))
648
649 (pass-if "non-empty prefix - match"
650 (string-prefix-ci? "fOo" "foo bar"))
651
652 (pass-if "non-empty prefix - no match"
653 (not (string-prefix-ci? "bAr" "foo bar"))))
654
655(with-test-prefix "string-suffix?"
656
657 (pass-if "empty suffix"
658 (string-suffix? "" "foo bar"))
659
660 (pass-if "non-empty suffix - match"
661 (string-suffix? "bar" "foo bar"))
662
663 (pass-if "non-empty suffix - no match"
664 (not (string-suffix? "foo" "foo bar"))))
665
666(with-test-prefix "string-suffix-ci?"
667
668 (pass-if "empty suffix"
669 (string-suffix-ci? "" "foo bar"))
670
671 (pass-if "non-empty suffix - match"
672 (string-suffix-ci? "bAr" "foo bar"))
673
674 (pass-if "non-empty suffix - no match"
675 (not (string-suffix-ci? "fOo" "foo bar"))))
676
5f5850b3
MG
677(with-test-prefix "string-index"
678
679 (pass-if "empty string - char"
680 (not (string-index "" #\a)))
681
682 (pass-if "non-empty - char - match"
683 (= 5 (string-index "foo bar" #\a)))
684
685 (pass-if "non-empty - char - no match"
686 (not (string-index "frobnicate" #\x)))
687
688 (pass-if "empty string - char - start index"
689 (not (string-index "" #\a 0)))
690
691 (pass-if "non-empty - char - match - start index"
692 (= 5 (string-index "foo bar" #\a 1)))
693
694 (pass-if "non-empty - char - no match - start index"
695 (not (string-index "frobnicate" #\x 2)))
696
697 (pass-if "empty string - char - start and end index"
698 (not (string-index "" #\a 0 0)))
699
700 (pass-if "non-empty - char - match - start and end index"
701 (= 5 (string-index "foo bar" #\a 1 6)))
702
703 (pass-if "non-empty - char - no match - start and end index"
704 (not (string-index "frobnicate" #\a 2 5)))
705
706 (pass-if "empty string - charset"
707 (not (string-index "" char-set:letter)))
708
709 (pass-if "non-empty - charset - match"
710 (= 0 (string-index "foo bar" char-set:letter)))
711
712 (pass-if "non-empty - charset - no match"
713 (not (string-index "frobnicate" char-set:digit)))
714
715 (pass-if "empty string - charset - start index"
716 (not (string-index "" char-set:letter 0)))
717
718 (pass-if "non-empty - charset - match - start index"
719 (= 1 (string-index "foo bar" char-set:letter 1)))
720
721 (pass-if "non-empty - charset - no match - start index"
722 (not (string-index "frobnicate" char-set:digit 2)))
723
724 (pass-if "empty string - charset - start and end index"
725 (not (string-index "" char-set:letter 0 0)))
726
727 (pass-if "non-empty - charset - match - start and end index"
728 (= 1 (string-index "foo bar" char-set:letter 1 6)))
729
730 (pass-if "non-empty - charset - no match - start and end index"
731 (not (string-index "frobnicate" char-set:digit 2 5)))
732
733 (pass-if "empty string - pred"
734 (not (string-index "" char-alphabetic?)))
735
736 (pass-if "non-empty - pred - match"
737 (= 0 (string-index "foo bar" char-alphabetic?)))
738
739 (pass-if "non-empty - pred - no match"
740 (not (string-index "frobnicate" char-numeric?)))
741
742 (pass-if "empty string - pred - start index"
743 (not (string-index "" char-alphabetic? 0)))
744
745 (pass-if "non-empty - pred - match - start index"
746 (= 1 (string-index "foo bar" char-alphabetic? 1)))
747
748 (pass-if "non-empty - pred - no match - start index"
749 (not (string-index "frobnicate" char-numeric? 2)))
750
751 (pass-if "empty string - pred - start and end index"
752 (not (string-index "" char-alphabetic? 0 0)))
753
754 (pass-if "non-empty - pred - match - start and end index"
755 (= 1 (string-index "foo bar" char-alphabetic? 1 6)))
756
757 (pass-if "non-empty - pred - no match - start and end index"
758 (not (string-index "frobnicate" char-numeric? 2 5))))
759
760(with-test-prefix "string-index-right"
761
762 (pass-if "empty string - char"
763 (not (string-index-right "" #\a)))
764
765 (pass-if "non-empty - char - match"
766 (= 5 (string-index-right "foo bar" #\a)))
767
768 (pass-if "non-empty - char - no match"
769 (not (string-index-right "frobnicate" #\x)))
770
771 (pass-if "empty string - char - start index-right"
772 (not (string-index-right "" #\a 0)))
773
774 (pass-if "non-empty - char - match - start index"
775 (= 5 (string-index-right "foo bar" #\a 1)))
776
777 (pass-if "non-empty - char - no match - start index"
778 (not (string-index-right "frobnicate" #\x 2)))
779
780 (pass-if "empty string - char - start and end index"
781 (not (string-index-right "" #\a 0 0)))
782
783 (pass-if "non-empty - char - match - start and end index"
784 (= 5 (string-index-right "foo bar" #\a 1 6)))
785
786 (pass-if "non-empty - char - no match - start and end index"
787 (not (string-index-right "frobnicate" #\a 2 5)))
788
789 (pass-if "empty string - charset"
790 (not (string-index-right "" char-set:letter)))
791
792 (pass-if "non-empty - charset - match"
793 (= 6 (string-index-right "foo bar" char-set:letter)))
794
795 (pass-if "non-empty - charset - no match"
796 (not (string-index-right "frobnicate" char-set:digit)))
797
798 (pass-if "empty string - charset - start index"
799 (not (string-index-right "" char-set:letter 0)))
800
801 (pass-if "non-empty - charset - match - start index"
802 (= 6 (string-index-right "foo bar" char-set:letter 1)))
803
804 (pass-if "non-empty - charset - no match - start index"
805 (not (string-index-right "frobnicate" char-set:digit 2)))
806
807 (pass-if "empty string - charset - start and end index"
808 (not (string-index-right "" char-set:letter 0 0)))
809
810 (pass-if "non-empty - charset - match - start and end index"
811 (= 5 (string-index-right "foo bar" char-set:letter 1 6)))
812
813 (pass-if "non-empty - charset - no match - start and end index"
814 (not (string-index-right "frobnicate" char-set:digit 2 5)))
815
816 (pass-if "empty string - pred"
817 (not (string-index-right "" char-alphabetic?)))
818
819 (pass-if "non-empty - pred - match"
820 (= 6 (string-index-right "foo bar" char-alphabetic?)))
821
822 (pass-if "non-empty - pred - no match"
823 (not (string-index-right "frobnicate" char-numeric?)))
824
825 (pass-if "empty string - pred - start index"
826 (not (string-index-right "" char-alphabetic? 0)))
827
828 (pass-if "non-empty - pred - match - start index"
829 (= 6 (string-index-right "foo bar" char-alphabetic? 1)))
830
831 (pass-if "non-empty - pred - no match - start index"
832 (not (string-index-right "frobnicate" char-numeric? 2)))
833
834 (pass-if "empty string - pred - start and end index"
835 (not (string-index-right "" char-alphabetic? 0 0)))
836
837 (pass-if "non-empty - pred - match - start and end index"
838 (= 5 (string-index-right "foo bar" char-alphabetic? 1 6)))
839
840 (pass-if "non-empty - pred - no match - start and end index"
841 (not (string-index-right "frobnicate" char-numeric? 2 5))))
842
843(with-test-prefix "string-skip"
844
845 (pass-if "empty string - char"
846 (not (string-skip "" #\a)))
847
848 (pass-if "non-empty - char - match"
849 (= 0 (string-skip "foo bar" #\a)))
850
851 (pass-if "non-empty - char - no match"
852 (= 0 (string-skip "frobnicate" #\x)))
853
854 (pass-if "empty string - char - start index"
855 (not (string-skip "" #\a 0)))
856
857 (pass-if "non-empty - char - match - start index"
858 (= 1 (string-skip "foo bar" #\a 1)))
859
860 (pass-if "non-empty - char - no match - start index"
861 (= 2 (string-skip "frobnicate" #\x 2)))
862
863 (pass-if "empty string - char - start and end index"
864 (not (string-skip "" #\a 0 0)))
865
866 (pass-if "non-empty - char - match - start and end index"
867 (= 1 (string-skip "foo bar" #\a 1 6)))
868
869 (pass-if "non-empty - char - no match - start and end index"
870 (= 2 (string-skip "frobnicate" #\a 2 5)))
871
872 (pass-if "empty string - charset"
873 (not (string-skip "" char-set:letter)))
874
875 (pass-if "non-empty - charset - match"
876 (= 3 (string-skip "foo bar" char-set:letter)))
877
878 (pass-if "non-empty - charset - no match"
879 (= 0 (string-skip "frobnicate" char-set:digit)))
880
881 (pass-if "empty string - charset - start index"
882 (not (string-skip "" char-set:letter 0)))
883
884 (pass-if "non-empty - charset - match - start index"
885 (= 3 (string-skip "foo bar" char-set:letter 1)))
886
887 (pass-if "non-empty - charset - no match - start index"
888 (= 2 (string-skip "frobnicate" char-set:digit 2)))
889
890 (pass-if "empty string - charset - start and end index"
891 (not (string-skip "" char-set:letter 0 0)))
892
893 (pass-if "non-empty - charset - match - start and end index"
894 (= 3 (string-skip "foo bar" char-set:letter 1 6)))
895
896 (pass-if "non-empty - charset - no match - start and end index"
897 (= 2 (string-skip "frobnicate" char-set:digit 2 5)))
898
899 (pass-if "empty string - pred"
900 (not (string-skip "" char-alphabetic?)))
901
902 (pass-if "non-empty - pred - match"
903 (= 3 (string-skip "foo bar" char-alphabetic?)))
904
905 (pass-if "non-empty - pred - no match"
906 (= 0 (string-skip "frobnicate" char-numeric?)))
907
908 (pass-if "empty string - pred - start index"
909 (not (string-skip "" char-alphabetic? 0)))
910
911 (pass-if "non-empty - pred - match - start index"
912 (= 3 (string-skip "foo bar" char-alphabetic? 1)))
913
914 (pass-if "non-empty - pred - no match - start index"
915 (= 2 (string-skip "frobnicate" char-numeric? 2)))
916
917 (pass-if "empty string - pred - start and end index"
918 (not (string-skip "" char-alphabetic? 0 0)))
919
920 (pass-if "non-empty - pred - match - start and end index"
921 (= 3 (string-skip "foo bar" char-alphabetic? 1 6)))
922
923 (pass-if "non-empty - pred - no match - start and end index"
924 (= 2 (string-skip "frobnicate" char-numeric? 2 5))))
925
926(with-test-prefix "string-skip-right"
927
928 (pass-if "empty string - char"
929 (not (string-skip-right "" #\a)))
930
931 (pass-if "non-empty - char - match"
932 (= 6 (string-skip-right "foo bar" #\a)))
933
934 (pass-if "non-empty - char - no match"
935 (= 9 (string-skip-right "frobnicate" #\x)))
936
937 (pass-if "empty string - char - start index-right"
938 (not (string-skip-right "" #\a 0)))
939
940 (pass-if "non-empty - char - match - start index"
941 (= 6 (string-skip-right "foo bar" #\a 1)))
942
943 (pass-if "non-empty - char - no match - start index"
944 (= 9 (string-skip-right "frobnicate" #\x 2)))
945
946 (pass-if "empty string - char - start and end index"
947 (not (string-skip-right "" #\a 0 0)))
948
949 (pass-if "non-empty - char - match - start and end index"
950 (= 4 (string-skip-right "foo bar" #\a 1 6)))
951
952 (pass-if "non-empty - char - no match - start and end index"
953 (= 4 (string-skip-right "frobnicate" #\a 2 5)))
954
955 (pass-if "empty string - charset"
956 (not (string-skip-right "" char-set:letter)))
957
958 (pass-if "non-empty - charset - match"
959 (= 3 (string-skip-right "foo bar" char-set:letter)))
960
961 (pass-if "non-empty - charset - no match"
962 (= 9 (string-skip-right "frobnicate" char-set:digit)))
963
964 (pass-if "empty string - charset - start index"
965 (not (string-skip-right "" char-set:letter 0)))
966
967 (pass-if "non-empty - charset - match - start index"
968 (= 3 (string-skip-right "foo bar" char-set:letter 1)))
969
970 (pass-if "non-empty - charset - no match - start index"
971 (= 9 (string-skip-right "frobnicate" char-set:digit 2)))
972
973 (pass-if "empty string - charset - start and end index"
974 (not (string-skip-right "" char-set:letter 0 0)))
975
976 (pass-if "non-empty - charset - match - start and end index"
977 (= 3 (string-skip-right "foo bar" char-set:letter 1 6)))
978
979 (pass-if "non-empty - charset - no match - start and end index"
980 (= 4 (string-skip-right "frobnicate" char-set:digit 2 5)))
981
982 (pass-if "empty string - pred"
983 (not (string-skip-right "" char-alphabetic?)))
984
985 (pass-if "non-empty - pred - match"
986 (= 3 (string-skip-right "foo bar" char-alphabetic?)))
987
988 (pass-if "non-empty - pred - no match"
989 (= 9 (string-skip-right "frobnicate" char-numeric?)))
990
991 (pass-if "empty string - pred - start index"
992 (not (string-skip-right "" char-alphabetic? 0)))
993
994 (pass-if "non-empty - pred - match - start index"
995 (= 3 (string-skip-right "foo bar" char-alphabetic? 1)))
996
997 (pass-if "non-empty - pred - no match - start index"
998 (= 9 (string-skip-right "frobnicate" char-numeric? 2)))
999
1000 (pass-if "empty string - pred - start and end index"
1001 (not (string-skip-right "" char-alphabetic? 0 0)))
1002
1003 (pass-if "non-empty - pred - match - start and end index"
1004 (= 3 (string-skip-right "foo bar" char-alphabetic? 1 6)))
1005
1006 (pass-if "non-empty - pred - no match - start and end index"
1007 (= 4 (string-skip-right "frobnicate" char-numeric? 2 5))))
1008
f764e6d1
MG
1009(with-test-prefix "string-replace"
1010
1011 (pass-if "empty string(s), no indices"
1012 (string=? "" (string-replace "" "")))
1013
1014 (pass-if "empty string(s), 1 index"
1015 (string=? "" (string-replace "" "" 0)))
1016
1017 (pass-if "empty string(s), 2 indices"
1018 (string=? "" (string-replace "" "" 0 0)))
1019
1020 (pass-if "empty string(s), 3 indices"
1021 (string=? "" (string-replace "" "" 0 0 0)))
1022
1023 (pass-if "empty string(s), 4 indices"
1024 (string=? "" (string-replace "" "" 0 0 0 0)))
1025
1026 (pass-if "no indices"
1027 (string=? "uu" (string-replace "foo bar" "uu")))
1028
1029 (pass-if "one index"
1030 (string=? "fuu" (string-replace "foo bar" "uu" 1)))
1031
1032 (pass-if "two indices"
1033 (string=? "fuuar" (string-replace "foo bar" "uu" 1 5)))
1034
1035 (pass-if "three indices"
1036 (string=? "fuar" (string-replace "foo bar" "uu" 1 5 1)))
1037
1038 (pass-if "four indices"
1039 (string=? "fuar" (string-replace "foo bar" "uu" 1 5 1 2))))
1040
1041(with-test-prefix "string-tokenize"
1042
1043 (pass-if "empty string, no char/pred"
1044 (zero? (length (string-tokenize ""))))
1045
f764e6d1
MG
1046 (pass-if "empty string, charset"
1047 (zero? (length (string-tokenize "" char-set:punctuation))))
1048
1049 (pass-if "no char/pred"
1050 (equal? '("foo" "bar" "!a") (string-tokenize "foo\tbar !a")))
1051
f764e6d1 1052 (pass-if "charset"
d26d2903
MV
1053 (equal? '("foo" "bar" "!a") (string-tokenize "foo\tbar !a"
1054 char-set:graphic)))
f764e6d1
MG
1055
1056 (pass-if "charset, start index"
d26d2903
MV
1057 (equal? '("oo" "bar" "!a") (string-tokenize "foo\tbar !a"
1058 char-set:graphic 1)))
f764e6d1
MG
1059
1060 (pass-if "charset, start and end index"
d26d2903
MV
1061 (equal? '("oo" "bar" "!") (string-tokenize "foo\tbar !a"
1062 char-set:graphic 1 9))))
f764e6d1
MG
1063
1064(with-test-prefix "string-filter"
1065
1066 (pass-if "empty string, char"
1067 (string=? "" (string-filter "" #\.)))
1068
1069 (pass-if "empty string, charset"
1070 (string=? "" (string-filter "" char-set:punctuation)))
1071
1072 (pass-if "empty string, pred"
1073 (string=? "" (string-filter "" char-alphabetic?)))
1074
1075 (pass-if "char"
1076 (string=? "..." (string-filter ".foo.bar." #\.)))
1077
1078 (pass-if "charset"
1079 (string=? "..." (string-filter ".foo.bar." char-set:punctuation)))
1080
1081 (pass-if "pred"
1082 (string=? "foobar" (string-filter ".foo.bar." char-alphabetic?)))
1083
1084 (pass-if "char, start index"
1085 (string=? ".." (string-filter ".foo.bar." #\. 2)))
1086
1087 (pass-if "charset, start index"
1088 (string=? ".." (string-filter ".foo.bar." char-set:punctuation 2)))
1089
1090 (pass-if "pred, start index"
1091 (string=? "oobar" (string-filter ".foo.bar." char-alphabetic? 2)))
1092
1093 (pass-if "char, start and end index"
1094 (string=? "" (string-filter ".foo.bar." #\. 2 4)))
1095
1096 (pass-if "charset, start and end index"
1097 (string=? "" (string-filter ".foo.bar." char-set:punctuation 2 4)))
1098
1099 (pass-if "pred, start and end index"
1100 (string=? "oo" (string-filter ".foo.bar." char-alphabetic? 2 4))))
1101
1102(with-test-prefix "string-delete"
1103
1104 (pass-if "empty string, char"
1105 (string=? "" (string-delete "" #\.)))
1106
1107 (pass-if "empty string, charset"
1108 (string=? "" (string-delete "" char-set:punctuation)))
1109
1110 (pass-if "empty string, pred"
1111 (string=? "" (string-delete "" char-alphabetic?)))
1112
1113 (pass-if "char"
1114 (string=? "foobar" (string-delete ".foo.bar." #\.)))
1115
1116 (pass-if "charset"
1117 (string=? "foobar" (string-delete ".foo.bar." char-set:punctuation)))
1118
1119 (pass-if "pred"
1120 (string=? "..." (string-delete ".foo.bar." char-alphabetic?)))
1121
1122 (pass-if "char, start index"
1123 (string=? "oobar" (string-delete ".foo.bar." #\. 2)))
1124
1125 (pass-if "charset, start index"
1126 (string=? "oobar" (string-delete ".foo.bar." char-set:punctuation 2)))
1127
1128 (pass-if "pred, start index"
1129 (string=? ".." (string-delete ".foo.bar." char-alphabetic? 2)))
1130
1131 (pass-if "char, start and end index"
1132 (string=? "oo" (string-delete ".foo.bar." #\. 2 4)))
1133
1134 (pass-if "charset, start and end index"
1135 (string=? "oo" (string-delete ".foo.bar." char-set:punctuation 2 4)))
1136
1137 (pass-if "pred, start and end index"
1138 (string=? "" (string-delete ".foo.bar." char-alphabetic? 2 4))))
80fdeb4e
MD
1139
1140(with-test-prefix "string-map"
1141
1142 (pass-if "constant"
1143 (string=? "xxx" (string-map (lambda (c) #\x) "foo")))
1144
1145 (pass-if "identity"
1146 (string=? "foo" (string-map identity "foo")))
1147
1148 (pass-if "upcase"
1149 (string=? "FOO" (string-map char-upcase "foo"))))
e5c5ac92
TTN
1150
1151(with-test-prefix "string-for-each"
1152
1153 (pass-if "copy"
1154 (let* ((foo "foo")
1155 (bar (make-string (string-length foo)))
1156 (i 0))
1157 (string-for-each
1158 (lambda (c) (string-set! bar i c) (set! i (1+ i))) foo)
1159 (string=? foo bar)))
1160
1161 (pass-if "index"
1162 (let* ((foo "foo")
1163 (bar (make-string (string-length foo))))
1164 (string-for-each-index
1165 (lambda (i) (string-set! bar i (string-ref foo i))) foo)
1166 (string=? foo bar))))
1167