summaryrefslogtreecommitdiff
path: root/test-suite/tests/encoding-utf8.test
blob: d2bdb6901e8986824546a24b98540762f2269212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
;;;; strings.test --- test suite for Guile's string functions    -*- mode: scheme; coding: utf-8 -*-
;;;;
;;;; Copyright (C) 2009 Free Software Foundation, Inc.
;;;; 
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
;;;; 
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING.  If not, write to
;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;;;; Boston, MA 02110-1301 USA

(define-module (test-strings)
  #:use-module (test-suite lib)
  #:use-module (srfi srfi-1))

(define exception:conversion
  (cons 'misc-error "^cannot convert to output locale"))

;; Create a string from integer char values, eg. (string-ints 65) => "A"
(define (string-ints . args)
  (apply string (map integer->char args)))

(define oldlocale #f)
(if (defined? 'setlocale)
    (set! oldlocale (setlocale LC_ALL "")))

(define ascii-a (integer->char 65))     ; LATIN CAPITAL LETTER A
(define a-acute (integer->char #x00c1)) ; LATIN CAPITAL LETTER A WITH ACUTE
(define alpha (integer->char #x03b1))   ; GREEK SMALL LETTER ALPHA
(define cherokee-a (integer->char #x13a0)) ; CHEROKEE LETTER A

(with-test-prefix "characters"
  (pass-if "input A"
    (char=? ascii-a #\A))

  (pass-if "input A acute"
    (char=? a-acute #\Á))
 
  (pass-if "input alpha"
    (char=? alpha #\α))
 
  (pass-if "input Cherokee A"
    (char=? cherokee-a #\Ꭰ))
 
 (pass-if "display A"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'substitute)
             (display ascii-a pt)
             (string=? "A"
                       (get-output-string pt))))

  (pass-if "display A acute"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'substitute)
             (display a-acute pt)
             (string=? "Á"
                       (get-output-string pt))))

  (pass-if "display alpha"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'substitute)
             (display alpha pt)
             (string-ci=? "α"
                       (get-output-string pt))))

  (pass-if "display Cherokee A"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'substitute)
             (display cherokee-a pt)
             (string-ci=? "Ꭰ"
                       (get-output-string pt))))

  (pass-if "write A"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'escape)
             (write ascii-a pt)
             (string=? "#\\A"
                       (get-output-string pt))))

  (pass-if "write A acute"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'escape)
             (write a-acute pt)
             (string=? "#\\Á"
                       (get-output-string pt))))

  (pass-if "write alpha"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'escape)
             (write alpha pt)
             (string=? "#\\α"
                       (get-output-string pt))))

  (pass-if "write Cherokee A"
           (let ((pt (open-output-string)))
             (set-port-encoding! pt "UTF-8")
             (set-port-conversion-strategy! pt 'escape)
             (write cherokee-a pt)
             (string=? "#\\Ꭰ"
                       (get-output-string pt)))))

(define s1 "última")
(define s2 "cédula")
(define s3 "años")
(define s4 "羅生門")

(with-test-prefix "string length"

  (pass-if "última"
	   (eq? (string-length s1) 6))
    
  (pass-if "cédula"
	   (eq? (string-length s2) 6))

  (pass-if "años"
	   (eq? (string-length s3) 4))

  (pass-if "羅生門"
	   (eq? (string-length s4) 3)))

(with-test-prefix "internal encoding"

  (pass-if "última"
	   (string=? s1 (string-ints #xfa #x6c #x74 #x69 #x6d #x61)))

  (pass-if "cédula"
	   (string=? s2 (string-ints #x63 #xe9 #x64 #x75 #x6c #x61)))

  (pass-if "años"
	   (string=? s3 (string-ints #x61 #xf1 #x6f #x73)))
 
  (pass-if "羅生門"
	   (string=? s4 (string-ints #x7f85 #x751f #x9580))))

(with-test-prefix "chars"
 
  (pass-if "última"
	   (list= eqv? (string->list s1)
		  (list #\ú #\l #\t #\i #\m #\a)))

  (pass-if "cédula"
	   (list= eqv? (string->list s2)
		  (list #\c #\é #\d #\u #\l #\a)))

  (pass-if "años"
	   (list= eqv? (string->list s3)
		  (list #\a #\ñ #\o #\s)))

  (pass-if "羅生門"
	   (list= eqv? (string->list s4)
		  (list #\羅 #\生 #\門))))

(with-test-prefix "symbols == strings"

  (pass-if "última"
	   (eq? (string->symbol s1) 'última))

  (pass-if "cédula"
	   (eq? (string->symbol s2) 'cédula))

  (pass-if "años"
	   (eq? (string->symbol s3) 'años))
 
  (pass-if "羅生門"
	   (eq? (string->symbol s4) '羅生門)))

(with-test-prefix "non-ascii variable names"

  (pass-if "1"
	   (let ((芥川龍之介  1)
		 (ñ 2))
	     (eq? (+  芥川龍之介 ñ) 3))))

(if (defined? 'setlocale)
    (setlocale LC_ALL oldlocale))