Debian Codegraph @

test/char.scm

raw
1 ;;
2 ;; Test for characters
3 ;;
4
5 (use gauche.test)
6
7 (test-start "characters")
8
9 (test-section "basic comparison")
10
11 ;; n-ary comparison
12 ;; ops is a list of comparison procedures
13 ;; data is a list (args = < <= > >=)
14 (define (char-cmp-matrix ops data)
15 (for-each (^d (let1 args (car d)
16 (for-each (^(op k) (test* (format "~a ~s" op args)
17 (~ d k)
18 (apply op args)))
19 ops
20 '(1 2 3 4 5))))
21 data))
22
23 (char-cmp-matrix
24 (list char=? char<? char<=? char>? char>=?)
25 ;; args = < <= > >=
26 '(((#\a #\a) #t #f #t #f #t)
27 ((#\a #\a #\a) #t #f #t #f #t)
28 ((#\a #\a #\a #\a) #t #f #t #f #t)
29 ((#\a #\b) #f #t #t #f #f)
30 ((#\a #\b #\b) #f #f #t #f #f)
31 ((#\a #\b #\b #\c) #f #f #t #f #f)
32 ((#\a #\b #\b #\a) #f #f #f #f #f)
33 ((#\a #\b #\c) #f #t #t #f #f)
34 ((#\a #\b #\c #\d) #f #t #t #f #f)
35 ((#\a #\b #\c #\b) #f #f #f #f #f)
36 ((#\c #\b) #f #f #f #t #t)
37 ((#\c #\b #\b) #f #f #f #f #t)
38 ((#\c #\b #\b #\a) #f #f #f #f #t)
39 ((#\c #\b #\b #\c) #f #f #f #f #f)
40 ((#\d #\c #\b #\a) #f #f #f #t #t)
41 ((#\d #\c #\b #\c) #f #f #f #f #f)
42 ))
43
44 (char-cmp-matrix
45 (list char-ci=? char-ci<? char-ci<=? char-ci>? char-ci>=?)
46 ;; args = < <= > >=
47 '(((#\A #\a) #t #f #t #f #t)
48 ((#\a #\A #\a) #t #f #t #f #t)
49 ((#\a #\A #\A #\a) #t #f #t #f #t)
50 ((#\a #\B) #f #t #t #f #f)
51 ((#\a #\B #\b) #f #f #t #f #f)
52 ((#\a #\B #\b #\C) #f #f #t #f #f)
53 ((#\a #\B #\b #\A) #f #f #f #f #f)
54 ((#\A #\b #\C) #f #t #t #f #f)
55 ((#\a #\B #\c #\D) #f #t #t #f #f)
56 ((#\a #\B #\c #\b) #f #f #f #f #f)
57 ((#\C #\b) #f #f #f #t #t)
58 ((#\C #\b #\B) #f #f #f #f #t)
59 ((#\C #\b #\B #\a) #f #f #f #f #t)
60 ((#\C #\b #\B #\c) #f #f #f #f #f)
61 ((#\D #\c #\B #\a) #f #f #f #t #t)
62 ((#\D #\c #\B #\c) #f #f #f #f #f)
63 ))
64 (test-section "case mappings and properties")
65
66 (let ()
67 ;; Case stuff
68 (define (t ch up down title fold)
69 (test* (format "case mapping ~a -> (upcase, downcase, titlecase, foldcase)" ch)
70 (list up down title fold)
71 (list (char-upcase ch)
72 (char-downcase ch)
73 (char-titlecase ch)
74 (char-foldcase ch))))
75
76 ;; ch up down title fold
77 (t #\i #\I #\i #\I #\i)
78 (t #\u00df #\u00df #\u00df #\u00df #\u00df) ; eszett
79 (t #\u03a3 #\u03a3 #\u03c3 #\u03a3 #\u03c3) ; sigma
80 (t #\u03c3 #\u03a3 #\u03c3 #\u03a3 #\u03c3) ; sigma
81 (t #\u03c2 #\u03a3 #\u03c2 #\u03a3 #\u03c3) ; final sigma
82
83 (t #\u00b5 #\u039c #\u00b5 #\u039c #\u03bc) ; micro sign
84 (t #\u00ff #\u0178 #\u00ff #\u0178 #\u00ff) ; y with diaeresis
85
86 (t #\u01f1 #\u01f1 #\u01f3 #\u01f2 #\u01f3) ; DZ -> DZ dz Dz dz
87 (t #\u01f2 #\u01f1 #\u01f3 #\u01f2 #\u01f3) ; Dz -> DZ dz Dz dz
88 (t #\u01f3 #\u01f1 #\u01f3 #\u01f2 #\u01f3) ; dz -> DZ dz Dz dz
89 )
90
91 ;; In the following tests, we check if the character is supported in
92 ;; the running platform by comparing the literal character with #\? or
93 ;; #\u3013 --- these are alternative characters replaced if the given
94 ;; literal character isn't supported on the platform. When adding tests,
95 ;; be careful not to test with #\? or #\u3013, for such tests won't be
96 ;; executed.
97
98 (let ()
99 (define (unsupported? ch) (or (eqv? ch #\?) (eqv? ch #\u3013)))
100
101 (let-syntax ([test2 (syntax-rules ()
102 [(_ fn c0 c1 expected)
103 (let ([t0 c0] [t1 c1])
104 (unless (or (unsupported? t0) (unsupported? t1))
105 (test* '(fn c0 c1) expected (fn t0 t1))))])]
106 [test1 (syntax-rules ()
107 [(_ fn c0 expected)
108 (let ([t0 c0])
109 (unless (unsupported? t0)
110 (test* '(fn c0) expected (fn t0))))])])
111
112 (test2 char-ci<? #\z #\Z #f)
113 (test2 char-ci<? #\Z #\z #f)
114 (test2 char-ci<? #\a #\Z #t)
115 (test2 char-ci<? #\Z #\a #f)
116 (test2 char-ci<=? #\z #\Z #t)
117 (test2 char-ci<=? #\Z #\z #t)
118 (test2 char-ci<=? #\a #\Z #t)
119 (test2 char-ci<=? #\Z #\a #f)
120 (test2 char-ci=? #\z #\a #f)
121 (test2 char-ci=? #\z #\Z #t)
122 (test2 char-ci=? #\u03c2 #\u03c3 #t) ; downcase sigma
123 (test2 char-ci=? #\u03b9 #\u0345 #t) ; downcase iota vs subsctipt iota
124 (test2 char-ci>? #\z #\Z #f)
125 (test2 char-ci>? #\Z #\z #f)
126 (test2 char-ci>? #\a #\Z #f)
127 (test2 char-ci>? #\Z #\a #t)
128 (test2 char-ci>=? #\Z #\z #t)
129 (test2 char-ci>=? #\z #\Z #t)
130 (test2 char-ci>=? #\z #\Z #t)
131 (test2 char-ci>=? #\a #\z #f)
132
133 (test1 char-alphabetic? #\a #t)
134 (test1 char-alphabetic? #\1 #f)
135 (test1 char-numeric? #\1 #t)
136 (test1 char-numeric? #\a #f)
137 (test1 char-whitespace? #\space #t)
138 (test1 char-whitespace? #\u00A0 #t)
139 (test1 char-whitespace? #\a #f)
140 (test1 char-upper-case? #\a #f)
141 (test1 char-upper-case? #\A #t)
142 (test1 char-upper-case? #\u03a3 #t) ; large sigma
143 (test1 char-lower-case? #\a #t)
144 (test1 char-lower-case? #\A #f)
145 (test1 char-lower-case? #\u03c3 #t) ; small sigma
146 (test1 char-lower-case? #\u00AA #t) ; feminine ordinal indicator
147 (test1 char-title-case? #\a #f)
148 (test1 char-title-case? #\A #f)
149 (test1 char-title-case? #\I #f)
150 (test1 char-title-case? #\u3004 #f) ; JIS mark
151 (test1 char-title-case? #\u01C5 #t) ; Dz with caron
152
153 (test1 char-general-category #\a 'Ll)
154 (test1 char-general-category #\space 'Zs)
155 (test1 char-general-category #\u10FFFF 'Cn)
156 ))
157
158 (let ()
159 (let-syntax ([t0
160 (syntax-rules ()
161 [(_ ch fn exp)
162 (test* (format "~a #\\~4,'0x" 'fn (char->integer ch))
163 exp (fn ch))])])
164 (define (t ch alpha? upper? lower? cat)
165 (t0 ch char-alphabetic? alpha?)
166 (t0 ch char-upper-case? upper?)
167 (t0 ch char-lower-case? lower?)
168 (t0 ch char-general-category cat))
169 ))
170
171 ;; Built-in char-set tests
172 ;; NB: We test some large-char range if we have utf8 ces. More comprehensive
173 ;; tests are in multibyte.scm.
174
175 ;; char-set writer
176 (let ()
177 (define (char-set-printer-tester p)
178 (test* "char-set-printer" (car p)
179 (write-to-string (apply char-set (cdr p)))))
180 (for-each char-set-printer-tester
181 '(("#[ace]" #\a #\e #\c)
182 ("#[ab]" #\a #\b)
183 ("#[a-c]" #\a #\b #\c)
184 ("#[a-d]" #\a #\b #\c #\d)
185 ("#[a-ce]" #\a #\b #\c #\e)
186 ("#[acd]" #\a #\c #\d)
187 ("#[ac-e]" #\a #\c #\d #\e)
188 ("#[ac-e]" #\a #\c #\d #\e)
189 ("#[\\-\\[\\]]" #\[ #\] #\-)
190 ("#[\\^a]" #\^ #\a)
191 ("#[!^]" #\^ #\!))))
192
193 ;; Test both mutable and immutable version
194 (define (test-cs name expect proc arg1 :optional (arg2 #f))
195 (if arg2
196 (let ([iarg1 (char-set-freeze arg1)]
197 [iarg2 (char-set-freeze arg2)])
198 (test* name (make-list 4 expect)
199 (list (proc arg1 arg2)
200 (proc iarg1 arg2)
201 (proc arg1 iarg2)
202 (proc iarg1 iarg2))))
203 (test* name (list expect expect)
204 (list (proc arg1) (proc (char-set-freeze arg1))))))
205
206 (test* "char-set?" #f (char-set? 5))
207 (test-cs "char-set?" #t char-set? (char-set #\a #\e #\i #\o #\u))
208
209 ;; N-ary comparison procedures are provided in SRFI-14. We test primitive
210 ;; ones here.
211 (test-cs "%char-set-equal?" #t
212 (with-module gauche.internal %char-set-equal?)
213 (char-set #\a #\e #\i #\o #\u)
214 (char-set #\a #\i #\e #\u #\o))
215 (test-cs "%char-set-equal?" #f
216 (with-module gauche.internal %char-set-equal?)
217 (char-set #\a #\e #\i #\o #\q)
218 (char-set #\a #\e #\i #\o #\u))
219 (test-cs "%char-set-equal?" #t
220 (with-module gauche.internal %char-set-equal?)
221 (char-set #\a #\b #\u3000 #\u3001 #\u3002 #\u3030 #\u3031 #\u3032)
222 (char-set #\u3000 #\u3030 #\a #\u3002 #\u3031 #\b #\u3001 #\u3032))
223 (test-cs "%char-set<=?" #t
224 (with-module gauche.internal %char-set<=?)
225 (char-set #\a #\e #\i #\o #\u)
226 (char-set #\a #\i #\e #\u #\o))
227 (test-cs "%char-set<=?" #t
228 (with-module gauche.internal %char-set<=?)
229 (char-set #\a #\i #\u)
230 (char-set #\a #\i #\e #\u #\o))
231 (test-cs "%char-set<=?" #f
232 (with-module gauche.internal %char-set<=?)
233 (char-set #\a #\e #\i #\o #\q)
234 (char-set #\a #\e #\i #\o #\u))
235 (test-cs "%char-set<=?" #f
236 (with-module gauche.internal %char-set<=?)
237 (char-set #\a #\i #\e #\u #\o)
238 (char-set #\a #\i #\u))
239 (test-cs "%char-set<=?" #t
240 (with-module gauche.internal %char-set<=?)
241 (char-set #\a #\b #\u3000 #\u3001 #\u3002 #\u3030 #\u3031 #\u3032)
242 (char-set #\u3000 #\u3030 #\a #\u3002 #\u3031 #\b #\u3001 #\u3032))
243 (test-cs "%char-set<=?" #t
244 (with-module gauche.internal %char-set<=?)
245 (char-set #\a #\b #\u3000 #\u3001 #\u3002 #\u3030 #\u3032)
246 (char-set #\u3000 #\u3030 #\a #\u3002 #\u3031 #\b #\u3001 #\u3032))
247 (test-cs "%char-set<=?" #f
248 (with-module gauche.internal %char-set<=?)
249 (char-set #\u3000 #\u3030 #\a #\u3002 #\u3031 #\b #\u3001 #\u3032)
250 (char-set #\a #\b #\u3000 #\u3001 #\u3002 #\u3030 #\u3032))
251
252 ;; predefined charsets
253 ;; here we just see if all of them are indeed charsets.
254 (test* "predefined-charsets" #t
255 (every char-set?
256 (list char-set:lower-case
257 char-set:ascii-lower-case
258 char-set:upper-case
259 char-set:ascii-upper-case
260 char-set:title-case
261 char-set:letter
262 char-set:ascii-letter
263 char-set:digit
264 char-set:ascii-digit
265 char-set:letter+digit
266 char-set:ascii-letter+digit
267 char-set:graphic
268 char-set:ascii-graphic
269 char-set:printing
270 char-set:ascii-printing
271 char-set:whitespace
272 char-set:ascii-whitespace
273 char-set:iso-control
274 char-set:ascii-control
275 char-set:punctuation
276 char-set:ascii-punctuation
277 char-set:symbol
278 char-set:ascii-symbol
279 char-set:hex-digit
280 char-set:blank
281 char-set:ascii-blank
282 char-set:ascii
283 char-set:empty
284 char-set:full
285 char-set:word
286 char-set:ascii-word)))
287
288 (test* "\\p and \\P syntax (\\p{Lu})" #t
289 (equal? #[\p{Lu}] char-set:Lu))
290 (test* "\\p and \\P syntax (\\P{Nd})" #t
291 (equal? #[\P{Nd}] (char-set-complement char-set:Nd)))
292 (test* "\\p and \\P syntax(\\p{Lu}a)" #t
293 (and (#[\p{Lu}a] #\A)
294 (#[\p{Lu}a] #\a)))
295 (test* "\\p and \\P syntax (\\p{L}\\P{L})" #t
296 (equal? #[\P{L}\p{L}] char-set:full))
297
298 (test-end)