Recently Written · git

forthlisp

Scheme on a from-scratch Forth on from-scratch machine code: core (host Lisp, Scheme in Forth, R7RS harness)

git clone https://github.com/equwal/forthlisp

Log | Files | Refs


tests/r7rs/r7rs-tests.scm (73439 bytes)

1 ;; -*- coding: utf-8 -*-
2 
3 (import (scheme base) (scheme char) (scheme lazy)
4         (scheme inexact) (scheme complex) (scheme time)
5         (scheme file) (scheme read) (scheme write)
6         (scheme eval) (scheme process-context) (scheme case-lambda)
7         (scheme r5rs)
8         (chibi test)  ; or (srfi 64)
9         )
10 
11 ;; R7RS test suite.  Covers all procedures and syntax in the small
12 ;; language except `delete-file'.  Currently assumes full-unicode
13 ;; support, the full numeric tower and all standard libraries
14 ;; provided.
15 ;;
16 ;; Uses the (chibi test) library which is written in portable R7RS.
17 ;; This is mostly a subset of SRFI-64, providing test-begin, test-end
18 ;; and test, which could be defined as something like:
19 ;;
20 ;;   (define (test-begin . o) #f)
21 ;;
22 ;;   (define (test-end . o) #f)
23 ;;
24 ;;   (define-syntax test
25 ;;     (syntax-rules ()
26 ;;       ((test expected expr)
27 ;;        (let ((res expr))
28 ;;          (cond
29 ;;           ((not (equal? expr expected))
30 ;;            (display "FAIL: ")
31 ;;            (write 'expr)
32 ;;            (display ": expected ")
33 ;;            (write expected)
34 ;;            (display " but got ")
35 ;;            (write res)
36 ;;            (newline)))))))
37 ;;
38 ;; however (chibi test) provides nicer output, timings, and
39 ;; approximate equivalence for floating point numbers.
40 
41 (test-begin "R7RS")
42 
43 (test-begin "4.1 Primitive expression types")
44 
45 (let ()
46   (define x 28)
47   (test 28 x))
48 
49 (test 'a (quote a))
50 (test #(a b c) (quote #(a b c)))
51 (test '(+ 1 2) (quote (+ 1 2)))
52 
53 (test 'a 'a)
54 (test #(a b c) '#(a b c))
55 (test '() '())
56 (test '(+ 1 2) '(+ 1 2))
57 (test '(quote a) '(quote a))
58 (test '(quote a) ''a)
59 
60 (test "abc" '"abc")
61 (test "abc" "abc")
62 (test 145932 '145932)
63 (test 145932 145932)
64 (test #t '#t)
65 (test #t #t)
66 
67 (test 7 (+ 3 4))
68 (test 12 ((if #f + *) 3 4))
69 
70 (test 8 ((lambda (x) (+ x x)) 4))
71 (define reverse-subtract
72   (lambda (x y) (- y x)))
73 (test 3 (reverse-subtract 7 10))
74 (define add4
75   (let ((x 4))
76     (lambda (y) (+ x y))))
77 (test 10 (add4 6))
78 
79 (test '(3 4 5 6) ((lambda x x) 3 4 5 6))
80 (test '(5 6) ((lambda (x y . z) z)
81  3 4 5 6))
82 
83 (test 'yes (if (> 3 2) 'yes 'no))
84 (test 'no (if (> 2 3) 'yes 'no))
85 (test 1 (if (> 3 2)
86     (- 3 2)
87     (+ 3 2)))
88 (let ()
89   (define x 2)
90   (test 3 (+ x 1)))
91 
92 (test-end)
93 
94 (test-begin "4.2 Derived expression types")
95 
96 (test 'greater
97     (cond ((> 3 2) 'greater)
98           ((< 3 2) 'less)))
99 
100 (test 'equal
101     (cond ((> 3 3) 'greater)
102           ((< 3 3) 'less)
103           (else 'equal)))
104 
105 (test 2
106     (cond ((assv 'b '((a 1) (b 2))) => cadr)
107           (else #f)))
108 
109 (test 'composite
110     (case (* 2 3)
111       ((2 3 5 7) 'prime)
112       ((1 4 6 8 9) 'composite)))
113 
114 (test 'c
115     (case (car '(c d))
116       ((a e i o u) 'vowel)
117       ((w y) 'semivowel)
118       (else => (lambda (x) x))))
119 
120 (test '((other . z) (semivowel . y) (other . x)
121         (semivowel . w) (vowel . u))
122     (map (lambda (x)
123            (case x
124              ((a e i o u) => (lambda (w) (cons 'vowel w)))
125              ((w y) (cons 'semivowel x))
126              (else => (lambda (w) (cons 'other w)))))
127          '(z y x w u)))
128 
129 (test #t (and (= 2 2) (> 2 1)))
130 (test #f (and (= 2 2) (< 2 1)))
131 (test '(f g) (and 1 2 'c '(f g)))
132 (test #t (and))
133 
134 (test #t (or (= 2 2) (> 2 1)))
135 (test #t (or (= 2 2) (< 2 1)))
136 (test #f (or #f #f #f))
137 (test '(b c) (or (memq 'b '(a b c))
138     (/ 3 0)))
139 
140 (test 6 (let ((x 2) (y 3))
141   (* x y)))
142 
143 (test 35 (let ((x 2) (y 3))
144   (let ((x 7)
145         (z (+ x y)))
146     (* z x))))
147 
148 (test 70 (let ((x 2) (y 3))
149   (let* ((x 7)
150          (z (+ x y)))
151     (* z x))))
152 
153 (test #t
154     (letrec ((even?
155               (lambda (n)
156                 (if (zero? n)
157                     #t
158                     (odd? (- n 1)))))
159              (odd?
160               (lambda (n)
161                 (if (zero? n)
162                     #f
163                     (even? (- n 1))))))
164       (even? 88)))
165 
166 (test 5
167     (letrec* ((p
168                (lambda (x)
169                  (+ 1 (q (- x 1)))))
170               (q
171                (lambda (y)
172                  (if (zero? y)
173                      0
174                      (+ 1 (p (- y 1))))))
175               (x (p 5))
176               (y x))
177              y))
178 
179 ;; By Jussi Piitulainen <jpiitula@ling.helsinki.fi>
180 ;; and John Cowan <cowan@mercury.ccil.org>:
181 ;; http://lists.scheme-reports.org/pipermail/scheme-reports/2013-December/003876.html
182 (define (means ton)
183   (letrec*
184      ((mean
185         (lambda (f g)
186           (f (/ (sum g ton) n))))
187       (sum
188         (lambda (g ton)
189           (if (null? ton)
190             (+)
191             (if (number? ton)
192                 (g ton)
193                 (+ (sum g (car ton))
194                    (sum g (cdr ton)))))))
195       (n (sum (lambda (x) 1) ton)))
196     (values (mean values values)
197             (mean exp log)
198             (mean / /))))
199 (let*-values (((a b c) (means '(8 5 99 1 22))))
200   (test 27 a)
201   (test 9.728 b)
202   (test 1800/497 c))
203 
204 (let*-values (((root rem) (exact-integer-sqrt 32)))
205   (test 35 (* root rem)))
206 
207 (test '(1073741824 0)
208     (let*-values (((root rem) (exact-integer-sqrt (expt 2 60))))
209       (list root rem)))
210 
211 (test '(1518500249 3000631951)
212     (let*-values (((root rem) (exact-integer-sqrt (expt 2 61))))
213       (list root rem)))
214 
215 (test '(815238614083298888 443242361398135744)
216     (let*-values (((root rem) (exact-integer-sqrt (expt 2 119))))
217       (list root rem)))
218 
219 (test '(1152921504606846976 0)
220     (let*-values (((root rem) (exact-integer-sqrt (expt 2 120))))
221       (list root rem)))
222 
223 (test '(1630477228166597776 1772969445592542976)
224     (let*-values (((root rem) (exact-integer-sqrt (expt 2 121))))
225       (list root rem)))
226 
227 (test '(31622776601683793319 62545769258890964239)
228     (let*-values (((root rem) (exact-integer-sqrt (expt 10 39))))
229       (list root rem)))
230 
231 (let*-values (((root rem) (exact-integer-sqrt (expt 2 140))))
232   (test 0 rem)
233   (test (expt 2 140) (square root)))
234 
235 (test '(x y x y) (let ((a 'a) (b 'b) (x 'x) (y 'y))
236   (let*-values (((a b) (values x y))
237                 ((x y) (values a b)))
238     (list a b x y))))
239 
240 (test 'ok (let-values () 'ok))
241 
242 (test 1 (let ((x 1))
243 	  (let*-values ()
244 	    (define x 2)
245 	    #f)
246 	  x))
247 
248 (let ()
249   (define x 0)
250   (set! x 5)
251   (test 6 (+ x 1)))
252 
253 (test #(0 1 2 3 4) (do ((vec (make-vector 5))
254      (i 0 (+ i 1)))
255     ((= i 5) vec)
256   (vector-set! vec i i)))
257 
258 (test 25 (let ((x '(1 3 5 7 9)))
259   (do ((x x (cdr x))
260        (sum 0 (+ sum (car x))))
261       ((null? x) sum))))
262 
263 (test '((6 1 3) (-5 -2))
264     (let loop ((numbers '(3 -2 1 6 -5))
265                (nonneg '())
266                (neg '()))
267       (cond ((null? numbers) (list nonneg neg))
268             ((>= (car numbers) 0)
269              (loop (cdr numbers)
270                    (cons (car numbers) nonneg)
271                    neg))
272             ((< (car numbers) 0)
273              (loop (cdr numbers)
274                    nonneg
275                    (cons (car numbers) neg))))))
276 
277 (test 3 (force (delay (+ 1 2))))
278 
279 (test '(3 3)  
280     (let ((p (delay (+ 1 2))))
281       (list (force p) (force p))))
282 
283 (define integers
284   (letrec ((next
285             (lambda (n)
286               (delay (cons n (next (+ n 1)))))))
287     (next 0)))
288 (define head
289   (lambda (stream) (car (force stream))))
290 (define tail
291   (lambda (stream) (cdr (force stream))))
292 
293 (test 2 (head (tail (tail integers))))
294 
295 (define (stream-filter p? s)
296   (delay-force
297    (if (null? (force s)) 
298        (delay '())
299        (let ((h (car (force s)))
300              (t (cdr (force s))))
301          (if (p? h)
302              (delay (cons h (stream-filter p? t)))
303              (stream-filter p? t))))))
304 
305 (test 5 (head (tail (tail (stream-filter odd? integers)))))
306 
307 (let ()
308   (define x 5)
309   (define count 0)
310   (define p
311     (delay (begin (set! count (+ count 1))
312                   (if (> count x)
313                       count
314                       (force p)))))
315   (test 6 (force p))
316   (test 6 (begin (set! x 10) (force p))))
317 
318 (test #t (promise? (delay (+ 2 2))))
319 (test #t (promise? (make-promise (+ 2 2))))
320 (test #t
321     (let ((x (delay (+ 2 2))))
322       (force x)
323       (promise? x)))
324 (test #t
325     (let ((x (make-promise (+ 2 2))))
326       (force x)
327       (promise? x)))
328 (test 4 (force (make-promise (+ 2 2))))
329 (test 4 (force (make-promise (make-promise (+ 2 2)))))
330 
331 (define radix
332   (make-parameter
333    10
334    (lambda (x)
335      (if (and (integer? x) (<= 2 x 16))
336          x
337          (error "invalid radix")))))
338 (define (f n) (number->string n (radix)))
339 (test "12" (f 12))
340 (test "1100" (parameterize ((radix 2))
341   (f 12)))
342 (test "12" (f 12))
343 
344 (test '(list 3 4) `(list ,(+ 1 2) 4))
345 (let ((name 'a)) (test '(list a (quote a)) `(list ,name ',name)))
346 (test '(a 3 4 5 6 b) `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b))
347 (test #(10 5 4 16 9 8)
348     `#(10 5 ,(square 2) ,@(map square '(4 3)) 8))
349 (test '(a `(b ,(+ 1 2) ,(foo 4 d) e) f)
350     `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f) )
351 (let ((name1 'x)
352       (name2 'y))
353    (test '(a `(b ,x ,'y d) e) `(a `(b ,,name1 ,',name2 d) e)))
354 (test '(list 3 4) (quasiquote (list (unquote (+ 1 2)) 4)) )
355 (test `(list ,(+ 1 2) 4) (quasiquote (list (unquote (+ 1 2)) 4)))
356 
357 (define any-arity
358   (case-lambda 
359     (() 'zero)
360     ((x) x)
361     ((x y) (cons x y))
362     ((x y z) (list x y z))
363     (args (cons 'many args))))
364 
365 (test 'zero (any-arity))
366 (test 1 (any-arity 1))
367 (test '(1 . 2) (any-arity 1 2))
368 (test '(1 2 3) (any-arity 1 2 3))
369 (test '(many 1 2 3 4) (any-arity 1 2 3 4))
370 
371 (define rest-arity
372   (case-lambda 
373     (() '(zero))
374     ((x) (list 'one x))
375     ((x y) (list 'two x y))
376     ((x y . z) (list 'more x y z))))
377 
378 (test '(zero) (rest-arity))
379 (test '(one 1) (rest-arity 1))
380 (test '(two 1 2) (rest-arity 1 2))
381 (test '(more 1 2 (3)) (rest-arity 1 2 3))
382 
383 (define dead-clause
384   (case-lambda
385     ((x . y) 'many)
386     (() 'none)
387     (foo 'unreachable)))
388 
389 (test 'none (dead-clause))
390 (test 'many (dead-clause 1))
391 (test 'many (dead-clause 1 2))
392 (test 'many (dead-clause 1 2 3))
393 
394 (test-end)
395 
396 (test-begin "4.3 Macros")
397 
398 (test 'now (let-syntax
399                ((when (syntax-rules ()
400                         ((when test stmt1 stmt2 ...)
401                          (if test
402                              (begin stmt1
403                                     stmt2 ...))))))
404              (let ((if #t))
405                (when if (set! if 'now))
406                if)))
407 
408 (test 'outer (let ((x 'outer))
409   (let-syntax ((m (syntax-rules () ((m) x))))
410     (let ((x 'inner))
411       (m)))))
412 
413 (test 7 (letrec-syntax
414   ((my-or (syntax-rules ()
415             ((my-or) #f)
416             ((my-or e) e)
417             ((my-or e1 e2 ...)
418              (let ((temp e1))
419                (if temp
420                    temp
421                    (my-or e2 ...)))))))
422   (let ((x #f)
423         (y 7)
424         (temp 8)
425         (let odd?)
426         (if even?))
427     (my-or x
428            (let temp)
429            (if y)
430            y))))
431 
432 (define-syntax be-like-begin1
433   (syntax-rules ()
434     ((be-like-begin1 name)
435      (define-syntax name
436        (syntax-rules ()
437          ((name expr (... ...))
438           (begin expr (... ...))))))))
439 (be-like-begin1 sequence1)
440 (test 3 (sequence1 0 1 2 3))
441 
442 (define-syntax be-like-begin2
443   (syntax-rules ()
444     ((be-like-begin2 name)
445      (define-syntax name
446        (... (syntax-rules ()
447               ((name expr ...)
448                (begin expr ...))))))))
449 (be-like-begin2 sequence2)
450 (test 4 (sequence2 1 2 3 4))
451 
452 (define-syntax be-like-begin3
453   (syntax-rules ()
454     ((be-like-begin3 name)
455      (define-syntax name
456        (syntax-rules dots ()
457          ((name expr dots)
458           (begin expr dots)))))))
459 (be-like-begin3 sequence3)
460 (test 5 (sequence3 2 3 4 5))
461 
462 ;; ellipsis escape
463 (define-syntax elli-esc-1
464   (syntax-rules ()
465     ((_)
466      '(... ...))
467     ((_ x)
468      '(... (x ...)))
469     ((_ x y)
470      '(... (... x y)))))
471 
472 (test '... (elli-esc-1))
473 (test '(100 ...) (elli-esc-1 100))
474 (test '(... 100 200) (elli-esc-1 100 200))
475 
476 ;; Syntax pattern with ellipsis in middle of proper list.
477 (define-syntax part-2
478   (syntax-rules ()
479     ((_ a b (m n) ... x y)
480      (vector (list a b) (list m ...) (list n ...) (list x y)))
481     ((_ . rest) 'error)))
482 (test '#((10 43) (31 41 51) (32 42 52) (63 77))
483     (part-2 10 (+ 21 22) (31 32) (41 42) (51 52) (+ 61 2) 77))
484 ;; Syntax pattern with ellipsis in middle of improper list.
485 (define-syntax part-2x
486   (syntax-rules ()
487     ((_ (a b (m n) ... x y . rest))
488      (vector (list a b) (list m ...) (list n ...) (list x y)
489              (cons "rest:" 'rest)))
490     ((_ . rest) 'error)))
491 (test '#((10 43) (31 41 51) (32 42 52) (63 77) ("rest:"))
492     (part-2x (10 (+ 21 22) (31 32) (41 42) (51 52) (+ 61 2) 77)))
493 (test '#((10 43) (31 41 51) (32 42 52) (63 77) ("rest:" . "tail"))
494     (part-2x (10 (+ 21 22) (31 32) (41 42) (51 52) (+ 61 2) 77 . "tail")))
495 
496 ;; underscore
497 (define-syntax underscore
498   (syntax-rules ()
499     ((foo _) '_)))
500 (test '_ (underscore foo))
501 
502 (let ()
503   (define-syntax underscore2
504     (syntax-rules ()
505       ((underscore2 (a _) ...) 42)))
506   (test 42 (underscore2 (1 2))))
507 
508 (define-syntax count-to-2
509   (syntax-rules ()
510     ((_) 0)
511     ((_ _) 1)
512     ((_ _ _) 2)
513     ((_ . _) 'many)))
514 (test '(2 0 many)
515     (list (count-to-2 a b) (count-to-2) (count-to-2 a b c d)))
516 
517 (define-syntax count-to-2_
518   (syntax-rules (_)
519     ((_) 0)
520     ((_ _) 1)
521     ((_ _ _) 2)
522     ((x . y) 'fail)))
523 (test '(2 0 fail fail)
524     (list (count-to-2_ _ _) (count-to-2_)
525           (count-to-2_ a b) (count-to-2_ a b c d)))
526 
527 (define-syntax jabberwocky
528   (syntax-rules ()
529     ((_ hatter)
530      (begin
531        (define march-hare 42)
532        (define-syntax hatter
533          (syntax-rules ()
534            ((_) march-hare)))))))
535 (jabberwocky mad-hatter)
536 (test 42 (mad-hatter))
537 
538 (test 'ok (let ((=> #f)) (cond (#t => 'ok))))
539 
540 (let ()
541   (define x 1)
542   (let-syntax ()
543     (define x 2)
544     #f)
545   (test 1 x))
546 
547 (let ()
548  (define-syntax foo
549    (syntax-rules ()
550      ((foo bar y)
551       (define-syntax bar
552         (syntax-rules ()
553           ((bar x) 'y))))))
554  (foo bar x)
555  (test 'x (bar 1)))
556 
557 (begin
558   (define-syntax ffoo
559     (syntax-rules ()
560       ((ffoo ff)
561        (begin
562          (define (ff x)
563            (gg x))
564          (define (gg x)
565            (* x x))))))
566   (ffoo ff)
567   (test 100 (ff 10)))
568 
569 (let-syntax ((vector-lit
570                (syntax-rules ()
571                  ((vector-lit)
572                   '#(b)))))
573   (test '#(b) (vector-lit)))
574 
575 (let ()
576   ;; forward hygienic refs
577   (define-syntax foo399
578     (syntax-rules () ((foo399) (bar399))))
579   (define (quux399)
580     (foo399))
581   (define (bar399)
582     42)
583   (test 42 (quux399)))
584 
585 (let-syntax
586     ((m (syntax-rules ()
587           ((m x) (let-syntax
588                      ((n (syntax-rules (k)
589                            ((n x) 'bound-identifier=?)
590                            ((n y) 'free-identifier=?))))
591                    (n z))))))
592   (test 'bound-identifier=? (m k)))
593 
594 ;; literal has priority to ellipsis (R7RS 4.3.2)
595 (let ()
596   (define-syntax elli-lit-1
597     (syntax-rules ... (...)
598       ((_ x)
599        '(x ...))))
600   (test '(100 ...) (elli-lit-1 100)))
601 
602 ;; bad ellipsis
603 #|
604 (test 'error
605       (guard (exn (else 'error))
606         (eval
607          '(define-syntax bad-elli-1
608             (syntax-rules ()
609               ((_ ... x)
610                '(... x))))
611          (interaction-environment))))
612 
613 (test 'error
614       (guard (exn (else 'error))
615         (eval
616          '(define-syntax bad-elli-2
617             (syntax-rules ()
618               ((_ (... x))
619                '(... x))))
620          (interaction-environment))))
621 |#
622 
623 (test-end)
624 
625 (test-begin "5 Program structure")
626 
627 (define add3
628   (lambda (x) (+ x 3)))
629 (test 6 (add3 3))
630 (define first car)
631 (test 1 (first '(1 2)))
632 
633 (test 45 (let ((x 5))
634   (define foo (lambda (y) (bar x y)))
635   (define bar (lambda (a b) (+ (* a b) a)))
636   (foo (+ x 3))))
637 
638 (test 'ok
639     (let ()
640       (define-values () (values))
641       'ok))
642 (test 1
643     (let ()
644       (define-values (x) (values 1))
645       x))
646 (test 3
647     (let ()
648       (define-values x (values 1 2))
649       (apply + x)))
650 (test 3
651     (let ()
652       (define-values (x y) (values 1 2))
653       (+ x y)))
654 (test 6
655     (let ()
656       (define-values (x y z) (values 1 2 3))
657       (+ x y z)))
658 (test 10
659     (let ()
660       (define-values (x y . z) (values 1 2 3 4))
661       (+ x y (car z) (cadr z))))
662 
663 (test '(2 1) (let ((x 1) (y 2))
664   (define-syntax swap!
665     (syntax-rules ()
666       ((swap! a b)
667        (let ((tmp a))
668          (set! a b)
669          (set! b tmp)))))
670   (swap! x y)
671   (list x y)))
672 
673 ;; Records
674 
675 (define-record-type <pare>
676   (kons x y)
677   pare?
678   (x kar set-kar!)
679   (y kdr))
680 
681 (test #t (pare? (kons 1 2)))
682 (test #f (pare? (cons 1 2)))
683 (test 1 (kar (kons 1 2)))
684 (test 2 (kdr (kons 1 2)))
685 (test 3 (let ((k (kons 1 2)))
686           (set-kar! k 3)
687           (kar k)))
688 
689 (test-end)
690 
691 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
692 ;; 6 Standard Procedures
693 
694 (test-begin "6.1 Equivalence Predicates")
695 
696 (test #t (eqv? 'a 'a))
697 (test #f (eqv? 'a 'b))
698 (test #t (eqv? 2 2))
699 (test #t (eqv? '() '()))
700 (test #t (eqv? 100000000 100000000))
701 (test #f (eqv? (cons 1 2) (cons 1 2)))
702 (test #f (eqv? (lambda () 1)
703                (lambda () 2)))
704 (test #f (eqv? #f 'nil))
705 
706 (define gen-counter
707   (lambda ()
708     (let ((n 0))
709       (lambda () (set! n (+ n 1)) n))))
710 (test #t
711     (let ((g (gen-counter)))
712       (eqv? g g)))
713 (test #f (eqv? (gen-counter) (gen-counter)))
714 (define gen-loser
715   (lambda ()
716     (let ((n 0))
717       (lambda () (set! n (+ n 1)) 27))))
718 (test #t (let ((g (gen-loser)))
719   (eqv? g g)))
720 
721 (test #f
722 (letrec ((f (lambda () (if (eqv? f g) 'f 'both)))
723          (g (lambda () (if (eqv? f g) 'g 'both))))
724    (eqv? f g)))
725 
726 (test #t
727     (let ((x '(a)))
728       (eqv? x x)))
729 
730 (test #t (eq? 'a 'a))
731 (test #f (eq? (list 'a) (list 'a)))
732 (test #t (eq? '() '()))
733 (test #t
734     (let ((x '(a)))
735       (eq? x x)))
736 (test #t
737     (let ((x '#()))
738       (eq? x x)))
739 (test #t
740     (let ((p (lambda (x) x)))
741       (eq? p p)))
742 
743 (test #t (equal? 'a 'a))
744 (test #t (equal? '(a) '(a)))
745 (test #t (equal? '(a (b) c)
746                  '(a (b) c)))
747 (test #t (equal? "abc" "abc"))
748 (test #t (equal? 2 2))
749 (test #t (equal? (make-vector 5 'a)
750                  (make-vector 5 'a)))
751 
752 (test-end)
753 
754 (test-begin "6.2 Numbers")
755 
756 (test #t (complex? 3+4i))
757 (test #t (complex? 3))
758 (test #t (real? 3))
759 (test #t (real? -2.5+0i))
760 (test #f (real? -2.5+0.0i))
761 (test #t (real? #e1e10))
762 (test #t (real? +inf.0))
763 (test #f (rational? -inf.0))
764 (test #f (rational? +nan.0))
765 (test #t (rational? 9007199254740991.0))
766 (test #t (rational? 9007199254740992.0))
767 (test #t (rational? 1.7976931348623157e308))
768 (test #t (rational? 6/10))
769 (test #t (rational? 6/3))
770 (test #t (integer? 3+0i))
771 (test #t (integer? 3.0))
772 (test #t (integer? 8/4))
773 
774 (test #f (exact? 3.0))
775 (test #t (exact? #e3.0))
776 (test #t (inexact? 3.))
777 
778 (test #t (exact-integer? 32))
779 (test #f (exact-integer? 32.0))
780 (test #f (exact-integer? 32/5))
781 
782 (test #t (finite? 3))
783 (test #f (finite? +inf.0))
784 (test #f (finite? 3.0+inf.0i))
785 
786 (test #f (infinite? 3))
787 (test #t (infinite? +inf.0))
788 (test #f (infinite? +nan.0))
789 (test #t (infinite? 3.0+inf.0i))
790 
791 (test #t (nan? +nan.0))
792 (test #f (nan? 32))
793 (test #t (nan? +nan.0+5.0i))
794 (test #t (nan? 1.0+nan.0i))
795 (test #t (nan? +nan.0i))
796 (test #t (nan? +nan.0+nan.0i))
797 (test #t (nan? +nan.0+inf.0i))
798 (test #t (nan? +inf.0+nan.0i))
799 (test #f (nan? 1+2i))
800 
801 (test #t (= 1 1.0 1.0+0.0i))
802 (test #f (= 1.0 1.0+1.0i))
803 (test #t (< 1 2 3))
804 (test #f (< 1 1 2))
805 (test #t (> 3.0 2.0 1.0))
806 (test #f (> -3.0 2.0 1.0))
807 (test #t (<= 1 1 2))
808 (test #f (<= 1 2 1))
809 (test #t (>= 2 1 1))
810 (test #f (>= 1 2 1))
811 (test #f (< +nan.0 0))
812 (test #f (> +nan.0 0))
813 (test #f (< +nan.0 0.0))
814 (test #f (> +nan.0 0.0))
815 (test '(#t #f) (list (<= 1 1 2) (<= 2 1 3)))
816 (test #f (= 9007199254740992.0 9007199254740993))
817 
818 ;; From R7RS 6.2.6 Numerical operations:
819 ;;
820 ;; These predicates are required to be transitive.
821 ;;
822 ;; _Note:_ The traditional implementations of these predicates in
823 ;; Lisp-like languages, which involve converting all arguments to inexact
824 ;; numbers if any argument is inexact, are not transitive.
825 
826 ;; Example from Alan Bawden
827 (let ((a (- (expt 2 1000) 1))
828       (b (inexact (expt 2 1000))) ; assuming > single-float-epsilon
829       (c (+ (expt 2 1000) 1)))
830   (test #t (if (and (= a b) (= b c))
831                (= a c)
832                #t)))
833 
834 ;; From CLtL 12.3. Comparisons on Numbers:
835 ;;
836 ;;  Let _a_ be the result of (/ 10.0 single-float-epsilon), and let
837 ;;  _j_ be the result of (floor a). ..., all of (<= a j), (< j (+ j
838 ;;  1)), and (<= (+ j 1) a) would be true; transitivity would then
839 ;;  imply that (< a a) ought to be true ...
840 
841 ;; Transliteration from Jussi Piitulainen
842 (define single-float-epsilon
843   (do ((eps 1.0 (* eps 2.0)))
844       ((= eps (+ eps 1.0)) eps)))
845 
846 (let* ((a (/ 10.0 single-float-epsilon))
847        (j (exact a)))
848   (test #t (if (and (<= a j) (< j (+ j 1)))
849                (not (<= (+ j 1) a))
850                #t)))
851 
852 (test #t (zero? 0))
853 (test #t (zero? 0.0))
854 (test #t (zero? 0.0+0.0i))
855 (test #f (zero? 1))
856 (test #f (zero? -1))
857 
858 (test #f (positive? 0))
859 (test #f (positive? 0.0))
860 (test #t (positive? 1))
861 (test #t (positive? 1.0))
862 (test #f (positive? -1))
863 (test #f (positive? -1.0))
864 (test #t (positive? +inf.0))
865 (test #f (positive? -inf.0))
866 (test #f (positive? +nan.0))
867 
868 (test #f (negative? 0))
869 (test #f (negative? 0.0))
870 (test #f (negative? 1))
871 (test #f (negative? 1.0))
872 (test #t (negative? -1))
873 (test #t (negative? -1.0))
874 (test #f (negative? +inf.0))
875 (test #t (negative? -inf.0))
876 (test #f (negative? +nan.0))
877 
878 (test #f (odd? 0))
879 (test #t (odd? 1))
880 (test #t (odd? -1))
881 (test #f (odd? 102))
882 
883 (test #t (even? 0))
884 (test #f (even? 1))
885 (test #t (even? -2))
886 (test #t (even? 102))
887 
888 (test 3 (max 3))
889 (test 4 (max 3 4))
890 (test 4.0 (max 3.9 4))
891 (test 5.0 (max 5 3.9 4))
892 (test +inf.0 (max 100 +inf.0))
893 (test 3 (min 3))
894 (test 3 (min 3 4))
895 (test 3.0 (min 3 3.1))
896 (test -inf.0 (min -inf.0 -100))
897 
898 (test 7 (+ 3 4))
899 (test 3 (+ 3))
900 (test 0 (+))
901 (test 4 (* 4))
902 (test 1 (*))
903 
904 (test -1 (- 3 4))
905 (test -6 (- 3 4 5))
906 (test -3 (- 3))
907 (test -3/2 (- 3/2))
908 (test -3/2-i (- 3/2+i))
909 (test 3/20 (/ 3 4 5))
910 (test 1/3 (/ 3))
911 
912 (test 1073741824 (/ -1073741824 -1))
913 (test 1073741824 (quotient -1073741824 -1))
914 (test 0 (remainder -1073741824 -1))
915 (test 4611686018427387904 (/ -4611686018427387904 -1))
916 (test 4611686018427387904 (quotient -4611686018427387904 -1))
917 (test 0 (remainder -4611686018427387904 -1))
918 
919 (test 7 (abs -7))
920 (test 7 (abs 7))
921 
922 (test-values (values 2 1) (floor/ 5 2))
923 (test-values (values -3 1) (floor/ -5 2))
924 (test-values (values -3 -1) (floor/ 5 -2))
925 (test-values (values 2 -1) (floor/ -5 -2))
926 (test-values (values 2 1) (truncate/ 5 2))
927 (test-values (values -2 -1) (truncate/ -5 2))
928 (test-values (values -2 1) (truncate/ 5 -2))
929 (test-values (values 2 -1) (truncate/ -5 -2))
930 (test-values (values 2.0 -1.0) (truncate/ -5.0 -2))
931 
932 (test 1 (modulo 13 4))
933 (test 1 (remainder 13 4))
934 
935 (test 3 (modulo -13 4))
936 (test -1 (remainder -13 4))
937 
938 (test -3 (modulo 13 -4))
939 (test 1 (remainder 13 -4))
940 
941 (test -1 (modulo -13 -4))
942 (test -1 (remainder -13 -4))
943 
944 (test -1.0 (remainder -13 -4.0))
945 
946 (test 4 (gcd 32 -36))
947 (test 0 (gcd))
948 (test 288 (lcm 32 -36))
949 (test 288.0 (lcm 32.0 -36))
950 (test 1 (lcm))
951 
952 (test 3 (numerator (/ 6 4)))
953 (test 2 (denominator (/ 6 4)))
954 (test 2.0 (denominator (inexact (/ 6 4))))
955 (test 11.0 (numerator 5.5))
956 (test 2.0 (denominator 5.5))
957 (test 5.0 (numerator 5.0))
958 (test 1.0 (denominator 5.0))
959 
960 (test -5.0 (floor -4.3))
961 (test -4.0 (ceiling -4.3))
962 (test -4.0 (truncate -4.3))
963 (test -4.0 (round -4.3))
964 
965 (test 3.0 (floor 3.5))
966 (test 4.0 (ceiling 3.5))
967 (test 3.0 (truncate 3.5))
968 (test 4.0 (round 3.5))
969 
970 (test 4 (round 7/2))
971 (test 7 (round 7))
972 (test 1 (round 7/10))
973 (test -4 (round -7/2))
974 (test -7 (round -7))
975 (test -1 (round -7/10))
976 
977 (test 1/3 (rationalize (exact .3) 1/10))
978 (test #i1/3 (rationalize .3 1/10))
979 
980 (test 1.0 (inexact (exp 0))) ;; may return exact number
981 (test 20.0855369231877 (exp 3))
982 
983 (test 0.0 (inexact (log 1))) ;; may return exact number
984 (test 1.0 (log (exp 1)))
985 (test 42.0 (log (exp 42)))
986 (test 2.0 (log 100 10))
987 (test 12.0 (log 4096 2))
988 
989 (test 0.0 (inexact (sin 0))) ;; may return exact number
990 (test 1.0 (sin 1.5707963267949))
991 (test 1.0 (inexact (cos 0))) ;; may return exact number
992 (test -1.0 (cos 3.14159265358979))
993 (test 0.0 (inexact (tan 0))) ;; may return exact number
994 (test 1.5574077246549 (tan 1))
995 
996 (test 0.0 (inexact (asin 0))) ;; may return exact number
997 (test 1.5707963267949 (asin 1))
998 (test 0.0 (inexact (acos 1))) ;; may return exact number
999 (test 3.14159265358979 (acos -1))
1000 
1001 ;; (test 0.0-0.0i (asin 0+0.0i))
1002 ;; (test 1.5707963267948966+0.0i (acos 0+0.0i))
1003 
1004 (test 0.0 (atan 0.0 1.0))
1005 (test -0.0 (atan -0.0 1.0))
1006 (test 0.785398163397448 (atan 1.0 1.0))
1007 (test 1.5707963267949 (atan 1.0 0.0))
1008 (test 2.35619449019234 (atan 1.0 -1.0))
1009 (test 3.14159265358979 (atan 0.0 -1.0))
1010 (test -3.14159265358979 (atan -0.0 -1.0)) ;
1011 (test -2.35619449019234 (atan -1.0 -1.0))
1012 (test -1.5707963267949 (atan -1.0 0.0))
1013 (test -0.785398163397448 (atan -1.0 1.0))
1014 ;; (test undefined (atan 0.0 0.0))
1015 
1016 (test 1764 (square 42))
1017 (test 4 (square 2))
1018 
1019 (test 3.0 (inexact (sqrt 9)))
1020 (test 1.4142135623731 (sqrt 2))
1021 (test 0.0+1.0i (inexact (sqrt -1)))
1022 (test 0.0+1.0i (sqrt -1.0-0.0i))
1023 
1024 (test '(2 0) (call-with-values (lambda () (exact-integer-sqrt 4)) list))
1025 (test '(2 1) (call-with-values (lambda () (exact-integer-sqrt 5)) list))
1026 
1027 (test 27 (expt 3 3))
1028 (test 1 (expt 0 0))
1029 (test 0 (expt 0 1))
1030 (test 1.0 (expt 0.0 0))
1031 (test 0.0 (expt 0 1.0))
1032 (test 1/1024 (expt 2 -10))
1033 (test 1/1180591620717411303424 (expt 2 -70))
1034 
1035 (test 1+2i (make-rectangular 1 2))
1036 
1037 (test 0.54030230586814+0.841470984807897i (make-polar 1 1))
1038 
1039 (test 1 (real-part 1+2i))
1040 
1041 (test 2 (imag-part 1+2i))
1042 
1043 (test 2.23606797749979 (magnitude 1+2i))
1044 
1045 (test 1.10714871779409 (angle 1+2i))
1046 
1047 (test 1.0 (inexact 1))
1048 (test #t (inexact? (inexact 1)))
1049 (test 1 (exact 1.0))
1050 (test #t (exact? (exact 1.0)))
1051 
1052 (test 100 (string->number "100"))
1053 (test 256 (string->number "100" 16))
1054 (test 100.0 (string->number "1e2"))
1055 (test #f (string->number "1 2"))
1056 
1057 (test-end)
1058 
1059 (test-begin "6.3 Booleans")
1060 
1061 (test #t #t)
1062 (test #f #f)
1063 (test #f '#f)
1064 
1065 (test #f (not #t))
1066 (test #f (not 3))
1067 (test #f (not (list 3)))
1068 (test #t (not #f))
1069 (test #f (not '()))
1070 (test #f (not (list)))
1071 (test #f (not 'nil))
1072 
1073 (test #t (boolean? #f))
1074 (test #f (boolean? 0))
1075 (test #f (boolean? '()))
1076 
1077 (test #t (boolean=? #t #t))
1078 (test #t (boolean=? #f #f))
1079 (test #f (boolean=? #t #f))
1080 (test #t (boolean=? #f #f #f))
1081 (test #f (boolean=? #t #t #f))
1082 
1083 (test-end)
1084 
1085 (test-begin "6.4 Lists")
1086 
1087 (let* ((x (list 'a 'b 'c))
1088        (y x))
1089   (test '(a b c) (values y))
1090   (test #t (list? y))
1091   (set-cdr! x 4)
1092   (test '(a . 4) (values x))
1093   (test #t (eqv? x y))
1094   (test #f (list? y))
1095   (set-cdr! x x)
1096   (test #f (list? x)))
1097 
1098 (test #t (pair? '(a . b)))
1099 (test #t (pair? '(a b c)))
1100 (test #f (pair? '()))
1101 (test #f (pair? '#(a b)))
1102 
1103 (test '(a) (cons 'a '()))
1104 (test '((a) b c d) (cons '(a) '(b c d)))
1105 (test '("a" b c) (cons "a" '(b c)))
1106 (test '(a . 3) (cons 'a 3))
1107 (test '((a b) . c) (cons '(a b) 'c))
1108 
1109 (test 'a (car '(a b c)))
1110 (test '(a) (car '((a) b c d)))
1111 (test 1 (car '(1 . 2)))
1112 
1113 (test '(b c d) (cdr '((a) b c d)))
1114 (test 2 (cdr '(1 . 2)))
1115 (define (g) '(constant-list))
1116 
1117 (test #t (list? '(a b c)))
1118 (test #t (list? '()))
1119 (test #f (list? '(a . b)))
1120 (test #f (let ((x (list 'a))) (set-cdr! x x) (list? x)))
1121 
1122 (test '(3 3) (make-list 2 3))
1123 
1124 (test '(a 7 c) (list 'a (+ 3 4) 'c))
1125 (test '() (list))
1126 
1127 (test 3 (length '(a b c)))
1128 (test 3 (length '(a (b) (c d e))))
1129 (test 0 (length '()))
1130 
1131 (test '(x y) (append '(x) '(y)))
1132 (test '(a b c d) (append '(a) '(b c d)))
1133 (test '(a (b) (c)) (append '(a (b)) '((c))))
1134 
1135 (test '(a b c . d) (append '(a b) '(c . d)))
1136 (test 'a (append '() 'a))
1137 
1138 (test '(c b a) (reverse '(a b c)))
1139 (test '((e (f)) d (b c) a) (reverse '(a (b c) d (e (f)))))
1140 
1141 (test '(d e) (list-tail '(a b c d e) 3))
1142 
1143 (test 'c (list-ref '(a b c d) 2))
1144 (test 'c (list-ref '(a b c d)
1145           (exact (round 1.8))))
1146 
1147 (test '(0 ("Sue" "Sue") "Anna")
1148     (let ((lst (list 0 '(2 2 2 2) "Anna")))
1149       (list-set! lst 1 '("Sue" "Sue"))
1150       lst))
1151 
1152 (test '(a b c) (memq 'a '(a b c)))
1153 (test '(b c) (memq 'b '(a b c)))
1154 (test #f (memq 'a '(b c d)))
1155 (test #f (memq (list 'a) '(b (a) c)))
1156 (test '((a) c) (member (list 'a) '(b (a) c)))
1157 (test '("b" "c") (member "B" '("a" "b" "c") string-ci=?))
1158 (test '(101 102) (memv 101 '(100 101 102)))
1159 
1160 (let ()
1161   (define e '((a 1) (b 2) (c 3)))
1162   (test '(a 1) (assq 'a e))
1163   (test '(b 2) (assq 'b e))
1164   (test #f (assq 'd e)))
1165 
1166 (test #f (assq (list 'a) '(((a)) ((b)) ((c)))))
1167 (test '((a)) (assoc (list 'a) '(((a)) ((b)) ((c)))))
1168 (test '(2 4) (assoc 2.0 '((1 1) (2 4) (3 9)) =))
1169 (test '(5 7) (assv 5 '((2 3) (5 7) (11 13))))
1170 
1171 (test '(1 2 3) (list-copy '(1 2 3)))
1172 (test "foo" (list-copy "foo"))
1173 (test '() (list-copy '()))
1174 (test '(3 . 4) (list-copy '(3 . 4)))
1175 (test '(6 7 8 . 9) (list-copy '(6 7 8 . 9)))
1176 (let* ((l1 '((a b) (c d) e))
1177        (l2 (list-copy l1)))
1178   (test l2 '((a b) (c d) e))
1179   (test #t (eq? (car l1) (car l2)))
1180   (test #t (eq? (cadr l1) (cadr l2)))
1181   (test #f (eq? (cdr l1) (cdr l2)))
1182   (test #f (eq? (cddr l1) (cddr l2))))
1183 
1184 (test-end)
1185 
1186 (test-begin "6.5 Symbols")
1187 
1188 (test #t (symbol? 'foo))
1189 (test #t (symbol? (car '(a b))))
1190 (test #f (symbol? "bar"))
1191 (test #t (symbol? 'nil))
1192 (test #f (symbol? '()))
1193 (test #f (symbol? #f))
1194 
1195 (test #t (symbol=? 'a 'a))
1196 (test #f (symbol=? 'a 'A))
1197 (test #t (symbol=? 'a 'a 'a))
1198 (test #f (symbol=? 'a 'a 'A))
1199 
1200 (test "flying-fish"     
1201 (symbol->string 'flying-fish))
1202 (test "Martin" (symbol->string 'Martin))
1203 (test "Malvina" (symbol->string (string->symbol "Malvina")))
1204 
1205 (test 'mISSISSIppi (string->symbol "mISSISSIppi"))
1206 (test #t (eq? 'bitBlt (string->symbol "bitBlt")))
1207 (test #t (eq? 'LollyPop (string->symbol (symbol->string 'LollyPop))))
1208 (test #t (string=? "K. Harper, M.D."
1209                    (symbol->string (string->symbol "K. Harper, M.D."))))
1210 
1211 (test-end)
1212 
1213 (test-begin "6.6 Characters")
1214 
1215 (test #t (char? #\a))
1216 (test #f (char? "a"))
1217 (test #f (char? 'a))
1218 (test #f (char? 0))
1219 
1220 (test #t (char=? #\a #\a #\a))
1221 (test #f (char=? #\a #\A))
1222 (test #t (char<? #\a #\b #\c))
1223 (test #f (char<? #\a #\a))
1224 (test #f (char<? #\b #\a))
1225 (test #f (char>? #\a #\b))
1226 (test #f (char>? #\a #\a))
1227 (test #t (char>? #\c #\b #\a))
1228 (test #t (char<=? #\a #\b #\b))
1229 (test #t (char<=? #\a #\a))
1230 (test #f (char<=? #\b #\a))
1231 (test #f (char>=? #\a #\b))
1232 (test #t (char>=? #\a #\a))
1233 (test #t (char>=? #\b #\b #\a))
1234 
1235 (test #t (char-ci=? #\a #\a))
1236 (test #t (char-ci=? #\a #\A #\a))
1237 (test #f (char-ci=? #\a #\b))
1238 (test #t (char-ci<? #\a #\B #\c))
1239 (test #f (char-ci<? #\A #\a))
1240 (test #f (char-ci<? #\b #\A))
1241 (test #f (char-ci>? #\A #\b))
1242 (test #f (char-ci>? #\a #\A))
1243 (test #t (char-ci>? #\c #\B #\a))
1244 (test #t (char-ci<=? #\a #\B #\b))
1245 (test #t (char-ci<=? #\A #\a))
1246 (test #f (char-ci<=? #\b #\A))
1247 (test #f (char-ci>=? #\A #\b))
1248 (test #t (char-ci>=? #\a #\A))
1249 (test #t (char-ci>=? #\b #\B #\a))
1250 
1251 (test #t (char-alphabetic? #\a))
1252 (test #f (char-alphabetic? #\space))
1253 (test #t (char-numeric? #\0))
1254 (test #f (char-numeric? #\.))
1255 (test #f (char-numeric? #\a))
1256 (test #t (char-whitespace? #\space))
1257 (test #t (char-whitespace? #\tab))
1258 (test #t (char-whitespace? #\newline))
1259 (test #f (char-whitespace? #\_))
1260 (test #f (char-whitespace? #\a))
1261 (test #t (char-upper-case? #\A))
1262 (test #f (char-upper-case? #\a))
1263 (test #f (char-upper-case? #\3))
1264 (test #t (char-lower-case? #\a))
1265 (test #f (char-lower-case? #\A))
1266 (test #f (char-lower-case? #\3))
1267 
1268 (test #t (char-alphabetic? #\Λ))
1269 (test #f (char-alphabetic? #\x0E50))
1270 (test #t (char-upper-case? #\Λ))
1271 (test #f (char-upper-case? #\λ))
1272 (test #f (char-lower-case? #\Λ))
1273 (test #t (char-lower-case? #\λ))
1274 (test #f (char-numeric? #\Λ))
1275 (test #t (char-numeric? #\x0E50))
1276 (test #t (char-whitespace? #\x1680))
1277 
1278 (test 0 (digit-value #\0))
1279 (test 3 (digit-value #\3))
1280 (test 9 (digit-value #\9))
1281 (test 4 (digit-value #\x0664))
1282 (test 0 (digit-value #\x0AE6))
1283 (test #f (digit-value #\.))
1284 (test #f (digit-value #\-))
1285 
1286 (test 97 (char->integer #\a))
1287 (test #\a (integer->char 97))
1288 
1289 (test #\A (char-upcase #\a))
1290 (test #\A (char-upcase #\A))
1291 (test #\a (char-downcase #\a))
1292 (test #\a (char-downcase #\A))
1293 (test #\a (char-foldcase #\a))
1294 (test #\a (char-foldcase #\A))
1295 
1296 (test #\Λ (char-upcase #\λ))
1297 (test #\Λ (char-upcase #\Λ))
1298 (test #\λ (char-downcase #\λ))
1299 (test #\λ (char-downcase #\Λ))
1300 (test #\λ (char-foldcase #\λ))
1301 (test #\λ (char-foldcase #\Λ))
1302 
1303 (test-end)
1304 
1305 (test-begin "6.7 Strings")
1306 
1307 (test #t (string? ""))
1308 (test #t (string? " "))
1309 (test #f (string? 'a))
1310 (test #f (string? #\a))
1311 
1312 (test 3 (string-length (make-string 3)))
1313 (test "---" (make-string 3 #\-))
1314 
1315 (test "" (string))
1316 (test "---" (string #\- #\- #\-))
1317 (test "kitten" (string #\k #\i #\t #\t #\e #\n))
1318 
1319 (test 0 (string-length ""))
1320 (test 1 (string-length "a"))
1321 (test 3 (string-length "abc"))
1322 
1323 (test #\a (string-ref "abc" 0))
1324 (test #\b (string-ref "abc" 1))
1325 (test #\c (string-ref "abc" 2))
1326 
1327 (test "a-c" (let ((str (string #\a #\b #\c))) (string-set! str 1 #\-) str))
1328 
1329 (test (string #\a #\x1F700 #\c)
1330     (let ((s (string #\a #\b #\c)))
1331       (string-set! s 1 #\x1F700)
1332       s))
1333 
1334 (test #t (string=? "" ""))
1335 (test #t (string=? "abc" "abc" "abc"))
1336 (test #f (string=? "" "abc"))
1337 (test #f (string=? "abc" "aBc"))
1338 
1339 (test #f (string<? "" ""))
1340 (test #f (string<? "abc" "abc"))
1341 (test #t (string<? "abc" "abcd" "acd"))
1342 (test #f (string<? "abcd" "abc"))
1343 (test #t (string<? "abc" "bbc"))
1344 
1345 (test #f (string>? "" ""))
1346 (test #f (string>? "abc" "abc"))
1347 (test #f (string>? "abc" "abcd"))
1348 (test #t (string>? "acd" "abcd" "abc"))
1349 (test #f (string>? "abc" "bbc"))
1350 
1351 (test #t (string<=? "" ""))
1352 (test #t (string<=? "abc" "abc"))
1353 (test #t (string<=? "abc" "abcd" "abcd"))
1354 (test #f (string<=? "abcd" "abc"))
1355 (test #t (string<=? "abc" "bbc"))
1356 
1357 (test #t (string>=? "" ""))
1358 (test #t (string>=? "abc" "abc"))
1359 (test #f (string>=? "abc" "abcd"))
1360 (test #t (string>=? "abcd" "abcd" "abc"))
1361 (test #f (string>=? "abc" "bbc"))
1362 
1363 (test #t (string-ci=? "" ""))
1364 (test #t (string-ci=? "abc" "abc"))
1365 (test #f (string-ci=? "" "abc"))
1366 (test #t (string-ci=? "abc" "aBc"))
1367 (test #f (string-ci=? "abc" "aBcD"))
1368 
1369 (test #f (string-ci<? "abc" "aBc"))
1370 (test #t (string-ci<? "abc" "aBcD"))
1371 (test #f (string-ci<? "ABCd" "aBc"))
1372 
1373 (test #f (string-ci>? "abc" "aBc"))
1374 (test #f (string-ci>? "abc" "aBcD"))
1375 (test #t (string-ci>? "ABCd" "aBc"))
1376 
1377 (test #t (string-ci<=? "abc" "aBc"))
1378 (test #t (string-ci<=? "abc" "aBcD"))
1379 (test #f (string-ci<=? "ABCd" "aBc"))
1380 
1381 (test #t (string-ci>=? "abc" "aBc"))
1382 (test #f (string-ci>=? "abc" "aBcD"))
1383 (test #t (string-ci>=? "ABCd" "aBc"))
1384 
1385 (test #t (string-ci=? "ΑΒΓ" "αβγ" "αβγ"))
1386 (test #f (string-ci<? "ΑΒΓ" "αβγ"))
1387 (test #f (string-ci>? "ΑΒΓ" "αβγ"))
1388 (test #t (string-ci<=? "ΑΒΓ" "αβγ"))
1389 (test #t (string-ci>=? "ΑΒΓ" "αβγ"))
1390 
1391 ;; latin
1392 (test "ABC" (string-upcase "abc"))
1393 (test "ABC" (string-upcase "ABC"))
1394 (test "abc" (string-downcase "abc"))
1395 (test "abc" (string-downcase "ABC"))
1396 (test "abc" (string-foldcase "abc"))
1397 (test "abc" (string-foldcase "ABC"))
1398 
1399 ;; cyrillic
1400 (test "ΑΒΓ" (string-upcase "αβγ"))
1401 (test "ΑΒΓ" (string-upcase "ΑΒΓ"))
1402 (test "αβγ" (string-downcase "αβγ"))
1403 (test "αβγ" (string-downcase "ΑΒΓ"))
1404 (test "αβγ" (string-foldcase "αβγ"))
1405 (test "αβγ" (string-foldcase "ΑΒΓ"))
1406 
1407 ;; special cases
1408 (test "SSA" (string-upcase "ßa"))
1409 (test "ßa" (string-downcase "ßa"))
1410 (test "ssa" (string-downcase "SSA"))
1411 (test "maß" (string-downcase "Maß"))
1412 (test "mass" (string-foldcase "Maß"))
1413 (test "İ" (string-upcase "İ"))
1414 (test "i\x0307;" (string-downcase "İ"))
1415 (test "i\x0307;" (string-foldcase "İ"))
1416 (test "J̌" (string-upcase "ǰ"))
1417 (test "ſ" (string-downcase "ſ"))
1418 (test "s" (string-foldcase "ſ"))
1419 
1420 ;; context-sensitive (final sigma)
1421 (test "ΓΛΏΣΣΑ" (string-upcase "γλώσσα"))
1422 (test "γλώσσα" (string-downcase "ΓΛΏΣΣΑ"))
1423 (test "γλώσσα" (string-foldcase "ΓΛΏΣΣΑ"))
1424 (test "ΜΈΛΟΣ" (string-upcase "μέλος"))
1425 (test #t (and (member (string-downcase "ΜΈΛΟΣ") '("μέλος" "μέλοσ")) #t))
1426 (test "μέλοσ" (string-foldcase "ΜΈΛΟΣ"))
1427 (test #t (and (member (string-downcase "ΜΈΛΟΣ ΕΝΌΣ")
1428                       '("μέλος ενός" "μέλοσ ενόσ"))
1429               #t))
1430 
1431 (test "" (substring "" 0 0))
1432 (test "" (substring "a" 0 0))
1433 (test "" (substring "abc" 1 1))
1434 (test "ab" (substring "abc" 0 2))
1435 (test "bc" (substring "abc" 1 3))
1436 
1437 (test "" (string-append ""))
1438 (test "" (string-append "" ""))
1439 (test "abc" (string-append "" "abc"))
1440 (test "abc" (string-append "abc" ""))
1441 (test "abcde" (string-append "abc" "de"))
1442 (test "abcdef" (string-append "abc" "de" "f"))
1443 
1444 (test '() (string->list ""))
1445 (test '(#\a) (string->list "a"))
1446 (test '(#\a #\b #\c) (string->list "abc"))
1447 (test '(#\a #\b #\c) (string->list "abc" 0))
1448 (test '(#\b #\c) (string->list "abc" 1))
1449 (test '(#\b #\c) (string->list "abc" 1 3))
1450 
1451 (test "" (list->string '()))
1452 (test "abc" (list->string '(#\a #\b #\c)))
1453 
1454 (test "" (string-copy ""))
1455 (test "" (string-copy "" 0))
1456 (test "" (string-copy "" 0 0))
1457 (test "abc" (string-copy "abc"))
1458 (test "abc" (string-copy "abc" 0))
1459 (test "bc" (string-copy "abc" 1))
1460 (test "b" (string-copy "abc" 1 2))
1461 (test "bc" (string-copy "abc" 1 3))
1462 
1463 (test "-----"
1464     (let ((str (make-string 5 #\x))) (string-fill! str #\-) str))
1465 (test "xx---"
1466     (let ((str (make-string 5 #\x))) (string-fill! str #\- 2) str))
1467 (test "xx-xx"
1468     (let ((str (make-string 5 #\x))) (string-fill! str #\- 2 3) str))
1469 
1470 (test "a12de"
1471     (let ((str (string-copy "abcde"))) (string-copy! str 1 "12345" 0 2) str))
1472 (test "-----"
1473     (let ((str (make-string 5 #\x))) (string-copy! str 0 "-----") str))
1474 (test "---xx"
1475     (let ((str (make-string 5 #\x))) (string-copy! str 0 "-----" 2) str))
1476 (test "xx---"
1477     (let ((str (make-string 5 #\x))) (string-copy! str 2 "-----" 0 3) str))
1478 (test "xx-xx"
1479     (let ((str (make-string 5 #\x))) (string-copy! str 2 "-----" 2 3) str))
1480 
1481 ;; same source and dest
1482 (test "aabde"
1483     (let ((str (string-copy "abcde"))) (string-copy! str 1 str 0 2) str))
1484 (test "abcab"
1485     (let ((str (string-copy "abcde"))) (string-copy! str 3 str 0 2) str))
1486 
1487 (test-end)
1488 
1489 (test-begin "6.8 Vectors")
1490 
1491 (test #t (vector? #()))
1492 (test #t (vector? #(1 2 3)))
1493 (test #t (vector? '#(1 2 3)))
1494 
1495 (test 0 (vector-length (make-vector 0)))
1496 (test 1000 (vector-length (make-vector 1000)))
1497 
1498 (test #(0 (2 2 2 2) "Anna") '#(0 (2 2 2 2) "Anna"))
1499 
1500 (test #(a b c) (vector 'a 'b 'c))
1501 
1502 (test 8 (vector-ref '#(1 1 2 3 5 8 13 21) 5))
1503 (test 13 (vector-ref '#(1 1 2 3 5 8 13 21)
1504             (let ((i (round (* 2 (acos -1)))))
1505               (if (inexact? i)
1506                   (exact i)
1507                   i))))
1508 
1509 (test #(0 ("Sue" "Sue") "Anna") (let ((vec (vector 0 '(2 2 2 2) "Anna")))
1510   (vector-set! vec 1 '("Sue" "Sue"))
1511   vec))
1512 
1513 (test '(dah dah didah) (vector->list '#(dah dah didah)))
1514 (test '(dah didah) (vector->list '#(dah dah didah) 1))
1515 (test '(dah) (vector->list '#(dah dah didah) 1 2))
1516 (test #(dididit dah) (list->vector '(dididit dah)))
1517 
1518 (test #() (string->vector ""))
1519 (test #(#\A #\B #\C) (string->vector "ABC"))
1520 (test #(#\B #\C) (string->vector "ABC" 1))
1521 (test #(#\B) (string->vector "ABC" 1 2))
1522 
1523 (test "" (vector->string #()))
1524 (test "123" (vector->string #(#\1 #\2 #\3)))
1525 (test "23" (vector->string #(#\1 #\2 #\3) 1))
1526 (test "2" (vector->string #(#\1 #\2 #\3) 1 2))
1527 
1528 (test #() (vector-copy #()))
1529 (test #(a b c) (vector-copy #(a b c)))
1530 (test #(b c) (vector-copy #(a b c) 1))
1531 (test #(b) (vector-copy #(a b c) 1 2))
1532 
1533 (test #() (vector-append #()))
1534 (test #() (vector-append #() #()))
1535 (test #(a b c) (vector-append #() #(a b c)))
1536 (test #(a b c) (vector-append #(a b c) #()))
1537 (test #(a b c d e) (vector-append #(a b c) #(d e)))
1538 (test #(a b c d e f) (vector-append #(a b c) #(d e) #(f)))
1539 
1540 (test #(1 2 smash smash 5)
1541     (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'smash 2 4) vec))
1542 (test #(x x x x x)
1543     (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'x) vec))
1544 (test #(1 2 x x x)
1545     (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'x 2) vec))
1546 (test #(1 2 x 4 5)
1547     (let ((vec (vector 1 2 3 4 5))) (vector-fill! vec 'x 2 3) vec))
1548 
1549 (test #(1 a b 4 5)
1550     (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 1 #(a b c d e) 0 2) vec))
1551 (test #(a b c d e)
1552     (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 0 #(a b c d e)) vec))
1553 (test #(c d e 4 5)
1554     (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 0 #(a b c d e) 2) vec))
1555 (test #(1 2 a b c)
1556     (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 2 #(a b c d e) 0 3) vec))
1557 (test #(1 2 c 4 5)
1558     (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 2 #(a b c d e) 2 3) vec))
1559 
1560 ;; same source and dest
1561 (test #(1 1 2 4 5)
1562     (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 1 vec 0 2) vec))
1563 (test #(1 2 3 1 2)
1564     (let ((vec (vector 1 2 3 4 5))) (vector-copy! vec 3 vec 0 2) vec))
1565 
1566 (test-end)
1567 
1568 (test-begin "6.9 Bytevectors")
1569 
1570 (test #t (bytevector? #u8()))
1571 (test #t (bytevector? #u8(0 1 2)))
1572 (test #f (bytevector? #()))
1573 (test #f (bytevector? #(0 1 2)))
1574 (test #f (bytevector? '()))
1575 (test #t (bytevector? (make-bytevector 0)))
1576 
1577 (test 0 (bytevector-length (make-bytevector 0)))
1578 (test 1024 (bytevector-length (make-bytevector 1024)))
1579 (test 1024 (bytevector-length (make-bytevector 1024 255)))
1580 
1581 (test 3 (bytevector-length (bytevector 0 1 2)))
1582 
1583 (test 0 (bytevector-u8-ref (bytevector 0 1 2) 0))
1584 (test 1 (bytevector-u8-ref (bytevector 0 1 2) 1))
1585 (test 2 (bytevector-u8-ref (bytevector 0 1 2) 2))
1586 
1587 (test #u8(0 255 2)
1588     (let ((bv (bytevector 0 1 2))) (bytevector-u8-set! bv 1 255) bv))
1589 
1590 (test #u8() (bytevector-copy #u8()))
1591 (test #u8(0 1 2) (bytevector-copy #u8(0 1 2)))
1592 (test #u8(1 2) (bytevector-copy #u8(0 1 2) 1))
1593 (test #u8(1) (bytevector-copy #u8(0 1 2) 1 2))
1594 
1595 (test #u8(1 6 7 4 5)
1596     (let ((bv (bytevector 1 2 3 4 5)))
1597       (bytevector-copy! bv 1 #u8(6 7 8 9 10) 0 2)
1598       bv))
1599 (test #u8(6 7 8 9 10)
1600     (let ((bv (bytevector 1 2 3 4 5)))
1601       (bytevector-copy! bv 0 #u8(6 7 8 9 10))
1602       bv))
1603 (test #u8(8 9 10 4 5)
1604     (let ((bv (bytevector 1 2 3 4 5)))
1605       (bytevector-copy! bv 0 #u8(6 7 8 9 10) 2)
1606       bv))
1607 (test #u8(1 2 6 7 8)
1608     (let ((bv (bytevector 1 2 3 4 5)))
1609       (bytevector-copy! bv 2 #u8(6 7 8 9 10) 0 3)
1610       bv))
1611 (test #u8(1 2 8 4 5)
1612     (let ((bv (bytevector 1 2 3 4 5)))
1613       (bytevector-copy! bv 2 #u8(6 7 8 9 10) 2 3)
1614       bv))
1615 
1616 ;; same source and dest
1617 (test #u8(1 1 2 4 5)
1618     (let ((bv (bytevector 1 2 3 4 5)))
1619       (bytevector-copy! bv 1 bv 0 2)
1620       bv))
1621 (test #u8(1 2 3 1 2)
1622     (let ((bv (bytevector 1 2 3 4 5)))
1623       (bytevector-copy! bv 3 bv 0 2)
1624       bv))
1625 
1626 (test #u8() (bytevector-append #u8()))
1627 (test #u8() (bytevector-append #u8() #u8()))
1628 (test #u8(0 1 2) (bytevector-append #u8() #u8(0 1 2)))
1629 (test #u8(0 1 2) (bytevector-append #u8(0 1 2) #u8()))
1630 (test #u8(0 1 2 3 4) (bytevector-append #u8(0 1 2) #u8(3 4)))
1631 (test #u8(0 1 2 3 4 5) (bytevector-append #u8(0 1 2) #u8(3 4) #u8(5)))
1632 
1633 (test "ABC" (utf8->string #u8(#x41 #x42 #x43)))
1634 (test "ABC" (utf8->string #u8(0 #x41 #x42 #x43) 1))
1635 (test "ABC" (utf8->string #u8(0 #x41  #x42 #x43 0) 1 4))
1636 (test "λ" (utf8->string #u8(0 #xCE #xBB 0) 1 3))
1637 (test #u8(#x41 #x42 #x43) (string->utf8 "ABC"))
1638 (test #u8(#x42 #x43) (string->utf8 "ABC" 1))
1639 (test #u8(#x42) (string->utf8 "ABC" 1 2))
1640 (test #u8(#xCE #xBB) (string->utf8 "λ"))
1641 
1642 (test-end)
1643 
1644 (test-begin "6.10 Control Features")
1645 
1646 (test #t (procedure? car))
1647 (test #f (procedure? 'car))
1648 (test #t (procedure? (lambda (x) (* x x))))
1649 (test #f (procedure? '(lambda (x) (* x x))))
1650 (test #t (call-with-current-continuation procedure?))
1651 
1652 (test 7 (apply + (list 3 4)))
1653 (test 7 (apply + 3 4 (list)))
1654 (test-error (apply +)) ;; not enough args
1655 (test-error (apply + 3)) ;; final arg not a list
1656 (test-error (apply + 3 4)) ;; final arg not a list
1657 (test-error (apply + '(2 3 . 4))) ;; final arg is improper
1658 
1659 
1660 (define compose
1661   (lambda (f g)
1662     (lambda args
1663       (f (apply g args)))))
1664 (test '(30 0)
1665     (call-with-values (lambda () ((compose exact-integer-sqrt *) 12 75))
1666       list))
1667 
1668 (test '(b e h) (map cadr '((a b) (d e) (g h))))
1669 
1670 (test '(1 4 27 256 3125) (map (lambda (n) (expt n n)) '(1 2 3 4 5)))
1671 
1672 (test '(5 7 9) (map + '(1 2 3) '(4 5 6 7)))
1673 
1674 (test #t
1675     (let ((res (let ((count 0))
1676                  (map (lambda (ignored)
1677                         (set! count (+ count 1))
1678                         count)
1679                       '(a b)))))
1680       (or (equal? res '(1 2))
1681           (equal? res '(2 1)))))
1682 
1683 (test '(10 200 3000 40 500 6000)
1684     (let ((ls1 (list 10 100 1000))
1685           (ls2 (list 1 2 3 4 5 6)))
1686       (set-cdr! (cddr ls1) ls1)
1687       (map * ls1 ls2)))
1688 
1689 (test "abdegh" (string-map char-foldcase "AbdEgH"))
1690 
1691 (test "IBM" (string-map
1692  (lambda (c)
1693    (integer->char (+ 1 (char->integer c))))
1694  "HAL"))
1695 
1696 (test "StUdLyCaPs"
1697     (string-map
1698      (lambda (c k) (if (eqv? k #\u) (char-upcase c) (char-downcase c)))
1699      "studlycaps xxx"
1700      "ululululul"))
1701 
1702 (test #(b e h) (vector-map cadr '#((a b) (d e) (g h))))
1703 
1704 (test #(1 4 27 256 3125)
1705     (vector-map (lambda (n) (expt n n))
1706                 '#(1 2 3 4 5)))
1707 
1708 (test #(5 7 9) (vector-map + '#(1 2 3) '#(4 5 6 7)))
1709 
1710 (test #t
1711     (let ((res (let ((count 0))
1712                  (vector-map
1713                   (lambda (ignored)
1714                     (set! count (+ count 1))
1715                     count)
1716                   '#(a b)))))
1717       (or (equal? res #(1 2))
1718           (equal? res #(2 1)))))
1719 
1720 (test #(0 1 4 9 16)
1721     (let ((v (make-vector 5)))
1722       (for-each (lambda (i)
1723                   (vector-set! v i (* i i)))
1724                 '(0 1 2 3 4))
1725       v))
1726 
1727 (test 9750
1728     (let ((ls1 (list 10 100 1000))
1729           (ls2 (list 1 2 3 4 5 6))
1730           (count 0))
1731       (set-cdr! (cddr ls1) ls1)
1732       (for-each (lambda (x y) (set! count (+ count (* x y)))) ls2 ls1)
1733       count))
1734 
1735 (test '(101 100 99 98 97)
1736     (let ((v '()))
1737       (string-for-each
1738        (lambda (c) (set! v (cons (char->integer c) v)))
1739        "abcde")
1740       v))
1741 
1742 (test '(0 1 4 9 16) (let ((v (make-list 5)))
1743   (vector-for-each
1744    (lambda (i) (list-set! v i (* i i)))
1745    '#(0 1 2 3 4))
1746   v))
1747 
1748 (test -3 (call-with-current-continuation
1749   (lambda (exit)
1750     (for-each (lambda (x)
1751                 (if (negative? x)
1752                     (exit x)))
1753               '(54 0 37 -3 245 19))
1754     #t)))
1755 (define list-length
1756   (lambda (obj)
1757     (call-with-current-continuation
1758       (lambda (return)
1759         (letrec ((r
1760                   (lambda (obj)
1761                     (cond ((null? obj) 0)
1762                           ((pair? obj)
1763                            (+ (r (cdr obj)) 1))
1764                           (else (return #f))))))
1765           (r obj))))))
1766 
1767 (test 4 (list-length '(1 2 3 4)))
1768 
1769 (test #f (list-length '(a b . c)))
1770 
1771 (test 5
1772     (call-with-values (lambda () (values 4 5))
1773       (lambda (a b) b)))
1774 
1775 (test -1 (call-with-values * -))
1776 
1777 (test '(connect talk1 disconnect
1778         connect talk2 disconnect)
1779     (let ((path '())
1780           (c #f))
1781       (let ((add (lambda (s)
1782                    (set! path (cons s path)))))
1783         (dynamic-wind
1784           (lambda () (add 'connect))
1785           (lambda ()
1786             (add (call-with-current-continuation
1787                   (lambda (c0)
1788                     (set! c c0)
1789                     'talk1))))
1790           (lambda () (add 'disconnect)))
1791         (if (< (length path) 4)
1792             (c 'talk2)
1793             (reverse path)))))
1794 
1795 (test-end)
1796 
1797 (test-begin "6.11 Exceptions")
1798 
1799 (test 65
1800     (with-exception-handler
1801      (lambda (con) 42)
1802      (lambda ()
1803        (+ (raise-continuable "should be a number")
1804           23))))
1805 
1806 (test #t
1807     (error-object? (guard (exn (else exn)) (error "BOOM!" 1 2 3))))
1808 (test "BOOM!"
1809     (error-object-message (guard (exn (else exn)) (error "BOOM!" 1 2 3))))
1810 (test '(1 2 3)
1811     (error-object-irritants (guard (exn (else exn)) (error "BOOM!" 1 2 3))))
1812 
1813 (test #f
1814     (file-error? (guard (exn (else exn)) (error "BOOM!"))))
1815 (test #t
1816     (file-error? (guard (exn (else exn)) (open-input-file " no such file "))))
1817 
1818 (test #f
1819     (read-error? (guard (exn (else exn)) (error "BOOM!"))))
1820 (test #t
1821     (read-error? (guard (exn (else exn)) (read (open-input-string ")")))))
1822 (test #t
1823     (read-error? (guard (exn (else exn)) (read (open-input-string "\"")))))
1824 
1825 (define something-went-wrong #f)
1826 (define (test-exception-handler-1 v)
1827   (call-with-current-continuation
1828    (lambda (k)
1829      (with-exception-handler
1830       (lambda (x)
1831         (set! something-went-wrong (list "condition: " x))
1832         (k 'exception))
1833       (lambda ()
1834         (+ 1 (if (> v 0) (+ v 100) (raise 'an-error))))))))
1835 (test 106 (test-exception-handler-1 5))
1836 (test #f something-went-wrong)
1837 (test 'exception (test-exception-handler-1 -1))
1838 (test '("condition: " an-error) something-went-wrong)
1839 
1840 (set! something-went-wrong #f)
1841 (define (test-exception-handler-2 v)
1842   (guard (ex (else 'caught-another-exception))
1843     (with-exception-handler
1844      (lambda (x)
1845        (set! something-went-wrong #t)
1846        (list "exception:" x))
1847      (lambda ()
1848        (+ 1 (if (> v 0) (+ v 100) (raise 'an-error)))))))
1849 (test 106 (test-exception-handler-2 5))
1850 (test #f something-went-wrong)
1851 (test 'caught-another-exception (test-exception-handler-2 -1))
1852 (test #t something-went-wrong)
1853 
1854 ;; Based on an example from R6RS-lib section 7.1 Exceptions.
1855 ;; R7RS section 6.11 Exceptions has a simplified version.
1856 (let* ((out (open-output-string))
1857        (value (with-exception-handler
1858                (lambda (con)
1859                  (cond
1860                   ((not (list? con))
1861                    (raise con))
1862                   ((list? con)
1863                    (display (car con) out))
1864                   (else
1865                    (display "a warning has been issued" out)))
1866                  42)
1867                (lambda ()
1868                  (+ (raise-continuable
1869                      (list "should be a number"))
1870                     23)))))
1871   (test "should be a number" (get-output-string out))
1872   (test 65 value))
1873 
1874 ;; From SRFI-34 "Examples" section - #3
1875 (define (test-exception-handler-3 v out)
1876   (guard (condition
1877           (else
1878            (display "condition: " out)
1879            (write condition out)
1880            (display #\! out)
1881            'exception))
1882          (+ 1 (if (= v 0) (raise 'an-error) (/ 10 v)))))
1883 (let* ((out (open-output-string))
1884        (value (test-exception-handler-3 0 out)))
1885   (test 'exception value)
1886   (test "condition: an-error!" (get-output-string out)))
1887 
1888 (define (test-exception-handler-4 v out)
1889   (call-with-current-continuation
1890    (lambda (k)
1891      (with-exception-handler
1892       (lambda (x)
1893         (display "reraised " out)
1894         (write x out) (display #\! out)
1895         (k 'zero))
1896       (lambda ()
1897         (guard (condition
1898                 ((positive? condition)
1899                  'positive)
1900                 ((negative? condition)
1901                  'negative))
1902           (raise v)))))))
1903 
1904 ;; From SRFI-34 "Examples" section - #5
1905 (let* ((out (open-output-string))
1906        (value (test-exception-handler-4 1 out)))
1907   (test "" (get-output-string out))
1908   (test 'positive value))
1909 ;; From SRFI-34 "Examples" section - #6
1910 (let* ((out (open-output-string))
1911        (value (test-exception-handler-4 -1 out)))
1912   (test "" (get-output-string out))
1913   (test 'negative value))
1914 ;; From SRFI-34 "Examples" section - #7
1915 (let* ((out (open-output-string))
1916        (value (test-exception-handler-4 0 out)))
1917   (test "reraised 0!" (get-output-string out))
1918   (test 'zero value))
1919 
1920 ;; From SRFI-34 "Examples" section - #8
1921 (test 42
1922     (guard (condition
1923             ((assq 'a condition) => cdr)
1924             ((assq 'b condition)))
1925       (raise (list (cons 'a 42)))))
1926 
1927 ;; From SRFI-34 "Examples" section - #9
1928 (test '(b . 23)
1929     (guard (condition
1930             ((assq 'a condition) => cdr)
1931             ((assq 'b condition)))
1932       (raise (list (cons 'b 23)))))
1933 
1934 (test 'caught-d
1935     (guard (condition
1936             ((assq 'c condition) 'caught-c)
1937             ((assq 'd condition) 'caught-d))
1938       (list
1939        (sqrt 8)
1940        (guard (condition
1941                ((assq 'a condition) => cdr)
1942                ((assq 'b condition)))
1943          (raise (list (cons 'd 24)))))))
1944 
1945 (test-end)
1946 
1947 (test-begin "6.12 Environments and evaluation")
1948 
1949 ;; (test 21 (eval '(* 7 3) (scheme-report-environment 5)))
1950 
1951 (test 20
1952     (let ((f (eval '(lambda (f x) (f x x)) (null-environment 5))))
1953       (f + 10)))
1954 
1955 (test 1024 (eval '(expt 2 10) (environment '(scheme base))))
1956 ;; (sin 0) may return exact number
1957 (test 0.0 (inexact (eval '(sin 0) (environment '(scheme inexact)))))
1958 ;; ditto
1959 (test 1024.0 (eval '(+ (expt 2 10) (inexact (sin 0)))
1960                    (environment '(scheme base) '(scheme inexact))))
1961 
1962 (test-end)
1963 
1964 (test-begin "6.13 Input and output")
1965 
1966 (test #t (port? (current-input-port)))
1967 (test #t (input-port? (current-input-port)))
1968 (test #t (output-port? (current-output-port)))
1969 (test #t (output-port? (current-error-port)))
1970 (test #t (input-port? (open-input-string "abc")))
1971 (test #t (output-port? (open-output-string)))
1972 
1973 (test #t (textual-port? (open-input-string "abc")))
1974 (test #t (textual-port? (open-output-string)))
1975 (test #t (binary-port? (open-input-bytevector #u8(0 1 2))))
1976 (test #t (binary-port? (open-output-bytevector)))
1977 
1978 (test #t (input-port-open? (open-input-string "abc")))
1979 (test #t (output-port-open? (open-output-string)))
1980 
1981 (test #f
1982     (let ((in (open-input-string "abc")))
1983       (close-input-port in)
1984       (input-port-open? in)))
1985 
1986 (test #f
1987     (let ((out (open-output-string)))
1988       (close-output-port out)
1989       (output-port-open? out)))
1990 
1991 (test #f
1992     (let ((out (open-output-string)))
1993       (close-port out)
1994       (output-port-open? out)))
1995 
1996 (test 'error
1997     (let ((in (open-input-string "abc")))
1998       (close-input-port in)
1999       (guard (exn (else 'error)) (read-char in))))
2000 
2001 (test 'error
2002     (let ((out (open-output-string)))
2003       (close-output-port out)
2004       (guard (exn (else 'error)) (write-char #\c out))))
2005 
2006 (test #t (eof-object? (eof-object)))
2007 (test #t (eof-object? (read (open-input-string ""))))
2008 (test #t (char-ready? (open-input-string "42")))
2009 (test 42 (read (open-input-string " 42 ")))
2010 
2011 (test #t (eof-object? (read-char (open-input-string ""))))
2012 (test #\a (read-char (open-input-string "abc")))
2013 
2014 (test #t (eof-object? (read-line (open-input-string ""))))
2015 (test "abc" (read-line (open-input-string "abc")))
2016 (test "abc" (read-line (open-input-string "abc\ndef\n")))
2017 
2018 (test #t (eof-object? (read-string 3 (open-input-string ""))))
2019 (test "abc" (read-string 3 (open-input-string "abcd")))
2020 (test "abc" (read-string 3 (open-input-string "abc\ndef\n")))
2021 
2022 (let ((in (open-input-string (string #\x10F700 #\x10F701 #\x10F702))))
2023   (let* ((c0 (peek-char in))
2024          (c1 (read-char in))
2025          (c2 (read-char in))
2026          (c3 (read-char in)))
2027     (test #\x10F700 c0)
2028     (test #\x10F700 c1)
2029     (test #\x10F701 c2)
2030     (test #\x10F702 c3)))
2031 
2032 (test (string #\x10F700)
2033     (let ((out (open-output-string)))
2034       (write-char #\x10F700 out)
2035       (get-output-string out)))
2036 
2037 (test "abc"
2038     (let ((out (open-output-string)))
2039       (write 'abc out)
2040       (get-output-string out)))
2041 
2042 (test "abc def"
2043     (let ((out (open-output-string)))
2044       (display "abc def" out)
2045       (get-output-string out)))
2046 
2047 (test "abc"
2048     (let ((out (open-output-string)))
2049       (display #\a out)
2050       (display "b" out)
2051       (display #\c out)
2052       (get-output-string out)))
2053 
2054 (test #t
2055       (let* ((out (open-output-string))
2056              (r (begin (newline out) (get-output-string out))))
2057         (or (equal? r "\n") (equal? r "\r\n"))))
2058 
2059 (test "abc def"
2060     (let ((out (open-output-string)))
2061       (write-string "abc def" out)
2062       (get-output-string out)))
2063 
2064 (test "def"
2065     (let ((out (open-output-string)))
2066       (write-string "abc def" out 4)
2067       (get-output-string out)))
2068 
2069 (test "c d"
2070     (let ((out (open-output-string)))
2071       (write-string "abc def" out 2 5)
2072       (get-output-string out)))
2073 
2074 (test ""
2075   (let ((out (open-output-string)))
2076     (flush-output-port out)
2077     (get-output-string out)))
2078 
2079 (test #t (eof-object? (read-u8 (open-input-bytevector #u8()))))
2080 (test 1 (read-u8 (open-input-bytevector #u8(1 2 3))))
2081 
2082 (test #t (eof-object? (read-bytevector 3 (open-input-bytevector #u8()))))
2083 (test #t (u8-ready? (open-input-bytevector #u8(1))))
2084 (test #u8(1) (read-bytevector 3 (open-input-bytevector #u8(1))))
2085 (test #u8(1 2) (read-bytevector 3 (open-input-bytevector #u8(1 2))))
2086 (test #u8(1 2 3) (read-bytevector 3 (open-input-bytevector #u8(1 2 3))))
2087 (test #u8(1 2 3) (read-bytevector 3 (open-input-bytevector #u8(1 2 3 4))))
2088 
2089 (test #t
2090     (let ((bv (bytevector 1 2 3 4 5)))
2091       (eof-object? (read-bytevector! bv (open-input-bytevector #u8())))))
2092 
2093 (test #u8(6 7 8 9 10)
2094   (let ((bv (bytevector 1 2 3 4 5)))
2095     (read-bytevector! bv (open-input-bytevector #u8(6 7 8 9 10)) 0 5)
2096     bv))
2097 
2098 (test #u8(6 7 8 4 5)
2099   (let ((bv (bytevector 1 2 3 4 5)))
2100     (read-bytevector! bv (open-input-bytevector #u8(6 7 8 9 10)) 0 3)
2101     bv))
2102 
2103 (test #u8(1 2 3 6 5)
2104   (let ((bv (bytevector 1 2 3 4 5)))
2105     (read-bytevector! bv (open-input-bytevector #u8(6 7 8 9 10)) 3 4)
2106     bv))
2107 
2108 (test #u8(1 2 3)
2109   (let ((out (open-output-bytevector)))
2110     (write-u8 1 out)
2111     (write-u8 2 out)
2112     (write-u8 3 out)
2113     (get-output-bytevector out)))
2114 
2115 (test #u8(1 2 3 4 5)
2116   (let ((out (open-output-bytevector)))
2117     (write-bytevector #u8(1 2 3 4 5) out)
2118     (get-output-bytevector out)))
2119 
2120 (test #u8(3 4 5)
2121   (let ((out (open-output-bytevector)))
2122     (write-bytevector #u8(1 2 3 4 5) out 2)
2123     (get-output-bytevector out)))
2124 
2125 (test #u8(3 4)
2126   (let ((out (open-output-bytevector)))
2127     (write-bytevector #u8(1 2 3 4 5) out 2 4)
2128     (get-output-bytevector out)))
2129 
2130 (test #u8()
2131   (let ((out (open-output-bytevector)))
2132     (flush-output-port out)
2133     (get-output-bytevector out)))
2134 
2135 (test #t
2136     (and (member
2137           (let ((out (open-output-string))
2138                 (x (list 1)))
2139             (set-cdr! x x)
2140             (write x out)
2141             (get-output-string out))
2142           ;; labels not guaranteed to be 0 indexed, spacing may differ
2143           '("#0=(1 . #0#)" "#1=(1 . #1#)"))
2144          #t))
2145 
2146 (test "((1 2 3) (1 2 3))"
2147     (let ((out (open-output-string))
2148           (x (list 1 2 3)))
2149       (write (list x x) out)
2150       (get-output-string out)))
2151 
2152 (test "((1 2 3) (1 2 3))"
2153     (let ((out (open-output-string))
2154           (x (list 1 2 3)))
2155       (write-simple (list x x) out)
2156       (get-output-string out)))
2157 
2158 (test #t
2159     (and (member (let ((out (open-output-string))
2160                        (x (list 1 2 3)))
2161                    (write-shared (list x x) out)
2162                    (get-output-string out))
2163                  '("(#0=(1 2 3) #0#)" "(#1=(1 2 3) #1#)"))
2164          #t))
2165 
2166 (test-begin "Read syntax")
2167 
2168 ;; check reading boolean followed by eof
2169 (test #t (read (open-input-string "#t")))
2170 (test #t (read (open-input-string "#true")))
2171 (test #f (read (open-input-string "#f")))
2172 (test #f (read (open-input-string "#false")))
2173 (define (read2 port)
2174   (let* ((o1 (read port)) (o2 (read port)))
2175     (cons o1 o2)))
2176 ;; check reading boolean followed by delimiter
2177 (test '(#t . (5)) (read2 (open-input-string "#t(5)")))
2178 (test '(#t . 6) (read2 (open-input-string "#true 6 ")))
2179 (test '(#f . 7) (read2 (open-input-string "#f 7")))
2180 (test '(#f . "8") (read2 (open-input-string "#false\"8\"")))
2181 
2182 (test '() (read (open-input-string "()")))
2183 (test '(1 2) (read (open-input-string "(1 2)")))
2184 (test '(1 . 2) (read (open-input-string "(1 . 2)")))
2185 (test '(1 2) (read (open-input-string "(1 . (2))")))
2186 (test '(1 2 3 4 5) (read (open-input-string "(1 . (2 3 4 . (5)))")))
2187 (test '1 (cadr (read (open-input-string "#0=(1 . #0#)"))))
2188 (test '(1 2 3) (cadr (read (open-input-string "(#0=(1 2 3) #0#)"))))
2189 
2190 (test '(quote (1 2)) (read (open-input-string "'(1 2)")))
2191 (test '(quote (1 (unquote 2))) (read (open-input-string "'(1 ,2)")))
2192 (test '(quote (1 (unquote-splicing 2))) (read (open-input-string "'(1 ,@2)")))
2193 (test '(quasiquote (1 (unquote 2))) (read (open-input-string "`(1 ,2)")))
2194 
2195 (test #() (read (open-input-string "#()")))
2196 (test #(a b) (read (open-input-string "#(a b)")))
2197 
2198 (test #u8() (read (open-input-string "#u8()")))
2199 (test #u8(0 1) (read (open-input-string "#u8(0 1)")))
2200 
2201 (test 'abc (read (open-input-string "abc")))
2202 (test 'abc (read (open-input-string "abc def")))
2203 (test 'ABC (read (open-input-string "ABC")))
2204 (test 'Hello (read (open-input-string "|H\\x65;llo|")))
2205 
2206 (test 'abc (read (open-input-string "#!fold-case ABC")))
2207 (test 'ABC (read (open-input-string "#!fold-case #!no-fold-case ABC")))
2208 
2209 (test 'def (read (open-input-string "#; abc def")))
2210 (test 'def (read (open-input-string "; abc \ndef")))
2211 (test 'def (read (open-input-string "#| abc |# def")))
2212 (test 'ghi (read (open-input-string "#| abc #| def |# |# ghi")))
2213 (test 'ghi (read (open-input-string "#; ; abc\n def ghi")))
2214 (test '(abs -16) (read (open-input-string "(#;sqrt abs -16)")))
2215 (test '(a d) (read (open-input-string "(a #; #;b c d)")))
2216 (test '(a e) (read (open-input-string "(a #;(b #;c d) e)")))
2217 (test '(a . c) (read (open-input-string "(a . #;b c)")))
2218 (test '(a . b) (read (open-input-string "(a . b #;c)")))
2219 
2220 (define (test-read-error str)
2221   (test-assert str
2222       (guard (exn (else #t))
2223         (read (open-input-string str))
2224         #f)))
2225 
2226 (test-read-error "(#;a . b)")
2227 (test-read-error "(a . #;b)")
2228 (test-read-error "(a #;. b)")
2229 (test-read-error "(#;x #;y . z)")
2230 (test-read-error "(#; #;x #;y . z)")
2231 (test-read-error "(#; #;x . z)")
2232 
2233 (test #\a (read (open-input-string "#\\a")))
2234 (test #\space (read (open-input-string "#\\space")))
2235 (test 0 (char->integer (read (open-input-string "#\\null"))))
2236 (test 7 (char->integer (read (open-input-string "#\\alarm"))))
2237 (test 8 (char->integer (read (open-input-string "#\\backspace"))))
2238 (test 9 (char->integer (read (open-input-string "#\\tab"))))
2239 (test 10 (char->integer (read (open-input-string "#\\newline"))))
2240 (test 13 (char->integer (read (open-input-string "#\\return"))))
2241 (test #x7F (char->integer (read (open-input-string "#\\delete"))))
2242 (test #x1B (char->integer (read (open-input-string "#\\escape"))))
2243 (test #x03BB (char->integer (read (open-input-string "#\\λ"))))
2244 (test #x03BB (char->integer (read (open-input-string "#\\x03BB"))))
2245 
2246 (test "abc" (read (open-input-string "\"abc\"")))
2247 (test "abc" (read (open-input-string "\"abc\" \"def\"")))
2248 (test "ABC" (read (open-input-string "\"ABC\"")))
2249 (test "Hello" (read (open-input-string "\"H\\x65;llo\"")))
2250 (test 7 (char->integer (string-ref (read (open-input-string "\"\\a\"")) 0)))
2251 (test 8 (char->integer (string-ref (read (open-input-string "\"\\b\"")) 0)))
2252 (test 9 (char->integer (string-ref (read (open-input-string "\"\\t\"")) 0)))
2253 (test 10 (char->integer (string-ref (read (open-input-string "\"\\n\"")) 0)))
2254 (test 13 (char->integer (string-ref (read (open-input-string "\"\\r\"")) 0)))
2255 (test #x22 (char->integer (string-ref (read (open-input-string "\"\\\"\"")) 0)))
2256 (test #x7C (char->integer (string-ref (read (open-input-string "\"\\|\"")) 0)))
2257 (test "line 1\nline 2\n" (read (open-input-string "\"line 1\nline 2\n\"")))
2258 (test "line 1continued\n" (read (open-input-string "\"line 1\\\ncontinued\n\"")))
2259 (test "line 1continued\n" (read (open-input-string "\"line 1\\ \ncontinued\n\"")))
2260 (test "line 1continued\n" (read (open-input-string "\"line 1\\\n continued\n\"")))
2261 (test "line 1continued\n" (read (open-input-string "\"line 1\\ \t \n \t continued\n\"")))
2262 (test "line 1\n\nline 3\n" (read (open-input-string "\"line 1\\ \t \n \t \n\nline 3\n\"")))
2263 (test #x03BB (char->integer (string-ref (read (open-input-string "\"\\x03BB;\"")) 0)))
2264 
2265 (define-syntax test-write-syntax
2266   (syntax-rules ()
2267     ((test-write-syntax expect-str obj-expr)
2268      (let ((out (open-output-string)))
2269        (write obj-expr out)
2270        (test expect-str (get-output-string out))))))
2271 
2272 (test-write-syntax "|.|" '|.|)
2273 (test-write-syntax "|a b|" '|a b|)
2274 (test-write-syntax "|,a|" '|,a|)
2275 (test-write-syntax "|\"|" '|\"|)
2276 (test-write-syntax "|\\||" '|\||)
2277 (test-write-syntax "||" '||)
2278 (test-write-syntax "|\\\\123|" '|\\123|)
2279 (test-write-syntax "a" '|a|)
2280 ;; (test-write-syntax "a.b" '|a.b|)
2281 (test-write-syntax "|2|" '|2|)
2282 (test-write-syntax "|+3|" '|+3|)
2283 (test-write-syntax "|-.4|" '|-.4|)
2284 (test-write-syntax "|+i|" '|+i|)
2285 (test-write-syntax "|-i|" '|-i|)
2286 (test-write-syntax "|+inf.0|" '|+inf.0|)
2287 (test-write-syntax "|-inf.0|" '|-inf.0|)
2288 (test-write-syntax "|+nan.0|" '|+nan.0|)
2289 (test-write-syntax "|+NaN.0|" '|+NaN.0|)
2290 (test-write-syntax "|+NaN.0abc|" '|+NaN.0abc|)
2291 
2292 (test-end)
2293 
2294 (test-begin "Numeric syntax")
2295 
2296 ;; Numeric syntax adapted from Peter Bex's tests.
2297 ;;
2298 ;; These are updated to R7RS, using string ports instead of
2299 ;; string->number, and "error" tests removed because implementations
2300 ;; are free to provide their own numeric extensions.  Currently all
2301 ;; tests are run by default - need to cond-expand and test for
2302 ;; infinities and -0.0.
2303 
2304 (define-syntax test-numeric-syntax
2305   (syntax-rules ()
2306     ((test-numeric-syntax str expect strs ...)
2307      (let* ((z (read (open-input-string str)))
2308             (out (open-output-string))
2309             (z-str (begin (write z out) (get-output-string out))))
2310        (test expect (values z))
2311        (test #t (and (member z-str '(str strs ...)) #t))))))
2312 
2313 ;; Each test is of the form:
2314 ;;
2315 ;;   (test-numeric-syntax input-str expected-value expected-write-values ...)
2316 ;;
2317 ;; where the input should be eqv? to the expected-value, and the
2318 ;; written output the same as any of the expected-write-values.  The
2319 ;; form
2320 ;;
2321 ;;   (test-numeric-syntax input-str expected-value)
2322 ;;
2323 ;; is a shorthand for
2324 ;;
2325 ;;   (test-numeric-syntax input-str expected-value (input-str))
2326 
2327 ;; Simple
2328 (test-numeric-syntax "1" 1)
2329 (test-numeric-syntax "+1" 1 "1")
2330 (test-numeric-syntax "-1" -1)
2331 (test-numeric-syntax "#i1" 1.0 "1.0" "1.")
2332 (test-numeric-syntax "#I1" 1.0 "1.0" "1.")
2333 (test-numeric-syntax "#i-1" -1.0 "-1.0" "-1.")
2334 ;; Decimal
2335 (test-numeric-syntax "1.0" 1.0 "1.0" "1.")
2336 (test-numeric-syntax "1." 1.0 "1.0" "1.")
2337 (test-numeric-syntax ".1" 0.1 "0.1" "100.0e-3")
2338 (test-numeric-syntax "-.1" -0.1 "-0.1" "-100.0e-3")
2339 ;; Some Schemes don't allow negative zero. This is okay with the standard
2340 (test-numeric-syntax "-.0" -0.0 "-0." "-0.0" "0.0" "0." ".0")
2341 (test-numeric-syntax "-0." -0.0 "-.0" "-0.0" "0.0" "0." ".0")
2342 (test-numeric-syntax "#i1.0" 1.0 "1.0" "1.")
2343 (test-numeric-syntax "#e1.0" 1 "1")
2344 (test-numeric-syntax "#e-.0" 0 "0")
2345 (test-numeric-syntax "#e-0." 0 "0")
2346 ;; Decimal notation with suffix
2347 (test-numeric-syntax "1e2" 100.0 "100.0" "100.")
2348 (test-numeric-syntax "1E2" 100.0 "100.0" "100.")
2349 (test-numeric-syntax "1s2" 100.0 "100.0" "100.")
2350 (test-numeric-syntax "1S2" 100.0 "100.0" "100.")
2351 (test-numeric-syntax "1f2" 100.0 "100.0" "100.")
2352 (test-numeric-syntax "1F2" 100.0 "100.0" "100.")
2353 (test-numeric-syntax "1d2" 100.0 "100.0" "100.")
2354 (test-numeric-syntax "1D2" 100.0 "100.0" "100.")
2355 (test-numeric-syntax "1l2" 100.0 "100.0" "100.")
2356 (test-numeric-syntax "1L2" 100.0 "100.0" "100.")
2357 ;; NaN, Inf
2358 (test-numeric-syntax "+nan.0" +nan.0 "+nan.0" "+NaN.0")
2359 (test-numeric-syntax "+NAN.0" +nan.0 "+nan.0" "+NaN.0")
2360 (test-numeric-syntax "+inf.0" +inf.0 "+inf.0" "+Inf.0")
2361 (test-numeric-syntax "+InF.0" +inf.0 "+inf.0" "+Inf.0")
2362 (test-numeric-syntax "-inf.0" -inf.0 "-inf.0" "-Inf.0")
2363 (test-numeric-syntax "-iNF.0" -inf.0 "-inf.0" "-Inf.0")
2364 (test-numeric-syntax "#i+nan.0" +nan.0 "+nan.0" "+NaN.0")
2365 (test-numeric-syntax "#i+inf.0" +inf.0 "+inf.0" "+Inf.0")
2366 (test-numeric-syntax "#i-inf.0" -inf.0 "-inf.0" "-Inf.0")
2367 ;; Exact ratios
2368 (test-numeric-syntax "1/2" (/ 1 2))
2369 (test-numeric-syntax "#e1/2" (/ 1 2) "1/2")
2370 (test-numeric-syntax "10/2" 5 "5")
2371 (test-numeric-syntax "-1/2" (- (/ 1 2)))
2372 (test-numeric-syntax "0/10" 0 "0")
2373 (test-numeric-syntax "#e0/10" 0 "0")
2374 (test-numeric-syntax "#i3/2" (/ 3.0 2.0) "1.5")
2375 ;; Exact complex
2376 (test-numeric-syntax "1+2i" (make-rectangular 1 2))
2377 (test-numeric-syntax "1+2I" (make-rectangular 1 2) "1+2i")
2378 (test-numeric-syntax "1-2i" (make-rectangular 1 -2))
2379 (test-numeric-syntax "-1+2i" (make-rectangular -1 2))
2380 (test-numeric-syntax "-1-2i" (make-rectangular -1 -2))
2381 (test-numeric-syntax "+i" (make-rectangular 0 1) "+i" "+1i" "0+i" "0+1i")
2382 (test-numeric-syntax "0+i" (make-rectangular 0 1) "+i" "+1i" "0+i" "0+1i")
2383 (test-numeric-syntax "0+1i" (make-rectangular 0 1) "+i" "+1i" "0+i" "0+1i")
2384 (test-numeric-syntax "-i" (make-rectangular 0 -1) "-i" "-1i" "0-i" "0-1i")
2385 (test-numeric-syntax "0-i" (make-rectangular 0 -1) "-i" "-1i" "0-i" "0-1i")
2386 (test-numeric-syntax "0-1i" (make-rectangular 0 -1) "-i" "-1i" "0-i" "0-1i")
2387 (test-numeric-syntax "+2i" (make-rectangular 0 2) "2i" "+2i" "0+2i")
2388 (test-numeric-syntax "-2i" (make-rectangular 0 -2) "-2i" "0-2i")
2389 ;; Decimal-notation complex numbers (rectangular notation)
2390 (test-numeric-syntax "1.0+2i" (make-rectangular 1.0 2) "1.0+2.0i" "1.0+2i" "1.+2i" "1.+2.i")
2391 (test-numeric-syntax "1+2.0i" (make-rectangular 1 2.0) "1.0+2.0i" "1+2.0i" "1.+2.i" "1+2.i")
2392 (test-numeric-syntax "1e2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
2393 (test-numeric-syntax "1s2+1.0i" (make-rectangular 100.0 1.0) "100.0+1.0i" "100.+1.i")
2394 (test-numeric-syntax "1.0+1e2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
2395 (test-numeric-syntax "1.0+1s2i" (make-rectangular 1.0 100.0) "1.0+100.0i" "1.+100.i")
2396 ;; Fractional complex numbers (rectangular notation)
2397 (test-numeric-syntax "1/2+3/4i" (make-rectangular (/ 1 2) (/ 3 4)))
2398 ;; Mixed fractional/decimal notation complex numbers (rectangular notation)
2399 (test-numeric-syntax "0.5+3/4i" (make-rectangular 0.5 (/ 3 4))
2400   "0.5+0.75i" ".5+.75i" "0.5+3/4i" ".5+3/4i" "500.0e-3+750.0e-3i")
2401 ;; Complex NaN, Inf (rectangular notation)
2402 ;;(test-numeric-syntax "+nan.0+nan.0i" (make-rectangular the-nan the-nan) "+NaN.0+NaN.0i") 
2403 (test-numeric-syntax "+inf.0+inf.0i" (make-rectangular +inf.0 +inf.0) "+Inf.0+Inf.0i")
2404 (test-numeric-syntax "-inf.0+inf.0i" (make-rectangular -inf.0 +inf.0) "-Inf.0+Inf.0i")
2405 (test-numeric-syntax "-inf.0-inf.0i" (make-rectangular -inf.0 -inf.0) "-Inf.0-Inf.0i")
2406 (test-numeric-syntax "+inf.0-inf.0i" (make-rectangular +inf.0 -inf.0) "+Inf.0-Inf.0i")
2407 ;; Complex numbers (polar notation)
2408 ;; Need to account for imprecision in write output.
2409 ;;(test-numeric-syntax "1@2" -0.416146836547142+0.909297426825682i "-0.416146836547142+0.909297426825682i")
2410 ;; Base prefixes
2411 (test-numeric-syntax "#x11" 17 "17")
2412 (test-numeric-syntax "#X11" 17 "17")
2413 (test-numeric-syntax "#d11" 11 "11")
2414 (test-numeric-syntax "#D11" 11 "11")
2415 (test-numeric-syntax "#o11" 9 "9")
2416 (test-numeric-syntax "#O11" 9 "9")
2417 (test-numeric-syntax "#b11" 3 "3")
2418 (test-numeric-syntax "#B11" 3 "3")
2419 (test-numeric-syntax "#o7" 7 "7")
2420 (test-numeric-syntax "#xa" 10 "10")
2421 (test-numeric-syntax "#xA" 10 "10")
2422 (test-numeric-syntax "#xf" 15 "15")
2423 (test-numeric-syntax "#x-10" -16 "-16")
2424 (test-numeric-syntax "#d-10" -10 "-10")
2425 (test-numeric-syntax "#o-10" -8 "-8")
2426 (test-numeric-syntax "#b-10" -2 "-2")
2427 ;; Combination of prefixes
2428 (test-numeric-syntax "#e#x10" 16 "16")
2429 (test-numeric-syntax "#i#x10" 16.0 "16.0" "16.")
2430 (test-numeric-syntax "#x#i10" 16.0 "16.0" "16.")
2431 (test-numeric-syntax "#i#x1/10" 0.0625 "0.0625")
2432 (test-numeric-syntax "#x#i1/10" 0.0625 "0.0625")
2433 ;; (Attempted) decimal notation with base prefixes
2434 (test-numeric-syntax "#d1." 1.0 "1.0" "1.")
2435 (test-numeric-syntax "#d.1" 0.1 "0.1" ".1" "100.0e-3")
2436 (test-numeric-syntax "#x1e2" 482 "482")
2437 (test-numeric-syntax "#d1e2" 100.0 "100.0" "100.")
2438 ;; Fractions with prefixes
2439 (test-numeric-syntax "#x10/2" 8 "8")
2440 (test-numeric-syntax "#x11/2" (/ 17 2) "17/2")
2441 (test-numeric-syntax "#d11/2" (/ 11 2) "11/2")
2442 (test-numeric-syntax "#o11/2" (/ 9 2) "9/2")
2443 (test-numeric-syntax "#b11/10" (/ 3 2) "3/2")
2444 ;; Complex numbers with prefixes
2445 ;;(test-numeric-syntax "#x10+11i" (make-rectangular 16 17) "16+17i")
2446 (test-numeric-syntax "#d1.0+1.0i" (make-rectangular 1.0 1.0) "1.0+1.0i" "1.+1.i")
2447 (test-numeric-syntax "#d10+11i" (make-rectangular 10 11) "10+11i")
2448 ;;(test-numeric-syntax "#o10+11i" (make-rectangular 8 9) "8+9i")
2449 ;;(test-numeric-syntax "#b10+11i" (make-rectangular 2 3) "2+3i")
2450 ;;(test-numeric-syntax "#e1.0+1.0i" (make-rectangular 1 1) "1+1i" "1+i")
2451 ;;(test-numeric-syntax "#i1.0+1.0i" (make-rectangular 1.0 1.0) "1.0+1.0i" "1.+1.i")
2452 
2453 (define-syntax test-precision
2454   (syntax-rules ()
2455     ((test-round-trip str alt ...)
2456      (let* ((n (string->number str))
2457             (str2 (number->string n))
2458             (accepted (list str alt ...))
2459             (ls (member str2 accepted)))
2460        (test-assert (string-append "(member? " str2 " "
2461                                    (let ((out (open-output-string)))
2462                                      (write accepted out)
2463                                      (get-output-string out))
2464                                    ")")
2465          (pair? ls))
2466        (when (pair? ls)
2467          (test-assert (string-append "(eqv?: " str " " str2 ")")
2468            (eqv? n (string->number (car ls)))))))))
2469 
2470 (test-precision "-1.7976931348623157e+308" "-inf.0")
2471 (test-precision "4.940656458412465e-324" "4.94065645841247e-324" "5.0e-324" "0.0")
2472 (test-precision "9.881312916824931e-324" "9.88131291682493e-324" "1.0e-323" "0.0")
2473 (test-precision "1.48219693752374e-323" "1.5e-323" "0.0")
2474 (test-precision "1.976262583364986e-323" "1.97626258336499e-323" "2.0e-323" "0.0")
2475 (test-precision "2.470328229206233e-323" "2.47032822920623e-323" "2.5e-323" "0.0")
2476 (test-precision "2.420921664622108e-322" "2.42092166462211e-322" "2.4e-322" "0.0")
2477 (test-precision "2.420921664622108e-320" "2.42092166462211e-320" "2.421e-320" "0.0")
2478 (test-precision "1.4489974452386991" "1.4489975")
2479 (test-precision "0.14285714285714282" "0.14285714285714288" "0.14285715")
2480 (test-precision "1.7976931348623157e+308" "+inf.0")
2481 
2482 (test-end)
2483 
2484 (test-end)
2485 
2486 (test-begin "6.14 System interface")
2487 
2488 ;; 6.14 System interface
2489 
2490 ;; (test "/usr/local/bin:/usr/bin:/bin" (get-environment-variable "PATH"))
2491 
2492 (test #t (string? (get-environment-variable "PATH")))
2493 
2494 ;; (test '(("USER" . "root") ("HOME" . "/")) (get-environment-variables))
2495 
2496 (let ((env (get-environment-variables)))
2497   (define (env-pair? x)
2498     (and (pair? x) (string? (car x)) (string? (cdr x))))
2499   (define (all? pred ls)
2500     (or (null? ls) (and (pred (car ls)) (all? pred (cdr ls)))))
2501   (test #t (list? env))
2502   (test #t (all? env-pair? env)))
2503 
2504 (test #t (list? (command-line)))
2505 
2506 (test #t (real? (current-second)))
2507 (test #t (inexact? (current-second)))
2508 (test #t (exact? (current-jiffy)))
2509 (test #t (exact? (jiffies-per-second)))
2510 
2511 (test #t (list? (features)))
2512 (test #t (and (memq 'r7rs (features)) #t))
2513 
2514 (test #t (file-exists? "."))
2515 (test #f (file-exists? " no such file "))
2516 
2517 (test #t (file-error?
2518           (guard (exn (else exn))
2519             (delete-file " no such file "))))
2520 
2521 (test-end)
2522 
2523 (test-end)