| 1 |
;; |
| 2 |
;; test for string related functions |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
|
| 7 |
(test-start "strings (basic)") |
| 8 |
|
| 9 |
;;------------------------------------------------------------------- |
| 10 |
(test-section "builtins") |
| 11 |
|
| 12 |
(test* "string" "abcdefg" (string #\a #\b #\c #\d #\e #\f #\g)) |
| 13 |
(test* "string" "" (string)) |
| 14 |
(test* "list->string" "abcdefg" |
| 15 |
(list->string '(#\a #\b #\c #\d #\e #\f #\g))) |
| 16 |
(test* "list->string" "" (list->string '())) |
| 17 |
(test* "make-string" "aaaaa" (make-string 5 #\a)) |
| 18 |
(test* "make-string" "" (make-string 0 #\a)) |
| 19 |
|
| 20 |
(test* "immutable" #t (string-immutable? "abcde")) |
| 21 |
(test* "immutable" #t (string-immutable? "")) |
| 22 |
(test* "immutable" #f (string-immutable? (string-copy "abcde"))) |
| 23 |
(test* "immutable" #f (string-immutable? (string #\a #\b))) |
| 24 |
(test* "immutable" #f (string-immutable? (string))) |
| 25 |
|
| 26 |
(let ([data '(("abc" "abc" =) |
| 27 |
("" "abc" <) |
| 28 |
("abd" "abc" >) |
| 29 |
(#**"abc" #**"abc" =) |
| 30 |
(#**"a" #**"abc" <) |
| 31 |
(#**"abd" #**"abc" >) |
| 32 |
("abc" #**"abc" <) |
| 33 |
("abc" #**"abb" >) |
| 34 |
(#**"abc" "abc" >) |
| 35 |
(#**"ab" "abc" <))]) |
| 36 |
(define (t a b result) |
| 37 |
(test* (string-append "compare " a " and " b) |
| 38 |
(list (boolean (memq result '(=))) |
| 39 |
(boolean (memq result '(<))) |
| 40 |
(boolean (memq result '(< =))) |
| 41 |
(boolean (memq result '(>))) |
| 42 |
(boolean (memq result '(> =)))) |
| 43 |
(list (string=? a b) |
| 44 |
(string<? a b) |
| 45 |
(string<=? a b) |
| 46 |
(string>? a b) |
| 47 |
(string>=? a b)))) |
| 48 |
(dolist [x data] |
| 49 |
(apply t x))) |
| 50 |
|
| 51 |
(test* "string->list" '(#\a #\b #\c #\d #\e #\f #\g) |
| 52 |
(string->list "abcdefg")) |
| 53 |
(test* "string->list" '(#\c #\d #\e #\f #\g) |
| 54 |
(string->list "abcdefg" 2)) ;SRFI-13 extension |
| 55 |
(test* "string->list" '(#\c #\d #\e) |
| 56 |
(string->list "abcdefg" 2 5)) ;SRFI-13 extension |
| 57 |
(test* "string->list" '(#\a) |
| 58 |
(string->list "abcdefg" 0 1)) ;SRFI-13 extension |
| 59 |
(test* "string->list" '() (string->list "")) |
| 60 |
|
| 61 |
(test* "string-copy" '("abcde" #f) |
| 62 |
(let* ((x "abcde") (y (string-copy x))) |
| 63 |
(list y (eq? x y)))) |
| 64 |
(test* "string-copy" "cde" (string-copy "abcde" 2)) |
| 65 |
(test* "string-copy" "cd" (string-copy "abcde" 2 4)) |
| 66 |
|
| 67 |
(test* "string-ref" #\b (string-ref "abc" 1)) |
| 68 |
(define x (string-copy "abcde")) |
| 69 |
(test* "string-set!" "abZde" (begin (string-set! x 2 #\Z) x)) |
| 70 |
|
| 71 |
(test* "string w/ char >= \\x80" #\u00a1 |
| 72 |
(string-ref (string #\u00a1) 0)) |
| 73 |
|
| 74 |
(test* "string-reader hex-escape" '(1 2 3) |
| 75 |
(let1 s "\x1;\x2;\x00003;" |
| 76 |
(map (^i (char->integer (string-ref s i))) '(0 1 2)))) |
| 77 |
(test* "string-reader hex-escape legacy support" '(1 2 0 48 48 51) |
| 78 |
(let1 s "\x01\x02\x00003" |
| 79 |
(map (^i (char->integer (string-ref s i))) '(0 1 2 3 4 5)))) |
| 80 |
(test* "string-reader hex-escape error cases 1 (no hexdigits)" |
| 81 |
(test-error) |
| 82 |
(read-from-string "\"\\x;\"")) |
| 83 |
(test* "string-reader hex-escape error cases 2 (integer overflow)" |
| 84 |
(test-error) |
| 85 |
(read-from-string "\"\\x11111111111111111111111111111111;\"")) |
| 86 |
(test* "string-reader hex-escape error cases 3 (out of unicode range)" |
| 87 |
(test-error) |
| 88 |
(read-from-string "\"\\x111111;\"")) |
| 89 |
(test* "string-reader hex-escape legacy fallback" "\x11;1111" |
| 90 |
(read-from-string "\"\\x111111\"")) |
| 91 |
|
| 92 |
|
| 93 |
(test* "string-fill!" "ZZZZZZ" |
| 94 |
(string-fill! (string-copy "000000") #\Z)) |
| 95 |
(test* "string-fill!" "000ZZZ" |
| 96 |
(string-fill! (string-copy "000000") #\Z 3)) |
| 97 |
(test* "string-fill!" "000ZZ0" |
| 98 |
(string-fill! (string-copy "000000") #\Z 3 5)) |
| 99 |
|
| 100 |
(test* "string-join" "foo bar baz" |
| 101 |
(string-join '("foo" "bar" "baz"))) |
| 102 |
(test* "string-join" "foo::bar::baz" |
| 103 |
(string-join '("foo" "bar" "baz") "::")) |
| 104 |
(test* "string-join" "foo::bar::baz" |
| 105 |
(string-join '("foo" "bar" "baz") "::" 'infix)) |
| 106 |
(test* "string-join" "" |
| 107 |
(string-join '() "::")) |
| 108 |
(test* "string-join" "foo::bar::baz::" |
| 109 |
(string-join '("foo" "bar" "baz") "::" 'suffix)) |
| 110 |
(test* "string-join" "" |
| 111 |
(string-join '() "::" 'suffix)) |
| 112 |
(test* "string-join" "::foo::bar::baz" |
| 113 |
(string-join '("foo" "bar" "baz") "::" 'prefix)) |
| 114 |
(test* "string-join" "" |
| 115 |
(string-join '() "::" 'prefix)) |
| 116 |
(test* "string-join" "foo::bar::baz" |
| 117 |
(string-join '("foo" "bar" "baz") "::" 'strict-infix)) |
| 118 |
|
| 119 |
(let () |
| 120 |
(define (test-string-scan out s1 s2 . opt) |
| 121 |
(if (pair? out) |
| 122 |
(begin |
| 123 |
(test* "string-scan" (car out) (apply string-scan s1 s2 opt)) |
| 124 |
(test* "string-scan-right" (cadr out) |
| 125 |
(apply string-scan-right s1 s2 opt))) |
| 126 |
(apply test-string-scan (list out out) s1 s2 opt))) |
| 127 |
(define (test-string-scan2 out1 out2 s1 s2 . opt) |
| 128 |
(if (pair? out1) |
| 129 |
(begin |
| 130 |
(test* "string-scan" (list (car out1) (car out2)) |
| 131 |
(receive r (apply string-scan s1 s2 opt) r)) |
| 132 |
(test* "string-scan-right" (list (cadr out1) (cadr out2)) |
| 133 |
(receive r (apply string-scan-right s1 s2 opt) r))) |
| 134 |
(apply test-string-scan2 (list out1 out1) (list out2 out2) s1 s2 opt))) |
| 135 |
|
| 136 |
(test-string-scan 3 "abcdefghi" "def") |
| 137 |
(test-string-scan 3 "abcdefghi" "def" 'index) |
| 138 |
(test-string-scan 6 "abcdefghi" "ghi") |
| 139 |
(test-string-scan 0 "abcdefghi" "abc") |
| 140 |
(test-string-scan '(0 6) "abcabcabc" "abc") |
| 141 |
(test-string-scan "abc" "abcdefghi" "def" 'before) |
| 142 |
(test-string-scan "ghi" "abcdefghi" "def" 'after) |
| 143 |
(test-string-scan2 "abc" "defghi" "abcdefghi" "def" 'before*) |
| 144 |
(test-string-scan2 "abcdef" "ghi" "abcdefghi" "def" 'after*) |
| 145 |
(test-string-scan2 "abc" "ghi" "abcdefghi" "def" 'both) |
| 146 |
|
| 147 |
(test-string-scan 4 "abcdefghi" #\e) |
| 148 |
(test-string-scan 8 "abcdefghi" #\i) |
| 149 |
(test-string-scan 0 "abcdefghi" #\a) |
| 150 |
(test-string-scan '(0 8) "abcdefgha" #\a) |
| 151 |
(test-string-scan "abcd" "abcdefghi" #\e 'before) |
| 152 |
(test-string-scan "fghi" "abcdefghi" #\e 'after) |
| 153 |
(test-string-scan2 "abcd" "efghi" "abcdefghi" #\e 'before*) |
| 154 |
(test-string-scan2 "abcde" "fghi" "abcdefghi" #\e 'after*) |
| 155 |
(test-string-scan2 "abcd" "fghi" "abcdefghi" #\e 'both) |
| 156 |
|
| 157 |
;; this tests boyer-moore |
| 158 |
(test-string-scan 216 |
| 159 |
"abracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabr" |
| 160 |
"abracadabra") |
| 161 |
(test-string-scan '(0 365) |
| 162 |
"axaxcadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababrabrabrabracadababaxaxax" |
| 163 |
"axax") |
| 164 |
|
| 165 |
;; results differ between leftmost and rightmost |
| 166 |
(test-string-scan '(1 8) "abracadabra" "br") |
| 167 |
(test-string-scan '("acadabra" "a") "abracadabra" "br" 'after) |
| 168 |
(test-string-scan '("a" "abracada") "abracadabra" "br" 'before) |
| 169 |
(test-string-scan2 '("abr" "abracadabr") '("acadabra" "a") |
| 170 |
"abracadabra" "br" 'after*) |
| 171 |
(test-string-scan2 '("a" "abracada") '("bracadabra" "bra") |
| 172 |
"abracadabra" "br" 'before*) |
| 173 |
(test-string-scan2 '("a" "abracada") '("acadabra" "a") |
| 174 |
"abracadabra" "br" 'both) |
| 175 |
|
| 176 |
;; edge case |
| 177 |
(test-string-scan '(0 11) "abakjrgaker" "") |
| 178 |
(test-string-scan '("abakjrgaker" "") "abakjrgaker" "" 'after) |
| 179 |
(test-string-scan '("" "abakjrgaker") "abakjrgaker" "" 'before) |
| 180 |
(test-string-scan2 '("" "abakjrgaker") '("abakjrgaker" "") |
| 181 |
"abakjrgaker" "" 'after*) |
| 182 |
(test-string-scan2 '("" "abakjrgaker") '("abakjrgaker" "") |
| 183 |
"abakjrgaker" "" 'before*) |
| 184 |
(test-string-scan2 '("" "abakjrgaker") '("abakjrgaker" "") |
| 185 |
"abakjrgaker" "" 'both) |
| 186 |
|
| 187 |
;; incomplete strings |
| 188 |
(test-string-scan 3 #**"abcdefghi" "def") |
| 189 |
(test-string-scan 3 "abcdefghi" #**"def") |
| 190 |
(test-string-scan #**"ghi" #**"abcdefghi" "def" 'after) |
| 191 |
(test-string-scan #**"abc" #**"abcdefghi" "def" 'before) |
| 192 |
(test-string-scan2 #**"abcdef" #**"ghi" #**"abcdefghi" "def" 'after*) |
| 193 |
(test-string-scan2 #**"abc" #**"defghi" #**"abcdefghi" "def" 'before*) |
| 194 |
(test-string-scan2 #**"abc" #**"ghi" #**"abcdefghi" "def" 'both) |
| 195 |
(test-string-scan2 #**"abc" #**"ghi" "abcdefghi" #**"def" 'both) |
| 196 |
(test-string-scan2 #**"abcd" #**"fghi" #**"abcdefghi" #\e 'both) |
| 197 |
) |
| 198 |
|
| 199 |
;;------------------------------------------------------------------- |
| 200 |
(test-section "string-split") |
| 201 |
|
| 202 |
(test* "string-split (char)" '("aa" "bbb" "c") |
| 203 |
(string-split "aa*bbb*c" #\*)) |
| 204 |
(test* "string-split (char)" '("aa" "bbb" "c" "") |
| 205 |
(string-split "aa*bbb*c*" #\*)) |
| 206 |
(test* "string-split (char)" '("aa" "bbb" "c" "" "") |
| 207 |
(string-split "aa*bbb*c**" #\*)) |
| 208 |
(test* "string-split (char)" '("aa") |
| 209 |
(string-split "aa" #\*)) |
| 210 |
(test* "string-split (char)" '() |
| 211 |
(string-split "" #\*)) |
| 212 |
(test* "string-split (char)" '("" "") |
| 213 |
(string-split "*" #\*)) |
| 214 |
|
| 215 |
(test* "string-split (1-char string)" '("aa" "bbb" "c") |
| 216 |
(string-split "aa*bbb*c" "*")) |
| 217 |
|
| 218 |
(test* "string-split (string)" '("aa" "bbb" "c*c") |
| 219 |
(string-split "aa**bbb**c*c" "**")) |
| 220 |
(test* "string-split (string)" '("aa**bbb**c*c") |
| 221 |
(string-split "aa**bbb**c*c" "--")) |
| 222 |
(test* "string-split (string)" '("aa" "bbb" "c*c" "") |
| 223 |
(string-split "aa**bbb**c*c**" "**")) |
| 224 |
(test* "string-split (string)" '() |
| 225 |
(string-split "" "**")) |
| 226 |
(test* "string-split (string)" '("" "") |
| 227 |
(string-split "**" "**")) |
| 228 |
|
| 229 |
(test* "string-split (regexp)" '("aa" "bbb" "c" "c") |
| 230 |
(string-split "aa--bbb--c-c" #/-+/)) |
| 231 |
(test* "string-split (regexp)" '("aa" "bbb" "-c-c") |
| 232 |
(string-split "aa--bbb---c-c" #/--/)) |
| 233 |
(test* "string-split (regexp)" '("" "aa" "bbb" "c" "c" "") |
| 234 |
(string-split "--aa--bbb---c-c-" #/-+/)) |
| 235 |
(test* "string-split (regexp)" '("--" "--" "---" "-" "-") |
| 236 |
(string-split "--aa--bbb---c-c-" #/\w+/)) |
| 237 |
(test* "string-split (regexp)" '("--aa--bbb---c-c-") |
| 238 |
(string-split "--aa--bbb---c-c-" #/z+/)) |
| 239 |
(test* "string-split (regexp)" '("" "a" "a" "" "b" "b" "b" "" "c" "" "c" "" "") |
| 240 |
(string-split "--aa--bbb---c-c-" #/-*/)) |
| 241 |
|
| 242 |
(test* "string-split (charset)" '("aa" "bbb" "c" "d") |
| 243 |
(string-split "aa---bbb***c&d" #[\W])) |
| 244 |
(test* "string-split (charset)" '("" "---" "***" "&" "") |
| 245 |
(string-split "aa---bbb***c&d" #[\w])) |
| 246 |
(test* "string-split (charset)" '() |
| 247 |
(string-split "" #[\w])) |
| 248 |
(test* "string-split (charset)" '("" "") |
| 249 |
(string-split "a" #[\w])) |
| 250 |
|
| 251 |
(test* "string-split (predicate)" '("" "---" "***" "&" "") |
| 252 |
(string-split "aa---bbb***c&d" char-alphabetic?)) |
| 253 |
|
| 254 |
(test-section "string-split(with limit)") |
| 255 |
|
| 256 |
(test* "string-split (char)" '("aa*bbb*c**") |
| 257 |
(string-split "aa*bbb*c**" #\* 0)) |
| 258 |
(test* "string-split (char)" '("aa" "bbb*c**") |
| 259 |
(string-split "aa*bbb*c**" #\* 1)) |
| 260 |
(test* "string-split (char)" '("aa" "bbb" "c**") |
| 261 |
(string-split "aa*bbb*c**" #\* 2)) |
| 262 |
|
| 263 |
(test* "string-split (char)" '("aa") |
| 264 |
(string-split "aa" #\* 1)) |
| 265 |
(test* "string-split (char)" '() |
| 266 |
(string-split "" #\* 2)) |
| 267 |
(test* "string-split (char)" '("" "") |
| 268 |
(string-split "*" #\* 3)) |
| 269 |
(test* "string-split (char)" '("*") |
| 270 |
(string-split "*" #\* 0)) |
| 271 |
|
| 272 |
(test* "string-split (1-char string)" '("aa" "bbb*c") |
| 273 |
(string-split "aa*bbb*c" "*" 1)) |
| 274 |
(test* "string-split (1-char string)" '("aa" "bbb" "c") |
| 275 |
(string-split "aa*bbb*c" "*" 2)) |
| 276 |
|
| 277 |
(test* "string-split (string)" '("aa**bbb**c*c") |
| 278 |
(string-split "aa**bbb**c*c" "**" 0)) |
| 279 |
(test* "string-split (string)" '("aa" "bbb**c*c") |
| 280 |
(string-split "aa**bbb**c*c" "**" 1)) |
| 281 |
(test* "string-split (string)" '("aa" "bbb" "c*c") |
| 282 |
(string-split "aa**bbb**c*c" "**" 2)) |
| 283 |
(test* "string-split (string)" '("aa" "bbb" "c*c") |
| 284 |
(string-split "aa**bbb**c*c" "**" 3)) |
| 285 |
(test* "string-split (string)" '("aa**bbb**c*c") |
| 286 |
(string-split "aa**bbb**c*c" "--" 2)) |
| 287 |
(test* "string-split (string)" '("aa**bbb**c*c**") |
| 288 |
(string-split "aa**bbb**c*c**" "**" 0)) |
| 289 |
(test* "string-split (string)" '("aa" "bbb**c*c**") |
| 290 |
(string-split "aa**bbb**c*c**" "**" 1)) |
| 291 |
(test* "string-split (string)" '("aa" "bbb" "c*c**") |
| 292 |
(string-split "aa**bbb**c*c**" "**" 2)) |
| 293 |
(test* "string-split (string)" '("aa" "bbb" "c*c" "") |
| 294 |
(string-split "aa**bbb**c*c**" "**" 3)) |
| 295 |
(test* "string-split (string)" '() |
| 296 |
(string-split "" "**" 0)) |
| 297 |
(test* "string-split (string)" '() |
| 298 |
(string-split "" "**" 1)) |
| 299 |
(test* "string-split (string)" '("**") |
| 300 |
(string-split "**" "**" 0)) |
| 301 |
(test* "string-split (string)" '("" "") |
| 302 |
(string-split "**" "**" 1)) |
| 303 |
|
| 304 |
(test* "string-split (regexp)" '("aa--bbb--c-c") |
| 305 |
(string-split "aa--bbb--c-c" #/-+/ 0)) |
| 306 |
(test* "string-split (regexp)" '("aa" "bbb--c-c") |
| 307 |
(string-split "aa--bbb--c-c" #/-+/ 1)) |
| 308 |
(test* "string-split (regexp)" '("aa" "bbb" "c-c") |
| 309 |
(string-split "aa--bbb--c-c" #/-+/ 2)) |
| 310 |
(test* "string-split (regexp)" '("aa" "bbb" "c" "c") |
| 311 |
(string-split "aa--bbb--c-c" #/-+/ 3)) |
| 312 |
(test* "string-split (regexp)" '("aa" "bbb" "c" "c") |
| 313 |
(string-split "aa--bbb--c-c" #/-+/ 4)) |
| 314 |
(test* "string-split (regexp)" '("aa--bbb---c-c") |
| 315 |
(string-split "aa--bbb---c-c" #/--/ 0)) |
| 316 |
(test* "string-split (regexp)" '("aa" "bbb---c-c") |
| 317 |
(string-split "aa--bbb---c-c" #/--/ 1)) |
| 318 |
(test* "string-split (regexp)" '("aa" "bbb" "-c-c") |
| 319 |
(string-split "aa--bbb---c-c" #/--/ 2)) |
| 320 |
(test* "string-split (regexp)" '("aa" "bbb" "-c-c") |
| 321 |
(string-split "aa--bbb---c-c" #/--/ 3)) |
| 322 |
(test* "string-split (regexp)" '("--aa--bbb---c-c-") |
| 323 |
(string-split "--aa--bbb---c-c-" #/-+/ 0)) |
| 324 |
(test* "string-split (regexp)" '("" "aa--bbb---c-c-") |
| 325 |
(string-split "--aa--bbb---c-c-" #/-+/ 1)) |
| 326 |
(test* "string-split (regexp)" '("" "aa" "bbb---c-c-") |
| 327 |
(string-split "--aa--bbb---c-c-" #/-+/ 2)) |
| 328 |
(test* "string-split (regexp)" '("" "aa" "bbb" "c-c-") |
| 329 |
(string-split "--aa--bbb---c-c-" #/-+/ 3)) |
| 330 |
(test* "string-split (regexp)" '("" "aa" "bbb" "c" "c-") |
| 331 |
(string-split "--aa--bbb---c-c-" #/-+/ 4)) |
| 332 |
(test* "string-split (regexp)" '("--aa--bbb---c-c-") |
| 333 |
(string-split "--aa--bbb---c-c-" #/\w+/ 0)) |
| 334 |
(test* "string-split (regexp)" '("--" "--bbb---c-c-") |
| 335 |
(string-split "--aa--bbb---c-c-" #/\w+/ 1)) |
| 336 |
(test* "string-split (regexp)" '("--" "--" "---c-c-") |
| 337 |
(string-split "--aa--bbb---c-c-" #/\w+/ 2)) |
| 338 |
(test* "string-split (regexp)" '("--" "--" "---" "-c-") |
| 339 |
(string-split "--aa--bbb---c-c-" #/\w+/ 3)) |
| 340 |
(test* "string-split (regexp)" '("--aa--bbb---c-c-") |
| 341 |
(string-split "--aa--bbb---c-c-" #/z+/ 2)) |
| 342 |
(test* "string-split (regexp)" '("" "a" "a" "--bbb---c-c-") |
| 343 |
(string-split "--aa--bbb---c-c-" #/-*/ 3)) |
| 344 |
(test* "string-split (bad limit)" (test-error) |
| 345 |
(string-split "--aa--bbb---c-c-" #/-+/ 'a)) |
| 346 |
|
| 347 |
;;------------------------------------------------------------------- |
| 348 |
(test-section "incomplete strings") |
| 349 |
|
| 350 |
;; Real test for incomplete string requires multibyte strings. |
| 351 |
;; Here I only check consistency of combination between complete |
| 352 |
;; and incomplete strings. |
| 353 |
|
| 354 |
(test* "string-incomplete?" #f (string-incomplete? "abc")) |
| 355 |
(test* "string-incomplete?" #t (string-incomplete? #**"abc")) |
| 356 |
(test* "string-incomplete?" #f (string-incomplete? "")) |
| 357 |
(test* "string-incomplete?" #t (string-incomplete? #**"")) |
| 358 |
|
| 359 |
(test* "string-complete->incomplete" #**"xyz" |
| 360 |
(string-complete->incomplete "xyz")) |
| 361 |
(test* "string-complete->incomplete" #**"xyz" |
| 362 |
(string-complete->incomplete #**"xyz")) |
| 363 |
(test* "string-incomplete->complete" "xyz" |
| 364 |
(string-incomplete->complete #**"xyz")) |
| 365 |
(test* "string-incomplete->complete" "xyz" |
| 366 |
(string-incomplete->complete "xyz")) |
| 367 |
|
| 368 |
(test* "string=?" #t (string=? #**"abc" #**"abc")) |
| 369 |
|
| 370 |
(test* "string-byte-ref" (char->integer #\b) |
| 371 |
(string-byte-ref #**"abc" 1)) |
| 372 |
(test* "string-byte-ref" 0 |
| 373 |
(string-byte-ref #**"\0\0\0" 1)) |
| 374 |
|
| 375 |
(test* "string-append" #**"abcdef" |
| 376 |
(string-append "abc" #**"def")) |
| 377 |
(test* "string-append" #**"abcdef" |
| 378 |
(string-append #**"abc" "def")) |
| 379 |
(test* "string-append" #**"abcdef" |
| 380 |
(string-append #**"abc" #**"def")) |
| 381 |
(test* "string-append" #**"abcdef" |
| 382 |
(string-append "a" #**"b" "c" "d" "e" #**"f")) |
| 383 |
|
| 384 |
(test* "string-join" #**"a:b:c" |
| 385 |
(string-join '("a" #**"b" "c") ":")) |
| 386 |
(test* "string-join" #**"a:b:c" |
| 387 |
(string-join '("a" "b" "c") #**":")) |
| 388 |
|
| 389 |
;; NB: should we allow this? |
| 390 |
(test* "string-set!" #**"abQde" |
| 391 |
(let ((s (string-copy #**"abcde"))) |
| 392 |
(string-set! s 2 #\Q) |
| 393 |
s)) |
| 394 |
(test* "string-byte-set!" #**"abQde" |
| 395 |
(let ((s (string-copy "abcde"))) |
| 396 |
(string-byte-set! s 2 (char->integer #\Q)) |
| 397 |
s)) |
| 398 |
(test* "string-byte-set!" #**"abQde" |
| 399 |
(let ((s (string-copy #**"abcde"))) |
| 400 |
(string-byte-set! s 2 (char->integer #\Q)) |
| 401 |
s)) |
| 402 |
|
| 403 |
;; In 0.9.1 this gets an error |
| 404 |
(unless (eq? (gauche-character-encoding) 'none) |
| 405 |
(test* "string-byte-set!" #**"\x01\x81\x82" |
| 406 |
(string-byte-set! (string-copy "\u3042") 0 1))) |
| 407 |
|
| 408 |
(test* "substring" #**"ab" |
| 409 |
(substring #**"abcde" 0 2)) |
| 410 |
|
| 411 |
;;------------------------------------------------------------------- |
| 412 |
(test-section "string-cursor") |
| 413 |
|
| 414 |
(define str "abcdefg") |
| 415 |
(define ss #f) |
| 416 |
(define se #f) |
| 417 |
(test* "string-cursor-start" #t |
| 418 |
(begin |
| 419 |
(set! ss (string-cursor-start str)) |
| 420 |
(string-cursor? ss))) |
| 421 |
(test* "string-cursor-start" #t |
| 422 |
(begin |
| 423 |
(set! se (string-cursor-end str)) |
| 424 |
(string-cursor? se))) |
| 425 |
(test* "string-cursor-next" #\b |
| 426 |
(string-ref str (string-cursor-next str ss))) |
| 427 |
(test* "string-cursor-forward" #\d |
| 428 |
(string-ref str (string-cursor-forward str ss 3))) |
| 429 |
(test* "string-cursor-prev" #\g |
| 430 |
(string-ref str (string-cursor-prev str se))) |
| 431 |
(test* "string-cursor-back" #\d |
| 432 |
(string-ref str (string-cursor-back str se 4))) |
| 433 |
(test* "string-index->cursor" #\f |
| 434 |
(string-ref str (string-index->cursor str 5))) |
| 435 |
(test* "string-cursor->index" 0 |
| 436 |
(string-cursor->index str ss)) |
| 437 |
(test* "string-cursor->index" 7 |
| 438 |
(string-cursor->index str se)) |
| 439 |
(test* "substring with cursors" "cde" |
| 440 |
(substring str |
| 441 |
(string-cursor-forward str ss 2) |
| 442 |
(string-cursor-back str se 2))) |
| 443 |
(test* "substring with cursors" "cde" |
| 444 |
(substring str |
| 445 |
2 |
| 446 |
(string-cursor-back str se 2))) |
| 447 |
(test* "substring with cursors" "cde" |
| 448 |
(substring str |
| 449 |
(string-cursor-forward str ss 2) |
| 450 |
5)) |
| 451 |
(test* "string->list" '(#\b #\c #\d #\e #\f) |
| 452 |
(string->list str |
| 453 |
(string-cursor-next str ss) |
| 454 |
(string-cursor-prev str se))) |
| 455 |
(test* "string->list" '(#\b #\c #\d #\e #\f) |
| 456 |
(string->list str |
| 457 |
1 |
| 458 |
(string-cursor-prev str se))) |
| 459 |
(test* "string->list" '(#\b #\c #\d #\e #\f) |
| 460 |
(string->list str |
| 461 |
(string-cursor-next str ss) |
| 462 |
6)) |
| 463 |
(test* "string-cursor=?" #t |
| 464 |
(string-cursor=? ss (string-cursor-back str se 7))) |
| 465 |
(test* "string-cursor=?" #t |
| 466 |
(string-cursor=? 0 0)) |
| 467 |
(test* "string-cursor<?" #t |
| 468 |
(string-cursor<? ss se)) |
| 469 |
(test* "string-cursor<=?" #t |
| 470 |
(string-cursor<=? ss se)) |
| 471 |
(test* "string-cursor<=?" #t |
| 472 |
(string-cursor<=? ss ss)) |
| 473 |
(test* "string-cursor>?" #t |
| 474 |
(string-cursor>? se ss)) |
| 475 |
(test* "string-cursor>=?" #t |
| 476 |
(string-cursor>=? se se)) |
| 477 |
(test* "string-cursor-diff" 7 |
| 478 |
(string-cursor-diff str ss se)) |
| 479 |
(test* "string-cursor-diff" -7 |
| 480 |
(string-cursor-diff str se ss)) |
| 481 |
|
| 482 |
;;------------------------------------------------------------------- |
| 483 |
(test-section "string mappers") |
| 484 |
|
| 485 |
(test* "string-map, single arg" "O'AFU" |
| 486 |
(string-map char-upcase "o'afu")) |
| 487 |
(test* "string-map, single arg" "" |
| 488 |
(string-map char-upcase "")) |
| 489 |
(test* "string-map, multi arg" "orpng" |
| 490 |
(string-map (^[a b] (if (char>? a b) a b)) |
| 491 |
"apple" "orange")) |
| 492 |
(test* "string-map, SRFI-13 style" "CALIFRAGILISTICEXPIALIDOCIOUS" |
| 493 |
(string-map char-upcase "supercalifragilisticexpialidocious" 5)) |
| 494 |
(test* "string-map, SRFI-13 style" "CALIFRAGILISTIC" |
| 495 |
(string-map char-upcase "supercalifragilisticexpialidocious" 5 20)) |
| 496 |
(test* "string-map, bad arg" (test-error) |
| 497 |
(string-map (^ _ #\space) "abc" 'def)) |
| 498 |
(test* "string-map, bad arg" (test-error) |
| 499 |
(string-map (^ _ #\space) 'abc)) |
| 500 |
(test* "string-map, bad arg" (test-error) |
| 501 |
(string-map (^ _ #\space) "abc" "def" 2)) |
| 502 |
(test* "string-map, bad arg" (test-error) |
| 503 |
(string-map (^ _ #\space) "abc" 1 2 3)) |
| 504 |
(test* "string-for-each single arg" "maui" |
| 505 |
(with-output-to-string |
| 506 |
(cut string-for-each write-char "maui"))) |
| 507 |
(test* "string-for-each multi arg" "Niuihi" |
| 508 |
(with-output-to-string |
| 509 |
(cut string-for-each (^[a b] (write-char (if (char>? a b) a b))) |
| 510 |
"Kaua'i" "Ni'ihau"))) |
| 511 |
(test* "string-for-each SRFI-13 style" "califragilistic" |
| 512 |
(with-output-to-string |
| 513 |
(cut string-for-each write-char |
| 514 |
"supercalifragilisticexpialidocious" 5 20))) |
| 515 |
|
| 516 |
;;------------------------------------------------------------------- |
| 517 |
(test-section "input string port") |
| 518 |
|
| 519 |
;; These also tests port's ungetc and scratch buffer, and |
| 520 |
;; some special string syntax. |
| 521 |
|
| 522 |
(define istr (open-input-string "abcdefg")) |
| 523 |
|
| 524 |
(test* "read-char" #\a (read-char istr)) |
| 525 |
(test* "peek-char" #\b (peek-char istr)) |
| 526 |
(test* "read-byte" 98 (read-byte istr)) |
| 527 |
(test* "read-byte from ungotten buffer" 99 |
| 528 |
(begin (peek-char istr) (read-byte istr))) |
| 529 |
(test* "read-block using ungotten buffer" #**"d" |
| 530 |
(begin (peek-char istr) (read-block 1 istr))) |
| 531 |
(test* "read-block using ungotten buffer" #**"efg" |
| 532 |
(begin (peek-char istr) (read-block 10 istr))) |
| 533 |
(test* "termination" #t |
| 534 |
(eof-object? (read-char istr))) |
| 535 |
(test* "termination" #t |
| 536 |
(eof-object? (read-byte istr))) |
| 537 |
(test* "termination" #t |
| 538 |
(eof-object? (read-block 3 istr))) |
| 539 |
|
| 540 |
(test* "get-remaining-input-string" "defg" |
| 541 |
(let ((istr (open-input-string "abcdefg"))) |
| 542 |
(read-char istr) |
| 543 |
(read-char istr) |
| 544 |
(read-char istr) |
| 545 |
(get-remaining-input-string istr))) |
| 546 |
(test* "get-remaining-input-string" "" |
| 547 |
(let ((istr (open-input-string "abcdefg"))) |
| 548 |
(read-line istr) |
| 549 |
(get-remaining-input-string istr))) |
| 550 |
(test* "get-remaining-input-string" "cdefg" |
| 551 |
(let ((istr (open-input-string "abcdefg"))) |
| 552 |
(read-char istr) |
| 553 |
(read-char istr) |
| 554 |
(peek-char istr) |
| 555 |
(get-remaining-input-string istr))) |
| 556 |
(test* "get-remaining-input-string" "cdefg" |
| 557 |
(let ((istr (open-input-string "abcdefg"))) |
| 558 |
(read-char istr) |
| 559 |
(read-char istr) |
| 560 |
(peek-byte istr) |
| 561 |
(get-remaining-input-string istr))) |
| 562 |
|
| 563 |
(test* "line continuation" "abcdefgh" |
| 564 |
(read (open-input-string "\"abcd\\\n efgh\""))) |
| 565 |
(test* "line continuation" "abcdefgh2" |
| 566 |
(read (open-input-string "\"abcd\\\nefgh2\""))) |
| 567 |
(test* "line continuation" "abcdefgh3" |
| 568 |
(read (open-input-string "\"abcd\\\n\t \tefgh3\""))) |
| 569 |
(test* "line continuation" "ABCDEFGH" |
| 570 |
(read (open-input-string "\"ABCD\\\r\n EFGH\""))) |
| 571 |
(test* "line continuation" "ABCDEFGH2" |
| 572 |
(read (open-input-string "\"ABCD\\\r\nEFGH2\""))) |
| 573 |
(test* "line continuation" "ABCDEFGH3" |
| 574 |
(read (open-input-string "\"ABCD\\\r\n \t EFGH3\""))) |
| 575 |
(test* "line continuation" "ABCDefgh" |
| 576 |
(read (open-input-string "\"ABCD\\\r efgh\""))) |
| 577 |
(test* "line continuation" "ABCDefgh2" |
| 578 |
(read (open-input-string "\"ABCD\\\refgh2\""))) |
| 579 |
(test* "line continuation" "ABCDefgh3" |
| 580 |
(read (open-input-string "\"ABCD\\ \t \refgh3\""))) |
| 581 |
(test* "line continuation" "0123 4567" |
| 582 |
(read (open-input-string "\"0123 \\\n 4567\""))) |
| 583 |
(test* "line continuation" "0123 4567" |
| 584 |
(read (open-input-string "\"0123 \\ \n 4567\""))) |
| 585 |
(test* "line continuation" "0123-4567" |
| 586 |
(read (open-input-string "\"0123\\\n \\ \n -4567\""))) |
| 587 |
(test* "line continuation" "abcd" |
| 588 |
(read (open-input-string "\"abcd\\\n\""))) |
| 589 |
(test* "line continuation" "abcd2" |
| 590 |
(read (open-input-string "\"abcd2\\\r\n \""))) |
| 591 |
(test* "line continuation" "abcd3" |
| 592 |
(read (open-input-string "\"abcd3\\\r\t\""))) |
| 593 |
(test* "line continuation (invalid)" (test-error) |
| 594 |
(read (open-input-string "\"1234\\ x\""))) |
| 595 |
|
| 596 |
(unless (eq? (gauche-character-encoding) 'none) |
| 597 |
(test* "line continuation (extended chars)" "abc def1" |
| 598 |
(read (open-input-string "\"abc \\\u3000\ndef1\""))) |
| 599 |
(test* "line continuation (extended chars)" "abc def2" |
| 600 |
(read (open-input-string "\"abc \\ \u3000\ndef2\""))) |
| 601 |
(test* "line continuation (extended chars)" "abc def3" |
| 602 |
(read (open-input-string "\"abc \\\u3000 \ndef3\""))) |
| 603 |
(test* "line continuation (extended chars)" "ABC DEF1" |
| 604 |
(read (open-input-string "\"ABC \\\n\u3000 DEF1\""))) |
| 605 |
(test* "line continuation (extended chars)" "ABC DEF2" |
| 606 |
(read (open-input-string "\"ABC \\\n \u3000DEF2\""))) |
| 607 |
(test* "line continuation (extended chars)" "uvw xyz1" |
| 608 |
(read (open-input-string "\"uvw \\\u3000\n\u3000\\\nxyz1\""))) |
| 609 |
) |
| 610 |
|
| 611 |
(define (read-line-tester str) |
| 612 |
(let1 s (open-input-string str) |
| 613 |
(let loop ((l (read-line s)) |
| 614 |
(r '())) |
| 615 |
(if (eof-object? l) (reverse r) (loop (read-line s) (cons l r)))))) |
| 616 |
|
| 617 |
(test* "read-line (nullstr)" '() |
| 618 |
(read-line-tester "")) |
| 619 |
(test* "read-line (NL)" '("") |
| 620 |
(read-line-tester "\n")) |
| 621 |
(test* "read-line (CR)" '("") |
| 622 |
(read-line-tester "\r")) |
| 623 |
(test* "read-line (CRNL)" '("") |
| 624 |
(read-line-tester "\r\n")) |
| 625 |
(test* "read-line (mix)" '("ab" "cd" "" "ef" "g") |
| 626 |
(read-line-tester "ab\rcd\r\r\nef\ng")) |
| 627 |
(test* "read-line (ungotten)" '("ab" "cd") |
| 628 |
(let1 s (open-input-string "ab\ncd") |
| 629 |
(let loop ((l (begin (peek-char s) (read-line s))) |
| 630 |
(r '())) |
| 631 |
(if (eof-object? l) (reverse r) (loop (read-line s) (cons l r)))))) |
| 632 |
|
| 633 |
;;------------------------------------------------------------------- |
| 634 |
(test-section "output string port") |
| 635 |
|
| 636 |
;; This effectively tests the dynamic string implemenatation. |
| 637 |
;; The parameter dstr-init-size and dstr-incr-factor have to |
| 638 |
;; match to test boundary conditions. |
| 639 |
|
| 640 |
(define *dstr-init-size* 32) |
| 641 |
(define *dstr-incr-factor* 3) |
| 642 |
|
| 643 |
(define (string-port-tester . args) |
| 644 |
(let ((out (open-output-string))) |
| 645 |
(for-each (^s (display s out)) args) |
| 646 |
(get-output-string out))) |
| 647 |
|
| 648 |
(define (test-string-port signature total seg) |
| 649 |
(let* ((repeat (inexact->exact (ceiling (/ total seg)))) |
| 650 |
(actual (* seg repeat)) |
| 651 |
(result (make-string actual #\?))) |
| 652 |
(test (string-append "string-port " signature) |
| 653 |
#t |
| 654 |
(lambda () |
| 655 |
(string=? result |
| 656 |
(apply string-port-tester (make-list repeat (make-string seg #\?)))))))) |
| 657 |
|
| 658 |
(define (test-string-ports signature total . segs) |
| 659 |
(test-string-port signature total total) |
| 660 |
(for-each (lambda (seg) (test-string-port signature total seg)) segs)) |
| 661 |
|
| 662 |
(test* "string-port (0)" "" |
| 663 |
(string-port-tester)) |
| 664 |
(test* "string-port (0)" "" |
| 665 |
(string-port-tester "" "" "")) |
| 666 |
|
| 667 |
(test-string-ports "(small-1)" (- *dstr-init-size* 1) 3 2 1) |
| 668 |
(test-string-ports "(small)" *dstr-init-size* 3 2 1) |
| 669 |
(test-string-ports "(small+1)" (+ *dstr-init-size* 1) 3 2 1) |
| 670 |
(test-string-ports "(mid-1)" |
| 671 |
(- (* *dstr-init-size* (+ *dstr-incr-factor* 1)) 1) |
| 672 |
(- *dstr-init-size* 1) *dstr-init-size* 3) |
| 673 |
(test-string-ports "(mid)" |
| 674 |
(* *dstr-init-size* (+ *dstr-incr-factor* 1)) |
| 675 |
(- *dstr-init-size* 1) *dstr-init-size* 3) |
| 676 |
(test-string-ports "(mid+1)" |
| 677 |
(+ (* *dstr-init-size* (+ *dstr-incr-factor* 1)) 1) |
| 678 |
(- *dstr-init-size* 1) *dstr-init-size* 3) |
| 679 |
(test-string-ports "(large)" 10000 |
| 680 |
(- *dstr-init-size* 1) *dstr-init-size* |
| 681 |
(+ *dstr-init-size* 1) |
| 682 |
(- (* *dstr-init-size* (+ *dstr-incr-factor* 1)) 1) |
| 683 |
(* *dstr-init-size* (+ *dstr-incr-factor* 1)) |
| 684 |
) |
| 685 |
|
| 686 |
;;------------------------------------------------------------------- |
| 687 |
(test-section "immutablility") |
| 688 |
|
| 689 |
(test* "string-immutable?" '(#t #f) |
| 690 |
(list (string-immutable? "abc") |
| 691 |
(string-immutable? (string-copy "abc")))) |
| 692 |
|
| 693 |
(test* "string-copy-immutable" '(#t #t #t #t) |
| 694 |
(list (string-immutable? (string-copy-immutable "abc")) |
| 695 |
(string-immutable? (string-copy-immutable "abc" 0 1)) |
| 696 |
(string-immutable? (string-copy-immutable (string-copy "abc"))) |
| 697 |
(string-immutable? (string-copy-immutable (string-copy "abc") 0 1)))) |
| 698 |
|
| 699 |
;;------------------------------------------------------------------- |
| 700 |
(test-section "string interpolation") |
| 701 |
|
| 702 |
(test* "string interpolation" "string interpolation" |
| 703 |
(let ((x "inter") (y "polation")) |
| 704 |
#"string ~|x|~|y|")) |
| 705 |
(test "string interpolation" "string interpolation" |
| 706 |
(lambda () |
| 707 |
(define (x) "inter") |
| 708 |
(define (y) "polation") |
| 709 |
#"string ~(x)~(y)")) |
| 710 |
(test "string interpolation" "string interpolation" |
| 711 |
(lambda () |
| 712 |
(define (x a) |
| 713 |
(if a "inter" "polation")) |
| 714 |
#"string ~(x #t)~(x #f)")) |
| 715 |
|
| 716 |
(test-end) |