1 ;;;; strings.test --- test suite for Guile's string functions -*- mode: scheme; coding: utf-8 -*-
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-strings)
21 #:use-module (test-suite lib)
22 #:use-module (srfi srfi-1))
24 (define exception:conversion
25 (cons 'misc-error "^cannot convert to output locale"))
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)))
32 (if (defined? 'setlocale)
33 (set! oldlocale (setlocale LC_ALL "")))
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
40 (with-test-prefix "characters"
44 (pass-if "input A acute"
47 (pass-if "input alpha"
50 (pass-if "input Cherokee A"
51 (char=? cherokee-a #\Ꭰ))
54 (let ((pt (open-output-string)))
55 (set-port-encoding! pt "UTF-8")
56 (set-port-conversion-strategy! pt 'substitute)
59 (get-output-string pt))))
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)
67 (get-output-string pt))))
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)
75 (get-output-string pt))))
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)
83 (get-output-string pt))))
86 (let ((pt (open-output-string)))
87 (set-port-encoding! pt "UTF-8")
88 (set-port-conversion-strategy! pt 'escape)
91 (get-output-string pt))))
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)
99 (get-output-string pt))))
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)
107 (get-output-string pt))))
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)
115 (get-output-string pt)))))
122 (with-test-prefix "string length"
125 (eq? (string-length s1) 6))
128 (eq? (string-length s2) 6))
131 (eq? (string-length s3) 4))
134 (eq? (string-length s4) 3)))
136 (with-test-prefix "internal encoding"
139 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
142 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
145 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
148 (string=? s4 (string-ints #x7f85 #x751f #x9580))))
150 (with-test-prefix "chars"
153 (list= eqv? (string->list s1)
154 (list #\ú #\l #\t #\i #\m #\a)))
157 (list= eqv? (string->list s2)
158 (list #\c #\é #\d #\u #\l #\a)))
161 (list= eqv? (string->list s3)
162 (list #\a #\ñ #\o #\s)))
165 (list= eqv? (string->list s4)
166 (list #\羅 #\生 #\門))))
168 (with-test-prefix "symbols == strings"
171 (eq? (string->symbol s1) 'última))
174 (eq? (string->symbol s2) 'cédula))
177 (eq? (string->symbol s3) 'años))
180 (eq? (string->symbol s4) '羅生門)))
182 (with-test-prefix "non-ascii variable names"
187 (eq? (+ 芥川龍之介 ñ) 3))))
189 (if (defined? 'setlocale)
190 (setlocale LC_ALL oldlocale))