summaryrefslogtreecommitdiff
path: root/test-suite/tests/iconv.test
blob: 676d94821c29dd708881f954fecbea54fb8b4d23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
;;;; iconv.test --- Exercise the iconv API.  -*- coding: utf-8; mode: scheme; -*-
;;;;
;;;; Copyright (C) 2013 Free Software Foundation, Inc.
;;;; Andy Wingo
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (test-suite iconv)
  #:use-module (ice-9 iconv)
  #:use-module (rnrs bytevectors)
  #:use-module (test-suite lib))


(define exception:encoding-error
  '(encoding-error . ""))

(define exception:decoding-error
  '(decoding-error . ""))


(with-test-prefix "ascii string"
  (let ((s "Hello, World!"))
    ;; For ASCII, all of these encodings should be the same.

    (pass-if "to ascii bytevector"
      (equal? (string->bytevector s "ASCII")
              #vu8(72 101 108 108 111 44 32 87 111 114 108 100 33)))
  
    (pass-if "to ascii bytevector (length check)"
      (equal? (string-length s)
              (bytevector-length (string->bytevector s "ascii"))))
  
    (pass-if "from ascii bytevector"
      (equal? s
              (bytevector->string (string->bytevector s "ascii") "ascii")))
  
    (pass-if "to utf-8 bytevector"
      (equal? (string->bytevector s "ASCII")
              (string->bytevector s "utf-8")))
  
    (pass-if "to UTF-8 bytevector (testing encoding case sensitivity)"
      (equal? (string->bytevector s "ascii")
              (string->bytevector s "UTF-8")))
  
    (pass-if "from utf-8 bytevector"
      (equal? s
              (bytevector->string (string->bytevector s "utf-8") "utf-8")))
  
    (pass-if "to latin1 bytevector"
      (equal? (string->bytevector s "ASCII")
              (string->bytevector s "latin1")))

    (pass-if "from latin1 bytevector"
      (equal? s
              (bytevector->string (string->bytevector s "utf-8") "utf-8")))))

(with-test-prefix "narrow non-ascii string"
  (let ((s "été"))
    (pass-if "to latin1 bytevector"
      (equal? (string->bytevector s "latin1")
              #vu8(233 116 233)))
  
    (pass-if "to latin1 bytevector (length check)"
      (equal? (string-length s)
              (bytevector-length (string->bytevector s "latin1"))))
  
    (pass-if "from latin1 bytevector"
      (equal? s
              (bytevector->string (string->bytevector s "latin1") "latin1")))
  
    (pass-if "to utf-8 bytevector"
      (equal? (string->bytevector s "utf-8")
              #vu8(195 169 116 195 169)))

    (pass-if "from utf-8 bytevector"
      (equal? s
              (bytevector->string (string->bytevector s "utf-8") "utf-8")))

    (pass-if-exception "encode latin1 as ascii" exception:encoding-error
      (string->bytevector s "ascii"))

    (pass-if-exception "misparse latin1 as utf8" exception:decoding-error
      (bytevector->string (string->bytevector s "latin1") "utf-8"))

    (pass-if "misparse latin1 as utf8 with substitutions"
      (equal? (bytevector->string (string->bytevector s "latin1")
                                  "utf-8" 'substitute)
              "\uFFFDt\uFFFD"))

    (pass-if-exception "misparse latin1 as ascii" exception:decoding-error
      (bytevector->string (string->bytevector s "latin1") "ascii"))))


(with-test-prefix "wide non-ascii string"
  (let ((s "ΧΑΟΣ"))
    (pass-if "to utf-8 bytevector"
      (equal? (string->bytevector s "utf-8")
              #vu8(206 167 206 145 206 159 206 163) ))

    (pass-if "from utf-8 bytevector"
      (equal? s
              (bytevector->string (string->bytevector s "utf-8") "utf-8")))

    (pass-if-exception "encode as ascii" exception:encoding-error
      (string->bytevector s "ascii"))

    (pass-if-exception "encode as latin1" exception:encoding-error
      (string->bytevector s "latin1"))

    (pass-if "encode as ascii with substitutions"
      (equal? (make-string (string-length s) #\?)
              (bytevector->string (string->bytevector s "ascii" 'substitute)
                                  "ascii")))))