remove vm-version, vm options
[bpt/guile.git] / test-suite / tests / compiler.test
1 ;;;; compiler.test --- tests for the compiler -*- scheme -*-
2 ;;;; Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
3 ;;;;
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 3 of the License, or (at your option) any later version.
8 ;;;;
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.
13 ;;;;
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
17
18 (define-module (test-suite tests compiler)
19 #:use-module (test-suite lib)
20 #:use-module (test-suite guile-test)
21 #:use-module (system base compile)
22 #:use-module ((system vm vm) #:select (the-vm vm-apply))
23 #:use-module ((system vm program) #:select (make-program
24 program-sources source:addr)))
25
26 (define read-and-compile
27 (@@ (system base compile) read-and-compile))
28
29
30 \f
31 (with-test-prefix "basic"
32
33 (pass-if "compile to value"
34 (equal? (compile 1) 1)))
35
36 \f
37 (with-test-prefix "psyntax"
38
39 (pass-if "compile uses a fresh module by default"
40 (begin
41 (compile '(define + -))
42 (eq? (compile '+) +)))
43
44 (pass-if "compile-time definitions are isolated"
45 (begin
46 (compile '(define foo-bar #t))
47 (not (module-variable (current-module) 'foo-bar))))
48
49 (pass-if "compile in current module"
50 (let ((o (begin
51 (compile '(define-macro (foo) 'bar)
52 #:env (current-module))
53 (compile '(let ((bar 'ok)) (foo))
54 #:env (current-module)))))
55 (and (macro? (module-ref (current-module) 'foo))
56 (eq? o 'ok))))
57
58 (pass-if "compile in fresh module"
59 (let* ((m (let ((m (make-module)))
60 (beautify-user-module! m)
61 m))
62 (o (begin
63 (compile '(define-macro (foo) 'bar) #:env m)
64 (compile '(let ((bar 'ok)) (foo)) #:env m))))
65 (and (module-ref m 'foo)
66 (eq? o 'ok))))
67
68 (pass-if "redefinition"
69 ;; In this case the locally-bound `round' must have the same value as the
70 ;; imported `round'. See the same test in `syntax.test' for details.
71 (let ((m (make-module)))
72 (beautify-user-module! m)
73 (compile '(define round round) #:env m)
74 (eq? round (module-ref m 'round)))))
75
76 \f
77 (with-test-prefix "current-reader"
78
79 (pass-if "default compile-time current-reader differs"
80 (not (eq? (compile 'current-reader)
81 current-reader)))
82
83 (pass-if "compile-time changes are honored and isolated"
84 ;; Make sure changing `current-reader' as the side-effect of a defmacro
85 ;; actually works.
86 (let ((r (fluid-ref current-reader))
87 (input (open-input-string
88 "(define-macro (install-reader!)
89 ;;(format #t \"current-reader = ~A~%\" current-reader)
90 (fluid-set! current-reader
91 (let ((first? #t))
92 (lambda args
93 (if first?
94 (begin
95 (set! first? #f)
96 ''ok)
97 (read (open-input-string \"\"))))))
98 #f)
99 (install-reader!)
100 this-should-be-ignored")))
101 (and (eq? (vm-apply (the-vm) (make-program (read-and-compile input)) '())
102 'ok)
103 (eq? r (fluid-ref current-reader)))))
104
105 (pass-if "with eval-when"
106 (let ((r (fluid-ref current-reader)))
107 (compile '(eval-when (compile eval)
108 (fluid-set! current-reader (lambda args 'chbouib))))
109 (eq? (fluid-ref current-reader) r))))
110
111 \f
112 (with-test-prefix "procedure-name"
113
114 (pass-if "program"
115 (let ((m (make-module)))
116 (beautify-user-module! m)
117 (compile '(define (foo x) x) #:env m)
118 (eq? (procedure-name (module-ref m 'foo)) 'foo)))
119
120 (pass-if "program with lambda"
121 (let ((m (make-module)))
122 (beautify-user-module! m)
123 (compile '(define foo (lambda (x) x)) #:env m)
124 (eq? (procedure-name (module-ref m 'foo)) 'foo)))
125
126 (pass-if "subr"
127 (eq? (procedure-name waitpid) 'waitpid)))
128
129 \f
130 (with-test-prefix "program-sources"
131
132 (with-test-prefix "source info associated with IP 0"
133
134 ;; Tools like `(system vm coverage)' like it when source info is associated
135 ;; with IP 0 of a VM program, which corresponds to the entry point. See
136 ;; also <http://savannah.gnu.org/bugs/?29817> for details.
137
138 (pass-if "lambda"
139 (let ((s (program-sources (compile '(lambda (x) x)))))
140 (not (not (memv 0 (map source:addr s))))))
141
142 (pass-if "lambda*"
143 (let ((s (program-sources
144 (compile '(lambda* (x #:optional y) x)))))
145 (not (not (memv 0 (map source:addr s))))))
146
147 (pass-if "case-lambda"
148 (let ((s (program-sources
149 (compile '(case-lambda (() #t)
150 ((y) y)
151 ((y z) (list y z)))))))
152 (not (not (memv 0 (map source:addr s))))))))