1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
;; Filename : test-eval.scm
;; About : unit test for evaluation
;;
;; Copyright (C) 2005-2006 Kazuki Ohta <mover AT hct.zaq.ne.jp>
;; Copyright (c) 2007-2008 SigScheme Project <uim-en AT googlegroups.com>
;;
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. Neither the name of authors nor the names of its contributors
;; may be used to endorse or promote products derived from this software
;; without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
;; IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
;; THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
;; EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
;; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(require-extension (unittest))
(define tn test-name)
(tn "eval")
(assert-equal? (tn) 3 (eval '(+ 1 2)
(interaction-environment)))
(assert-equal? (tn) 3 (eval '((lambda (x y) (+ x y)) 1 2)
(interaction-environment)))
(tn "eval with invalid environment specifiers")
(assert-error (tn) (lambda ()
(eval '(+ 1 2) 3)))
(assert-error (tn) (lambda ()
(eval '(+ 1 2) 'symbol)))
(assert-error (tn) (lambda ()
(eval '(+ 1 2) "string")))
(assert-error (tn) (lambda ()
(eval '(+ 1 2) #\a)))
;; R5RS: 4.1.3 Procedure calls
;; > Procedure calls may return any number of values (see values in section see
;; > section 6.4 Control features). With the exception of `values' the
;; > procedures available in the initial environment return one value or, for
;; > procedures such as `apply', pass on the values returned by a call to one
;; > of their arguments.
;; SigScheme apply this specification for 'eval' also. -- YamaKen 2006-09-02
(tn "eval that returns multiple values")
(call-with-values
(lambda ()
(eval '(values 1 2 3)
(interaction-environment)))
(lambda vals
(assert-equal? (tn) '(1 2 3) vals)))
(call-with-values
(lambda ()
(eval '(apply values '(1 2 3))
(interaction-environment)))
(lambda vals
(assert-equal? (tn) '(1 2 3) vals)))
(tn "scheme-report-environment")
(if (provided? "sigscheme")
(begin
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(scheme-report-environment 4))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(scheme-report-environment 6))))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(scheme-report-environment 'symbol))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(scheme-report-environment "string"))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(scheme-report-environment #\a))))
(assert-equal? (tn) 3 (eval '(+ 1 2)
(scheme-report-environment 5)))
;; R5RS: 6.5 Eval
;; `eval' is not allowed to create new bindings in the environments associated
;; with `null-environment' or `scheme-report-environment'.
;; FIXME: SigScheme does not support this yet.
(if (and #f (provided? "sigscheme"))
(begin
(assert-error (tn) (lambda ()
;; Although other implementations evaluate this
;; expression without error (with or without 'foo'
;; defined), SigScheme will adopt this behavior.
(eval '(define foo 1)
(scheme-report-environment 5))))
(assert-error (tn) (lambda ()
(eval 'use
(scheme-report-environment 5))))))
(tn "null-environment")
(if (and #f (provided? "sigscheme"))
(begin
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(null-environment 4))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(null-environment 6))))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(null-environment 'symbol))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(null-environment "string"))))
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
(null-environment #\a))))
(assert-equal? (tn) 3 (eval '(+ 1 2)
(null-environment 5)))
;; R5RS: 6.5 Eval
;; `eval' is not allowed to create new bindings in the environments associated
;; with `null-environment' or `scheme-report-environment'.
;; FIXME: SigScheme does not support this yet.
(if (and #f (provided? "sigscheme"))
(begin
(assert-error (tn) (lambda ()
;; Although other implementations evaluate this
;; expression without error (with or without 'foo'
;; defined), SigScheme will adopt this behavior.
(eval '(define foo 1)
(null-environment 5))))
(assert-error (tn) (lambda ()
(eval 'use
(null-environment 5))))
(assert-error (tn) (lambda ()
(eval 'procedure?
(null-environment 5))))))
(if (provided? "sigscheme")
(begin
(tn "eval with handmade env")
;; single frame
(assert-equal? (tn) 10 (eval '(+ x y)
'(((x y) . (4 6)))))
;; 2 frames
(assert-equal? (tn) 15 (eval '(+ x y z)
'(((x y) . (4 6))
((z) . (5)))))
;; 3 frames
(assert-equal? (tn) 14 (eval '(+ x y z v w)
'(((x y) . (4 6))
((v w) . (0 -1))
((z) . (5)))))
;; dotted arg as formals
(assert-equal? (tn) 44 (eval '(apply + lst)
'(((x y . lst) . (4 6 8 10 12 14))
((z) . (5)))))
;; symbol as formals
(assert-equal? (tn) 54 (eval '(apply + lst)
'((lst . (4 6 8 10 12 14))
((z) . (5)))))
(tn "eval with invalid handmade env")
;; improper frame list
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x y) . (4 6))
. #t))))
;; actuals shortage
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x y z) . (4 6))))))
;; actuals shortage #2
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x y . z) . (4))))))
;; superfluous actuals
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x y) . (4 6 8))))))
;; dotted actuals
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x y) . (4 . 6))))))
;; dotted actuals #2
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x y) . (4 6 . 8))))))
;; dotted actuals #3
;; This pattern has been allowd when let-optionals* is introduced.
(assert-equal? (tn)
'(4 (6 . 8))
(eval '(list x y)
'(((x . y) . (4 6 . 8)))))
;; not a symbol in formals
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x 3) . (4 6))))))
;; not a list as actuals
(assert-error (tn) (lambda ()
(eval '(+ 1 2)
'(((x) . 4)
((y) . 6)))))
;; not a list as both formals and actuals
;; This pattern has been allowd when let-optionals* is introduced.
(assert-equal? (tn)
'(4 6)
(eval '(list x y)
'((x . 4)
(y . 6))))))
(total-report)
|