summaryrefslogtreecommitdiff
path: root/test-suite/tests/compiler.test
blob: 619b16740d81121a0e478ad5f8f8367b38e6a0b5 (plain)
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
;;;; compiler.test --- tests for the compiler      -*- scheme -*-
;;;; Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
;;;; 
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;; 
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;;; Lesser General Public License for more details.
;;;; 
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

(define-module (tests compiler)
  #:use-module (test-suite lib)
  #:use-module (test-suite guile-test)
  #:use-module (system base compile)
  #:use-module ((system vm program) #:select (make-program
                                              program-sources source:addr)))

(define read-and-compile
  (@@ (system base compile) read-and-compile))



(with-test-prefix "basic"

  (pass-if "compile to value"
    (equal? (compile 1) 1)))


(with-test-prefix "psyntax"

  (pass-if "compile uses a fresh module by default"
    (begin
      (compile '(define + -))
      (eq? (compile '+) +)))

  (pass-if "compile-time definitions are isolated"
    (begin
      (compile '(define foo-bar #t))
      (not (module-variable (current-module) 'foo-bar))))

  (pass-if "compile in current module"
    (let ((o (begin
               (compile '(define-macro (foo) 'bar)
                        #:env (current-module))
               (compile '(let ((bar 'ok)) (foo))
                        #:env (current-module)))))
      (and (macro? (module-ref (current-module) 'foo))
           (eq? o 'ok))))

  (pass-if "compile in fresh module"
    (let* ((m  (let ((m (make-module)))
                 (beautify-user-module! m)
                 m))
           (o  (begin
                 (compile '(define-macro (foo) 'bar) #:env m)
                 (compile '(let ((bar 'ok)) (foo)) #:env m))))
      (and (module-ref m 'foo)
           (eq? o 'ok))))

  (pass-if "redefinition"
    ;; In this case the locally-bound `round' must have the same value as the
    ;; imported `round'.  See the same test in `syntax.test' for details.
    (let ((m (make-module)))
      (beautify-user-module! m)
      (compile '(define round round) #:env m)
      (eq? round (module-ref m 'round)))))


(with-test-prefix "current-reader"

  (pass-if "default compile-time current-reader differs"
    (not (eq? (compile 'current-reader)
              current-reader)))

  (pass-if "compile-time changes are honored and isolated"
    ;; Make sure changing `current-reader' as the side-effect of a defmacro
    ;; actually works.
    (let ((r     (fluid-ref current-reader))
          (input (open-input-string
                  "(define-macro (install-reader!)
                     ;;(format #t \"current-reader = ~A~%\" current-reader)
                     (fluid-set! current-reader
                                 (let ((first? #t))
                                   (lambda args
                                     (if first?
                                         (begin
                                           (set! first? #f)
                                           ''ok)
                                         (read (open-input-string \"\"))))))
                     #f)
                   (install-reader!)
                   this-should-be-ignored")))
      (and (eq? ((make-program (read-and-compile input)))
                'ok)
           (eq? r (fluid-ref current-reader)))))

  (pass-if "with eval-when"
    (let ((r (fluid-ref current-reader)))
      (compile '(eval-when (compile eval)
                  (fluid-set! current-reader (lambda args 'chbouib))))
      (eq? (fluid-ref current-reader) r))))


(with-test-prefix "procedure-name"

  (pass-if "program"
    (let ((m  (make-module)))
      (beautify-user-module! m)
      (compile '(define (foo x) x) #:env m)
      (eq? (procedure-name (module-ref m 'foo)) 'foo)))

  (pass-if "program with lambda"
    (let ((m  (make-module)))
      (beautify-user-module! m)
      (compile '(define foo (lambda (x) x)) #:env m)
      (eq? (procedure-name (module-ref m 'foo)) 'foo)))

  (pass-if "subr"
    (eq? (procedure-name waitpid) 'waitpid)))


(with-test-prefix "program-sources"

  (with-test-prefix "source info associated with IP 0"

    ;; Tools like `(system vm coverage)' like it when source info is associated
    ;; with IP 0 of a VM program, which corresponds to the entry point.  See
    ;; also <http://savannah.gnu.org/bugs/?29817> for details.

    (pass-if "lambda"
      (let ((s (program-sources (compile '(lambda (x) x)))))
        (not (not (memv 0 (map source:addr s))))))

    (pass-if "lambda*"
      (let ((s (program-sources
                (compile '(lambda* (x #:optional y) x)))))
        (not (not (memv 0 (map source:addr s))))))

    (pass-if "case-lambda"
      (let ((s (program-sources
                (compile '(case-lambda (()    #t)
                                       ((y)   y)
                                       ((y z) (list y z)))))))
        (not (not (memv 0 (map source:addr s))))))))

(with-test-prefix "case-lambda"
  (pass-if "self recursion to different clause"
    (equal? (with-output-to-string
              (lambda ()
                (let ()
                  (define t
                    (case-lambda
                      ((x)
                       (t x 'y))
                      ((x y)
                       (display (list x y))
                       (list x y))))
                  (display (t 'x)))))
            "(x y)(x y)")))