strings.test 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. ;;;; strings.test --- test suite for Guile's string functions -*- scheme -*-
  2. ;;;; Jim Blandy <jimb@red-bean.com> --- August 1999
  3. ;;;;
  4. ;;;; Copyright (C) 1999, 2001, 2004-2006, 2008-2011, 2013,
  5. ;;;; 2015 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. (define-module (test-strings)
  21. #:use-module ((system base compile) #:select (compile))
  22. #:use-module (test-suite lib))
  23. (define exception:read-only-string
  24. (cons 'misc-error "^string is read-only"))
  25. (define exception:illegal-escape
  26. (cons 'read-error "illegal character in escape sequence"))
  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"))
  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. ;; string internals
  36. ;;
  37. ;; Some abbreviations
  38. ;; BMP - Basic Multilingual Plane (codepoints below U+FFFF)
  39. ;; SMP - Suplementary Multilingual Plane (codebpoints from U+10000 to U+1FFFF)
  40. (with-test-prefix "string internals"
  41. (pass-if "new string starts at 1st char in stringbuf"
  42. (let ((s "abc"))
  43. (= 0 (assq-ref (%string-dump s) 'start))))
  44. (pass-if "length of new string same as stringbuf"
  45. (let ((s "def"))
  46. (= (string-length s) (assq-ref (%string-dump s) 'stringbuf-length))))
  47. (pass-if "contents of new string same as stringbuf"
  48. (let ((s "ghi"))
  49. (string=? s (assq-ref (%string-dump s) 'stringbuf-chars))))
  50. (pass-if "writable strings are not read-only"
  51. (let ((s "zyx"))
  52. (not (assq-ref (%string-dump s) 'read-only))))
  53. (pass-if "read-only strings are read-only"
  54. (let ((s (substring/read-only "zyx" 0)))
  55. (assq-ref (%string-dump s) 'read-only)))
  56. (pass-if "new Latin-1 encoded strings are not shared"
  57. (let ((s "abc"))
  58. (not (assq-ref (%string-dump s) 'stringbuf-shared))))
  59. (pass-if "new UCS-4 encoded strings are not shared"
  60. (let ((s "\u0100bc"))
  61. (not (assq-ref (%string-dump s) 'stringbuf-shared))))
  62. ;; Should this be true? It isn't currently true.
  63. (pass-if "null shared substrings are shared"
  64. (let* ((s1 "")
  65. (s2 (substring/shared s1 0 0)))
  66. (throw 'untested)
  67. (eq? (assq-ref (%string-dump s2) 'shared)
  68. s1)))
  69. (pass-if "ASCII shared substrings are shared"
  70. (let* ((s1 "foobar")
  71. (s2 (substring/shared s1 0 3)))
  72. (eq? (assq-ref (%string-dump s2) 'shared)
  73. s1)))
  74. (pass-if "BMP shared substrings are shared"
  75. (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
  76. (s2 (substring/shared s1 0 3)))
  77. (eq? (assq-ref (%string-dump s2) 'shared)
  78. s1)))
  79. (pass-if "null substrings are not shared"
  80. (let* ((s1 "")
  81. (s2 (substring s1 0 0)))
  82. (not (eq? (assq-ref (%string-dump s2) 'shared)
  83. s1))))
  84. (pass-if "ASCII substrings are not shared"
  85. (let* ((s1 "foobar")
  86. (s2 (substring s1 0 3)))
  87. (not (eq? (assq-ref (%string-dump s2) 'shared)
  88. s1))))
  89. (pass-if "BMP substrings are not shared"
  90. (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
  91. (s2 (substring s1 0 3)))
  92. (not (eq? (assq-ref (%string-dump s2) 'shared)
  93. s1))))
  94. (pass-if "ASCII substrings immutable before copy-on-write"
  95. (let* ((s1 "foobar")
  96. (s2 (substring s1 0 3)))
  97. (and (not (assq-ref (%string-dump s1) 'stringbuf-mutable))
  98. (not (assq-ref (%string-dump s2) 'stringbuf-mutable)))))
  99. (pass-if "BMP substrings immutable before copy-on-write"
  100. (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
  101. (s2 (substring s1 0 3)))
  102. (and (not (assq-ref (%string-dump s1) 'stringbuf-mutable))
  103. (not (assq-ref (%string-dump s2) 'stringbuf-mutable)))))
  104. (pass-if "ASCII base string still immutable after copy-on-write"
  105. (let* ((s1 "foobar")
  106. (s2 (substring s1 0 3)))
  107. (string-set! s2 0 #\F)
  108. (and (not (assq-ref (%string-dump s1) 'stringbuf-mutable))
  109. (assq-ref (%string-dump s2) 'stringbuf-mutable))))
  110. (pass-if "BMP base string still immutable after copy-on-write"
  111. (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
  112. (s2 (substring s1 0 3)))
  113. (string-set! s2 0 #\F)
  114. (and (not (assq-ref (%string-dump s1) 'stringbuf-mutable))
  115. (assq-ref (%string-dump s2) 'stringbuf-mutable))))
  116. (pass-if "ASCII substrings mutable after shared mutation"
  117. (let* ((s1 "foobar")
  118. (s2 (substring/shared s1 0 3)))
  119. (string-set! s2 0 #\F)
  120. (and (assq-ref (%string-dump s1) 'stringbuf-mutable)
  121. (assq-ref (%string-dump s2) 'stringbuf-mutable))))
  122. (pass-if "BMP substrings mutable after shared mutation"
  123. (let* ((s1 "\u0100\u0101\u0102\u0103\u0104\u0105")
  124. (s2 (substring/shared s1 0 3)))
  125. (string-set! s2 0 #\F)
  126. (and (assq-ref (%string-dump s1) 'stringbuf-mutable)
  127. (assq-ref (%string-dump s2) 'stringbuf-mutable))))
  128. (with-test-prefix "encodings"
  129. (pass-if "null strings are Latin-1 encoded"
  130. (let ((s ""))
  131. (not (assq-ref (%string-dump s) 'stringbuf-wide))))
  132. (pass-if "ASCII strings are Latin-1 encoded"
  133. (let ((s "jkl"))
  134. (not (assq-ref (%string-dump s) 'stringbuf-wide))))
  135. (pass-if "Latin-1 strings are Latin-1 encoded"
  136. (let ((s "\xC0\xC1\xC2"))
  137. (not (assq-ref (%string-dump s) 'stringbuf-wide))))
  138. (pass-if "BMP strings are UCS-4 encoded"
  139. (let ((s "\u0100\u0101\x0102"))
  140. (assq-ref (%string-dump s) 'stringbuf-wide)))
  141. (pass-if "SMP strings are UCS-4 encoded"
  142. (let ((s "\U010300\u010301\x010302"))
  143. (assq-ref (%string-dump s) 'stringbuf-wide)))
  144. (pass-if "null list->string is Latin-1 encoded"
  145. (let ((s (string-ints)))
  146. (not (assq-ref (%string-dump s) 'stringbuf-wide))))
  147. (pass-if "ASCII list->string is Latin-1 encoded"
  148. (let ((s (string-ints 65 66 67)))
  149. (not (assq-ref (%string-dump s) 'stringbuf-wide))))
  150. (pass-if "Latin-1 list->string is Latin-1 encoded"
  151. (let ((s (string-ints #xc0 #xc1 #xc2)))
  152. (not (assq-ref (%string-dump s) 'stringbuf-wide))))
  153. (pass-if "BMP list->string is UCS-4 encoded"
  154. (let ((s (string-ints #x0100 #x0101 #x0102)))
  155. (assq-ref (%string-dump s) 'stringbuf-wide)))
  156. (pass-if "SMP list->string is UCS-4 encoded"
  157. (let ((s (string-ints #x010300 #x010301 #x010302)))
  158. (assq-ref (%string-dump s) 'stringbuf-wide)))
  159. (pass-if "encoding of string not based on escape style"
  160. (let ((s "\U000040"))
  161. (not (assq-ref (%string-dump s) 'stringbuf-wide))))))
  162. (with-test-prefix "escapes"
  163. (pass-if-exception "non-hex char in two-digit hex-escape"
  164. exception:illegal-escape
  165. (with-input-from-string "\"\\x0g\"" read))
  166. (pass-if-exception "non-hex char in four-digit hex-escape"
  167. exception:illegal-escape
  168. (with-input-from-string "\"\\u000g\"" read))
  169. (pass-if-exception "non-hex char in six-digit hex-escape"
  170. exception:illegal-escape
  171. (with-input-from-string "\"\\U00000g\"" read))
  172. (pass-if-exception "premature termination of two-digit hex-escape"
  173. exception:illegal-escape
  174. (with-input-from-string "\"\\x0\"" read))
  175. (pass-if-exception "premature termination of four-digit hex-escape"
  176. exception:illegal-escape
  177. (with-input-from-string "\"\\u000\"" read))
  178. (pass-if-exception "premature termination of six-digit hex-escape"
  179. exception:illegal-escape
  180. (with-input-from-string "\"\\U00000\"" read))
  181. (pass-if "extra hex digits ignored for two-digit hex escape"
  182. (eqv? (string-ref "--\xfff--" 2)
  183. (integer->char #xff)))
  184. (pass-if "extra hex digits ignored for four-digit hex escape"
  185. (eqv? (string-ref "--\u0100f--" 2)
  186. (integer->char #x0100)))
  187. (pass-if "extra hex digits ignored for six-digit hex escape"
  188. (eqv? (string-ref "--\U010300f--" 2)
  189. (integer->char #x010300)))
  190. (pass-if "escaped characters match non-escaped ASCII characters"
  191. (string=? "ABC" "\x41\u0042\U000043"))
  192. (pass-if "R5RS backslash escapes"
  193. (string=? "\"\\" (string #\" #\\)))
  194. (pass-if "R6RS backslash escapes"
  195. (string=? "\a\b\t\n\v\f\r"
  196. (string #\alarm #\backspace #\tab #\newline #\vtab
  197. #\page #\return)))
  198. (pass-if "Guile extensions backslash escapes"
  199. (string=? "\0" (string #\nul))))
  200. ;;
  201. ;; string?
  202. ;;
  203. (with-test-prefix "string?"
  204. (pass-if "string"
  205. (string? "abc"))
  206. (pass-if "symbol"
  207. (not (string? 'abc))))
  208. ;;
  209. ;; literals
  210. ;;
  211. (with-test-prefix "literals"
  212. ;; The "Storage Model" section of R5RS reads: "In such systems literal
  213. ;; constants and the strings returned by `symbol->string' are
  214. ;; immutable objects". `eval' doesn't support it yet, but it doesn't
  215. ;; really matter because `eval' doesn't coalesce repeated constants,
  216. ;; unlike the bytecode compiler.
  217. (pass-if-exception "literals are constant"
  218. exception:read-only-string
  219. (compile '(string-set! "literal string" 0 #\x)
  220. #:from 'scheme
  221. #:to 'value)))
  222. ;;
  223. ;; string-null?
  224. ;;
  225. (with-test-prefix "string-null?"
  226. (pass-if "null string"
  227. (string-null? ""))
  228. (pass-if "non-null string"
  229. (not (string-null? "a")))
  230. (pass-if "respects \\0"
  231. (not (string-null? "\0")))
  232. (pass-if-exception "symbol"
  233. exception:wrong-type-arg
  234. (string-null? 'a)))
  235. ;;
  236. ;; string=?
  237. ;;
  238. (with-test-prefix "string=?"
  239. (pass-if "respects 1st parameter's string length"
  240. (not (string=? "foo\0" "foo")))
  241. (pass-if "respects 2nd paramter's string length"
  242. (not (string=? "foo" "foo\0")))
  243. (with-test-prefix "wrong argument type"
  244. (pass-if-exception "1st argument symbol"
  245. exception:wrong-type-arg
  246. (string=? 'a "a"))
  247. (pass-if-exception "2nd argument symbol"
  248. exception:wrong-type-arg
  249. (string=? "a" 'b))
  250. (pass-if-exception "1st argument EOF"
  251. exception:wrong-type-arg
  252. (string=? (with-input-from-string "" read) "b"))
  253. (pass-if-exception "2nd argument EOF"
  254. exception:wrong-type-arg
  255. (string=? "a" (with-input-from-string "" read)))))
  256. ;;
  257. ;; string<?
  258. ;;
  259. (with-test-prefix "string<?"
  260. (pass-if "respects string length"
  261. (and (not (string<? "foo\0a" "foo\0a"))
  262. (string<? "foo\0a" "foo\0b")))
  263. (with-test-prefix "wrong argument type"
  264. (pass-if-exception "1st argument symbol"
  265. exception:wrong-type-arg
  266. (string<? 'a "a"))
  267. (pass-if-exception "2nd argument symbol"
  268. exception:wrong-type-arg
  269. (string<? "a" 'b)))
  270. (pass-if "same as char<?"
  271. (eq? (char<? (integer->char 0) (integer->char 255))
  272. (string<? (string-ints 0) (string-ints 255)))))
  273. ;;
  274. ;; string-ci<?
  275. ;;
  276. (with-test-prefix "string-ci<?"
  277. (pass-if "respects string length"
  278. (and (not (string-ci<? "foo\0a" "foo\0a"))
  279. (string-ci<? "foo\0a" "foo\0b")))
  280. (with-test-prefix "wrong argument type"
  281. (pass-if-exception "1st argument symbol"
  282. exception:wrong-type-arg
  283. (string-ci<? 'a "a"))
  284. (pass-if-exception "2nd argument symbol"
  285. exception:wrong-type-arg
  286. (string-ci<? "a" 'b)))
  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. ;; string<=?
  292. ;;
  293. (with-test-prefix "string<=?"
  294. (pass-if "same as char<=?"
  295. (eq? (char<=? (integer->char 0) (integer->char 255))
  296. (string<=? (string-ints 0) (string-ints 255)))))
  297. ;;
  298. ;; string-ci<=?
  299. ;;
  300. (with-test-prefix "string-ci<=?"
  301. (pass-if "same as char-ci<=?"
  302. (eq? (char-ci<=? (integer->char 0) (integer->char 255))
  303. (string-ci<=? (string-ints 0) (string-ints 255)))))
  304. ;;
  305. ;; string>?
  306. ;;
  307. (with-test-prefix "string>?"
  308. (pass-if "same as char>?"
  309. (eq? (char>? (integer->char 0) (integer->char 255))
  310. (string>? (string-ints 0) (string-ints 255)))))
  311. ;;
  312. ;; string-ci>?
  313. ;;
  314. (with-test-prefix "string-ci>?"
  315. (pass-if "same as char-ci>?"
  316. (eq? (char-ci>? (integer->char 0) (integer->char 255))
  317. (string-ci>? (string-ints 0) (string-ints 255)))))
  318. ;;
  319. ;; string>=?
  320. ;;
  321. (with-test-prefix "string>=?"
  322. (pass-if "same as char>=?"
  323. (eq? (char>=? (integer->char 0) (integer->char 255))
  324. (string>=? (string-ints 0) (string-ints 255)))))
  325. ;;
  326. ;; string-ci>=?
  327. ;;
  328. (with-test-prefix "string-ci>=?"
  329. (pass-if "same as char-ci>=?"
  330. (eq? (char-ci>=? (integer->char 0) (integer->char 255))
  331. (string-ci>=? (string-ints 0) (string-ints 255)))))
  332. ;;
  333. ;; Unicode string normalization forms
  334. ;;
  335. ;;
  336. ;; string-normalize-nfd
  337. ;;
  338. (with-test-prefix "string-normalize-nfd"
  339. (pass-if "canonical decomposition is equal?"
  340. (equal? (string-normalize-nfd "\xe9") "\x65\u0301")))
  341. ;;
  342. ;; string-normalize-nfkd
  343. ;;
  344. (with-test-prefix "string-normalize-nfkd"
  345. (pass-if "compatibility decomposition is equal?"
  346. (equal? (string-normalize-nfkd "\u1e9b\u0323") "s\u0323\u0307")))
  347. ;;
  348. ;; string-normalize-nfc
  349. ;;
  350. (with-test-prefix "string-normalize-nfc"
  351. (pass-if "canonical composition is equal?"
  352. (equal? (string-normalize-nfc "\x65\u0301") "\xe9")))
  353. ;;
  354. ;; string-normalize-nfkc
  355. ;;
  356. (with-test-prefix "string-normalize-nfkc"
  357. (pass-if "compatibility composition is equal?"
  358. (equal? (string-normalize-nfkc "\u1e9b\u0323") "\u1e69")))
  359. ;;
  360. ;; string-utf8-length
  361. ;;
  362. (with-test-prefix "string-utf8-length"
  363. (pass-if-exception "wrong type argument"
  364. exception:wrong-type-arg
  365. (string-utf8-length 50))
  366. (pass-if-equal 0 (string-utf8-length ""))
  367. (pass-if-equal 1 (string-utf8-length "\0"))
  368. (pass-if-equal 5 (string-utf8-length "hello"))
  369. (pass-if-equal 7 (string-utf8-length "helloλ"))
  370. (pass-if-equal 9 (string-utf8-length "ሠላም")))
  371. ;;
  372. ;; string-ref
  373. ;;
  374. (with-test-prefix "string-ref"
  375. (pass-if-exception "empty string"
  376. exception:out-of-range
  377. (string-ref "" 0))
  378. (pass-if-exception "empty string and non-zero index"
  379. exception:out-of-range
  380. (string-ref "" 123))
  381. (pass-if-exception "out of range"
  382. exception:out-of-range
  383. (string-ref "hello" 123))
  384. (pass-if-exception "negative index"
  385. exception:out-of-range
  386. (string-ref "hello" -1))
  387. (pass-if "regular string, ASCII char"
  388. (char=? (string-ref "GNU Guile" 4) #\G))
  389. (pass-if "regular string, hex escaped Latin-1 char"
  390. (char=? (string-ref "--\xff--" 2)
  391. (integer->char #xff)))
  392. (pass-if "regular string, hex escaped BMP char"
  393. (char=? (string-ref "--\u0100--" 2)
  394. (integer->char #x0100)))
  395. (pass-if "regular string, hex escaped SMP char"
  396. (char=? (string-ref "--\U010300--" 2)
  397. (integer->char #x010300))))
  398. ;;
  399. ;; string-set!
  400. ;;
  401. (with-test-prefix "string-set!"
  402. (pass-if-exception "empty string"
  403. exception:out-of-range
  404. (string-set! (string-copy "") 0 #\x))
  405. (pass-if-exception "empty string and non-zero index"
  406. exception:out-of-range
  407. (string-set! (string-copy "") 123 #\x))
  408. (pass-if-exception "out of range"
  409. exception:out-of-range
  410. (string-set! (string-copy "hello") 123 #\x))
  411. (pass-if-exception "negative index"
  412. exception:out-of-range
  413. (string-set! (string-copy "hello") -1 #\x))
  414. (pass-if-exception "read-only string"
  415. exception:read-only-string
  416. (string-set! (substring/read-only "abc" 0) 1 #\space))
  417. (pass-if "regular string, ASCII char"
  418. (let ((s (string-copy "GNU guile")))
  419. (string-set! s 4 #\G)
  420. (char=? (string-ref s 4) #\G)))
  421. (pass-if "regular string, Latin-1 char"
  422. (let ((s (string-copy "GNU guile")))
  423. (string-set! s 4 (integer->char #xfe))
  424. (char=? (string-ref s 4) (integer->char #xfe))))
  425. (pass-if "regular string, BMP char"
  426. (let ((s (string-copy "GNU guile")))
  427. (string-set! s 4 (integer->char #x0100))
  428. (char=? (string-ref s 4) (integer->char #x0100))))
  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)))))
  433. ;;
  434. ;; list->string
  435. ;;
  436. (with-test-prefix "string"
  437. (pass-if-exception "convert circular list to string"
  438. '(wrong-type-arg . "Apply to non-list")
  439. (let ((foo (list #\a #\b #\c)))
  440. (set-cdr! (cddr foo) (cdr foo))
  441. (apply string foo))))
  442. (with-test-prefix "string-split"
  443. ;; in guile 1.6.7 and earlier, character >=128 wasn't matched in the string
  444. (pass-if "char 255"
  445. (equal? '("a" "b")
  446. (string-split (string #\a (integer->char 255) #\b)
  447. (integer->char 255))))
  448. (pass-if "empty string - char"
  449. (equal? '("")
  450. (string-split "" #\:)))
  451. (pass-if "non-empty - char - no delimiters"
  452. (equal? '("foobarfrob")
  453. (string-split "foobarfrob" #\:)))
  454. (pass-if "non-empty - char - delimiters"
  455. (equal? '("foo" "bar" "frob")
  456. (string-split "foo:bar:frob" #\:)))
  457. (pass-if "non-empty - char - leading delimiters"
  458. (equal? '("" "" "foo" "bar" "frob")
  459. (string-split "::foo:bar:frob" #\:)))
  460. (pass-if "non-empty - char - trailing delimiters"
  461. (equal? '("foo" "bar" "frob" "" "")
  462. (string-split "foo:bar:frob::" #\:)))
  463. (pass-if "empty string - charset"
  464. (equal? '("")
  465. (string-split "" (char-set #\:))))
  466. (pass-if "non-empty - charset - no delimiters"
  467. (equal? '("foobarfrob")
  468. (string-split "foobarfrob" (char-set #\:))))
  469. (pass-if "non-empty - charset - delimiters"
  470. (equal? '("foo" "bar" "frob")
  471. (string-split "foo:bar:frob" (char-set #\:))))
  472. (pass-if "non-empty - charset - leading delimiters"
  473. (equal? '("" "" "foo" "bar" "frob")
  474. (string-split "::foo:bar:frob" (char-set #\:))))
  475. (pass-if "non-empty - charset - trailing delimiters"
  476. (equal? '("foo" "bar" "frob" "" "")
  477. (string-split "foo:bar:frob::" (char-set #\:))))
  478. (pass-if "empty string - pred"
  479. (equal? '("")
  480. (string-split "" (negate char-alphabetic?))))
  481. (pass-if "non-empty - pred - no delimiters"
  482. (equal? '("foobarfrob")
  483. (string-split "foobarfrob" (negate char-alphabetic?))))
  484. (pass-if "non-empty - pred - delimiters"
  485. (equal? '("foo" "bar" "frob")
  486. (string-split "foo:bar:frob" (negate char-alphabetic?))))
  487. (pass-if "non-empty - pred - leading delimiters"
  488. (equal? '("" "" "foo" "bar" "frob")
  489. (string-split "::foo:bar:frob" (negate char-alphabetic?))))
  490. (pass-if "non-empty - pred - trailing delimiters"
  491. (equal? '("foo" "bar" "frob" "" "")
  492. (string-split "foo:bar:frob::" (negate char-alphabetic?)))))
  493. (with-test-prefix "substring-move!"
  494. (pass-if-exception "substring-move! checks start and end correctly"
  495. exception:out-of-range
  496. (substring-move! "sample" 3 0 "test" 3)))
  497. (with-test-prefix "substring/shared"
  498. (pass-if "modify indirectly"
  499. (let ((str (string-copy "foofoofoo")))
  500. (string-upcase! (substring/shared str 3 6))
  501. (string=? str "fooFOOfoo")))
  502. (pass-if "modify cow indirectly"
  503. (let* ((str1 (string-copy "foofoofoo"))
  504. (str2 (string-copy str1)))
  505. (string-upcase! (substring/shared str2 3 6))
  506. (and (string=? str1 "foofoofoo")
  507. (string=? str2 "fooFOOfoo"))))
  508. (pass-if "modify double indirectly"
  509. (let* ((str1 (string-copy "foofoofoo"))
  510. (str2 (substring/shared str1 2 7)))
  511. (string-upcase! (substring/shared str2 1 4))
  512. (string=? str1 "fooFOOfoo")))
  513. (pass-if "modify cow double indirectly"
  514. (let* ((str1 "foofoofoo")
  515. (str2 (substring str1 2 7)))
  516. (string-upcase! (substring/shared str2 1 4))
  517. (and (string=? str1 "foofoofoo")
  518. (string=? str2 "oFOOf")))))