encoding-utf8.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ;;;; encoding-utf8.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2009, 2010, 2014 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. (define-module (test-strings)
  19. #:use-module (test-suite lib)
  20. #:use-module (srfi srfi-1))
  21. ;; Create a string from integer char values, eg. (string-ints 65) => "A"
  22. (define (string-ints . args)
  23. (apply string (map integer->char args)))
  24. (when (defined? 'setlocale)
  25. (setlocale LC_ALL ""))
  26. (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
  27. (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
  28. (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
  29. (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
  30. (with-test-prefix "characters"
  31. (pass-if "input A"
  32. (char=? ascii-a #\A))
  33. (pass-if "input A acute"
  34. (char=? a-acute #\Á))
  35. (pass-if "input alpha"
  36. (char=? alpha #\α))
  37. (pass-if "input Cherokee A"
  38. (char=? cherokee-a #\Ꭰ))
  39. (pass-if "display A"
  40. (let ((pt (open-output-string)))
  41. (set-port-encoding! pt "UTF-8")
  42. (set-port-conversion-strategy! pt 'substitute)
  43. (display ascii-a pt)
  44. (string=? "A"
  45. (get-output-string pt))))
  46. (pass-if "display A acute"
  47. (let ((pt (open-output-string)))
  48. (set-port-encoding! pt "UTF-8")
  49. (set-port-conversion-strategy! pt 'substitute)
  50. (display a-acute pt)
  51. (string=? "Á"
  52. (get-output-string pt))))
  53. (pass-if "display alpha"
  54. (let ((pt (open-output-string)))
  55. (set-port-encoding! pt "UTF-8")
  56. (set-port-conversion-strategy! pt 'substitute)
  57. (display alpha pt)
  58. (string-ci=? "α"
  59. (get-output-string pt))))
  60. (pass-if "display Cherokee A"
  61. (let ((pt (open-output-string)))
  62. (set-port-encoding! pt "UTF-8")
  63. (set-port-conversion-strategy! pt 'substitute)
  64. (display cherokee-a pt)
  65. (string-ci=? "Ꭰ"
  66. (get-output-string pt))))
  67. (pass-if "write A"
  68. (let ((pt (open-output-string)))
  69. (set-port-encoding! pt "UTF-8")
  70. (set-port-conversion-strategy! pt 'escape)
  71. (write ascii-a pt)
  72. (string=? "#\\A"
  73. (get-output-string pt))))
  74. (pass-if "write A acute"
  75. (let ((pt (open-output-string)))
  76. (set-port-encoding! pt "UTF-8")
  77. (set-port-conversion-strategy! pt 'escape)
  78. (write a-acute pt)
  79. (string=? "#\\Á"
  80. (get-output-string pt))))
  81. (pass-if "write A followed by combining accent"
  82. (let ((pt (open-output-string)))
  83. (set-port-encoding! pt "UTF-8")
  84. (set-port-conversion-strategy! pt 'escape)
  85. (write (string #\A (integer->char #x030f)) pt)
  86. (string-ci=? "\"Ȁ\""
  87. (get-output-string pt))))
  88. (pass-if "write alpha"
  89. (let ((pt (open-output-string)))
  90. (set-port-encoding! pt "UTF-8")
  91. (set-port-conversion-strategy! pt 'escape)
  92. (write alpha pt)
  93. (string=? "#\\α"
  94. (get-output-string pt))))
  95. (pass-if "write Cherokee A"
  96. (let ((pt (open-output-string)))
  97. (set-port-encoding! pt "UTF-8")
  98. (set-port-conversion-strategy! pt 'escape)
  99. (write cherokee-a pt)
  100. (string=? "#\\Ꭰ"
  101. (get-output-string pt)))))
  102. (define s1 "última")
  103. (define s2 "cédula")
  104. (define s3 "años")
  105. (define s4 "羅生門")
  106. (with-test-prefix "string length"
  107. (pass-if "última"
  108. (eqv? (string-length s1) 6))
  109. (pass-if "cédula"
  110. (eqv? (string-length s2) 6))
  111. (pass-if "años"
  112. (eqv? (string-length s3) 4))
  113. (pass-if "羅生門"
  114. (eqv? (string-length s4) 3)))
  115. (with-test-prefix "internal encoding"
  116. (pass-if "última"
  117. (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
  118. (pass-if "cédula"
  119. (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
  120. (pass-if "años"
  121. (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
  122. (pass-if "羅生門"
  123. (string=? s4 (string-ints #x7f85 #x751f #x9580))))
  124. (with-test-prefix "chars"
  125. (pass-if "última"
  126. (list= eqv? (string->list s1)
  127. (list #\ú #\l #\t #\i #\m #\a)))
  128. (pass-if "cédula"
  129. (list= eqv? (string->list s2)
  130. (list #\c #\é #\d #\u #\l #\a)))
  131. (pass-if "años"
  132. (list= eqv? (string->list s3)
  133. (list #\a #\ñ #\o #\s)))
  134. (pass-if "羅生門"
  135. (list= eqv? (string->list s4)
  136. (list #\羅 #\生 #\門))))
  137. (with-test-prefix "symbols == strings"
  138. (pass-if "última"
  139. (eq? (string->symbol s1) 'última))
  140. (pass-if "cédula"
  141. (eq? (string->symbol s2) 'cédula))
  142. (pass-if "años"
  143. (eq? (string->symbol s3) 'años))
  144. (pass-if "羅生門"
  145. (eq? (string->symbol s4) '羅生門)))
  146. (with-test-prefix "non-ascii variable names"
  147. (pass-if "1"
  148. (let ((芥川龍之介 1)
  149. (ñ 2))
  150. (eqv? (+ 芥川龍之介 ñ) 3))))