strings.test 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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, 2005, 2006, 2008 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This program is free software; you can redistribute it and/or modify
  7. ;;;; it under the terms of the GNU General Public License as published by
  8. ;;;; the Free Software Foundation; either version 2, or (at your option)
  9. ;;;; any later version.
  10. ;;;;
  11. ;;;; This program is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;;; GNU General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU General Public License
  17. ;;;; along with this software; see the file COPYING. If not, write to
  18. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. ;;;; Boston, MA 02110-1301 USA
  20. (define-module (test-strings)
  21. #:use-module (test-suite lib))
  22. (define exception:read-only-string
  23. (cons 'misc-error "^string is read-only"))
  24. ;; Create a string from integer char values, eg. (string-ints 65) => "A"
  25. (define (string-ints . args)
  26. (apply string (map integer->char args)))
  27. ;;
  28. ;; string=?
  29. ;;
  30. (with-test-prefix "string=?"
  31. (pass-if "respects 1st parameter's string length"
  32. (not (string=? "foo\0" "foo")))
  33. (pass-if "respects 2nd paramter's string length"
  34. (not (string=? "foo" "foo\0")))
  35. (with-test-prefix "wrong argument type"
  36. (pass-if-exception "1st argument symbol"
  37. exception:wrong-type-arg
  38. (string=? 'a "a"))
  39. (pass-if-exception "2nd argument symbol"
  40. exception:wrong-type-arg
  41. (string=? "a" 'b))))
  42. ;;
  43. ;; string<?
  44. ;;
  45. (with-test-prefix "string<?"
  46. (pass-if "respects string length"
  47. (and (not (string<? "foo\0a" "foo\0a"))
  48. (string<? "foo\0a" "foo\0b")))
  49. (with-test-prefix "wrong argument type"
  50. (pass-if-exception "1st argument symbol"
  51. exception:wrong-type-arg
  52. (string<? 'a "a"))
  53. (pass-if-exception "2nd argument symbol"
  54. exception:wrong-type-arg
  55. (string<? "a" 'b)))
  56. (pass-if "same as char<?"
  57. (eq? (char<? (integer->char 0) (integer->char 255))
  58. (string<? (string-ints 0) (string-ints 255)))))
  59. ;;
  60. ;; string-ci<?
  61. ;;
  62. (with-test-prefix "string-ci<?"
  63. (pass-if "respects string length"
  64. (and (not (string-ci<? "foo\0a" "foo\0a"))
  65. (string-ci<? "foo\0a" "foo\0b")))
  66. (with-test-prefix "wrong argument type"
  67. (pass-if-exception "1st argument symbol"
  68. exception:wrong-type-arg
  69. (string-ci<? 'a "a"))
  70. (pass-if-exception "2nd argument symbol"
  71. exception:wrong-type-arg
  72. (string-ci<? "a" 'b)))
  73. (pass-if "same as char-ci<?"
  74. (eq? (char-ci<? (integer->char 0) (integer->char 255))
  75. (string-ci<? (string-ints 0) (string-ints 255)))))
  76. ;;
  77. ;; string<=?
  78. ;;
  79. (with-test-prefix "string<=?"
  80. (pass-if "same as char<=?"
  81. (eq? (char<=? (integer->char 0) (integer->char 255))
  82. (string<=? (string-ints 0) (string-ints 255)))))
  83. ;;
  84. ;; string-ci<=?
  85. ;;
  86. (with-test-prefix "string-ci<=?"
  87. (pass-if "same as char-ci<=?"
  88. (eq? (char-ci<=? (integer->char 0) (integer->char 255))
  89. (string-ci<=? (string-ints 0) (string-ints 255)))))
  90. ;;
  91. ;; string>?
  92. ;;
  93. (with-test-prefix "string>?"
  94. (pass-if "same as char>?"
  95. (eq? (char>? (integer->char 0) (integer->char 255))
  96. (string>? (string-ints 0) (string-ints 255)))))
  97. ;;
  98. ;; string-ci>?
  99. ;;
  100. (with-test-prefix "string-ci>?"
  101. (pass-if "same as char-ci>?"
  102. (eq? (char-ci>? (integer->char 0) (integer->char 255))
  103. (string-ci>? (string-ints 0) (string-ints 255)))))
  104. ;;
  105. ;; string>=?
  106. ;;
  107. (with-test-prefix "string>=?"
  108. (pass-if "same as char>=?"
  109. (eq? (char>=? (integer->char 0) (integer->char 255))
  110. (string>=? (string-ints 0) (string-ints 255)))))
  111. ;;
  112. ;; string-ci>=?
  113. ;;
  114. (with-test-prefix "string-ci>=?"
  115. (pass-if "same as char-ci>=?"
  116. (eq? (char-ci>=? (integer->char 0) (integer->char 255))
  117. (string-ci>=? (string-ints 0) (string-ints 255)))))
  118. ;;
  119. ;; string-ref
  120. ;;
  121. (with-test-prefix "string-ref"
  122. (pass-if-exception "empty string"
  123. exception:out-of-range
  124. (string-ref "" 0))
  125. (pass-if-exception "empty string and non-zero index"
  126. exception:out-of-range
  127. (string-ref "" 123))
  128. (pass-if-exception "out of range"
  129. exception:out-of-range
  130. (string-ref "hello" 123))
  131. (pass-if-exception "negative index"
  132. exception:out-of-range
  133. (string-ref "hello" -1))
  134. (pass-if "regular string"
  135. (char=? (string-ref "GNU Guile" 4) #\G)))
  136. ;;
  137. ;; string-set!
  138. ;;
  139. (with-test-prefix "string-set!"
  140. (pass-if-exception "empty string"
  141. exception:out-of-range
  142. (string-set! (string-copy "") 0 #\x))
  143. (pass-if-exception "empty string and non-zero index"
  144. exception:out-of-range
  145. (string-set! (string-copy "") 123 #\x))
  146. (pass-if-exception "out of range"
  147. exception:out-of-range
  148. (string-set! (string-copy "hello") 123 #\x))
  149. (pass-if-exception "negative index"
  150. exception:out-of-range
  151. (string-set! (string-copy "hello") -1 #\x))
  152. (pass-if-exception "read-only string"
  153. exception:read-only-string
  154. (string-set! (substring/read-only "abc" 0) 1 #\space))
  155. (pass-if "regular string"
  156. (let ((s (string-copy "GNU guile")))
  157. (string-set! s 4 #\G)
  158. (char=? (string-ref s 4) #\G))))
  159. (with-test-prefix "string-split"
  160. ;; in guile 1.6.7 and earlier, character >=128 wasn't matched in the string
  161. (pass-if "char 255"
  162. (equal? '("a" "b")
  163. (string-split (string #\a (integer->char 255) #\b)
  164. (integer->char 255)))))
  165. (with-test-prefix "substring-move!"
  166. (pass-if-exception "substring-move! checks start and end correctly"
  167. exception:out-of-range
  168. (substring-move! "sample" 3 0 "test" 3)))
  169. (with-test-prefix "substring/shared"
  170. (pass-if "modify indirectly"
  171. (let ((str (string-copy "foofoofoo")))
  172. (string-upcase! (substring/shared str 3 6))
  173. (string=? str "fooFOOfoo")))
  174. (pass-if "modify cow indirectly"
  175. (let* ((str1 (string-copy "foofoofoo"))
  176. (str2 (string-copy str1)))
  177. (string-upcase! (substring/shared str2 3 6))
  178. (and (string=? str1 "foofoofoo")
  179. (string=? str2 "fooFOOfoo"))))
  180. (pass-if "modify double indirectly"
  181. (let* ((str1 (string-copy "foofoofoo"))
  182. (str2 (substring/shared str1 2 7)))
  183. (string-upcase! (substring/shared str2 1 4))
  184. (string=? str1 "fooFOOfoo")))
  185. (pass-if "modify cow double indirectly"
  186. (let* ((str1 "foofoofoo")
  187. (str2 (substring str1 2 7)))
  188. (string-upcase! (substring/shared str2 1 4))
  189. (and (string=? str1 "foofoofoo")
  190. (string=? str2 "oFOOf")))))