iconv.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. ;;;; iconv.test --- Exercise the iconv API. -*- coding: utf-8; mode: scheme; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2013 Free Software Foundation, Inc.
  4. ;;;; Andy Wingo
  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. (define-module (test-suite iconv)
  20. #:use-module (ice-9 iconv)
  21. #:use-module (rnrs bytevectors)
  22. #:use-module (test-suite lib))
  23. (define exception:encoding-error
  24. '(encoding-error . ""))
  25. (define exception:decoding-error
  26. '(decoding-error . ""))
  27. (with-test-prefix "ascii string"
  28. (let ((s "Hello, World!"))
  29. ;; For ASCII, all of these encodings should be the same.
  30. (pass-if "to ascii bytevector"
  31. (equal? (string->bytevector s "ASCII")
  32. #vu8(72 101 108 108 111 44 32 87 111 114 108 100 33)))
  33. (pass-if "to ascii bytevector (length check)"
  34. (equal? (string-length s)
  35. (bytevector-length (string->bytevector s "ascii"))))
  36. (pass-if "from ascii bytevector"
  37. (equal? s
  38. (bytevector->string (string->bytevector s "ascii") "ascii")))
  39. (pass-if "to utf-8 bytevector"
  40. (equal? (string->bytevector s "ASCII")
  41. (string->bytevector s "utf-8")))
  42. (pass-if "to UTF-8 bytevector (testing encoding case sensitivity)"
  43. (equal? (string->bytevector s "ascii")
  44. (string->bytevector s "UTF-8")))
  45. (pass-if "from utf-8 bytevector"
  46. (equal? s
  47. (bytevector->string (string->bytevector s "utf-8") "utf-8")))
  48. (pass-if "to latin1 bytevector"
  49. (equal? (string->bytevector s "ASCII")
  50. (string->bytevector s "latin1")))
  51. (pass-if "from latin1 bytevector"
  52. (equal? s
  53. (bytevector->string (string->bytevector s "utf-8") "utf-8")))))
  54. (with-test-prefix "narrow non-ascii string"
  55. (let ((s "été"))
  56. (pass-if "to latin1 bytevector"
  57. (equal? (string->bytevector s "latin1")
  58. #vu8(233 116 233)))
  59. (pass-if "to latin1 bytevector (length check)"
  60. (equal? (string-length s)
  61. (bytevector-length (string->bytevector s "latin1"))))
  62. (pass-if "from latin1 bytevector"
  63. (equal? s
  64. (bytevector->string (string->bytevector s "latin1") "latin1")))
  65. (pass-if "to utf-8 bytevector"
  66. (equal? (string->bytevector s "utf-8")
  67. #vu8(195 169 116 195 169)))
  68. (pass-if "from utf-8 bytevector"
  69. (equal? s
  70. (bytevector->string (string->bytevector s "utf-8") "utf-8")))
  71. (pass-if-exception "encode latin1 as ascii" exception:encoding-error
  72. (string->bytevector s "ascii"))
  73. (pass-if-exception "misparse latin1 as utf8" exception:decoding-error
  74. (bytevector->string (string->bytevector s "latin1") "utf-8"))
  75. (pass-if "misparse latin1 as utf8 with substitutions"
  76. (equal? (bytevector->string (string->bytevector s "latin1")
  77. "utf-8" 'substitute)
  78. "\uFFFDt\uFFFD"))
  79. (pass-if-exception "misparse latin1 as ascii" exception:decoding-error
  80. (bytevector->string (string->bytevector s "latin1") "ascii"))))
  81. (with-test-prefix "wide non-ascii string"
  82. (let ((s "ΧΑΟΣ"))
  83. (pass-if "to utf-8 bytevector"
  84. (equal? (string->bytevector s "utf-8")
  85. #vu8(206 167 206 145 206 159 206 163) ))
  86. (pass-if "from utf-8 bytevector"
  87. (equal? s
  88. (bytevector->string (string->bytevector s "utf-8") "utf-8")))
  89. (pass-if-exception "encode as ascii" exception:encoding-error
  90. (string->bytevector s "ascii"))
  91. (pass-if-exception "encode as latin1" exception:encoding-error
  92. (string->bytevector s "latin1"))
  93. (pass-if "encode as ascii with substitutions"
  94. (equal? (make-string (string-length s) #\?)
  95. (bytevector->string (string->bytevector s "ascii" 'substitute)
  96. "ascii")))))