encoding-iso88591.test 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. ;;;; encoding-iso88591.test --- test suite for Guile's string encodings -*- mode: scheme; coding: iso-8859-1 -*-
  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. ;; Set locale to the environment's locale, so that the prints look OK.
  25. (when (defined? 'setlocale)
  26. (setlocale LC_ALL ""))
  27. (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
  28. (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
  29. (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
  30. (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
  31. (with-test-prefix "characters"
  32. (pass-if "input A"
  33. (char=? ascii-a #\A))
  34. (pass-if "input A acute"
  35. (char=? a-acute #\Á))
  36. (pass-if "display A"
  37. (let ((pt (open-output-string)))
  38. (set-port-encoding! pt "ISO-8859-1")
  39. (set-port-conversion-strategy! pt 'escape)
  40. (display ascii-a pt)
  41. (string=? "A"
  42. (get-output-string pt))))
  43. (pass-if "display A acute"
  44. (let ((pt (open-output-string)))
  45. (set-port-encoding! pt "ISO-8859-1")
  46. (set-port-conversion-strategy! pt 'escape)
  47. (display a-acute pt)
  48. (string=? "Á"
  49. (get-output-string pt))))
  50. (pass-if "display alpha"
  51. (let ((pt (open-output-string)))
  52. (set-port-encoding! pt "ISO-8859-1")
  53. (set-port-conversion-strategy! pt 'escape)
  54. (display alpha pt)
  55. (string-ci=? "\\u03b1"
  56. (get-output-string pt))))
  57. (pass-if "display Cherokee a"
  58. (let ((pt (open-output-string)))
  59. (set-port-encoding! pt "ISO-8859-1")
  60. (set-port-conversion-strategy! pt 'escape)
  61. (display cherokee-a pt)
  62. (string-ci=? "\\u13a0"
  63. (get-output-string pt))))
  64. (pass-if "write A"
  65. (let ((pt (open-output-string)))
  66. (set-port-encoding! pt "ISO-8859-1")
  67. (set-port-conversion-strategy! pt 'escape)
  68. (write ascii-a pt)
  69. (string=? "#\\A"
  70. (get-output-string pt))))
  71. (pass-if "write A acute"
  72. (let ((pt (open-output-string)))
  73. (set-port-encoding! pt "ISO-8859-1")
  74. (set-port-conversion-strategy! pt 'escape)
  75. (write a-acute pt)
  76. (string=? "#\\Á"
  77. (get-output-string pt))))
  78. (pass-if "write A followed by combining accent"
  79. (let ((pt (open-output-string)))
  80. (set-port-encoding! pt "ISO-8859-1")
  81. (set-port-conversion-strategy! pt 'escape)
  82. (write (string #\A (integer->char #x030f)) pt)
  83. (string-ci=? "\"A\\u030f\""
  84. (get-output-string pt)))))
  85. (define s1 "última")
  86. (define s2 "cédula")
  87. (define s3 "años")
  88. (define s4 "¿Cómo?")
  89. (with-test-prefix "string length"
  90. (pass-if "última"
  91. (eqv? (string-length s1) 6))
  92. (pass-if "cédula"
  93. (eqv? (string-length s2) 6))
  94. (pass-if "años"
  95. (eqv? (string-length s3) 4))
  96. (pass-if "¿Cómo?"
  97. (eqv? (string-length s4) 6)))
  98. (with-test-prefix "internal encoding"
  99. (pass-if "última"
  100. (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
  101. (pass-if "cédula"
  102. (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
  103. (pass-if "años"
  104. (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
  105. (pass-if "¿Cómo?"
  106. (string=? s4 (string-ints #xbf #x43 #xf3 #x6d #x6f #x3f))))
  107. (with-test-prefix "chars"
  108. (pass-if "última"
  109. (list= eqv? (string->list s1)
  110. (list #\ú #\l #\t #\i #\m #\a)))
  111. (pass-if "cédula"
  112. (list= eqv? (string->list s2)
  113. (list #\c #\é #\d #\u #\l #\a)))
  114. (pass-if "años"
  115. (list= eqv? (string->list s3)
  116. (list #\a #\ñ #\o #\s)))
  117. (pass-if "¿Cómo?"
  118. (list= eqv? (string->list s4)
  119. (list #\¿ #\C #\ó #\m #\o #\?))))
  120. (with-test-prefix "symbols == strings"
  121. (pass-if "última"
  122. (eq? (string->symbol s1) 'última))
  123. (pass-if "cédula"
  124. (eq? (string->symbol s2) 'cédula))
  125. (pass-if "años"
  126. (eq? (string->symbol s3) 'años))
  127. (pass-if "¿Cómo?"
  128. (eq? (string->symbol s4) '¿Cómo?)))
  129. (with-test-prefix "non-ascii variable names"
  130. (pass-if "1"
  131. (let ((á 1)
  132. (ñ 2))
  133. (eqv? (+ á ñ) 3))))
  134. (with-test-prefix "output errors"
  135. (pass-if-exception "char 256" exception:encoding-error
  136. (let ((pt (open-output-string)))
  137. (set-port-encoding! pt "ISO-8859-1")
  138. (set-port-conversion-strategy! pt 'error)
  139. (display (string-ints 256) pt))))