Commit | Line | Data |
---|---|---|
a17d2654 | 1 | ;;;; srfi-14.test --- Test suite for Guile's SRFI-14 functions. |
072ad0fe MG |
2 | ;;;; Martin Grabmueller, 2001-07-16 |
3 | ;;;; | |
6e7d5622 | 4 | ;;;; Copyright (C) 2001, 2006 Free Software Foundation, Inc. |
072ad0fe | 5 | ;;;; |
53befeb7 NJ |
6 | ;;;; This library is free software; you can redistribute it and/or |
7 | ;;;; modify it under the terms of the GNU Lesser General Public | |
8 | ;;;; License as published by the Free Software Foundation; either | |
9 | ;;;; version 3 of the License, or (at your option) any later version. | |
072ad0fe | 10 | ;;;; |
53befeb7 | 11 | ;;;; This library is distributed in the hope that it will be useful, |
072ad0fe | 12 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
53befeb7 NJ |
13 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | ;;;; Lesser General Public License for more details. | |
072ad0fe | 15 | ;;;; |
53befeb7 NJ |
16 | ;;;; You should have received a copy of the GNU Lesser General Public |
17 | ;;;; License along with this library; if not, write to the Free Software | |
18 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
072ad0fe | 19 | |
a17d2654 LC |
20 | (define-module (test-suite test-srfi-14) |
21 | :use-module (srfi srfi-14) | |
22 | :use-module (srfi srfi-1) ;; `every' | |
23 | :use-module (test-suite lib)) | |
24 | ||
072ad0fe MG |
25 | |
26 | (define exception:invalid-char-set-cursor | |
27 | (cons 'misc-error "^invalid character set cursor")) | |
28 | ||
29 | (define exception:non-char-return | |
30 | (cons 'misc-error "returned non-char")) | |
31 | ||
32 | (with-test-prefix "char-set?" | |
33 | ||
34 | (pass-if "success on empty set" | |
35 | (char-set? (char-set))) | |
36 | ||
37 | (pass-if "success on non-empty set" | |
38 | (char-set? char-set:printing)) | |
39 | ||
40 | (pass-if "failure on empty set" | |
41 | (not (char-set? #t)))) | |
42 | ||
43 | ||
44 | (with-test-prefix "char-set=" | |
45 | (pass-if "success, no arg" | |
46 | (char-set=)) | |
47 | ||
48 | (pass-if "success, one arg" | |
49 | (char-set= char-set:lower-case)) | |
50 | ||
51 | (pass-if "success, two args" | |
52 | (char-set= char-set:upper-case char-set:upper-case)) | |
53 | ||
54 | (pass-if "failure, first empty" | |
55 | (not (char-set= (char-set) (char-set #\a)))) | |
56 | ||
57 | (pass-if "failure, second empty" | |
58 | (not (char-set= (char-set #\a) (char-set)))) | |
59 | ||
60 | (pass-if "success, more args" | |
61 | (char-set= char-set:blank char-set:blank char-set:blank))) | |
62 | ||
63 | (with-test-prefix "char-set<=" | |
64 | (pass-if "success, no arg" | |
65 | (char-set<=)) | |
66 | ||
67 | (pass-if "success, one arg" | |
68 | (char-set<= char-set:lower-case)) | |
69 | ||
70 | (pass-if "success, two args" | |
71 | (char-set<= char-set:upper-case char-set:upper-case)) | |
72 | ||
73 | (pass-if "success, first empty" | |
74 | (char-set<= (char-set) (char-set #\a))) | |
75 | ||
76 | (pass-if "failure, second empty" | |
77 | (not (char-set<= (char-set #\a) (char-set)))) | |
78 | ||
79 | (pass-if "success, more args, equal" | |
80 | (char-set<= char-set:blank char-set:blank char-set:blank)) | |
81 | ||
82 | (pass-if "success, more args, not equal" | |
83 | (char-set<= char-set:blank | |
84 | (char-set-adjoin char-set:blank #\F) | |
85 | (char-set-adjoin char-set:blank #\F #\o)))) | |
86 | ||
87 | (with-test-prefix "char-set-hash" | |
88 | (pass-if "empty set, bound" | |
89 | (let ((h (char-set-hash char-set:empty 31))) | |
90 | (and h (number? h) (exact? h) (>= h 0) (< h 31)))) | |
91 | ||
92 | (pass-if "empty set, no bound" | |
93 | (let ((h (char-set-hash char-set:empty))) | |
94 | (and h (number? h) (exact? h) (>= h 0)))) | |
95 | ||
96 | (pass-if "full set, bound" | |
97 | (let ((h (char-set-hash char-set:full 31))) | |
98 | (and h (number? h) (exact? h) (>= h 0) (< h 31)))) | |
99 | ||
100 | (pass-if "full set, no bound" | |
101 | (let ((h (char-set-hash char-set:full))) | |
102 | (and h (number? h) (exact? h) (>= h 0)))) | |
103 | ||
104 | (pass-if "other set, bound" | |
105 | (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r) 31))) | |
106 | (and h (number? h) (exact? h) (>= h 0) (< h 31)))) | |
107 | ||
108 | (pass-if "other set, no bound" | |
109 | (let ((h (char-set-hash (char-set #\f #\o #\b #\a #\r)))) | |
110 | (and h (number? h) (exact? h) (>= h 0))))) | |
111 | ||
112 | ||
113 | (with-test-prefix "char-set cursor" | |
114 | ||
115 | (pass-if-exception "invalid character cursor" | |
116 | exception:invalid-char-set-cursor | |
117 | (let* ((cs (char-set #\B #\r #\a #\z)) | |
118 | (cc (char-set-cursor cs))) | |
119 | (char-set-ref cs 1000))) | |
120 | ||
121 | (pass-if "success" | |
122 | (let* ((cs (char-set #\B #\r #\a #\z)) | |
123 | (cc (char-set-cursor cs))) | |
124 | (char? (char-set-ref cs cc)))) | |
125 | ||
126 | (pass-if "end of set fails" | |
127 | (let* ((cs (char-set #\a)) | |
128 | (cc (char-set-cursor cs))) | |
129 | (not (end-of-char-set? cc)))) | |
130 | ||
131 | (pass-if "end of set succeeds, empty set" | |
132 | (let* ((cs (char-set)) | |
133 | (cc (char-set-cursor cs))) | |
134 | (end-of-char-set? cc))) | |
135 | ||
136 | (pass-if "end of set succeeds, non-empty set" | |
137 | (let* ((cs (char-set #\a)) | |
138 | (cc (char-set-cursor cs)) | |
139 | (cc (char-set-cursor-next cs cc))) | |
140 | (end-of-char-set? cc)))) | |
141 | ||
142 | (with-test-prefix "char-set-fold" | |
143 | ||
144 | (pass-if "count members" | |
145 | (= (char-set-fold (lambda (c n) (+ n 1)) 0 (char-set #\a #\b)) 2)) | |
146 | ||
147 | (pass-if "copy set" | |
148 | (= (char-set-size (char-set-fold (lambda (c cs) (char-set-adjoin cs c)) | |
149 | (char-set) (char-set #\a #\b))) 2))) | |
150 | ||
151 | (with-test-prefix "char-set-unfold" | |
152 | ||
153 | (pass-if "create char set" | |
154 | (char-set= char-set:full | |
155 | (char-set-unfold (lambda (s) (= s 256)) integer->char | |
156 | (lambda (s) (+ s 1)) 0))) | |
157 | (pass-if "create char set (base set)" | |
158 | (char-set= char-set:full | |
159 | (char-set-unfold (lambda (s) (= s 256)) integer->char | |
160 | (lambda (s) (+ s 1)) 0 char-set:empty)))) | |
161 | ||
162 | (with-test-prefix "char-set-unfold!" | |
163 | ||
164 | (pass-if "create char set" | |
165 | (char-set= char-set:full | |
166 | (char-set-unfold! (lambda (s) (= s 256)) integer->char | |
167 | (lambda (s) (+ s 1)) 0 | |
168 | (char-set-copy char-set:empty)))) | |
169 | ||
170 | (pass-if "create char set" | |
171 | (char-set= char-set:full | |
172 | (char-set-unfold! (lambda (s) (= s 32)) integer->char | |
173 | (lambda (s) (+ s 1)) 0 | |
174 | (char-set-copy char-set:full))))) | |
175 | ||
176 | ||
177 | (with-test-prefix "char-set-for-each" | |
178 | ||
179 | (pass-if "copy char set" | |
180 | (= (char-set-size (let ((cs (char-set))) | |
181 | (char-set-for-each | |
182 | (lambda (c) (char-set-adjoin! cs c)) | |
183 | (char-set #\a #\b)) | |
184 | cs)) | |
185 | 2))) | |
186 | ||
187 | (with-test-prefix "char-set-map" | |
188 | ||
189 | (pass-if "upper case char set" | |
190 | (char-set= (char-set-map char-upcase char-set:lower-case) | |
191 | char-set:upper-case))) | |
a17d2654 LC |
192 | |
193 | (with-test-prefix "string->char-set" | |
194 | ||
195 | (pass-if "some char set" | |
196 | (let ((chars '(#\g #\u #\i #\l #\e))) | |
197 | (char-set= (list->char-set chars) | |
198 | (string->char-set (apply string chars)))))) | |
199 | ||
200 | ;; Make sure we get an ASCII charset and character classification. | |
201 | (if (defined? 'setlocale) (setlocale LC_CTYPE "C")) | |
202 | ||
203 | (with-test-prefix "standard char sets (ASCII)" | |
204 | ||
205 | (pass-if "char-set:letter" | |
206 | (char-set= (string->char-set | |
207 | (string-append "abcdefghijklmnopqrstuvwxyz" | |
208 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) | |
209 | char-set:letter)) | |
210 | ||
211 | (pass-if "char-set:punctuation" | |
212 | (char-set= (string->char-set "!\"#%&'()*,-./:;?@[\\]_{}") | |
213 | char-set:punctuation)) | |
214 | ||
215 | (pass-if "char-set:symbol" | |
216 | (char-set= (string->char-set "$+<=>^`|~") | |
217 | char-set:symbol)) | |
218 | ||
219 | (pass-if "char-set:letter+digit" | |
220 | (char-set= char-set:letter+digit | |
221 | (char-set-union char-set:letter char-set:digit))) | |
222 | ||
223 | (pass-if "char-set:graphic" | |
224 | (char-set= char-set:graphic | |
225 | (char-set-union char-set:letter char-set:digit | |
226 | char-set:punctuation char-set:symbol))) | |
227 | ||
228 | (pass-if "char-set:printing" | |
229 | (char-set= char-set:printing | |
230 | (char-set-union char-set:whitespace char-set:graphic)))) | |
231 | ||
232 | ||
233 | \f | |
234 | ;;; | |
235 | ;;; 8-bit charsets. | |
236 | ;;; | |
237 | ;;; Here, we only test ISO-8859-1 (Latin-1), notably because behavior of | |
238 | ;;; SRFI-14 for implementations supporting this charset is well-defined. | |
239 | ;;; | |
240 | ||
241 | (define (every? pred lst) | |
242 | (not (not (every pred lst)))) | |
243 | ||
244 | (define (find-latin1-locale) | |
245 | ;; Try to find and install an ISO-8859-1 locale. Return `#f' on failure. | |
246 | (if (defined? 'setlocale) | |
247 | (let loop ((locales (map (lambda (lang) | |
248 | (string-append lang ".iso88591")) | |
249 | '("de_DE" "en_GB" "en_US" "es_ES" | |
250 | "fr_FR" "it_IT")))) | |
251 | (if (null? locales) | |
252 | #f | |
253 | (if (false-if-exception (setlocale LC_CTYPE (car locales))) | |
254 | (car locales) | |
255 | (loop (cdr locales))))) | |
256 | #f)) | |
257 | ||
258 | ||
259 | (define %latin1 (find-latin1-locale)) | |
260 | ||
261 | (with-test-prefix "Latin-1 (8-bit charset)" | |
262 | ||
263 | ;; Note: the membership tests below are not exhaustive. | |
264 | ||
265 | (pass-if "char-set:letter (membership)" | |
266 | (if (not %latin1) | |
267 | (throw 'unresolved) | |
268 | (let ((letters (char-set->list char-set:letter))) | |
269 | (every? (lambda (8-bit-char) | |
270 | (memq 8-bit-char letters)) | |
271 | (append '(#\a #\b #\c) ;; ASCII | |
272 |