Switch the `encoding*.test' files to LGPLv3+.
[bpt/guile.git] / test-suite / tests / encoding-iso88597.test
1 ;;;; encoding-iso88697.test --- test suite for Guile's string encodings -*- mode: scheme; coding: iso-8859-7 -*-
2 ;;;;
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library 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 GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-strings)
20 #:use-module (test-suite lib)
21 #:use-module (srfi srfi-1))
22
23 (define exception:conversion
24 (cons 'misc-error "^cannot convert to output locale"))
25
26 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
27 (define (string-ints . args)
28 (apply string (map integer->char args)))
29
30 (define oldlocale #f)
31 (if (defined? 'setlocale)
32 (set! oldlocale (setlocale LC_ALL "")))
33 (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
34 (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
35 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
36 (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
37
38 (with-test-prefix "characters"
39 (pass-if "input A"
40 (char=? ascii-a #\A))
41
42 (pass-if "input alpha"
43 (char=? alpha #\á))
44
45 (pass-if "display A"
46 (let ((pt (open-output-string)))
47 (set-port-encoding! pt "ISO-8859-7")
48 (set-port-conversion-strategy! pt 'escape)
49 (display ascii-a pt)
50 (string=? "A"
51 (get-output-string pt))))
52
53 (pass-if "display A acute"
54 (let ((pt (open-output-string)))
55 (set-port-encoding! pt "ISO-8859-7")
56 (set-port-conversion-strategy! pt 'escape)
57 (display a-acute pt)
58 (string-ci=? "\\xc1"
59 (get-output-string pt))))
60
61 (pass-if "display alpha"
62 (let ((pt (open-output-string)))
63 (set-port-encoding! pt "ISO-8859-7")
64 (set-port-conversion-strategy! pt 'escape)
65 (display alpha pt)
66 (string-ci=? "á"
67 (get-output-string pt))))
68
69 (pass-if "display Cherokee A"
70 (let ((pt (open-output-string)))
71 (set-port-encoding! pt "ISO-8859-7")
72 (set-port-conversion-strategy! pt 'escape)
73 (display cherokee-a pt)
74 (string-ci=? "\\u13a0"
75 (get-output-string pt))))
76
77 (pass-if "write A"
78 (let ((pt (open-output-string)))
79 (set-port-encoding! pt "ISO-8859-7")
80 (set-port-conversion-strategy! pt 'escape)
81 (write ascii-a pt)
82 (string=? "#\\A"
83 (get-output-string pt))))
84
85 (pass-if "write alpha"
86 (let ((pt (open-output-string)))
87 (set-port-encoding! pt "ISO-8859-7")
88 (set-port-conversion-strategy! pt 'escape)
89 (write alpha pt)
90 (string=? "#\\á"
91 (get-output-string pt)))))
92
93 (define s1 "Ðåñß")
94 (define s2 "ôçò")
95 (define s3 "êñéôéêÞò")
96 (define s4 "êáé")
97
98 (with-test-prefix "string length"
99
100 (pass-if "s1"
101 (eq? (string-length s1) 4))
102
103 (pass-if "s2"
104 (eq? (string-length s2) 3))
105
106 (pass-if "s3"
107 (eq? (string-length s3) 8))
108
109 (pass-if "s4"
110 (eq? (string-length s4) 3)))
111
112 (with-test-prefix "internal encoding"
113
114 (pass-if "s1"
115 (string=? s1 (string-ints #x03a0 #x03b5 #x03c1 #x03af)))
116
117 (pass-if "s2"
118 (string=? s2 (string-ints #x03c4 #x03b7 #x03c2)))
119
120 (pass-if "s3"
121 (string=? s3 (string-ints #x03ba #x03c1 #x03b9 #x03c4 #x03b9 #x03ba #x03ae #x03c2)))
122
123 (pass-if "s4"
124 (string=? s4 (string-ints #x03ba #x03b1 #x03b9))))
125
126 (with-test-prefix "chars"
127
128 (pass-if "s1"
129 (list= eqv? (string->list s1)
130 (list #\Ð #\å #\ñ #\ß)))
131
132 (pass-if "s2"
133 (list= eqv? (string->list s2)
134 (list #\ô #\ç #\ò)))
135
136 (pass-if "s3"
137 (list= eqv? (string->list s3)
138 (list #\ê #\ñ #\é #\ô #\é #\ê #\Þ #\ò)))
139
140 (pass-if "s4"
141 (list= eqv? (string->list s4)
142 (list #\ê #\á #\é))))
143
144 (with-test-prefix "symbols == strings"
145
146 (pass-if "Ðåñß"
147 (eq? (string->symbol s1) 'Ðåñß))
148
149 (pass-if "ôçò"
150 (eq? (string->symbol s2) 'ôçò))
151
152 (pass-if "êñéôéêÞò"
153 (eq? (string->symbol s3) 'êñéôéêÞò))
154
155 (pass-if "êáé"
156 (eq? (string->symbol s4) 'êáé)))
157
158 (with-test-prefix "non-ascii variable names"
159
160 (pass-if "1"
161 (let ((á 1)
162 (ñ 2))
163 (eq? (+ á ñ) 3))))
164
165 (with-test-prefix "output errors"
166
167 (pass-if-exception "char #x0400"
168 exception:conversion
169 (let ((pt (open-output-string)))
170 (set-port-encoding! pt "ISO-8859-7")
171 (set-port-conversion-strategy! pt 'error)
172 (display (string-ints #x0400) pt))))
173
174 ;; Reset locale
175 (if (defined? 'setlocale)
176 (setlocale LC_ALL oldlocale))