1 ;;;; strings.test --- test suite for Guile's string functions -*- mode: scheme; coding: iso-8859-1 -*-
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)))
31 ;; Set locale to the environment's locale, so that the prints look OK.
33 (if (defined? 'setlocale)
34 (set! oldlocale (setlocale LC_ALL "")))
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
41 (with-test-prefix "characters"
45 (pass-if "input A acute"
49 (let ((pt (open-output-string)))
50 (set-port-encoding! pt "ISO-8859-1")
51 (set-port-conversion-strategy! pt 'escape)
54 (get-output-string pt))))
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)
62 (get-output-string pt))))
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)
69 (string-ci=? "\\u03b1"
70 (get-output-string pt))))
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))))
81 (let ((pt (open-output-string)))
82 (set-port-encoding! pt "ISO-8859-1")
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 "ISO-8859-1")
91 (set-port-conversion-strategy! pt 'escape)
94 (get-output-string pt)))))
102 (with-test-prefix "string length"
105 (eq? (string-length s1) 6))
108 (eq? (string-length s2) 6))
111 (eq? (string-length s3) 4))
114 (eq? (string-length s4) 6)))
116 (with-test-prefix "internal encoding"
119 (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))
122 (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))
125 (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
128 (string=? s4 (string-ints #xbf #x43 #xf3 #x6d #x6f #x3f))))
130 (with-test-prefix "chars"
133 (list= eqv? (string->list s1)
134 (list #\ú #\l #\t #\i #\m #\a)))
137 (list= eqv? (string->list s2)
138 (list #\c #\é #\d #\u #\l #\a)))
141 (list= eqv? (string->list s3)
142 (list #\a #\ñ #\o #\s)))
145 (list= eqv? (string->list s4)
146 (list #\¿ #\C #\ó #\m #\o #\?))))
148 (with-test-prefix "symbols == strings"
151 (eq? (string->symbol s1) 'última))
154 (eq? (string->symbol s2) 'cédula))
157 (eq? (string->symbol s3) 'años))
160 (eq? (string->symbol s4) '¿Cómo?)))
162 (with-test-prefix "non-ascii variable names"
169 (with-test-prefix "output errors"
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))))
178 (if (defined? 'setlocale)
179 (setlocale LC_ALL oldlocale))