Added guile-primitive construct for references to primitives from Elisp.
[bpt/guile.git] / test-suite / tests / elisp-compiler.test
CommitLineData
d158fa62
DK
1;;;; elisp-compiler.test --- Test the compiler for Elisp.
2;;;;
3;;;; Copyright (C) 2009 Free Software Foundation, Inc.
4;;;; Daniel Kraft
5;;;;
6;;;; This library is free software; you can redistribute it and/or
7;;;; modify it under the terms of the GNU Lesser General Public
8;;;; License as published by the Free Software Foundation; either
9;;;; version 3 of the License, or (at your option) any later version.
10;;;;
11;;;; This library is distributed in the hope that it will be useful,
12;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14;;;; Lesser General Public License for more details.
15;;;;
16;;;; You should have received a copy of the GNU Lesser General Public
17;;;; License along with this library; if not, write to the Free Software
18;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20(define-module (test-elisp-compiler)
21 :use-module (test-suite lib)
22 :use-module (system base compile)
23 :use-module (language elisp runtime))
24
25
26; Macros to handle the compilation conveniently.
27
28(define-syntax compile-test
29 (syntax-rules (pass-if pass-if-exception)
30 ((_ (pass-if test-name exp))
31 (pass-if test-name (compile 'exp #:from 'elisp #:to 'value)))
a0899974
DK
32 ((_ (pass-if test-name exp #:opts opts))
33 (pass-if test-name (compile 'exp #:from 'elisp #:to 'value #:opts opts)))
d158fa62
DK
34 ((_ (pass-if-equal test-name result exp))
35 (pass-if test-name (equal? result
36 (compile 'exp #:from 'elisp #:to 'value))))
37 ((_ (pass-if-exception test-name exc exp))
38 (pass-if-exception test-name exc
39 (compile 'exp #:from 'elisp #:to 'value)))))
40
41(define-syntax with-test-prefix/compile
42 (syntax-rules ()
43 ((_ section-name exp ...)
44 (with-test-prefix section-name (compile-test exp) ...))))
45
46
47; Test control structures.
48; ========================
49
50(with-test-prefix/compile "Sequencing"
51
52 (pass-if-equal "progn" 1
53 (progn (setq a 0)
54 (setq a (1+ a))
fb66a47a
DK
55 a))
56
57 (pass-if "prog1"
58 (progn (setq a 0)
59 (setq b (prog1 a (setq a (1+ a))))
60 (and (= a 1) (= b 0))))
61
62 (pass-if "prog2"
63 (progn (setq a 0)
64 (setq b (prog2 (setq a (1+ a))
65 (setq a (1+ a))
66 (setq a (1+ a))))
67 (and (= a 3) (= b 2)))))
d158fa62
DK
68
69(with-test-prefix/compile "Conditionals"
70
71 (pass-if-equal "succeeding if" 1
72 (if t 1 2))
7d1a9782
DK
73 (pass-if "failing if"
74 (and (= (if nil
75 1
76 (setq a 2) (setq a (1+ a)) a)
77 3)
78 (equal (if nil 1) nil)))
79
80 (pass-if-equal "failing when" nil-value
81 (when nil 1 2 3))
82 (pass-if-equal "succeeding when" 42
83 (progn (setq a 0)
84 (when t (setq a 42) a)))
85
86 (pass-if-equal "failing unless" nil-value
87 (unless t 1 2 3))
88 (pass-if-equal "succeeding unless" 42
89 (progn (setq a 0)
90 (unless nil (setq a 42) a)))
d158fa62
DK
91
92 (pass-if-equal "empty cond" nil-value
93 (cond))
94 (pass-if-equal "all failing cond" nil-value
95 (cond (nil) (nil)))
96 (pass-if-equal "only condition" 5
97 (cond (nil) (5)))
98 (pass-if-equal "succeeding cond value" 42
99 (cond (nil) (t 42) (t 0)))
100 (pass-if-equal "succeeding cond side-effect" 42
101 (progn (setq a 0)
102 (cond (nil) (t (setq a 42) 1) (t (setq a 0)))
103 a)))
104
105(with-test-prefix/compile "Combining Conditions"
106
107 (pass-if-equal "empty and" t-value (and))
108 (pass-if-equal "failing and" nil-value (and 1 2 nil 3))
109 (pass-if-equal "succeeding and" 3 (and 1 2 3))
110
111 (pass-if-equal "empty or" nil-value (or))
112 (pass-if-equal "failing or" nil-value (or nil nil nil))
b6b9d596
DK
113 (pass-if-equal "succeeding or" 1 (or nil 1 nil 2 nil 3))
114
115 (pass-if-equal "not true" nil-value (not 1))
116 (pass-if-equal "not false" t-value (not nil)))
d158fa62
DK
117
118(with-test-prefix/compile "Iteration"
119
120 (pass-if-equal "failing while" 0
121 (progn (setq a 0)
122 (while nil (setq a 1))
123 a))
124 (pass-if-equal "running while" 120
125 (progn (setq prod 1
126 i 1)
127 (while (<= i 5)
128 (setq prod (* i prod))
129 (setq i (1+ i)))
7d1a9782
DK
130 prod))
131
132 (pass-if "dotimes"
133 (progn (setq a 0)
134 (setq count 100)
135 (setq b (dotimes (i count)
136 (setq j (1+ i))
137 (setq a (+ a j))))
138 (setq c (dotimes (i 10 42) nil))
fb66a47a
DK
139 (and (= a 5050) (equal b nil) (= c 42))))
140
141 (pass-if "dolist"
142 (let ((mylist '(7 2 5)))
143 (setq sum 0)
144 (setq a (dolist (i mylist)
145 (setq sum (+ sum i))))
146 (setq b (dolist (i mylist 5) 0))
147 (and (= sum (+ 7 2 5))
148 (equal a nil)
149 (equal mylist '(7 2 5))
150 (equal b 5)))))
d158fa62 151
35b2e41d
DK
152(with-test-prefix/compile "Exceptions"
153
154 (pass-if "catch without exception"
155 (and (setq a 0)
156 (= (catch 'foobar
157 (setq a (1+ a))
158 (setq a (1+ a))
159 a)
160 2)
161 (= (catch (+ 1 2) a) 2)))
162
163 ; FIXME: Figure out how to do this...
164 ;(pass-if-exception "uncaught exception" 'elisp-exception
165 ; (throw 'abc 1))
166
167 (pass-if "catch and throw"
168 (and (setq mylist '(1 2))
169 (= (catch 'abc (throw 'abc 2) 1) 2)
33da12ee 170 (= (catch 'abc (catch 'def (throw 'abc (1+ 0)) 2) 3) 1)
35b2e41d 171 (= (catch 'abc (catch 'def (throw 'def 1) 2) 3) 3)
33da12ee
DK
172 (= (catch mylist (catch '(1 2) (throw mylist 1) 2) 3) 1)))
173
174 (pass-if "unwind-protect"
175 (progn (setq a 0 b 1 c 1)
176 (catch 'exc
177 (unwind-protect (progn (setq a 1)
178 (throw 'exc 0))
179 (setq a 0)
180 (setq b 0)))
181 (unwind-protect nil (setq c 0))
182 (and (= a 0) (= b 0) (= c 0)
183 (= (unwind-protect 42 1 2 3) 42)))))
35b2e41d 184
d158fa62
DK
185
186; Test handling of variables.
187; ===========================
188
189(with-test-prefix/compile "Variable Setting/Referencing"
190
191 ; TODO: Check for variable-void error
192
193 (pass-if-equal "setq and reference" 6
570c12ac
DK
194 (progn (setq a 1 b 2 c 3)
195 (+ a b c)))
570c12ac 196 (pass-if-equal "setq value" 2
37099846
DK
197 (progn (setq a 1 b 2)))
198
199 (pass-if "set and symbol-value"
200 (progn (setq myvar 'a)
201 (and (= (set myvar 42) 42)
202 (= a 42)
203 (= (symbol-value myvar) 42))))
204 (pass-if "void variables"
205 (progn (setq a 1 b 2)
206 (and (eq (makunbound 'b) 'b)
207 (boundp 'a)
a0899974
DK
208 (not (boundp 'b)))))
209
210 (pass-if "disabled void check (all)"
211 (progn (makunbound 'a) a t)
212 #:opts '(#:disable-void-check all))
213 (pass-if "disabled void check (symbol list)"
214 (progn (makunbound 'a) a t)
215 #:opts '(#:disable-void-check (x y a b))))
d158fa62
DK
216
217(with-test-prefix/compile "Let and Let*"
218
219 (pass-if-equal "let without value" nil-value
220 (let (a (b 5)) a))
221 (pass-if-equal "basic let" 0
222 (progn (setq a 0)
223 (let ((a 1)
224 (b a))
225 b)))
fd40f371
DK
226
227 (pass-if "let*"
d158fa62 228 (progn (setq a 0)
fd40f371
DK
229 (and (let* ((a 1)
230 (b a))
231 (= b 1))
232 (let* (a b)
233 (setq a 1 b 2)
234 (and (= a 1) (= b 2)))
235 (= a 0)
236 (not (boundp 'b)))))
d158fa62
DK
237
238 (pass-if "local scope"
239 (progn (setq a 0)
240 (setq b (let (a)
241 (setq a 1)
242 a))
243 (and (= a 0)
244 (= b 1)))))
245
a6a5cf03
DK
246(with-test-prefix/compile "Lexical Scoping"
247
248 (pass-if "basic let semantics"
249 (and (setq a 1)
250 (lexical-let ((a 2) (b a))
251 (and (= a 2) (= b 1)))
252 (lexical-let* ((a 2) (b a))
253 (and (= a 2) (= b 2) (setq a 42) (= a 42)))
254 (= a 1)))
255
256 (pass-if "lexical scope with lexical-let's"
257 (and (setq a 1)
258 (defun dyna () a)
259 (lexical-let (a)
260 (setq a 2)
261 (and (= a 2) (= (dyna) 1)))
262 (= a 1)
263 (lexical-let* (a)
264 (setq a 2)
265 (and (= a 2) (= (dyna) 1)))
266 (= a 1)))
267
268 (pass-if "lexical scoping vs. symbol-value / set"
269 (and (setq a 1)
270 (lexical-let ((a 2))
271 (and (= a 2)
272 (= (symbol-value 'a) 1)
273 (set 'a 3)
274 (= a 2)
275 (= (symbol-value 'a) 3)))
276 (= a 3)))
277
278 (pass-if "let inside lexical-let"
279 (and (setq a 1 b 1)
280 (defun dynvals () (cons a b))
281 (lexical-let ((a 2))
282 (and (= a 2) (equal (dynvals) '(1 . 1))
283 (let ((a 3) (b a))
284 (and (= a 3) (= b 2)
285 (equal (dynvals) '(1 . 2))))
286 (let* ((a 4) (b a))
287 (and (= a 4) (= b 4)
288 (equal (dynvals) '(1 . 4))))
289 (= a 2)))
290 (= a 1)))
291
292 (pass-if "lambda args inside lexical-let"
293 (and (setq a 1)
294 (defun dyna () a)
295 (lexical-let ((a 2) (b 42))
296 (and (= a 2) (= (dyna) 1)
297 ((lambda (a) (and (= a 3) (= b 42) (= (dyna) 3))) 3)
298 (= a 2) (= (dyna) 1)))
299 (= a 1)))
300
301 (pass-if "closures"
302 (and (defun make-counter ()
303 (lexical-let ((cnt 0))
304 (lambda ()
305 (setq cnt (1+ cnt)))))
306 (setq c1 (make-counter) c2 (make-counter))
c61ec8e2
DK
307 (= ((guile-primitive apply) c1 '()) 1)
308 (= ((guile-primitive apply) c1 '()) 2)
309 (= ((guile-primitive apply) c1 '()) 3)
310 (= ((guile-primitive apply) c2 '()) 1)
311 (= ((guile-primitive apply) c2 '()) 2)
312 (= ((guile-primitive apply) c1 '()) 4)
313 (= ((guile-primitive apply) c2 '()) 3))))
a6a5cf03 314
d158fa62
DK
315(with-test-prefix/compile "defconst and defvar"
316
317 (pass-if-equal "defconst without docstring" 3.141
318 (progn (setq pi 3)
319 (defconst pi 3.141)
320 pi))
321 (pass-if-equal "defconst value" 'pi
322 (defconst pi 3.141 "Pi"))
323
324 (pass-if-equal "defvar without value" 42
325 (progn (setq a 42)
326 (defvar a)
327 a))
328 (pass-if-equal "defvar on already defined variable" 42
329 (progn (setq a 42)
330 (defvar a 1 "Some docstring is also ok")
331 a))
d158fa62 332 (pass-if-equal "defvar on undefined variable" 1
37099846
DK
333 (progn (makunbound 'a)
334 (defvar a 1)
d158fa62
DK
335 a))
336 (pass-if-equal "defvar value" 'a
337 (defvar a)))
338
339
340; Functions and lambda expressions.
341; =================================
342
343(with-test-prefix/compile "Lambda Expressions"
344
345 (pass-if-equal "required arguments" 3
346 ((lambda (a b c) c) 1 2 3))
347
348 (pass-if-equal "optional argument" 3
349 ((function (lambda (a &optional b c) c)) 1 2 3))
350 (pass-if-equal "optional missing" nil-value
351 ((lambda (&optional a) a)))
352
353 (pass-if-equal "rest argument" '(3 4 5)
354 ((lambda (a b &rest c) c) 1 2 3 4 5))
355 (pass-if-equal "rest missing" nil-value
356 ((lambda (a b &rest c) c) 1 2)))
357
358(with-test-prefix/compile "Function Definitions"
359
360 (pass-if-equal "defun" 3
361 (progn (defun test (a b) (+ a b))
362 (test 1 2)))
363 (pass-if-equal "defun value" 'test
37099846
DK
364 (defun test (a b) (+ a b)))
365
366 (pass-if "fset and symbol-function"
367 (progn (setq myfunc 'x x 5)
368 (and (= (fset myfunc 42) 42)
369 (= (symbol-function myfunc) 42)
370 (= x 5))))
371 (pass-if "void function values"
372 (progn (setq a 1)
373 (defun test (a b) (+ a b))
374 (fmakunbound 'a)
375 (fset 'b 5)
376 (and (fboundp 'b) (fboundp 'test)
377 (not (fboundp 'a))
e8f18b3f
DK
378 (= a 1))))
379
380 (pass-if "flet and flet*"
381 (progn (defun foobar () 42)
382 (defun test () (foobar))
383 (and (= (test) 42)
384 (flet ((foobar (lambda () 0))
385 (myfoo (symbol-function 'foobar)))
386 (and (= (myfoo) 42)
387 (= (test) 0)))
388 (flet* ((foobar (lambda () 0))
389 (myfoo (symbol-function 'foobar)))
390 (= (myfoo) 0))
391 (flet (foobar)
392 (defun foobar () 0)
393 (= (test) 0))
394 (= (test) 42)))))
d158fa62
DK
395
396(with-test-prefix/compile "Calling Functions"
397
398 (pass-if-equal "recursion" 120
399 (progn (defun factorial (n prod)
400 (if (zerop n)
401 prod
402 (factorial (1- n) (* prod n))))
403 (factorial 5 1)))
404
405 (pass-if "dynamic scoping"
406 (progn (setq a 0)
407 (defun foo ()
408 (setq a (1+ a))
409 a)
410 (defun bar (a)
411 (foo))
412 (and (= 43 (bar 42))
413 (zerop a)))))
b6b9d596
DK
414
415
9b5ff6a6
DK
416; Quoting and Backquotation.
417; ==========================
418
419(with-test-prefix/compile "Quotation"
420
421 (pass-if "quote"
422 (and (equal '42 42) (equal '"abc" "abc")
423 (equal '(1 2 (3 (4) x)) '(1 2 (3 (4) x)))
424 (not (equal '(1 2 (3 4 (x))) '(1 2 3 4 x)))
425 (equal '(1 2 . 3) '(1 2 . 3))))
426
427 (pass-if "simple backquote"
428 (and (equal (\` 42) 42)
429 (equal (\` (1 (a))) '(1 (a)))
430 (equal (\` (1 . 2)) '(1 . 2))))
431 (pass-if "unquote"
432 (progn (setq a 42 l '(18 12))
433 (and (equal (\` (\, a)) 42)
434 (equal (\` (1 a ((\, l)) . (\, a))) '(1 a ((18 12)) . 42)))))
435 (pass-if "unquote splicing"
436 (progn (setq l '(18 12) empty '())
437 (and (equal (\` (\,@ l)) '(18 12))
438 (equal (\` (l 2 (3 (\,@ l)) ((\,@ l)) (\,@ l)))
439 '(l 2 (3 18 12) (18 12) 18 12))
440 (equal (\` (1 2 (\,@ empty) 3)) '(1 2 3))))))
441
442
443
74c009da
DK
444; Macros.
445; =======
446
447(with-test-prefix/compile "Macros"
448
449 (pass-if-equal "defmacro value" 'magic-number
450 (defmacro magic-number () 42))
451
452 (pass-if-equal "macro expansion" 1
453 (progn (defmacro take-first (a b) a)
454 (take-first 1 (/ 1 0)))))
455
456
b6b9d596
DK
457; Test the built-ins.
458; ===================
459
e905e490
DK
460(with-test-prefix/compile "Equivalence Predicates"
461
462 (pass-if "equal"
463 (and (equal 2 2) (not (equal 1 2))
464 (equal "abc" "abc") (not (equal "abc" "ABC"))
465 (equal 'abc 'abc) (not (equal 'abc 'def))
466 (equal '(1 2 (3 4) 5) '(1 2 (3 4) 5))
467 (not (equal '(1 2 3 4 5) '(1 2 (3 4) 5)))))
468
469 (pass-if "eq"
470 (progn (setq some-list '(1 2))
471 (setq some-string "abc")
472 (and (eq 2 2) (not (eq 1 2))
473 (eq 'abc 'abc) (not (eq 'abc 'def))
474 (eq some-string some-string) (not (eq some-string "abc"))
475 (eq some-list some-list) (not (eq some-list '(1 2)))))))
476
b6b9d596
DK
477(with-test-prefix/compile "Number Built-Ins"
478
479 (pass-if "floatp"
480 (and (floatp 1.0) (not (floatp 1)) (not (floatp 'a))))
481 (pass-if "integerp"
482 (and (integerp 42) (integerp -2) (not (integerp 1.0))))
483 (pass-if "numberp"
484 (and (numberp 1.0) (numberp -2) (not (numberp 'a))))
485 (pass-if "wholenump"
486 (and (wholenump 0) (not (wholenump -2)) (not (wholenump 1.0))))
487 (pass-if "zerop"
488 (and (zerop 0) (zerop 0.0) (not (zerop 1))))
489
490 (pass-if "comparisons"
491 (and (= 1 1.0) (/= 0 1)
492 (< 1 2) (> 2 1) (>= 1 1) (<= 1 1)
493 (not (< 1 1)) (not (<= 2 1))))
494
495 (pass-if "max and min"
496 (and (= (max -5 2 4.0 1) 4.0) (= (min -5 2 4.0 1) -5)
497 (= (max 1) 1) (= (min 1) 1)))
498 (pass-if "abs"
499 (and (= (abs 1.0) 1.0) (= (abs -5) 5)))
500
501 (pass-if "float"
502 (and (= (float 1) 1) (= (float 5.5) 5.5)
503 (floatp (float 1))))
504
505 (pass-if-equal "basic arithmetic operators" -8.5
506 (+ (1+ 0) (1- 0) (- 5.5) (* 2 -2) (- 2 1)))
507 (pass-if "modulo"
508 (= (% 5 3) 2))
509
510 (pass-if "floating point rounding"
511 (and (= (ffloor 1.7) 1.0) (= (ffloor -1.2) -2.0) (= (ffloor 1.0) 1.0)
512 (= (fceiling 1.2) 2.0) (= (fceiling -1.7) -1.0) (= (fceiling 1.0) 1.0)
513 (= (ftruncate 1.6) 1.0) (= (ftruncate -1.7) -1.0)
514 (= (fround 1.2) 1.0) (= (fround 1.7) 2.0) (= (fround -1.7) -2.0))))
f614ca12
DK
515
516(with-test-prefix/compile "List Built-Ins"
517
518 (pass-if "consp and atomp"
519 (and (consp '(1 2 3)) (consp '(1 2 . 3)) (consp '(a . b))
520 (not (consp '())) (not (consp 1)) (not (consp "abc"))
521 (atomp 'a) (atomp '()) (atomp -1.5) (atomp "abc")
522 (not (atomp '(1 . 2))) (not (atomp '(1)))))
523 (pass-if "listp and nlistp"
524 (and (listp '(1 2 3)) (listp '(1)) (listp '()) (listp '(1 . 2))
525 (not (listp 'a)) (not (listp 42)) (nlistp 42)
526 (not (nlistp '())) (not (nlistp '(1 2 3))) (not (nlistp '(1 . 2)))))
527 (pass-if "null"
528 (and (null '()) (not (null 1)) (not (null '(1 2))) (not (null '(1 . 2)))))
529
530 (pass-if "car and cdr"
531 (and (equal (car '(1 2 3)) 1) (equal (cdr '(1 2 3)) '(2 3))
532 (equal (car '()) nil) (equal (cdr '()) nil)
533 (equal (car '(1 . 2)) 1) (equal (cdr '(1 . 2)) 2)
534 (null (cdr '(1)))))
535 (pass-if "car-safe and cdr-safe"
536 (and (equal (car-safe '(1 2)) 1) (equal (cdr-safe '(1 2)) '(2))
537 (equal (car-safe 5) nil) (equal (cdr-safe 5) nil)))
538
539 (pass-if "pop"
540 (progn (setq mylist '(a b c))
541 (setq value (pop mylist))
542 (and (equal value 'a)
543 (equal mylist '(b c)))))
544 (pass-if-equal "push" '(a b c)
545 (progn (setq mylist '(b c))
546 (push 'a mylist)))
547
548 (pass-if "nth and nthcdr"
549 (and (equal (nth -5 '(1 2 3)) 1) (equal (nth 3 '(1 2 3)) nil)
550 (equal (nth 0 '(1 2 3)) 1) (equal (nth 2 '(1 2 3)) 3)
551 (equal (nthcdr -5 '(1 2 3)) '(1 2 3))
552 (equal (nthcdr 4 '(1 2 3)) nil)
553 (equal (nthcdr 1 '(1 2 3)) '(2 3))
554 (equal (nthcdr 2 '(1 2 3)) '(3))))
555
c2c7c277
DK
556 (pass-if "length"
557 (and (= (length '()) 0)
558 (= (length '(1 2 3 4 5)) 5)
559 (= (length '(1 2 (3 4 (5)) 6)) 4)))
560
f614ca12
DK
561 (pass-if "cons, list and make-list"
562 (and (equal (cons 1 2) '(1 . 2)) (equal (cons 1 '(2 3)) '(1 2 3))
563 (equal (cons 1 '()) '(1))
564 (equal (list 'a) '(a)) (equal (list) '()) (equal (list 1 2) '(1 2))
565 (equal (make-list 3 42) '(42 42 42))
566 (equal (make-list 0 1) '())))
567 (pass-if "append"
568 (and (equal (append '(1 2) '(3 4) '(5)) '(1 2 3 4 5))
569 (equal (append '(1 2) 3) '(1 2 . 3))))
570 (pass-if "reverse"
571 (and (equal (reverse '(5 4 3 2 1)) '(1 2 3 4 5))
572 (equal (reverse '()) '())))
573 (pass-if "copy-tree"
574 (progn (setq mylist '(1 2 (3 4)))
575 (and (not (eq mylist (copy-tree mylist)))
576 (equal mylist (copy-tree mylist)))))
577
578 (pass-if "number-sequence"
579 (and (equal (number-sequence 5) '(5))
580 (equal (number-sequence 5 9) '(5 6 7 8 9))
581 (equal (number-sequence 5 9 3) '(5 8))
582 (equal (number-sequence 5 1 -2) '(5 3 1))
583 (equal (number-sequence 5 8 -1) '())
584 (equal (number-sequence 5 1) '())
585 (equal (number-sequence 5 5 0) '(5))))
586
587 (pass-if "setcar and setcdr"
588 (progn (setq pair '(1 . 2))
589 (setq copy pair)
590 (setq a (setcar copy 3))
591 (setq b (setcdr copy 4))
592 (and (= a 3) (= b 4)
593 (equal pair '(3 . 4))))))