File: test-legacy-macro.scm

package info (click to toggle)
sigscheme 0.8.5-4.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,752 kB
  • ctags: 7,396
  • sloc: lisp: 37,498; ansic: 30,976; sh: 10,329; makefile: 746; asm: 333; ruby: 288
file content (232 lines) | stat: -rw-r--r-- 6,119 bytes parent folder | download | duplicates (17)
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
225
226
227
228
229
230
231
232
;;  Filename : test-legacy-macro.scm
;;  About    : unit tests for legacy define-macro
;;
;;  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.

;; SigScheme does not have gensym.

(require-extension (unittest))


(test-begin "define-macro invalid forms")
;; invalid identifier
(test-error (define-macro #\m (lambda () #t)))
(test-error (define-macro "m" (lambda () #f)))
;; invalid closure
(test-error (define-macro m '(lambda () #t)))
(test-error (define-macro m #f))
(test-error m)  ;; must be unbound here
;; syntactic keyword
(test-error (define-macro m and))
;; another macro as alias
(define-macro m (lambda () #t))
(test-error (define-macro m2 m))
(test-end)

(test-begin "define-macro non-toplevel env")
(cond-expand
 (sigscheme
  ;; SigScheme does not accept non-toplevel env for syntactic closures.
  (test-error (define-macro m
                (let ((?var 'val))
                  (lambda ()
                    ``,?var)))))
 (else
  (define-macro m
    (let ((?var 'val))
      (lambda ()
        ``,?var)))
  (test-eq 'val (m))))
(test-end)

(test-begin "define-macro bad definition placement")
;; non-toplevel definition
(test-error (if #t (define-macro m
                     (lambda ()
                       '(define foo 3)))))
;; non-toplevel definition by a form returned by syntactic closure
(define-macro m
  (lambda ()
    '(define foo 3)))
(test-error (if #t (m)))
(test-end)

(test-begin "define-macro referring runtime env")
(define cnt 0)
(define-macro m
  (lambda ()
    (set! cnt (+ cnt 1))
    cnt))
;; The macro is expanded for each instantiation.
(test-eqv 1 (m))
(test-eqv 2 (m))
(test-eqv 3 (m))
(define proc-m
  (lambda ()
    (m)))
(cond-expand
 (sigscheme
  ;; SigScheme expands the macro for each procedure call.
  (test-eqv 4 (proc-m))
  (test-eqv 5 (proc-m))
  (test-eqv 6 (proc-m)))
 (else
  ;; Ordinary implementations expand the macro only once.
  (test-eqv 4 (proc-m))
  (test-eqv 4 (proc-m))
  (test-eqv 4 (proc-m))))
(test-end)

(test-begin "define-macro varname conflict")
(define foo 1)
(define bar 2)
(define tmp 3)
(define-macro swap
  (lambda (x y)
    `(let ((tmp ,x))
       (set! ,x ,y)
       (set! ,y tmp))))
(swap foo bar)
(test-eqv 2 foo)
(test-eqv 1 bar)
(test-eqv 3 tmp)
(swap foo bar)
(test-eqv 1 foo)
(test-eqv 2 bar)
(test-eqv 3 tmp)
(swap foo tmp)
(test-eqv 1 foo)
(test-eqv 2 bar)
(test-eqv 3 tmp)
(swap foo tmp)
(test-eqv 1 foo)
(test-eqv 2 bar)
(test-eqv 3 tmp)
(test-end)

(test-begin "define-macro evaluation timings")
(define foo 3)
(define bar 4)
(define-macro m
  (lambda ()
    '(+ foo bar)))
(define-macro m2
  (lambda ()
    (+ foo bar)))
(define proc-m
  (lambda ()
    (m)))
(define proc-m2
  (lambda ()
    (m2)))
(test-eqv 7 (m))
(test-eqv 7 (m2))
(test-eqv 7 (proc-m))
(test-eqv 7 (proc-m2))
(set! foo 5)
(test-eqv 9 (m))
(test-eqv 9 (m2))
(test-eqv 9 (proc-m))
(cond-expand
 (sigscheme
  (test-eqv 9 (proc-m2)))
 (else
  (test-eqv 7 (proc-m2))))
(test-end)

(test-begin "define-macro syntactic keywords handling")
(define-macro m
  (lambda (op x y)
    `(,op ,x ,y)))
(test-false (m and #f #f))
(test-false (m and #f #t))
(test-false (m and #t #f))
(test-true  (m and #t #t))
(test-false (m or  #f #f))
(test-true  (m or  #f #t))
(test-true  (m or  #t #f))
(test-true  (m or  #t #t))
(define-macro m
  (lambda args
    `,args))
(test-false (m and #f #f))
(test-false (m and #f #t))
(test-false (m and #t #f))
(test-true  (m and #t #t))
(test-true  (m and #t #t #t))
(test-false (m or  #f #f))
(test-true  (m or  #f #t))
(test-true  (m or  #t #f))
(test-true  (m or  #t #t))
(test-true  (m or  #t #t #f))
(define-macro (m op x y)
    `(,op ,x ,y))
(test-false (m and #f #f))
(test-false (m and #f #t))
(test-false (m and #t #f))
(test-true  (m and #t #t))
(test-false (m or  #f #f))
(test-true  (m or  #f #t))
(test-true  (m or  #t #f))
(test-true  (m or  #t #t))
(define-macro (m . args)
  `,args)
(test-false (m and #f #f))
(test-false (m and #f #t))
(test-false (m and #t #f))
(test-true  (m and #t #t))
(test-true  (m and #t #t #t))
(test-false (m or  #f #f))
(test-true  (m or  #f #t))
(test-true  (m or  #t #f))
(test-true  (m or  #t #t))
(test-true  (m or  #t #t #f))
(test-end)

(test-begin "define-macro nested macro")
(define-macro swap
  (lambda (x y)
    `(let ((?tmp ,x))
       (set! ,x ,y)
       (set! ,y ?tmp))))
(define-macro m
  (lambda (x y)
    `(begin
       (swap ,x ,y)
       (- ,x ,y))))
(define foo 3)
(define bar 4)
(test-eqv -1 (- foo bar))
(test-eqv 1  (m foo bar))
(test-eqv 4 foo)
(test-eqv 3 bar)
(test-end)

(test-report-result)