Remove locale u8vector functions
[bpt/guile.git] / test-suite / tests / encoding-utf8.test
1 ;;;; strings.test --- test suite for Guile's string functions -*- mode: scheme; coding: utf-8 -*-
2 ;;;;
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
9 ;;;;
10 ;;;; This program 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
13 ;;;; GNU General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 ;;;; Boston, MA 02110-1301 USA
19
20 (define-module (test-strings)
21 #:use-module (test-suite lib)
22 #:use-module (srfi srfi-1))
23
24 (define exception:conversion
25 (cons 'misc-error "^cannot convert to output locale"))
26
27 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
28 (define (string-ints . args)
29 (apply string (map integer->char args)))
30
31 (define oldlocale #f)
32 (if (defined? 'setlocale)
33 (set! oldlocale (setlocale LC_ALL "")))
34
35 (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
36 (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
37 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
38 (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
39
40 (with-test-prefix "characters"
41 (pass-if "input A"
42 (char=? ascii-a #\A))
43
44 (pass-if "input A acute"
45 (char=? a-acute #\Á))
46
47 (pass-if "input alpha"
48 (char=? alpha #\α))
49
50 (pass-if "input Cherokee A"
51 (char=? cherokee-a #\Ꭰ))
52
53 (pass-if "display A"
54 (let ((pt (open-output-string)))
55 (set-port-encoding! pt "UTF-8")
56 (set-port-conversion-strategy! pt 'substitute)
57 (display ascii-a pt)
58 (string=? "A"
59 (get-output-string pt))))
60
61 (pass-if "display A acute"
62 (let ((pt (open-output-string)))
63 (set-port-encoding! pt "UTF-8")
64 (set-port-conversion-strategy! pt 'substitute)
65 (display a-acute pt)
66 (string=? "Á"
67 (get-output-string pt))))
68
69 (pass-if "display alpha"
70 (let ((pt (open-output-string)))
71 (set-port-encoding! pt "UTF-8")
72 (set-port-conversion-strategy! pt 'substitute)
73 (display alpha pt)
74 (string-ci=? "α"
75 (get-output-string pt))))
76
77 (pass-if "display Cherokee A"
78 (let ((pt (open-output-string)))
79 (set-port-encoding! pt "UTF-8")
80 (set-port-conversion-strategy! pt 'substitute)
81 (display cherokee-a pt)
82 (string-ci=? "Ꭰ"
83 (get-output-string pt))))
84
85 (pass-if "write A"
86 (let ((pt (open-output-string)))
87 (set-port-encoding! pt "UTF-8")
88 (set-port-conversion-strategy! pt 'escape)
89 (write ascii-a pt)
90 (string=? "#\\A"
91 (get-output-string pt))))
92
93 (pass-if "write A acute"
94 (let ((pt (open-output-string)))
95 (set-port-encoding! pt "UTF-8")
96 (set-port-conversion-strategy! pt 'escape)
97 (write a-acute pt)
98 (string=? "#\\Á"
99 (get-output-string pt))))
100
101 (pass-if "write alpha"
102 (let ((pt (open-output-string)))
103 (set-port-encoding! pt "UTF-8")
104 (set-port-conversion-strategy! pt 'escape)
105 (write alpha pt)
106 (string=? "#\\α"
107 (get-output-string pt))))
108
109 (pass-if "write Cherokee A"
110 (let ((pt (open-output-string)))
111 (set-port-encoding! pt "UTF-8")
112 (set-port-conversion-strategy! pt 'escape)
113 (write cherokee-a pt)
114 (string=? "#\\Ꭰ"
115 (get-output-string pt)))))
116
117 (define s1 "última")
118 (define s2 "cédula")
119 (define s3 "años")
120 (define s4 "羅生門")
121
122 (with-test-prefix "string length"
123
124 (pass-if "última"
125 (eq? (string-length s1) 6))
126
127 (pass-if "cédula"
128 (eq? (string-length s2) 6))
129
130 (pass-if "años"
131 (eq? (string-length s3) 4))
132
133 (pass-if "羅生門"
134 (eq? (string-length s4) 3)))
135
136 (with-test-prefix "internal encoding"
137
138 (pass-if "última"
139 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
140
141 (pass-if "cédula"
142 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
143
144 (pass-if "años"
145 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
146
147 (pass-if "羅生門"
148 (string=? s4 (string-ints #x7f85 #x751f #x9580))))
149
150 (with-test-prefix "chars"
151
152 (pass-if "última"
153 (list= eqv? (string->list s1)
154 (list #\ú #\l #\t #\i #\m #\a)))
155
156 (pass-if "cédula"
157 (list= eqv? (string->list s2)
158 (list #\c #\é #\d #\u #\l #\a)))
159
160 (pass-if "años"
161 (list= eqv? (string->list s3)
162 (list #\a #\ñ #\o #\s)))
163
164 (pass-if "羅生門"
165 (list= eqv? (string->list s4)
166 (list #\羅 #\生 #\門))))
167
168 (with-test-prefix "symbols == strings"
169
170 (pass-if "última"
171 (eq? (string->symbol s1) 'última))
172
173 (pass-if "cédula"
174 (eq? (string->symbol s2) 'cédula))
175
176 (pass-if "años"
177 (eq? (string->symbol s3) 'años))
178
179 (pass-if "羅生門"
180 (eq? (string->symbol s4) '羅生門)))
181
182 (with-test-prefix "non-ascii variable names"
183
184 (pass-if "1"
185 (let ((芥川龍之介 1)
186 (ñ 2))
187 (eq? (+ 芥川龍之介 ñ) 3))))
188
189 (if (defined? 'setlocale)
190 (setlocale LC_ALL oldlocale))