chars.test 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ;;;; chars.test --- Characters. -*- coding: utf-8; mode: scheme; -*-
  2. ;;;; Greg J. Badros <gjb@cs.washington.edu>
  3. ;;;;
  4. ;;;; Copyright (C) 2000, 2006, 2009, 2010, 2013, 2021 Free Software Foundation, Inc.
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library 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 GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (use-modules (test-suite lib))
  20. (define exception:wrong-type-to-apply
  21. (cons 'misc-error "^Wrong type to apply:"))
  22. (define exception:unknown-character-name
  23. (cons #t "unknown character"))
  24. (with-test-prefix "basic char handling"
  25. (with-test-prefix "evaluator"
  26. ;; The following test makes sure that the evaluator distinguishes between
  27. ;; evaluator-internal instruction codes and characters.
  28. (pass-if-exception "evaluating chars"
  29. exception:wrong-type-arg
  30. (eval '(#\0) (interaction-environment))))
  31. (with-test-prefix "comparisons"
  32. ;; char=?
  33. (pass-if "char=? #\\A #\\A"
  34. (char=? #\A #\A))
  35. (pass-if "char=? #\\A #\\a"
  36. (not (char=? #\A #\a)))
  37. (pass-if "char=? #\\A #\\B"
  38. (not (char=? #\A #\B)))
  39. (pass-if "char=? #\\B #\\A"
  40. (not (char=? #\A #\B)))
  41. ;; char<?
  42. (pass-if "char<? #\\A #\\A"
  43. (not (char<? #\A #\A)))
  44. (pass-if "char<? #\\A #\\a"
  45. (char<? #\A #\a))
  46. (pass-if "char<? #\\A #\\B"
  47. (char<? #\A #\B))
  48. (pass-if "char<? #\\B #\\A"
  49. (not (char<? #\B #\A)))
  50. ;; char<=?
  51. (pass-if "char<=? #\\A #\\A"
  52. (char<=? #\A #\A))
  53. (pass-if "char<=? #\\A #\\a"
  54. (char<=? #\A #\a))
  55. (pass-if "char<=? #\\A #\\B"
  56. (char<=? #\A #\B))
  57. (pass-if "char<=? #\\B #\\A"
  58. (not (char<=? #\B #\A)))
  59. ;; char>?
  60. (pass-if "char>? #\\A #\\A"
  61. (not (char>? #\A #\A)))
  62. (pass-if "char>? #\\A #\\a"
  63. (not (char>? #\A #\a)))
  64. (pass-if "char>? #\\A #\\B"
  65. (not (char>? #\A #\B)))
  66. (pass-if "char>? #\\B #\\A"
  67. (char>? #\B #\A))
  68. ;; char>=?
  69. (pass-if "char>=? #\\A #\\A"
  70. (char>=? #\A #\A))
  71. (pass-if "char>=? #\\A #\\a"
  72. (not (char>=? #\A #\a)))
  73. (pass-if "char>=? #\\A #\\B"
  74. (not (char>=? #\A #\B)))
  75. (pass-if "char>=? #\\B #\\A"
  76. (char>=? #\B #\A))
  77. ;; char-ci=?
  78. (pass-if "char-ci=? #\\A #\\A"
  79. (char-ci=? #\A #\A))
  80. (pass-if "char-ci=? #\\A #\\a"
  81. (char-ci=? #\A #\a))
  82. (pass-if "char-ci=? #\\A #\\B"
  83. (not (char-ci=? #\A #\B)))
  84. (pass-if "char-ci=? #\\B #\\A"
  85. (not (char-ci=? #\A #\B)))
  86. ;; char-ci<?
  87. (pass-if "char-ci<? #\\A #\\A"
  88. (not (char-ci<? #\A #\A)))
  89. (pass-if "char-ci<? #\\A #\\a"
  90. (not (char-ci<? #\A #\a)))
  91. (pass-if "char-ci<? #\\A #\\B"
  92. (char-ci<? #\A #\B))
  93. (pass-if "char-ci<? #\\B #\\A"
  94. (not (char-ci<? #\B #\A)))
  95. ;; char-ci<=?
  96. (pass-if "char-ci<=? #\\A #\\A"
  97. (char-ci<=? #\A #\A))
  98. (pass-if "char-ci<=? #\\A #\\a"
  99. (char-ci<=? #\A #\a))
  100. (pass-if "char-ci<=? #\\A #\\B"
  101. (char-ci<=? #\A #\B))
  102. (pass-if "char-ci<=? #\\B #\\A"
  103. (not (char-ci<=? #\B #\A)))
  104. ;; char-ci>?
  105. (pass-if "char-ci>? #\\A #\\A"
  106. (not (char-ci>? #\A #\A)))
  107. (pass-if "char-ci>? #\\A #\\a"
  108. (not (char-ci>? #\A #\a)))
  109. (pass-if "char-ci>? #\\A #\\B"
  110. (not (char-ci>? #\A #\B)))
  111. (pass-if "char-ci>? #\\B #\\A"
  112. (char-ci>? #\B #\A))
  113. ;; char-ci>=?
  114. (pass-if "char-ci>=? #\\A #\\A"
  115. (char-ci>=? #\A #\A))
  116. (pass-if "char-ci>=? #\\A #\\a"
  117. (char-ci>=? #\A #\a))
  118. (pass-if "char-ci>=? #\\A #\\B"
  119. (not (char-ci>=? #\A #\B)))
  120. (pass-if "char-ci>=? #\\B #\\A"
  121. (char-ci>=? #\B #\A)))
  122. (with-test-prefix "categories"
  123. (pass-if "char-alphabetic?"
  124. (and (char-alphabetic? #\a)
  125. (char-alphabetic? #\A)
  126. (not (char-alphabetic? #\1))
  127. (not (char-alphabetic? #\+))))
  128. (pass-if "char-numeric?"
  129. (and (not (char-numeric? #\a))
  130. (not (char-numeric? #\A))
  131. (char-numeric? #\1)
  132. (not (char-numeric? #\+))))
  133. (pass-if "char-whitespace?"
  134. (and (not (char-whitespace? #\a))
  135. (not (char-whitespace? #\A))
  136. (not (char-whitespace? #\1))
  137. (char-whitespace? #\space)
  138. (not (char-whitespace? #\+))))
  139. (pass-if "char-upper-case?"
  140. (and (not (char-upper-case? #\a))
  141. (char-upper-case? #\A)
  142. (not (char-upper-case? #\1))
  143. (not (char-upper-case? #\+))))
  144. (pass-if "char-lower-case?"
  145. (and (char-lower-case? #\a)
  146. (not (char-lower-case? #\A))
  147. (not (char-lower-case? #\1))
  148. (not (char-lower-case? #\+))))
  149. (pass-if "char-is-both? works"
  150. (and
  151. (not (char-is-both? #\?))
  152. (not (char-is-both? #\newline))
  153. (char-is-both? #\a)
  154. (char-is-both? #\Z)
  155. (not (char-is-both? #\1))))
  156. (pass-if "char-general-category"
  157. (and (eq? (char-general-category #\a) 'Ll)
  158. (eq? (char-general-category #\A) 'Lu)
  159. (eq? (char-general-category #\762) 'Lt))))
  160. (with-test-prefix "integer"
  161. (pass-if "char->integer"
  162. (eqv? (char->integer #\A) 65))
  163. (pass-if "integer->char"
  164. (eqv? (integer->char 65) #\A))
  165. (pass-if-exception "integer->char out of range, -1" exception:out-of-range
  166. (integer->char -1))
  167. (pass-if-exception "integer->char out of range, surrrogate"
  168. exception:out-of-range
  169. (integer->char #xd800))
  170. (pass-if-exception "integer->char out of range, too big"
  171. exception:out-of-range
  172. (integer->char #x110000))
  173. (pass-if-exception "octal out of range, surrrogate"
  174. exception:out-of-range
  175. (with-input-from-string "#\\154000" read))
  176. (pass-if-exception "octal out of range, too big"
  177. exception:out-of-range
  178. (with-input-from-string "#\\4200000" read)))
  179. (with-test-prefix "case"
  180. (pass-if "char-upcase"
  181. (eqv? (char-upcase #\a) #\A))
  182. (pass-if "char-downcase"
  183. (eqv? (char-downcase #\A) #\a))
  184. (pass-if "char-titlecase"
  185. (and (eqv? (char-titlecase #\a) #\A)
  186. (eqv? (char-titlecase #\763) #\762))))
  187. (with-test-prefix "charnames"
  188. (pass-if "R5RS character names"
  189. (and (eqv? #\space (integer->char #x20))
  190. (eqv? #\newline (integer->char #x0A))))
  191. (pass-if "R6RS character names"
  192. (and (eqv? #\nul (integer->char #x00))
  193. (eqv? #\alarm (integer->char #x07))
  194. (eqv? #\backspace (integer->char #x08))
  195. (eqv? #\tab (integer->char #x09))
  196. (eqv? #\linefeed (integer->char #x0A))
  197. (eqv? #\newline (integer->char #x0A))
  198. (eqv? #\vtab (integer->char #x0B))
  199. (eqv? #\page (integer->char #x0C))
  200. (eqv? #\return (integer->char #x0D))
  201. (eqv? #\esc (integer->char #x1B))
  202. (eqv? #\space (integer->char #x20))
  203. (eqv? #\delete (integer->char #x7F))))
  204. (pass-if "R5RS character names are case insensitive"
  205. (and (eqv? #\space #\ )
  206. (eqv? #\SPACE #\ )
  207. (eqv? #\Space #\ )
  208. (eqv? #\newline (integer->char 10))
  209. (eqv? #\NEWLINE (integer->char 10))
  210. (eqv? #\Newline (integer->char 10))))
  211. (pass-if "C0 control names are case insensitive"
  212. (and (eqv? #\nul #\000)
  213. (eqv? #\soh #\001)
  214. (eqv? #\stx #\002)
  215. (eqv? #\NUL #\000)
  216. (eqv? #\SOH #\001)
  217. (eqv? #\STX #\002)
  218. (eqv? #\Nul #\000)
  219. (eqv? #\Soh #\001)
  220. (eqv? #\Stx #\002)))
  221. (pass-if "alt charnames are case insensitive"
  222. (eqv? #\null #\nul)
  223. (eqv? #\NULL #\nul)
  224. (eqv? #\Null #\nul))
  225. (pass-if-exception "bad charname" exception:unknown-character-name
  226. (with-input-from-string "#\\blammo" read))
  227. (pass-if "R5RS character names are preferred write format"
  228. (string=?
  229. (with-output-to-string (lambda () (write #\space)))
  230. "#\\space"))
  231. (pass-if "C0 control character names are preferred write format"
  232. (string=?
  233. (with-output-to-string (lambda () (write #\soh)))
  234. "#\\soh"))
  235. (pass-if "combining accent is pretty-printed"
  236. (let ((accent (integer->char #x030f))) ; COMBINING DOUBLE GRAVE ACCENT
  237. (string=?
  238. (with-output-to-string (lambda () (write accent)))
  239. "#\\◌̏")))
  240. (pass-if "combining X is pretty-printed"
  241. (let ((x (integer->char #x0353))) ; COMBINING X BELOW
  242. (string=?
  243. (with-output-to-string (lambda () (write x)))
  244. "#\\◌͓")))))