strings.test 20 KB

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