1 ;;;; strings.test --- test suite for Guile's string functions -*- scheme -*-
2 ;;;; Jim Blandy <jimb@red-bean.com> --- August 1999
4 ;;;; Copyright (C) 1999, 2001, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
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.
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.
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., 51 Franklin Street, Fifth Floor,
19 ;;;; Boston, MA 02110-1301 USA
21 (define-module (test-strings)
22 #:use-module (test-suite lib))
25 (define exception:read-only-string
26 (cons 'misc-error "^string is read-only"))
28 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
29 (define (string-ints . args)
30 (apply string (map integer->char args)))
37 (with-test-prefix "string=?"
39 (pass-if "respects 1st parameter's string length"
40 (not (string=? "foo\0" "foo")))
42 (pass-if "respects 2nd paramter's string length"
43 (not (string=? "foo" "foo\0")))
45 (with-test-prefix "wrong argument type"
47 (pass-if-exception "1st argument symbol"
48 exception:wrong-type-arg
51 (pass-if-exception "2nd argument symbol"
52 exception:wrong-type-arg
59 (with-test-prefix "string<?"
61 (pass-if "respects string length"
62 (and (not (string<? "foo\0a" "foo\0a"))
63 (string<? "foo\0a" "foo\0b")))
65 (with-test-prefix "wrong argument type"
67 (pass-if-exception "1st argument symbol"
68 exception:wrong-type-arg
71 (pass-if-exception "2nd argument symbol"
72 exception:wrong-type-arg
75 (pass-if "same as char<?"
76 (eq? (char<? (integer->char 0) (integer->char 255))
77 (string<? (string-ints 0) (string-ints 255)))))
83 (with-test-prefix "string-ci<?"
85 (pass-if "respects string length"
86 (and (not (string-ci<? "foo\0a" "foo\0a"))
87 (string-ci<? "foo\0a" "foo\0b")))
89 (with-test-prefix "wrong argument type"
91 (pass-if-exception "1st argument symbol"
92 exception:wrong-type-arg
95 (pass-if-exception "2nd argument symbol"
96 exception:wrong-type-arg
97 (string-ci<? "a" 'b)))
99 (pass-if "same as char-ci<?"
100 (eq? (char-ci<? (integer->char 0) (integer->char 255))
101 (string-ci<? (string-ints 0) (string-ints 255)))))
107 (with-test-prefix "string<=?"
109 (pass-if "same as char<=?"
110 (eq? (char<=? (integer->char 0) (integer->char 255))
111 (string<=? (string-ints 0) (string-ints 255)))))
117 (with-test-prefix "string-ci<=?"
119 (pass-if "same as char-ci<=?"
120 (eq? (char-ci<=? (integer->char 0) (integer->char 255))
121 (string-ci<=? (string-ints 0) (string-ints 255)))))
127 (with-test-prefix "string>?"
129 (pass-if "same as char>?"
130 (eq? (char>? (integer->char 0) (integer->char 255))
131 (string>? (string-ints 0) (string-ints 255)))))
137 (with-test-prefix "string-ci>?"
139 (pass-if "same as char-ci>?"
140 (eq? (char-ci>? (integer->char 0) (integer->char 255))
141 (string-ci>? (string-ints 0) (string-ints 255)))))
147 (with-test-prefix "string>=?"
149 (pass-if "same as char>=?"
150 (eq? (char>=? (integer->char 0) (integer->char 255))
151 (string>=? (string-ints 0) (string-ints 255)))))
157 (with-test-prefix "string-ci>=?"
159 (pass-if "same as char-ci>=?"
160 (eq? (char-ci>=? (integer->char 0) (integer->char 255))
161 (string-ci>=? (string-ints 0) (string-ints 255)))))
167 (with-test-prefix "string-ref"
169 (pass-if-exception "empty string"
170 exception:out-of-range
173 (pass-if-exception "empty string and non-zero index"
174 exception:out-of-range
177 (pass-if-exception "out of range"
178 exception:out-of-range
179 (string-ref "hello" 123))
181 (pass-if-exception "negative index"
182 exception:out-of-range
183 (string-ref "hello" -1))
185 (pass-if "regular string"
186 (char=? (string-ref "GNU Guile" 4) #\G)))
192 (with-test-prefix "string-set!"
194 (pass-if-exception "empty string"
195 exception:out-of-range
196 (string-set! (string-copy "") 0 #\x))
198 (pass-if-exception "empty string and non-zero index"
199 exception:out-of-range
200 (string-set! (string-copy "") 123 #\x))
202 (pass-if-exception "out of range"
203 exception:out-of-range
204 (string-set! (string-copy "hello") 123 #\x))
206 (pass-if-exception "negative index"
207 exception:out-of-range
208 (string-set! (string-copy "hello") -1 #\x))
210 (pass-if-exception "read-only string"
211 exception:read-only-string
212 (string-set! (substring/read-only "abc" 0) 1 #\space))
214 (pass-if "regular string"
215 (let ((s (string-copy "GNU guile")))
216 (string-set! s 4 #\G)
217 (char=? (string-ref s 4) #\G))))
220 (with-test-prefix "string-split"
222 ;; in guile 1.6.7 and earlier, character >=128 wasn't matched in the string
225 (string-split (string #\a (integer->char 255) #\b)
226 (integer->char 255)))))
228 (with-test-prefix "substring-move!"
230 (pass-if-exception "substring-move! checks start and end correctly"
231 exception:out-of-range
232 (substring-move! "sample" 3 0 "test" 3)))
234 (with-test-prefix "substring/shared"
236 (pass-if "modify indirectly"
237 (let ((str (string-copy "foofoofoo")))
238 (string-upcase! (substring/shared str 3 6))
239 (string=? str "fooFOOfoo")))
241 (pass-if "modify cow indirectly"
242 (let* ((str1 (string-copy "foofoofoo"))
243 (str2 (string-copy str1)))
244 (string-upcase! (substring/shared str2 3 6))
245 (and (string=? str1 "foofoofoo")
246 (string=? str2 "fooFOOfoo"))))
248 (pass-if "modify double indirectly"
249 (let* ((str1 (string-copy "foofoofoo"))
250 (str2 (substring/shared str1 2 7)))
251 (string-upcase! (substring/shared str2 1 4))
252 (string=? str1 "fooFOOfoo")))
254 (pass-if "modify cow double indirectly"
255 (let* ((str1 "foofoofoo")
256 (str2 (substring str1 2 7)))
257 (string-upcase! (substring/shared str2 1 4))
258 (and (string=? str1 "foofoofoo")
259 (string=? str2 "oFOOf")))))