Debian Codegraph @

test/srfi.scm

raw
1 ;; -*- coding:utf-8 -*-
2 ;;
3 ;; Test for SRFIs
4 ;;
5
6 (use gauche.test)
7 (use compat.chibi-test)
8
9 (test-start "SRFIs")
10
11 ;; NB: For this test, we 'use' and run test-module for the srfis that
12 ;; are supported as built-in. Portable programs would explicitly use them so
13 ;; we need to make sure they're available as use form, and their exports
14 ;; are consistent.
15
16 ;; Also, some srfis shadows built-in bindings. To avoid unwanted
17 ;; side-effects, tests of each srfi is enclosed in a dedicated module.
18
19 ;;-----------------------------------------------------------------------
20 ;; Feature-based conditional expansion
21 (test-section "SRFI-0")
22
23 (define-module srfi-0-tests
24 (use gauche.test)
25 (use srfi.0)
26 (test-module 'srfi.0)
27
28 (test* "cond-expand" 0
29 (cond-expand [(library srfi.0) 0] [else 1]))
30 (test* "cond-expand" 1
31 (cond-expand [hogehoge 0] [else 1]))
32 (test* "cond-expand" 0
33 (cond-expand [(and (library srfi.0) (library srfi.1)) 0] [else 1]))
34 (test* "cond-expand" #t
35 (cond-expand [(and (library srfi.2) (library srfi.1))
36 (use srfi.1) (procedure? xcons)]
37 [else #f]))
38 (test* "cond-expand" 0
39 (cond-expand [(or hogehoge (library srfi.1)) 0] [else 1]))
40 (test* "cond-expand" 0
41 (cond-expand [(or (library srfi.1) hogehoge) 0] [else 1]))
42 (test* "cond-expand" 1
43 (cond-expand [(or (not (library srfi.1)) hogehoge) 0] [else 1]))
44 (test* "cond-expand" 0
45 (cond-expand [gauche 0] [else 1]))
46 (test* "cond-expand" 0
47 (cond-expand [scm -1] [gauche 0] [else 1]))
48 (test* "cond-expand (library)" 0
49 (cond-expand [(library (gauche time)) 0] [else 1]))
50 )
51
52 ;;-----------------------------------------------------------------------
53 ;; SRFI-1 is scheme.list
54
55 ;;-----------------------------------------------------------------------
56 ;; And-let*
57 (test-section "SRFI-2")
58
59 (define-module srfi-2-tests
60 (use gauche.test)
61 (use srfi.2)
62 (test-module 'srfi.2)
63 (define (srfi-2-look-up key alist)
64 (and-let* ([x (assq key alist)]) (cdr x)))
65 (test* "and-let*" 3
66 (srfi-2-look-up 'c '((a . 1) (b . 2) (c . 3))))
67 (test* "and-let*" #f
68 (srfi-2-look-up 'd '((a . 1) (b . 2) (c . 3))))
69 (test* "and-let*" 3
70 (let ([x 3])
71 (and-let* ([(positive? x)]
72 [y x])
73 y)))
74 (test* "and-let*" #f
75 (let ([x -3])
76 (and-let* ([(positive? x)]
77 [y x])
78 y)))
79
80 (test* "empty and-let*" #t (and-let* ()))
81 (test* "empty body and-let* var" 1 (let ((x 1)) (and-let* (x))))
82 (test* "empty body and-let* expr" 2 (let ((x 1)) (and-let* ([ (+ x x) ]))))
83 (test* "empty body and-let* (var expr)" 2
84 (let ((x 1)) (and-let* ([y (+ x x)]))))
85 )
86
87 ;;-----------------------------------------------------------------------
88 ;; A compatible let form with signatures and rest arguments
89
90 (test-section "SRFI-5")
91
92 (define-module srfi-5-test
93 (use gauche.test)
94 (use srfi.5)
95 (test-module 'srfi.5)
96
97 (test* "let - standard" 3
98 (let ([x 1] [y 2])
99 (let ()
100 (+ x y))))
101
102 (test* "let - standard" 1
103 (let ([x 1] [y 2])
104 (let ([y x] [x y])
105 (- x y))))
106
107 (test* "let - standard" 1
108 (let ()
109 (define x 1)
110 (* x x)))
111
112 (test* "let - standard, named" 55
113 (let loop ([x 1] [sum 0])
114 (if (> x 10) sum (loop (+ x 1) (+ sum x)))))
115
116 (test* "let - signature style" 55
117 (let (loop [x 1] [sum 0])
118 (if (> x 10) sum (loop (+ x 1) (+ sum x)))))
119
120 (test* "let - signature style" #t
121 (let (loop)
122 (procedure? loop)))
123
124 (test* "let - rest binding" '(0 1 (2 3 4))
125 (let ([x 0] [y 1] . [z 2 3 4]) (list x y z)))
126
127 (test* "let - rest binding, named" '((2 3 4) 0 (1))
128 (let loop ([x 0] [y 1] . [z 2 3 4])
129 (if (list? x) (list x y z) (loop z x y))))
130 )
131
132 ;;-----------------------------------------------------------------------
133 ;; Feature-based program configuration language
134 (test-section "SRFI-7")
135
136 ;; NB: SRFI-7 is a "meta-language". The 'program' form doesn't need
137 ;; to be evaluated within Scheme---an implementation can use a preprocessor
138 ;; to produce an evaluatable form from the 'program' form.
139 ;; Gauche directly expands it within the macro processor and evaluates it.
140 ;;
141 ;; These tests also relies on how Gauche compiles the empty begin form.
142 ;; See the notes in lib/srfi-7.scm for the details.
143
144 (define-module srfi-7-tests
145 (use gauche.test)
146
147 (sys-system "rm -rf test.o")
148 (sys-system "mkdir test.o")
149 (with-output-to-file "test.o/a.scm"
150 (^[] (write '(define x 3))))
151 (with-output-to-file "test.o/b.scm"
152 (^[] (write '(define (y) (+ x x)))))
153
154 (test* "program (empty)" 'ok
155 (begin (eval '(program) (make-module #f))
156 'ok))
157
158 (test* "program (requires, code)" #t
159 (eval '(program
160 (requires srfi-1)
161 (code (procedure? list-tabulate)))
162 (make-module #f)))
163 (test* "program (requires, multiple code)" '(1 2 1)
164 (eval '(program
165 (requires srfi-1)
166 (code (define foo (circular-list 1 2)))
167 (requires srfi-2)
168 (code (and-let* ([x (circular-list? foo)])
169 (take foo 3))))
170 (make-module #f)))
171 (test* "program (requires, no such feature)" (test-error)
172 (eval '(program
173 (requires no-such-feature))
174 (make-module #f)))
175 (test* "program (files (empty))" '(1 . 2)
176 (eval '(program
177 (files)
178 (code (cons 1 2)))
179 (make-module #f)))
180 (test* "program (files)" 6
181 (eval '(program
182 (files "./test.o/a")
183 (files "./test.o/b")
184 (code (y)))
185 (make-module #f)))
186 (test* "program (files (multi))" 6
187 (eval '(program
188 (files "./test.o/a" "./test.o/b")
189 (code (y)))
190 (make-module #f)))
191 (test* "program (feature-cond)" 2
192 (eval '(program
193 (feature-cond
194 [(and srfi-1 srfi-2) (code (define x 1))]
195 [else (code (define x 2))])
196 (code (+ x x)))
197 (make-module #f)))
198 (test* "program (feature-cond)" 4
199 (eval '(program
200 (feature-cond
201 [(and srfi-1 no-such-feature) (code (define x 1))]
202 [else (code (define x 2))])
203 (code (+ x x)))
204 (make-module #f)))
205 (test* "program (feature-cond)" 6
206 (eval '(program
207 (feature-cond
208 [(or srfi-1 no-such-feature) (code (define x 3))]
209 [else (code (define x 2))])
210 (code (+ x x)))
211 (make-module #f)))
212 (test* "program (feature-cond w/o else)" (test-error)
213 (eval '(program
214 (feature-cond
215 [(not srfi-1) (code (define x 5))]))
216 (make-module #f)))
217
218 (sys-system "rm -rf test.o")
219 )
220
221 ;;-----------------------------------------------------------------------
222 ;; SRFI-14 charset : moved to ext/scheme
223
224 ;;-----------------------------------------------------------------------
225 ;; SRFI-16 case-lambda : moved to procedure.scm (builtin)
226
227 ;;-----------------------------------------------------------------------
228 ;; Generalized set!
229 (test-section "SRFI-17")
230
231 (define-module srfi-17-tests
232 (use gauche.test)
233 (use srfi.17)
234 (test-module 'srfi.17)
235
236 (define x (cons 1 2))
237 (test "(setter car)" '((3 3) . 2)
238 (^[] (set! (car x) (list 3 3)) x))
239 (test "(setter cdr)" '((3 3) 4 5)
240 (^[] (set! (cdr x) (list 4 5)) x))
241 (test "(setter caar)" '(((8 9) 3) 4 5)
242 (^[] (set! (caar x) (list 8 9)) x))
243 (test "(setter cadr)" '(((8 9) 3) (7 6) 5)
244 (^[] (set! (cadr x) (list 7 6)) x))
245 (test "(setter cdar)" '(((8 9) 4 5) (7 6) 5)
246 (^[] (set! (cdar x) (list 4 5)) x))
247 (test "(setter cddr)" '(((8 9) 4 5) (7 6) 11 12)
248 (^[] (set! (cddr x) (list 11 12)) x))
249 (test "(setter caaar)" '((((13 14) 9) 4 5) (7 6) 11 12)
250 (^[] (set! (caaar x) (list 13 14)) x))
251 (test "(setter caadr)" '((((13 14) 9) 4 5) ((0 1) 6) 11 12)
252 (^[] (set! (caadr x) (list 0 1)) x))
253 (test "(setter cadar)" '((((13 14) 9) (2 3) 5) ((0 1) 6) 11 12)
254 (^[] (set! (cadar x) (list 2 3)) x))
255 (test "(setter caddr)" '((((13 14) 9) (2 3) 5) ((0 1) 6) (4 5) 12)
256 (^[] (set! (caddr x) (list 4 5)) x))
257 (test "(setter cdaar)" '((((13 14) 5 6) (2 3) 5) ((0 1) 6) (4 5) 12)
258 (^[] (set! (cdaar x) (list 5 6)) x))
259 (test "(setter cdadr)" '((((13 14) 5 6) (2 3) 5) ((0 1) 7 8) (4 5) 12)
260 (^[] (set! (cdadr x) (list 7 8)) x))
261 (test "(setter cddar)" '((((13 14) 5 6) (2 3) 9 10) ((0 1) 7 8) (4 5) 12)
262 (^[] (set! (cddar x) (list 9 10)) x))
263 (test "(setter cdddr)" '((((13 14) 5 6) (2 3) 9 10) ((0 1) 7 8) (4 5) -1 -2)
264 (^[] (set! (cdddr x) (list -1 -2)) x))
265 (test "(setter caaaar)" '(((((1 3) 14) 5 6) (2 3) 9 10) ((0 1) 7 8) (4 5) -1 -2)
266 (^[] (set! (caaaar x) (list 1 3)) x))
267 (test "(setter caaadr)" '(((((1 3) 14) 5 6) (2 3) 9 10) (((2 3) 1) 7 8) (4 5) -1 -2)
268 (^[] (set! (caaadr x) (list 2 3)) x))
269 (test "(setter caadar)" '(((((1 3) 14) 5 6) ((0 1) 3) 9 10) (((2 3) 1) 7 8) (4 5) -1 -2)
270 (^[] (set! (caadar x) (list 0 1)) x))
271 (test "(setter caaddr)" '(((((1 3) 14) 5 6) ((0 1) 3) 9 10) (((2 3) 1) 7 8) ((0 1) 5) -1 -2)
272 (^[] (set! (caaddr x) (list 0 1)) x))
273 (test "(setter cadaar)" '(((((1 3) 14) (0 1) 6) ((0 1) 3) 9 10) (((2 3) 1) 7 8) ((0 1) 5) -1 -2)
274 (^[] (set! (cadaar x) (list 0 1)) x))
275 (test "(setter cadadr)" '(((((1 3) 14) (0 1) 6) ((0 1) 3) 9 10) (((2 3) 1) (0 1) 8) ((0 1) 5) -1 -2)
276 (^[] (set! (cadadr x) (list 0 1)) x))
277 (test "(setter caddar)" '(((((1 3) 14) (0 1) 6) ((0 1) 3) (0 1) 10) (((2 3) 1) (0 1) 8) ((0 1) 5) -1 -2)
278 (^[] (set! (caddar x) (list 0 1)) x))
279 (test "(setter cadddr)" '(((((1 3) 14) (0 1) 6) ((0 1) 3) (0 1) 10) (((2 3) 1) (0 1) 8) ((0 1) 5) (0 1) -2)
280 (^[] (set! (cadddr x) (list 0 1)) x))
281 (test "(setter cdaaar)" '(((((1 3) 0 1) (0 1) 6) ((0 1) 3) (0 1) 10) (((2 3) 1) (0 1) 8) ((0 1) 5) (0 1) -2)
282 (^[] (set! (cdaaar x) (list 0 1)) x))
283 (test "(setter cdaadr)" '(((((1 3) 0 1) (0 1) 6) ((0 1) 3) (0 1) 10) (((2 3) 0 1) (0 1) 8) ((0 1) 5) (0 1) -2)
284 (^[] (set! (cdaadr x) (list 0 1)) x))
285 (test "(setter cdadar)" '(((((1 3) 0 1) (0 1) 6) ((0 1) 0 1) (0 1) 10) (((2 3) 0 1) (0 1) 8) ((0 1) 5) (0 1) -2)
286 (^[] (set! (cdadar x) (list 0 1)) x))
287 (test "(setter cdaddr)" '(((((1 3) 0 1) (0 1) 6) ((0 1) 0 1) (0 1) 10) (((2 3) 0 1) (0 1) 8) ((0 1) 0 1) (0 1) -2)
288 (^[] (set! (cdaddr x) (list 0 1)) x))
289 (test "(setter cddaar)" '(((((1 3) 0 1) (0 1) 0 1) ((0 1) 0 1) (0 1) 10) (((2 3) 0 1) (0 1) 8) ((0 1) 0 1) (0 1) -2)
290 (^[] (set! (cddaar x) (list 0 1)) x))
291 (test "(setter cddadr)" '(((((1 3) 0 1) (0 1) 0 1) ((0 1) 0 1) (0 1) 10) (((2 3) 0 1) (0 1) 0 1) ((0 1) 0 1) (0 1) -2)
292 (^[] (set! (cddadr x) (list 0 1)) x))
293 (test "(setter cdddar)" '(((((1 3) 0 1) (0 1) 0 1) ((0 1) 0 1) (0 1) 0 1) (((2 3) 0 1) (0 1) 0 1) ((0 1) 0 1) (0 1) -2)
294 (^[] (set! (cdddar x) (list 0 1)) x))
295 (test "(setter cddddr)" '(((((1 3) 0 1) (0 1) 0 1) ((0 1) 0 1) (0 1) 0 1) (((2 3) 0 1) (0 1) 0 1) ((0 1) 0 1) (0 1) 0 1)
296 (^[] (set! (cddddr x) (list 0 1)) x))
297
298 (define x (vector 1 2 3 4 5))
299 (test "(setter vector-ref)" '#(1 2 3 #f 5)
300 (^[] (set! (vector-ref x 3) #f) x))
301
302 (define x (string-copy "abcde"))
303 (test "(setter string-ref)" "abcQe"
304 (^[] (set! (string-ref x 3) #\Q) x))
305
306 (define (set-kar! p v) (set-car! p v))
307 (define kar (getter-with-setter (^p (car p)) set-kar!))
308
309 (define x (cons 1 2))
310 (test "(setter kar)" '(3 . 2) (^[] (set! (kar x) 3) x))
311
312 ;; see if it works as the normal set!
313 (test "set!" '#f (^[] (set! x #f) x))
314 )
315
316 ;;-----------------------------------------------------------------------
317 ;; Notation for specializing parameters without currying
318
319 (test-section "SRFI-26")
320
321 (define-module srfi-26-tests
322 (use gauche.test)
323 (use srfi.26)
324 (test-module 'srfi.26)
325
326 ;; The test cases are taken from the SRFI-26 test program by Sebastian Egner.
327 (test* "cut list" '() ((cut list)))
328 (test* "cut list <...>" '() ((cut list <...>)))
329 (test* "cut list 1" '(1) ((cut list 1)))
330 (test* "cut list <>" '(1) ((cut list <>) 1))
331 (test* "cut list <...>" '(1) ((cut list <...>) 1))
332 (test* "cut list 1 2" '(1 2) ((cut list 1 2)))
333 (test* "cut list 1 <>" '(1 2) ((cut list 1 <>) 2))
334 (test* "cut list 1 <...>" '(1 2) ((cut list 1 <...>) 2))
335 (test* "cut list 1 <...>" '(1 2 3 4) ((cut list 1 <...>) 2 3 4))
336 (test* "cut list 1 <> 3 <>" '(1 2 3 4) ((cut list 1 <> 3 <>) 2 4))
337 (test* "cut list 1 <> 3 <...>" '(1 2 3 4 5 6) ((cut list 1 <> 3 <...>) 2 4 5 6))
338 (test* "cut (eval order)" '(ok)
339 (let* ([x 'wrong] [y (cut list x)]) (set! x 'ok) (y)))
340 (test* "cut (eval order)" 2
341 (let ([a 0])
342 (map (cut + (begin (set! a (+ a 1)) a) <>)
343 '(1 2))
344 a))
345
346 (test* "cute list" '() ((cute list)))
347 (test* "cute list <...>" '() ((cute list <...>)))
348 (test* "cute list 1" '(1) ((cute list 1)))
349 (test* "cute list <>" '(1) ((cute list <>) 1))
350 (test* "cute list <...>" '(1) ((cute list <...>) 1))
351 (test* "cute list 1 2" '(1 2) ((cute list 1 2)))
352 (test* "cute list 1 <>" '(1 2) ((cute list 1 <>) 2))
353 (test* "cute list 1 <...>" '(1 2) ((cute list 1 <...>) 2))
354 (test* "cute list 1 <...>" '(1 2 3 4) ((cute list 1 <...>) 2 3 4))
355 (test* "cute list 1 <> 3 <>" '(1 2 3 4) ((cute list 1 <> 3 <>) 2 4))
356 (test* "cute list 1 <> 3 <...>" '(1 2 3 4 5 6) ((cute list 1 <> 3 <...>) 2 4 5 6))
357 (test* "cute (eval order)" '(ok)
358 (let* ([x 'ok] [y (cute list x)]) (set! x 'wrong) (y)))
359 (test* "cute (eval order)" 1
360 (let ([a 0])
361 (map (cute + (begin (set! a (+ a 1)) a) <>)
362 '(1 2))
363 a))
364 )
365
366 ;;-----------------------------------------------------------------------
367 ;; SRFI-27 depends on mt-random, and will be tested in ext/srfi.
368
369 ;;-----------------------------------------------------------------------
370 ;; Localization
371
372 (test-section "SRFI-29")
373
374 (define-module srfi-29-tests
375 (use gauche.test)
376 (use srfi.29)
377 (test-module 'srfi.29)
378
379 ;; test taken from the example of SRFI-29 document.
380 (let ((translations
381 '(((en) . ((time . "Its ~a, ~a.")
382 (goodbye . "Goodbye, ~a.")))
383 ((fr) . ((time . "~1@*~a, c'est ~a.")
384 (goodbye . "Au revoir, ~a."))))))
385 (for-each (^[translation]
386 (let ([bundle-name (cons 'hello-program (car translation))])
387 (if (not (load-bundle! bundle-name))
388 (begin
389 (declare-bundle! bundle-name (cdr translation))
390 (store-bundle! bundle-name)))))
391 translations))
392
393 (define localized-message
394 (^[message-name . args]
395 (apply format (cons (localized-template 'hello-program
396 message-name)
397 args))))
398
399 (let ([myname "Fred"])
400 (test* "localized-message (en)"
401 '("Its 12:00, Fred." "Goodbye, Fred.")
402 (begin
403 (current-language 'en)
404 (current-country 'us)
405 (list (localized-message 'time "12:00" myname)
406 (localized-message 'goodbye myname))))
407
408 (test* "localized-message (fr)"
409 '("Fred, c'est 12:00." "Au revoir, Fred.")
410 (begin
411 (current-language 'fr)
412 (current-country 'fr)
413 (list (localized-message 'time "12:00" myname)
414 (localized-message 'goodbye myname))))
415 ))
416
417 ;;-----------------------------------------------------------------------
418 ;; Nested multi-line comments
419
420 (test-section "SRFI-30")
421
422 (define-module srfi-30-tests
423 (use gauche.test)
424
425 (test "SRFI-30" 1
426 (^[] #|hohoho|# 1))
427
428 (test "SRFI-30" '(1)
429 (^[] '(#|hohoho|# 1)))
430
431 (test "SRFI-30, multiline" '(1)
432 (^[]
433 '(
434 #|
435 hohoho
436 |#
437 1)))
438
439 (test "SRFI-30, multiline" '(1)
440 (^[]
441 '(1
442 #|
443 hohoho
444 |#
445 )))
446
447 (test "SRFI-30, multiline" '()
448 (^[]
449 '(
450 #|
451 hohoho
452 |#
453 )))
454
455 (test "SRFI-30, nesting" '(1)
456 (^[]
457 '(#| nested #| nested |# nested |# 1)))
458
459 (test "SRFI-30, nesting" '(1)
460 (^[]
461 '(#| nested #| nested; |# nested |# 1)))
462
463 (test "SRFI-30, nesting" '(1)
464 (^[]
465 '(#|##|###|#|||#### ||#|||||#|#1)))
466
467 (test "SRFI-30, intertwined" '(1)
468 (^[]
469 '(;; #| this is a single-line comment
470 1 #|
471 ;; this is a multi-line comment #|
472 we're still in multi-line comment ;;
473 #|#|multi-line comment can be nested many times|#|#
474 ;; yes, this is still in multi-line comment |#
475 finally, this closes the comment |#
476 ;and this is in single-line comment
477 )))
478
479 (test "SRFI-30, dot syntax" '(1 . 1)
480 (^[]
481 '(1 . #|foo bar|#1)))
482
483 (test "SRFI-30, quasiquote" '(1 #(2 3))
484 (^[]
485 (let ([x 1] [y 2])
486 `(#|foo|# ,x #|foo|# #(#|,|# ,y ,(+ #|x|# x y #|y|#) #|foo|#)))))
487 )
488
489 ;;-----------------------------------------------------------------------
490 ;; A special form 'rec'
491
492 (test-section "SRFI-31")
493
494 (define-module srfi-31-tests
495 (use gauche.test)
496 (use srfi.31)
497 (test-module 'srfi.31)
498
499 ;; taken from SRFI-31 document
500
501 (define f (rec (f n)
502 ((rec (g k l)
503 (if (zero? k)
504 l
505 (g (- k 1) (* k l)))) n 1)))
506
507 (test "SRFI-31" 1 (^[] (f 0)))
508 (test "SRFI-31" 3628800 (^[] (f 10)))
509
510 (test "SRFI-31" "11111"
511 (^[] (with-output-to-string
512 (^[] (let loop ([i 0]
513 [stream (rec s (cons 1 (delay s)))])
514 (when (< i 5)
515 (display (car stream))
516 (loop (+ i 1) (force (cdr stream)))))))))
517 )
518
519 ;;-----------------------------------------------------------------------
520 ;; args-fold
521
522 (test-section "SRFI-37")
523
524 (define-module srfi-37-tests
525 (use gauche.test)
526 (use srfi.37)
527 (test-module 'srfi.37)
528
529 (define options
530 (list (option '(#\l "long-display") #f #f
531 (^[option name arg seed1 seed2]
532 (values (cons 'l seed1) seed2)))
533 (option '(#\o "output-file") #t #f
534 (^[option name arg seed1 seed2]
535 (values (acons 'o arg seed1) seed2)))
536 (option '(#\d "debug") #f #t
537 (^[option name arg seed1 seed2]
538 (values (acons 'd arg seed1) seed2)))
539 (option '(#\b "batch") #f #f
540 (^[option name arg seed1 seed2]
541 (values (cons 'b seed1) seed2)))
542 (option '(#\i "interactive") #f #f
543 (^[option name arg seed1 seed2]
544 (values (cons 'i seed1) seed2)))
545 ))
546
547 (define (test-options . args)
548 (receive (opts operands)
549 (args-fold args options
550 (^[option name arg seed1 seed2] ;; unrecognized-proc
551 (values (acons '? name seed1) seed2))
552 (^[arg seed1 seed2] ;; operand-proc
553 (values seed1 (cons arg seed2)))
554 '() '())
555 (list (reverse opts) (reverse operands))))
556
557 (test* "SRFI-37 (short)" '((i l b) ())
558 (test-options "-ilb"))
559
560 (test* "SRFI-37 (short, arg)" '((i (o . "foo") l (d . "8")) ())
561 (test-options "-iofoo" "-l" "-d8"))
562
563 (test* "SRFI-37 (short, arg)" '(((o . "foo") (d . #f)) ("bar"))
564 (test-options "-o" "foo" "-d" "bar"))
565
566 (test* "SRFI-37 (short, missing arg)" '(((o . "-d")) ("bar"))
567 (test-options "-o" "-d" "bar"))
568
569 (test* "SRFI-37 (short, missing arg)" '(((o . #f)) ())
570 (test-options "-o"))
571
572 (test* "SRFI-37 (long, arg)" '((l i (d . "v") b (d . #f)) ())
573 (test-options "--long-display" "--interactive" "--debug=v" "-bd"))
574
575 (test* "SRFI-37 (long, arg)" '((l i (d . "v") b (d . #f)) ())
576 (test-options "--long-display" "--interactive" "--debug=v" "-bd"))
577
578 (test* "SRFI-37 (operand)" '((i b) ("foo" "bar"))
579 (test-options "-i" "foo" "-b" "bar"))
580
581 (test* "SRFI-37 (operand)" '((i) ("foo" "-b" "bar"))
582 (test-options "-i" "--" "foo" "-b" "bar"))
583
584 (test* "SRFI-37 (operand)" '((i b) ("-" "foo" "bar"))
585 (test-options "-i" "-" "foo" "-b" "bar"))
586 )
587
588 ;;-----------------------------------------------------------------------
589 ;; streams
590
591 (test-section "SRFI-41")
592
593 (define-module srfi-41-tests
594 (use gauche.test)
595 (use srfi.41)
596 (test-module 'srfi.41))
597
598 ;;-----------------------------------------------------------------------
599 ;; Eager comprehension
600
601 (test-section "SRFI-42")
602
603 (define-module srfi-42-tests
604 (use gauche.test)
605 (use srfi.42)
606 (test-module 'srfi.42)
607
608 ;; tests took from examples of SRFI-42 reference implementation.
609
610 (test* "do-ec" 1
611 (let ([x 0]) (do-ec (set! x (+ x 1))) x))
612 (test* "do-ec" 10
613 (let ([x 0]) (do-ec (:range i 10) (set! x (+ x 1))) x))
614 (test* "do-ec" 45
615 (let ([x 0]) (do-ec (:range n 10) (:range k n) (set! x (+ x 1))) x))
616
617
618 (test* "list-ec" '(1)
619 (list-ec 1))
620 (test* "list-ec" '(0 1 2 3)
621 (list-ec (:range i 4) i))
622 (test* "list-ec" '((0 0) (1 0) (1 1) (2 0) (2 1) (2 2))
623 (list-ec (:range n 3) (:range k (+ n 1)) (list n k)))
624 (test* "list-ec" '((0 0) (2 0) (2 1) (2 2) (4 0) (4 1) (4 2) (4 3) (4 4))
625 (list-ec (:range n 5) (if (even? n)) (:range k (+ n 1)) (list n k)))
626 (test* "list-ec" '((1 0) (1 1) (3 0) (3 1) (3 2) (3 3))
627 (list-ec (:range n 5) (not (even? n)) (:range k (+ n 1)) (list n k)))
628 (test* "list-ec" '((4 0) (4 1) (4 2) (4 3) (4 4))
629 (list-ec (:range n 5)
630 (and (even? n) (> n 2))
631 (:range k (+ n 1))
632 (list n k)))
633 (test* "list-ec" '((0 0) (2 0) (2 1) (2 2) (4 0) (4 1) (4 2) (4 3) (4 4))
634 (list-ec (:range n 5)
635 (or (even? n) (> n 3))
636 (:range k (+ n 1))
637 (list n k)))
638 (test* "list-ec" 10
639 (let ((x 0)) (list-ec (:range n 10) (begin (set! x (+ x 1))) n) x))
640 (test* "list-ec" '(0 0 1)
641 (list-ec (nested (:range n 3) (:range k n)) k))
642
643 (test* "append-ec" '(a b) (append-ec '(a b)))
644 (test* "append-ec" '() (append-ec (:range i 0) '(a b)))
645 (test* "append-ec" '(a b) (append-ec (:range i 1) '(a b)))
646 (test* "append-ec" '(a b a b) (append-ec (:range i 2) '(a b)))
647
648 (test* "string-ec" "a" (string-ec #\a))
649 (test* "string-ec" "" (string-ec (:range i 0) #\a))
650 (test* "string-ec" "a" (string-ec (:range i 1) #\a))
651 (test* "string-ec" "aa" (string-ec (:range i 2) #\a))
652
653 (test* "string-append-ec" "ab" (string-append-ec "ab"))
654 (test* "string-append-ec" "" (string-append-ec (:range i 0) "ab"))
655 (test* "string-append-ec" "ab" (string-append-ec (:range i 1) "ab"))
656 (test* "string-append-ec" "abab" (string-append-ec (:range i 2) "ab"))
657
658 (test* "vector-ec" '#(1) (vector-ec 1))
659 (test* "vector-ec" '#() (vector-ec (:range i 0) i))
660 (test* "vector-ec" '#(0) (vector-ec (:range i 1) i))
661 (test* "vector-ec" '#(0 1) (vector-ec (:range i 2) i))
662
663 (test* "vector-of-length-ec" '#(1) (vector-of-length-ec 1 1))
664 (test* "vector-of-length-ec" '#() (vector-of-length-ec 0 (:range i 0) i))
665 (test* "vector-of-length-ec" '#(0) (vector-of-length-ec 1 (:range i 1) i))
666 (test* "vector-of-length-ec" '#(0 1) (vector-of-length-ec 2 (:range i 2) i))
667
668 (test* "sum-ec" 1 (sum-ec 1))
669 (test* "sum-ec" 0 (sum-ec (:range i 0) i))
670 (test* "sum-ec" 0 (sum-ec (:range i 1) i))
671 (test* "sum-ec" 1 (sum-ec (:range i 2) i))
672 (test* "sum-ec" 3 (sum-ec (:range i 3) i))
673
674 (test* "product-ec" 1 (product-ec 1))
675 (test* "product-ec" 1 (product-ec (:range i 1 0) i))
676 (test* "product-ec" 1 (product-ec (:range i 1 1) i))
677 (test* "product-ec" 1 (product-ec (:range i 1 2) i))
678 (test* "product-ec" 2 (product-ec (:range i 1 3) i))
679 (test* "product-ec" 6 (product-ec (:range i 1 4) i))
680
681 (test* "min-ec" 1 (min-ec 1))
682 (test* "min-ec" 0 (min-ec (:range i 1) i))
683 (test* "min-ec" 0 (min-ec (:range i 2) i))
684
685 (test* "max-ec" 1 (max-ec 1))
686 (test* "max-ec" 0 (max-ec (:range i 1) i))
687 (test* "max-ec" 1 (max-ec (:range i 2) i))
688
689 (test* "first-ec" 1 (first-ec #f 1))
690 (test* "first-ec" #f (first-ec #f (:range i 0) i))
691 (test* "first-ec" 0 (first-ec #f (:range i 1) i))
692 (test* "first-ec" 0 (first-ec #f (:range i 2) i))
693 (test* "first-ec" 0
694 (let ((last-i -1))
695 (first-ec #f (:range i 10) (begin (set! last-i i)) i)
696 last-i))
697
698 (test* "last-ec" 1 (last-ec #f 1))
699 (test* "last-ec" #f (last-ec #f (:range i 0) i))
700 (test* "last-ec" 0 (last-ec #f (:range i 1) i))
701 (test* "last-ec" 1 (last-ec #f (:range i 2) i))
702
703 (test* "any-ec" #f (any?-ec #f))
704 (test* "any-ec" #t (any?-ec #t))
705 (test* "any-ec" #f (any?-ec (:range i 2 2) (even? i)))
706 (test* "any-ec" #t (any?-ec (:range i 2 3) (even? i)))
707
708 (test* "every-ec" #f (every?-ec #f))
709 (test* "every-ec" #t (every?-ec #t))
710 (test* "every-ec" #t (every?-ec (:range i 2 2) (even? i)))
711 (test* "every-ec" #t (every?-ec (:range i 2 3) (even? i)))
712 (test* "every-ec" #f (every?-ec (:range i 2 4) (even? i)))
713
714 (test* "fold-ec" 285
715 (let ([sum-sqr (^[x result] (+ result (* x x)))])
716 (fold-ec 0 (:range i 10) i sum-sqr)))
717 (test* "fold3-ec" 284
718 (let ([minus-1 (^[x] (- x 1))]
719 [sum-sqr (^[x result] (+ result (* x x)))])
720 (fold3-ec (error "wrong") (:range i 10) i minus-1 sum-sqr)))
721 (test* "fold3-ec" 'infinity
722 (fold3-ec 'infinity (:range i 0) i min min))
723
724 (test* ":list" '()
725 (list-ec (:list x '()) x))
726 (test* ":list" '(1)
727 (list-ec (:list x '(1)) x))
728 (test* ":list" '(1 2 3)
729 (list-ec (:list x '(1 2 3)) x))
730 (test* ":list" '(1 2)
731 (list-ec (:list x '(1) '(2)) x))
732 (test* ":list" '(1 2 3)
733 (list-ec (:list x '(1) '(2) '(3)) x))
734
735 (test* ":string" '()
736 (list-ec (:string c "") c))
737 (test* ":string" '(#\1)
738 (list-ec (:string c "1") c))
739 (test* ":string" '(#\1 #\2 #\3)
740 (list-ec (:string c "123") c))
741 (test* ":string" '(#\1 #\2)
742 (list-ec (:string c "1" "2") c))
743 (test* ":string" '(#\1 #\2 #\3)
744 (list-ec (:string c "1" "2" "3") c))
745
746 (test* ":vector" '()
747 (list-ec (:vector x (vector)) x))
748 (test* ":vector" '(1)
749 (list-ec (:vector x (vector 1)) x))
750 (test* ":vector" '(1 2 3)
751 (list-ec (:vector x (vector 1 2 3)) x))
752 (test* ":vector" '(1 2)
753 (list-ec (:vector x (vector 1) (vector 2)) x))
754 (test* ":vector" '(1 2 3)
755 (list-ec (:vector x (vector 1) (vector 2) (vector 3)) x))
756
757 (test* ":uvector" '()
758 (list-ec (:uvector x '#u8()) x))
759 (test* ":uvector" '(1)
760 (list-ec (:uvector x '#s8(1)) x))
761 (test* ":uvector" '(1 2 3)
762 (list-ec (:uvector x '#u16(1 2 3)) x))
763 (test* ":uvector" '(1 2)
764 (list-ec (:uvector x '#s16(1) '#s16(2)) x))
765 (test* ":uvector" '(1.0 2.0 3.0)
766 (list-ec (:uvector x '#f16(1) '#f16(2) '#f16(3)) x))
767
768 (test* ":range" '()
769 (list-ec (:range x -2) x))
770 (test* ":range" '()
771 (list-ec (:range x -1) x))
772 (test* ":range" '()
773 (list-ec (:range x 0) x))
774 (test* ":range" '(0)
775 (list-ec (:range x 1) x))
776 (test* ":range" '(0 1)
777 (list-ec (:range x 2) x))
778 (test* ":range" '(0 1 2)
779 (list-ec (:range x 0 3) x))
780 (test* ":range" '(1 2)
781 (list-ec (:range x 1 3) x))
782 (test* ":range" '(-2)
783 (list-ec (:range x -2 -1) x))
784 (test* ":range" '()
785 (list-ec (:range x -2 -2) x))
786 (test* ":range" '(1 3)
787 (list-ec (:range x 1 5 2) x))
788 (test* ":range" '(1 3 5)
789 (list-ec (:range x 1 6 2) x))
790 (test* ":range" '(5 3)
791 (list-ec (:range x 5 1 -2) x))
792 (test* ":range" '(6 4 2)
793 (list-ec (:range x 6 1 -2) x))
794
795 (test* ":real-range" '(0. 1. 2.)
796 (list-ec (:real-range x 0.0 3.0) x))
797 (test* ":real-range" '(0. 1. 2.)
798 (list-ec (:real-range x 0 3.0) x))
799 (test* ":real-range" '(0. 1. 2.)
800 (list-ec (:real-range x 0 3 1.0) x))
801
802 (test* ":char-range" "abcdefghijklmnopqrstuvwxyz"
803 (string-ec (:char-range c #\a #\z) c) )
804
805 (sys-system "rm -f tmp1.o")
806
807 (test* ":port" (list-ec (:range n 10) n)
808 (begin
809 (with-output-to-file "tmp1.o"
810 (^[] (do-ec (:range n 10) (begin (write n) (newline)))))
811 (call-with-input-file "tmp1.o"
812 (^[port] (list-ec (:port x port read) x)))))
813
814 (test* ":generator" (list-ec (:range n 10) (cons (- 9 n) n))
815 ;; we can't load gauche.generator yet
816 (letrec ([k 10]
817 [g (^[] (if (zero? k) (eof-object) (begin (dec! k) k)))])
818 (list-ec (:generator v (index i) g) (cons v i))))
819
820 (test* ":generator" (list-ec (:range n 10) (cons (- 9 n) n))
821 ;; we can't load gauche.generator yet
822 (letrec ([k 10]
823 [g (^[] (if (zero? k) (eof-object) (begin (dec! k) k)))])
824 (list-ec (: v (index i) g) (cons v i))))
825
826 (sys-system "rm -f tmp1.o")
827
828 (test* ":do" '(0 1 2 3) (list-ec (:do ((i 0)) (< i 4) ((+ i 1))) i))
829 (test* ":do" '(10 9 8 7)
830 (list-ec
831 (:do (let ((x 'x)))
832 ((i 0))
833 (< i 4)
834 (let ((j (- 10 i))))
835 #t
836 ((+ i 1)))
837 j))
838
839 (test* ":let" '(1) (list-ec (:let x 1) x))
840 (test* ":let" '(2) (list-ec (:let x 1) (:let y (+ x 1)) y))
841 (test* ":let" '(2) (list-ec (:let x 1) (:let x (+ x 1)) x))
842
843 (test* ":parallel" '((1 a) (2 b) (3 c))
844 (list-ec (:parallel (:range i 1 10) (:list x '(a b c))) (list i x)))
845 (test* ":until" '(1 2 3 4 5)
846 (list-ec (:until (:range i 1 10) (>= i 5)) i))
847 (test* ":while" '(1 2 3 4)
848 (list-ec (:while (:list i '(1 2 3 4 5 6 7 8 9)) (< i 5)) i))
849 (test* ":until" '(1 2 3 4 5)
850 (list-ec (:until (:list i '(1 2 3 4 5 6 7 8 9)) (>= i 5)) i))
851
852 (test* ":while and :parallel" '((1 1) (2 2) (3 3) (4 4))
853 (list-ec (:while (:parallel (:range i 1 10)
854 (:list j '(1 2 3 4 5 6 7 8 9)))
855 (< i 5))
856 (list i j)))
857 (test* ":until and :parallel" '((1 1) (2 2) (3 3) (4 4) (5 5))
858 (list-ec (:until (:parallel (:range i 1 10)
859 (:list j '(1 2 3 4 5 6 7 8 9)))
860 (>= i 5))
861 (list i j)))
862 (test* ":while stopping loop" 5
863 (rlet1 n 0
864 (do-ec (:while (:range i 1 10) (begin (set! n (+ n 1)) (< i 5)))
865 (if #f #f))))
866 (test* ":until stopping loop" 5
867 (rlet1 n 0
868 (do-ec (:until (:range i 1 10) (begin (set! n (+ n 1)) (>= i 5)))
869 (if #f #f))))
870 (test* ":while stopping loop" 5
871 (rlet1 n 0
872 (do-ec (:while (:parallel (:range i 1 10)
873 (:do () (begin (set! n (+ n 1)) #t) ()))
874 (< i 5))
875 (if #f #f))))
876 (test* ":until stopping loop" 5
877 (rlet1 n 0
878 (do-ec (:until (:parallel (:range i 1 10)
879 (:do () (begin (set! n (+ n 1)) #t) ()))
880 (>= i 5))
881 (if #f #f))))
882
883 ;; Those tests manifest the bug in the original reference implementation
884 (test* ":while loop boundary condition" '(1)
885 (list-ec (:while (:list x '(1 2)) (= x 1)) x))
886 (test* ":while loop off-by-one error" '(1)
887 (list-ec (:while (:list x '(1)) #t) x))
888 (test* ":while loop off-by-one error on vector" '(1 2 3 4 5)
889 (list-ec (:while (:vector x (index i) '#(1 2 3 4 5)) (< x 10)) x))
890
891
892 (test* ": list" '(a b) (list-ec (: c '(a b)) c))
893 (test* ": list" '(a b c d) (list-ec (: c '(a b) '(c d)) c))
894
895 (test* ": string" '(#\a #\b) (list-ec (: c "ab") c))
896 (test* ": string" '(#\a #\b #\c #\d) (list-ec (: c "ab" "cd") c))
897
898 (test* ": vector" '(a b) (list-ec (: c (vector 'a 'b)) c))
899 (test* ": vector" '(a b c) (list-ec (: c (vector 'a 'b) (vector 'c)) c))
900
901 (test* ": uvector" '(1 2 3) (list-ec (: c '#u8(1 2) '#u8(3)) c))
902 (test* ": uvector" '(1 -1 2 -2)
903 (list-ec (: c '#s8(1) '#s8(-1) '#s8() '#s8(2) '#s8(-2)) c))
904
905 (test* ": range" '() (list-ec (: i 0) i))
906 (test* ": range" '(0) (list-ec (: i 1) i))
907 (test* ": range" '(0 1 2 3 4 5 6 7 8 9) (list-ec (: i 10) i))
908 (test* ": range" '(1) (list-ec (: i 1 2) i))
909 (test* ": range" '(1) (list-ec (: i 1 2 3) i))
910 (test* ": range" '(1 4 7) (list-ec (: i 1 9 3) i))
911
912 (test* ": real-range" '(0. 0.2 0.4 0.6 0.8) (list-ec (: i 0.0 1.0 0.2) i)
913 (^[x y]
914 (every (^[p q] (< (abs (- p q)) 1.0e-5)) x y)))
915 (test* ": char" '(#\a #\b #\c) (list-ec (: c #\a #\c) c))
916
917 (sys-system "rm -f tmp1.o")
918 (test* ": port" (list-ec (:range n 10) n)
919 (begin
920 (with-output-to-file "tmp1.o"
921 (^[]
922 (do-ec (:range n 10) (begin (write n) (newline)))))
923 (call-with-input-file "tmp1.o"
924 (^[port] (list-ec (: x port read) x)))))
925
926 (sys-system "rm -f tmp1.o")
927 (test* ": port" (list-ec (:range n 10) n)
928 (begin
929 (with-output-to-file "tmp1.o"
930 (^[]
931 (do-ec (:range n 10) (begin (write n) (newline)))))
932 (call-with-input-file "tmp1.o"
933 (^[port] (list-ec (: x port) x)))))
934
935 (sys-system "rm -f tmp1.o")
936
937 (test* ":list index" '((a 0) (b 1))
938 (list-ec (:list c (index i) '(a b)) (list c i)))
939 (test* ":string index" '((#\a 0))
940 (list-ec (:string c (index i) "a") (list c i)))
941 (test* ":vector index" '((a 0))
942 (list-ec (:vector c (index i) (vector 'a)) (list c i)))
943 (test* ":range index" '((0 0) (-1 1) (-2 2))
944 (list-ec (:range i (index j) 0 -3 -1) (list i j)) )
945 (test* ":real-range index" '((0. 0) (0.2 1) (0.4 2) (0.6 3) (0.8 4))
946 (list-ec (:real-range i (index j) 0 1 0.2) (list i j))
947 (^[x y]
948 (every (^[p q] (and (< (abs (- (car p) (car q))) 1e-5)
949 (= (cadr p) (cadr q))))
950 x y)))
951 (test* ":char-range index" '((#\a 0) (#\b 1) (#\c 2))
952 (list-ec (:char-range c (index i) #\a #\c) (list c i)) )
953 (test* ": index" '((a 0) (b 1) (c 2) (d 3))
954 (list-ec (: x (index i) '(a b c d)) (list x i)) )
955 (sys-system "rm -f tmp1.o")
956 (test* ": index port"
957 '((0 0) (1 1) (2 2) (3 3) (4 4) (5 5) (6 6) (7 7) (8 8) (9 9))
958 (begin
959 (with-output-to-file "tmp1.o"
960 (^[]
961 (do-ec (:range n 10) (begin (write n) (newline)))))
962 (call-with-input-file "tmp1.o"
963 (^[port] (list-ec (: x (index i) port) (list x i))))))
964 (sys-system "rm -f tmp1.o")
965
966 (test* "example 1" '(0 1 4 9 16)
967 (list-ec (: i 5) (* i i)))
968 (test* "example 2" '((1 0) (2 0) (2 1) (3 0) (3 1) (3 2))
969 (list-ec (: n 1 4) (: i n) (list n i)))
970 (test* "example 3" '((#\a 0) (#\b 1) (#\c 2))
971 (list-ec (: x (index i) "abc") (list x i)) )
972 (test* "example 4" '((#\a . 0) (#\b . 1))
973 (list-ec (:string c (index i) "a" "b") (cons c i)) )
974
975 (test* ":range :range" '(0 0 1 0 1 2 0 1 2 3)
976 (list-ec (:range x 5) (:range x x) x))
977 (test* ":list :" '(0 1 #\2 #\3 4)
978 (list-ec (:list x '(2 "23" (4))) (: y x) y))
979 (test* ":parallel :integers :do" '((0 10) (1 9) (2 8) (3 7) (4 6))
980 (list-ec (:parallel (:integers x)
981 (:do ((i 10)) (< x i) ((- i 1))))
982 (list x i)))
983
984 (let ()
985 (define (factorial n) ; n * (n-1) * .. * 1 for n >= 0
986 (product-ec (:range k 2 (+ n 1)) k) )
987
988 (test* "factorial" 1 (factorial 0))
989 (test* "factorial" 1 (factorial 1))
990 (test* "factorial" 6 (factorial 3))
991 (test* "factorial" 120 (factorial 5))
992 )
993
994 (use srfi.4)
995 (let ()
996 (define (eratosthenes n) ; primes in {2..n-1} for n >= 1
997 (let ([p? (make-u8vector n 1)])
998 (do-ec (:range k 2 n)
999 (if (= (u8vector-ref p? k) 1))
1000 (:range i (* 2 k) n k)
1001 (u8vector-set! p? i 0))
1002 (list-ec (:range k 2 n) (if (= (u8vector-ref p? k) 1)) k) ))
1003 (test* "eratosthenes" '(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47)
1004 (eratosthenes 50))
1005 (test* "eratosthenes" 9592 (length (eratosthenes 100000)))
1006 )
1007
1008 (let ()
1009 (define (pythagoras n) ; a, b, c s.t. 1 <= a <= b <= c <= n, a^2 + b^2 = c^2
1010 (list-ec
1011 (:let sqr-n (* n n))
1012 (:range a 1 (+ n 1))
1013 (:let sqr-a (* a a))
1014 (:range b a (+ n 1))
1015 (:let sqr-c (+ sqr-a (* b b)))
1016 (if (<= sqr-c sqr-n))
1017 (:range c b (+ n 1))
1018 (if (= (* c c) sqr-c))
1019 (list a b c) ))
1020 (test* "pythagoras" '((3 4 5) (5 12 13) (6 8 10) (9 12 15))
1021 (pythagoras 15))
1022 (test* "pythagoras" 127 (length (pythagoras 200)))
1023 )
1024
1025 (let ()
1026 (define (qsort xs) ; stable
1027 (if (null? xs)
1028 '()
1029 (let ((pivot (car xs)) (xrest (cdr xs)))
1030 (append
1031 (qsort (list-ec (:list x xrest) (if (< x pivot)) x))
1032 (list pivot)
1033 (qsort (list-ec (:list x xrest) (if (>= x pivot)) x)) ))))
1034 (test* "qsort" '(1 1 2 2 3 3 4 4 5 5) (qsort '(1 5 4 2 4 5 3 2 1 3)))
1035 )
1036
1037 (let ()
1038 (define (pi-BBP m) ; approx. of pi within 16^-m (Bailey-Borwein-Plouffe)
1039 (sum-ec
1040 (:range n 0 (+ m 1))
1041 (:let n8 (* 8 n))
1042 (* (- (/ 4 (+ n8 1))
1043 (+ (/ 2 (+ n8 4))
1044 (/ 1 (+ n8 5))
1045 (/ 1 (+ n8 6))))
1046 (/ 1 (expt 16 n)) )))
1047 (test* "pi-BBP" (/ 40413742330349316707 12864093722915635200)
1048 (pi-BBP 5))
1049 )
1050
1051 (let ()
1052 (define (read-line port) ; next line (incl. #\newline) of port
1053 (let ([line
1054 (string-ec
1055 (:until (:port c port read-char)
1056 (char=? c #\newline) )
1057 c )])
1058 (if (string=? line "")
1059 (read-char port) ; eof-object
1060 line )))
1061
1062 (define (read-lines filename) ; list of all lines
1063 (call-with-input-file filename
1064 (^[port]
1065 (list-ec (:port line port read-line) line) )))
1066
1067 (sys-system "rm -f tmp1.o")
1068 (test* "read-lines" (list-ec (:char-range c #\0 #\9) (string c #\newline))
1069 (begin
1070 (with-output-to-file "tmp1.o"
1071 (^[]
1072 (do-ec (:range n 10) (begin (write n) (newline)))))
1073 (read-lines "tmp1.o")))
1074 )
1075 )
1076
1077 ;;-----------------------------------------------------------------------
1078 ;; Integers as bits
1079
1080 (test-section "SRFI-60")
1081
1082 (define-module srfi-60-tests
1083 (use gauche.test)
1084 (use srfi.60)
1085 (test-module 'srfi.60)
1086
1087 ;; Most procedures are builtin and tested in test/numbers.scm.
1088
1089 (test* "bitwise-if" #b01100110
1090 (bitwise-if #b10101100 #b00110101 #b11001010))
1091
1092 (for-each (^(n r) (test* (format "log2-binary-factors(~a)" n) r
1093 (log2-binary-factors n)))
1094 '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1095 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16)
1096 '(-1 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 4
1097 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 4))
1098
1099 (define (test-rotate-bit-field n c s e r)
1100 (test* (format "rotate-bit-field(~s,~a,~a,~a)" (number->string n 2) c s e) r
1101 (number->string (rotate-bit-field n c s e) 2)))
1102 (test-rotate-bit-field #b0100 3 4 0 "100") ; trivial path
1103 (test-rotate-bit-field #b0100 3 0 4 "10")
1104 (test-rotate-bit-field #b0100 -1 0 4 "10")
1105 (test-rotate-bit-field #b0100 10 0 4 "1")
1106 (test-rotate-bit-field #b110100100010000 -1 5 9 "110100010010000")
1107 (test-rotate-bit-field #b110100100010000 1 5 9 "110100000110000")
1108
1109 (define (test-reverse-bit-field n s e r)
1110 (test* (format "reverse-bit-field(~s,~a,~a)" (number->string n 2) s e) r
1111 (number->string (reverse-bit-field n s e) 2)))
1112 (test-reverse-bit-field #xa7 8 0 "10100111")
1113 (test-reverse-bit-field #xa7 0 8 "11100101")
1114 (test-reverse-bit-field #xa7 1 5 "10111001")
1115
1116 (test* "integer->list" '(#t #f #f #t) (integer->list 9))
1117 (test* "integer->list" '(#f #f #t #f #f #t) (integer->list 9 6))
1118 (test* "list->integer" 9 (list->integer '(#t #f #f #t)))
1119 (test* "list->integer" 9 (list->integer '(#f #f #t #f #f #t)))
1120 ;; tests bignum path
1121 (test* "list->integer" (+ (expt 2 63) (expt 2 62) (expt 2 31) (expt 2 30) 1)
1122 (list->integer '(#t #t #f #f #f #f #f #f
1123 #f #f #f #f #f #f #f #f
1124 #f #f #f #f #f #f #f #f
1125 #f #f #f #f #f #f #f #f
1126 #t #t #f #f #f #f #f #f
1127 #f #f #f #f #f #f #f #f
1128 #f #f #f #f #f #f #f #f
1129 #f #f #f #f #f #f #f #t)))
1130
1131 (test* "booleans->integer" 9 (booleans->integer #f #f #t #f #f #t))
1132 )
1133
1134 ;;-----------------------------------------------------------------------
1135 ;; A Scheme API for test suites
1136
1137 (test-section "SRFI-64")
1138
1139 (define-module srfi-64-tests
1140 (use gauche.test)
1141 (use srfi.64)
1142 (test-module 'srfi.64)
1143 (include "include/srfi-64-test"))
1144
1145 ;;-----------------------------------------------------------------------
1146 ;; Octet vectors
1147
1148 (test-section "SRFI-66")
1149
1150 (define-module srfi-66-tests
1151 (use gauche.test)
1152 (use gauche.uvector)
1153
1154 ;; SRFI-66 provides u8vector-copy! with different argument order,
1155 ;; so import it with prefix to avoid conflict.
1156 (use srfi.66 :prefix srfi-66:)
1157 (test-module 'srfi.66)
1158
1159 (test* "SRFI-66 uvector-copy!" '#u8(0 0 1 2 3 0 0)
1160 (rlet1 v (u8vector 0 0 0 0 0 0 0)
1161 (srfi-66:u8vector-copy! '#u8(0 1 2 3 4) 1 v 2 3)))
1162 )
1163
1164 ;;-----------------------------------------------------------------------
1165 ;; Basic hash tables
1166
1167 (test-section "SRFI-69")
1168
1169 (define-module srfi-69-tests
1170 (use gauche.test)
1171 (use srfi.69)
1172 (test-module 'srfi.69)
1173
1174 ;; These test is contributed by Takashi Kato @tk_riple
1175 (letrec-syntax
1176 ([test-error
1177 (syntax-rules ()
1178 ((_ expr)
1179 (test* 'expr #t (guard (e (else #t)) expr #f))))]
1180 [test-equal
1181 (syntax-rules ()
1182 ((_ expect expr)
1183 (test* 'expr expect expr)))]
1184 [test-assert
1185 (syntax-rules ()
1186 ((_ expr)
1187 (test-assert 'expr expr))
1188 ((_ name expr)
1189 (test* name #t expr (lambda (a r) (and a r)))))]
1190 [test-not
1191 (syntax-rules ()
1192 ((_ expr)
1193 (test-not 'expr expr))
1194 ((_ name expr)
1195 (test-assert name (not expr))))]
1196 [test-lset-eq?
1197 (syntax-rules ()
1198 ((test-lset= a b)
1199 (test-assert 'a (lset= eq? a b))))]
1200 [test-lset-equal?
1201 (syntax-rules ()
1202 ((test-lset-equal? a b)
1203 (test-assert 'a (lset= equal? a b))))])
1204
1205 (let ((ht (make-hash-table eq?)))
1206 ;; 3 initial elements
1207
1208 (test-equal 0 (hash-table-size ht))
1209 (hash-table-set! ht 'cat 'black)
1210 (hash-table-set! ht 'dog 'white)
1211 (hash-table-set! ht 'elephant 'pink)
1212
1213 (test-equal 3 (hash-table-size ht))
1214 (test-assert (hash-table-exists? ht 'dog))
1215 (test-assert (hash-table-exists? ht 'cat))
1216 (test-assert (hash-table-exists? ht 'elephant))
1217 (test-not (hash-table-exists? ht 'goose))
1218 (test-equal 'white (hash-table-ref ht 'dog))
1219 (test-equal 'black (hash-table-ref ht 'cat))
1220 (test-equal 'pink (hash-table-ref ht 'elephant))
1221 (test-error (hash-table-ref ht 'goose))
1222 (test-equal 'grey (hash-table-ref ht 'goose (lambda () 'grey)))
1223 (test-equal 'grey (hash-table-ref/default ht 'goose 'grey))
1224 (test-lset-eq? '(cat dog elephant) (hash-table-keys ht))
1225 (test-lset-eq? '(black white pink) (hash-table-values ht))
1226 (test-lset-equal? '((cat . black) (dog . white) (elephant . pink))
1227 (hash-table->alist ht))
1228
1229 ;; remove an element
1230 (hash-table-delete! ht 'dog)
1231 (test-equal 2 (hash-table-size ht))
1232 (test-not (hash-table-exists? ht 'dog))
1233 (test-assert (hash-table-exists? ht 'cat))
1234 (test-assert (hash-table-exists? ht 'elephant))
1235 (test-error (hash-table-ref ht 'dog))
1236 (test-equal 'black (hash-table-ref ht 'cat))
1237 (test-equal 'pink (hash-table-ref ht 'elephant))
1238 (test-lset-eq? '(cat elephant) (hash-table-keys ht))
1239 (test-lset-eq? '(black pink) (hash-table-values ht))
1240 (test-lset-equal? '((cat . black) (elephant . pink)) (hash-table->alist ht))
1241
1242 ;; remove a non-existing element
1243 (hash-table-delete! ht 'dog)
1244 (test-equal 2 (hash-table-size ht))
1245 (test-not (hash-table-exists? ht 'dog))
1246
1247 ;; overwrite an existing element
1248 (hash-table-set! ht 'cat 'calico)
1249 (test-equal 2 (hash-table-size ht))
1250 (test-not (hash-table-exists? ht 'dog))
1251 (test-assert (hash-table-exists? ht 'cat))
1252 (test-assert (hash-table-exists? ht 'elephant))
1253 (test-error (hash-table-ref ht 'dog))
1254 (test-equal 'calico (hash-table-ref ht 'cat))
1255 (test-equal 'pink (hash-table-ref ht 'elephant))
1256 (test-lset-eq? '(cat elephant) (hash-table-keys ht))
1257 (test-lset-eq? '(calico pink) (hash-table-values ht))
1258 (test-lset-equal? '((cat . calico) (elephant . pink)) (hash-table->alist ht))
1259
1260 ;; walk and fold
1261 (test-lset-equal?
1262 '((cat . calico) (elephant . pink))
1263 (let ((a '()))
1264 (hash-table-walk ht (lambda (k v) (set! a (cons (cons k v) a))))
1265 a))
1266 (test-lset-equal? '((cat . calico) (elephant . pink))
1267 (hash-table-fold ht (lambda (k v a) (cons (cons k v) a)) '()))
1268
1269 ;; copy
1270 (let ((ht2 (hash-table-copy ht)))
1271 (test-equal 2 (hash-table-size ht2))
1272 (test-not (hash-table-exists? ht2 'dog))
1273 (test-assert (hash-table-exists? ht2 'cat))
1274 (test-assert (hash-table-exists? ht2 'elephant))
1275 (test-error (hash-table-ref ht2 'dog))
1276 (test-equal 'calico (hash-table-ref ht2 'cat))
1277 (test-equal 'pink (hash-table-ref ht2 'elephant))
1278 (test-lset-eq? '(cat elephant) (hash-table-keys ht2))
1279 (test-lset-eq? '(calico pink) (hash-table-values ht2))
1280 (test-lset-equal? '((cat . calico) (elephant . pink))
1281 (hash-table->alist ht2)))
1282
1283 ;; merge
1284 (let ((ht2 (make-hash-table eq?)))
1285 (hash-table-set! ht2 'bear 'brown)
1286 (test-equal 1 (hash-table-size ht2))
1287 (test-not (hash-table-exists? ht2 'dog))
1288 (test-assert (hash-table-exists? ht2 'bear))
1289 (hash-table-merge! ht2 ht)
1290 (test-equal 3 (hash-table-size ht2))
1291 (test-assert (hash-table-exists? ht2 'bear))
1292 (test-assert (hash-table-exists? ht2 'cat))
1293 (test-assert (hash-table-exists? ht2 'elephant))
1294 (test-not (hash-table-exists? ht2 'goose))
1295 (test-equal 'brown (hash-table-ref ht2 'bear))
1296 (test-equal 'calico (hash-table-ref ht2 'cat))
1297 (test-equal 'pink (hash-table-ref ht2 'elephant))
1298 (test-error (hash-table-ref ht2 'goose))
1299 (test-equal 'grey (hash-table-ref/default ht2 'goose 'grey))
1300 (test-lset-eq? '(bear cat elephant) (hash-table-keys ht2))
1301 (test-lset-eq? '(brown calico pink) (hash-table-values ht2))
1302 (test-lset-equal? '((cat . calico) (bear . brown) (elephant . pink))
1303 (hash-table->alist ht2)))
1304
1305 ;; alist->hash-table
1306 (test-lset-equal? (hash-table->alist ht)
1307 (hash-table->alist
1308 (alist->hash-table
1309 '((cat . calico) (elephant . pink)))))
1310 )
1311
1312 ;; update
1313 (let ((ht (make-hash-table eq?))
1314 (add1 (lambda (x) (+ x 1))))
1315 (hash-table-set! ht 'sheep 0)
1316 (hash-table-update! ht 'sheep add1)
1317 (hash-table-update! ht 'sheep add1)
1318 (test-equal 2 (hash-table-ref ht 'sheep))
1319 (hash-table-update! ht 'fish add1 (^[] 100))
1320 (hash-table-update! ht 'fish add1 (^[] 100))
1321 (hash-table-update! ht 'fish add1 (^[] 100))
1322 (test-equal 103 (hash-table-ref ht 'fish))
1323 (hash-table-update!/default ht 'crows add1 0)
1324 (hash-table-update!/default ht 'crows add1 0)
1325 (hash-table-update!/default ht 'crows add1 0)
1326 (test-equal 3 (hash-table-ref ht 'crows)))
1327
1328 ;; string keys
1329 (let ((ht (make-hash-table equal?)))
1330 (hash-table-set! ht "cat" 'black)
1331 (hash-table-set! ht "dog" 'white)
1332 (hash-table-set! ht "elephant" 'pink)
1333 (hash-table-ref/default ht "dog" #f)
1334 (test-equal 'white (hash-table-ref ht "dog"))
1335 (test-equal 'black (hash-table-ref ht "cat"))
1336 (test-equal 'pink (hash-table-ref ht "elephant"))
1337 (test-error (hash-table-ref ht "goose"))
1338 (test-equal 'grey (hash-table-ref/default ht "goose" 'grey))
1339 (test-lset-equal? '("cat" "dog" "elephant") (hash-table-keys ht))
1340 (test-lset-equal? '(black white pink) (hash-table-values ht))
1341 (test-lset-equal?
1342 '(("cat" . black) ("dog" . white) ("elephant" . pink))
1343 (hash-table->alist ht)))
1344
1345 ;; string-ci keys
1346 (let ((ht (make-hash-table string-ci=? string-ci-hash)))
1347 (hash-table-set! ht "cat" 'black)
1348 (hash-table-set! ht "dog" 'white)
1349 (hash-table-set! ht "elephant" 'pink)
1350 (hash-table-ref/default ht "DOG" #f)
1351 (test-equal 'white (hash-table-ref ht "DOG"))
1352 (test-equal 'black (hash-table-ref ht "Cat"))
1353 (test-equal 'pink (hash-table-ref ht "eLePhAnT"))
1354 (test-error (hash-table-ref ht "goose"))
1355 (test-lset-equal? '("cat" "dog" "elephant") (hash-table-keys ht))
1356 (test-lset-equal? '(black white pink) (hash-table-values ht))
1357 (test-lset-equal?
1358 '(("cat" . black) ("dog" . white) ("elephant" . pink))
1359 (hash-table->alist ht)))
1360
1361 ;; stress test
1362 (test-equal 625
1363 (let ((ht (make-hash-table)))
1364 (do ((i 0 (+ i 1))) ((= i 1000))
1365 (hash-table-set! ht i (* i i)))
1366 (hash-table-ref/default ht 25 #f)))
1367 )
1368 )
1369
1370 ;;-----------------------------------------------------------------------
1371 ;; Octed-addressed binary blocks
1372
1373 (test-section "SRFI-74")
1374
1375 (define-module srfi-74-tests
1376 (use gauche.test)
1377
1378 (use srfi.74)
1379 (test-module 'srfi.74)
1380
1381 ;; most procedures are just an alias of binary.io so we don't do
1382 ;; exhausitive tests.
1383
1384 (let ((b1 (make-blob 10)))
1385 (test* "blob?" #t (blob? b1))
1386 (test* "blob?" #f (blob? '#(1 2 3)))
1387 (test* "endianness" 'big-endian (endianness big))
1388 (test* "endianness" 'little-endian (endianness little))
1389 (test* "endianness" (native-endian) (endianness native))
1390
1391 (test* "blob-length" 10 (blob-length b1))
1392 (test* "blob-[su]8-set!" #u8(#xff #xfe 0 0 0 0 0 0 0 0)
1393 (begin
1394 (blob-u8-set! b1 0 255)
1395 (blob-s8-set! b1 1 -2)
1396 b1))
1397 (test* "blob-[su]8-ref" '(-1 254)
1398 (list (blob-s8-ref b1 0)
1399 (blob-u8-ref b1 1)))
1400 (test* "blob-uint-ref"
1401 '(#xff #xff #xfffe #xfeff #xfffe00 #x00feff #xfe0000 #x0000fe)
1402 (list (blob-uint-ref 1 (endianness big) b1 0)
1403 (blob-uint-ref 1 (endianness little) b1 0)
1404 (blob-uint-ref 2 (endianness big) b1 0)
1405 (blob-uint-ref 2 (endianness little) b1 0)
1406 (blob-uint-ref 3 (endianness big) b1 0)
1407 (blob-uint-ref 3 (endianness little) b1 0)
1408 (blob-uint-ref 3 (endianness big) b1 1)
1409 (blob-uint-ref 3 (endianness little) b1 1)))
1410 (test* "blob-sint-ref"
1411 '(-1 -1 -2 -257 -512 #x00feff -131072 #x0000fe)
1412 (list (blob-sint-ref 1 (endianness big) b1 0)
1413 (blob-sint-ref 1 (endianness little) b1 0)
1414 (blob-sint-ref 2 (endianness big) b1 0)
1415 (blob-sint-ref 2 (endianness little) b1 0)
1416 (blob-sint-ref 3 (endianness big) b1 0)
1417 (blob-sint-ref 3 (endianness little) b1 0)
1418 (blob-sint-ref 3 (endianness big) b1 1)
1419 (blob-sint-ref 3 (endianness little) b1 1)))
1420
1421 (test* "blob-uint-set!"
1422 '#u8(0 #xff #xff 0 0 #xff #xfe #xfe #xff 0)
1423 (rlet1 b1 (make-blob 10)
1424 (blob-uint-set! 2 (endianness big) b1 0 #xff)
1425 (blob-uint-set! 2 (endianness little) b1 2 #xff)
1426 (blob-uint-set! 3 (endianness big) b1 4 #xfffe)
1427 (blob-uint-set! 3 (endianness little) b1 7 #xfffe)))
1428 (test* "blob-sint-set!"
1429 '#u8(#xff #xfe #xfe #xff #xff #xfe 00 00 #xfe #xff)
1430 (rlet1 b1 (make-blob 10)
1431 (blob-sint-set! 2 (endianness big) b1 0 -2)
1432 (blob-sint-set! 2 (endianness little) b1 2 -2)
1433 (blob-sint-set! 3 (endianness big) b1 4 -512)
1434 (blob-sint-set! 3 (endianness little) b1 7 -512)))
1435
1436 (let1 b1 '#u8(0 1 2 3 4 5 6 7 #xff)
1437 (test* "blob->uint-list"
1438 '((#x000102 #x030405 #x0607ff)
1439 (#x020100 #x050403 #xff0706))
1440 (list (blob->uint-list 3 (endianness big) b1)
1441 (blob->uint-list 3 (endianness little) b1)))
1442 (test* "blob->uint-list"
1443 '((#x000102 #x030405 #x0607ff)
1444 (#x020100 #x050403 #x-00f8fa))
1445 (list (blob->sint-list 3 (endianness big) b1)
1446 (blob->sint-list 3 (endianness little) b1)))
1447
1448 (test* "uint-list->blob" (list b1 b1)
1449 (list (uint-list->blob 3 (endianness big)
1450 (blob->uint-list 3 (endianness big) b1))
1451 (uint-list->blob 3 (endianness little)
1452 (blob->uint-list 3 (endianness little) b1))))
1453 (test* "sint-list->blob" (list b1 b1)
1454 (list (sint-list->blob 3 (endianness big)
1455 (blob->sint-list 3 (endianness big) b1))
1456 (sint-list->blob 3 (endianness little)
1457 (blob->sint-list 3 (endianness little) b1))))
1458 )
1459 )
1460 )
1461
1462 ;;-----------------------------------------------------------------------
1463 ;; Lightweight testing. SRFI-78 is used in other srfi's testing, so
1464 ;; we only check module bindings.
1465
1466 (test-section "SRFI-78")
1467
1468 (define-module srfi-78-tests
1469 (use gauche.test)
1470 (use srfi.78)
1471 (test-module 'srfi.78)
1472 )
1473
1474 ;;-----------------------------------------------------------------------
1475 ;; An interface to access environment variables. Just aliases of built-ins.
1476
1477 (test-section "SRFI-98")
1478
1479 (define-module srfi-98-tests
1480 (use gauche.test)
1481 (use srfi.98)
1482 (test-module 'srfi.98)
1483 )
1484
1485 ;;-----------------------------------------------------------------------
1486 ;; (scheme.rlist) Purely functional random-access pairs and lists
1487
1488 (test-section "SRFI-101")
1489
1490 (define-module srfi-101-tests
1491 (use gauche.test)
1492 (use util.match)
1493 (use srfi.101 :prefix ra:)
1494 (test-module 'srfi.101)
1495
1496 (test* "ra-quote" (ra:quote (1 2 3)) (ra:list 1 2 3))
1497 (test* "ra-quote" (ra:quote (1 . 2)) (ra:cons 1 2))
1498 (test* "ra-quote" (ra:quote 1) 1)
1499 (test* "ra-quote" (ra:quote ()) '())
1500 (test* "ra-pairs" #t (ra:pair? (ra:cons 'a 'b)))
1501 (test* "ra-pairs" #t (ra:pair? (ra:list 'a 'b 'c)))
1502 (test* "ra-pairs" #f (ra:pair? (ra:quote ())))
1503 (test* "ra-pairs" #f (ra:pair? (ra:quote #(a b))))
1504
1505 ;; NB: spec says those conversion procedures only accept proper lists
1506 (test* "conversions" '(a b c d e)
1507 (ra:random-access-list->linear-access-list
1508 (ra:list 'a 'b 'c 'd 'e)))
1509 (test* "conversions" '()
1510 (ra:random-access-list->linear-access-list (ra:list)))
1511 ;; (test* "conversions" '(a b . c)
1512 ;; (ra:random-access-list->linear-access-list
1513 ;; (ra:cons 'a (ra:cons 'b 'c))))
1514 (test* "conversions" (ra:list 'a 'b 'c 'd 'e)
1515 (ra:linear-access-list->random-access-list
1516 '(a b c d e)))
1517 (test* "conversions" (ra:list)
1518 (ra:linear-access-list->random-access-list '()))
1519 ;; (test* "conversions" (ra:cons 'a (ra:cons 'b 'c))
1520 ;; (ra:linear-access-list->random-access-list
1521 ;; '(a b . c)))
1522 ;; (test* "conversions" 'a
1523 ;; (ra:linear-access-list->random-access-list 'a))
1524
1525 (let ()
1526 (define-syntax t-pair
1527 (syntax-rules ()
1528 [(_ ca cd)
1529 (begin (test* #"(ra-cons ~ca ~cd)" ca (ra:car (ra:cons ca cd)))
1530 (test* #"(ra-cons ~ca ~cd)" cd (ra:cdr (ra:cons ca cd))))]))
1531 (t-pair 1 2)
1532 (t-pair 1 (ra:list 2))
1533 (t-pair 1 '()))
1534 (test* "ra-car" (test-error) (ra:car '()))
1535 (test* "ra-cdr" (test-error) (ra:cdr '()))
1536
1537 (let ()
1538 (define (make-tree kons depth seed)
1539 (if (zero? depth)
1540 (values seed (+ seed 1))
1541 (receive (x sx) (make-tree kons (- depth 1) seed)
1542 (receive (y sy) (make-tree kons (- depth 1) sx)
1543 (values (kons x y) sy)))))
1544 (define tree (make-tree cons 4 0))
1545 (define ra-tree (make-tree ra:cons 4 0))
1546 (define (x->ra x)
1547 (if (pair? x)
1548 (ra:cons (x->ra (car x)) (x->ra (cdr x)))
1549 x))
1550 (define-syntax t
1551 (er-macro-transformer
1552 (^[f r c]
1553 (match f
1554 [(_ name)
1555 (let ([ra-name (symbol-append 'ra: name)])
1556 (quasirename r
1557 `(test* ,ra-name (x->ra (,name tree))
1558 (,ra-name ra-tree))))]))))
1559 (t car) (t cdr)
1560 (t caar) (t cadr) (t cdar) (t cddr)
1561 (t caaar) (t caadr) (t cadar) (t caddr)
1562 (t cdaar) (t cdadr) (t cddar) (t cdddr)
1563 (t caaaar) (t caaadr) (t caadar) (t caaddr)
1564 (t cadaar) (t cadadr) (t caddar) (t cadddr)
1565 (t cdaaar) (t cdaadr) (t cdadar) (t cdaddr)
1566 (t cddaar) (t cddadr) (t cdddar) (t cddddr)
1567 )
1568
1569 (test* "ra-null?" #t (ra:null? '()))
1570 (test* "ra-null?" #t (ra:null? (ra:list)))
1571 (test* "ra-null?" #f (ra:null? 'x))
1572 (test* "ra-list?" #t (ra:list? '()))
1573 (test* "ra-list?" #t (ra:list? (ra:list)))
1574 (test* "ra-list?" #t (ra:list? (ra:list 1 2 3)))
1575 (test* "ra-list?" #f (ra:list? (ra:cons 1 2)))
1576 (test* "ra-make-list" '(1 1 1 1 1)
1577 (ra:random-access-list->linear-access-list (ra:make-list 5 1)))
1578
1579 (dotimes [n 10]
1580 (test* "ra-length" n (ra:length (ra:make-list n))))
1581 (test* "ra-append" (ra:quote (a b c))
1582 (ra:append (ra:quote (a b c))))
1583 (test* "ra-append" (ra:quote x)
1584 (ra:append (ra:quote x)))
1585 (test* "ra-append" (ra:quote (a b c d e f . g))
1586 (ra:append (ra:quote (a b c)) (ra:quote (d e f . g))))
1587 (test* "ra-append" (ra:quote (a b c d e f . g))
1588 (ra:append (ra:quote (a b c)) (ra:quote (d e f)) (ra:quote g)))
1589 (test* "ra-append" (ra:quote (a b c d e f . g))
1590 (ra:append (ra:quote (a b c)) (ra:quote (d e)) (ra:quote (f . g))))
1591 (test* "ra-append" (ra:quote (a b . c))
1592 (ra:append (ra:quote (a b)) (ra:quote ()) (ra:quote c)))
1593 (test* "ra-append" (test-error) (ra:append (ra:quote x) (ra:quote y)))
1594 (test* "ra-reverse" (ra:quote ())
1595 (ra:reverse '()))
1596 (test* "ra-reverse" (ra:quote (e d c b a))
1597 (ra:reverse (ra:quote (a b c d e))))
1598 (test* "ra-reverse" (test-error) (ra:reverse (ra:quote a)))
1599
1600 (test* "ra-list-tail" '() (ra:list-tail (ra:list) 0))
1601 (test* "ra-list-tail" (test-error) (ra:list-tail (ra:list) 1))
1602 (test* "ra-list-tail" (ra:list 1 2 3) (ra:list-tail (ra:list 0 1 2 3) 1))
1603 (test* "ra-list-tail" (ra:cons 2 3) (ra:list-tail (ra:quote (0 1 2 . 3)) 2))
1604 (test* "ra-list-tail" 3 (ra:list-tail (ra:quote (0 1 2 . 3)) 3))
1605 (test* "ra-list-tail" (test-error) (ra:list-tail (ra:quote (0 1 2 . 3)) 4))
1606 (test* "ra-list-ref" (test-error) (ra:list-ref (ra:list) 0))
1607 (test* "ra-list-ref" 'a (ra:list-ref (ra:list 'a 'b 'c) 0))
1608 (test* "ra-list-ref" 'b (ra:list-ref (ra:list 'a 'b 'c) 1))
1609 (test* "ra-list-ref" 'c (ra:list-ref (ra:list 'a 'b 'c) 2))
1610 (test* "ra-list-ref" 'a (ra:list-ref (ra:cons 'a 'b) 0))
1611 (test* "ra-list-set" (test-error) (ra:list-set (ra:list) 0 'a))
1612 (test* "ra-list-set" (ra:quote (z b c))
1613 (ra:list-set (ra:list 'a 'b 'c) 0 'z))
1614 (test* "ra-list-set" (ra:quote (a z c))
1615 (ra:list-set (ra:list 'a 'b 'c) 1 'z))
1616 (test* "ra-list-set" (ra:quote (a b z))
1617 (ra:list-set (ra:list 'a 'b 'c) 2 'z))
1618 (test* "ra-list-ref/update" (list 1 (ra:quote (0 10 2)))
1619 (values->list
1620 (ra:list-ref/update (ra:list 0 1 2) 1 (cut * <> 10))))
1621
1622 (test* "ra-map" (ra:quote (2 4 6))
1623 (ra:map (cut * 2 <>) (ra:list 1 2 3)))
1624 (test* "ra-map" (ra:quote ((1 . 4) (2 . 5) (3 . 6)))
1625 (ra:map ra:cons (ra:list 1 2 3) (ra:list 4 5 6)))
1626 (test* "ra-map" (test-error)
1627 (ra:map ra:cons (ra:list 1 2 3) (ra:list 4 5 6 7)))
1628 (test* "ra-for-each" '(4 3 2 1)
1629 (rlet1 xs '()
1630 (ra:for-each (^x (push! xs x)) (ra:list 1 2 3 4))))
1631 (test* "ra-for-each" '((3 . 6) (2 . 5) (1 . 4))
1632 (rlet1 xs '()
1633 (ra:for-each (^[x y] (push! xs (cons x y)))
1634 (ra:list 1 2 3) (ra:list 4 5 6))))
1635 (test* "ra-for-each" (test-error)
1636 (rlet1 xs '()
1637 (ra:for-each (^[x y] (push! xs (cons x y)))
1638 (ra:list 1 2 3 4) (ra:list 4 5 6))))
1639 )
1640
1641 ;;-----------------------------------------------------------------------
1642 ;; SRFI-106 will be tested in test/net.scm
1643
1644 ;;-----------------------------------------------------------------------
1645 ;; Boxes & Multiple-vlaue boxes
1646
1647 (test-section "SRFI-111 & SRFI-195")
1648
1649 (define-module srfi-111-195-tests
1650 (use gauche.test)
1651 (use srfi.111)
1652 (test-module 'srfi.111)
1653 (use srfi.195)
1654 (test-module 'srfi.195)
1655
1656 (test* "box primitives"
1657 '(#t 1 #f 2)
1658 (let1 b (box 2)
1659 (list (box? b) (box-arity b) (box? 2) (unbox b))))
1660
1661 (test* "box setter" 3
1662 (let1 b (box 2)
1663 (set-box! b 3)
1664 (unbox b)))
1665
1666 (test* "box compare"
1667 '(#t #f #f #t #f)
1668 (let ([b1 (box 2)]
1669 [b2 (box 2)]
1670 [b3 (box 3)])
1671 (list (equal? b1 b2)
1672 (equal? b2 b3)
1673 (equal? b1 2)
1674 (eqv? b1 b1)
1675 (eqv? b1 b2))))
1676
1677 (test* "mv box" '(#t 3 (a b c))
1678 (let1 b (box 'a 'b 'c)
1679 (list (box? b)
1680 (box-arity b)
1681 (values->list (unbox b)))))
1682
1683 (test* "mv box set" '(d e f)
1684 (let1 b (box 'a 'b 'c)
1685 (set-box! b 'd 'e 'f)
1686 (values->list (unbox b))))
1687
1688 (test* "mv box (zero value)" '(#t 0 ())
1689 (let1 b (box)
1690 (list (box? b) (box-arity b) (values->list (unbox b)))))
1691
1692 (test* "mv box index ref" 'b
1693 (let1 b (box 'a 'b 'c)
1694 (unbox-value b 1)))
1695
1696 (test* "mv box index set" '(a e c)
1697 (let1 b (box 'a 'b 'c)
1698 (set-box-value! b 1 'e)
1699 (values->list (unbox b))))
1700
1701 (test* "box error handling" (test-error <error> #/out of range/)
1702 (unbox-value (box) 0))
1703
1704 (test* "box error handling" (test-error <error> #/out of range/)
1705 (unbox-value (box 1) 1))
1706
1707 (test* "box error handling" (test-error <error> #/Wrong number/)
1708 (set-box! (box 1) 2 3))
1709
1710 (test* "box error handling" (test-error <error> #/Wrong number/)
1711 (set-box! (box 1 2) 3))
1712
1713
1714 ;; https://github.com/shirok/Gauche/issues/787
1715 (test* "box circular printing" "#0=#<box #0#>"
1716 (let1 b (box 1)
1717 (set-box! b b)
1718 (write-to-string b)))
1719 (test* "mvbox circular printing" "#0=#<mv-box[2] 0 #0#>"
1720 (let1 b (box 1 2)
1721 (set-box! b 0 b)
1722 (write-to-string b)))
1723
1724 ;; Gauche specific - generic referencer
1725 (let ([b0 (box 1)]
1726 [b1 (box 1 2 3)])
1727 (test* "box generic referencer" '(1 1 2 3)
1728 (let ([a (~ b0 '*)] ;srfi-122
1729 [b (~ b0 0)])
1730 (set! (~ b0 0) 2)
1731 (let ([c (~ b0 '*)])
1732 (set! (~ b0 '*) 3)
1733 (let ([d (~ b0 0)])
1734 (list a b c d)))))
1735 (test* "mvbox generic referencer" '(1 2 3 10 20 30)
1736 (let ([a (~ b1 0)]
1737 [b (~ b1 1)]
1738 [c (~ b1 2)])
1739 (set! (~ b1 0) 10)
1740 (set! (~ b1 1) 20)
1741 (set! (~ b1 2) 30)
1742 (list* a b c (values->list (unbox b1)))))
1743 )
1744 )
1745
1746 ;;-----------------------------------------------------------------------
1747 ;; Comparators
1748 ;;
1749 ;; SRFI-113 depends on SRFI-114, so we test this first.
1750
1751 (test-section "SRFI-114")
1752
1753 (define-module srfi-114-tests
1754 (use gauche.test)
1755 (use srfi.114)
1756 (test-module 'srfi.114)
1757
1758 ;; builtin comparators are tested in test/compare.scm
1759 (define (test-cmp msg cmpr data) ; data = ((a b result) ...)
1760 (test* msg data
1761 (map (^x (list (car x) (cadr x)
1762 (comparator-compare cmpr (car x) (cadr x))))
1763 data)))
1764
1765 (test-cmp "inexact1"
1766 (make-inexact-real-comparator 0.25 'round 'min)
1767 '((1.0 1.1 0)
1768 (1.1 1.2 -1)
1769 (1.2 1.1 1)
1770 (1.1 1.0 0)
1771 (1.2 1.3 0)
1772 (+nan.0 1.1 -1)
1773 (1.1 +nan.0 1)
1774 (+nan.0 +nan.0 0)))
1775
1776 (test-cmp "inexact2"
1777 (make-inexact-real-comparator 0.25 'round 'max)
1778 '((+nan.0 1.1 1)
1779 (1.1 +nan.0 -1)
1780 (+nan.0 +nan.0 0)))
1781
1782 (test-cmp "inexact3 - nan handling"
1783 ($ make-inexact-real-comparator 0.25 'round
1784 (^x (comparator-compare default-comparator 0 x)))
1785 '((+nan.0 1.1 -1)
1786 (+nan.0 0 0)
1787 (+nan.0 -1.1 1)
1788 (+nan.0 +nan.0 0)))
1789
1790 (test-cmp "list" (make-list-comparator
1791 (make-inexact-real-comparator 0.25 'round 'error))
1792 '(((1 2 3) (1 2 3) 0)
1793 ((1 2 3) (1.1 2 3) 0)
1794 ((1 2 3) (1.1 2) 1)
1795 ((1 2 3) (1.2 2) -1)
1796 ((1 2 3) (1.2 2 3) -1)
1797 (() () 0)
1798 (() (1) -1)
1799 ((1 2 3) (1 2 2.9) 0)
1800 ((1 2 3) (1 2.2 2.9) -1)
1801 ((1 2.2 2.9) (1 2 3) 1)
1802 ))
1803
1804 (test-cmp "vector" (make-vector-comparator
1805 (make-inexact-real-comparator 0.25 'round 'error))
1806 '((#(1 2 3) #(1 2 3) 0)
1807 (#(1 2 3) #(1.1 2 3) 0)
1808 (#(1 2 3) #(1.1 2) 1)
1809 (#(1 2 3) #(1.2 2) 1)
1810 (#(1 2 3) #(1.2 2 3) -1)
1811 (#() #() 0)
1812 (#() #(1) -1)
1813 (#(1 2 3) #(1 2 2.9) 0)
1814 (#(1 2 3) #(1 2.2 2.9) -1)
1815 (#(1 2.2 2.9) #(1 2 3) 1)
1816 ))
1817
1818 (define ca (make-car-comparator string-comparator))
1819 (define cd (make-cdr-comparator integer-comparator))
1820 (define ta ((with-module gauche make-hash-table) ca))
1821 (define td ((with-module gauche make-hash-table) cd))
1822 (define sa '("orange" "apple" "mango" "papaya"))
1823 (define sd '(29 34 98 1001))
1824 (define data (append-map (^a (map (^d (cons a d)) sd)) sa))
1825
1826 (test* "make-car-comparator" (sort sa)
1827 (begin (dolist [x data]
1828 (hash-table-put! ta x #t))
1829 (sort (map car (hash-table-keys ta)))))
1830
1831 (test* "make-cdr-comparator" (sort sd)
1832 (begin (dolist [x data]
1833 (hash-table-put! td x #t))
1834 (sort (map cdr (hash-table-keys td)))))
1835
1836 (let ([lw ($ make-listwise-comparator vector?
1837 (make-reverse-comparator number-comparator)
1838 (^x (= (vector-length x) 0))
1839 (^x (vector-ref x 0))
1840 (^x (vector-copy x 1)))])
1841 (define (test-lw a b expect)
1842 (test* (format "listwise ~s ~s" a b)
1843 (list (zero? expect) expect (- expect) #t)
1844 (list (comparator-equal? lw a b)
1845 (comparator-compare lw a b)
1846 (comparator-compare lw b a)
1847 (integer? (comparator-hash lw a)))))
1848 (test-lw '#(1) '#(2) 1)
1849 (test-lw '#(1 4) '#(1 3 5) -1)
1850 (test-lw '#(1 2) '#(1 3 5) 1)
1851 (test-lw '#(1 3 5) '#(1 3 5) 0)
1852 (test-lw '#(1 3 6) '#(1 3 5) -1)
1853 (test-lw '#() '#() 0)
1854 (test-lw '#() '#(1) -1))
1855
1856 (let ([vw ($ make-vectorwise-comparator list?
1857 (make-reverse-comparator number-comparator)
1858 length
1859 list-ref)])
1860 (define (test-vw a b expect)
1861 (test* (format "vectorwise ~s ~s" a b)
1862 (list (zero? expect) expect (- expect) #t)
1863 (list (comparator-equal? vw a b)
1864 (comparator-compare vw a b)
1865 (comparator-compare vw b a)
1866 (integer? (comparator-hash vw a)))))
1867 (test-vw '(1) '(2) 1)
1868 (test-vw '(1 4) '(1 3 5) -1)
1869 (test-vw '(1 2) '(1 3 5) -1)
1870 (test-vw '(1 3 5) '(1 3 5) 0)
1871 (test-vw '(1 3 6) '(1 3 5) -1)
1872 (test-vw '() '() 0)
1873 (test-vw '() '(1) -1))
1874
1875 (define (test-minmax msg cmp min-item max-item args)
1876 (test* (format "test-minmax ~a" msg)
1877 (list min-item max-item max-item min-item)
1878 (list (apply comparator-min cmp args)
1879 (apply comparator-max cmp args)
1880 (apply comparator-min (make-reverse-comparator cmp) args)
1881 (apply comparator-max (make-reverse-comparator cmp) args))))
1882
1883 (test-minmax "default" default-comparator 1 9
1884 '(3 1 4 5 9 2 6 8 7))
1885 )
1886
1887 ;;-----------------------------------------------------------------------
1888 ;; Sets and bags
1889
1890 (test-section "SRFI-113")
1891
1892 (define-module srfi-113-tests
1893 (use gauche.test)
1894 (use srfi.113)
1895 (test-module 'srfi.113)
1896
1897 (use compat.chibi-test)
1898 (chibi-test
1899 (include "include/sets-test"))
1900
1901 ;; collection framework test
1902 ;; (we use SRFI-1 with prefix not to mess gauche.collection#fold etc.)
1903 (use srfi.1 :prefix srfi-1:)
1904 (use gauche.collection :prefix col:)
1905 (test* "coerce-to" '(a b)
1906 (col:coerce-to <list> (set eq-comparator 'a 'b 'a 'b))
1907 (cut srfi-1:lset= eq? <> <>))
1908 (test* "coerce-to" '(a a b b)
1909 (col:coerce-to <list> (bag eq-comparator 'a 'b 'a 'b))
1910 (cut srfi-1:lset= eq? <> <>))
1911 )
1912
1913 ;;-----------------------------------------------------------------------
1914 ;; Scheme regular expressions
1915
1916 (test-section "SRFI-115")
1917
1918 (define-module srfi-115-tests
1919 (use gauche.test)
1920 (use srfi.115)
1921 (test-module 'srfi.115)
1922
1923 (test* "rx" "abc" (((rx "ab" #\c) "abc") 0))
1924 (test* "regexp" "abc" (((regexp '(:"ab" #\c)) "abc") 0))
1925 (test* "regexp->sre"
1926 '(seq #\a #\b #\c)
1927 (regexp->sre (regexp '(:"ab" #\c))))
1928 (test* "char-set->sre" '("cba") (char-set->sre #[abc]))
1929 (test* "valid-sre? returns true" #t (valid-sre? '(seq #\a #\b)))
1930 (test* "valid-sre? returns false" #f (valid-sre? '(seq or)))
1931
1932 (test* "regexp-matches?" #t (regexp-matches? "abc" "abc"))
1933 (test* "regexp-matches?" #f (regexp-matches? "abc" "abcd"))
1934 (test* "regexp-matches?" #f (regexp-matches? "abc" "zabc"))
1935 (test* "regexp-matches?" #t (regexp-matches? "abc" "zabc" 1))
1936 (test* "regexp-matches?" #t (regexp-matches? "abc" "zabcd" 1 4))
1937
1938 (test* "regexp-fold"
1939 '((9 "abc" 12 15)
1940 (3 "abc" 6 9)
1941 (0 "abc" 0 3))
1942 (regexp-fold "abc"
1943 (lambda (i match str acc)
1944 (cons (if match
1945 (list i
1946 (match 0)
1947 (rxmatch-start match)
1948 (rxmatch-end match))
1949 #f)
1950 acc))
1951 '()
1952 "abc x abc z abc"))
1953
1954 (test* "regexp-fold"
1955 '((0 "abc" 0 3)
1956 (3 "abc" 6 9)
1957 (9 "abc" 12 15))
1958 (regexp-fold "abc"
1959 (lambda (i match str acc)
1960 (cons (if match
1961 (list i
1962 (match 0)
1963 (rxmatch-start match)
1964 (rxmatch-end match))
1965 #f)
1966 acc))
1967 '()
1968 "abc x abc z abc"
1969 (lambda (i match str acc)
1970 (reverse acc))))
1971
1972 (test* "regexp-extract"
1973 '("192" "168" "0" "1")
1974 (regexp-extract '(+ numeric) "192.168.0.1"))
1975 (test* "regexp-extract"
1976 '("123" "456" "789")
1977 (regexp-extract '(+ numeric) "abc123def456ghi789"))
1978 (test* "regexp-extract"
1979 '("123" "456" "789")
1980 (regexp-extract '(* numeric) "abc123def456ghi789"))
1981 (cond-expand
1982 (full-unicode
1983 (test* "regexp-extract"
1984 '("a" "b" "c") (regexp-extract 'grapheme "abc"))
1985 (test* "regexp-extract"
1986 '("a" " " "b" " " "c") (regexp-extract 'grapheme "a b c"))
1987 (test* "regexp-extract"
1988 '("a" "\n" "b" "\r\n" "c") (regexp-extract 'grapheme "a\nb\r\nc"))
1989 (test* "regexp-extract"
1990 '("a\x0300;" "b\x0301;\x0302;" "c\x0303;\x0304;\x0305;")
1991 (regexp-extract 'grapheme "a\x0300;b\x0301;\x0302;c\x0303;\x0304;\x0305;"))
1992 (test* "regexp-extract"
1993 '("한" "글") (regexp-extract 'grapheme "한글"))
1994 (test* "regexp-extract"
1995 '("한" "글")
1996 (regexp-extract
1997 'grapheme
1998 ((with-module gauche.unicode utf8->string)
1999 '#u8(#xe1 #x84 #x92 #xe1 #x85 #xa1 #xe1 #x86 #xab
2000 #xe1 #x84 #x80 #xe1 #x85 #xb3 #xe1 #x86 #xaf)))))
2001 (else))
2002
2003 (test* "regexp-split"
2004 '("abc" "def" "ghi" "")
2005 (regexp-split '(+ numeric) "abc123def456ghi789"))
2006 (test* "regexp-split"
2007 '("abc" "def" "ghi" "")
2008 (regexp-split '(* numeric) "abc123def456ghi789"))
2009 (test* "regexp-split"
2010 '("a" "b") (regexp-split '(+ whitespace) "a b"))
2011 (test* "regexp-split"
2012 '("a" "" "b")
2013 (regexp-split '(",;") "a,,b"))
2014 (test* "regexp-split"
2015 '("a" "" "b" "")
2016 (regexp-split '(",;") "a,,b,"))
2017
2018 (test* "regexp-partition"
2019 '("")
2020 (regexp-partition '(* numeric) ""))
2021 (test* "regexp-partition"
2022 '("abc" "123" "def" "456" "ghi")
2023 (regexp-partition '(* numeric) "abc123def456ghi"))
2024 (test* "regexp-partition"
2025 '("abc" "123" "def" "456" "ghi" "789")
2026 (regexp-partition '(* numeric) "abc123def456ghi789"))
2027
2028 (test* "regexp-replace"
2029 "abc def"
2030 (regexp-replace '(+ space) "abc \t\n def" " "))
2031 (test* "regexp-replace"
2032 " abc-abc"
2033 (regexp-replace '(: ($ (+ alpha)) ":" (* space)) " abc: " '(1 "-" 1)))
2034 (test* "regexp-replace"
2035 " abc- abc"
2036 (regexp-replace '(: ($ (+ alpha)) ":" (* space)) " abc: " '(1 "-" pre 1)))
2037 (test* "regexp-replace"
2038 "abcghighi"
2039 (regexp-replace "def" "abcdefghi" 'post))
2040 (test* "regexp-replace"
2041 "abcabcghi"
2042 (regexp-replace "def" "abcdefghi" 'pre))
2043
2044 (test* "regexp-replace"
2045 "-abc \t\n d ef "
2046 (regexp-replace '(+ space) " abc \t\n d ef " "-" 0))
2047 (test* "regexp-replace"
2048 "-abc \t\n d ef "
2049 (regexp-replace '(+ space) " abc \t\n d ef " "-" 0 #f 0))
2050 (test* "regexp-replace"
2051 " abc-d ef "
2052 (regexp-replace '(+ space) " abc \t\n d ef " "-" 0 #f 1))
2053 (test* "regexp-replace"
2054 " abc \t\n d-ef "
2055 (regexp-replace '(+ space) " abc \t\n d ef " "-" 0 #f 2))
2056 (test* "regexp-replace"
2057 " abc \t\n d ef-"
2058 (regexp-replace '(+ space) " abc \t\n d ef " "-" 0 #f 3))
2059 (test* "regexp-replace"
2060 " abc \t\n d ef "
2061 (regexp-replace '(+ space) " abc \t\n d ef " "-" 0 #f 4))
2062 (test* "regexp-replace"
2063 " abc d ef "
2064 (regexp-replace-all '(+ space) " abc \t\n d ef " " "))
2065
2066 (test* "regexp-replace"
2067 "bc pre: <<<bc >>> match1: <<<def>>> post: <<<gh>>>gh"
2068 (regexp-replace '(: ($ (+ alpha)) ":" (* space))
2069 "abc def: ghi"
2070 '("pre: <<<" pre ">>> match1: <<<" 1 ">>> post: <<<" post ">>>")
2071 1 11))
2072 )
2073
2074 ;;-----------------------------------------------------------------------
2075 ;; Immutable list library
2076
2077 (test-section "SRFI-116")
2078
2079 (define-module srfi-116-tests
2080 (use gauche.test)
2081 (use srfi.116)
2082 (test-module 'srfi.116)
2083 (test-include-r7 "include/srfi-116-tests.scm"
2084 (exclude (ilists ilists))))
2085
2086 ;;-----------------------------------------------------------------------
2087 ;; Queues based on lists
2088
2089 (test-section "SRFI-117")
2090
2091 (define-module srfi-117-tests
2092 (use gauche.test)
2093 (use srfi.117)
2094 (test-module 'srfi.117)
2095
2096 (use compat.chibi-test)
2097 (chibi-test
2098 (include "include/list-queues-test")))
2099
2100 ;;-----------------------------------------------------------------------
2101 ;; Simple adjustable-size strings
2102
2103 (test-section "SRFI-118")
2104
2105 (define-module srfi-118-tests
2106 (use gauche.test)
2107 (use srfi.118)
2108 (test-module 'srfi.118)
2109
2110 (test* "string-append!" "abcdef"
2111 (rlet1 s (string-copy "ab")
2112 (string-append! s "cd" #\e "f")))
2113
2114 (test* "string-replace!" "abcXYZfgh"
2115 (rlet1 s (string-copy "abcdefgh")
2116 (string-replace! s 3 5 "XYZ")))
2117
2118 (test* "string-replace!" "abcXYZfgh"
2119 (rlet1 s (string-copy "abcdefgh")
2120 (string-replace! s 3 5 "WXYZw" 1 4)))
2121 )
2122
2123 ;;-----------------------------------------------------------------------
2124 ;; SRFI-120 depends on control.scheduler, so tested in control.scm
2125
2126 ;;-----------------------------------------------------------------------
2127 ;; Generators
2128
2129 (test-section "SRFI-121")
2130
2131 (define-module srfi-121-tests
2132 (use gauche.test)
2133 (use compat.chibi-test)
2134 (use scheme.list)
2135 (use scheme.base :prefix r7rs:)
2136 (use srfi.11)
2137 (use srfi.121) ; this is in fact gauche.generator
2138 (test-module 'srfi.121)
2139
2140 (let ([string-for-each r7rs:string-for-each])
2141 (chibi-test
2142 (include "include/generators-test")))
2143
2144 (test* "generator->vector!" '(3 #(1 2 3) 4 0)
2145 (let ((g (list->generator '(1 2 3 4 5)))
2146 (v (make-vector 3)))
2147 (list (generator->vector! v 0 g)
2148 v
2149 (g)
2150 (generator->vector! v 3 g))))
2151 )
2152
2153 ;;-----------------------------------------------------------------------
2154 ;; Ephemerons
2155
2156 (test-section "SRFI-124")
2157
2158 (define-module srfi-124-tests
2159 (use gauche.test)
2160 (use srfi.124)
2161 (test-module 'srfi.124)
2162
2163 (define k0 (list 'key0))
2164 (define k1 (list 'key1))
2165 (define d0 (list 'datum0))
2166 (define d1 (list 'datum2))
2167 (define ds (make-weak-vector 2))
2168 (weak-vector-set! ds 0 d0)
2169 (weak-vector-set! ds 1 d1)
2170 (define e0 (make-ephemeron k0 d0))
2171 (define e1 (make-ephemeron k1 d1))
2172
2173 (test* "ephemeron?" #t (ephemeron? e0))
2174 (test* "ephemeron-key" #t
2175 (and (eq? (ephemeron-key e0) k0)
2176 (eq? (ephemeron-key e1) k1)))
2177 (test* "ephemeron-datum" #t
2178 (and (eq? (ephemeron-datum e0) d0)
2179 (eq? (ephemeron-datum e1) d1)))
2180
2181 (set! d0 #f)
2182 (set! d1 #f)
2183
2184 (gc)
2185 (gc)
2186
2187 (test* "ephemeron-broken?" #f
2188 (or (ephemeron-broken? e0) (ephemeron-broken? e1)))
2189
2190 (set! k0 #f)
2191
2192 (gc)
2193 (gc)
2194
2195 (test* "ephemeron-broken? e0" #t (ephemeron-broken? e0))
2196 (test* "ephemeron-broken? e1" #f (ephemeron-broken? e1))
2197
2198 (gc)
2199 (gc)
2200
2201 (test* "d0 collected" #f (weak-vector-ref ds 0 #f))
2202 (test* "d1 not collected" #f (not (weak-vector-ref ds 1 #f)))
2203 )
2204
2205 ;;-----------------------------------------------------------------------
2206 ;; Intermediate hash tables
2207
2208 (test-section "SRFI-125")
2209
2210 (define-module srfi-125-tests
2211 (use gauche.test :only (test* test-module))
2212 (use srfi.125)
2213 (test-module 'srfi.125)
2214 (use scheme.sort :only (list-sort))
2215 (define (exit x) #f) ;ignore
2216 (define-syntax import ;ignore
2217 (syntax-rules ()
2218 [(_ . xs) (begin)]))
2219 (define-syntax test
2220 (syntax-rules ()
2221 [(_ expr expected) (test* 'expr expected expr)]))
2222 (define-syntax test-assert
2223 (syntax-rules ()
2224 [(_ expr) (test* 'expr #t expr)]))
2225 (define-syntax test-deny
2226 (syntax-rules ()
2227 [(_ expr) (test* 'expr #f expr)]))
2228 ;; srfi-125-tests includes deprecated SRFI-69 procedures tests.
2229 ;; some of them emits warnings in Gauche, so we use dummies here.
2230 (define (hash x . _) (default-hash x))
2231 (define (hash-by-identity x . _) (eq-hash x))
2232 (define deprecated:string-hash string-hash)
2233 (define deprecated:string-ci-hash string-ci-hash)
2234 (include "include/srfi-125-tests.scm"))
2235
2236 ;;-----------------------------------------------------------------------
2237 ;; Lazy sequences
2238
2239 (test-section "SRFI-127")
2240
2241 (define-module srfi-127-tests
2242 (use gauche.test)
2243 (use srfi.127)
2244 (test-module 'srfi.127)
2245
2246 (use compat.chibi-test)
2247 (chibi-test
2248 (include "include/lseqs-test.scm"))
2249 )
2250
2251 ;;-----------------------------------------------------------------------
2252 ;; Comparators (reduced)
2253
2254 (test-section "SRFI-128")
2255
2256 (define-module srfi-128-tests
2257 ;; everything is built-in, so we just test bindings.
2258 (use gauche.test)
2259 (use srfi.128)
2260 (test-module 'srfi.128)
2261 )
2262
2263 ;;-----------------------------------------------------------------------
2264 ;; Cursor-based string library
2265
2266 (test-section "SRFI-130")
2267
2268 (define-module srfi-130-tests
2269 (use gauche.test)
2270 (use srfi.130)
2271 (test-module 'srfi.130)
2272
2273 (include "include/srfi-130-tests.scm"))
2274
2275 ;;-----------------------------------------------------------------------
2276 ;; Sort libraries
2277
2278 (test-section "SRFI-132")
2279
2280 (define-module srfi-132-tests
2281 (use gauche.test)
2282 (use srfi.132)
2283 (use scheme.list)
2284 (test-module 'srfi.132)
2285
2286 ;; partition-in-place! is not external, but it is such a fundamental
2287 ;; part of the algorithm that we test it independently.
2288
2289 (let ([partition-in-place! (with-module srfi.132 partition-in-place!)]
2290 [data '((a #(a))
2291 (a #(b))
2292 (b #(a))
2293 (q #(q z))
2294 (z #(q z))
2295 (r #(q z))
2296 (q #(q q))
2297 (z #(j z e))
2298 (e #(j e j))
2299 (m #(m m m))
2300 (p #(z p e a))
2301 (m #(m z m m m))
2302 (m #(m z z m m))
2303 (m #(m z m z m))
2304 (m #(p q m r s))
2305 (m #(a z m z m))
2306 (m #(m z a z m))
2307 (m #(m a m m m))
2308 (m #(a m a m m))
2309 (m #(m a m a a))
2310 (p #(t w o h o u s e h o l d s b o t h a l i k e i n d i g n
2311 i t y i n f a i r v e l o n a w h e r e w e l a y o u
2312 r s c e n e)))])
2313 (define (t vec pivot)
2314 (let* ([smaller (filter (^e (<? default-comparator e pivot))
2315 (vector->list vec))]
2316 [greater (filter (^e (<? default-comparator pivot e))
2317 (vector->list vec))]
2318 [same (filter (^e (=? default-comparator pivot e))
2319 (vector->list vec))])
2320 (let1 v (vector-copy vec)
2321 (test* (format "partition-in-place! ~s @~s" vec pivot)
2322 (list smaller greater)
2323 (receive (i j) (partition-in-place!
2324 (^[a b] (<? default-comparator a b)) pivot
2325 v 0 (vector-length vec))
2326 (list (vector->list v 0 i)
2327 (vector->list v i j)))
2328 (^[a b]
2329 (and (lset= eq? (car a) (car b))
2330 (lset= eq? (cadr a) (cadr b))))))
2331 (unless (null? same)
2332 (let1 k (+ (length smaller)
2333 (modulo (sys-random) (length same)))
2334 (test* (format "vector-separate! ~s ~s" vec k)
2335 (list (append smaller (make-list (- k (length smaller)) pivot))
2336 (append greater (make-list (- (length same)
2337 (- k (length smaller)))
2338 pivot)))
2339 (let1 v (vector-copy vec)
2340 (vector-separate! (^[a b] (<? default-comparator a b)) v k)
2341 (list (vector->list v 0 k)
2342 (vector->list v k (vector-length vec))))
2343 (^[a b]
2344 (and (lset= eq? (car a) (car b))
2345 (lset= eq? (cadr a) (cadr b)))))))))
2346 (dolist [d data] [t (vector-copy (cadr d)) (car d)]))
2347
2348 (let ([data '(#()
2349 #(15)
2350 #(2 5)
2351 #(18 61 30)
2352 #(61 39 13 4)
2353 #(78 61 19 38 51)
2354 #(60 68 17 45 6 1)
2355 #(80 38 30 1 9 23 68)
2356 #(12 60 49 83 3 17 90 39)
2357 #(66 75 31 42 52 20 54 56 18)
2358 #(96 84 77 44 93 39 89 92 59 72)
2359 #(92 32 8 57 19 52 4 96 32 84 38 74 27 53)
2360 #(54 27 14 29 80 16 7 1 98 42 77 39 19 29 0 1 81)
2361 #(72 79 77 60 7 92 35 65 11 91 86 85 62 44 33 9 6 37 31 66)
2362 #(32 21 51 55 70 40 36 6 97 25 96 24 25 69 49 71 30 14 28 99
2363 3 26 60 97 50 41 5 3 27 34 90 63 23 68 14 53 48 44 98 55))])
2364 (define (my-median elt< vec fallback)
2365 (let* ([svec (vector-sort elt< vec)]
2366 [len (vector-length vec)])
2367 (if (zero? len)
2368 fallback
2369 (if (odd? len)
2370 (vector-ref svec (quotient len 2))
2371 (/ (+ (vector-ref svec (quotient len 2))
2372 (vector-ref svec (- (quotient len 2) 1)))
2373 2)))))
2374 (define (t vec)
2375 (test* (format "vector-select! ~s <" vec)
2376 (vector->list (vector-sort < vec))
2377 (map (^k (vector-select! < (vector-copy vec) k))
2378 (iota (vector-length vec))))
2379 (test* (format "vector-select! ~s >" vec)
2380 (vector->list (vector-sort > vec))
2381 (map (^k (vector-select! > (vector-copy vec) k))
2382 (iota (vector-length vec))))
2383 (let1 expect (list (my-median < vec 'none)
2384 (my-median > vec 'none))
2385 (test* (format "vector-find-median ~s" vec)
2386 expect
2387 (list (vector-find-median < vec 'none)
2388 (vector-find-median > vec 'none)))
2389 (test* (format "vector-find-median! ~s" vec)
2390 expect
2391 (list (vector-find-median! < (vector-copy vec) 'none)
2392 (vector-find-median! > (vector-copy vec) 'none)))))
2393
2394 (for-each t data))
2395 )
2396
2397 ;;-----------------------------------------------------------------------
2398 ;; Immutable texts
2399
2400 (test-section "SRFI-135")
2401
2402 (define-module srfi-135-tests
2403 (use gauche.test)
2404 (use srfi.135)
2405 (test-module 'srfi.135)
2406 ;; srfi-135-tests.scm rolls its own test framework; this hack adapts them
2407 ;; to Gauche's.
2408 (define-syntax orig-or (with-module gauche or))
2409 (define-syntax or
2410 (syntax-rules (fail)
2411 [(_ x (fail 'what))
2412 (test* (format "~a: ~s" 'what 'x) #t x)]
2413 [(_ . xs) (orig-or . xs)]))
2414 (test-include-r7 "include/srfi-135-tests.scm"))
2415
2416 ;;-----------------------------------------------------------------------
2417 ;; Syntax parameters
2418
2419 (test-section "SRFI-139")
2420
2421 (define-module srfi-139-tests
2422 (use gauche.test)
2423 ;; srfi-139 is built-in
2424 (define-syntax-parameter foo (syntax-rules () [(_ a) '(a a)]))
2425
2426 (test* "syntax parameter 1" '((oof oof) (foof foof foof))
2427 (list (foo oof)
2428 (syntax-parameterize ([foo (syntax-rules () [(_ a) '(a a a)])])
2429 (foo foof))))
2430
2431 (test* "syntax-parameter 2" '((oof oof) (foof))
2432 (list (foo oof)
2433 (syntax-parameterize ([foo (make-id-transformer
2434 (syntax-rules ()
2435 [_ 'foof]))])
2436 (list foo))))
2437 )
2438
2439 ;;-----------------------------------------------------------------------
2440 ;; Integer division
2441
2442 (test-section "SRFI-141")
2443
2444 (define-module srfi-141-tests
2445 (use gauche.test)
2446 (use srfi.141)
2447 (test-module 'srfi.141)
2448
2449 (let1 data
2450 ;;numer denom floor truncate ceiling round euclidean balanced
2451 '[(23 5 (4 3) (4 3) (5 -2) (5 -2) (4 3) (5 -2))
2452 (22 5 (4 2) (4 2) (5 -3) (4 2) (4 2) (4 2))
2453 (23 -5 (-5 -2) (-4 3) (-4 3) (-5 -2) (-4 3) (-5 -2))
2454 (22 -5 (-5 -3) (-4 2) (-4 2) (-4 2) (-4 2) (-4 2))
2455 (-23 5 (-5 2) (-4 -3) (-4 -3) (-5 2) (-5 2) (-5 2))
2456 (-22 5 (-5 3) (-4 -2) (-4 -2) (-4 -2) (-5 3) (-4 -2))
2457 (-23 -5 (4 -3) (4 -3) (5 2) (5 2) (5 2) (5 2))
2458 (-22 -5 (4 -2) (4 -2) (5 3) (4 -2) (5 3) (4 -2))
2459 (25 5 (5 0) (5 0) (5 0) (5 0) (5 0) (5 0))
2460 (-25 5 (-5 0) (-5 0) (-5 0) (-5 0) (-5 0) (-5 0))
2461 (25 -5 (-5 0) (-5 0) (-5 0) (-5 0) (-5 0) (-5 0))
2462 (-25 -5 (5 0) (5 0) (5 0) (5 0) (5 0) (5 0))
2463 (1 2 (0 1) (0 1) (1 -1) (0 1) (0 1) (1 -1))
2464 (-1 2 (-1 1) (0 -1) (0 -1) (0 -1) (-1 1) (0 -1))
2465 (1 -2 (-1 -1) (0 1) (0 1) (0 1) (0 1) (-1 -1))
2466 (-1 -2 (0 -1) (0 -1) (1 1) (0 -1) (1 1) (0 -1))
2467 (3 2 (1 1) (1 1) (2 -1) (2 -1) (1 1) (2 -1))
2468 (-3 2 (-2 1) (-1 -1) (-1 -1) (-2 1) (-2 1) (-1 -1))
2469 (3 -2 (-2 -1) (-1 1) (-1 1) (-2 -1) (-1 1) (-2 -1))
2470 (-3 -2 (1 -1) (1 -1) (2 1) (2 1) (2 1) (1 -1))
2471 (39 6 (6 3) (6 3) (7 -3) (6 3) (6 3) (7 -3))
2472 (39 -6 (-7 -3) (-6 3) (-6 3) (-6 3) (-6 3) (-7 -3))
2473 (-39 6 (-7 3) (-6 -3) (-6 -3) (-6 -3) (-7 3) (-6 -3))
2474 (-39 -6 (6 -3) (6 -3) (7 3) (6 -3) (7 3) (6 -3))
2475 ]
2476 (define (t-1 numer denom floor truncate ceiling round euclidean balanced)
2477 (define (t name x-/ x-quotient x-remainder answers)
2478 (test* #"SRFI-141 ~name ~|numer|/~|denom|"
2479 (list answers answers
2480 (map inexact answers) (map inexact answers)
2481 (map inexact answers) (map inexact answers))
2482 (list (values->list (x-/ numer denom))
2483 (list (x-quotient numer denom)
2484 (x-remainder numer denom))
2485 (values->list (x-/ (inexact numer) denom))
2486 (list (x-quotient (inexact numer) denom)
2487 (x-remainder (inexact numer) denom))
2488 (values->list (x-/ numer (inexact denom)))
2489 (list (x-quotient numer (inexact denom))
2490 (x-remainder numer (inexact denom))))))
2491 (t 'floor floor/ floor-quotient floor-remainder floor)
2492 (t 'truncate truncate/ truncate-quotient truncate-remainder truncate)
2493 (t 'ceiling ceiling/ ceiling-quotient ceiling-remainder ceiling)
2494 (t 'round round/ round-quotient round-remainder round)
2495 (t 'euclidean euclidean/ euclidean-quotient euclidean-remainder euclidean)
2496 (t 'balanced balanced/ balanced-quotient balanced-remainder balanced))
2497
2498 (for-each (cut apply t-1 <>) data))
2499 )
2500
2501 ;;-----------------------------------------------------------------------
2502 ;; Fixnums
2503
2504 (test-section "SRFI-143")
2505
2506 (define-module srfi-143-tests
2507 (use gauche.test)
2508 (use srfi.143)
2509 (test-module 'srfi.143)
2510
2511 (use compat.chibi-test)
2512 (chibi-test
2513 (include "include/fixnum-tests.scm")))
2514
2515 ;;----------------------------------------------------------------------
2516 ;; Mappings
2517
2518 (test-section "SRFI-146")
2519
2520 (define-module srfi-146-tests
2521 (use gauche.test)
2522 (use srfi.146)
2523 (test-module 'srfi.146)
2524 (use srfi.146.hash)
2525 (test-module 'srfi.146.hash)
2526
2527 (use scheme.list)
2528 (use srfi.64)
2529 (use compat.r7rs-srfi-tests)
2530
2531 (let ()
2532 (include "include/srfi-146-tests.scm")
2533 (run-tests))
2534
2535 (let ()
2536 (include "include/srfi-146-hash-tests.scm")
2537 (run-tests))
2538 )
2539
2540 ;;-----------------------------------------------------------------------
2541 ;; Custom macro transformers
2542
2543 (test-section "SRFI-147")
2544
2545 (define-module srfi-147-tests
2546 (use gauche.test)
2547 (use srfi.147)
2548 (test-module 'srfi.147)
2549
2550 (use srfi.64)
2551 (use compat.r7rs-srfi-tests)
2552 (include "include/srfi-147-tests.scm")
2553 (run-tests)
2554 )
2555
2556 ;;-----------------------------------------------------------------------
2557 ;; NB: SRFI-150 is tested in ext/gauche
2558
2559 ;;-----------------------------------------------------------------------
2560 ;; Bitwise operations
2561
2562 (test-section "SRFI-151")
2563
2564 (define-module srfi-151-tests
2565 (use gauche.test)
2566 (use srfi.151)
2567 (test-module 'srfi.151)
2568
2569 (use compat.chibi-test)
2570 (chibi-test
2571 (include "include/srfi-151-tests.scm"))
2572 )
2573
2574 ;;-----------------------------------------------------------------------
2575 ;; String library (reduced)
2576
2577 (test-section "SRFI-152")
2578
2579 (define-module srfi-152-tests
2580 (use gauche.test)
2581 (use srfi.152)
2582 (test-module 'srfi.152)
2583
2584 (use compat.chibi-test)
2585 (chibi-test
2586 (include "include/srfi-152-tests.scm")))
2587
2588 ;;-----------------------------------------------------------------------
2589 ;; First-class dynamic extents
2590
2591 (test-section "SRFI-154")
2592
2593 (define-module srfi-154-tests
2594 (use gauche.test)
2595 (use srfi.64)
2596 (use srfi.154)
2597 (test-module 'srfi.154)
2598
2599 (use compat.r7rs-srfi-tests)
2600 (include "include/srfi-154-tests.scm")
2601 (run-tests))
2602
2603 ;;-----------------------------------------------------------------------
2604 ;; Promises
2605
2606 (test-section "SRFI-155")
2607
2608 (define-module srfi-155-tests
2609 (use gauche.test)
2610 (use srfi.155)
2611 (test-module 'srfi.155)
2612
2613 (use srfi.64)
2614 (use srfi.154)
2615 (use compat.r7rs-srfi-tests)
2616 (include "include/srfi-155-tests.scm")
2617 (run-tests)
2618
2619 (test-section "srfi-155 additional")
2620 (test* "extent" 1
2621 (let ()
2622 (define x (make-parameter 1))
2623 (define p (delay (x)))
2624 (define (g p) (parameterize ((x 2)) (force p)))
2625 (g p)))
2626 )
2627
2628 ;;-----------------------------------------------------------------------
2629 ;; Generators and accumulators
2630
2631 (test-section "SRFI-158")
2632
2633 (define-module srfi-158-tests
2634 (use gauche.test)
2635 (use srfi.158)
2636 (test-module 'srfi.158)
2637
2638 (use compat.chibi-test)
2639 (use gauche.uvector)
2640 (define bytevector u8vector)
2641 (use scheme.list)
2642 (use srfi.13)
2643 (chibi-test
2644 (include "include/srfi-158-tests.scm")))
2645
2646 ;;-----------------------------------------------------------------------
2647 ;; Combinator formatting
2648
2649 (test-section "SRFI-159")
2650
2651 (define-module srfi-159-tests
2652 (use gauche.test)
2653 (use srfi.159)
2654 (test-module 'srfi.159)
2655
2656 (use compat.chibi-test)
2657 (use scheme.list)
2658 (use scheme.file)
2659 (chibi-test
2660 (include "include/srfi-159-tests.scm"))
2661 )
2662
2663 ;;-----------------------------------------------------------------------
2664 ;; Homogeneous numeric vector libraries
2665
2666 (test-section "SRFI-160")
2667
2668 (define-module srfi-160-tests
2669 (use gauche.test)
2670 (use srfi.160)
2671 (test-module 'srfi.160)
2672
2673 (use compat.chibi-test)
2674 (chibi-test
2675 (include "include/srfi-160-base-tests.scm")
2676 (include "include/srfi-160-tests.scm")
2677 ))
2678
2679 ;;-----------------------------------------------------------------------
2680 ;; Comparators sublibrary
2681
2682 (test-section "SRFI-162")
2683
2684 (define-module srfi-160-tests
2685 (use gauche.test)
2686 (use srfi.162)
2687 (test-module 'srfi.162)
2688 ;; if we pass srfi-114 tests, it should be ok.
2689 )
2690
2691 ;;-----------------------------------------------------------------------
2692 ;; POSIX API
2693
2694 (test-section "SRFI-170")
2695
2696 ;; SRFI-170 is a thin wrapper of Gauche's builtins. We only test
2697 ;; procedures that are doing something more.
2698
2699 (define-module srfi-170-tests
2700 (use gauche.test)
2701 (use srfi.170)
2702 (test-module 'srfi.170)
2703
2704 (use scheme.list)
2705 (use file.util)
2706 (use gauche.test)
2707 (use gauche.generator)
2708
2709 (when (file-exists? "test.o")
2710 (if (file-is-directory? "test.o")
2711 (remove-directory* "test.o")
2712 (sys-unlink "test.o")))
2713 (make-directory* "test.o")
2714 (touch-files '("test.o/a" "test.o/b" "test.o/c" "test.o/.a"))
2715
2716 (unwind-protect
2717 (begin
2718 (test* "directory-files" '("a" "b" "c")
2719 (directory-files "test.o")
2720 (cut lset= equal? <> <>))
2721 (test* "directory-files w/dot" '("a" "b" "c" ".a")
2722 (directory-files "test.o" #t)
2723 (cut lset= equal? <> <>))
2724
2725 (test* "make-directory-files-generator"
2726 '("a" "b" "c")
2727 (generator->list (make-directory-files-generator "test.o"))
2728 (cut lset= equal? <> <>))
2729 (test* "make-directory-files-generator w/dot"
2730 '("a" "b" "c" ".a")
2731 (generator->list (make-directory-files-generator "test.o" #t))
2732 (cut lset= equal? <> <>))
2733
2734 (test* "open/read/close-directory"
2735 '("a" "b" "c")
2736 (let1 d (open-directory "test.o")
2737 (unwind-protect
2738 (generator->list (cut read-directory d))
2739 (close-directory d))))
2740
2741 (test* "call-with-temporary-filename"
2742 #t
2743 (let1 ff (call-with-temporary-filename
2744 (^f (let1 z (open-file f 'binary-output
2745 (logior open/create
2746 open/exclusive)
2747 #o600)
2748 (close-port z)
2749 f))
2750 "test.o/")
2751 (boolean (#/^test.o\/\w+/ ff))))
2752 )
2753 (remove-directory* "test.o"))
2754 )
2755
2756 ;;-----------------------------------------------------------------------
2757 ;; Two safer subsets of R7RS
2758
2759 (test-section "SRFI-172")
2760
2761 (define-module srfi-172-tests
2762 (use gauche.test)
2763 (use srfi.172)
2764 (test-module 'srfi.172))
2765
2766 (define-module srfi-172-functional-tests
2767 (use gauche.test)
2768 (use srfi.172.functional)
2769 (test-module 'srfi.172.functional))
2770
2771 ;;-----------------------------------------------------------------------
2772 ;; Hooks
2773
2774 (test-section "SRFI-173")
2775
2776 (define-module srfi-173-tests
2777 (use gauche.test)
2778 (use srfi.173)
2779 (test-module 'srfi.173)
2780
2781 (use compat.chibi-test)
2782 (chibi-test
2783 (include "include/srfi-173-tests.scm")))
2784
2785 ;;-----------------------------------------------------------------------
2786 ;; POSIX timespecs
2787
2788 (test-section "SRFI-174")
2789
2790 (define-module srfi-174-tests
2791 (use gauche.test)
2792 (use srfi.174)
2793 (test-module 'srfi.174)
2794
2795 (let ([t1 (timespec 123456789 987654321)]
2796 [t2 (timespec 123456789 987654322)]
2797 [t3 (timespec -123456789 987654321)])
2798 (define (ts-approx=? a b)
2799 (and (= (timespec-seconds a) (timespec-seconds b))
2800 (< (abs (- (timespec-nanoseconds a) (timespec-nanoseconds b)))
2801 10)))
2802 (test* "timespec?" '(#t #t #t)
2803 (map timespec? (list t1 t2 t3)))
2804 (test* "timespec-seconds" 123456789 (timespec-seconds t1))
2805 (test* "timespec-seconds" -123456789 (timespec-seconds t3))
2806 (test* "timespec-nanoseconds" 987654322 (timespec-nanoseconds t2))
2807 (test* "timespec-nanoseconds" 987654321 (timespec-nanoseconds t3))
2808 (test* "inexact <-> timespec" t1
2809 (inexact->timespec (timespec->inexact t1))
2810 ts-approx=?)
2811 (test* "inexact <-> timespec" t3
2812 (inexact->timespec (timespec->inexact t3))
2813 ts-approx=?)
2814 (test* "inexact conversion" (- (timespec->inexact t1))
2815 (timespec->inexact t3))
2816 (test* "timespec=?" #t (timespec=? t1 t1))
2817 (test* "timespec=?" #f (timespec=? t1 t2))
2818 (test* "timespec=?" #f (timespec=? t1 t3))
2819 (test* "timespec<?" #t (timespec<? t1 t2))
2820 (test* "timespec<?" #f (timespec<? t2 t1))
2821 (test* "timespec<?" #t (timespec<? t3 t1))
2822 (test* "timespec<?" #t (timespec<? t3 t2))
2823 )
2824 )
2825
2826 ;;-----------------------------------------------------------------------
2827 ;; ASCII character library
2828
2829 (test-section "SRFI-175")
2830
2831 (define-module srfi-175-tests
2832 (use gauche.test)
2833 (use srfi.175)
2834 (test-module 'srfi.175)
2835
2836 (use gauche.unicode) ;string->utf8
2837 (define-syntax want
2838 (syntax-rules ()
2839 [(_ expected expr) (test* (write-to-string 'expr) expected expr)]))
2840 (include "include/srfi-175-tests"))
2841
2842 ;;-----------------------------------------------------------------------
2843 ;; NB: SRFI-180 depends on ext/peg, so it is tested in it.
2844
2845 ;;-----------------------------------------------------------------------
2846 ;; NB: SRFI-181 is tested in gauche.vport
2847
2848 ;;-----------------------------------------------------------------------
2849 ;; Linear adjustable-length strings
2850 (test-section "SRFI-185")
2851
2852 (define-module srfi-185-tests
2853 (use gauche.test)
2854 (use srfi.185)
2855 (test-module 'srfi.185)
2856
2857 (test* "string-append-linear!" "abcdefghij"
2858 (string-append-linear! "abc" "def" #\g "" "hij"))
2859 (test* "string-append-linear!" "xyz"
2860 (string-append-linear! "xyz"))
2861 (test* "string-replace-linear!" "abqqqi"
2862 (string-replace-linear! "abcdefghi" 2 8 "qqq"))
2863 (test* "string-replace-linear!" "yzghi"
2864 (string-replace-linear! "abcdefghi" 0 6 "xyz" 1))
2865 (test* "string-replace-linear!" "yghi"
2866 (string-replace-linear! "abcdefghi" 0 6 "xyz" 1 2))
2867
2868 (test* "string-append!" "abcdef"
2869 (rlet1 s "ab"
2870 (string-append! s "cd" #\e "f")))
2871
2872 (test* "string-replace!" "abcXYZfgh"
2873 (rlet1 s "abcdefgh"
2874 (string-replace! s 3 5 "XYZ")))
2875
2876 (test* "string-replace!" "abcXYZfgh"
2877 (rlet1 s "abcdefgh"
2878 (string-replace! s 3 5 "WXYZw" 1 4)))
2879 )
2880
2881 ;;-----------------------------------------------------------------------
2882 ;; Maybe and either
2883
2884 (test-section "SRFI-189")
2885
2886 (define-module srfi-189-tests
2887 (use gauche.test)
2888 (use srfi.189)
2889 (test-module 'srfi.189)
2890 (test-include-r7 "include/srfi-189-tests"))
2891
2892 ;;-----------------------------------------------------------------------
2893 ;; Coroutine generators
2894
2895 (test-section "SRFI-190")
2896
2897 (define-module srfi-190-tests
2898 (use gauche.test)
2899 (use srfi.190)
2900 (test-module 'srfi.190)
2901 (test-include-r7 "include/srfi-190-tests"))
2902
2903 ;;-----------------------------------------------------------------------
2904 ;; NB: SRFI-192 is tested in gauche.vport
2905
2906 ;;-----------------------------------------------------------------------
2907 ;; NB: SRFI-194 is tested in ext/srfi
2908
2909 ;;-----------------------------------------------------------------------
2910 ;; NB: SRFI-196 is tested with data.range
2911
2912 ;;-----------------------------------------------------------------------
2913 ;; Pipeline operators
2914
2915 (test-section "SRFI-197")
2916
2917 (define-module srfi-197-tests
2918 (use gauche.test)
2919 (use srfi.197)
2920 (test-module 'srfi.197)
2921
2922 (use scheme.base)
2923 (use scheme.process-context)
2924 (use scheme.write)
2925 (use srfi.64)
2926 (include "include/srfi-197-test"))
2927
2928 ;;-----------------------------------------------------------------------
2929 ;; NB: SRFI-207 is tested with gauche.uvector
2930
2931 ;;-----------------------------------------------------------------------
2932 ;; NB: SRFI-209 is tested with gauche.bitvector
2933
2934 ;;-----------------------------------------------------------------------
2935 ;; Procedures and syntax for multiple values
2936
2937 (test-section "SRFI-210")
2938
2939 (define-module srfi-210-tests
2940 (use gauche.test)
2941 (use srfi.210)
2942 (test-module 'srfi.210)
2943 (test-include-r7 "include/srfi-210-tests")
2944 (with-module srfi.210.test (run-tests)))
2945
2946 ;;-----------------------------------------------------------------------
2947 ;; Central log exchange
2948
2949 (test-section "SRFI-215")
2950
2951 (define-module srfi-215-tests
2952 (use gauche.test)
2953 (use srfi.215)
2954 (test-module 'srfi.215)
2955
2956 (use gauche.logger)
2957 (parameterize ((log-default-drain log-default-drain))
2958 (define (t what expected . args)
2959 (test* #"send-log (~what)" expected
2960 (with-output-to-string (cut apply send-log args))))
2961 (log-open 'current-output :prefix ">>>")
2962 (t "basic" ">>>INFO: test ()\n" INFO "test")
2963 (t "params" ">>>DEBUG: params ((METHOD . \"get\") (PATH . \"/login\") (SEQ . 255))\n"
2964 DEBUG "params" 'METHOD 'get 'PATH "/login" 'SEQ 255)
2965
2966 (parameterize ((current-log-fields
2967 '(USER "guest" TOKEN #u8(1 2 3) OP :delete)))
2968 (t "additional log fields"
2969 ">>>WARNING: oops ((CODE . 500) (USER . \"guest\") (TOKEN . #u8(1 2 3)) (OP . \":delete\"))\n"
2970 WARNING "oops" 'CODE 500))
2971 (let ((z '()))
2972 (parameterize ((current-log-callback (^[alist] (push! z alist))))
2973 (test* "changing current-log-callback"
2974 '(((SEVERITY . 0) (MESSAGE . "good bye.") (TYPE . "catastrophy"))
2975 ((SEVERITY . 2) (MESSAGE . "everything's wrong!"))
2976 ((SEVERITY . 5) (MESSAGE . "something's wrong")))
2977 (begin
2978 (send-log NOTICE "something's wrong")
2979 (send-log CRITICAL "everything's wrong!")
2980 (send-log EMERGENCY "good bye." 'TYPE "catastrophy")
2981 z))))
2982
2983 (test* "error check (severity)" (test-error <error> #/a severity from/)
2984 (send-log 'INFO "foo"))
2985 (test* "error check (message)" (test-error <error> #/message to be a string/)
2986 (send-log INFO 'baz))
2987 (test* "error check (plist)"
2988 (test-error <error> #/Incomplete field plist: \(foo bar baz\)/)
2989 (send-log INFO "yay" 'foo 'bar 'baz))
2990 ))
2991
2992 ;;-----------------------------------------------------------------------
2993 ;; SCIP prerequisites
2994
2995 (test-section "SRFI-216")
2996
2997 (define-module srfi-216-test
2998 (use gauche.test)
2999 (use srfi.216)
3000 (test-module 'srfi.216)
3001 )
3002
3003 ;;-----------------------------------------------------------------------
3004 ;; Integer sets
3005
3006 (test-section "SRFI-217")
3007
3008 (define-module srfi-217-tests
3009 (use gauche.test)
3010 (use srfi.217)
3011 (test-module 'srfi.217)
3012 (test-include-r7 "include/srfi-217-test")
3013
3014 ;; Here are some tests specifically tailored to check
3015 ;; the corner case of Gauche's implementation
3016 (let ()
3017 (define (t a b uni int dif xor)
3018 (test* "iset set operation (union, intersection, differnce, xor"
3019 (list uni int dif xor)
3020 (let ([a. (list->iset a)]
3021 [b. (list->iset b)])
3022 (list (iset->list (iset-union a. b.))
3023 (iset->list (iset-intersection a. b.))
3024 (iset->list (iset-difference a. b.))
3025 (iset->list (iset-xor a. b.)))))
3026 (test* "iset set operation (union!, intersection!, differnce!, xor!"
3027 (list uni int dif xor)
3028 (let ([a. (list->iset a)]
3029 [b. (list->iset b)])
3030 (list (iset->list (iset-union! (iset-copy a.) b.))
3031 (iset->list (iset-intersection! (iset-copy a.) b.))
3032 (iset->list (iset-difference! (iset-copy a.) b.))
3033 (iset->list (iset-xor! (iset-copy a.) b.)))))
3034 )
3035 (t '(-1 0) '() '(-1 0) '() '(-1 0) '(-1 0))
3036 (t '() '(-1 0) '(-1 0) '() '() '(-1 0))
3037 (t '(0 1 2 3 5 6 7 8) '(3 4 5)
3038 '(0 1 2 3 4 5 6 7 8)
3039 '(3 5)
3040 '(0 1 2 6 7 8)
3041 '(0 1 2 4 6 7 8))
3042 (t '(0 1 2 5 6 7) '(-1 3 4 8)
3043 '(-1 0 1 2 3 4 5 6 7 8)
3044 '()
3045 '(0 1 2 5 6 7)
3046 '(-1 0 1 2 3 4 5 6 7 8))
3047 )
3048 )
3049
3050 ;;-----------------------------------------------------------------------
3051 ;; Generator/accumulator sublibrary
3052
3053 (test-section "SRFI-221")
3054
3055 (define-module srfi-221-tests
3056 (use gauche.test)
3057 (use srfi.221)
3058 (test-module 'srfi.221)
3059 (test-include-r7 "include/srfi-221-test"))
3060
3061 ;;-----------------------------------------------------------------------
3062 ;; Compound objects
3063
3064 (test-section "SRFI-222")
3065
3066 (define-module srfi-222-tests
3067 (use gauche.test)
3068 (use srfi.222)
3069 (test-module 'srfi.222)
3070 (test-include-r7 "include/srfi-222-tests"
3071 (exclude (compounds)))
3072
3073 ;; Gauche object protocol
3074 (test* "hash function of compound" #t
3075 (integer? (default-hash (make-compound 1 2 3))))
3076 (test* "compare function of compound" '(-1 0 1)
3077 (list (compare (make-compound 1 2) (make-compound 1 2 3))
3078 (compare (make-compound 1 2) (make-compound 1 2))
3079 (compare (make-compound 1 2 3) (make-compound 1 2))))
3080 )
3081
3082 ;;-----------------------------------------------------------------------
3083 ;; Dictionaries
3084
3085 (test-section "SRFI-225")
3086
3087 (define-module srfi-225-tests
3088 (use gauche.test)
3089 (use srfi.225)
3090 (test-module 'srfi.225)
3091 )
3092
3093 ;;-----------------------------------------------------------------------
3094 ;; Control Features
3095
3096 (test-section "SRFI-226")
3097
3098 (define-module srfi-226-tests
3099 (use gauche.test)
3100 (use srfi.226)
3101 (test-module 'srfi.226)
3102 )
3103
3104 ;;-----------------------------------------------------------------------
3105 ;; Optional arguments
3106 (test-section "SRFI-227")
3107
3108 (define-module srfi-227-tests
3109 (use gauche.test)
3110 (use srfi.227)
3111 (test-module 'srfi.227)
3112
3113 (define f
3114 (opt*-lambda (x y (z (+ x 1)) (w (* y z)))
3115 (list x y z w)))
3116
3117 (test* "opt*-lambda 0" (test-error) (f))
3118 (test* "opt*-lambda 1" (test-error) (f 10))
3119 (test* "opt*-lambda 2" '(10 20 11 220) (f 10 20))
3120 (test* "opt*-lambda 3" '(10 20 30 600) (f 10 20 30))
3121 (test* "opt*-lambda 4" '(10 20 30 40) (f 10 20 30 40))
3122 (test* "opt*-lambda 5" (test-error) (f 10 20 30 40 50))
3123
3124 (define f+
3125 (opt*-lambda (x (y (+ x 1)) . r)
3126 (list x y r)))
3127
3128 (test* "opt*-lambda rest 0" (test-error) (f+))
3129 (test* "opt*-lambda rest 1" '(10 11 ()) (f+ 10))
3130 (test* "opt*-lambda rest 2" '(10 20 ()) (f+ 10 20))
3131 (test* "opt*-lambda rest 3" '(10 20 (30)) (f+ 10 20 30))
3132 (test* "opt*-lambda rest 3" '(10 20 (30 40))(f+ 10 20 30 40))
3133
3134 (define g
3135 (let ((x -1) (y -2) (z -3))
3136 (opt-lambda (x y (z (+ x 1)) (w (* y z)))
3137 (list x y z w))))
3138
3139 (test* "opt-lambda 0" (test-error) (g))
3140 (test* "opt-lambda 1" (test-error) (g 10))
3141 (test* "opt-lambda 2" '(10 20 0 6) (g 10 20))
3142 (test* "opt-lambda 3" '(10 20 30 6) (g 10 20 30))
3143 (test* "opt-lambda 4" '(10 20 30 40) (g 10 20 30 40))
3144 (test* "opt-lambda 5" (test-error) (g 10 20 30 40 50))
3145
3146 (define g+
3147 (let ((x -1))
3148 (opt-lambda (x (y (+ x 1)) . r)
3149 (list x y r))))
3150
3151 (test* "opt-lambda rest 0" (test-error) (g+))
3152 (test* "opt-lambda rest 1" '(10 0 ()) (g+ 10))
3153 (test* "opt-lambda rest 2" '(10 20 ()) (g+ 10 20))
3154 (test* "opt-lambda rest 3" '(10 20 (30)) (g+ 10 20 30))
3155 (test* "opt-lambda rest 3" '(10 20 (30 40))(g+ 10 20 30 40))
3156
3157 ;; https://github.com/shirok/Gauche/issues/881
3158 (test* "opt-lambda corner case 1" 1 ((opt-lambda (a) a) 1))
3159 (test* "opt*-lambda corner case 2" 2 ((opt*-lambda (a) a) 2))
3160
3161 (test* "let-optionals*" '(1 2 4)
3162 (let ((y -2) (z -3))
3163 (let-optionals* '(1 2)
3164 (x (y (+ x 1)) (z (+ y 2)))
3165 (list x y z))))
3166
3167 (test* "let-optionals" '(1 2 0)
3168 (let ((y -2) (z -3))
3169 (let-optionals '(1 2)
3170 (x (y (+ x 1)) (z (+ y 2)))
3171 (list x y z))))
3172 )
3173
3174 (define-module srfi-227-definitions-tests
3175 (use gauche.test)
3176 (use srfi.227.definitions)
3177 (test-module 'srfi.227.definitions)
3178
3179 (define x 1)
3180 (define y 2)
3181 (define z 3)
3182 (define-optionals* (f x y (z (+ x 1)) (w (* y z)))
3183 (list x y z w))
3184 (define-optionals (g x y (z (+ x 1)) (w (* y z)))
3185 (list x y z w))
3186
3187 (test* "define-optionals 0" (test-error) (f))
3188 (test* "define-optionals 2" '(10 20 11 220) (f 10 20))
3189 (test* "define-optionals 3" '(10 20 30 600) (f 10 20 30))
3190 (test* "define-optionals 4" '(10 20 30 40) (f 10 20 30 40))
3191 (test* "define-optionals* 0" (test-error) (g))
3192 (test* "define-optionals* 2" '(10 20 2 6) (g 10 20))
3193 (test* "define-optionals* 3" '(10 20 30 6) (g 10 20 30))
3194 (test* "define-optionals* 4" '(10 20 30 40) (g 10 20 30 40))
3195 )
3196
3197 ;;-----------------------------------------------------------------------
3198 ;; Composing comparators
3199
3200 (test-section "SRFI-228")
3201
3202 (define-module srfi-228-tests
3203 (use gauche.test)
3204 (use srfi.228)
3205 (test-module 'srfi.228)
3206
3207 (use compat.chibi-test)
3208 (use scheme.base)
3209 (use srfi.132 :only (list-sort))
3210 (chibi-test
3211 (include "include/srfi-228-test.scm")))
3212
3213 ;;-----------------------------------------------------------------------
3214 ;; Tagged procedures
3215
3216 (test-section "SRFI-229")
3217
3218 (define-module srfi-229-tests
3219 (use gauche.test)
3220 (use srfi.229)
3221 (test-module 'srfi.229)
3222 (test-include-r7 "include/srfi-229-tests"))
3223
3224 ;;-----------------------------------------------------------------------
3225 ;; Flexible curried procedures
3226
3227 (test-section "SRFI-232")
3228
3229 (define-module srfi-232-tests
3230 (use gauche.test)
3231 (use srfi.232)
3232 (test-module 'srfi.232)
3233
3234 (use srfi.64)
3235 (include "include/srfi-232-tests"))
3236
3237 ;;-----------------------------------------------------------------------
3238 ;; Topological sort
3239
3240 (test-section "SRFI-234")
3241
3242 (define-module srfi-234-tests
3243 (use gauche.test)
3244 (use srfi.234)
3245 (test-module 'srfi.234)
3246 (test-include-r7 "include/srfi-234-test"))
3247
3248 ;;-----------------------------------------------------------------------
3249 ;; Combinators
3250
3251 (test-section "SRFI-235")
3252
3253 (define-module srfi-235-tests
3254 (use gauche.test)
3255 (use srfi.235)
3256 (test-module 'srfi.235)
3257 (test-include-r7 "include/srfi-235-test"))
3258
3259 ;;-----------------------------------------------------------------------
3260 ;; Evaluating expressiosn in an unspecified order
3261
3262 (test-section "SRFI-236")
3263
3264 (define-module srfi-236-tests
3265 (use gauche.test)
3266 (use srfi.236) ;NB: SRFI-236 is built-in
3267 (test-module 'srfi.236)
3268 (test-include-r7 "include/srfi-236-tests"))
3269
3270
3271 ;;-----------------------------------------------------------------------
3272 ;; Codesets
3273
3274 (test-section "SRFI-238")
3275
3276 (define-module srfi-238-tests
3277 (use gauche.test)
3278 (use srfi.238)
3279 (test-module 'srfi.238)
3280
3281 (test* "codeset?" '(#t #t #f)
3282 (map codeset? '(errno signal foobar)))
3283 (test* "codeset-symbols (errno)" #t
3284 (boolean (memq 'ENOENT (codeset-symbols 'errno))))
3285 (test* "codeset-symbols (signal)" #t
3286 (boolean (memq 'SIGINT (codeset-symbols 'signal))))
3287 )
3288
3289 ;;-----------------------------------------------------------------------
3290 ;; Destructuring lists
3291
3292 (test-section "SRFI-239")
3293
3294 (define-module srfi-239-tests
3295 (use gauche.test)
3296 (use srfi.239)
3297 (test-module 'srfi.239)
3298
3299 (test* "list-case pair" 1
3300 (list-case '(1 2 3) [(h . _) h]))
3301 (test* "list-case pair" '(2 3)
3302 (list-case '(1 2 3) [(_ . t) t]))
3303 (test* "list-case pair vs null" (test-error)
3304 (list-case '(1 2 3) [() 'a]))
3305 (test* "list-case pair vs atom" (test-error)
3306 (list-case '(1 2 3) [v v]))
3307 (test* "list-case atom" 1
3308 (list-case 1 [(h . t) 'a] [() 'b] [v v]))
3309
3310 (define type-of
3311 (^x (list-case x
3312 [(_ . _) 'pair]
3313 [() 'null]
3314 [_ 'atom])))
3315 (test* "list-case type-of" '(atom null pair)
3316 (list (type-of 'a)
3317 (type-of ())
3318 (type-of '(1 2 3))))
3319 )
3320
3321 ;;-----------------------------------------------------------------------
3322 ;; Syntactic monads
3323
3324 (test-section "SRFI-247")
3325
3326 (define-module srfi-247-tests
3327 (use gauche.test)
3328 (use scheme.list :rename ((partition r7rs:partition)))
3329 (use srfi.247)
3330 (test-module 'srfi.247)
3331
3332 (define-syntax import (syntax-rules () ((_ . _) #f)))
3333 (define-syntax assert (syntax-rules (equal?)
3334 [(_ (equal? expected expr))
3335 (test* 'expr expected expr)]))
3336 (include "include/srfi-247-tests")
3337 )
3338
3339 ;;-----------------------------------------------------------------------
3340 ;; SRFI-252 depends on mt-random, and will be tested in ext/srfi.
3341
3342
3343 (test-end)