;;;; syntax.test --- test suite for Guile's syntactic forms -*- scheme -*-
;;;;
-;;;; Copyright (C) 2001, 2003 Free Software Foundation, Inc.
+;;;; Copyright (C) 2001,2003,2004, 2005 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
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING. If not, write to
-;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
-;;;; Boston, MA 02111-1307 USA
+;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;;;; Boston, MA 02110-1301 USA
(define-module (test-suite test-syntax)
:use-module (test-suite lib))
(cons 'syntax-error "Bad expression"))
(define exception:missing/extra-expr
- (cons 'misc-error "^missing or extra expression"))
-(define exception:missing/extra-expr-syntax
- (cons 'syntax-error "missing or extra expression"))
+ (cons 'syntax-error "Missing or extra expression"))
(define exception:missing-expr
(cons 'syntax-error "Missing expression"))
+(define exception:missing-body-expr
+ (cons 'syntax-error "Missing body expression"))
(define exception:extra-expr
(cons 'syntax-error "Extra expression"))
+(define exception:illegal-empty-combination
+ (cons 'syntax-error "Illegal empty combination"))
(define exception:bad-bindings
- (cons 'misc-error "^bad bindings"))
-(define exception:duplicate-bindings
- (cons 'misc-error "^duplicate bindings"))
+ (cons 'syntax-error "Bad bindings"))
+(define exception:bad-binding
+ (cons 'syntax-error "Bad binding"))
+(define exception:duplicate-binding
+ (cons 'syntax-error "Duplicate binding"))
(define exception:bad-body
(cons 'misc-error "^bad body"))
(define exception:bad-formals
(define exception:bad-cond-clause
(cons 'syntax-error "Bad cond clause"))
-(define exception:bad-var
- (cons 'misc-error "^bad variable"))
-
(with-test-prefix "expressions"
;; Fixed on 2001-3-3
(pass-if-exception "empty parentheses \"()\""
- exception:missing/extra-expr
+ exception:illegal-empty-combination
(eval '()
(interaction-environment)))))
(begin)
#t)
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal begin"
+ (let ((foo (lambda () (if (= 1 1) (begin (+ 1) (+ 2))))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (if (= 1 1) (begin (+ 1) (+ 2)))))))
+
+ (pass-if "redundant nested begin"
+ (let ((foo (lambda () (if (= 1 1) (begin (+ 1) (begin (+ 2) (+ 3)))))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (if (= 1 1) (begin (+ 1) (begin (+ 2) (+ 3))))))))
+
+ (pass-if "redundant begin at start of body"
+ (let ((foo (lambda () (begin (+ 1) (+ 2))))) ; should be optimized
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (begin (+ 1) (+ 2)))))))
+
(expect-fail-exception "illegal (begin)"
exception:bad-body
(if #t (begin))
(with-test-prefix "lambda"
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal lambda"
+ (let ((foo (lambda () (lambda (x y) (+ x y)))))
+ ((foo) 1 2) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (lambda (x y) (+ x y))))))
+
+ (pass-if "lambda with documentation"
+ (let ((foo (lambda () (lambda (x y) "docstring" (+ x y)))))
+ ((foo) 1 2) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (lambda (x y) "docstring" (+ x y)))))))
+
(with-test-prefix "bad formals"
(pass-if-exception "(lambda)"
(with-test-prefix "let"
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal let"
+ (let ((foo (lambda () (let ((i 1) (j 2)) (+ i j)))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (let ((i 1) (j 2)) (+ i j)))))))
+
(with-test-prefix "bindings"
(pass-if-exception "late binding"
(with-test-prefix "bad bindings"
(pass-if-exception "(let)"
- exception:bad-bindings
+ exception:missing-expr
(eval '(let)
(interaction-environment)))
(pass-if-exception "(let 1)"
- exception:bad-bindings
+ exception:missing-expr
(eval '(let 1)
(interaction-environment)))
(pass-if-exception "(let (x))"
- exception:bad-bindings
+ exception:missing-expr
(eval '(let (x))
(interaction-environment)))
- ;; FIXME: Wouldn't one rather expect a 'bad bindings' error?
- ;; (Even although the body is bad as well...)
(pass-if-exception "(let ((x)))"
- exception:bad-body
+ exception:missing-expr
(eval '(let ((x)))
(interaction-environment)))
(pass-if-exception "(let (x) 1)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(let (x) 1)
(interaction-environment)))
(pass-if-exception "(let ((x)) 3)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(let ((x)) 3)
(interaction-environment)))
(pass-if-exception "(let ((x 1) y) x)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(let ((x 1) y) x)
(interaction-environment)))
(pass-if-exception "(let ((1 2)) 3)"
- exception:bad-var
+ exception:bad-variable
(eval '(let ((1 2)) 3)
(interaction-environment))))
(with-test-prefix "duplicate bindings"
(pass-if-exception "(let ((x 1) (x 2)) x)"
- exception:duplicate-bindings
+ exception:duplicate-binding
(eval '(let ((x 1) (x 2)) x)
(interaction-environment))))
(with-test-prefix "bad body"
(pass-if-exception "(let ())"
- exception:bad-body
+ exception:missing-expr
(eval '(let ())
(interaction-environment)))
(pass-if-exception "(let ((x 1)))"
- exception:bad-body
+ exception:missing-expr
(eval '(let ((x 1)))
(interaction-environment)))))
(with-test-prefix "bad bindings"
(pass-if-exception "(let x (y))"
- exception:bad-bindings
+ exception:missing-expr
(eval '(let x (y))
(interaction-environment))))
(with-test-prefix "bad body"
(pass-if-exception "(let x ())"
- exception:bad-body
+ exception:missing-expr
(eval '(let x ())
(interaction-environment)))
(pass-if-exception "(let x ((y 1)))"
- exception:bad-body
+ exception:missing-expr
(eval '(let x ((y 1)))
(interaction-environment)))))
(with-test-prefix "let*"
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal let*"
+ (let ((foo (lambda () (let* ((x 1) (y 2)) (+ x y)))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (let* ((x 1) (y 2)) (+ x y))))))
+
+ (pass-if "let* without bindings"
+ (let ((foo (lambda () (let ((x 1) (y 2))
+ (let* ()
+ (and (= x 1) (= y 2)))))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (let ((x 1) (y 2))
+ (let* ()
+ (and (= x 1) (= y 2)))))))))
+
(with-test-prefix "bindings"
(pass-if "(let* ((x 1) (x 2)) ...)"
(pass-if "(let* ((x 1) (x x)) ...)"
(let* ((x 1) (x x))
- (= x 1))))
+ (= x 1)))
+
+ (pass-if "(let ((x 1) (y 2)) (let* () ...))"
+ (let ((x 1) (y 2))
+ (let* ()
+ (and (= x 1) (= y 2))))))
(with-test-prefix "bad bindings"
(pass-if-exception "(let*)"
- exception:bad-bindings
+ exception:missing-expr
(eval '(let*)
(interaction-environment)))
(pass-if-exception "(let* 1)"
- exception:bad-bindings
+ exception:missing-expr
(eval '(let* 1)
(interaction-environment)))
(pass-if-exception "(let* (x))"
- exception:bad-bindings
+ exception:missing-expr
(eval '(let* (x))
(interaction-environment)))
(pass-if-exception "(let* (x) 1)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(let* (x) 1)
(interaction-environment)))
(pass-if-exception "(let* ((x)) 3)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(let* ((x)) 3)
(interaction-environment)))
(pass-if-exception "(let* ((x 1) y) x)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(let* ((x 1) y) x)
(interaction-environment)))
(interaction-environment)))
(pass-if-exception "(let* ((1 2)) 3)"
- exception:bad-var
+ exception:bad-variable
(eval '(let* ((1 2)) 3)
(interaction-environment))))
(with-test-prefix "bad body"
(pass-if-exception "(let* ())"
- exception:bad-body
+ exception:missing-expr
(eval '(let* ())
(interaction-environment)))
(pass-if-exception "(let* ((x 1)))"
- exception:bad-body
+ exception:missing-expr
(eval '(let* ((x 1)))
(interaction-environment)))))
(with-test-prefix "letrec"
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal letrec"
+ (let ((foo (lambda () (letrec ((i 1) (j 2)) (+ i j)))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (letrec ((i 1) (j 2)) (+ i j)))))))
+
(with-test-prefix "bindings"
(pass-if-exception "initial bindings are undefined"
- exception:unbound-var
+ exception:used-before-defined
(let ((x 1))
(letrec ((x 1) (y x)) y))))
(with-test-prefix "bad bindings"
(pass-if-exception "(letrec)"
- exception:bad-bindings
+ exception:missing-expr
(eval '(letrec)
(interaction-environment)))
(pass-if-exception "(letrec 1)"
- exception:bad-bindings
+ exception:missing-expr
(eval '(letrec 1)
(interaction-environment)))
(pass-if-exception "(letrec (x))"
- exception:bad-bindings
+ exception:missing-expr
(eval '(letrec (x))
(interaction-environment)))
(pass-if-exception "(letrec (x) 1)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(letrec (x) 1)
(interaction-environment)))
(pass-if-exception "(letrec ((x)) 3)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(letrec ((x)) 3)
(interaction-environment)))
(pass-if-exception "(letrec ((x 1) y) x)"
- exception:bad-bindings
+ exception:bad-binding
(eval '(letrec ((x 1) y) x)
(interaction-environment)))
(interaction-environment)))
(pass-if-exception "(letrec ((1 2)) 3)"
- exception:bad-var
+ exception:bad-variable
(eval '(letrec ((1 2)) 3)
(interaction-environment))))
(with-test-prefix "duplicate bindings"
(pass-if-exception "(letrec ((x 1) (x 2)) x)"
- exception:duplicate-bindings
+ exception:duplicate-binding
(eval '(letrec ((x 1) (x 2)) x)
(interaction-environment))))
(with-test-prefix "bad body"
(pass-if-exception "(letrec ())"
- exception:bad-body
+ exception:missing-expr
(eval '(letrec ())
(interaction-environment)))
(pass-if-exception "(letrec ((x 1)))"
- exception:bad-body
+ exception:missing-expr
(eval '(letrec ((x 1)))
(interaction-environment)))))
(with-test-prefix "if"
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal if"
+ (let ((foo (lambda (x) (if x (+ 1) (+ 2)))))
+ (foo #t) ; make sure, memoization has been performed
+ (foo #f) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda (x) (if x (+ 1) (+ 2))))))
+
+ (pass-if "if without else"
+ (let ((foo (lambda (x) (if x (+ 1)))))
+ (foo #t) ; make sure, memoization has been performed
+ (foo #f) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda (x) (if x (+ 1))))))
+
+ (pass-if "if #f without else"
+ (let ((foo (lambda () (if #f #f))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ `(lambda () (if #f #f))))))
+
(with-test-prefix "missing or extra expressions"
(pass-if-exception "(if)"
- exception:missing/extra-expr-syntax
+ exception:missing/extra-expr
(eval '(if)
(interaction-environment)))
(pass-if-exception "(if 1 2 3 4)"
- exception:missing/extra-expr-syntax
+ exception:missing/extra-expr
(eval '(if 1 2 3 4)
(interaction-environment)))))
(with-test-prefix "cond"
+ (with-test-prefix "cond is hygienic"
+
+ (pass-if "bound 'else is handled correctly"
+ (eq? (let ((else 'ok)) (cond (else))) 'ok))
+
+ (with-test-prefix "bound '=> is handled correctly"
+
+ (pass-if "#t => 'ok"
+ (let ((=> 'foo))
+ (eq? (cond (#t => 'ok)) 'ok)))
+
+ (pass-if "else =>"
+ (let ((=> 'foo))
+ (eq? (cond (else =>)) 'foo)))
+
+ (pass-if "else => identity"
+ (let ((=> 'foo))
+ (eq? (cond (else => identity)) identity)))))
+
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal clauses"
+ (let ((foo (lambda (x) (cond ((= x 1) 'bar) ((= x 2) 'baz)))))
+ (foo 1) ; make sure, memoization has been performed
+ (foo 2) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda (x) (cond ((= x 1) 'bar) ((= x 2) 'baz))))))
+
+ (pass-if "else"
+ (let ((foo (lambda () (cond (else 'bar)))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (cond (else 'bar))))))
+
+ (pass-if "=>"
+ (let ((foo (lambda () (cond (#t => identity)))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (cond (#t => identity)))))))
+
(with-test-prefix "bad or missing clauses"
(pass-if-exception "(cond)"
(pass-if-exception "(cond (1) 1)"
exception:bad-cond-clause
(eval '(cond (1) 1)
- (interaction-environment)))))
-
-(with-test-prefix "cond =>"
-
- (with-test-prefix "cond is hygienic"
-
- (pass-if "bound 'else is handled correctly"
- (eq? (let ((else 'ok)) (cond (else))) 'ok))
-
- (pass-if "bound '=> is handled correctly"
- (eq? (let ((=> #f)) (cond (#t => 'ok))) 'ok)))
-
- (with-test-prefix "else is handled correctly"
-
- (pass-if "else =>"
- (let ((=> 'foo))
- (eq? (cond (else =>)) 'foo)))
-
- (pass-if "else => identity"
- (let* ((=> 'foo))
- (eq? (cond (else => identity)) identity))))
+ (interaction-environment))))
(with-test-prefix "wrong number of arguments"
(eval '(let ((else #f)) (case 1 (else #f)))
(interaction-environment))))
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal clauses"
+ (let ((foo (lambda (x) (case x ((1) 'bar) ((2) 'baz) (else 'foobar)))))
+ (foo 1) ; make sure, memoization has been performed
+ (foo 2) ; make sure, memoization has been performed
+ (foo 3) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda (x) (case x ((1) 'bar) ((2) 'baz) (else 'foobar))))))
+
+ (pass-if "empty labels"
+ (let ((foo (lambda (x) (case x ((1) 'bar) (() 'baz) (else 'foobar)))))
+ (foo 1) ; make sure, memoization has been performed
+ (foo 2) ; make sure, memoization has been performed
+ (foo 3) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda (x) (case x ((1) 'bar) (() 'baz) (else 'foobar)))))))
+
(with-test-prefix "bad or missing clauses"
(pass-if-exception "(case)"
(eval '(case 1 (else #f) ((1) #t))
(interaction-environment)))))
-(with-test-prefix "define"
+(with-test-prefix "top-level define"
+
+ (pass-if "binding is created before expression is evaluated"
+ (= (eval '(begin
+ (define foo
+ (begin
+ (set! foo 1)
+ (+ foo 1)))
+ foo)
+ (interaction-environment))
+ 2))
(with-test-prefix "currying"
(pass-if "(define ((foo)) #f)"
- (define ((foo)) #t)
- ((foo))))
+ (eval '(begin
+ (define ((foo)) #t)
+ ((foo)))
+ (interaction-environment))))
+
+ (with-test-prefix "unmemoization"
+
+ (pass-if "definition unmemoized without prior execution"
+ (eval '(begin
+ (define (blub) (cons ('(1 . 2)) 2))
+ (equal?
+ (procedure-source blub)
+ '(lambda () (cons ('(1 . 2)) 2))))
+ (interaction-environment)))
+
+ (pass-if "definition with documentation unmemoized without prior execution"
+ (eval '(begin
+ (define (blub) "Comment" (cons ('(1 . 2)) 2))
+ (equal?
+ (procedure-source blub)
+ '(lambda () "Comment" (cons ('(1 . 2)) 2))))
+ (interaction-environment))))
(with-test-prefix "missing or extra expressions"
(eval '(define)
(interaction-environment)))))
+(with-test-prefix "internal define"
+
+ (pass-if "internal defines become letrec"
+ (eval '(let ((a identity) (b identity) (c identity))
+ (define (a x) (if (= x 0) 'a (b (- x 1))))
+ (define (b x) (if (= x 0) 'b (c (- x 1))))
+ (define (c x) (if (= x 0) 'c (a (- x 1))))
+ (and (eq? 'a (a 0) (a 3))
+ (eq? 'b (a 1) (a 4))
+ (eq? 'c (a 2) (a 5))))
+ (interaction-environment)))
+
+ (pass-if "internal defines with begin"
+ (false-if-exception
+ (eval '(let ((a identity) (b identity) (c identity))
+ (define (a x) (if (= x 0) 'a (b (- x 1))))
+ (begin
+ (define (b x) (if (= x 0) 'b (c (- x 1)))))
+ (define (c x) (if (= x 0) 'c (a (- x 1))))
+ (and (eq? 'a (a 0) (a 3))
+ (eq? 'b (a 1) (a 4))
+ (eq? 'c (a 2) (a 5))))
+ (interaction-environment))))
+
+ (pass-if "internal defines with empty begin"
+ (false-if-exception
+ (eval '(let ((a identity) (b identity) (c identity))
+ (define (a x) (if (= x 0) 'a (b (- x 1))))
+ (begin)
+ (define (b x) (if (= x 0) 'b (c (- x 1))))
+ (define (c x) (if (= x 0) 'c (a (- x 1))))
+ (and (eq? 'a (a 0) (a 3))
+ (eq? 'b (a 1) (a 4))
+ (eq? 'c (a 2) (a 5))))
+ (interaction-environment))))
+
+ (pass-if "internal defines with macro application"
+ (false-if-exception
+ (eval '(begin
+ (defmacro my-define forms
+ (cons 'define forms))
+ (let ((a identity) (b identity) (c identity))
+ (define (a x) (if (= x 0) 'a (b (- x 1))))
+ (my-define (b x) (if (= x 0) 'b (c (- x 1))))
+ (define (c x) (if (= x 0) 'c (a (- x 1))))
+ (and (eq? 'a (a 0) (a 3))
+ (eq? 'b (a 1) (a 4))
+ (eq? 'c (a 2) (a 5)))))
+ (interaction-environment))))
+
+ (pass-if-exception "missing body expression"
+ exception:missing-body-expr
+ (eval '(let () (define x #t))
+ (interaction-environment)))
+
+ (pass-if "unmemoization"
+ (eval '(begin
+ (define (foo)
+ (define (bar)
+ 'ok)
+ (bar))
+ (foo)
+ (equal?
+ (procedure-source foo)
+ '(lambda () (letrec ((bar (lambda () (quote ok)))) (bar)))))
+ (interaction-environment))))
+
+(with-test-prefix "do"
+
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal case"
+ (let ((foo (lambda () (do ((i 1 (+ i 1)) (j 2))
+ ((> i 9) (+ i j))
+ (identity i)))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (do ((i 1 (+ i 1)) (j 2))
+ ((> i 9) (+ i j))
+ (identity i))))))
+
+ (pass-if "reduced case"
+ (let ((foo (lambda () (do ((i 1 (+ i 1)) (j 2 j)) ; redundant step for j
+ ((> i 9) (+ i j))
+ (identity i)))))
+ (foo) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda () (do ((i 1 (+ i 1)) (j 2)) ; no redundancy here
+ ((> i 9) (+ i j))
+ (identity i))))))))
+
(with-test-prefix "set!"
+ (with-test-prefix "unmemoization"
+
+ (pass-if "normal set!"
+ (let ((foo (lambda (x) (set! x (+ 1 x)))))
+ (foo 1) ; make sure, memoization has been performed
+ (equal? (procedure-source foo)
+ '(lambda (x) (set! x (+ 1 x)))))))
+
(with-test-prefix "missing or extra expressions"
(pass-if-exception "(set!)"
(with-test-prefix "bad variable"
(pass-if-exception "(set! \"\" #t)"
- exception:bad-var
+ exception:bad-variable
(eval '(set! "" #t)
(interaction-environment)))
(pass-if-exception "(set! 1 #t)"
- exception:bad-var
+ exception:bad-variable
(eval '(set! 1 #t)
(interaction-environment)))
(pass-if-exception "(set! #t #f)"
- exception:bad-var
+ exception:bad-variable
(eval '(set! #t #f)
(interaction-environment)))
(pass-if-exception "(set! #f #t)"
- exception:bad-var
+ exception:bad-variable
(eval '(set! #f #t)
(interaction-environment)))
- (pass-if-exception "(set! #\space #f)"
- exception:bad-var
+ (pass-if-exception "(set! #\\space #f)"
+ exception:bad-variable
(eval '(set! #\space #f)
(interaction-environment)))))
;; an environment with no bindings at all
(define empty-environment
(make-module 1))
+
+ ;; these tests are 'unresolved because to work with ice-9 syncase it was
+ ;; necessary to drop the unquote from `do' in the implementation, and
+ ;; unfortunately that makes `while' depend on its evaluation environment
(pass-if "empty body"
+ (throw 'unresolved)
(eval `(,while #f)
empty-environment)
#t)
(pass-if "initially false"
+ (throw 'unresolved)
(eval `(,while #f
#f)
empty-environment)
#t)
(pass-if "iterating"
+ (throw 'unresolved)
(let ((cond (make-iterations-cond 3)))
(eval `(,while (,cond)
123 456)