|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-suite test-r4rs) |
|
:use-module (test-suite lib) |
|
:use-module (test-suite guile-test)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define cur-section '())(define errs '()) |
|
|
|
(define SECTION (lambda args |
|
(set! cur-section args) |
|
(define record-error (lambda (e) (set! errs (cons (list cur-section e) errs)))) |
|
(define (report-errs) |
|
|
|
(define test |
|
(lambda (expect fun . args) |
|
(let ((res (if (procedure? fun) (apply fun args) (car args)))) |
|
(with-test-prefix cur-section |
|
(pass-if (call-with-output-string (lambda (port) |
|
(write (cons fun args) port))) |
|
(equal? expect res)))))) |
|
|
|
|
|
(SECTION 2 1) |
|
'(+ - ... !.. $.+ %.- &.! *.: /:. :+. <-. =. >. ?. ~. _. ^.) |
|
|
|
(SECTION 3 4) |
|
(define disjoint-type-functions |
|
(list boolean? char? null? number? pair? procedure? string? symbol? vector?)) |
|
(define type-examples |
|
(list |
|
#t #f #\a '() 9739 '(test) (lambda () #f) car "test" "" 'test |
|
'#() ' |
|
(define type-matrix |
|
(map (lambda (x) |
|
(let ((t (map (lambda (f) (f x)) disjoint-type-functions))) |
|
t)) |
|
type-examples)) |
|
(for-each (lambda (object row) |
|
(let ((count (apply + (map (lambda (elt) (if elt 1 0)) |
|
row)))) |
|
(pass-if (call-with-output-string |
|
(lambda (port) |
|
(display "object recognized by only one predicate: " |
|
port) |
|
(display object port))) |
|
(= count 1)))) |
|
type-examples |
|
type-matrix) |
|
|
|
(SECTION 4 1 2) |
|
(test '(quote a) 'quote (quote 'a)) |
|
(test '(quote a) 'quote ''a) |
|
(SECTION 4 1 3) |
|
(test 12 (if #f + *) 3 4) |
|
(SECTION 4 1 4) |
|
(test 8 (lambda (x) (+ x x)) 4) |
|
(define reverse-subtract |
|
(lambda (x y) (- y x))) |
|
(test 3 reverse-subtract 7 10) |
|
(define add4 |
|
(let ((x 4)) |
|
(lambda (y) (+ x y)))) |
|
(test 10 add4 6) |
|
(test '(3 4 5 6) (lambda x x) 3 4 5 6) |
|
(test '(5 6) (lambda (x y . z) z) 3 4 5 6) |
|
(SECTION 4 1 5) |
|
(test 'yes 'if (if (> 3 2) 'yes 'no)) |
|
(test 'no 'if (if (> 2 3) 'yes 'no)) |
|
(test '1 'if (if (> 3 2) (- 3 2) (+ 3 2))) |
|
(SECTION 4 1 6) |
|
(define x 2) |
|
(test 3 'define (+ x 1)) |
|
(set! x 4) |
|
(test 5 'set! (+ x 1)) |
|
(SECTION 4 2 1) |
|
(test 'greater 'cond (cond ((> 3 2) 'greater) |
|
((< 3 2) 'less))) |
|
(test 'equal 'cond (cond ((> 3 3) 'greater) |
|
((< 3 3) 'less) |
|
(else 'equal))) |
|
(test 2 'cond (cond ((assv 'b '((a 1) (b 2))) => cadr) |
|
(else #f))) |
|
(test 'composite 'case (case (* 2 3) |
|
((2 3 5 7) 'prime) |
|
((1 4 6 8 9) 'composite))) |
|
(test 'consonant 'case (case (car '(c d)) |
|
((a e i o u) 'vowel) |
|
((w y) 'semivowel) |
|
(else 'consonant))) |
|
(test #t 'and (and (= 2 2) (> 2 1))) |
|
(test |
|
(test '(f g) 'and (and 1 2 'c '(f g))) |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test '(b c) 'or (or (memq 'b '(a b c)) (+ 3 0))) |
|
(SECTION 4 2 2) |
|
(test 6 'let (let ((x 2) (y 3)) (* x y))) |
|
(test 35 'let (let ((x 2) (y 3)) (let ((x 7) (z (+ x y))) (* z x)))) |
|
(test 70 'let* (let ((x 2) (y 3)) (let* ((x 7) (z (+ x y))) (* z x)))) |
|
(test #t 'letrec (letrec ((even? |
|
(lambda (n) (if (zero? n) |
|
(odd? |
|
(lambda (n) (if (zero? n) |
|
(even? 88))) |
|
(define x 34) |
|
(test 5 'let (let ((x 3)) (define x 5) x)) |
|
(test 34 'let x) |
|
(test 6 'let (let () (define x 6) x)) |
|
(test 34 'let x) |
|
(test 7 'let* (let* ((x 3)) (define x 7) x)) |
|
(test 34 'let* x) |
|
(test 8 'let* (let* () (define x 8) x)) |
|
(test 34 'let* x) |
|
(test 9 'letrec (letrec () (define x 9) x)) |
|
(test 34 'letrec x) |
|
(test 10 'letrec (letrec ((x 3)) (define x 10) x)) |
|
(test 34 'letrec x) |
|
(SECTION 4 2 3) |
|
(define x 0) |
|
(test 6 'begin (begin (set! x 5) (+ x 1))) |
|
(SECTION 4 2 4) |
|
(test ' |
|
(i 0 (+ i 1))) |
|
((= i 5) vec) |
|
(vector-set! vec i i))) |
|
(test 25 'do (let ((x '(1 3 5 7 9))) |
|
(do ((x x (cdr x)) |
|
(sum 0 (+ sum (car x)))) |
|
((null? x) sum)))) |
|
(test 1 'let (let foo () 1)) |
|
(test '((6 1 3) (-5 -2)) 'let |
|
(let loop ((numbers '(3 -2 1 6 -5)) |
|
(nonneg '()) |
|
(neg '())) |
|
(cond ((null? numbers) (list nonneg neg)) |
|
((negative? (car numbers)) |
|
(loop (cdr numbers) |
|
nonneg |
|
(cons (car numbers) neg))) |
|
(else |
|
(loop (cdr numbers) |
|
(cons (car numbers) nonneg) |
|
neg))))) |
|
(SECTION 4 2 6) |
|
(test '(list 3 4) 'quasiquote `(list ,(+ 1 2) 4)) |
|
(test '(list a (quote a)) 'quasiquote (let ((name 'a)) `(list ,name ',name))) |
|
(test '(a 3 4 5 6 b) 'quasiquote `(a ,(+ 1 2) ,@(map abs '(4 -5 6)) b)) |
|
(test '((foo 7) . cons) |
|
'quasiquote |
|
`((foo ,(- 10 3)) ,@(cdr '(c)) . ,(car '(cons)))) |
|
|
|
;;; sqt is defined here because not all implementations are required to |
|
;;; support it. |
|
(define (sqt x) |
|
(do ((i 0 (+ i 1))) |
|
((> (* i i) x) (- i 1)))) |
|
|
|
(test ' |
|
(test 5 'quasiquote `,(+ 2 3)) |
|
(test '(a `(b ,(+ 1 2) ,(foo 4 d) e) f) |
|
'quasiquote `(a `(b ,(+ 1 2) ,(foo ,(+ 1 3) d) e) f)) |
|
(test '(a `(b ,x ,'y d) e) 'quasiquote |
|
(let ((name1 'x) (name2 'y)) `(a `(b ,,name1 ,',name2 d) e))) |
|
(test '(list 3 4) 'quasiquote (quasiquote (list (unquote (+ 1 2)) 4))) |
|
(test '`(list ,(+ 1 2) 4) 'quasiquote '(quasiquote (list (unquote (+ 1 2)) 4))) |
|
(SECTION 5 2 1) |
|
(define add3 (lambda (x) (+ x 3))) |
|
(test 6 'define (add3 3)) |
|
(define first car) |
|
(test 1 'define (first '(1 2))) |
|
(SECTION 5 2 2) |
|
(test 45 'define |
|
(let ((x 5)) |
|
(define foo (lambda (y) (bar x y))) |
|
(define bar (lambda (a b) (+ (* a b) a))) |
|
(foo (+ x 3)))) |
|
(define x 34) |
|
(define (foo) (define x 5) x) |
|
(test 5 foo) |
|
(test 34 'define x) |
|
(define foo (lambda () (define x 5) x)) |
|
(test 5 foo) |
|
(test 34 'define x) |
|
(define (foo x) ((lambda () (define x 5) x)) x) |
|
(test 88 foo 88) |
|
(test 4 foo 4) |
|
(test 34 'define x) |
|
(SECTION 6 1) |
|
(test #f not #t) |
|
(test #f not 3) |
|
(test #f not (list 3)) |
|
(test #t not #f) |
|
(test #f not '()) |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(SECTION 6 2) |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(let ((p (lambda (x) x))) |
|
(test |
|
(define gen-counter |
|
(lambda () |
|
(let ((n 0)) |
|
(lambda () (set! n (+ n 1)) n)))) |
|
(let ((g (gen-counter))) (test |
|
(test |
|
(letrec ((f (lambda () (if (eqv? f g) 'f 'both))) |
|
(g (lambda () (if (eqv? f g) 'g 'both)))) |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(let ((x '(a))) (test #t eq? x x)) |
|
(let ((x ' |
|
(let ((x (lambda (x) x))) (test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(SECTION 6 3) |
|
(test '(a b c d e) 'dot '(a . (b . (c . (d . (e . ())))))) |
|
(define x (list 'a 'b 'c)) |
|
(define y x) |
|
(and list? (test |
|
(set-cdr! x 4) |
|
(test '(a . 4) 'set-cdr! x) |
|
(test |
|
(test '(a b c . d) 'dot '(a . (b . (c . d)))) |
|
(and list? (test #f list? y)) |
|
(and list? (let ((x (list 'a))) (set-cdr! x x) (test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test '(a) cons 'a '()) |
|
(test '((a) b c d) cons '(a) '(b c d)) |
|
(test '("a" b c) cons "a" '(b c)) |
|
(test '(a . 3) cons 'a 3) |
|
(test '((a b) . c) cons '(a b) 'c) |
|
|
|
(test 'a car '(a b c)) |
|
(test '(a) car '((a) b c d)) |
|
(test 1 car '(1 . 2)) |
|
|
|
(test '(b c d) cdr '((a) b c d)) |
|
(test 2 cdr '(1 . 2)) |
|
|
|
(test '(a 7 c) list 'a (+ 3 4) 'c) |
|
(test '() list) |
|
|
|
(test 3 length '(a b c)) |
|
(test 3 length '(a (b) (c d e))) |
|
(test 0 length '()) |
|
|
|
(test '(x y) append '(x) '(y)) |
|
(test '(a b c d) append '(a) '(b c d)) |
|
(test '(a (b) (c)) append '(a (b)) '((c))) |
|
(test '() append) |
|
(test '(a b c . d) append '(a b) '(c . d)) |
|
(test 'a append '() 'a) |
|
|
|
(test '(c b a) reverse '(a b c)) |
|
(test '((e (f)) d (b c) a) reverse '(a (b c) d (e (f)))) |
|
|
|
(test 'c list-ref '(a b c d) 2) |
|
|
|
(test '(a b c) memq 'a '(a b c)) |
|
(test '(b c) memq 'b '(a b c)) |
|
(test '#f memq 'a '(b c d)) |
|
(test ' |
|
(test '((a) c) member (list 'a) '(b (a) c)) |
|
(test '(101 102) memv 101 '(100 101 102)) |
|
|
|
(define e '((a 1) (b 2) (c 3))) |
|
(test '(a 1) assq 'a e) |
|
(test '(b 2) assq 'b e) |
|
(test |
|
(test |
|
(test '((a)) assoc (list 'a) '(((a)) ((b)) ((c)))) |
|
(test '(5 7) assv 5 '((2 3) (5 7) (11 13))) |
|
(SECTION 6 4) |
|
(test #t symbol? 'foo) |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(define char-standard-case char-upcase) |
|
(if (string=? (symbol->string 'A) "a") |
|
(set! char-standard-case char-downcase)) |
|
;;; Not for Guile |
|
;(test #t 'standard-case |
|
|
|
|
|
|
|
|
|
(define (str-copy s) |
|
(let ((v (make-string (string-length s)))) |
|
(do ((i (- (string-length v) 1) (- i 1))) |
|
((< i 0) v) |
|
(string-set! v i (string-ref s i))))) |
|
(define (string-standard-case s) |
|
(set! s (str-copy s)) |
|
(do ((i 0 (+ 1 i)) |
|
(sl (string-length s))) |
|
((>= i sl) s) |
|
(string-set! s i (char-standard-case (string-ref s i))))) |
|
|
|
|
|
|
|
(test "Malvina" symbol->string (string->symbol "Malvina")) |
|
|
|
|
|
|
|
(define x (string |
|
(define y (string->symbol x)) |
|
(string-set! x 0 |
|
(test "cb" 'string-set! x) |
|
(test "ab" symbol->string y) |
|
(test y string->symbol "ab") |
|
|
|
;;; Not for Guile |
|
;(test #t eq? 'mISSISSIppi 'mississippi) |
|
;(test #f 'string->symbol (eq? 'bitBlt (string->symbol "bitBlt"))) |
|
(test 'HeyJeffrey string->symbol (symbol->string 'HeyJeffrey)) |
|
|
|
(SECTION 6 5 5) |
|
(test #t number? 3) |
|
(test #t complex? 3) |
|
(test #t real? 3) |
|
(test #t rational? 3) |
|
(test #t integer? 3) |
|
|
|
(test #t exact? 3) |
|
(test #f inexact? 3) |
|
|
|
(test #t = 22 22 22) |
|
(test #t = 22 22) |
|
(test #f = 34 34 35) |
|
(test #f = 34 35) |
|
(test #t > 3 -6246) |
|
(test #f > 9 9 -2424) |
|
(test #t >= 3 -4 -6246) |
|
(test #t >= 9 9) |
|
(test #f >= 8 9) |
|
(test #t < -1 2 3 4 5 6 7 8) |
|
(test #f < -1 2 3 4 4 5 6 7) |
|
(test #t <= -1 2 3 4 5 6 7 8) |
|
(test #t <= -1 2 3 4 4 5 6 7) |
|
(test #f < 1 3 2) |
|
(test #f >= 1 3 2) |
|
|
|
(test #t zero? 0) |
|
(test #f zero? 1) |
|
(test #f zero? -1) |
|
(test #f zero? -100) |
|
(test #t positive? 4) |
|
(test #f positive? -4) |
|
(test #f positive? 0) |
|
(test #f negative? 4) |
|
(test #t negative? -4) |
|
(test #f negative? 0) |
|
(test #t odd? 3) |
|
(test #f odd? 2) |
|
(test #f odd? -4) |
|
(test #t odd? -1) |
|
(test #f even? 3) |
|
(test #t even? 2) |
|
(test #t even? -4) |
|
(test #f even? -1) |
|
|
|
(test 38 max 34 5 7 38 6) |
|
(test -24 min 3 5 5 330 4 -24) |
|
|
|
(test 7 + 3 4) |
|
(test '3 + 3) |
|
(test 0 +) |
|
(test 4 * 4) |
|
(test 1 *) |
|
|
|
(test -1 - 3 4) |
|
(test -3 - 3) |
|
(test 7 abs -7) |
|
(test 7 abs 7) |
|
(test 0 abs 0) |
|
|
|
(test 5 quotient 35 7) |
|
(test -5 quotient -35 7) |
|
(test -5 quotient 35 -7) |
|
(test 5 quotient -35 -7) |
|
(test 1 modulo 13 4) |
|
(test 1 remainder 13 4) |
|
(test 3 modulo -13 4) |
|
(test -1 remainder -13 4) |
|
(test -3 modulo 13 -4) |
|
(test 1 remainder 13 -4) |
|
(test -1 modulo -13 -4) |
|
(test -1 remainder -13 -4) |
|
(define (divtest n1 n2) |
|
(= n1 (+ (* n2 (quotient n1 n2)) |
|
(remainder n1 n2)))) |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test 4 gcd 0 4) |
|
(test 4 gcd -4 0) |
|
(test 4 gcd 32 -36) |
|
(test 0 gcd) |
|
(test 288 lcm 32 -36) |
|
(test 1 lcm) |
|
|
|
|
|
|
|
(define (test-inexact) |
|
(define f3.9 (string->number "3.9")) |
|
(define f4.0 (string->number "4.0")) |
|
(define f-3.25 (string->number "-3.25")) |
|
(define f.25 (string->number ".25")) |
|
(define f4.5 (string->number "4.5")) |
|
(define f3.5 (string->number "3.5")) |
|
(define f0.0 (string->number "0.0")) |
|
(define f0.8 (string->number "0.8")) |
|
(define f1.0 (string->number "1.0")) |
|
(define wto write-test-obj) |
|
(define dto display-test-obj) |
|
(define lto load-test-obj) |
|
(SECTION 6 5 5) |
|
(test |
|
(test |
|
(test f4.0 'max (max f3.9 4)) |
|
(test f4.0 'exact->inexact (exact->inexact 4)) |
|
(test (- f4.0) round (- f4.5)) |
|
(test (- f4.0) round (- f3.5)) |
|
(test (- f4.0) round (- f3.9)) |
|
(test f0.0 round f0.0) |
|
(test f0.0 round f.25) |
|
(test f1.0 round f0.8) |
|
(test f4.0 round f3.5) |
|
(test f4.0 round f4.5) |
|
(set! write-test-obj (list f.25 f-3.25)) |
|
(set! display-test-obj (list f.25 f-3.25)) |
|
(set! load-test-obj (list 'define 'foo (list 'quote write-test-obj))) |
|
(test #t call-with-output-file |
|
(data-file-name "tmp3") |
|
(lambda (test-file) |
|
(write-char #\; test-file) |
|
(display write-test-obj test-file) |
|
(newline test-file) |
|
(write load-test-obj test-file) |
|
(output-port? test-file))) |
|
(check-test-file (data-file-name "tmp3")) |
|
(set! write-test-obj wto) |
|
(set! display-test-obj dto) |
|
(set! load-test-obj lto) |
|
(let ((x (string->number "4195835.0")) |
|
(y (string->number "3145727.0"))) |
|
(test #t 'pentium-fdiv-bug (> f1.0 (- x (* (/ x y) y))))) |
|
(report-errs)) |
|
|
|
(define (test-bignum) |
|
(define tb |
|
(lambda (n1 n2) |
|
(= n1 (+ (* n2 (quotient n1 n2)) |
|
(remainder n1 n2))))) |
|
(SECTION 6 5 5) |
|
(test 0 modulo -2177452800 86400) |
|
(test 0 modulo 2177452800 -86400) |
|
(test 0 modulo 2177452800 86400) |
|
(test 0 modulo -2177452800 -86400) |
|
(test |
|
(test |
|
(SECTION 6 5 6) |
|
(test 281474976710655 string->number "281474976710655") |
|
(test "281474976710655" number->string 281474976710655) |
|
(report-errs)) |
|
|
|
(SECTION 6 5 6) |
|
(test "0" number->string 0) |
|
(test "100" number->string 100) |
|
(test "100" number->string 256 16) |
|
(test 100 string->number "100") |
|
(test 256 string->number "100" 16) |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(SECTION 6 6) |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
|
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(SECTION 6 7) |
|
(test |
|
(test |
|
(define f (make-string 3 |
|
(test "?**" 'string-set! (begin (string-set! f 0 #\?) f)) |
|
(test "abc" string #\a #\b #\c) |
|
(test "" string) |
|
(test 3 string-length "abc") |
|
(test #\a string-ref "abc" 0) |
|
(test #\c string-ref "abc" 2) |
|
(test 0 string-length "") |
|
(test "" substring "ab" 0 0) |
|
(test "" substring "ab" 1 1) |
|
(test "" substring "ab" 2 2) |
|
(test "a" substring "ab" 0 1) |
|
(test "b" substring "ab" 1 2) |
|
(test "ab" substring "ab" 0 2) |
|
(test "foobar" string-append "foo" "bar") |
|
(test "foo" string-append "foo") |
|
(test "foo" string-append "foo" "") |
|
(test "foo" string-append "" "foo") |
|
(test "" string-append) |
|
(test "" make-string 0) |
|
(test #t string=? "" "") |
|
(test #f string<? "" "") |
|
(test #f string>? "" "") |
|
(test #t string<=? "" "") |
|
(test #t string>=? "" "") |
|
(test #t string-ci=? "" "") |
|
(test #f string-ci<? "" "") |
|
(test #f string-ci>? "" "") |
|
(test #t string-ci<=? "" "") |
|
(test #t string-ci>=? "" "") |
|
|
|
(test #f string=? "A" "B") |
|
(test #f string=? "a" "b") |
|
(test #f string=? "9" "0") |
|
(test #t string=? "A" "A") |
|
|
|
(test #t string<? "A" "B") |
|
(test #t string<? "a" "b") |
|
(test #f string<? "9" "0") |
|
(test #f string<? "A" "A") |
|
|
|
(test #f string>? "A" "B") |
|
(test #f string>? "a" "b") |
|
(test #t string>? "9" "0") |
|
(test #f string>? "A" "A") |
|
|
|
(test #t string<=? "A" "B") |
|
(test #t string<=? "a" "b") |
|
(test #f string<=? "9" "0") |
|
(test #t string<=? "A" "A") |
|
|
|
(test #f string>=? "A" "B") |
|
(test #f string>=? "a" "b") |
|
(test #t string>=? "9" "0") |
|
(test #t string>=? "A" "A") |
|
|
|
(test #f string-ci=? "A" "B") |
|
(test #f string-ci=? "a" "B") |
|
(test #f string-ci=? "A" "b") |
|
(test #f string-ci=? "a" "b") |
|
(test #f string-ci=? "9" "0") |
|
(test #t string-ci=? "A" "A") |
|
(test #t string-ci=? "A" "a") |
|
|
|
(test #t string-ci<? "A" "B") |
|
(test #t string-ci<? "a" "B") |
|
(test #t string-ci<? "A" "b") |
|
(test #t string-ci<? "a" "b") |
|
(test #f string-ci<? "9" "0") |
|
(test #f string-ci<? "A" "A") |
|
(test #f string-ci<? "A" "a") |
|
|
|
(test #f string-ci>? "A" "B") |
|
(test #f string-ci>? "a" "B") |
|
(test #f string-ci>? "A" "b") |
|
(test #f string-ci>? "a" "b") |
|
(test #t string-ci>? "9" "0") |
|
(test #f string-ci>? "A" "A") |
|
(test #f string-ci>? "A" "a") |
|
|
|
(test #t string-ci<=? "A" "B") |
|
(test #t string-ci<=? "a" "B") |
|
(test #t string-ci<=? "A" "b") |
|
(test #t string-ci<=? "a" "b") |
|
(test #f string-ci<=? "9" "0") |
|
(test #t string-ci<=? "A" "A") |
|
(test #t string-ci<=? "A" "a") |
|
|
|
(test #f string-ci>=? "A" "B") |
|
(test #f string-ci>=? "a" "B") |
|
(test #f string-ci>=? "A" "b") |
|
(test #f string-ci>=? "a" "b") |
|
(test #t string-ci>=? "9" "0") |
|
(test #t string-ci>=? "A" "A") |
|
(test #t string-ci>=? "A" "a") |
|
(SECTION 6 8) |
|
(test #t vector? ' |
|
(test |
|
(test '#(a b c) vector 'a 'b 'c) |
|
(test '#() vector) |
|
(test 3 vector-length ' |
|
(test 0 vector-length '#()) |
|
(test 8 vector-ref ' |
|
(test '#(0 ("Sue" "Sue") "Anna") 'vector-set |
|
(let ((vec (vector 0 '(2 2 2 2) "Anna"))) |
|
(vector-set! vec 1 '("Sue" "Sue")) |
|
vec)) |
|
(test '#(hi hi) make-vector 2 'hi) |
|
(test '#() make-vector 0) |
|
(test ' |
|
(SECTION 6 9) |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test |
|
(test 7 apply + (list 3 4)) |
|
(test 7 apply (lambda (a b) (+ a b)) (list 3 4)) |
|
(test 17 apply + 10 (list 3 4)) |
|
(test '() apply list '()) |
|
(define compose (lambda (f g) (lambda args (f (apply g args))))) |
|
(test 30 (compose sqt *) 12 75) |
|
|
|
(test '(b e h) map cadr '((a b) (d e) (g h))) |
|
(test '(5 7 9) map + '(1 2 3) '(4 5 6)) |
|
(test ' |
|
(let ((v (make-vector 5))) |
|
(for-each (lambda (i) (vector-set! v i (* i i))) |
|
'(0 1 2 3 4)) |
|
v)) |
|
(test -3 call-with-current-continuation |
|
(lambda (exit) |
|
(for-each (lambda (x) (if (negative? x) (exit x))) |
|
'(54 0 37 -3 245 19)) |
|
|
|
(define list-length |
|
(lambda (obj) |
|
(call-with-current-continuation |
|
(lambda (return) |
|
(letrec ((r (lambda (obj) (cond ((null? obj) 0) |
|
((pair? obj) (+ (r (cdr obj)) 1)) |
|
(else (return |
|
(r obj)))))) |
|
(test 4 list-length '(1 2 3 4)) |
|
(test #f list-length '(a b . c)) |
|
(test '() map cadr '()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (next-leaf-generator obj eot) |
|
(letrec ((return |
|
(cont (lambda (x) |
|
(recur obj) |
|
(set! cont (lambda (x) (return eot))) |
|
(cont |
|
(recur (lambda (obj) |
|
(if (pair? obj) |
|
(for-each recur obj) |
|
(call-with-current-continuation |
|
(lambda (c) |
|
(set! cont c) |
|
(return obj))))))) |
|
(lambda () (call-with-current-continuation |
|
(lambda (ret) (set! return ret) (cont |
|
(define (leaf-eq? x y) |
|
(let* ((eot (list 'eot)) |
|
(xf (next-leaf-generator x eot)) |
|
(yf (next-leaf-generator y eot))) |
|
(letrec ((loop (lambda (x y) |
|
(cond ((not (eq? x y)) #f) |
|
((eq? eot x) #t) |
|
(else (loop (xf) (yf))))))) |
|
(loop (xf) (yf))))) |
|
(define (test-cont) |
|
(SECTION 6 9) |
|
(test #t leaf-eq? '(a (b (c))) '((a) b c)) |
|
(test #f leaf-eq? '(a (b (c))) '((a) b c d)) |
|
(report-errs)) |
|
|
|
;;; Test Optional R4RS DELAY syntax and FORCE procedure |
|
(define (test-delay) |
|
(SECTION 6 9) |
|
(test 3 'delay (force (delay (+ 1 2)))) |
|
(test '(3 3) 'delay (let ((p (delay (+ 1 2)))) |
|
(list (force p) (force p)))) |
|
(test 2 'delay (letrec ((a-stream |
|
(letrec ((next (lambda (n) |
|
(cons n (delay (next (+ n 1))))))) |
|
(next 0))) |
|
(head car) |
|
(tail (lambda (stream) (force (cdr stream))))) |
|
(head (tail (tail a-stream))))) |
|
(letrec ((count 0) |
|
(p (delay (begin (set! count (+ count 1)) |
|
(if (> count x) |
|
count |
|
(force p))))) |
|
(x 5)) |
|
(test 6 force p) |
|
(set! x 10) |
|
(test 6 force p)) |
|
(test 3 'force |
|
(letrec ((p (delay (if c 3 (begin (set! c |
|
(c |
|
(force p))) |
|
(report-errs)) |
|
|
|
(SECTION 6 10 1) |
|
(test |
|
(test |
|
(test |
|
(define this-file (open-input-file (test-file-name "r4rs.test"))) |
|
(test |
|
(SECTION 6 10 2) |
|
(test |
|
(test |
|
(read this-file) |
|
(test '(define cur-section '()) read this-file) |
|
(test |
|
(test '(define errs '()) read this-file) |
|
(close-input-port this-file) |
|
(close-input-port this-file) |
|
(define (check-test-file name) |
|
(define test-file (open-input-file name)) |
|
(test |
|
(call-with-input-file |
|
name |
|
(lambda (test-file) |
|
(test load-test-obj read test-file) |
|
(test |
|
(test |
|
(input-port? test-file)))) |
|
(test |
|
(test display-test-obj read test-file) |
|
(test load-test-obj read test-file) |
|
(close-input-port test-file)) |
|
(SECTION 6 10 3) |
|
(define write-test-obj |
|
'(#t #f #\a () 9739 -3 . #((test) "te \" \" st" "" test #() b c))) |
|
(define display-test-obj |
|
'( |
|
(define load-test-obj |
|
(list 'define 'foo (list 'quote write-test-obj))) |
|
(test #t call-with-output-file |
|
(data-file-name "tmp1") |
|
(lambda (test-file) |
|
(write-char #\; test-file) |
|
(display write-test-obj test-file) |
|
(newline test-file) |
|
(write load-test-obj test-file) |
|
(output-port? test-file))) |
|
(check-test-file (data-file-name "tmp1")) |
|
|
|
(define test-file (open-output-file (data-file-name "tmp2"))) |
|
(write-char #\; test-file) |
|
(display write-test-obj test-file) |
|
(newline test-file) |
|
(write load-test-obj test-file) |
|
(test #t output-port? test-file) |
|
(close-output-port test-file) |
|
(check-test-file (data-file-name "tmp2")) |
|
(define (test-sc4) |
|
(SECTION 6 7) |
|
(test '( |
|
(test '() string->list "") |
|
(test "1\\\"" list->string '( |
|
(test "" list->string '()) |
|
(SECTION 6 8) |
|
(test '(dah dah didah) vector->list '#(dah dah didah)) |
|
(test '() vector->list '#()) |
|
(test ' |
|
(test '#() list->vector '()) |
|
(SECTION 6 10 4) |
|
(load (data-file-name "tmp1")) |
|
(test write-test-obj 'load foo) |
|
(report-errs)) |
|
|
|
(report-errs) |
|
(if (and (string->number "0.0") (inexact? (string->number "0.0"))) |
|
(test-inexact)) |
|
|
|
(let ((n (string->number "281474976710655"))) |
|
(if (and n (exact? n)) |
|
(test-bignum))) |
|
(test-cont) |
|
(test-sc4) |
|
(test-delay) |
|
"last item in file" |
|
|
|
(delete-file (data-file-name "tmp1")) |
|
(delete-file (data-file-name "tmp2")) |
|
(delete-file (data-file-name "tmp3")) |
|
|