Remove unused variables in system/language.
[bpt/guile.git] / test-suite / tests / strings.test
CommitLineData
9aa2c796
JB
1;;;; strings.test --- test suite for Guile's string functions -*- scheme -*-
2;;;; Jim Blandy <jimb@red-bean.com> --- August 1999
3;;;;
f5d7662f 4;;;; Copyright (C) 1999, 2001, 2004, 2005, 2006, 2008, 2009 Free Software Foundation, Inc.
9aa2c796 5;;;;
53befeb7
NJ
6;;;; This library is free software; you can redistribute it and/or
7;;;; modify it under the terms of the GNU Lesser General Public
8;;;; License as published by the Free Software Foundation; either
9;;;; version 3 of the License, or (at your option) any later version.
9aa2c796 10;;;;
53befeb7 11;;;; This library is distributed in the hope that it will be useful,
9aa2c796 12;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
53befeb7
NJ
13;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14;;;; Lesser General Public License for more details.
9aa2c796 15;;;;
53befeb7
NJ
16;;;; You should have received a copy of the GNU Lesser General Public
17;;;; License along with this library; if not, write to the Free Software
18;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
9aa2c796 19
6e7d5622
KR
20(define-module (test-strings)
21 #:use-module (test-suite lib))
22
d7e4c2da
MV
23(define exception:read-only-string
24 (cons 'misc-error "^string is read-only"))
f5d7662f
MG
25(define exception:illegal-escape
26 (cons 'read-error "illegal character in escape sequence"))
2759c092
MG
27;; Wrong types may have either the 'wrong-type-arg key when
28;; interpreted or 'vm-error when compiled. This matches both.
29(define exception:wrong-type-arg
30 (cons #t "Wrong type"))
548b9252 31
6e7d5622
KR
32;; Create a string from integer char values, eg. (string-ints 65) => "A"
33(define (string-ints . args)
34 (apply string (map integer->char args)))
35
f5d7662f
MG
36;;
37;; string internals
38;;
39
40;; Some abbreviations
41;; BMP - Basic Multilingual Plane (codepoints below U+FFFF)
42;; SMP - Suplementary Multilingual Plane (codebpoints from U+10000 to U+1FFFF)
43
44(with-test-prefix "string internals"
45
46 (pass-if "new string starts at 1st char in stringbuf"
47 (let ((s "abc"))
48 (= 0 (assq-ref (%string-dump s) 'start))))
49
50 (pass-if "length of new string same as stringbuf"
51 (let ((s "def"))
52 (= (string-length s) (assq-ref (%string-dump s) 'stringbuf-length))))
53
54 (pass-if "contents of new string same as stringbuf"
55 (let ((s "ghi"))
56 (string=? s (assq-ref (%string-dump s) 'stringbuf-chars))))
57
58 (pass-if "writable strings are not read-only"
59 (let ((s "zyx"))
60 (not (assq-ref (%string-dump s) 'read-only))))
61
62 (pass-if "read-only strings are read-only"
63 (let ((s (substring/read-only "zyx" 0)))
64 (assq-ref (%string-dump s) 'read-only)))
65
f5d7662f
MG
66 (pass-if "new Latin-1 encoded strings are not shared"
67 (let ((s "abc"))
68 (not (assq-ref (%string-dump s) 'stringbuf-shared))))
69
70 (pass-if "new UCS-4 encoded strings are not shared"
71 (let ((s "\u0100bc"))
72 (not (assq-ref (%string-dump s) 'stringbuf-shared))))
73
74 ;; Should this be true? It isn't currently true.
75 (pass-if "null shared substrings are shared"
76 (let* ((s1 "")
77 (s2 (substring/shared s1 0 0)))
78 (throw 'untested)
79 (eq? (assq-ref (%string-dump s2) 'shared)
80 s1)))
81
82 (pass-if "ASCII shared substrings are shared"
83 (let* ((s1 "foobar")
84 (s2 (substring/shared s1 0 3)))
85 (eq? (assq-ref (%string-dump s2) 'shared)
86 s1)))
87
88 (pass-if "BMP shared substrings are shared"
89 (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
90 (s2 (substring/shared s1 0 3)))
91 (eq? (assq-ref (%string-dump s2) 'shared)
92 s1)))
93
94 (pass-if "null substrings are not shared"
95 (let* ((s1 "")
96 (s2 (substring s1 0 0)))
97 (not (eq? (assq-ref (%string-dump s2) 'shared)
98 s1))))
99
100 (pass-if "ASCII substrings are not shared"
101 (let* ((s1 "foobar")
102 (s2 (substring s1 0 3)))
103 (not (eq? (assq-ref (%string-dump s2) 'shared)
104 s1))))
105
106 (pass-if "BMP substrings are not shared"
107 (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
108 (s2 (substring s1 0 3)))
109 (not (eq? (assq-ref (%string-dump s2) 'shared)
110 s1))))
111
112 (pass-if "ASCII substrings share stringbufs before copy-on-write"
113 (let* ((s1 "foobar")
114 (s2 (substring s1 0 3)))
115 (assq-ref (%string-dump s1) 'stringbuf-shared)))
116
117 (pass-if "BMP substrings share stringbufs before copy-on-write"
118 (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
119 (s2 (substring s1 0 3)))
120 (assq-ref (%string-dump s1) 'stringbuf-shared)))
121
122 (pass-if "ASCII substrings don't share stringbufs after copy-on-write"
123 (let* ((s1 "foobar")
124 (s2 (substring s1 0 3)))
125 (string-set! s2 0 #\F)
126 (not (assq-ref (%string-dump s2) 'stringbuf-shared))))
127
128 (pass-if "BMP substrings don't share stringbufs after copy-on-write"
129 (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
130 (s2 (substring s1 0 3)))
131 (string-set! s2 0 #\F)
132 (not (assq-ref (%string-dump s2) 'stringbuf-shared))))
133
134 (with-test-prefix "encodings"
135
136 (pass-if "null strings are Latin-1 encoded"
137 (let ((s ""))
138 (not (assq-ref (%string-dump s) 'stringbuf-wide))))
139
140 (pass-if "ASCII strings are Latin-1 encoded"
141 (let ((s "jkl"))
142 (not (assq-ref (%string-dump s) 'stringbuf-wide))))
143
144 (pass-if "Latin-1 strings are Latin-1 encoded"
145 (let ((s "\xC0\xC1\xC2"))
146 (not (assq-ref (%string-dump s) 'stringbuf-wide))))
147
148 (pass-if "BMP strings are UCS-4 encoded"
149 (let ((s "\u0100\u0101\x0102"))
150 (assq-ref (%string-dump s) 'stringbuf-wide)))
151
152 (pass-if "SMP strings are UCS-4 encoded"
153 (let ((s "\U010300\u010301\x010302"))
154 (assq-ref (%string-dump s) 'stringbuf-wide)))
155
156 (pass-if "null list->string is Latin-1 encoded"
157 (let ((s (string-ints)))
158 (not (assq-ref (%string-dump s) 'stringbuf-wide))))
159
160 (pass-if "ASCII list->string is Latin-1 encoded"
161 (let ((s (string-ints 65 66 67)))
162 (not (assq-ref (%string-dump s) 'stringbuf-wide))))
163
164 (pass-if "Latin-1 list->string is Latin-1 encoded"
165 (let ((s (string-ints #xc0 #xc1 #xc2)))
166 (not (assq-ref (%string-dump s) 'stringbuf-wide))))
167
168 (pass-if "BMP list->string is UCS-4 encoded"
169 (let ((s (string-ints #x0100 #x0101 #x0102)))
170 (assq-ref (%string-dump s) 'stringbuf-wide)))
171
172 (pass-if "SMP list->string is UCS-4 encoded"
173 (let ((s (string-ints #x010300 #x010301 #x010302)))
174 (assq-ref (%string-dump s) 'stringbuf-wide)))
175
176 (pass-if "encoding of string not based on escape style"
177 (let ((s "\U000040"))
178 (not (assq-ref (%string-dump s) 'stringbuf-wide))))))
179
180(with-test-prefix "hex escapes"
181
182 (pass-if-exception "non-hex char in two-digit hex-escape"
183 exception:illegal-escape
184 (with-input-from-string "\"\\x0g\"" read))
185
186 (pass-if-exception "non-hex char in four-digit hex-escape"
187 exception:illegal-escape
188 (with-input-from-string "\"\\u000g\"" read))
189
190 (pass-if-exception "non-hex char in six-digit hex-escape"
191 exception:illegal-escape
192 (with-input-from-string "\"\\U00000g\"" read))
193
194 (pass-if-exception "premature termination of two-digit hex-escape"
195 exception:illegal-escape
196 (with-input-from-string "\"\\x0\"" read))
197
198 (pass-if-exception "premature termination of four-digit hex-escape"
199 exception:illegal-escape
200 (with-input-from-string "\"\\u000\"" read))
201
202 (pass-if-exception "premature termination of six-digit hex-escape"
203 exception:illegal-escape
204 (with-input-from-string "\"\\U00000\"" read))
205
206 (pass-if "extra hex digits ignored for two-digit hex escape"
207 (eqv? (string-ref "--\xfff--" 2)
208 (integer->char #xff)))
209
210 (pass-if "extra hex digits ignored for four-digit hex escape"
211 (eqv? (string-ref "--\u0100f--" 2)
212 (integer->char #x0100)))
213
214 (pass-if "extra hex digits ignored for six-digit hex escape"
215 (eqv? (string-ref "--\U010300f--" 2)
216 (integer->char #x010300)))
217
218 (pass-if "escaped characters match non-escaped ASCII characters"
219 (string=? "ABC" "\x41\u0042\U000043")))
6e7d5622
KR
220
221;;
222;; string=?
223;;
224
049fa449
DH
225(with-test-prefix "string=?"
226
227 (pass-if "respects 1st parameter's string length"
228 (not (string=? "foo\0" "foo")))
229
230 (pass-if "respects 2nd paramter's string length"
231 (not (string=? "foo" "foo\0")))
232
233 (with-test-prefix "wrong argument type"
234
235 (pass-if-exception "1st argument symbol"
236 exception:wrong-type-arg
237 (string=? 'a "a"))
238
239 (pass-if-exception "2nd argument symbol"
240 exception:wrong-type-arg
241 (string=? "a" 'b))))
242
6e7d5622
KR
243;;
244;; string<?
245;;
246
049fa449
DH
247(with-test-prefix "string<?"
248
249 (pass-if "respects string length"
250 (and (not (string<? "foo\0a" "foo\0a"))
251 (string<? "foo\0a" "foo\0b")))
252
253 (with-test-prefix "wrong argument type"
254
255 (pass-if-exception "1st argument symbol"
256 exception:wrong-type-arg
257 (string<? 'a "a"))
258
259 (pass-if-exception "2nd argument symbol"
260 exception:wrong-type-arg
6e7d5622
KR
261 (string<? "a" 'b)))
262
263 (pass-if "same as char<?"
264 (eq? (char<? (integer->char 0) (integer->char 255))
265 (string<? (string-ints 0) (string-ints 255)))))
266
267;;
268;; string-ci<?
269;;
049fa449
DH
270
271(with-test-prefix "string-ci<?"
272
273 (pass-if "respects string length"
274 (and (not (string-ci<? "foo\0a" "foo\0a"))
275 (string-ci<? "foo\0a" "foo\0b")))
276
277 (with-test-prefix "wrong argument type"
278
279 (pass-if-exception "1st argument symbol"
280 exception:wrong-type-arg
281 (string-ci<? 'a "a"))
282
283 (pass-if-exception "2nd argument symbol"
284 exception:wrong-type-arg
6e7d5622
KR
285 (string-ci<? "a" 'b)))
286
287 (pass-if "same as char-ci<?"
288 (eq? (char-ci<? (integer->char 0) (integer->char 255))
289 (string-ci<? (string-ints 0) (string-ints 255)))))
290
291;;
292;; string<=?
293;;
294
295(with-test-prefix "string<=?"
296
297 (pass-if "same as char<=?"
298 (eq? (char<=? (integer->char 0) (integer->char 255))
299 (string<=? (string-ints 0) (string-ints 255)))))
300
301;;
302;; string-ci<=?
303;;
304
305(with-test-prefix "string-ci<=?"
306
307 (pass-if "same as char-ci<=?"
308 (eq? (char-ci<=? (integer->char 0) (integer->char 255))
309 (string-ci<=? (string-ints 0) (string-ints 255)))))
310
311;;
312;; string>?
313;;
314
315(with-test-prefix "string>?"
316
317 (pass-if "same as char>?"
318 (eq? (char>? (integer->char 0) (integer->char 255))
319 (string>? (string-ints 0) (string-ints 255)))))
320
321;;
322;; string-ci>?
323;;
324
325(with-test-prefix "string-ci>?"
326
327 (pass-if "same as char-ci>?"
328 (eq? (char-ci>? (integer->char 0) (integer->char 255))
329 (string-ci>? (string-ints 0) (string-ints 255)))))
330
331;;
332;; string>=?
333;;
334
335(with-test-prefix "string>=?"
336
337 (pass-if "same as char>=?"
338 (eq? (char>=? (integer->char 0) (integer->char 255))
339 (string>=? (string-ints 0) (string-ints 255)))))
340
341;;
342;; string-ci>=?
343;;
344
345(with-test-prefix "string-ci>=?"
346
347 (pass-if "same as char-ci>=?"
348 (eq? (char-ci>=? (integer->char 0) (integer->char 255))
349 (string-ci>=? (string-ints 0) (string-ints 255)))))
350
3ae3166b
LC
351;;
352;; string-ref
353;;
354
355(with-test-prefix "string-ref"
356
357 (pass-if-exception "empty string"
358 exception:out-of-range
359 (string-ref "" 0))
360
361 (pass-if-exception "empty string and non-zero index"
362 exception:out-of-range
363 (string-ref "" 123))
364
365 (pass-if-exception "out of range"
366 exception:out-of-range
367 (string-ref "hello" 123))
368
369 (pass-if-exception "negative index"
370 exception:out-of-range
371 (string-ref "hello" -1))
372
f5d7662f
MG
373 (pass-if "regular string, ASCII char"
374 (char=? (string-ref "GNU Guile" 4) #\G))
375
376 (pass-if "regular string, hex escaped Latin-1 char"
377 (char=? (string-ref "--\xff--" 2)
378 (integer->char #xff)))
379
380 (pass-if "regular string, hex escaped BMP char"
381 (char=? (string-ref "--\u0100--" 2)
382 (integer->char #x0100)))
383
384 (pass-if "regular string, hex escaped SMP char"
385 (char=? (string-ref "--\U010300--" 2)
386 (integer->char #x010300))))
3ae3166b 387
6e7d5622
KR
388;;
389;; string-set!
390;;
049fa449
DH
391
392(with-test-prefix "string-set!"
393
3ae3166b
LC
394 (pass-if-exception "empty string"
395 exception:out-of-range
396 (string-set! (string-copy "") 0 #\x))
397
398 (pass-if-exception "empty string and non-zero index"
399 exception:out-of-range
400 (string-set! (string-copy "") 123 #\x))
401
402 (pass-if-exception "out of range"
403 exception:out-of-range
404 (string-set! (string-copy "hello") 123 #\x))
405
406 (pass-if-exception "negative index"
407 exception:out-of-range
408 (string-set! (string-copy "hello") -1 #\x))
409
b144a33c 410 (pass-if-exception "read-only string"
d7e4c2da 411 exception:read-only-string
3ae3166b
LC
412 (string-set! (substring/read-only "abc" 0) 1 #\space))
413
f5d7662f 414 (pass-if "regular string, ASCII char"
3ae3166b
LC
415 (let ((s (string-copy "GNU guile")))
416 (string-set! s 4 #\G)
f5d7662f
MG
417 (char=? (string-ref s 4) #\G)))
418
419 (pass-if "regular string, Latin-1 char"
420 (let ((s (string-copy "GNU guile")))
421 (string-set! s 4 (integer->char #xfe))
422 (char=? (string-ref s 4) (integer->char #xfe))))
423
424 (pass-if "regular string, BMP char"
425 (let ((s (string-copy "GNU guile")))
426 (string-set! s 4 (integer->char #x0100))
427 (char=? (string-ref s 4) (integer->char #x0100))))
428
429 (pass-if "regular string, SMP char"
430 (let ((s (string-copy "GNU guile")))
431 (string-set! s 4 (integer->char #x010300))
432 (char=? (string-ref s 4) (integer->char #x010300)))))
3ae3166b 433
3c7cf7f5
MG
434;;
435;; list->string
436;;
437(with-test-prefix "string"
438
439 (pass-if-exception "convert circular list to string"
440 exception:wrong-type-arg
441 (let ((foo (list #\a #\b #\c)))
442 (set-cdr! (cddr foo) (cdr foo))
443 (apply string foo))))
444
50e20a60
KR
445(with-test-prefix "string-split"
446
447 ;; in guile 1.6.7 and earlier, character >=128 wasn't matched in the string
448 (pass-if "char 255"
449 (equal? '("a" "b")
450 (string-split (string #\a (integer->char 255) #\b)
451 (integer->char 255)))))
452
049fa449
DH
453(with-test-prefix "substring-move!"
454
455 (pass-if-exception "substring-move! checks start and end correctly"
456 exception:out-of-range
457 (substring-move! "sample" 3 0 "test" 3)))
1c17f6b0
MV
458
459(with-test-prefix "substring/shared"
460
461 (pass-if "modify indirectly"
462 (let ((str (string-copy "foofoofoo")))
463 (string-upcase! (substring/shared str 3 6))
464 (string=? str "fooFOOfoo")))
465
466 (pass-if "modify cow indirectly"
467 (let* ((str1 (string-copy "foofoofoo"))
468 (str2 (string-copy str1)))
469 (string-upcase! (substring/shared str2 3 6))
470 (and (string=? str1 "foofoofoo")
7aa29a87
MV
471 (string=? str2 "fooFOOfoo"))))
472
473 (pass-if "modify double indirectly"
d7e4c2da 474 (let* ((str1 (string-copy "foofoofoo"))
7aa29a87
MV
475 (str2 (substring/shared str1 2 7)))
476 (string-upcase! (substring/shared str2 1 4))
477 (string=? str1 "fooFOOfoo")))
478
479 (pass-if "modify cow double indirectly"
480 (let* ((str1 "foofoofoo")
481 (str2 (substring str1 2 7)))
482 (string-upcase! (substring/shared str2 1 4))
483 (and (string=? str1 "foofoofoo")
484 (string=? str2 "oFOOf")))))