1 ;;;; encoding-utf8.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
3 ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
19 (define-module (test-strings)
20 #:use-module (test-suite lib)
21 #:use-module (srfi srfi-1))
23 ;; Create a string from integer char values, eg. (string-ints 65) => "A"
24 (define (string-ints . args)
25 (apply string (map integer->char args)))
28 (if (defined? 'setlocale)
29 (set! oldlocale (setlocale LC_ALL "")))
31 (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
32 (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
33 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
34 (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
36 (with-test-prefix "characters"
40 (pass-if "input A acute"
43 (pass-if "input alpha"
46 (pass-if "input Cherokee A"
47 (char=? cherokee-a #\Ꭰ))
50 (let ((pt (open-output-string)))
51 (set-port-encoding! pt "UTF-8")
52 (set-port-conversion-strategy! pt 'substitute)
55 (get-output-string pt))))
57 (pass-if "display A acute"
58 (let ((pt (open-output-string)))
59 (set-port-encoding! pt "UTF-8")
60 (set-port-conversion-strategy! pt 'substitute)
63 (get-output-string pt))))
65 (pass-if "display alpha"
66 (let ((pt (open-output-string)))
67 (set-port-encoding! pt "UTF-8")
68 (set-port-conversion-strategy! pt 'substitute)
71 (get-output-string pt))))
73 (pass-if "display Cherokee A"
74 (let ((pt (open-output-string)))
75 (set-port-encoding! pt "UTF-8")
76 (set-port-conversion-strategy! pt 'substitute)
77 (display cherokee-a pt)
79 (get-output-string pt))))
82 (let ((pt (open-output-string)))
83 (set-port-encoding! pt "UTF-8")
84 (set-port-conversion-strategy! pt 'escape)
87 (get-output-string pt))))
89 (pass-if "write A acute"
90 (let ((pt (open-output-string)))
91 (set-port-encoding! pt "UTF-8")
92 (set-port-conversion-strategy! pt 'escape)
95 (get-output-string pt))))
97 (pass-if "write A followed by combining accent"
98 (let ((pt (open-output-string)))
99 (set-port-encoding! pt "UTF-8")
100 (set-port-conversion-strategy! pt 'escape)
101 (write (string #\A (integer->char #x030f)) pt)
102 (string-ci=? "\"Ȁ\""
103 (get-output-string pt))))
105 (pass-if "write alpha"
106 (let ((pt (open-output-string)))
107 (set-port-encoding! pt "UTF-8")
108 (set-port-conversion-strategy! pt 'escape)
111 (get-output-string pt))))
113 (pass-if "write Cherokee A"
114 (let ((pt (open-output-string)))
115 (set-port-encoding! pt "UTF-8")
116 (set-port-conversion-strategy! pt 'escape)
117 (write cherokee-a pt)
119 (get-output-string pt)))))
126 (with-test-prefix "string length"
129 (eqv? (string-length s1) 6))
132 (eqv? (string-length s2) 6))
135 (eqv? (string-length s3) 4))
138 (eqv? (string-length s4) 3)))
140 (with-test-prefix "internal encoding"
143 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
146 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
149 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
152 (string=? s4 (string-ints #x7f85 #x751f #x9580))))
154 (with-test-prefix "chars"
157 (list= eqv? (string->list s1)
158 (list #\ú #\l #\t #\i #\m #\a)))
161 (list= eqv? (string->list s2)
162 (list #\c #\é #\d #\u #\l #\a)))
165 (list= eqv? (string->list s3)
166 (list #\a #\ñ #\o #\s)))
169 (list= eqv? (string->list s4)
170 (list #\羅 #\生 #\門))))
172 (with-test-prefix "symbols == strings"
175 (eq? (string->symbol s1) 'última))
178 (eq? (string->symbol s2) 'cédula))
181 (eq? (string->symbol s3) 'años))
184 (eq? (string->symbol s4) '羅生門)))
186 (with-test-prefix "non-ascii variable names"
191 (eqv? (+ 芥川龍之介 ñ) 3))))
193 (if (defined? 'setlocale)
194 (setlocale LC_ALL oldlocale))