1 ;;;; eval.test --- tests guile's evaluator -*- scheme -*-
2 ;;;; Copyright (C) 2000, 2001, 2006, 2007, 2009 Free Software Foundation, Inc.
4 ;;;; This library is free software; you can redistribute it and/or
5 ;;;; modify it under the terms of the GNU Lesser General Public
6 ;;;; License as published by the Free Software Foundation; either
7 ;;;; version 2.1 of the License, or (at your option) any later version.
9 ;;;; This library is distributed in the hope that it will be useful,
10 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;;; Lesser General Public License for more details.
14 ;;;; You should have received a copy of the GNU Lesser General Public
15 ;;;; License along with this library; if not, write to the Free Software
16 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 (define-module (test-suite test-eval)
19 :use-module (test-suite lib)
20 :use-module ((srfi srfi-1) :select (unfold count))
21 :use-module (ice-9 documentation))
24 (define exception:bad-expression
25 (cons 'syntax-error "Bad expression"))
32 (define (documented? object)
33 (not (not (object-documentation object))))
40 (with-test-prefix "memoization"
42 (with-test-prefix "copy-tree"
44 (pass-if "(#t . #(#t))"
45 (let* ((foo (cons #t (vector #t)))
46 (bar (copy-tree foo)))
47 (vector-set! (cdr foo) 0 #f)
48 (equal? bar '(#t . #(#t)))))
50 (pass-if-exception "circular lists in forms"
51 exception:bad-expression
52 (let ((foo (list #f)))
56 (pass-if "transparency"
58 (eval x (current-module))
59 (equal? '(begin 1) x))))
66 (with-test-prefix "evaluator"
68 (with-test-prefix "symbol lookup"
70 (with-test-prefix "top level"
72 (with-test-prefix "unbound"
74 (pass-if-exception "variable reference"
78 (pass-if-exception "procedure"
82 (with-test-prefix "parameter error"
84 ;; This is currently a bug in guile:
85 ;; Macros are accepted as function parameters.
86 ;; Functions that 'apply' macros are rewritten!!!
88 (expect-fail-exception "macro as argument"
89 exception:wrong-type-arg
90 (let ((f (lambda (p a b) (p a b))))
93 (expect-fail-exception "passing macro as parameter"
94 exception:wrong-type-arg
95 (let* ((f (lambda (p a b) (p a b)))
96 (foo (procedure-source f)))
98 (equal? (procedure-source f) foo)))
106 (with-test-prefix "call"
108 (with-test-prefix "wrong number of arguments"
110 (pass-if-exception "((lambda () #f) 1)"
111 exception:wrong-num-args
114 (pass-if-exception "((lambda (x) #f))"
115 exception:wrong-num-args
118 (pass-if-exception "((lambda (x) #f) 1 2)"
119 exception:wrong-num-args
120 ((lambda (x) #f) 1 2))
122 (pass-if-exception "((lambda (x y) #f))"
123 exception:wrong-num-args
126 (pass-if-exception "((lambda (x y) #f) 1)"
127 exception:wrong-num-args
128 ((lambda (x y) #f) 1))
130 (pass-if-exception "((lambda (x y) #f) 1 2 3)"
131 exception:wrong-num-args
132 ((lambda (x y) #f) 1 2 3))
134 (pass-if-exception "((lambda (x . rest) #f))"
135 exception:wrong-num-args
136 ((lambda (x . rest) #f)))
138 (pass-if-exception "((lambda (x y . rest) #f))"
139 exception:wrong-num-args
140 ((lambda (x y . rest) #f)))
142 (pass-if-exception "((lambda (x y . rest) #f) 1)"
143 exception:wrong-num-args
144 ((lambda (x y . rest) #f) 1))))
150 (with-test-prefix "apply"
152 (with-test-prefix "scm_tc7_subr_2o"
154 ;; prior to guile 1.6.9 and 1.8.1 this called the function with
155 ;; SCM_UNDEFIEND, which in the case of make-vector resulted in
156 ;; wrong-type-arg, instead of the intended wrong-num-args
157 (pass-if-exception "0 args" exception:wrong-num-args
158 (apply make-vector '()))
161 (vector? (apply make-vector '(1))))
164 (vector? (apply make-vector '(1 2))))
166 ;; prior to guile 1.6.9 and 1.8.1 this error wasn't detected
167 (pass-if-exception "3 args" exception:wrong-num-args
168 (apply make-vector '(1 2 3)))))
174 (with-test-prefix "map"
176 ;; Is documentation available?
178 (expect-fail "documented?"
181 (with-test-prefix "argument error"
183 (with-test-prefix "non list argument"
186 (with-test-prefix "different length lists"
188 (pass-if-exception "first list empty"
189 exception:out-of-range
192 (pass-if-exception "second list empty"
193 exception:out-of-range
196 (pass-if-exception "first list shorter"
197 exception:out-of-range
200 (pass-if-exception "second list shorter"
201 exception:out-of-range
206 ;;; define with procedure-name
209 (define old-procnames-flag (memq 'procnames (debug-options)))
210 (debug-enable 'procnames)
212 ;; names are only set on top-level procedures (currently), so these can't be
215 (define foo-closure (lambda () "hello"))
216 (define bar-closure foo-closure)
217 (define foo-pws (make-procedure-with-setter car set-car!))
218 (define bar-pws foo-pws)
220 (with-test-prefix "define set procedure-name"
223 (eq? 'foo-closure (procedure-name bar-closure)))
225 (pass-if "procedure-with-setter"
226 (eq? 'foo-pws (pk (procedure-name bar-pws)))))
228 (if old-procnames-flag
229 (debug-enable 'procnames)
230 (debug-disable 'procnames))
236 (with-test-prefix "promises"
238 (with-test-prefix "basic promise behaviour"
240 (pass-if "delay gives a promise"
241 (promise? (delay 1)))
243 (pass-if "force evaluates a promise"
244 (eqv? (force (delay (+ 1 2))) 3))
246 (pass-if "a forced promise is a promise"
247 (let ((p (delay (+ 1 2))))
251 (pass-if "forcing a forced promise works"
252 (let ((p (delay (+ 1 2))))
256 (pass-if "a promise is evaluated once"
263 (pass-if "a promise may call itself"
269 (if (> x 1) x (force p))))))
272 (pass-if "a promise carries its environment"
275 (set! p (delay (+ x 1))))
278 (pass-if "a forced promise does not reference its environment"
279 (let* ((g (make-guardian))
281 (let* ((x (cons #f #f)))
283 (set! p (delay (car x))))
286 (if (not (equal? (g) (cons #f #f)))
290 (with-test-prefix "extended promise behaviour"
292 (pass-if-exception "forcing a non-promise object is not supported"
293 exception:wrong-type-arg
296 (pass-if-exception "implicit forcing is not supported"
297 exception:wrong-type-arg
298 (+ (delay (* 3 7)) 13))
300 ;; Tests that require the debugging evaluator...
301 (with-debugging-evaluator
303 (pass-if "unmemoizing a promise"
306 (false-if-exception (lazy-catch #t
308 (let ((f (lambda (g) (delay (g)))))
311 (set! stack (make-stack #t)))))
313 (%make-void-port "w"))
321 (define (stack->frames stack)
322 ;; Return the list of frames comprising STACK.
324 (>= i (stack-length stack)))
330 (with-test-prefix "stacks"
331 (with-debugging-evaluator
333 (pass-if "stack involving a subr"
334 ;; The subr involving the error must appear exactly once on the stack.
338 (lazy-catch 'wrong-type-arg
340 ;; Trigger a `wrong-type-arg' exception.
341 (fluid-ref 'not-a-fluid))
343 (let* ((stack (make-stack #t))
344 (frames (stack->frames stack)))
346 (count (lambda (frame)
347 (and (frame-procedure? frame)
348 (eq? (frame-procedure frame)
354 (pass-if "stack involving a gsubr"
355 ;; The gsubr involving the error must appear exactly once on the stack.
356 ;; This is less obvious since gsubr application may require an
357 ;; additional `SCM_APPLY ()' call, which should not be visible to the
362 (lazy-catch 'wrong-type-arg
364 ;; Trigger a `wrong-type-arg' exception.
365 (hashq-ref 'wrong 'type 'arg))
367 (let* ((stack (make-stack #t))
368 (frames (stack->frames stack)))
370 (count (lambda (frame)
371 (and (frame-procedure? frame)
372 (eq? (frame-procedure frame)
379 ;;; letrec init evaluation
382 (with-test-prefix "letrec init evaluation"
384 (pass-if "lots of inits calculated in correct order"
385 (equal? (letrec ((a 'a) (b 'b) (c 'c) (d 'd)
386 (e 'e) (f 'f) (g 'g) (h 'h)
387 (i 'i) (j 'j) (k 'k) (l 'l)
388 (m 'm) (n 'n) (o 'o) (p 'p)
389 (q 'q) (r 'r) (s 's) (t 't)
390 (u 'u) (v 'v) (w 'w) (x 'x)
392 (list a b c d e f g h i j k l m
393 n o p q r s t u v w x y z))
394 '(a b c d e f g h i j k l m
395 n o p q r s t u v w x y z))))
401 (with-test-prefix "values"
403 (pass-if "single value"
404 (equal? 1 (values 1)))
406 (pass-if "call-with-values"
407 (equal? (call-with-values (lambda () (values 1 2 3 4)) list)
411 (equal? (values 1 2 3 4) (values 1 2 3 4))))
413 ;;; eval.test ends here