| 1 |
;;;; strings.test --- test suite for Guile's string functions -*- scheme -*- |
| 2 |
;;;; Jim Blandy <[email protected]> --- August 1999 |
| 3 |
;;;; |
| 4 |
;;;; Copyright (C) 1999,2001,2004-2006,2008-2011,2013,2015,2018,2020,2023 |
| 5 |
;;;; Free Software Foundation, Inc. |
| 6 |
;;;; |
| 7 |
;;;; This library is free software; you can redistribute it and/or |
| 8 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 9 |
;;;; License as published by the Free Software Foundation; either |
| 10 |
;;;; version 3 of the License, or (at your option) any later version. |
| 11 |
;;;; |
| 12 |
;;;; This library is distributed in the hope that it will be useful, |
| 13 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
;;;; Lesser General Public License for more details. |
| 16 |
;;;; |
| 17 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 18 |
;;;; License along with this library; if not, write to the Free Software |
| 19 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
|
| 21 |
(define-module (test-strings) |
| 22 |
#:use-module ((system base compile) #:select (compile)) |
| 23 |
#:use-module (test-suite lib) |
| 24 |
#:use-module (ice-9 string-fun)) |
| 25 |
|
| 26 |
(define exception:read-only-string |
| 27 |
(cons 'misc-error "^string is read-only")) |
| 28 |
(define exception:invalid-escape |
| 29 |
(cons 'read-error "invalid character in escape sequence")) |
| 30 |
|
| 31 |
;; Create a string from integer char values, eg. (string-ints 65) => "A" |
| 32 |
(define (string-ints . args) |
| 33 |
(apply string (map integer->char args))) |
| 34 |
|
| 35 |
;; |
| 36 |
;; string internals |
| 37 |
;; |
| 38 |
|
| 39 |
;; Some abbreviations |
| 40 |
;; BMP - Basic Multilingual Plane (codepoints below U+FFFF) |
| 41 |
;; SMP - Suplementary Multilingual Plane (codebpoints from U+10000 to U+1FFFF) |
| 42 |
|
| 43 |
(with-test-prefix "string internals" |
| 44 |
|
| 45 |
(pass-if "new string starts at 1st char in stringbuf" |
| 46 |
(let ((s "abc")) |
| 47 |
(= 0 (assq-ref (%string-dump s) 'start)))) |
| 48 |
|
| 49 |
(pass-if "length of new string same as stringbuf" |
| 50 |
(let ((s "def")) |
| 51 |
(= (string-length s) (assq-ref (%string-dump s) 'stringbuf-length)))) |
| 52 |
|
| 53 |
(pass-if "contents of new string same as stringbuf" |
| 54 |
(let ((s "ghi")) |
| 55 |
(string=? s (assq-ref (%string-dump s) 'stringbuf-chars)))) |
| 56 |
|
| 57 |
(pass-if "writable strings are not read-only" |
| 58 |
(let ((s "zyx")) |
| 59 |
(not (assq-ref (%string-dump s) 'read-only)))) |
| 60 |
|
| 61 |
(pass-if "read-only strings are read-only" |
| 62 |
(let ((s (substring/read-only "zyx" 0))) |
| 63 |
(assq-ref (%string-dump s) 'read-only))) |
| 64 |
|
| 65 |
(pass-if "new Latin-1 encoded strings are not shared" |
| 66 |
(let ((s "abc")) |
| 67 |
(not (assq-ref (%string-dump s) 'stringbuf-shared)))) |
| 68 |
|
| 69 |
(pass-if "new UCS-4 encoded strings are not shared" |
| 70 |
(let ((s "\u0100bc")) |
| 71 |
(not (assq-ref (%string-dump s) 'stringbuf-shared)))) |
| 72 |
|
| 73 |
;; Should this be true? It isn't currently true. |
| 74 |
(pass-if "null shared substrings are shared" |
| 75 |
(let* ((s1 "") |
| 76 |
(s2 (substring/shared s1 0 0))) |
| 77 |
(throw 'untested) |
| 78 |
(eq? (assq-ref (%string-dump s2) 'shared) |
| 79 |
s1))) |
| 80 |
|
| 81 |
(pass-if "ASCII shared substrings are shared" |
| 82 |
(let* ((s1 "foobar") |
| 83 |
(s2 (substring/shared s1 0 3))) |
| 84 |
(eq? (assq-ref (%string-dump s2) 'shared) |
| 85 |
s1))) |
| 86 |
|
| 87 |
(pass-if "BMP shared substrings are shared" |
| 88 |
(let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105") |
| 89 |
(s2 (substring/shared s1 0 3))) |
| 90 |
(eq? (assq-ref (%string-dump s2) 'shared) |
| 91 |
s1))) |
| 92 |
|
| 93 |
(pass-if "null substrings are not shared" |
| 94 |
(let* ((s1 "") |
| 95 |
(s2 (substring s1 0 0))) |
| 96 |
(not (eq? (assq-ref (%string-dump s2) 'shared) |
| 97 |
s1)))) |
| 98 |
|
| 99 |
(pass-if "ASCII substrings are not shared" |
| 100 |
(let* ((s1 "foobar") |
| 101 |
(s2 (substring s1 0 3))) |
| 102 |
(not (eq? (assq-ref (%string-dump s2) 'shared) |
| 103 |
s1)))) |
| 104 |
|
| 105 |
(pass-if "BMP substrings are not shared" |
| 106 |
(let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105") |
| 107 |
(s2 (substring s1 0 3))) |
| 108 |
(not (eq? (assq-ref (%string-dump s2) 'shared) |
| 109 |
s1)))) |
| 110 |
|
| 111 |
(pass-if "ASCII substrings immutable before copy-on-write" |
| 112 |
(let* ((s1 "foobar") |
| 113 |
(s2 (substring s1 0 3))) |
| 114 |
(and (not (assq-ref (%string-dump s1) 'stringbuf-mutable)) |
| 115 |
(not (assq-ref (%string-dump s2) 'stringbuf-mutable))))) |
| 116 |
|
| 117 |
(pass-if "BMP substrings immutable before copy-on-write" |
| 118 |
(let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105") |
| 119 |
(s2 (substring s1 0 3))) |
| 120 |
(and (not (assq-ref (%string-dump s1) 'stringbuf-mutable)) |
| 121 |
(not (assq-ref (%string-dump s2) 'stringbuf-mutable))))) |
| 122 |
|
| 123 |
(pass-if "ASCII base string still immutable after copy-on-write" |
| 124 |
(let* ((s1 "foobar") |
| 125 |
(s2 (substring s1 0 3))) |
| 126 |
(string-set! s2 0 #\F) |
| 127 |
(and (not (assq-ref (%string-dump s1) 'stringbuf-mutable)) |
| 128 |
(assq-ref (%string-dump s2) 'stringbuf-mutable)))) |
| 129 |
|
| 130 |
(pass-if "BMP base string still immutable after copy-on-write" |
| 131 |
(let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105") |
| 132 |
(s2 (substring s1 0 3))) |
| 133 |
(string-set! s2 0 #\F) |
| 134 |
(and (not (assq-ref (%string-dump s1) 'stringbuf-mutable)) |
| 135 |
(assq-ref (%string-dump s2) 'stringbuf-mutable)))) |
| 136 |
|
| 137 |
(pass-if "ASCII substrings mutable after shared mutation" |
| 138 |
(let* ((s1 "foobar") |
| 139 |
(s2 (substring/shared s1 0 3))) |
| 140 |
(string-set! s2 0 #\F) |
| 141 |
(and (assq-ref (%string-dump s1) 'stringbuf-mutable) |
| 142 |
(assq-ref (%string-dump s2) 'stringbuf-mutable)))) |
| 143 |
|
| 144 |
(pass-if "BMP substrings mutable after shared mutation" |
| 145 |
(let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105") |
| 146 |
(s2 (substring/shared s1 0 3))) |
| 147 |
(string-set! s2 0 #\F) |
| 148 |
(and (assq-ref (%string-dump s1) 'stringbuf-mutable) |
| 149 |
(assq-ref (%string-dump s2) 'stringbuf-mutable)))) |
| 150 |
|
| 151 |
(with-test-prefix "encodings" |
| 152 |
|
| 153 |
(pass-if "null strings are Latin-1 encoded" |
| 154 |
(let ((s "")) |
| 155 |
(not (assq-ref (%string-dump s) 'stringbuf-wide)))) |
| 156 |
|
| 157 |
(pass-if "ASCII strings are Latin-1 encoded" |
| 158 |
(let ((s "jkl")) |
| 159 |
(not (assq-ref (%string-dump s) 'stringbuf-wide)))) |
| 160 |
|
| 161 |
(pass-if "Latin-1 strings are Latin-1 encoded" |
| 162 |
(let ((s "\xC0\xC1\xC2")) |
| 163 |
(not (assq-ref (%string-dump s) 'stringbuf-wide)))) |
| 164 |
|
| 165 |
(pass-if "BMP strings are UCS-4 encoded" |
| 166 |
(let ((s "\u0100\u0101\x0102")) |
| 167 |
(assq-ref (%string-dump s) 'stringbuf-wide))) |
| 168 |
|
| 169 |
(pass-if "SMP strings are UCS-4 encoded" |
| 170 |
(let ((s "\U010300\u010301\x010302")) |
| 171 |
(assq-ref (%string-dump s) 'stringbuf-wide))) |
| 172 |
|
| 173 |
(pass-if "null list->string is Latin-1 encoded" |
| 174 |
(let ((s (string-ints))) |
| 175 |
(not (assq-ref (%string-dump s) 'stringbuf-wide)))) |
| 176 |
|
| 177 |
(pass-if "ASCII list->string is Latin-1 encoded" |
| 178 |
(let ((s (string-ints 65 66 67))) |
| 179 |
(not (assq-ref (%string-dump s) 'stringbuf-wide)))) |
| 180 |
|
| 181 |
(pass-if "Latin-1 list->string is Latin-1 encoded" |
| 182 |
(let ((s (string-ints #xc0 #xc1 #xc2))) |
| 183 |
(not (assq-ref (%string-dump s) 'stringbuf-wide)))) |
| 184 |
|
| 185 |
(pass-if "BMP list->string is UCS-4 encoded" |
| 186 |
(let ((s (string-ints #x0100 #x0101 #x0102))) |
| 187 |
(assq-ref (%string-dump s) 'stringbuf-wide))) |
| 188 |
|
| 189 |
(pass-if "SMP list->string is UCS-4 encoded" |
| 190 |
(let ((s (string-ints #x010300 #x010301 #x010302))) |
| 191 |
(assq-ref (%string-dump s) 'stringbuf-wide))) |
| 192 |
|
| 193 |
(pass-if "encoding of string not based on escape style" |
| 194 |
(let ((s "\U000040")) |
| 195 |
(not (assq-ref (%string-dump s) 'stringbuf-wide)))))) |
| 196 |
|
| 197 |
(with-test-prefix "escapes" |
| 198 |
|
| 199 |
(pass-if-exception "non-hex char in two-digit hex-escape" |
| 200 |
exception:invalid-escape |
| 201 |
(with-input-from-string "\"\\x0g\"" read)) |
| 202 |
|
| 203 |
(pass-if-exception "non-hex char in four-digit hex-escape" |
| 204 |
exception:invalid-escape |
| 205 |
(with-input-from-string "\"\\u000g\"" read)) |
| 206 |
|
| 207 |
(pass-if-exception "non-hex char in six-digit hex-escape" |
| 208 |
exception:invalid-escape |
| 209 |
(with-input-from-string "\"\\U00000g\"" read)) |
| 210 |
|
| 211 |
(pass-if-exception "premature termination of two-digit hex-escape" |
| 212 |
exception:invalid-escape |
| 213 |
(with-input-from-string "\"\\x0\"" read)) |
| 214 |
|
| 215 |
(pass-if-exception "premature termination of four-digit hex-escape" |
| 216 |
exception:invalid-escape |
| 217 |
(with-input-from-string "\"\\u000\"" read)) |
| 218 |
|
| 219 |
(pass-if-exception "premature termination of six-digit hex-escape" |
| 220 |
exception:invalid-escape |
| 221 |
(with-input-from-string "\"\\U00000\"" read)) |
| 222 |
|
| 223 |
(pass-if "extra hex digits ignored for two-digit hex escape" |
| 224 |
(eqv? (string-ref "--\xfff--" 2) |
| 225 |
(integer->char #xff))) |
| 226 |
|
| 227 |
(pass-if "extra hex digits ignored for four-digit hex escape" |
| 228 |
(eqv? (string-ref "--\u0100f--" 2) |
| 229 |
(integer->char #x0100))) |
| 230 |
|
| 231 |
(pass-if "extra hex digits ignored for six-digit hex escape" |
| 232 |
(eqv? (string-ref "--\U010300f--" 2) |
| 233 |
(integer->char #x010300))) |
| 234 |
|
| 235 |
(pass-if "escaped characters match non-escaped ASCII characters" |
| 236 |
(string=? "ABC" "\x41\u0042\U000043")) |
| 237 |
|
| 238 |
(pass-if "R5RS backslash escapes" |
| 239 |
(string=? "\"\\" (string #\" #\\))) |
| 240 |
|
| 241 |
(pass-if "R6RS backslash escapes" |
| 242 |
(string=? "\a\b\t\n\v\f\r" |
| 243 |
(string #\alarm #\backspace #\tab #\newline #\vtab |
| 244 |
#\page #\return))) |
| 245 |
|
| 246 |
(pass-if "Guile extensions backslash escapes" |
| 247 |
(string=? "\0" (string #\nul)))) |
| 248 |
|
| 249 |
;; |
| 250 |
;; string? |
| 251 |
;; |
| 252 |
(with-test-prefix "string?" |
| 253 |
|
| 254 |
(pass-if "string" |
| 255 |
(string? "abc")) |
| 256 |
|
| 257 |
(pass-if "symbol" |
| 258 |
(not (string? 'abc)))) |
| 259 |
|
| 260 |
;; |
| 261 |
;; literals |
| 262 |
;; |
| 263 |
|
| 264 |
(with-test-prefix "literals" |
| 265 |
|
| 266 |
;; The "Storage Model" section of R5RS reads: "In such systems literal |
| 267 |
;; constants and the strings returned by `symbol->string' are |
| 268 |
;; immutable objects". `eval' doesn't support it yet, but it doesn't |
| 269 |
;; really matter because `eval' doesn't coalesce repeated constants, |
| 270 |
;; unlike the bytecode compiler. |
| 271 |
|
| 272 |
(pass-if-exception "literals are constant" |
| 273 |
exception:read-only-string |
| 274 |
(compile '(string-set! "literal string" 0 #\x) |
| 275 |
#:from 'scheme |
| 276 |
#:to 'value))) |
| 277 |
|
| 278 |
;; |
| 279 |
;; string-null? |
| 280 |
;; |
| 281 |
|
| 282 |
(with-test-prefix "string-null?" |
| 283 |
|
| 284 |
(pass-if "null string" |
| 285 |
(string-null? "")) |
| 286 |
|
| 287 |
(pass-if "non-null string" |
| 288 |
(not (string-null? "a"))) |
| 289 |
|
| 290 |
(pass-if "respects \\0" |
| 291 |
(not (string-null? "\0"))) |
| 292 |
|
| 293 |
(pass-if-exception "symbol" |
| 294 |
exception:wrong-type-arg |
| 295 |
(string-null? 'a))) |
| 296 |
|
| 297 |
;; |
| 298 |
;; string=? |
| 299 |
;; |
| 300 |
|
| 301 |
(with-test-prefix "string=?" |
| 302 |
|
| 303 |
(pass-if "respects 1st parameter's string length" |
| 304 |
(not (string=? "foo\0" "foo"))) |
| 305 |
|
| 306 |
(pass-if "respects 2nd paramter's string length" |
| 307 |
(not (string=? "foo" "foo\0"))) |
| 308 |
|
| 309 |
(with-test-prefix "wrong argument type" |
| 310 |
|
| 311 |
(pass-if-exception "1st argument symbol" |
| 312 |
exception:wrong-type-arg |
| 313 |
(string=? 'a "a")) |
| 314 |
|
| 315 |
(pass-if-exception "2nd argument symbol" |
| 316 |
exception:wrong-type-arg |
| 317 |
(string=? "a" 'b)) |
| 318 |
|
| 319 |
(pass-if-exception "1st argument EOF" |
| 320 |
exception:wrong-type-arg |
| 321 |
(string=? (with-input-from-string "" read) "b")) |
| 322 |
|
| 323 |
(pass-if-exception "2nd argument EOF" |
| 324 |
exception:wrong-type-arg |
| 325 |
(string=? "a" (with-input-from-string "" read))))) |
| 326 |
|
| 327 |
;; |
| 328 |
;; string<? |
| 329 |
;; |
| 330 |
|
| 331 |
(with-test-prefix "string<?" |
| 332 |
|
| 333 |
(pass-if "respects string length" |
| 334 |
(and (not (string<? "foo\0a" "foo\0a")) |
| 335 |
(string<? "foo\0a" "foo\0b"))) |
| 336 |
|
| 337 |
(with-test-prefix "wrong argument type" |
| 338 |
|
| 339 |
(pass-if-exception "1st argument symbol" |
| 340 |
exception:wrong-type-arg |
| 341 |
(string<? 'a "a")) |
| 342 |
|
| 343 |
(pass-if-exception "2nd argument symbol" |
| 344 |
exception:wrong-type-arg |
| 345 |
(string<? "a" 'b))) |
| 346 |
|
| 347 |
(pass-if "same as char<?" |
| 348 |
(eq? (char<? (integer->char 0) (integer->char 255)) |
| 349 |
(string<? (string-ints 0) (string-ints 255))))) |
| 350 |
|
| 351 |
;; |
| 352 |
;; string-ci<? |
| 353 |
;; |
| 354 |
|
| 355 |
(with-test-prefix "string-ci<?" |
| 356 |
|
| 357 |
(pass-if "respects string length" |
| 358 |
(and (not (string-ci<? "foo\0a" "foo\0a")) |
| 359 |
(string-ci<? "foo\0a" "foo\0b"))) |
| 360 |
|
| 361 |
(with-test-prefix "wrong argument type" |
| 362 |
|
| 363 |
(pass-if-exception "1st argument symbol" |
| 364 |
exception:wrong-type-arg |
| 365 |
(string-ci<? 'a "a")) |
| 366 |
|
| 367 |
(pass-if-exception "2nd argument symbol" |
| 368 |
exception:wrong-type-arg |
| 369 |
(string-ci<? "a" 'b))) |
| 370 |
|
| 371 |
(pass-if "same as char-ci<?" |
| 372 |
(eq? (char-ci<? (integer->char 0) (integer->char 255)) |
| 373 |
(string-ci<? (string-ints 0) (string-ints 255))))) |
| 374 |
|
| 375 |
;; |
| 376 |
;; string<=? |
| 377 |
;; |
| 378 |
|
| 379 |
(with-test-prefix "string<=?" |
| 380 |
|
| 381 |
(pass-if "same as char<=?" |
| 382 |
(eq? (char<=? (integer->char 0) (integer->char 255)) |
| 383 |
(string<=? (string-ints 0) (string-ints 255))))) |
| 384 |
|
| 385 |
;; |
| 386 |
;; string-ci<=? |
| 387 |
;; |
| 388 |
|
| 389 |
(with-test-prefix "string-ci<=?" |
| 390 |
|
| 391 |
(pass-if "same as char-ci<=?" |
| 392 |
(eq? (char-ci<=? (integer->char 0) (integer->char 255)) |
| 393 |
(string-ci<=? (string-ints 0) (string-ints 255))))) |
| 394 |
|
| 395 |
;; |
| 396 |
;; string>? |
| 397 |
;; |
| 398 |
|
| 399 |
(with-test-prefix "string>?" |
| 400 |
|
| 401 |
(pass-if "same as char>?" |
| 402 |
(eq? (char>? (integer->char 0) (integer->char 255)) |
| 403 |
(string>? (string-ints 0) (string-ints 255))))) |
| 404 |
|
| 405 |
;; |
| 406 |
;; string-ci>? |
| 407 |
;; |
| 408 |
|
| 409 |
(with-test-prefix "string-ci>?" |
| 410 |
|
| 411 |
(pass-if "same as char-ci>?" |
| 412 |
(eq? (char-ci>? (integer->char 0) (integer->char 255)) |
| 413 |
(string-ci>? (string-ints 0) (string-ints 255))))) |
| 414 |
|
| 415 |
;; |
| 416 |
;; string>=? |
| 417 |
;; |
| 418 |
|
| 419 |
(with-test-prefix "string>=?" |
| 420 |
|
| 421 |
(pass-if "same as char>=?" |
| 422 |
(eq? (char>=? (integer->char 0) (integer->char 255)) |
| 423 |
(string>=? (string-ints 0) (string-ints 255))))) |
| 424 |
|
| 425 |
;; |
| 426 |
;; string-ci>=? |
| 427 |
;; |
| 428 |
|
| 429 |
(with-test-prefix "string-ci>=?" |
| 430 |
|
| 431 |
(pass-if "same as char-ci>=?" |
| 432 |
(eq? (char-ci>=? (integer->char 0) (integer->char 255)) |
| 433 |
(string-ci>=? (string-ints 0) (string-ints 255))))) |
| 434 |
|
| 435 |
;; |
| 436 |
;; Unicode string normalization forms |
| 437 |
;; |
| 438 |
|
| 439 |
;; |
| 440 |
;; string-normalize-nfd |
| 441 |
;; |
| 442 |
|
| 443 |
(with-test-prefix "string-normalize-nfd" |
| 444 |
|
| 445 |
(pass-if "canonical decomposition is equal?" |
| 446 |
(equal? (string-normalize-nfd "\xe9") "\x65\u0301"))) |
| 447 |
|
| 448 |
;; |
| 449 |
;; string-normalize-nfkd |
| 450 |
;; |
| 451 |
|
| 452 |
(with-test-prefix "string-normalize-nfkd" |
| 453 |
|
| 454 |
(pass-if "compatibility decomposition is equal?" |
| 455 |
(equal? (string-normalize-nfkd "\u1e9b\u0323") "s\u0323\u0307"))) |
| 456 |
|
| 457 |
;; |
| 458 |
;; string-normalize-nfc |
| 459 |
;; |
| 460 |
|
| 461 |
(with-test-prefix "string-normalize-nfc" |
| 462 |
|
| 463 |
(pass-if "canonical composition is equal?" |
| 464 |
(equal? (string-normalize-nfc "\x65\u0301") "\xe9"))) |
| 465 |
|
| 466 |
;; |
| 467 |
;; string-normalize-nfkc |
| 468 |
;; |
| 469 |
|
| 470 |
(with-test-prefix "string-normalize-nfkc" |
| 471 |
|
| 472 |
(pass-if "compatibility composition is equal?" |
| 473 |
(equal? (string-normalize-nfkc "\u1e9b\u0323") "\u1e69"))) |
| 474 |
|
| 475 |
;; |
| 476 |
;; normalizing large strings |
| 477 |
;; |
| 478 |
|
| 479 |
(pass-if "string-normalize-{nfd,nfc,nfkd,nfkc} on large strings" |
| 480 |
;; In Guile <= 2.2.4, these would overflow the C stack and crash. |
| 481 |
(let ((large (make-string 4000000 #\a))) |
| 482 |
(and (string=? large (string-normalize-nfd large)) |
| 483 |
(string=? large (string-normalize-nfc large)) |
| 484 |
(string=? large (string-normalize-nfkd large)) |
| 485 |
(string=? large (string-normalize-nfkc large))))) |
| 486 |
|
| 487 |
;; |
| 488 |
;; string-utf8-length |
| 489 |
;; |
| 490 |
|
| 491 |
(with-test-prefix "string-utf8-length" |
| 492 |
|
| 493 |
(pass-if-exception "wrong type argument" |
| 494 |
exception:wrong-type-arg |
| 495 |
(string-utf8-length 50)) |
| 496 |
|
| 497 |
(pass-if-equal 0 (string-utf8-length "")) |
| 498 |
(pass-if-equal 1 (string-utf8-length "\0")) |
| 499 |
(pass-if-equal 5 (string-utf8-length "hello")) |
| 500 |
(pass-if-equal 7 (string-utf8-length "helloλ")) |
| 501 |
(pass-if-equal 9 (string-utf8-length "ሠላም"))) |
| 502 |
|
| 503 |
;; |
| 504 |
;; string-ref |
| 505 |
;; |
| 506 |
|
| 507 |
(with-test-prefix/c&e "string-ref" |
| 508 |
|
| 509 |
(pass-if-exception "empty string" |
| 510 |
exception:out-of-range |
| 511 |
(string-ref "" 0)) |
| 512 |
|
| 513 |
(pass-if-exception "empty string and non-zero index" |
| 514 |
exception:out-of-range |
| 515 |
(string-ref "" 123)) |
| 516 |
|
| 517 |
(pass-if-exception "out of range" |
| 518 |
exception:out-of-range |
| 519 |
(string-ref "hello" 123)) |
| 520 |
|
| 521 |
(pass-if-equal "negative index" ;<https://bugs.gnu.org/60488> |
| 522 |
'(0 upper -1) |
| 523 |
(catch 'out-of-range |
| 524 |
(lambda () |
| 525 |
(string-ref "hello" -1)) |
| 526 |
(lambda args |
| 527 |
(let ((bounds+index (list-ref args 3))) |
| 528 |
;; At -O2, the out-of-range error is raised when converting |
| 529 |
;; "-1" to uint64, so the upper bound that's reported is |
| 530 |
;; UINT64_MAX. |
| 531 |
(list (list-ref bounds+index 0) 'upper |
| 532 |
(list-ref bounds+index 2)))))) |
| 533 |
|
| 534 |
(pass-if "regular string, ASCII char" |
| 535 |
(char=? (string-ref "GNU Guile" 4) #\G)) |
| 536 |
|
| 537 |
(pass-if "regular string, hex escaped Latin-1 char" |
| 538 |
(char=? (string-ref "--\xff--" 2) |
| 539 |
(integer->char #xff))) |
| 540 |
|
| 541 |
(pass-if "regular string, hex escaped BMP char" |
| 542 |
(char=? (string-ref "--\u0100--" 2) |
| 543 |
(integer->char #x0100))) |
| 544 |
|
| 545 |
(pass-if "regular string, hex escaped SMP char" |
| 546 |
(char=? (string-ref "--\U010300--" 2) |
| 547 |
(integer->char #x010300)))) |
| 548 |
|
| 549 |
;; |
| 550 |
;; string-set! |
| 551 |
;; |
| 552 |
|
| 553 |
(with-test-prefix "string-set!" |
| 554 |
|
| 555 |
(pass-if-exception "empty string" |
| 556 |
exception:out-of-range |
| 557 |
(string-set! (string-copy "") 0 #\x)) |
| 558 |
|
| 559 |
(pass-if-exception "empty string and non-zero index" |
| 560 |
exception:out-of-range |
| 561 |
(string-set! (string-copy "") 123 #\x)) |
| 562 |
|
| 563 |
(pass-if-exception "out of range" |
| 564 |
exception:out-of-range |
| 565 |
(string-set! (string-copy "hello") 123 #\x)) |
| 566 |
|
| 567 |
(pass-if-exception "negative index" |
| 568 |
exception:out-of-range |
| 569 |
(string-set! (string-copy "hello") -1 #\x)) |
| 570 |
|
| 571 |
(pass-if-exception "read-only string" |
| 572 |
exception:read-only-string |
| 573 |
(string-set! (substring/read-only "abc" 0) 1 #\space)) |
| 574 |
|
| 575 |
(pass-if "regular string, ASCII char" |
| 576 |
(let ((s (string-copy "GNU guile"))) |
| 577 |
(string-set! s 4 #\G) |
| 578 |
(char=? (string-ref s 4) #\G))) |
| 579 |
|
| 580 |
(pass-if "regular string, Latin-1 char" |
| 581 |
(let ((s (string-copy "GNU guile"))) |
| 582 |
(string-set! s 4 (integer->char #xfe)) |
| 583 |
(char=? (string-ref s 4) (integer->char #xfe)))) |
| 584 |
|
| 585 |
(pass-if "regular string, BMP char" |
| 586 |
(let ((s (string-copy "GNU guile"))) |
| 587 |
(string-set! s 4 (integer->char #x0100)) |
| 588 |
(char=? (string-ref s 4) (integer->char #x0100)))) |
| 589 |
|
| 590 |
(pass-if "regular string, SMP char" |
| 591 |
(let ((s (string-copy "GNU guile"))) |
| 592 |
(string-set! s 4 (integer->char #x010300)) |
| 593 |
(char=? (string-ref s 4) (integer->char #x010300))))) |
| 594 |
|
| 595 |
;; |
| 596 |
;; list->string |
| 597 |
;; |
| 598 |
(with-test-prefix "string" |
| 599 |
|
| 600 |
(pass-if-exception "convert circular list to string" |
| 601 |
'(wrong-type-arg . "Apply to non-list") |
| 602 |
(let ((foo (list #\a #\b #\c))) |
| 603 |
(set-cdr! (cddr foo) (cdr foo)) |
| 604 |
(apply string foo)))) |
| 605 |
|
| 606 |
(with-test-prefix "string-split" |
| 607 |
|
| 608 |
;; in guile 1.6.7 and earlier, character >=128 wasn't matched in the string |
| 609 |
(pass-if "char 255" |
| 610 |
(equal? '("a" "b") |
| 611 |
(string-split (string #\a (integer->char 255) #\b) |
| 612 |
(integer->char 255)))) |
| 613 |
|
| 614 |
(pass-if "empty string - char" |
| 615 |
(equal? '("") |
| 616 |
(string-split "" #\:))) |
| 617 |
|
| 618 |
(pass-if "non-empty - char - no delimiters" |
| 619 |
(equal? '("foobarfrob") |
| 620 |
(string-split "foobarfrob" #\:))) |
| 621 |
|
| 622 |
(pass-if "non-empty - char - delimiters" |
| 623 |
(equal? '("foo" "bar" "frob") |
| 624 |
(string-split "foo:bar:frob" #\:))) |
| 625 |
|
| 626 |
(pass-if "non-empty - char - leading delimiters" |
| 627 |
(equal? '("" "" "foo" "bar" "frob") |
| 628 |
(string-split "::foo:bar:frob" #\:))) |
| 629 |
|
| 630 |
(pass-if "non-empty - char - trailing delimiters" |
| 631 |
(equal? '("foo" "bar" "frob" "" "") |
| 632 |
(string-split "foo:bar:frob::" #\:))) |
| 633 |
|
| 634 |
(pass-if "empty string - charset" |
| 635 |
(equal? '("") |
| 636 |
(string-split "" (char-set #\:)))) |
| 637 |
|
| 638 |
(pass-if "non-empty - charset - no delimiters" |
| 639 |
(equal? '("foobarfrob") |
| 640 |
(string-split "foobarfrob" (char-set #\:)))) |
| 641 |
|
| 642 |
(pass-if "non-empty - charset - delimiters" |
| 643 |
(equal? '("foo" "bar" "frob") |
| 644 |
(string-split "foo:bar:frob" (char-set #\:)))) |
| 645 |
|
| 646 |
(pass-if "non-empty - charset - leading delimiters" |
| 647 |
(equal? '("" "" "foo" "bar" "frob") |
| 648 |
(string-split "::foo:bar:frob" (char-set #\:)))) |
| 649 |
|
| 650 |
(pass-if "non-empty - charset - trailing delimiters" |
| 651 |
(equal? '("foo" "bar" "frob" "" "") |
| 652 |
(string-split "foo:bar:frob::" (char-set #\:)))) |
| 653 |
|
| 654 |
(pass-if "empty string - pred" |
| 655 |
(equal? '("") |
| 656 |
(string-split "" (negate char-alphabetic?)))) |
| 657 |
|
| 658 |
(pass-if "non-empty - pred - no delimiters" |
| 659 |
(equal? '("foobarfrob") |
| 660 |
(string-split "foobarfrob" (negate char-alphabetic?)))) |
| 661 |
|
| 662 |
(pass-if "non-empty - pred - delimiters" |
| 663 |
(equal? '("foo" "bar" "frob") |
| 664 |
(string-split "foo:bar:frob" (negate char-alphabetic?)))) |
| 665 |
|
| 666 |
(pass-if "non-empty - pred - leading delimiters" |
| 667 |
(equal? '("" "" "foo" "bar" "frob") |
| 668 |
(string-split "::foo:bar:frob" (negate char-alphabetic?)))) |
| 669 |
|
| 670 |
(pass-if "non-empty - pred - trailing delimiters" |
| 671 |
(equal? '("foo" "bar" "frob" "" "") |
| 672 |
(string-split "foo:bar:frob::" (negate char-alphabetic?))))) |
| 673 |
|
| 674 |
(with-test-prefix "substring-move!" |
| 675 |
|
| 676 |
(pass-if-exception "substring-move! checks start and end correctly" |
| 677 |
exception:out-of-range |
| 678 |
(substring-move! "sample" 3 0 "test" 3))) |
| 679 |
|
| 680 |
(with-test-prefix "substring/shared" |
| 681 |
|
| 682 |
(pass-if "modify indirectly" |
| 683 |
(let ((str (string-copy "foofoofoo"))) |
| 684 |
(string-upcase! (substring/shared str 3 6)) |
| 685 |
(string=? str "fooFOOfoo"))) |
| 686 |
|
| 687 |
(pass-if "modify cow indirectly" |
| 688 |
(let* ((str1 (string-copy "foofoofoo")) |
| 689 |
(str2 (string-copy str1))) |
| 690 |
(string-upcase! (substring/shared str2 3 6)) |
| 691 |
(and (string=? str1 "foofoofoo") |
| 692 |
(string=? str2 "fooFOOfoo")))) |
| 693 |
|
| 694 |
(pass-if "modify double indirectly" |
| 695 |
(let* ((str1 (string-copy "foofoofoo")) |
| 696 |
(str2 (substring/shared str1 2 7))) |
| 697 |
(string-upcase! (substring/shared str2 1 4)) |
| 698 |
(string=? str1 "fooFOOfoo"))) |
| 699 |
|
| 700 |
(pass-if "modify cow double indirectly" |
| 701 |
(let* ((str1 "foofoofoo") |
| 702 |
(str2 (substring str1 2 7))) |
| 703 |
(string-upcase! (substring/shared str2 1 4)) |
| 704 |
(and (string=? str1 "foofoofoo") |
| 705 |
(string=? str2 "oFOOf"))))) |
| 706 |
|
| 707 |
(with-test-prefix "miscellaneous string functions" |
| 708 |
|
| 709 |
(pass-if "string-replace-substring" |
| 710 |
(string=? (string-replace-substring "a ring of strings" "ring" "rut") |
| 711 |
"a rut of struts"))) |