; Verification code for myenv.scm and other my standard "environments" ; For Bigloo, you should evaluate or compile vmyenv-bigloo.scm, ; which contains a module declaration that includes the present file. ; For SCM, (load "myenv-scm.scm") as well as env.scm and util.scm ; before evaluating this file ; ; IMPORT ; appropriate prelude: myenv.scm, myenv-bigloo.scm, myenv-scm.scm ; depending on your system ; catch-error.scm -- for procedure, for-syntax ; env.scm ; util.scm ; ; $Id: vmyenv.scm,v 1.12 2004/11/03 22:45:29 oleg Exp $ (cerr nl "Verifying increment/decrement operators: inc, inc! etc..." nl) (let ((x 0)) (assert (= (inc x) 1)) (assert (= (dec x) -1)) (assert (begin (inc! x) (= x 1))) (assert (begin (dec! x) (dec! x) (zero? (inc x)))) ) (cerr nl "Verifying begin0..." nl) (let ((x 0)) (assert (= x (begin0 x))) (assert (= 0 (begin0 x 1))) (assert (= 1 (begin x 1))) (assert (= 0 (begin0 x (inc! x) x))) (assert (= 2 (begin x (inc! x) x))) ) (cerr nl "Verifying extended branching instructions..." nl) (let ((x 0)) (assert (= 2 (begin (when (zero? x) (inc! x)) (inc x)))) (assert (begin (when (zero? x) (inc! x)) (= x 1))) (whennot (zero? x) (dec! x)) (assert (zero? x)) (assert (zero? (begin (whennot (positive? x) (dec! x)) (inc x)))) (assert (= -1 (begin (whennot (negative? x) (inc! x)) x))) ) (cerr nl "Verifying assert..." nl) (let ((x 1)) (assert (eq? (positive? x) (assert (positive? x)))) (assert (eq? x (assert x report: x))) (assert (eq? x (assert 0 x))) (assert (failed? (assert (zero? x)))) (assert (failed? (assert (zero? x) report: "failure"))) (assert (failed? (assert (zero? x) report: "failure" x (+ x 1) "!"))) (assert (failed? (let ((y 2)) (assert (let ((z x)) (positive? z)) (positive? y) (zero? x) report: "failure" x (+ x 1))))) (assert (failed? (let ((y 2)) (assert (zero? x) (positive? (+ y x)))))) (assert (failed? (let ((y 2)) (assert (let ((z x)) (positive? z)) (positive? y) (zero? x) (positive? (+ y x)) )))) ) (cerr nl "Verifying values and let*-values" nl) (let () ; R5RS example (assert (= 5 (call-with-values (lambda () (values 4 5)) (lambda (a b) b)))) (assert (= 4 (call-with-values (lambda () (values 4)) (lambda (b) b)))) (assert (= 7 (call-with-values (lambda () (values)) (lambda () 7)))) (assert (= 140 (call-with-values (lambda () (values 4 5 7)) *))) ; R5RS example ;(call-with-values * -) (assert (= -1 (call-with-values (lambda () (values (*))) -))) ; let*-values ; On some system, pp (pretty-printer) can print out closures (pp (lambda () (let*-values (((a) 1) ((b) 2)) (+ a b)))) (assert (= 3 (let*-values (((a) 1) ((b) 2)) (+ a b)))) ; (assert (= 3 ; (let*-values ((a 1) (b 2)) (+ a b)))) (pp (lambda () (let*-values (((a) 1) ((b) 2) ((c d) (values 3 4))) (+ a b (* c d))))) (assert (= 15 (let*-values (((a) 1) ((b) 2) ((c d) (values 3 4))) (+ a b (* c d))))) (pp (lambda () (let*-values (((a) 1) ((b) 2) ((c d e) (values 1 2 3))) (+ a b (* c d e))))) (assert (= 63 (let*-values (((a) 1) ((b) 2) ((c d e) (values 3 4 5))) (+ a b (* c d e))))) (pp (lambda () (let*-values (((a) (values 1)) ((c d e) (values 3 4 5)) ((b) d)) (+ a b (* c d e))))) (assert (= 65 (let*-values (((a) 1) ((c d e) (values 3 4 5)) ((b) d)) (+ a b (* c d e))))) ; Two examples from MzScheme reference (let ((x 0)) (assert (= 5 (let*-values (((x) 5) ((y) x)) y)))) (let ((x 0)) (assert (= 0 (let*-values (((x y) (values 5 x))) y)))) ; Examples from SRFI-11 (let ((result (let*-values (((a b . c) (values 1 2 3 4))) (list a b c)))) (assert (equal? result '(1 2 (3 4))))) (let ((result (let*-values (((a . b) (values 1 2 3 4))) (list a b)))) (assert (equal? result '(1 (2 3 4))))) (let ((result (let*-values ((a (values 1 2 3 4))) (list a)))) (assert (equal? result '((1 2 3 4))))) (let ((result (let ((a 'a) (b 'b) (x 'x) (y 'y)) (let*-values (((a b) (values x y)) ((x y) (values a b))) (list a b x y))))) (assert (equal? result '(x y x y)))) ; An examples of 0,1,n values -> list (cond-expand ((not gambit) (let ((result (let*-values ((a (values)) (b (values 1)) (c 2) (d (values 3 4))) (list a b c d)))) (assert (equal? result '(() (1) (2) (3 4))))) ) (else #f)) ) (cerr nl "Verifying cond-expand: SRFI-0" nl) (let () (cond-expand (gambit (cout "Expanded in Gambit" nl)) (else #f)) (cond-expand (scm (cout "Expanded in SCM" nl)) (else #f)) (cond-expand (mit-scheme (cout "Expanded in MIT Scheme" nl)) (else #f)) (cond-expand (petite-chez (cout "Expanded in Petite Chez Scheme" nl)) (else #f)) (cond-expand (bigloo (cout "Expanded in Bigloo" nl)) (else #f)) (assert (cond-expand (xxx (/ 1 0)) (else #t))) (assert (cond-expand ((not xxx) #t))) (assert (cond-expand ((or xxx (not xxx)) #t))) (assert (cond-expand ((and (not xxx) xxx) (/ 1 0)) (else #t))) (cond-expand ((or gambit scm mit-scheme bigloo petite-chez) (assert (= 1 (+ (cond-expand (gambit 1) (else 0)) (cond-expand (scm 1) (else 0)) (cond-expand (mit-scheme 1) (else 0)) (cond-expand (bigloo 1) (else 0)) (cond-expand (petite-chez 1) (else 0))))) (cond-expand (gambit (assert (failed? (cond-expand ((not gambit) #t))))) (else #t)) (assert (memv (cond-expand (gambit 0 1) (scm 0 2) (bigloo 0 3) (mit-scheme 0 4) (petite-chez 0 5) (else 0)) '(1 2 3 4 5))) (assert (memv (cond-expand ((and bigloo srfi-0) 0 3) ((and gambit srfi-0) 0 1) ((and scm srfi-0) 0 2) ((and mit-scheme srfi-0) 0 4) ((and petite-chez srfi-0) 0 5) (else 0)) '(1 2 3 4 5))) (assert (memv (cond-expand ((or xxx gambit zzz) 0 1) ((or xxx scm zzz) 0 2) ((or xxx bigloo zzz) 0 3) ((or xxx mit-scheme zzz) 0 4) ((or xxx petite-chez zzz) 0 5) (else 0)) '(1 2 3 4 5))) (assert (memv (cond-expand ((not gambit) 0 1) ((not scm) 0 2) ((not mit-scheme) 0 4) ((not bigloo) 0 3) (else 0)) '(1 2))) (assert (memv (cond-expand ((or (not gambit) (and gambit gambit)) 0 1) ((or (not scm) (and scm scm)) 0 2) ((or (not mit-scheme) (and mit-scheme mit-scheme)) 0 4) ((or (not petite-chez) (and petite-chez petite-chez)) 0 5) ((or (not bigloo) (and bigloo bigloo)) 0 3) (else 0)) '(1 2 3 4 5))) (assert (cond-expand ((not (and gambit scm mit-scheme petite-chez)) #t))) (assert (cond-expand (gambit (positive? +inf.)) ; works only in Gambit (scm (procedure? try-load)) ; works only in SCM (bigloo ( x 1) x #f)) (cerr " finding an element in a list" nl) (test-driver gt1? '(1 2 3 4 5) 2) (test-driver gt1? '(1 1 1 1 1) #f) (test-driver gt1? '(4 1 1 1 1) 4) (test-driver gt1? '(4 5 6 1 9) 4) (test-driver gt1? '(-4 -5 -6 1 9) 9) (test-driver eq-a? '(#\b #\c #\a #\k) #\a) (cerr " finding an element in a vector" nl) (test-driver gt1? '#(1 2 3 4 5) 2) (test-driver gt1? '#(1 1 1 1 1) #f) (test-driver gt1? '#(4 1 1 1 1) 4) (test-driver gt1? '#(4 5 6 1 9) 4) (test-driver gt1? '#(-4 -5 -6 1 9) 9) (test-driver eq-a? '#(#\b #\c #\a #\k) #\a) ) (cerr nl "Verifying our environments..." nl) (cerr nl nl "verifying environments") (env.print "Initial environment") (cerr "adding a few bindings..." nl) (%%env.bind 'a 1) (%%env.bind 'b-1 'c) (%%env.bind 'cc "c c") (%%env.bind 'dd '(1 3 5 (6) ())) (cerr nl "The resulting environment. Now trying to get the stuff back..." nl) (assert (= 1 (%%env.find 'a))) (assert (not (%%env.find 'b))) ; (%%env.demand 'b) (assert (failed? (%%env.demand 'b))) (assert (eq? 'c (%%env.demand 'b-1))) (assert (string=? "c c" (%%env.demand 'cc))) (assert (equal? '(1 3 5 (6) ()) (%%env.find 'dd))) (let ((alist (env.->alist))) (cerr "\nThe environment exported as an assoc-list\n") (pp alist) (assert (equal? alist '((dd 1 3 5 (6) ()) (cc . "c c") (b-1 . c) (a . 1)))) ) (let ((mark (env.mark)) (capture #f)) (cerr "placing mark " mark nl) (env.bind* '((a . 3) (b . #(1 2 1/4)))) (assert (= 3 (%%env.find 'a))) (assert (equal? '#(1 2 1/4) (%%env.demand 'b))) (env.print "after adding the mark") (let ((another-mark (env.mark))) (%%env.bind 'a 4) (env.print "after adding another mark " another-mark) (assert (= 4 (%%env.find 'a))) (cerr "capturing the env" nl) (set! capture (env.capture! another-mark "Captured Env")) (assert (= 3 (%%env.demand 'a))) (env.extend capture) (env.print "after putting the captured env back") ) (assert (= 4 (%%env.find 'a))) (cerr "flushing through the mark " mark) (env.flush! mark) (env.print) (assert (= 1 (%%env.find 'a))) (assert (failed? (env.flush! mark))) (assert (= -1 (env.with capture (lambda () (env.print "temporarily extended env") (assert (= 4 (%%env.find 'a))) (assert (failed? (%%env.demand 'b))) -1)))) (assert (= 1 (%%env.find 'a))) (assert (= -3 (env.with-exclusive capture (lambda () (env.print "temporarily replaced env") (assert (= 4 (%%env.find 'a))) (assert (failed? (%%env.demand 'dd))) -3)))) (assert (= 1 (%%env.find 'a))) (assert (equal? '(1 3 5 (6) ()) (%%env.find 'dd))) ) (cerr nl "All tests passed" nl)