1 ;;;; iconv.test --- Exercise the iconv API. -*- coding: utf-8; mode: scheme; -*-
3 ;;;; Copyright (C) 2013 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-suite iconv)
21 #:use-module (ice-9 iconv)
22 #:use-module (rnrs bytevectors)
23 #:use-module (test-suite lib))
26 (define exception:encoding-error
27 '(encoding-error . ""))
29 (define exception:decoding-error
30 '(decoding-error . ""))
33 (with-test-prefix "ascii string"
34 (let ((s "Hello, World!"))
35 ;; For ASCII, all of these encodings should be the same.
37 (pass-if "to ascii bytevector"
38 (equal? (string->bytevector s "ASCII")
39 #vu8(72 101 108 108 111 44 32 87 111 114 108 100 33)))
41 (pass-if "to ascii bytevector (length check)"
42 (equal? (string-length s)
43 (bytevector-length (string->bytevector s "ascii"))))
45 (pass-if "from ascii bytevector"
47 (bytevector->string (string->bytevector s "ascii") "ascii")))
49 (pass-if "to utf-8 bytevector"
50 (equal? (string->bytevector s "ASCII")
51 (string->bytevector s "utf-8")))
53 (pass-if "to UTF-8 bytevector (testing encoding case sensitivity)"
54 (equal? (string->bytevector s "ascii")
55 (string->bytevector s "UTF-8")))
57 (pass-if "from utf-8 bytevector"
59 (bytevector->string (string->bytevector s "utf-8") "utf-8")))
61 (pass-if "to latin1 bytevector"
62 (equal? (string->bytevector s "ASCII")
63 (string->bytevector s "latin1")))
65 (pass-if "from latin1 bytevector"
67 (bytevector->string (string->bytevector s "utf-8") "utf-8")))))
69 (with-test-prefix "narrow non-ascii string"
71 (pass-if "to latin1 bytevector"
72 (equal? (string->bytevector s "latin1")
75 (pass-if "to latin1 bytevector (length check)"
76 (equal? (string-length s)
77 (bytevector-length (string->bytevector s "latin1"))))
79 (pass-if "from latin1 bytevector"
81 (bytevector->string (string->bytevector s "latin1") "latin1")))
83 (pass-if "to utf-8 bytevector"
84 (equal? (string->bytevector s "utf-8")
85 #vu8(195 169 116 195 169)))
87 (pass-if "from utf-8 bytevector"
89 (bytevector->string (string->bytevector s "utf-8") "utf-8")))
91 (pass-if-exception "encode latin1 as ascii" exception:encoding-error
92 (string->bytevector s "ascii"))
94 (pass-if-exception "misparse latin1 as utf8" exception:decoding-error
95 (bytevector->string (string->bytevector s "latin1") "utf-8"))
97 (pass-if-exception "misparse latin1 as ascii" exception:decoding-error
98 (bytevector->string (string->bytevector s "latin1") "ascii"))))
101 (with-test-prefix "wide non-ascii string"
103 (pass-if "to utf-8 bytevector"
104 (equal? (string->bytevector s "utf-8")
105 #vu8(206 167 206 145 206 159 206 163) ))
107 (pass-if "from utf-8 bytevector"
109 (bytevector->string (string->bytevector s "utf-8") "utf-8")))
111 (pass-if-exception "encode as ascii" exception:encoding-error
112 (string->bytevector s "ascii"))
114 (pass-if-exception "encode as latin1" exception:encoding-error
115 (string->bytevector s "latin1"))
117 (pass-if "encode as ascii with substitutions"
118 (equal? (make-string (string-length s) #\?)
119 (bytevector->string (string->bytevector s "ascii" 'substitute)