1 ;;;; encoding-utf8.test --- test suite for Guile's string encodings -*- mode: scheme; coding: utf-8 -*-
3 ;;;; Copyright (C) 2009, 2010, 2014 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)))
27 (when (defined? 'setlocale)
28 (setlocale LC_ALL ""))
30 (define ascii-a (integer->char 65)) ; LATIN CAPITAL LETTER A
31 (define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
32 (define alpha (integer->char #x03b1)) ; GREEK SMALL LETTER ALPHA
33 (define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A
35 (with-test-prefix "characters"
39 (pass-if "input A acute"
42 (pass-if "input alpha"
45 (pass-if "input Cherokee A"
46 (char=? cherokee-a #\Ꭰ))
49 (let ((pt (open-output-string)))
50 (set-port-encoding! pt "UTF-8")
51 (set-port-conversion-strategy! pt 'substitute)
54 (get-output-string pt))))
56 (pass-if "display A acute"
57 (let ((pt (open-output-string)))
58 (set-port-encoding! pt "UTF-8")
59 (set-port-conversion-strategy! pt 'substitute)
62 (get-output-string pt))))
64 (pass-if "display alpha"
65 (let ((pt (open-output-string)))
66 (set-port-encoding! pt "UTF-8")
67 (set-port-conversion-strategy! pt 'substitute)
70 (get-output-string pt))))
72 (pass-if "display Cherokee A"
73 (let ((pt (open-output-string)))
74 (set-port-encoding! pt "UTF-8")
75 (set-port-conversion-strategy! pt 'substitute)
76 (display cherokee-a pt)
78 (get-output-string pt))))
81 (let ((pt (open-output-string)))
82 (set-port-encoding! pt "UTF-8")
83 (set-port-conversion-strategy! pt 'escape)
86 (get-output-string pt))))
88 (pass-if "write A acute"
89 (let ((pt (open-output-string)))
90 (set-port-encoding! pt "UTF-8")
91 (set-port-conversion-strategy! pt 'escape)
94 (get-output-string pt))))
96 (pass-if "write A followed by combining accent"
97 (let ((pt (open-output-string)))
98 (set-port-encoding! pt "UTF-8")
99 (set-port-conversion-strategy! pt 'escape)
100 (write (string #\A (integer->char #x030f)) pt)
101 (string-ci=? "\"Ȁ\""
102 (get-output-string pt))))
104 (pass-if "write alpha"
105 (let ((pt (open-output-string)))
106 (set-port-encoding! pt "UTF-8")
107 (set-port-conversion-strategy! pt 'escape)
110 (get-output-string pt))))
112 (pass-if "write Cherokee A"
113 (let ((pt (open-output-string)))
114 (set-port-encoding! pt "UTF-8")
115 (set-port-conversion-strategy! pt 'escape)
116 (write cherokee-a pt)
118 (get-output-string pt)))))
125 (with-test-prefix "string length"
128 (eqv? (string-length s1) 6))
131 (eqv? (string-length s2) 6))
134 (eqv? (string-length s3) 4))
137 (eqv? (string-length s4) 3)))
139 (with-test-prefix "internal encoding"
142 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
145 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
148 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
151 (string=? s4 (string-ints #x7f85 #x751f #x9580))))
153 (with-test-prefix "chars"
156 (list= eqv? (string->list s1)
157 (list #\ú #\l #\t #\i #\m #\a)))
160 (list= eqv? (string->list s2)
161 (list #\c #\é #\d #\u #\l #\a)))
164 (list= eqv? (string->list s3)
165 (list #\a #\ñ #\o #\s)))
168 (list= eqv? (string->list s4)
169 (list #\羅 #\生 #\門))))
171 (with-test-prefix "symbols == strings"
174 (eq? (string->symbol s1) 'última))
177 (eq? (string->symbol s2) 'cédula))
180 (eq? (string->symbol s3) 'años))
183 (eq? (string->symbol s4) '羅生門)))
185 (with-test-prefix "non-ascii variable names"
190 (eqv? (+ 芥川龍之介 ñ) 3))))