Debian Codegraph @

ext/srfi/test-srfi-13.scm

raw
1 ;;;
2 ;;; test SRFI-13
3 ;;;
4
5 (use gauche.test)
6 (test-start "SRFI-13")
7 (test-section "SRFI-13")
8
9 (define-module srfi-13-tests
10 (use gauche.test)
11 (use srfi.13)
12 (test-module 'srfi.13)
13
14 (test* "string-null?" #f (string-null? "abc"))
15 (test* "string-null?" #t (string-null? ""))
16 (test* "string-every" #t (string-every #\a ""))
17 (test* "string-every" #t (string-every #\a "aaaa"))
18 (test* "string-every" #f (string-every #\a "aaba"))
19 (test* "string-every" #t (string-every #[a-z] "aaba"))
20 (test* "string-every" #f (string-every #[a-z] "aAba"))
21 (test* "string-every" #t (string-every #[a-z] ""))
22 (test* "string-every" #t (string-every (^x (char-ci=? x #\a)) "aAaA"))
23 (test* "string-every" #f (string-every (^x (char-ci=? x #\a)) "aAbA"))
24 (test* "string-every" (char->integer #\A)
25 (string-every (^x (char->integer x)) "aAbA"))
26 (test* "string-every" #t
27 (string-every (^x (error "hoge")) ""))
28 (test* "string-any" #t (string-any #\a "aaaa"))
29 (test* "string-any" #f (string-any #\a "Abcd"))
30 (test* "string-any" #f (string-any #\a ""))
31 (test* "string-any" #t (string-any #[a-z] "ABcD"))
32 (test* "string-any" #f (string-any #[a-z] "ABCD"))
33 (test* "string-any" #f (string-any #[a-z] ""))
34 (test* "string-any" #t (string-any (^x (char-ci=? x #\a)) "CAaA"))
35 (test* "string-any" #f (string-any (^x (char-ci=? x #\a)) "ZBRC"))
36 (test* "string-any" #f (string-any (^x (char-ci=? x #\a)) ""))
37 (test* "string-any" (char->integer #\a)
38 (string-any (^x (char->integer x)) "aAbA"))
39 (test* "string-tabulate" "0123456789"
40 (string-tabulate (^[code] (integer->char (+ code (char->integer #\0))))
41 10))
42 (test* "string-tabulate" ""
43 (string-tabulate (^[code] (integer->char (+ code (char->integer #\0))))
44 0))
45 (test* "reverse-list->string" "cBa"
46 (reverse-list->string '(#\a #\B #\c)))
47 (test* "reverse-list->string" ""
48 (reverse-list->string '()))
49 ; string-join : Gauche builtin.
50 (test* "substring/shared" "cde" (substring/shared "abcde" 2))
51 (test* "substring/shared" "cd" (substring/shared "abcde" 2 4))
52 (test* "string-copy!" "abCDEfg"
53 (rlet1 x (string-copy "abcdefg")
54 (string-copy! x 2 "CDE")))
55 (test* "string-copy!" "abCDEfg"
56 (rlet1 x (string-copy "abcdefg")
57 (string-copy! x 2 "ZABCDE" 3)))
58 (test* "string-copy!" "abCDEfg"
59 (rlet1 x (string-copy "abcdefg")
60 (string-copy! x 2 "ZABCDEFG" 3 6)))
61 (test* "string-copy!" "CDEFGfg"
62 (rlet1 x (string-copy "abcdefg")
63 (string-copy! x 0 "ZABCDEFG" 3)))
64 (test* "string-take" "Pete S" (string-take "Pete Szilagyi" 6))
65 (test* "string-take" "" (string-take "Pete Szilagyi" 0))
66 (test* "string-take" "Pete Szilagyi" (string-take "Pete Szilagyi" 13))
67 (test* "string-drop" "zilagyi" (string-drop "Pete Szilagyi" 6))
68 (test* "string-drop" "Pete Szilagyi" (string-drop "Pete Szilagyi" 0))
69 (test* "string-drop" "" (string-drop "Pete Szilagyi" 13))
70
71 (test* "string-take-right" "rules" (string-take-right "Beta rules" 5))
72 (test* "string-take-right" "" (string-take-right "Beta rules" 0))
73 (test* "string-take-right" "Beta rules" (string-take-right "Beta rules" 10))
74 (test* "string-drop-right" "Beta " (string-drop-right "Beta rules" 5))
75 (test* "string-drop-right" "Beta rules" (string-drop-right "Beta rules" 0))
76 (test* "string-drop-right" "" (string-drop-right "Beta rules" 10))
77
78 (test* "string-pad" " 325" (string-pad "325" 5))
79 (test* "string-pad" "71325" (string-pad "71325" 5))
80 (test* "string-pad" "71325" (string-pad "8871325" 5))
81 (test* "string-pad" "~~325" (string-pad "325" 5 #\~))
82 (test* "string-pad" "~~~25" (string-pad "325" 5 #\~ 1))
83 (test* "string-pad" "~~~~2" (string-pad "325" 5 #\~ 1 2))
84 (test* "string-pad-right" "325 " (string-pad-right "325" 5))
85 (test* "string-pad-right" "71325" (string-pad-right "71325" 5))
86 (test* "string-pad-right" "88713" (string-pad-right "8871325" 5))
87 (test* "string-pad-right" "325~~" (string-pad-right "325" 5 #\~))
88 (test* "string-pad-right" "25~~~" (string-pad-right "325" 5 #\~ 1))
89 (test* "string-pad-right" "2~~~~" (string-pad-right "325" 5 #\~ 1 2))
90
91 (test* "string-trim" "a b c d \r\n"
92 (string-trim " \t a b c d \r\n"))
93 (test* "string-trim" "\t a b c d \r\n"
94 (string-trim " \t a b c d \r\n" #\space))
95 (test* "string-trim" "a b c d \r\n"
96 (string-trim "4358948a b c d \r\n" #[\d]))
97
98 (test* "string-trim-right" " \t a b c d"
99 (string-trim-right " \t a b c d \r\n"))
100 (test* "string-trim-right" " \t a b c d "
101 (string-trim-right " \t a b c d \r\n" #[\r\n]))
102 (test* "string-trim-right" "349853a b c d"
103 (string-trim-right "349853a b c d03490" #[\d]))
104
105 (test* "string-trim-both" "a b c d"
106 (string-trim-both " \t a b c d \r\n"))
107 (test* "string-trim-both" " \t a b c d "
108 (string-trim-both " \t a b c d \r\n" #[\r\n]))
109 (test* "string-trim-both" "a b c d"
110 (string-trim-both "349853a b c d03490" #[\d]))
111
112 ;; string-fill - in string.scm
113
114 (test* "string-compare" 5
115 (string-compare "The cat in the hat" "abcdefgh"
116 values values values
117 4 6 2 4))
118 (test* "string-compare-ci" 5
119 (string-compare-ci "The cat in the hat" "ABCDEFGH"
120 values values values
121 4 6 2 4))
122
123 ;; TODO: bunch of string= families
124
125 (test* "string-prefix-length" 5
126 (string-prefix-length "cancaNCAM" "cancancan"))
127 (test* "string-prefix-length" 1
128 (string-prefix-length "abc" "a"))
129 (test* "string-prefix-length" 1
130 (string-prefix-length "a" "abc"))
131 (test* "string-prefix-length" 0
132 (string-prefix-length "abc" ""))
133 (test* "string-prefix-length" 0
134 (string-prefix-length "" "abc"))
135 (test* "string-prefix-length" 0
136 (string-prefix-length "" ""))
137 (test* "string-prefix-length-ci" 8
138 (string-prefix-length-ci "cancaNCAM" "cancancan"))
139 (test* "string-suffix-length" 2
140 (string-suffix-length "CanCan" "cankancan"))
141 (test* "string-suffix-length-ci" 5
142 (string-suffix-length-ci "CanCan" "cankancan"))
143 (test* "string-prefix-length-ci" 1
144 (string-prefix-length-ci "abc" "A"))
145 (test* "string-prefix-length-ci" 1
146 (string-prefix-length-ci "A" "abc"))
147 (test* "string-prefix-length-ci" 0
148 (string-prefix-length-ci "abc" ""))
149 (test* "string-prefix-length-ci" 0
150 (string-prefix-length-ci "" "abc"))
151 (test* "string-prefix-length-ci" 0
152 (string-prefix-length-ci "" ""))
153
154 (test* "string-prefix?" #t (string-prefix? "abcd" "abcdefg"))
155 (test* "string-prefix?" #f (string-prefix? "abcf" "abcdefg"))
156 (test* "string-prefix-ci?" #t (string-prefix-ci? "abcd" "aBCDEfg"))
157 (test* "string-prefix-ci?" #f (string-prefix-ci? "abcf" "aBCDEfg"))
158 (test* "string-suffix?" #t (string-suffix? "defg" "abcdefg"))
159 (test* "string-suffix?" #f (string-suffix? "aefg" "abcdefg"))
160 (test* "string-suffix-ci?" #t (string-suffix-ci? "defg" "aBCDEfg"))
161 (test* "string-suffix-ci?" #f (string-suffix-ci? "aefg" "aBCDEfg"))
162
163 (test* "string-index" 4
164 (string-index "abcd:efgh:ijkl" #\:))
165 (test* "string-index" 4
166 (string-index "abcd:efgh;ijkl" #[\W]))
167 (test* "string-index" #f
168 (string-index "abcd:efgh;ijkl" #[\d]))
169 (test* "string-index" 9
170 (string-index "abcd:efgh:ijkl" #\: 5))
171 (test* "string-index-right" 4
172 (string-index-right "abcd:efgh;ijkl" #\:))
173 (test* "string-index-right" 9
174 (string-index-right "abcd:efgh;ijkl" #[\W]))
175 (test* "string-index-right" #f
176 (string-index-right "abcd:efgh;ijkl" #[\d]))
177 (test* "string-index-right" 4
178 (string-index-right "abcd:efgh;ijkl" #[\W] 2 5))
179
180 (test* "string-count" 2
181 (string-count "abc def\tghi jkl" #\space))
182 (test* "string-count" 3
183 (string-count "abc def\tghi jkl" #[\s]))
184 (test* "string-count" 2
185 (string-count "abc def\tghi jkl" #[\s] 4))
186 (test* "string-count" 1
187 (string-count "abc def\tghi jkl" #[\s] 4 9))
188 (test* "string-contains" 3
189 (string-contains "Ma mere l'oye" "mer"))
190 (test* "string-contains" #f
191 (string-contains "Ma mere l'oye" "Mer"))
192 (test* "string-contains-ci" 3
193 (string-contains-ci "Ma mere l'oye" "Mer"))
194 (test* "string-contains-ci" #f
195 (string-contains-ci "Ma mere l'oye" "Meer"))
196 (test* "string-contains" 15
197 (string-contains "eek -- what a geek." "ee" 12 18))
198 (test* "string-contains-ci" 15
199 (string-contains-ci "Eek -- what a geek." "EE" 12 18))
200
201 (test* "string-titlecase" "--Capitalize This Sentence."
202 (string-titlecase "--capitalize tHIS sentence."))
203 (test* "string-titlecase" "3Com Makes Routers."
204 (string-titlecase "3com makes routers."))
205 (test* "string-titlecase!" "alSo Whatever"
206 (rlet1 s (string-copy "also whatever")
207 (string-titlecase! s 2 9)))
208
209 (test* "string-upcase" "SPEAK LOUDLY"
210 (string-upcase "speak loudly"))
211 (test* "string-upcase" "PEAK"
212 (string-upcase "speak loudly" 1 5))
213 (test* "string-upcase!" "sPEAK loudly"
214 (rlet1 s (string-copy "speak loudly")
215 (string-upcase! s 1 5)))
216
217 (test* "string-downcase" "speak softly"
218 (string-downcase "SPEAK SOFTLY"))
219 (test* "string-downcase" "peak"
220 (string-downcase "SPEAK SOFTLY" 1 5))
221 (test* "string-downcase!" "speak softly"
222 (rlet1 s (string-copy "SPEAK SOFTLY")
223 (string-downcase! s)))
224 (test* "string-downcase!" "Speak softly"
225 (rlet1 s (string-copy "SPEAK SOFTLY")
226 (string-downcase! s 1)))
227 (test* "string-downcase!" "Speak SOFTLY"
228 (rlet1 s (string-copy "SPEAK SOFTLY")
229 (string-downcase! s 1 5)))
230
231 (test* "string-reverse" "nomel on nolem on"
232 (string-reverse "no melon no lemon"))
233 (test* "string-reverse" "nomel on"
234 (string-reverse "no melon no lemon" 9))
235 (test* "string-reverse" "on"
236 (string-reverse "no melon no lemon" 9 11))
237 (test* "string-reverse!" "nomel on nolem on"
238 (rlet1 s (string-copy "no melon no lemon")
239 (string-reverse! s)))
240 (test* "string-reverse!" "no melon nomel on"
241 (rlet1 s (string-copy "no melon no lemon")
242 (string-reverse! s 9)))
243 (test* "string-reverse!" "no melon on lemon"
244 (rlet1 s (string-copy "no melon no lemon")
245 (string-reverse! s 9 11)))
246
247 (test* "string-append" #f
248 (let1 s "test" (eq? s (string-append s))))
249 (test* "string-concatenate" "" (string-concatenate '()))
250 (test* "string-concatenate" #f
251 (let1 s "test" (eq? s (string-concatenate (list s)))))
252 (test* "string-concatenate" "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
253 (string-concatenate
254 '("A" "B" "C" "D" "E" "F" "G" "H"
255 "I" "J" "K" "L" "M" "N" "O" "P"
256 "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
257 "a" "b" "c" "d" "e" "f" "g" "h"
258 "i" "j" "k" "l" "m" "n" "o" "p"
259 "q" "r" "s" "t" "u" "v" "w" "x" "y" "z")))
260 (test* "string-concatenate (long list)" #t
261 (string=? (make-string 20000 #\a)
262 (string-concatenate (make-list 20000 "a"))))
263 (test* "string-concatenate/shared" "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
264 (string-concatenate/shared
265 '("A" "B" "C" "D" "E" "F" "G" "H"
266 "I" "J" "K" "L" "M" "N" "O" "P"
267 "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
268 "a" "b" "c" "d" "e" "f" "g" "h"
269 "i" "j" "k" "l" "m" "n" "o" "p"
270 "q" "r" "s" "t" "u" "v" "w" "x" "y" "z")))
271 (test* "string-concatenate-reverse" "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA"
272 (string-concatenate-reverse
273 '("A" "B" "C" "D" "E" "F" "G" "H"
274 "I" "J" "K" "L" "M" "N" "O" "P"
275 "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
276 "a" "b" "c" "d" "e" "f" "g" "h"
277 "i" "j" "k" "l" "m" "n" "o" "p"
278 "q" "r" "s" "t" "u" "v" "w" "x" "y" "z")))
279 (test* "string-concatenate-reverse" #f
280 (let1 s "test"
281 (eq? s (string-concatenate-reverse (list s)))))
282 (test* "string-concatenate-reverse (optarg)"
283 "Hello, I must be going.XXXXX"
284 (string-concatenate-reverse '(" must be" "Hello, I") " going.XXXXX"))
285 (test* "string-concatenate-reverse (optarg)"
286 "Hello, I must be going."
287 (string-concatenate-reverse '(" must be" "Hello, I") " going.XXXXX" 7))
288
289 (test* "string-concatenate-reverse/shared" "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA"
290 (string-concatenate-reverse/shared
291 '("A" "B" "C" "D" "E" "F" "G" "H"
292 "I" "J" "K" "L" "M" "N" "O" "P"
293 "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
294 "a" "b" "c" "d" "e" "f" "g" "h"
295 "i" "j" "k" "l" "m" "n" "o" "p"
296 "q" "r" "s" "t" "u" "v" "w" "x" "y" "z")))
297
298 (test* "string-map" "svool"
299 (string-map (^c (integer->char (- 219 (char->integer c))))
300 "hello"))
301 (test* "string-map" "vool"
302 (string-map (^c (integer->char (- 219 (char->integer c))))
303 "hello" 1))
304 (test* "string-map" "vo"
305 (string-map (^c (integer->char (- 219 (char->integer c))))
306 "hello" 1 3))
307 (test* "string-map!" "svool"
308 (rlet1 s (string-copy "hello")
309 (string-map! (^c (integer->char (- 219 (char->integer c))))
310 s)))
311 (test* "string-map!" "hvool"
312 (rlet1 s (string-copy "hello")
313 (string-map! (^c (integer->char (- 219 (char->integer c))))
314 s 1)))
315 (test* "string-map!" "hvolo"
316 (rlet1 s (string-copy "hello")
317 (string-map! (^c (integer->char (- 219 (char->integer c))))
318 s 1 3)))
319
320 (test* "string-fold" '(#\o #\l #\l #\e #\h . #t)
321 (string-fold cons #t "hello"))
322 (test* "string-fold" '(#\l #\e . #t)
323 (string-fold cons #t "hello" 1 3))
324 (test* "string-fold-right" '(#\h #\e #\l #\l #\o . #t)
325 (string-fold-right cons #t "hello"))
326 (test* "string-fold-right" '(#\e #\l . #t)
327 (string-fold-right cons #t "hello" 1 3))
328
329 (test* "string-unfold" "hello"
330 (string-unfold null? car cdr '(#\h #\e #\l #\l #\o)))
331 (test* "string-unfold" "hi hello"
332 (string-unfold null? car cdr '(#\h #\e #\l #\l #\o) "hi "))
333 (test* "string-unfold" "hi hello ho"
334 (string-unfold null? car cdr
335 '(#\h #\e #\l #\l #\o) "hi "
336 (^x " ho")))
337
338 (test* "string-unfold-right" "olleh"
339 (string-unfold-right null? car cdr '(#\h #\e #\l #\l #\o)))
340 (test* "string-unfold-right" "olleh hi"
341 (string-unfold-right null? car cdr '(#\h #\e #\l #\l #\o) " hi"))
342 (test* "string-unfold-right" "ho olleh hi"
343 (string-unfold-right null? car cdr
344 '(#\h #\e #\l #\l #\o) " hi"
345 (^x "ho ")))
346
347 (test* "string-for-each" "CLtL"
348 (let ([out (open-output-string)]
349 [prev #f])
350 (string-for-each (^c (when (or (not prev)
351 (char-whitespace? prev))
352 (write-char c out))
353 (set! prev c))
354 "Common Lisp, the Language")
355 (get-output-string out)))
356 (test* "string-for-each" "oLtL"
357 (let ([out (open-output-string)]
358 [prev #f])
359 (string-for-each (^c (when (or (not prev)
360 (char-whitespace? prev))
361 (write-char c out))
362 (set! prev c))
363 "Common Lisp, the Language" 1)
364 (get-output-string out)))
365 (test* "string-for-each" "oL"
366 (let ([out (open-output-string)]
367 [prev #f])
368 (string-for-each (^c (when (or (not prev)
369 (char-whitespace? prev))
370 (write-char c out))
371 (set! prev c))
372 "Common Lisp, the Language" 1 10)
373 (get-output-string out)))
374 (test* "string-for-each-index" '(4 3 2 1 0)
375 (rlet1 r '()
376 (string-for-each-index (^i (set! r (cons i r))) "hello")))
377 (test* "string-for-each-index" '(4 3 2 1)
378 (rlet1 r '()
379 (string-for-each-index (^i (set! r (cons i r))) "hello" 1)))
380 (test* "string-for-each-index" '(2 1)
381 (rlet1 r '()
382 (string-for-each-index (^i (set! r (cons i r))) "hello" 1 3)))
383
384 (test* "xsubstring" "cdefab"
385 (xsubstring "abcdef" 2))
386 (test* "xsubstring" "efabcd"
387 (xsubstring "abcdef" -2))
388 (test* "xsubstring" "abcabca"
389 (xsubstring "abc" 0 7))
390 (test* "xsubstring" "abcabca"
391 (xsubstring "abc"
392 30000000000000000000000000000000
393 30000000000000000000000000000007))
394 (test* "xsubstring" "abcde"
395 (xsubstring "abcde" -10 -5))
396 (test* "xsubstring" "eabcdea"
397 (xsubstring "abcde" -11 -4))
398 (test* "xsubstring" "eabcdeabcdeabc"
399 (xsubstring "abcde" -11 3))
400 (test* "xsubstring" "defdefd"
401 (xsubstring "abcdefg" 0 7 3 6))
402 (test* "xsubstring" ""
403 (xsubstring "abcdefg" 9 9 3 6))
404
405 (test* "string-xcopy!" "ZZcdefabZZ"
406 (rlet1 s (make-string 10 #\Z)
407 (string-xcopy! s 2 "abcdef" 2)))
408 (test* "string-xcopy!" "ZZdefdefZZ"
409 (rlet1 s (make-string 10 #\Z)
410 (string-xcopy! s 2 "abcdef" 0 6 3)))
411
412 (test* "string-replace" "abcdXYZghi"
413 (string-replace "abcdefghi" "XYZ" 4 6))
414 (test* "string-replace" "abcdZghi"
415 (string-replace "abcdefghi" "XYZ" 4 6 2))
416 (test* "string-replace" "abcdZefghi"
417 (string-replace "abcdefghi" "XYZ" 4 4 2))
418 (test* "string-replace" "abcdefghi"
419 (string-replace "abcdefghi" "XYZ" 4 4 1 1))
420 (test* "string-replace" "abcdhi"
421 (string-replace "abcdefghi" "" 4 7))
422
423 (test* "string-tokenize" '("Help" "make" "programs" "run," "run," "RUN!")
424 (string-tokenize "Help make programs run, run, RUN!"))
425 (test* "string-tokenize" '("Help" "make" "programs" "run" "run" "RUN")
426 (string-tokenize "Help make programs run, run, RUN!"
427 #[a-zA-Z]))
428 (test* "string-tokenize" '("programs" "run" "run" "RUN")
429 (string-tokenize "Help make programs run, run, RUN!"
430 #[a-zA-Z] 10))
431 (test* "string-tokenize" '("elp" "make" "programs" "run" "run")
432 (string-tokenize "Help make programs run, run, RUN!"
433 #[a-z]))
434
435 (test* "string-filter" "rrrr"
436 (string-filter #\r "Help make programs run, run, RUN!"))
437 (test* "string-filter" "HelpmakeprogramsrunrunRUN"
438 (string-filter #[a-zA-Z]
439 "Help make programs run, run, RUN!"))
440 (test* "string-filter" "programsrunrun"
441 (string-filter (^c (char-lower-case? c))
442 "Help make programs run, run, RUN!" 10))
443 (test* "string-filter" ""
444 (string-filter (^c (char-lower-case? c)) ""))
445 (test* "string-delete" "Help make pogams un, un, RUN!"
446 (string-delete #\r "Help make programs run, run, RUN!"))
447 (test* "string-delete" " , , !"
448 (string-delete #[a-zA-Z]
449 "Help make programs run, run, RUN!"))
450 (test* "string-delete" " , , RUN!"
451 (string-delete (^c (char-lower-case? c))
452 "Help make programs run, run, RUN!" 10))
453 (test* "string-delete" ""
454 (string-delete (^c (char-lower-case? c)) ""))
455 )
456
457 (test-end)