srfi-14.test 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. ;;;; srfi-14.test --- Test suite for Guile's SRFI-14 functions.
  2. ;;;; Martin Grabmueller, 2001-07-16
  3. ;;;;
  4. ;;;; Copyright (C) 2001, 2006 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-suite test-srfi-14)
  21. :use-module (srfi srfi-14)
  22. :use-module (srfi srfi-1) ;; `every'
  23. :use-module (test-suite lib))
  24. (define exception:invalid-char-set-cursor
  25. (cons 'misc-error "^invalid character set cursor"))
  26. (define exception:non-char-return
  27. (cons 'misc-error "returned non-char"))
  28. (with-test-prefix "char-set?"
  29. (pass-if "success on empty set"
  30. (char-set? (char-set)))
  31. (pass-if "success on non-empty set"
  32. (char-set? char-set:printing))
  33. (pass-if "failure on empty set"
  34. (not (char-set? #t))))
  35. (with-test-prefix "char-set="
  36. (pass-if "success, no arg"
  37. (char-set=))
  38. (pass-if "success, one arg"
  39. (char-set= char-set:lower-case))
  40. (pass-if "success, two args"
  41. (char-set= char-set:upper-case char-set:upper-case))
  42. (pass-if "failure, first empty"
  43. (not (char-set= (char-set) (char-set #\a))))
  44. (pass-if "failure, second empty"
  45. (not (char-set= (char-set #\a) (char-set))))
  46. (pass-if "success, more args"
  47. (char-set= char-set:blank char-set:blank char-set:blank)))
  48. (with-test-prefix "char-set<="
  49. (pass-if "success, no arg"
  50. (char-set<=))
  51. (pass-if "success, one arg"
  52. (char-set<= char-set:lower-case))
  53. (pass-if "success, two args"
  54. (char-set<= char-set:upper-case char-set:upper-case))
  55. (pass-if "success, first empty"
  56. (char-set<= (char-set) (char-set #\a)))
  57. (pass-if "failure, second empty"
  58. (not (char-set<= (char-set #\a) (char-set))))
  59. (pass-if "success, more args, equal"
  60. (char-set<= char-set:blank char-set:blank char-set:blank))
  61. (pass-if "success, more args, not equal"
  62. (char-set<= char-set:blank
  63. (char-set-adjoin char-set:blank #\F)
  64. (char-set-adjoin char-set:blank #\F #\o))))
  65. (with-test-prefix "char-set-hash"
  66. (pass-if "empty set, bound"
  67. (let ((h (char-set-hash char-set:empty 31)))
  68. (and h (number? h) (exact? h) (>= h 0) (< h 31))))
  69. (pass-if "empty set, no bound"
  70. (let ((h (char-set-hash char-set:empty)))
  71. (and h (number? h) (exact? h) (>= h 0))))
  72. (pass-if "full set, bound"
  73. (let ((h (char-set-hash char-set:full 31)))
  74. (and h (number? h) (exact? h) (>= h 0) (< h 31))))
  75. (pass-if "full set, no bound"
  76. (let ((h (char-set-hash char-set:full)))
  77. (and h (number? h) (exact? h) (>= h 0))))
  78. (pass-if "other set, bound"
  79. (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r) 31)))
  80. (and h (number? h) (exact? h) (>= h 0) (< h 31))))
  81. (pass-if "other set, no bound"
  82. (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r))))
  83. (and h (number? h) (exact? h) (>= h 0)))))
  84. (with-test-prefix "char-set cursor"
  85. (pass-if-exception "invalid character cursor"
  86. exception:invalid-char-set-cursor
  87. (let* ((cs (char-set #\B #\r #\a #\z))
  88. (cc (char-set-cursor cs)))
  89. (char-set-ref cs 1000)))
  90. (pass-if "success"
  91. (let* ((cs (char-set #\B #\r #\a #\z))
  92. (cc (char-set-cursor cs)))
  93. (char? (char-set-ref cs cc))))
  94. (pass-if "end of set fails"
  95. (let* ((cs (char-set #\a))
  96. (cc (char-set-cursor cs)))
  97. (not (end-of-char-set? cc))))
  98. (pass-if "end of set succeeds, empty set"
  99. (let* ((cs (char-set))
  100. (cc (char-set-cursor cs)))
  101. (end-of-char-set? cc)))
  102. (pass-if "end of set succeeds, non-empty set"
  103. (let* ((cs (char-set #\a))
  104. (cc (char-set-cursor cs))
  105. (cc (char-set-cursor-next cs cc)))
  106. (end-of-char-set? cc))))
  107. (with-test-prefix "char-set-fold"
  108. (pass-if "count members"
  109. (= (char-set-fold (lambda (c n) (+ n 1)) 0 (char-set #\a #\b)) 2))
  110. (pass-if "copy set"
  111. (= (char-set-size (char-set-fold (lambda (c cs) (char-set-adjoin cs c))
  112. (char-set) (char-set #\a #\b))) 2)))
  113. (with-test-prefix "char-set-unfold"
  114. (pass-if "create char set"
  115. (char-set= char-set:full
  116. (char-set-unfold (lambda (s) (= s 256)) integer->char
  117. (lambda (s) (+ s 1)) 0)))
  118. (pass-if "create char set (base set)"
  119. (char-set= char-set:full
  120. (char-set-unfold (lambda (s) (= s 256)) integer->char
  121. (lambda (s) (+ s 1)) 0 char-set:empty))))
  122. (with-test-prefix "char-set-unfold!"
  123. (pass-if "create char set"
  124. (char-set= char-set:full
  125. (char-set-unfold! (lambda (s) (= s 256)) integer->char
  126. (lambda (s) (+ s 1)) 0
  127. (char-set-copy char-set:empty))))
  128. (pass-if "create char set"
  129. (char-set= char-set:full
  130. (char-set-unfold! (lambda (s) (= s 32)) integer->char
  131. (lambda (s) (+ s 1)) 0
  132. (char-set-copy char-set:full)))))
  133. (with-test-prefix "char-set-for-each"
  134. (pass-if "copy char set"
  135. (= (char-set-size (let ((cs (char-set)))
  136. (char-set-for-each
  137. (lambda (c) (char-set-adjoin! cs c))
  138. (char-set #\a #\b))
  139. cs))
  140. 2)))
  141. (with-test-prefix "char-set-map"
  142. (pass-if "upper case char set"
  143. (char-set= (char-set-map char-upcase char-set:lower-case)
  144. char-set:upper-case)))
  145. (with-test-prefix "string->char-set"
  146. (pass-if "some char set"
  147. (let ((chars '(#\g #\u #\i #\l #\e)))
  148. (char-set= (list->char-set chars)
  149. (string->char-set (apply string chars))))))
  150. ;; Make sure we get an ASCII charset and character classification.
  151. (if (defined? 'setlocale) (setlocale LC_CTYPE "C"))
  152. (with-test-prefix "standard char sets (ASCII)"
  153. (pass-if "char-set:letter"
  154. (char-set= (string->char-set
  155. (string-append "abcdefghijklmnopqrstuvwxyz"
  156. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
  157. char-set:letter))
  158. (pass-if "char-set:punctuation"
  159. (char-set= (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}")
  160. char-set:punctuation))
  161. (pass-if "char-set:symbol"
  162. (char-set= (string->char-set "$+<=>^`|~")
  163. char-set:symbol))
  164. (pass-if "char-set:letter+digit"
  165. (char-set= char-set:letter+digit
  166. (char-set-union char-set:letter char-set:digit)))
  167. (pass-if "char-set:graphic"
  168. (char-set= char-set:graphic
  169. (char-set-union char-set:letter char-set:digit
  170. char-set:punctuation char-set:symbol)))
  171. (pass-if "char-set:printing"
  172. (char-set= char-set:printing
  173. (char-set-union char-set:whitespace char-set:graphic))))
  174. ;;;
  175. ;;; 8-bit charsets.
  176. ;;;
  177. ;;; Here, we only test ISO-8859-1 (Latin-1), notably because behavior of
  178. ;;; SRFI-14 for implementations supporting this charset is well-defined.
  179. ;;;
  180. (define (every? pred lst)
  181. (not (not (every pred lst))))
  182. (define (find-latin1-locale)
  183. ;; Try to find and install an ISO-8859-1 locale. Return `#f' on failure.
  184. (if (defined? 'setlocale)
  185. (let loop ((locales (map (lambda (lang)
  186. (string-append lang ".iso88591"))
  187. '("de_DE" "en_GB" "en_US" "es_ES"
  188. "fr_FR" "it_IT"))))
  189. (if (null? locales)
  190. #f
  191. (if (false-if-exception (setlocale LC_CTYPE (car locales)))
  192. (car locales)
  193. (loop (cdr locales)))))
  194. #f))
  195. (define %latin1 (find-latin1-locale))
  196. (with-test-prefix "Latin-1 (8-bit charset)"
  197. ;; Note: the membership tests below are not exhaustive.
  198. (pass-if "char-set:letter (membership)"
  199. (if (not %latin1)
  200. (throw 'unresolved)
  201. (let ((letters (char-set->list char-set:letter)))
  202. (every? (lambda (8-bit-char)
  203. (memq 8-bit-char letters))
  204. (append '(#\a #\b #\c) ;; ASCII
  205. (string->list "çéèâùÉÀÈÊ") ;; French
  206. (string->list "øñÑíßåæðþ"))))))
  207. (pass-if "char-set:letter (size)"
  208. (if (not %latin1)
  209. (throw 'unresolved)
  210. (= (char-set-size char-set:letter) 117)))
  211. (pass-if "char-set:lower-case (size)"
  212. (if (not %latin1)
  213. (throw 'unresolved)
  214. (= (char-set-size char-set:lower-case) (+ 26 33))))
  215. (pass-if "char-set:upper-case (size)"
  216. (if (not %latin1)
  217. (throw 'unresolved)
  218. (= (char-set-size char-set:upper-case) (+ 26 30))))
  219. (pass-if "char-set:punctuation (membership)"
  220. (if (not %latin1)
  221. (throw 'unresolved)
  222. (let ((punctuation (char-set->list char-set:punctuation)))
  223. (every? (lambda (8-bit-char)
  224. (memq 8-bit-char punctuation))
  225. (append '(#\! #\. #\?) ;; ASCII
  226. (string->list "¡¿") ;; Castellano
  227. (string->list "«»")))))) ;; French
  228. (pass-if "char-set:letter+digit"
  229. (char-set= char-set:letter+digit
  230. (char-set-union char-set:letter char-set:digit)))
  231. (pass-if "char-set:graphic"
  232. (char-set= char-set:graphic
  233. (char-set-union char-set:letter char-set:digit
  234. char-set:punctuation char-set:symbol)))
  235. (pass-if "char-set:printing"
  236. (char-set= char-set:printing
  237. (char-set-union char-set:whitespace char-set:graphic))))
  238. ;; Local Variables:
  239. ;; mode: scheme
  240. ;; coding: latin-1
  241. ;; End: