Debian Codegraph @

test-suite/tests/r7rs.test

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