1
;; FileName : test-define.scm
2
;; About : unit test for R5RS 'define'
4
;; Copyright (C) 2005-2006 Kazuki Ohta <mover AT hct.zaq.ne.jp>
6
;; All rights reserved.
8
;; Redistribution and use in source and binary forms, with or without
9
;; modification, are permitted provided that the following conditions
12
;; 1. Redistributions of source code must retain the above copyright
13
;; notice, this list of conditions and the following disclaimer.
14
;; 2. Redistributions in binary form must reproduce the above copyright
15
;; notice, this list of conditions and the following disclaimer in the
16
;; documentation and/or other materials provided with the distribution.
17
;; 3. Neither the name of authors nor the names of its contributors
18
;; may be used to endorse or promote products derived from this software
19
;; without specific prior written permission.
21
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
22
;; IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23
;; THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
25
;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26
;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27
;; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
(load "./test/unittest.scm")
36
(define *test-track-progress* #f)
39
(assert-error "define invalid form #1"
42
(assert-error "define invalid form #2"
45
(assert-error "define invalid form #3"
48
(assert-error "define invalid form #4"
50
(define a 1 'excessive)))
51
(assert-error "define invalid form #5"
54
(if (and (provided? "sigscheme")
55
(provided? "strict-argcheck"))
56
(assert-error "define invalid form #6"
62
(assert-equal? "basic define check" 3 val1)
66
(assert-equal? "redefine check" 5 val1)
71
(assert-equal? "func define" 10 (what? 10))
76
(assert-equal? "func define" 10 (what2? 10))
80
(assert-equal? "nullarg test" "nullarg" (nullarg))
84
(assert-equal? "func define" 10 (add 2 8))
86
; tests for dot list arguments
89
(assert-equal? "dot arg test 1" '(1 2) (dotarg1 1 2))
91
(define (dotarg2 a . b)
93
(assert-equal? "dot arg test 2" 1 (dotarg2 1 2))
95
(define (dotarg3 a . b)
97
(assert-equal? "dot arg test 3" '(2) (dotarg3 1 2))
98
(assert-equal? "dot arg test 4" '(2 3) (dotarg3 1 2 3))
101
(define (dotarg4 a b . c)
103
(assert-equal? "dot arg test 5" 2 (dotarg4 1 2 3))
105
(define (dotarg5 a b . c)
107
(assert-equal? "dot arg test 6" '(3 4) (dotarg5 1 2 3 4))
109
; test for internal define
110
(define (idefine-o a)
111
(define (idefine-i c)
115
(assert-equal? "internal define1" 5 (idefine-o 2))
118
(define (idefine1 . args)
122
(+ (idefine1 1 2 3 4 5) (idefine2 a)))
124
(assert-equal? "internal define2" 17 (idefine0 0))
126
(if (or (symbol-bound? 'f)
130
(symbol-bound? 'bar))
131
(error "global variables for internal definitions tests are tainted"))
133
(tn "internal defintions")
172
(tn "internal defintions: letrec-like behavior")
176
(define foo (lambda (y) (bar x y)))
177
(define bar (lambda (a b) (+ (* a b) a)))
182
(define bar (lambda (a b) (+ (* a b) a)))
183
(define foo (lambda (y) (bar x y)))
189
(define bar (lambda (a b) (+ (* a b) a)))
194
(define bar (lambda (a b) (+ (* a b) a)))
201
(define bar (+ foo 1))
206
(define bar (+ foo 1))
213
(define bar (+ foo 1))
218
(define bar (+ foo 1))
222
(tn "internal defintions: non-beginning of block")
260
(tn "internal defintions: non-beginning of block (in begin)")
303
(tn "internal defintions: non-beginning of block (in eval)")
309
(eval '(define bar 2)
310
(interaction-environment))
317
(eval '(define bar 2)
318
(interaction-environment))
325
(eval '(define bar 2)
326
(interaction-environment))
333
(eval '(define bar 2)
334
(interaction-environment))
342
(eval '(define bar 2)
343
(interaction-environment))
347
;; As specified as follows in R5RS, definitions in following forms are invalid.
351
;; Definitions are valid in some, but not all, contexts where expressions are
352
;; allowed. They are valid only at the top level of a <program> and at the
353
;; beginning of a <body>.
355
;; 5.2.2 Internal definitions
357
;; Definitions may occur at the beginning of a <body> (that is, the body of a
358
;; lambda, let, let*, letrec, let-syntax, or letrec-syntax expression or that
359
;; of a definition of an appropriate form).
361
;; Wherever an internal definition may occur (begin <definition1> ...) is
362
;; equivalent to the sequence of definitions that form the body of the begin.
363
(tn "definition in do")
380
(interaction-environment))))
381
(tn "definition in if")
395
(interaction-environment))))
397
;; 'begin' is treated as if transparent, as described as the third rule of
399
(tn "definition in begin")
437
(tn "definition in sequencial begin")
490
(tn "definition in sequencial nested begin")
551
(tn "definition in invalid sequencial begin")
611
(define (set-dot a . b)
615
(assert-equal? "set dot test" '(1 2) (set-dot '()))
617
(if (and (provided? "sigscheme")
618
(provided? "strict-argcheck"))
620
(tn "define function form: boolean as an arg")
621
(assert-error (tn) (lambda () (define (f . #t) #t)))
622
(assert-error (tn) (lambda () (define (f #t) #t)))
623
(assert-error (tn) (lambda () (define (f x #t) #t)))
624
(assert-error (tn) (lambda () (define (f #t x) #t)))
625
(assert-error (tn) (lambda () (define (f x . #t) #t)))
626
(assert-error (tn) (lambda () (define (f #t . x) #t)))
627
(assert-error (tn) (lambda () (define (f x y #t) #t)))
628
(assert-error (tn) (lambda () (define (f x y . #t) #t)))
629
(assert-error (tn) (lambda () (define (f x #t y) #t)))
630
(assert-error (tn) (lambda () (define (f x #t . y) #t)))
631
(tn "define function form: intger as an arg")
632
(assert-error (tn) (lambda () (define (f . 1) #t)))
633
(assert-error (tn) (lambda () (define (f 1) #t)))
634
(assert-error (tn) (lambda () (define (f x 1) #t)))
635
(assert-error (tn) (lambda () (define (f 1 x) #t)))
636
(assert-error (tn) (lambda () (define (f x . 1) #t)))
637
(assert-error (tn) (lambda () (define (f 1 . x) #t)))
638
(assert-error (tn) (lambda () (define (f x y 1) #t)))
639
(assert-error (tn) (lambda () (define (f x y . 1) #t)))
640
(assert-error (tn) (lambda () (define (f x 1 y) #t)))
641
(assert-error (tn) (lambda () (define (f x 1 . y) #t)))
642
(tn "define function form: null as an arg")
643
(assert-true (tn) (define (f . ()) #t))
644
(assert-error (tn) (lambda () (define (f ()) #t)))
645
(assert-error (tn) (lambda () (define (f x ()) #t)))
646
(assert-error (tn) (lambda () (define (f () x) #t)))
647
(assert-true (tn) (define (f x . ()) #t))
648
(assert-error (tn) (lambda () (define (f () . x) #t)))
649
(assert-error (tn) (lambda () (define (f x y ()) #t)))
650
(assert-true (tn) (define (f x y . ()) #t))
651
(assert-error (tn) (lambda () (define (f x () y) #t)))
652
(assert-error (tn) (lambda () (define (f x () . y) #t)))
653
(tn "define function form: pair as an arg")
654
(assert-true (tn) (define (f . (a)) #t))
655
(assert-error (tn) (lambda () (define (f (a)) #t)))
656
(assert-error (tn) (lambda () (define (f x (a)) #t)))
657
(assert-error (tn) (lambda () (define (f (a) x) #t)))
658
(assert-true (tn) (define (f x . (a)) #t))
659
(assert-error (tn) (lambda () (define (f (a) . x) #t)))
660
(assert-error (tn) (lambda () (define (f x y (a)) #t)))
661
(assert-true (tn) (define (f x y . (a)) #t))
662
(assert-error (tn) (lambda () (define (f x (a) y) #t)))
663
(assert-error (tn) (lambda () (define (f x (a) . y) #t)))
664
(tn "define function form: char as an arg")
665
(assert-error (tn) (lambda () (define (f . #\a) #t)))
666
(assert-error (tn) (lambda () (define (f #\a) #t)))
667
(assert-error (tn) (lambda () (define (f x #\a) #t)))
668
(assert-error (tn) (lambda () (define (f #\a x) #t)))
669
(assert-error (tn) (lambda () (define (f x . #\a) #t)))
670
(assert-error (tn) (lambda () (define (f #\a . x) #t)))
671
(assert-error (tn) (lambda () (define (f x y #\a) #t)))
672
(assert-error (tn) (lambda () (define (f x y . #\a) #t)))
673
(assert-error (tn) (lambda () (define (f x #\a y) #t)))
674
(assert-error (tn) (lambda () (define (f x #\a . y) #t)))
675
(tn "define function form: string as an arg")
676
(assert-error (tn) (lambda () (define (f . "a") #t)))
677
(assert-error (tn) (lambda () (define (f "a") #t)))
678
(assert-error (tn) (lambda () (define (f x "a") #t)))
679
(assert-error (tn) (lambda () (define (f "a" x) #t)))
680
(assert-error (tn) (lambda () (define (f x . "a") #t)))
681
(assert-error (tn) (lambda () (define (f "a" . x) #t)))
682
(assert-error (tn) (lambda () (define (f x y "a") #t)))
683
(assert-error (tn) (lambda () (define (f x y . "a") #t)))
684
(assert-error (tn) (lambda () (define (f x "a" y) #t)))
685
(assert-error (tn) (lambda () (define (f x "a" . y) #t)))
686
(tn "define function form: vector as an arg")
687
(assert-error (tn) (lambda () (define (f . #(a)) #t)))
688
(assert-error (tn) (lambda () (define (f #(a)) #t)))
689
(assert-error (tn) (lambda () (define (f x #(a)) #t)))
690
(assert-error (tn) (lambda () (define (f #(a) x) #t)))
691
(assert-error (tn) (lambda () (define (f x . #(a)) #t)))
692
(assert-error (tn) (lambda () (define (f #(a) . x) #t)))
693
(assert-error (tn) (lambda () (define (f x y #(a)) #t)))
694
(assert-error (tn) (lambda () (define (f x y . #(a)) #t)))
695
(assert-error (tn) (lambda () (define (f x #(a) y) #t)))
696
(assert-error (tn) (lambda () (define (f x #(a) . y) #t)))))