1 ;;;; elisp-compiler.test --- Test the compiler for Elisp.
3 ;;;; Copyright (C) 2009 Free Software Foundation, Inc.
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.
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.
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
20 (define-module (test-elisp-compiler)
21 :use-module (test-suite lib)
22 :use-module (system base compile)
23 :use-module (language elisp runtime))
26 ; Macros to handle the compilation conveniently.
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)))
32 ((_ (pass-if test-name exp #:opts opts))
33 (pass-if test-name (compile 'exp #:from 'elisp #:to 'value #:opts opts)))
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)))))
41 (define-syntax with-test-prefix/compile
43 ((_ section-name exp ...)
44 (with-test-prefix section-name (compile-test exp) ...))))
47 ; Test control structures.
48 ; ========================
50 (with-test-prefix/compile "Sequencing"
52 (pass-if-equal "progn" 1
59 (setq b (prog1 a (setq a (1+ a))))
60 (and (= a 1) (= b 0))))
64 (setq b (prog2 (setq a (1+ a))
67 (and (= a 3) (= b 2)))))
69 (with-test-prefix/compile "Conditionals"
71 (pass-if-equal "succeeding if" 1
76 (setq a 2) (setq a (1+ a)) a)
78 (equal (if nil 1) nil)))
80 (pass-if-equal "failing when" nil-value
82 (pass-if-equal "succeeding when" 42
84 (when t (setq a 42) a)))
86 (pass-if-equal "failing unless" nil-value
88 (pass-if-equal "succeeding unless" 42
90 (unless nil (setq a 42) a)))
92 (pass-if-equal "empty cond" nil-value
94 (pass-if-equal "all failing cond" nil-value
96 (pass-if-equal "only condition" 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
102 (cond (nil) (t (setq a 42) 1) (t (setq a 0)))
105 (with-test-prefix/compile "Combining Conditions"
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))
111 (pass-if-equal "empty or" nil-value (or))
112 (pass-if-equal "failing or" nil-value (or nil nil nil))
113 (pass-if-equal "succeeding or" 1 (or nil 1 nil 2 nil 3))
115 (pass-if-equal "not true" nil-value (not 1))
116 (pass-if-equal "not false" t-value (not nil)))
118 (with-test-prefix/compile "Iteration"
120 (pass-if-equal "failing while" 0
122 (while nil (setq a 1))
124 (pass-if-equal "running while" 120
128 (setq prod (* i prod))
135 (setq b (dotimes (i count)
138 (setq c (dotimes (i 10 42) nil))
139 (and (= a 5050) (equal b nil) (= c 42))))
142 (let ((mylist '(7 2 5)))
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))
149 (equal mylist '(7 2 5))
152 (with-test-prefix/compile "Exceptions"
154 (pass-if "catch without exception"
161 (= (catch (+ 1 2) a) 2)))
163 ; FIXME: Figure out how to do this...
164 ;(pass-if-exception "uncaught exception" 'elisp-exception
167 (pass-if "catch and throw"
168 (and (setq mylist '(1 2))
169 (= (catch 'abc (throw 'abc 2) 1) 2)
170 (= (catch 'abc (catch 'def (throw 'abc (1+ 0)) 2) 3) 1)
171 (= (catch 'abc (catch 'def (throw 'def 1) 2) 3) 3)
172 (= (catch mylist (catch '(1 2) (throw mylist 1) 2) 3) 1)))
174 (pass-if "unwind-protect"
175 (progn (setq a 0 b 1 c 1)
177 (unwind-protect (progn (setq a 1)
181 (unwind-protect nil (setq c 0))
182 (and (= a 0) (= b 0) (= c 0)
183 (= (unwind-protect 42 1 2 3) 42)))))
186 ; Test handling of variables.
187 ; ===========================
189 (with-test-prefix/compile "Variable Setting/Referencing"
191 ; TODO: Check for variable-void error
193 (pass-if-equal "setq and reference" 6
194 (progn (setq a 1 b 2 c 3)
196 (pass-if-equal "setq value" 2
197 (progn (setq a 1 b 2)))
199 (pass-if "set and symbol-value"
200 (progn (setq myvar 'a)
201 (and (= (set myvar 42) 42)
203 (= (symbol-value myvar) 42))))
204 (pass-if "void variables"
205 (progn (setq a 1 b 2)
206 (and (eq (makunbound 'b) 'b)
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))))
217 (with-test-prefix/compile "Let and Let*"
219 (pass-if-equal "let without value" nil-value
221 (pass-if-equal "basic let" 0
226 (pass-if-equal "let*" 1
232 (pass-if "local scope"
240 (with-test-prefix/compile "defconst and defvar"
242 (pass-if-equal "defconst without docstring" 3.141
246 (pass-if-equal "defconst value" 'pi
247 (defconst pi 3.141 "Pi"))
249 (pass-if-equal "defvar without value" 42
253 (pass-if-equal "defvar on already defined variable" 42
255 (defvar a 1 "Some docstring is also ok")
257 (pass-if-equal "defvar on undefined variable" 1
258 (progn (makunbound 'a)
261 (pass-if-equal "defvar value" 'a
265 ; Functions and lambda expressions.
266 ; =================================
268 (with-test-prefix/compile "Lambda Expressions"
270 (pass-if-equal "required arguments" 3
271 ((lambda (a b c) c) 1 2 3))
273 (pass-if-equal "optional argument" 3
274 ((function (lambda (a &optional b c) c)) 1 2 3))
275 (pass-if-equal "optional missing" nil-value
276 ((lambda (&optional a) a)))
278 (pass-if-equal "rest argument" '(3 4 5)
279 ((lambda (a b &rest c) c) 1 2 3 4 5))
280 (pass-if-equal "rest missing" nil-value
281 ((lambda (a b &rest c) c) 1 2)))
283 (with-test-prefix/compile "Function Definitions"
285 (pass-if-equal "defun" 3
286 (progn (defun test (a b) (+ a b))
288 (pass-if-equal "defun value" 'test
289 (defun test (a b) (+ a b)))
291 (pass-if "fset and symbol-function"
292 (progn (setq myfunc 'x x 5)
293 (and (= (fset myfunc 42) 42)
294 (= (symbol-function myfunc) 42)
296 (pass-if "void function values"
298 (defun test (a b) (+ a b))
301 (and (fboundp 'b) (fboundp 'test)
305 (pass-if "flet and flet*"
306 (progn (defun foobar () 42)
307 (defun test () (foobar))
309 (flet ((foobar (lambda () 0))
310 (myfoo (symbol-function 'foobar)))
313 (flet* ((foobar (lambda () 0))
314 (myfoo (symbol-function 'foobar)))
321 (with-test-prefix/compile "Calling Functions"
323 (pass-if-equal "recursion" 120
324 (progn (defun factorial (n prod)
327 (factorial (1- n) (* prod n))))
330 (pass-if "dynamic scoping"
341 ; Quoting and Backquotation.
342 ; ==========================
344 (with-test-prefix/compile "Quotation"
347 (and (equal '42 42) (equal '"abc" "abc")
348 (equal '(1 2 (3 (4) x)) '(1 2 (3 (4) x)))
349 (not (equal '(1 2 (3 4 (x))) '(1 2 3 4 x)))
350 (equal '(1 2 . 3) '(1 2 . 3))))
352 (pass-if "simple backquote"
353 (and (equal (\` 42) 42)
354 (equal (\` (1 (a))) '(1 (a)))
355 (equal (\` (1 . 2)) '(1 . 2))))
357 (progn (setq a 42 l '(18 12))
358 (and (equal (\` (\, a)) 42)
359 (equal (\` (1 a ((\, l)) . (\, a))) '(1 a ((18 12)) . 42)))))
360 (pass-if "unquote splicing"
361 (progn (setq l '(18 12) empty '())
362 (and (equal (\` (\,@ l)) '(18 12))
363 (equal (\` (l 2 (3 (\,@ l)) ((\,@ l)) (\,@ l)))
364 '(l 2 (3 18 12) (18 12) 18 12))
365 (equal (\` (1 2 (\,@ empty) 3)) '(1 2 3))))))
372 (with-test-prefix/compile "Macros"
374 (pass-if-equal "defmacro value" 'magic-number
375 (defmacro magic-number () 42))
377 (pass-if-equal "macro expansion" 1
378 (progn (defmacro take-first (a b) a)
379 (take-first 1 (/ 1 0)))))
382 ; Test the built-ins.
383 ; ===================
385 (with-test-prefix/compile "Equivalence Predicates"
388 (and (equal 2 2) (not (equal 1 2))
389 (equal "abc" "abc") (not (equal "abc" "ABC"))
390 (equal 'abc 'abc) (not (equal 'abc 'def))
391 (equal '(1 2 (3 4) 5) '(1 2 (3 4) 5))
392 (not (equal '(1 2 3 4 5) '(1 2 (3 4) 5)))))
395 (progn (setq some-list '(1 2))
396 (setq some-string "abc")
397 (and (eq 2 2) (not (eq 1 2))
398 (eq 'abc 'abc) (not (eq 'abc 'def))
399 (eq some-string some-string) (not (eq some-string "abc"))
400 (eq some-list some-list) (not (eq some-list '(1 2)))))))
402 (with-test-prefix/compile "Number Built-Ins"
405 (and (floatp 1.0) (not (floatp 1)) (not (floatp 'a))))
407 (and (integerp 42) (integerp -2) (not (integerp 1.0))))
409 (and (numberp 1.0) (numberp -2) (not (numberp 'a))))
411 (and (wholenump 0) (not (wholenump -2)) (not (wholenump 1.0))))
413 (and (zerop 0) (zerop 0.0) (not (zerop 1))))
415 (pass-if "comparisons"
416 (and (= 1 1.0) (/= 0 1)
417 (< 1 2) (> 2 1) (>= 1 1) (<= 1 1)
418 (not (< 1 1)) (not (<= 2 1))))
420 (pass-if "max and min"
421 (and (= (max -5 2 4.0 1) 4.0) (= (min -5 2 4.0 1) -5)
422 (= (max 1) 1) (= (min 1) 1)))
424 (and (= (abs 1.0) 1.0) (= (abs -5) 5)))
427 (and (= (float 1) 1) (= (float 5.5) 5.5)
430 (pass-if-equal "basic arithmetic operators" -8.5
431 (+ (1+ 0) (1- 0) (- 5.5) (* 2 -2) (- 2 1)))
435 (pass-if "floating point rounding"
436 (and (= (ffloor 1.7) 1.0) (= (ffloor -1.2) -2.0) (= (ffloor 1.0) 1.0)
437 (= (fceiling 1.2) 2.0) (= (fceiling -1.7) -1.0) (= (fceiling 1.0) 1.0)
438 (= (ftruncate 1.6) 1.0) (= (ftruncate -1.7) -1.0)
439 (= (fround 1.2) 1.0) (= (fround 1.7) 2.0) (= (fround -1.7) -2.0))))
441 (with-test-prefix/compile "List Built-Ins"
443 (pass-if "consp and atomp"
444 (and (consp '(1 2 3)) (consp '(1 2 . 3)) (consp '(a . b))
445 (not (consp '())) (not (consp 1)) (not (consp "abc"))
446 (atomp 'a) (atomp '()) (atomp -1.5) (atomp "abc")
447 (not (atomp '(1 . 2))) (not (atomp '(1)))))
448 (pass-if "listp and nlistp"
449 (and (listp '(1 2 3)) (listp '(1)) (listp '()) (listp '(1 . 2))
450 (not (listp 'a)) (not (listp 42)) (nlistp 42)
451 (not (nlistp '())) (not (nlistp '(1 2 3))) (not (nlistp '(1 . 2)))))
453 (and (null '()) (not (null 1)) (not (null '(1 2))) (not (null '(1 . 2)))))
455 (pass-if "car and cdr"
456 (and (equal (car '(1 2 3)) 1) (equal (cdr '(1 2 3)) '(2 3))
457 (equal (car '()) nil) (equal (cdr '()) nil)
458 (equal (car '(1 . 2)) 1) (equal (cdr '(1 . 2)) 2)
460 (pass-if "car-safe and cdr-safe"
461 (and (equal (car-safe '(1 2)) 1) (equal (cdr-safe '(1 2)) '(2))
462 (equal (car-safe 5) nil) (equal (cdr-safe 5) nil)))
465 (progn (setq mylist '(a b c))
466 (setq value (pop mylist))
467 (and (equal value 'a)
468 (equal mylist '(b c)))))
469 (pass-if-equal "push" '(a b c)
470 (progn (setq mylist '(b c))
473 (pass-if "nth and nthcdr"
474 (and (equal (nth -5 '(1 2 3)) 1) (equal (nth 3 '(1 2 3)) nil)
475 (equal (nth 0 '(1 2 3)) 1) (equal (nth 2 '(1 2 3)) 3)
476 (equal (nthcdr -5 '(1 2 3)) '(1 2 3))
477 (equal (nthcdr 4 '(1 2 3)) nil)
478 (equal (nthcdr 1 '(1 2 3)) '(2 3))
479 (equal (nthcdr 2 '(1 2 3)) '(3))))
481 (pass-if "cons, list and make-list"
482 (and (equal (cons 1 2) '(1 . 2)) (equal (cons 1 '(2 3)) '(1 2 3))
483 (equal (cons 1 '()) '(1))
484 (equal (list 'a) '(a)) (equal (list) '()) (equal (list 1 2) '(1 2))
485 (equal (make-list 3 42) '(42 42 42))
486 (equal (make-list 0 1) '())))
488 (and (equal (append '(1 2) '(3 4) '(5)) '(1 2 3 4 5))
489 (equal (append '(1 2) 3) '(1 2 . 3))))
491 (and (equal (reverse '(5 4 3 2 1)) '(1 2 3 4 5))
492 (equal (reverse '()) '())))
494 (progn (setq mylist '(1 2 (3 4)))
495 (and (not (eq mylist (copy-tree mylist)))
496 (equal mylist (copy-tree mylist)))))
498 (pass-if "number-sequence"
499 (and (equal (number-sequence 5) '(5))
500 (equal (number-sequence 5 9) '(5 6 7 8 9))
501 (equal (number-sequence 5 9 3) '(5 8))
502 (equal (number-sequence 5 1 -2) '(5 3 1))
503 (equal (number-sequence 5 8 -1) '())
504 (equal (number-sequence 5 1) '())
505 (equal (number-sequence 5 5 0) '(5))))
507 (pass-if "setcar and setcdr"
508 (progn (setq pair '(1 . 2))
510 (setq a (setcar copy 3))
511 (setq b (setcdr copy 4))
513 (equal pair '(3 . 4))))))