Debian Codegraph @

test/include/r7rs-tests.scm

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