Debian Codegraph @

test-suite/tests/eval.test

raw
1 ;;;; eval.test --- tests guile's evaluator -*- scheme -*-
2 ;;;; Copyright (C) 2000-2001,2003-2015,2017,2019,2020
3 ;;;; Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
9 ;;;;
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19 (define-module (test-suite test-eval)
20 :use-module (test-suite lib)
21 :use-module ((srfi srfi-1) :select (unfold count))
22 :use-module ((system vm vm) :select (call-with-stack-overflow-handler))
23 :use-module ((system vm frame) :select (frame-call-representation))
24 :use-module (ice-9 documentation)
25 :use-module (ice-9 exceptions)
26 :use-module (ice-9 local-eval))
27
28
29 (define exception:bad-expression
30 (cons 'syntax-error "Bad expression"))
31
32 (define exception:failed-match
33 (cons 'syntax-error "failed to match any pattern"))
34
35 (define exception:not-a-list
36 (cons 'wrong-type-arg "Not a list"))
37
38 (define exception:wrong-length
39 (cons 'wrong-type-arg "wrong length"))
40
41 ;;;
42 ;;; miscellaneous
43 ;;;
44
45 (define (documented? object)
46 (not (not (object-documentation object))))
47
48
49 ;;;
50 ;;; memoization
51 ;;;
52
53 (with-test-prefix "memoization"
54 (pass-if "transparency"
55 (let ((x '(begin 1)))
56 (eval x (current-module))
57 (equal? '(begin 1) x))))
58
59
60 ;;;
61 ;;; eval
62 ;;;
63
64 (with-test-prefix "evaluator"
65
66 (pass-if "definitions return #<unspecified>"
67 (eq? (primitive-eval '(define test-var 'foo))
68 (if #f #f)))
69
70 (with-test-prefix "symbol lookup"
71
72 (with-test-prefix "top level"
73
74 (with-test-prefix "unbound"
75
76 (pass-if-exception "variable reference"
77 exception:unbound-var
78 x)
79
80 (pass-if-exception "procedure"
81 exception:unbound-var
82 (x)))))
83
84 (with-test-prefix "parameter error"
85
86 ;; This is currently a bug in guile:
87 ;; Macros are accepted as function parameters.
88 ;; Functions that 'apply' macros are rewritten!!!
89
90 (pass-if-exception "macro as argument"
91 exception:failed-match
92 (primitive-eval
93 '(let ((f (lambda (p a b) (p a b))))
94 (f and #t #t))))
95
96 (pass-if-exception "passing macro as parameter"
97 exception:failed-match
98 (primitive-eval
99 '(let* ((f (lambda (p a b) (p a b)))
100 (foo (procedure-source f)))
101 (f and #t #t)
102 (equal? (procedure-source f) foo))))
103
104 ))
105
106 ;;;
107 ;;; call
108 ;;;
109
110 (with-test-prefix "call"
111
112 (with-test-prefix "wrong number of arguments"
113
114 (pass-if-exception "((lambda () #f) 1)"
115 exception:wrong-num-args
116 ((lambda () #f) 1))
117
118 (pass-if-exception "((lambda (x) #f))"
119 exception:wrong-num-args
120 ((lambda (x) #f)))
121
122 (pass-if-exception "((lambda (x) #f) 1 2)"
123 exception:wrong-num-args
124 ((lambda (x) #f) 1 2))
125
126 (pass-if-exception "((lambda (x y) #f))"
127 exception:wrong-num-args
128 ((lambda (x y) #f)))
129
130 (pass-if-exception "((lambda (x y) #f) 1)"
131 exception:wrong-num-args
132 ((lambda (x y) #f) 1))
133
134 (pass-if-exception "((lambda (x y) #f) 1 2 3)"
135 exception:wrong-num-args
136 ((lambda (x y) #f) 1 2 3))
137
138 (pass-if-exception "((lambda (x . rest) #f))"
139 exception:wrong-num-args
140 ((lambda (x . rest) #f)))
141
142 (pass-if-exception "((lambda (x y . rest) #f))"
143 exception:wrong-num-args
144 ((lambda (x y . rest) #f)))
145
146 (pass-if-exception "((lambda (x y . rest) #f) 1)"
147 exception:wrong-num-args
148 ((lambda (x y . rest) #f) 1))))
149
150 ;;;
151 ;;; apply
152 ;;;
153
154 (with-test-prefix "apply"
155
156 (with-test-prefix "scm_tc7_subr_2o"
157
158 ;; prior to guile 1.6.9 and 1.8.1 this called the function with
159 ;; SCM_UNDEFINED, which in the case of make-vector resulted in
160 ;; wrong-type-arg, instead of the intended wrong-num-args
161 (pass-if-exception "0 args" exception:wrong-num-args
162 (apply make-vector '()))
163
164 (pass-if "1 arg"
165 (vector? (apply make-vector '(1))))
166
167 (pass-if "2 args"
168 (vector? (apply make-vector '(1 2))))
169
170 ;; prior to guile 1.6.9 and 1.8.1 this error wasn't detected
171 (pass-if-exception "3 args" exception:wrong-num-args
172 (apply make-vector '(1 2 3)))))
173
174 ;;;
175 ;;; map
176 ;;;
177
178 (with-test-prefix "map"
179
180 ;; Is documentation available?
181
182 (expect-fail "documented?"
183 (documented? map))
184
185 (with-test-prefix "argument error"
186
187 (with-test-prefix "non list argument"
188 #t)
189
190 (with-test-prefix "different length lists"
191
192 (pass-if-exception "first list empty"
193 exception:wrong-length
194 (map + '() '(1)))
195
196 (pass-if-exception "second list empty"
197 exception:wrong-length
198 (map + '(1) '()))
199
200 (pass-if-exception "first list shorter"
201 exception:wrong-length
202 (map + '(1) '(2 3)))
203
204 (pass-if-exception "second list shorter"
205 exception:wrong-length
206 (map + '(1 2) '(3)))
207 )))
208
209 (with-test-prefix "for-each"
210
211 (pass-if-exception "1 arg, non-list, even number of elements"
212 exception:not-a-list
213 (for-each values '(1 2 3 4 . 5)))
214
215 (pass-if-exception "1 arg, non-list, odd number of elements"
216 exception:not-a-list
217 (for-each values '(1 2 3 . 4))))
218
219 ;;;
220 ;;; define with procedure-name
221 ;;;
222
223 ;; names are only set on top-level procedures (currently), so these can't be
224 ;; hidden in a let
225 ;;
226 (define foo-closure (lambda () "hello"))
227 (define bar-closure foo-closure)
228 ;; make sure that make-procedure-with-setter returns an anonymous
229 ;; procedure-with-setter by passing it an anonymous getter.
230 (define foo-pws (make-procedure-with-setter
231 (lambda (x) (car x))
232 (lambda (x y) (set-car! x y))))
233 (define bar-pws foo-pws)
234
235 (with-test-prefix "define set procedure-name"
236
237 (pass-if "closure"
238 (eq? 'foo-closure (procedure-name bar-closure)))
239
240 (expect-fail "procedure-with-setter" ; FIXME: `pass-if' when it's supported
241 (eq? 'foo-pws (procedure-name bar-pws))))
242
243 ;;;
244 ;;; promises
245 ;;;
246
247 (with-test-prefix "promises"
248
249 (with-test-prefix "basic promise behaviour"
250
251 (pass-if "delay gives a promise"
252 (promise? (delay 1)))
253
254 (pass-if "force evaluates a promise"
255 (eqv? (force (delay (+ 1 2))) 3))
256
257 (pass-if "a forced promise is a promise"
258 (let ((p (delay (+ 1 2))))
259 (force p)
260 (promise? p)))
261
262 (pass-if "forcing a forced promise works"
263 (let ((p (delay (+ 1 2))))
264 (force p)
265 (eqv? (force p) 3)))
266
267 (pass-if "a promise is evaluated once"
268 (let* ((x 1)
269 (p (delay (+ x 1))))
270 (force p)
271 (set! x (+ x 1))
272 (eqv? (force p) 2)))
273
274 (pass-if "a promise may call itself"
275 (define p
276 (let ((x 0))
277 (delay
278 (begin
279 (set! x (+ x 1))
280 (if (> x 1) x (force p))))))
281 (eqv? (force p) 2))
282
283 (pass-if "a promise carries its environment"
284 (let* ((x 1) (p #f))
285 (let* ((x 2))
286 (set! p (delay (+ x 1))))
287 (eqv? (force p) 3)))
288
289 (pass-if "a forced promise does not reference its environment"
290 (let* ((g (make-guardian))
291 (p #f))
292 (let* ((x (cons #f #f)))
293 (g x)
294 (set! p (delay (car x))))
295 (force p)
296 (gc)
297 ;; Though this test works reliably when running just eval.test,
298 ;; it often does the unresolved case when running the full
299 ;; suite. Adding this extra gc makes the full-suite behavior
300 ;; pass more reliably.
301 (gc)
302 (if (not (equal? (g) (cons #f #f)))
303 (throw 'unresolved)
304 #t))))
305
306 (with-test-prefix "extended promise behaviour"
307
308 (pass-if-exception "forcing a non-promise object is not supported"
309 exception:wrong-type-arg
310 (force 1))
311
312 (pass-if "unmemoizing a promise"
313 (display-backtrace
314 (let ((stack #f))
315 (false-if-exception
316 (with-throw-handler #t
317 (lambda ()
318 (let ((f (lambda (g) (delay (g)))))
319 (force (f error))))
320 (lambda _
321 (set! stack (make-stack #t)))))
322 stack)
323 (%make-void-port "w"))
324 #t)))
325
326
327 ;;;
328 ;;; stacks
329 ;;;
330
331 (define (stack->frames stack)
332 ;; Return the list of frames comprising STACK.
333 (unfold (lambda (i)
334 (>= i (stack-length stack)))
335 (lambda (i)
336 (stack-ref stack i))
337 1+
338 0))
339
340 (define (make-tagged-trimmed-stack tag spec)
341 (catch 'result
342 (lambda ()
343 (call-with-prompt
344 tag
345 (lambda ()
346 (with-throw-handler 'wrong-type-arg
347 (lambda () (substring 'wrong 'type 'arg))
348 (lambda _ (throw 'result (apply make-stack spec)))))
349 (lambda () (throw 'make-stack-failed))))
350 (lambda (key result) result)))
351
352 (define tag (make-prompt-tag "foo"))
353
354 (with-test-prefix "stacks"
355 (pass-if "stack involving a primitive"
356 ;; The primitive involving the error must appear exactly once on the
357 ;; stack.
358 (let* ((stack (make-tagged-trimmed-stack tag '(#t)))
359 (frames (stack->frames stack))
360 (num (count (lambda (frame) (eq? (frame-procedure-name frame)
361 'substring))
362 frames)))
363 (= num 1)))
364
365 (pass-if "arguments of a primitive stack frame"
366 ;; Create a stack with two primitive frames and make sure the
367 ;; arguments are correct.
368 (let* ((stack (make-tagged-trimmed-stack tag '(#t)))
369 (call-list (map frame-call-representation (stack->frames stack))))
370 (and (equal? (car call-list) '(make-stack #t))
371 (pair? (member '(substring wrong type arg)
372 (cdr call-list))))))
373
374 (pass-if "inner trim with prompt tag"
375 (let* ((stack (make-tagged-trimmed-stack tag `(#t ,tag)))
376 (frames (stack->frames stack)))
377 ;; the top frame on the stack is the body of the catch, and the
378 ;; next frame is the with-exception-handler corresponding to the
379 ;; (catch 'result ...)
380 (eq? (car (frame-call-representation (cadr frames)))
381 'with-exception-handler)))
382
383 (pass-if "outer trim with prompt tag"
384 (let* ((stack (make-tagged-trimmed-stack tag `(#t 0 ,tag)))
385 (frames (stack->frames stack)))
386 ;; the top frame on the stack is the make-stack call, and the last
387 ;; frame is the (with-throw-handler 'wrong-type-arg ...)
388 (and (eq? (car (frame-call-representation (car frames)))
389 'make-stack)
390 (eq? (car (frame-call-representation (car (last-pair frames))))
391 'with-exception-handler))))
392
393 (pass-if "avoid frame-local-ref out of range"
394 (with-exception-handler
395 (lambda (ex)
396 ;; If frame-call-representation fails, we'll catch that
397 ;; instead of the expected "Wrong type to apply" error.
398 (string-prefix? "Wrong type to apply" (exception-message ex)))
399 (lambda ()
400 (with-exception-handler
401 (lambda (ex)
402 (let* ((stack (make-stack #t))
403 (frames (stack->frames stack)))
404 (for-each frame-call-representation frames))
405 (raise-exception ex))
406 (lambda ()
407 ;; This throws a "Wrong type to apply" error, creating a
408 ;; frame with an incorrect number of local slots as
409 ;; described in bug <https://bugs.gnu.org/56493>.
410 (primitive-eval '(define foo (#t))))))
411 #:unwind? #t)))
412
413 ;;;
414 ;;; letrec init evaluation
415 ;;;
416
417 (with-test-prefix "letrec init evaluation"
418
419 (pass-if "lots of inits calculated in correct order"
420 (equal? (letrec ((a 'a) (b 'b) (c 'c) (d 'd)
421 (e 'e) (f 'f) (g 'g) (h 'h)
422 (i 'i) (j 'j) (k 'k) (l 'l)
423 (m 'm) (n 'n) (o 'o) (p 'p)
424 (q 'q) (r 'r) (s 's) (t 't)
425 (u 'u) (v 'v) (w 'w) (x 'x)
426 (y 'y) (z 'z))
427 (list a b c d e f g h i j k l m
428 n o p q r s t u v w x y z))
429 '(a b c d e f g h i j k l m
430 n o p q r s t u v w x y z))))
431
432 ;;;
433 ;;; values
434 ;;;
435
436 (with-test-prefix "values"
437
438 (pass-if "single value"
439 (equal? 1 (values 1)))
440
441 (pass-if "call-with-values"
442 (equal? (call-with-values (lambda () (values 1 2 3 4)) list)
443 '(1 2 3 4)))
444
445 (pass-if "equal?"
446 (equal? (values 1 2 3 4) (values 1 2 3 4))))
447
448 ;;;
449 ;;; stack overflow handling
450 ;;;
451
452 (with-test-prefix "stack overflow handlers"
453 (define (trigger-overflow)
454 (trigger-overflow)
455 (error "not reached"))
456
457 (define (dynwind-test n)
458 (catch 'foo
459 (lambda ()
460 (call-with-stack-overflow-handler n
461 (lambda ()
462 (dynamic-wind (lambda () #t)
463 trigger-overflow
464 trigger-overflow))
465 (lambda ()
466 (throw 'foo))))
467 (lambda _ #t)))
468
469 (pass-if-exception "limit should be number"
470 exception:wrong-type-arg
471 (call-with-stack-overflow-handler #t
472 trigger-overflow trigger-overflow))
473
474 (pass-if-exception "limit should be exact integer"
475 exception:wrong-type-arg
476 (call-with-stack-overflow-handler 2.0
477 trigger-overflow trigger-overflow))
478
479 (pass-if-exception "limit should be non-negative"
480 exception:out-of-range
481 (call-with-stack-overflow-handler -1
482 trigger-overflow trigger-overflow))
483
484 (pass-if-exception "limit should be positive"
485 exception:out-of-range
486 (call-with-stack-overflow-handler 0
487 trigger-overflow trigger-overflow))
488
489 (pass-if-exception "limit should be within address space"
490 exception:out-of-range
491 (call-with-stack-overflow-handler (ash 1 64)
492 trigger-overflow trigger-overflow))
493
494 (pass-if "exception on overflow"
495 (catch 'foo
496 (lambda ()
497 (call-with-stack-overflow-handler 10000
498 trigger-overflow
499 (lambda ()
500 (throw 'foo))))
501 (lambda _ #t)))
502
503 (pass-if "exception on overflow with dynwind"
504 ;; Try all limits between 1 and 200 words.
505 (let lp ((n 1))
506 (or (= n 200)
507 (and (dynwind-test n)
508 (lp (1+ n))))))
509
510 (pass-if-exception "overflow handler should return number"
511 exception:wrong-type-arg
512 (call-with-stack-overflow-handler 1000
513 trigger-overflow
514 (lambda () #t)))
515 (pass-if-exception "overflow handler should return exact integer"
516 exception:wrong-type-arg
517 (call-with-stack-overflow-handler 1000
518 trigger-overflow
519 (lambda () 2.0)))
520 (pass-if-exception "overflow handler should be non-negative"
521 exception:out-of-range
522 (call-with-stack-overflow-handler 1000
523 trigger-overflow
524 (lambda () -1)))
525 (pass-if-exception "overflow handler should be positive"
526 exception:out-of-range
527 (call-with-stack-overflow-handler 1000
528 trigger-overflow
529 (lambda () 0)))
530
531 (letrec ((fac (lambda (n)
532 (if (zero? n) 1 (* n (fac (1- n)))))))
533 (pass-if-equal "overflow handler can allow recursion to continue"
534 (fac 10)
535 (call-with-stack-overflow-handler 1
536 (lambda () (fac 10))
537 (lambda () 1)))))
538
539 ;;;
540 ;;; docstrings
541 ;;;
542
543 (with-test-prefix "docstrings"
544
545 (pass-if-equal "fixed closure"
546 '("hello" "world")
547 (map procedure-documentation
548 (list (eval '(lambda (a b) "hello" (+ a b))
549 (current-module))
550 (eval '(lambda (a b) "world" (- a b))
551 (current-module)))))
552
553 (pass-if-equal "fixed closure with many args"
554 "So many args."
555 (procedure-documentation
556 (eval '(lambda (a b c d e f g h i j k)
557 "So many args."
558 (+ a b))
559 (current-module))))
560
561 (pass-if-equal "general closure"
562 "How general."
563 (procedure-documentation
564 (eval '(lambda* (a b #:key k #:rest r)
565 "How general."
566 (+ a b))
567 (current-module)))))
568
569 ;;;
570 ;;; local-eval
571 ;;;
572
573 (with-test-prefix "local evaluation"
574
575 (pass-if "local-eval"
576
577 (let* ((env1 (local-eval '(let ((x 1) (y 2) (z 3))
578 (define-syntax-rule (foo x) (quote x))
579 (the-environment))
580 (current-module)))
581 (env2 (local-eval '(let ((x 111) (a 'a))
582 (define-syntax-rule (bar x) (quote x))
583 (the-environment))
584 env1)))
585 (local-eval '(set! x 11) env1)
586 (local-eval '(set! y 22) env1)
587 (local-eval '(set! z 33) env2)
588 (and (equal? (local-eval '(list x y z) env1)
589 '(11 22 33))
590 (equal? (local-eval '(list x y z a) env2)
591 '(111 22 33 a)))))
592
593 (pass-if "local-compile"
594
595 (let* ((env1 (local-compile '(let ((x 1) (y 2) (z 3))
596 (define-syntax-rule (foo x) (quote x))
597 (the-environment))
598 (current-module)))
599 (env2 (local-compile '(let ((x 111) (a 'a))
600 (define-syntax-rule (bar x) (quote x))
601 (the-environment))
602 env1)))
603 (local-compile '(set! x 11) env1)
604 (local-compile '(set! y 22) env1)
605 (local-compile '(set! z 33) env2)
606 (and (equal? (local-compile '(list x y z) env1)
607 '(11 22 33))
608 (equal? (local-compile '(list x y z a) env2)
609 '(111 22 33 a)))))
610
611 (pass-if "the-environment within a macro"
612 (let ((module-a-name '(test module the-environment a))
613 (module-b-name '(test module the-environment b)))
614 (let ((module-a (resolve-module module-a-name))
615 (module-b (resolve-module module-b-name)))
616 (module-use! module-a (resolve-interface '(guile)))
617 (module-use! module-a (resolve-interface '(ice-9 local-eval)))
618 (eval '(begin
619 (define z 3)
620 (define-syntax-rule (test)
621 (let ((x 1) (y 2))
622 (the-environment))))
623 module-a)
624 (module-use! module-b (resolve-interface '(guile)))
625 (let ((env (local-eval `(let ((x 111) (y 222))
626 ((@@ ,module-a-name test)))
627 module-b)))
628 (equal? (local-eval '(list x y z) env)
629 '(1 2 3))))))
630
631 (pass-if "capture pattern variables"
632 (let ((env (syntax-case #'(((a 1) (b 2) (c 3))
633 ((d 4) (e 5) (f 6))) ()
634 ((((k v) ...) ...) (the-environment)))))
635 (equal? (syntax->datum (local-eval '#'((k ... v ...) ...) env))
636 '((a b c 1 2 3) (d e f 4 5 6)))))
637
638 (pass-if "mixed primitive-eval, local-eval and local-compile"
639
640 (let* ((env1 (primitive-eval '(let ((x 1) (y 2) (z 3))
641 (define-syntax-rule (foo x) (quote x))
642 (the-environment))))
643 (env2 (local-eval '(let ((x 111) (a 'a))
644 (define-syntax-rule (bar x) (quote x))
645 (the-environment))
646 env1))
647 (env3 (local-compile '(let ((y 222) (b 'b))
648 (the-environment))
649 env2)))
650 (local-eval '(set! x 11) env1)
651 (local-compile '(set! y 22) env2)
652 (local-eval '(set! z 33) env2)
653 (local-compile '(set! a (* y 2)) env3)
654 (and (equal? (local-compile '(list x y z) env1)
655 '(11 22 33))
656 (equal? (local-eval '(list x y z a) env2)
657 '(111 22 33 444))
658 (equal? (local-eval '(list x y z a b) env3)
659 '(111 222 33 444 b))))))
660
661 ;;; eval.test ends here