Remove locale u8vector functions
[bpt/guile.git] / test-suite / tests / encoding-iso88591.test
1 ;;;; strings.test --- test suite for Guile's string functions -*- mode: scheme; coding: iso-8859-1 -*-
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 ;; Set locale to the environment's locale, so that the prints look OK.
32 (define oldlocale #f)
33 (if (defined? 'setlocale)
34 (set! oldlocale (setlocale LC_ALL "")))
35
36 (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
37 (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
38 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
39 (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
40
41 (with-test-prefix "characters"
42 (pass-if "input A"
43 (char=? ascii-a #\A))
44
45 (pass-if "input A acute"
46 (char=? a-acute #\Á))
47
48 (pass-if "display A"
49 (let ((pt (open-output-string)))
50 (set-port-encoding! pt "ISO-8859-1")
51 (set-port-conversion-strategy! pt 'escape)
52 (display ascii-a pt)
53 (string=? "A"
54 (get-output-string pt))))
55
56 (pass-if "display A acute"
57 (let ((pt (open-output-string)))
58 (set-port-encoding! pt "ISO-8859-1")
59 (set-port-conversion-strategy! pt 'escape)
60 (display a-acute pt)
61 (string=? "Á"
62 (get-output-string pt))))
63
64 (pass-if "display alpha"
65 (let ((pt (open-output-string)))
66 (set-port-encoding! pt "ISO-8859-1")
67 (set-port-conversion-strategy! pt 'escape)
68 (display alpha pt)
69 (string-ci=? "\\u03b1"
70 (get-output-string pt))))
71
72 (pass-if "display Cherokee a"
73 (let ((pt (open-output-string)))
74 (set-port-encoding! pt "ISO-8859-1")
75 (set-port-conversion-strategy! pt 'escape)
76 (display cherokee-a pt)
77 (string-ci=? "\\u13a0"
78 (get-output-string pt))))
79
80 (pass-if "write A"
81 (let ((pt (open-output-string)))
82 (set-port-encoding! pt "ISO-8859-1")
83 (set-port-conversion-strategy! pt 'escape)
84 (write ascii-a pt)
85 (string=? "#\\A"
86 (get-output-string pt))))
87
88 (pass-if "write A acute"
89 (let ((pt (open-output-string)))
90 (set-port-encoding! pt "ISO-8859-1")
91 (set-port-conversion-strategy! pt 'escape)
92 (write a-acute pt)
93 (string=? "#\\Á"
94 (get-output-string pt)))))
95
96
97 (define s1 "última")
98 (define s2 "cédula")
99 (define s3 "años")
100 (define s4 "¿Cómo?")
101
102 (with-test-prefix "string length"
103
104 (pass-if "última"
105 (eq? (string-length s1) 6))
106
107 (pass-if "cédula"
108 (eq? (string-length s2) 6))
109
110 (pass-if "años"
111 (eq? (string-length s3) 4))
112
113 (pass-if "¿Cómo?"
114 (eq? (string-length s4) 6)))
115
116 (with-test-prefix "internal encoding"
117
118 (pass-if "última"
119 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
120
121 (pass-if "cédula"
122 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
123
124 (pass-if "años"
125 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
126
127 (pass-if "¿Cómo?"
128 (string=? s4 (string-ints #xbf #x43 #xf3 #x6d #x6f #x3f))))
129
130 (with-test-prefix "chars"
131
132 (pass-if "última"
133 (list= eqv? (string->list s1)
134 (list #\ú #\l #\t #\i #\m #\a)))
135
136 (pass-if "cédula"
137 (list= eqv? (string->list s2)
138 (list #\c #\é #\d #\u #\l #\a)))
139
140 (pass-if "años"
141 (list= eqv? (string->list s3)
142 (list #\a #\ñ #\o #\s)))
143
144 (pass-if "¿Cómo?"
145 (list= eqv? (string->list s4)
146 (list #\¿ #\C #\ó #\m #\o #\?))))
147
148 (with-test-prefix "symbols == strings"
149
150 (pass-if "última"
151 (eq? (string->symbol s1) 'última))
152
153 (pass-if "cédula"
154 (eq? (string->symbol s2) 'cédula))
155
156 (pass-if "años"
157 (eq? (string->symbol s3) 'años))
158
159 (pass-if "¿Cómo?"
160 (eq? (string->symbol s4) '¿Cómo?)))
161
162 (with-test-prefix "non-ascii variable names"
163
164 (pass-if "1"
165 (let ((á 1)
166 (ñ 2))
167 (eq? (+ á ñ) 3))))
168
169 (with-test-prefix "output errors"
170
171 (pass-if-exception "char 256" exception:conversion
172 (let ((pt (open-output-string)))
173 (set-port-encoding! pt "ISO-8859-1")
174 (set-port-conversion-strategy! pt 'error)
175 (display (string-ints 256) pt))))
176
177 ;; Reset locales
178 (if (defined? 'setlocale)
179 (setlocale LC_ALL oldlocale))