1 ;;;; srfi-14.test -*- mode:scheme; coding: iso-8859-1 -*-
2 ;;;; --- Test suite for Guile's SRFI-14 functions.
3 ;;;; Martin Grabmueller, 2001-07-16
5 ;;;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
7 ;;;; This library is free software; you can redistribute it and/or
8 ;;;; modify it under the terms of the GNU Lesser General Public
9 ;;;; License as published by the Free Software Foundation; either
10 ;;;; version 3 of the License, or (at your option) any later version.
12 ;;;; This library is distributed in the hope that it will be useful,
13 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;;;; Lesser General Public License for more details.
17 ;;;; You should have received a copy of the GNU Lesser General Public
18 ;;;; License along with this library; if not, write to the Free Software
19 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (test-suite test-srfi-14)
22 :use-module (srfi srfi-14)
23 :use-module (srfi srfi-1) ;; `every'
24 :use-module (test-suite lib))
27 (define exception:invalid-char-set-cursor
28 (cons 'misc-error "^invalid character set cursor"))
30 (define exception:non-char-return
31 (cons 'misc-error "returned non-char"))
34 (with-test-prefix "char set contents"
38 (char-set->list (char-set))
41 (pass-if "single char"
43 (char-set->list (char-set #\a))
46 (pass-if "contiguous chars"
48 (char-set->list (char-set #\a #\b #\c))
51 (pass-if "discontiguous chars"
53 (char-set->list (char-set #\a #\c #\e))
57 (with-test-prefix "char-set?"
59 (pass-if "success on empty set"
60 (char-set? (char-set)))
62 (pass-if "success on non-empty set"
63 (char-set? char-set:printing))
65 (pass-if "failure on empty set"
66 (not (char-set? #t))))
69 (with-test-prefix "char-set="
70 (pass-if "success, no arg"
73 (pass-if "success, one arg"
74 (char-set= char-set:lower-case))
76 (pass-if "success, two args"
77 (char-set= char-set:upper-case char-set:upper-case))
79 (pass-if "failure, first empty"
80 (not (char-set= (char-set) (char-set #\a))))
82 (pass-if "failure, second empty"
83 (not (char-set= (char-set #\a) (char-set))))
85 (pass-if "success, more args"
86 (char-set= char-set:blank char-set:blank char-set:blank)))
88 (with-test-prefix "char-set<="
89 (pass-if "success, no arg"
92 (pass-if "success, one arg"
93 (char-set<= char-set:lower-case))
95 (pass-if "success, two args"
96 (char-set<= char-set:upper-case char-set:upper-case))
98 (pass-if "success, first empty"
99 (char-set<= (char-set) (char-set #\a)))
101 (pass-if "failure, second empty"
102 (not (char-set<= (char-set #\a) (char-set))))
104 (pass-if "success, more args, equal"
105 (char-set<= char-set:blank char-set:blank char-set:blank))
107 (pass-if "success, more args, not equal"
108 (char-set<= char-set:blank
109 (char-set-adjoin char-set:blank #\F)
110 (char-set-adjoin char-set:blank #\F #\o))))
112 (with-test-prefix "char-set-hash"
113 (pass-if "empty set, bound"
114 (let ((h (char-set-hash char-set:empty 31)))
115 (and h (number? h) (exact? h) (>= h 0) (< h 31))))
117 (pass-if "empty set, no bound"
118 (let ((h (char-set-hash char-set:empty)))
119 (and h (number? h) (exact? h) (>= h 0))))
121 (pass-if "full set, bound"
122 (let ((h (char-set-hash char-set:full 31)))
123 (and h (number? h) (exact? h) (>= h 0) (< h 31))))
125 (pass-if "full set, no bound"
126 (let ((h (char-set-hash char-set:full)))
127 (and h (number? h) (exact? h) (>= h 0))))
129 (pass-if "other set, bound"
130 (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r) 31)))
131 (and h (number? h) (exact? h) (>= h 0) (< h 31))))
133 (pass-if "other set, no bound"
134 (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r))))
135 (and h (number? h) (exact? h) (>= h 0)))))
138 (with-test-prefix "char-set cursor"
140 (pass-if-exception "invalid character cursor"
141 exception:wrong-type-arg
142 (let* ((cs (char-set #\B #\r #\a #\z))
143 (cc (char-set-cursor cs)))
144 (char-set-ref cs 1000)))
147 (let* ((cs (char-set #\B #\r #\a #\z))
148 (cc (char-set-cursor cs)))
149 (char? (char-set-ref cs cc))))
151 (pass-if "end of set fails"
152 (let* ((cs (char-set #\a))
153 (cc (char-set-cursor cs)))
154 (not (end-of-char-set? cc))))
156 (pass-if "end of set succeeds, empty set"
157 (let* ((cs (char-set))
158 (cc (char-set-cursor cs)))
159 (end-of-char-set? cc)))
161 (pass-if "end of set succeeds, non-empty set"
162 (let* ((cs (char-set #\a))
163 (cc (char-set-cursor cs))
164 (cc (char-set-cursor-next cs cc)))
165 (end-of-char-set? cc))))
167 (with-test-prefix "char-set-fold"
169 (pass-if "count members"
170 (= (char-set-fold (lambda (c n) (+ n 1)) 0 (char-set #\a #\b)) 2))
173 (= (char-set-size (char-set-fold (lambda (c cs) (char-set-adjoin cs c))
174 (char-set) (char-set #\a #\b))) 2)))
177 (string->char-set (apply string (map integer->char (iota 256)))))
179 (with-test-prefix "char-set-unfold"
181 (pass-if "create char set"
182 (char-set= char-set:256
183 (char-set-unfold (lambda (s) (= s 256)) integer->char
184 (lambda (s) (+ s 1)) 0)))
185 (pass-if "create char set (base set)"
186 (char-set= char-set:256
187 (char-set-unfold (lambda (s) (= s 256)) integer->char
188 (lambda (s) (+ s 1)) 0 char-set:empty))))
190 (with-test-prefix "char-set-unfold!"
192 (pass-if "create char set"
193 (char-set= char-set:256
194 (char-set-unfold! (lambda (s) (= s 256)) integer->char
195 (lambda (s) (+ s 1)) 0
196 (char-set-copy char-set:empty))))
198 (pass-if "create char set"
199 (char-set= char-set:256
200 (char-set-unfold! (lambda (s) (= s 32)) integer->char
201 (lambda (s) (+ s 1)) 0
202 (char-set-copy char-set:256)))))
205 (with-test-prefix "char-set-for-each"
207 (pass-if "copy char set"
208 (= (char-set-size (let ((cs (char-set)))
210 (lambda (c) (char-set-adjoin! cs c))
215 (with-test-prefix "char-set-map"
217 (pass-if "upper case char set 1"
218 (char-set= (char-set-map char-upcase
219 (string->char-set "abcdefghijklmnopqrstuvwxyz"))
220 (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")))
222 (pass-if "upper case char set 2"
223 (char-set= (char-set-map char-upcase
224 (string->char-set "àáâãäåæçèéêëìíîïñòóôõöøùúûüýþ"))
225 (string->char-set "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝÞ"))))
227 (with-test-prefix "string->char-set"
229 (pass-if "some char set"
230 (let ((chars '(#\g #\u #\i #\l #\e)))
231 (char-set= (list->char-set chars)
232 (string->char-set (apply string chars))))))
234 (with-test-prefix "char-set->string"
236 (pass-if "some char set"
237 (let ((cs (char-set #\g #\u #\i #\l #\e)))
238 (string=? (char-set->string cs)
241 (with-test-prefix "standard char sets (ASCII)"
243 (pass-if "char-set:lower-case"
244 (char-set<= (string->char-set "abcdefghijklmnopqrstuvwxyz")
245 char-set:lower-case))
247 (pass-if "char-set:upper-case"
248 (char-set<= (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
249 char-set:upper-case))
251 (pass-if "char-set:title-case"
252 (char-set<= (string->char-set "")
253 char-set:title-case))
255 (pass-if "char-set:letter"
256 (char-set<= (char-set-union
257 (string->char-set "abcdefghijklmnopqrstuvwxyz")
258 (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
261 (pass-if "char-set:digit"
262 (char-set<= (string->char-set "0123456789")
265 (pass-if "char-set:hex-digit"
266 (char-set<= (string->char-set "0123456789abcdefABCDEF")
269 (pass-if "char-set:letter+digit"
270 (char-set<= (char-set-union
271 (string->char-set "abcdefghijklmnopqrstuvwxyz")
272 (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
273 (string->char-set "0123456789"))
274 char-set:letter+digit))
276 (pass-if "char-set:punctuation"
277 (char-set<= (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}")
278 char-set:punctuation))
280 (pass-if "char-set:symbol"
281 (char-set<= (string->char-set "$+<=>^`|~")
284 (pass-if "char-set:graphic"
285 (char-set<= (char-set-union
286 (string->char-set "abcdefghijklmnopqrstuvwxyz")
287 (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
288 (string->char-set "0123456789")
289 (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}")
290 (string->char-set "$+<=>^`|~"))
293 (pass-if "char-set:whitespace"
294 (char-set<= (string->char-set
301 (integer->char #x20)))
302 char-set:whitespace))
304 (pass-if "char-set:printing"
305 (char-set<= (char-set-union
306 (string->char-set "abcdefghijklmnopqrstuvwxyz")
307 (string->char-set "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
308 (string->char-set "0123456789")
309 (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}")
310 (string->char-set "$+<=>^`|~")
311 (string->char-set (string
317 (integer->char #x20))))
320 (pass-if "char-set:iso-control"
321 (char-set<= (string->char-set
323 (map integer->char (append
327 char-set:iso-control)))
331 ;;; Non-ASCII codepoints
333 ;;; Here, we only test ISO-8859-1 (Latin-1), notably because behavior of
334 ;;; SRFI-14 for implementations supporting this charset is well-defined.
337 (define (every? pred lst)
338 (not (not (every pred lst))))
340 (define oldlocale #f)
341 (if (defined? 'setlocale)
342 (set! oldlocale (setlocale LC_ALL "")))
344 (with-test-prefix "Latin-1 (8-bit charset)"
346 (pass-if "char-set:lower-case"
347 (char-set<= (string->char-set
348 (string-append "abcdefghijklmnopqrstuvwxyz"
349 "µßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ")
350 char-set:lower-case)))
352 (pass-if "char-set:upper-case"
353 (char-set<= (string->char-set
354 (string-append "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
355 "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ")
356 char-set:lower-case)))
358 (pass-if "char-set:title-case"
359 (char-set<= (string->char-set "")
360 char-set:title-case))
362 (pass-if "char-set:letter"
363 (char-set<= (string->char-set
366 "abcdefghijklmnopqrstuvwxyz"
367 "µßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ"
369 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
370 "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ"
375 (pass-if "char-set:digit"
376 (char-set<= (string->char-set "0123456789")
379 (pass-if "char-set:hex-digit"
380 (char-set<= (string->char-set "0123456789abcdefABCDEF")
383 (pass-if "char-set:letter+digit"
384 (char-set<= (char-set-union
387 char-set:letter+digit))
389 (pass-if "char-set:punctuation"
390 (char-set<= (string->char-set
391 (string-append "!\"#%&'()*,-./:;?@[\\]_{}"
393 char-set:punctuation))
395 (pass-if "char-set:symbol"
396 (char-set<= (string->char-set
397 (string-append "$+<=>^`|~"
398 "¢£¤¥¦§¨©¬®¯°±´¶¸×÷"))
401 ;; Note that SRFI-14 itself is inconsistent here. Characters that
402 ;; are non-digit numbers (such as category No) are clearly 'graphic'
403 ;; but don't occur in the letter, digit, punct, or symbol charsets.
404 (pass-if "char-set:graphic"
405 (char-set<= (char-set-union
412 (pass-if "char-set:whitespace"
413 (char-set<= (string->char-set
421 (integer->char #xa0)))
422 char-set:whitespace))
424 (pass-if "char-set:printing"
425 (char-set<= (char-set-union char-set:graphic char-set:whitespace)
428 (pass-if "char-set:iso-control"
429 (char-set<= (string->char-set
431 (map integer->char (append
436 (map (lambda (x) (+ #x80 x))
438 char-set:iso-control)))
440 (if (defined? 'setlocale)
441 (setlocale LC_ALL oldlocale))