strings.test 20 KB

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