📄 r4rstest.scm
字号:
;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 2000, 2003 Free Software Foundation, Inc.;;;; This program is free software; you can redistribute it and/or modify it;; under the terms of the GNU General Public License as published by the;; Free Software Foundation; either version 2, or (at your option) any;; later version.;;;; This program is distributed in the hope that it will be useful,;; but WITHOUT ANY WARRANTY; without even the implied warranty of;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the;; GNU General Public License for more details.;;;; To receive a copy of the GNU General Public License, write to the;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,;; Boston, MA 02111-1307, USA; or view;; http://swissnet.ai.mit.edu/~jaffer/GPL.html;;;; "r4rstest.scm" Test correctness of scheme implementations.;;; Author: Aubrey Jaffer;;; This includes examples from;;; William Clinger and Jonathan Rees, editors.;;; Revised^4 Report on the Algorithmic Language Scheme;;; and the IEEE specification.;;; The input tests read this file expecting it to be named "r4rstest.scm".;;; Files `tmp1', `tmp2' and `tmp3' will be created in the course of running;;; these tests. You may need to delete them in order to run;;; "r4rstest.scm" more than once.;;; There are three optional tests:;;; (TEST-CONT) tests multiple returns from call-with-current-continuation;;;;;; (TEST-SC4) tests procedures required by R4RS but not by IEEE;;;;;; (TEST-DELAY) tests DELAY and FORCE, which are not required by;;; either standard.;;; If you are testing a R3RS version which does not have `list?' do:;;; (define list? #f);;; send corrections or additions to agj @ alum.mit.edu(define cur-section '())(define errs '())(define SECTION (lambda args (display "SECTION") (write args) (newline) (set! cur-section args) #t))(define record-error (lambda (e) (set! errs (cons (list cur-section e) errs))))(define test (lambda (expect fun . args) (write (cons fun args)) (display " ==> ") ((lambda (res) (write res) (newline) (cond ((not (equal? expect res)) (record-error (list res expect (cons fun args))) (display " BUT EXPECTED ") (write expect) (newline) #f) (else #t))) (if (procedure? fun) (apply fun args) (car args)))))(define (report-errs) (newline) (if (null? errs) (display "Passed all tests") (begin (display "errors were:") (newline) (display "(SECTION (got expected (call)))") (newline) (for-each (lambda (l) (write l) (newline)) errs))) (newline))(SECTION 2 1);; test that all symbol characters are supported.'(+ - ... !.. $.+ %.- &.! *.: /:. :+. <-. =. >. ?. ~. _. ^.)(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) record-error "test" "" 'test '#() '#(a b c) ))(define i 1)(for-each (lambda (x) (display (make-string i #\ )) (set! i (+ 3 i)) (write x) (newline)) disjoint-type-functions)(define type-matrix (map (lambda (x) (let ((t (map (lambda (f) (f x)) disjoint-type-functions))) (write t) (write x) (newline) t)) type-examples))(set! i 0)(define j 0)(for-each (lambda (x y) (set! j (+ 1 j)) (set! i 0) (for-each (lambda (f) (set! i (+ 1 i)) (cond ((and (= i j)) (cond ((not (f x)) (test #t f x)))) ((f x) (test #f f x))) (cond ((and (= i j)) (cond ((not (f y)) (test #t f y)))) ((f y) (test #f f y)))) disjoint-type-functions)) (list #t #\a '() 9739 '(test) record-error "test" 'car '#(a b c)) (list #f #\newline '() -3252 '(t . t) car "" 'nil '#()))(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 #f 'and (and (= 2 2) (< 2 1)))(test '(f g) 'and (and 1 2 'c '(f g)))(test #t 'and (and))(test #t 'or (or (= 2 2) (> 2 1)))(test #t 'or (or (= 2 2) (< 2 1)))(test #f 'or (or #f #f #f))(test #f 'or (or))(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) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (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)(define (s x) (if x (let () (set! s x) (set! x s))))(SECTION 4 2 3)(define x 0)(test 6 'begin (begin (set! x (begin (begin 5))) (begin ((begin +) (begin x) (begin (begin 1))))))(SECTION 4 2 4)(test '#(0 1 2 3 4) 'do (do ((vec (make-vector 5)) (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)))));;From: Allegro Petrofsky <Allegro@Petrofsky.Berkeley.CA.US>(test -1 'let (let ((f -)) (let f ((n (f 1))) n)))(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 '#(10 5 2 4 3 8) 'quasiquote `#(10 5 ,(sqt 4) ,@(map sqt '(16 9)) 8))(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)))(define old-+ +)(begin (begin (begin) (begin (begin (begin) (define + (lambda (x y) (list y x))) (begin))) (begin)) (begin) (begin (begin (begin) (test '(3 6) add3 6) (begin))))(set! + old-+)(test 9 add3 6)(begin)(begin (begin))(begin (begin (begin (begin))))(SECTION 5 2 2)#;(test 45 'define (let ((x 5)) (begin (begin (begin) (begin (begin (begin) (define foo (lambda (y) (bar x y))) (begin))) (begin)) (begin) (begin) (begin (define bar (lambda (a b) (+ (* a b) a)))) (begin)) (begin) (begin (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)(test 99 'internal-define (letrec ((foo (lambda (arg) (or arg (and (procedure? foo) (foo 99)))))) (define bar (foo #f)) (foo #f)))(test 77 'internal-define (letrec ((foo 77) (bar #f) (retfoo (lambda () foo))) (define baz (retfoo)) (retfoo)))(SECTION 6 1)(test #f not #t)(test #f not 3)(test #f not (list 3))(test #t not #f)(test #f not '())(test #f not (list))(test #f not 'nil);(test #t boolean? #f);(test #f boolean? 0);(test #f boolean? '())(SECTION 6 2)(test #t eqv? 'a 'a)(test #f eqv? 'a 'b)(test #t eqv? 2 2)(test #t eqv? '() '())(test #t eqv? '10000 '10000)(test #f eqv? (cons 1 2)(cons 1 2))(test #f eqv? (lambda () 1) (lambda () 2))(test #f eqv? #f 'nil)(let ((p (lambda (x) x))) (test #t eqv? p p))(define gen-counter (lambda () (let ((n 0)) (lambda () (set! n (+ n 1)) n))))(let ((g (gen-counter))) (test #t eqv? g g))(test #f eqv? (gen-counter) (gen-counter))(letrec ((f (lambda () (if (eqv? f g) 'f 'both))) (g (lambda () (if (eqv? f g) 'g 'both)))) (test #f eqv? f g))(test #t eq? 'a 'a)(test #f eq? (list 'a) (list 'a))(test #t eq? '() '())(test #t eq? car car)(let ((x '(a))) (test #t eq? x x))(let ((x '#())) (test #t eq? x x))(let ((x (lambda (x) x))) (test #t eq? x x))(define test-eq?-eqv?-agreement (lambda (obj1 obj2) (cond ((eq? (eq? obj1 obj2) (eqv? obj1 obj2))) (else (record-error (list #f #t (list 'test-eq?-eqv?-agreement obj1 obj2))) (display "eqv? and eq? disagree about ") (write obj1) (display #\ ) (write obj2) (newline)))))(test-eq?-eqv?-agreement '#f '#f)(test-eq?-eqv?-agreement '#t '#t)(test-eq?-eqv?-agreement '#t '#f)(test-eq?-eqv?-agreement '(a) '(a))(test-eq?-eqv?-agreement '(a) '(b))(test-eq?-eqv?-agreement car car)(test-eq?-eqv?-agreement car cdr)(test-eq?-eqv?-agreement (list 'a) (list 'a))(test-eq?-eqv?-agreement (list 'a) (list 'b))(test-eq?-eqv?-agreement '#(a) '#(a))(test-eq?-eqv?-agreement '#(a) '#(b))(test-eq?-eqv?-agreement "abc" "abc")(test-eq?-eqv?-agreement "abc" "abz")(test #t equal? 'a 'a)(test #t equal? '(a) '(a))(test #t equal? '(a (b) c) '(a (b) c))(test #t equal? "abc" "abc")(test #t equal? 2 2)(test #t equal? (make-vector 5 'a) (make-vector 5 'a))(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 #t list? y))(set-cdr! x 4)(test '(a . 4) 'set-cdr! x)(test #t eqv? x y)(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 #f 'list? (list? x))));(test #t pair? '(a . b));(test #t pair? '(a . 1));(test #t pair? '(a b c));(test #f pair? '());(test #f pair? '#(a b))(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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -