Commit | Line | Data |
---|---|---|
92a61010 | 1 | ;;;; elisp-compiler.test --- Test the compiler for Elisp. -*- scheme -*- |
d158fa62 | 2 | ;;;; |
92a61010 | 3 | ;;;; Copyright (C) 2009, 2010 Free Software Foundation, Inc. |
d158fa62 DK |
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 | |
450cb504 | 29 | (syntax-rules (pass-if pass-if-equal pass-if-exception) |
d158fa62 DK |
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 | ||
f6e0a4a6 BT |
50 | (compile '(%set-lexical-binding-mode #nil) #:from 'elisp #:to 'value) |
51 | ||
d158fa62 DK |
52 | (with-test-prefix/compile "Sequencing" |
53 | ||
54 | (pass-if-equal "progn" 1 | |
55 | (progn (setq a 0) | |
56 | (setq a (1+ a)) | |
fb66a47a DK |
57 | a)) |
58 | ||
d5ac6923 BT |
59 | (pass-if-equal "empty progn" #nil |
60 | (progn)) | |
61 | ||
fb66a47a DK |
62 | (pass-if "prog1" |
63 | (progn (setq a 0) | |
64 | (setq b (prog1 a (setq a (1+ a)))) | |
65 | (and (= a 1) (= b 0)))) | |
66 | ||
67 | (pass-if "prog2" | |
68 | (progn (setq a 0) | |
69 | (setq b (prog2 (setq a (1+ a)) | |
70 | (setq a (1+ a)) | |
71 | (setq a (1+ a)))) | |
72 | (and (= a 3) (= b 2))))) | |
d158fa62 DK |
73 | |
74 | (with-test-prefix/compile "Conditionals" | |
75 | ||
76 | (pass-if-equal "succeeding if" 1 | |
77 | (if t 1 2)) | |
7d1a9782 DK |
78 | (pass-if "failing if" |
79 | (and (= (if nil | |
80 | 1 | |
81 | (setq a 2) (setq a (1+ a)) a) | |
82 | 3) | |
83 | (equal (if nil 1) nil))) | |
84 | ||
d5ac6923 BT |
85 | (pass-if-equal "if with no else" #nil |
86 | (if nil t)) | |
87 | ||
d158fa62 DK |
88 | (pass-if-equal "empty cond" nil-value |
89 | (cond)) | |
90 | (pass-if-equal "all failing cond" nil-value | |
91 | (cond (nil) (nil))) | |
92 | (pass-if-equal "only condition" 5 | |
93 | (cond (nil) (5))) | |
94 | (pass-if-equal "succeeding cond value" 42 | |
95 | (cond (nil) (t 42) (t 0))) | |
96 | (pass-if-equal "succeeding cond side-effect" 42 | |
97 | (progn (setq a 0) | |
98 | (cond (nil) (t (setq a 42) 1) (t (setq a 0))) | |
99 | a))) | |
100 | ||
101 | (with-test-prefix/compile "Combining Conditions" | |
102 | ||
103 | (pass-if-equal "empty and" t-value (and)) | |
104 | (pass-if-equal "failing and" nil-value (and 1 2 nil 3)) | |
105 | (pass-if-equal "succeeding and" 3 (and 1 2 3)) | |
106 | ||
107 | (pass-if-equal "empty or" nil-value (or)) | |
108 | (pass-if-equal "failing or" nil-value (or nil nil nil)) | |
b6b9d596 DK |
109 | (pass-if-equal "succeeding or" 1 (or nil 1 nil 2 nil 3)) |
110 | ||
111 | (pass-if-equal "not true" nil-value (not 1)) | |
112 | (pass-if-equal "not false" t-value (not nil))) | |
d158fa62 DK |
113 | |
114 | (with-test-prefix/compile "Iteration" | |
115 | ||
116 | (pass-if-equal "failing while" 0 | |
117 | (progn (setq a 0) | |
118 | (while nil (setq a 1)) | |
119 | a)) | |
120 | (pass-if-equal "running while" 120 | |
121 | (progn (setq prod 1 | |
122 | i 1) | |
123 | (while (<= i 5) | |
124 | (setq prod (* i prod)) | |
125 | (setq i (1+ i))) | |
a338fa3d | 126 | prod))) |
d158fa62 | 127 | |
35b2e41d DK |
128 | (with-test-prefix/compile "Exceptions" |
129 | ||
130 | (pass-if "catch without exception" | |
131 | (and (setq a 0) | |
132 | (= (catch 'foobar | |
133 | (setq a (1+ a)) | |
134 | (setq a (1+ a)) | |
135 | a) | |
136 | 2) | |
137 | (= (catch (+ 1 2) a) 2))) | |
138 | ||
139 | ; FIXME: Figure out how to do this... | |
140 | ;(pass-if-exception "uncaught exception" 'elisp-exception | |
141 | ; (throw 'abc 1)) | |
142 | ||
143 | (pass-if "catch and throw" | |
144 | (and (setq mylist '(1 2)) | |
145 | (= (catch 'abc (throw 'abc 2) 1) 2) | |
33da12ee | 146 | (= (catch 'abc (catch 'def (throw 'abc (1+ 0)) 2) 3) 1) |
35b2e41d | 147 | (= (catch 'abc (catch 'def (throw 'def 1) 2) 3) 3) |
59e46065 | 148 | (= (catch mylist (catch (list 1 2) (throw mylist 1) 2) 3) 1))) |
33da12ee DK |
149 | |
150 | (pass-if "unwind-protect" | |
151 | (progn (setq a 0 b 1 c 1) | |
152 | (catch 'exc | |
153 | (unwind-protect (progn (setq a 1) | |
154 | (throw 'exc 0)) | |
155 | (setq a 0) | |
156 | (setq b 0))) | |
157 | (unwind-protect nil (setq c 0)) | |
158 | (and (= a 0) (= b 0) (= c 0) | |
159 | (= (unwind-protect 42 1 2 3) 42))))) | |
35b2e41d | 160 | |
e96a9591 DK |
161 | (with-test-prefix/compile "Eval" |
162 | ||
163 | (pass-if-equal "basic eval" 3 | |
164 | (progn (setq code '(+ 1 2)) | |
165 | (eval code))) | |
166 | ||
167 | (pass-if "real dynamic code" | |
168 | (and (setq a 1 b 1 c 1) | |
169 | (defun set-code (var val) | |
170 | (list 'setq var val)) | |
171 | (= a 1) (= b 1) (= c 1) | |
172 | (eval (set-code 'a '(+ 2 3))) | |
173 | (eval (set-code 'c 42)) | |
174 | (= a 5) (= b 1) (= c 42))) | |
175 | ||
176 | ; Build code that recursively again and again calls eval. What we want is | |
177 | ; something like: | |
178 | ; (eval '(1+ (eval '(1+ (eval 1))))) | |
179 | (pass-if "recursive eval" | |
180 | (progn (setq depth 10 i depth) | |
181 | (setq code '(eval 0)) | |
182 | (while (not (zerop i)) | |
0dbfdeef | 183 | (setq code (#{`}# (eval (quote (1+ (#{,}# code)))))) |
e96a9591 DK |
184 | (setq i (1- i))) |
185 | (= (eval code) depth)))) | |
186 | ||
d158fa62 DK |
187 | |
188 | ; Test handling of variables. | |
189 | ; =========================== | |
190 | ||
191 | (with-test-prefix/compile "Variable Setting/Referencing" | |
192 | ||
193 | ; TODO: Check for variable-void error | |
194 | ||
195 | (pass-if-equal "setq and reference" 6 | |
570c12ac DK |
196 | (progn (setq a 1 b 2 c 3) |
197 | (+ a b c))) | |
e96a9591 DK |
198 | (pass-if-equal "setq evaluation order" 1 |
199 | (progn (setq a 0 b 0) | |
200 | (setq a 1 b a))) | |
570c12ac | 201 | (pass-if-equal "setq value" 2 |
37099846 DK |
202 | (progn (setq a 1 b 2))) |
203 | ||
204 | (pass-if "set and symbol-value" | |
205 | (progn (setq myvar 'a) | |
206 | (and (= (set myvar 42) 42) | |
207 | (= a 42) | |
208 | (= (symbol-value myvar) 42)))) | |
209 | (pass-if "void variables" | |
210 | (progn (setq a 1 b 2) | |
211 | (and (eq (makunbound 'b) 'b) | |
212 | (boundp 'a) | |
3f70b2dc | 213 | (not (boundp 'b)))))) |
d158fa62 DK |
214 | |
215 | (with-test-prefix/compile "Let and Let*" | |
216 | ||
217 | (pass-if-equal "let without value" nil-value | |
218 | (let (a (b 5)) a)) | |
219 | (pass-if-equal "basic let" 0 | |
220 | (progn (setq a 0) | |
221 | (let ((a 1) | |
222 | (b a)) | |
223 | b))) | |
fd40f371 | 224 | |
d5ac6923 BT |
225 | (pass-if-equal "empty let" #nil (let ())) |
226 | ||
fd40f371 | 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 | 237 | |
d5ac6923 BT |
238 | (pass-if-equal "empty let*" #nil |
239 | (let* ())) | |
240 | ||
d158fa62 DK |
241 | (pass-if "local scope" |
242 | (progn (setq a 0) | |
243 | (setq b (let (a) | |
244 | (setq a 1) | |
245 | a)) | |
246 | (and (= a 0) | |
247 | (= b 1))))) | |
248 | ||
a6a5cf03 DK |
249 | (with-test-prefix/compile "Lexical Scoping" |
250 | ||
251 | (pass-if "basic let semantics" | |
252 | (and (setq a 1) | |
253 | (lexical-let ((a 2) (b a)) | |
254 | (and (= a 2) (= b 1))) | |
255 | (lexical-let* ((a 2) (b a)) | |
256 | (and (= a 2) (= b 2) (setq a 42) (= a 42))) | |
257 | (= a 1))) | |
258 | ||
259 | (pass-if "lexical scope with lexical-let's" | |
260 | (and (setq a 1) | |
261 | (defun dyna () a) | |
262 | (lexical-let (a) | |
263 | (setq a 2) | |
264 | (and (= a 2) (= (dyna) 1))) | |
265 | (= a 1) | |
266 | (lexical-let* (a) | |
267 | (setq a 2) | |
268 | (and (= a 2) (= (dyna) 1))) | |
269 | (= a 1))) | |
270 | ||
271 | (pass-if "lexical scoping vs. symbol-value / set" | |
272 | (and (setq a 1) | |
273 | (lexical-let ((a 2)) | |
274 | (and (= a 2) | |
275 | (= (symbol-value 'a) 1) | |
276 | (set 'a 3) | |
277 | (= a 2) | |
278 | (= (symbol-value 'a) 3))) | |
279 | (= a 3))) | |
280 | ||
281 | (pass-if "let inside lexical-let" | |
282 | (and (setq a 1 b 1) | |
283 | (defun dynvals () (cons a b)) | |
284 | (lexical-let ((a 2)) | |
285 | (and (= a 2) (equal (dynvals) '(1 . 1)) | |
286 | (let ((a 3) (b a)) | |
f6e0a4a6 | 287 | (declare (lexical a)) |
a6a5cf03 DK |
288 | (and (= a 3) (= b 2) |
289 | (equal (dynvals) '(1 . 2)))) | |
290 | (let* ((a 4) (b a)) | |
f6e0a4a6 | 291 | (declare (lexical a)) |
a6a5cf03 DK |
292 | (and (= a 4) (= b 4) |
293 | (equal (dynvals) '(1 . 4)))) | |
294 | (= a 2))) | |
295 | (= a 1))) | |
296 | ||
297 | (pass-if "lambda args inside lexical-let" | |
298 | (and (setq a 1) | |
299 | (defun dyna () a) | |
300 | (lexical-let ((a 2) (b 42)) | |
301 | (and (= a 2) (= (dyna) 1) | |
f6e0a4a6 BT |
302 | ((lambda (a) |
303 | (declare (lexical a)) | |
304 | (and (= a 3) (= b 42) (= (dyna) 1))) 3) | |
dfbc6e9d | 305 | ((lambda () (let ((a 3)) |
f6e0a4a6 | 306 | (declare (lexical a)) |
dfbc6e9d | 307 | (and (= a 3) (= (dyna) 1))))) |
a6a5cf03 DK |
308 | (= a 2) (= (dyna) 1))) |
309 | (= a 1))) | |
310 | ||
311 | (pass-if "closures" | |
312 | (and (defun make-counter () | |
313 | (lexical-let ((cnt 0)) | |
314 | (lambda () | |
315 | (setq cnt (1+ cnt))))) | |
316 | (setq c1 (make-counter) c2 (make-counter)) | |
ce305387 DK |
317 | (= (funcall c1) 1) |
318 | (= (funcall c1) 2) | |
319 | (= (funcall c1) 3) | |
320 | (= (funcall c2) 1) | |
321 | (= (funcall c2) 2) | |
322 | (= (funcall c1) 4) | |
c808c926 DK |
323 | (= (funcall c2) 3))) |
324 | ||
dfbc6e9d DK |
325 | (pass-if "lexical lambda args" |
326 | (progn (setq a 1 b 1) | |
327 | (defun dyna () a) | |
328 | (defun dynb () b) | |
e5a361d1 | 329 | (lexical-let (a c) |
dfbc6e9d | 330 | ((lambda (a b &optional c) |
f6e0a4a6 | 331 | (declare (lexical a c)) |
dfbc6e9d DK |
332 | (and (= a 3) (= (dyna) 1) |
333 | (= b 2) (= (dynb) 2) | |
334 | (= c 1))) | |
335 | 3 2 1)))) | |
336 | ||
337 | ; Check if a lambda without dynamically bound arguments | |
338 | ; is tail-optimized by doing a deep recursion that would otherwise overflow | |
339 | ; the stack. | |
340 | (pass-if "lexical lambda tail-recursion" | |
e5a361d1 | 341 | (lexical-let (i) |
dfbc6e9d DK |
342 | (setq to 1000000) |
343 | (defun iteration-1 (i) | |
f6e0a4a6 | 344 | (declare (lexical i)) |
dfbc6e9d DK |
345 | (if (< i to) |
346 | (iteration-1 (1+ i)))) | |
347 | (iteration-1 0) | |
348 | (setq x 0) | |
349 | (defun iteration-2 () | |
350 | (if (< x to) | |
351 | (setq x (1+ x)) | |
352 | (iteration-2))) | |
353 | (iteration-2) | |
354 | t))) | |
355 | ||
a6a5cf03 | 356 | |
d158fa62 DK |
357 | (with-test-prefix/compile "defconst and defvar" |
358 | ||
359 | (pass-if-equal "defconst without docstring" 3.141 | |
360 | (progn (setq pi 3) | |
361 | (defconst pi 3.141) | |
362 | pi)) | |
363 | (pass-if-equal "defconst value" 'pi | |
364 | (defconst pi 3.141 "Pi")) | |
365 | ||
366 | (pass-if-equal "defvar without value" 42 | |
367 | (progn (setq a 42) | |
368 | (defvar a) | |
369 | a)) | |
370 | (pass-if-equal "defvar on already defined variable" 42 | |
371 | (progn (setq a 42) | |
372 | (defvar a 1 "Some docstring is also ok") | |
373 | a)) | |
d158fa62 | 374 | (pass-if-equal "defvar on undefined variable" 1 |
37099846 DK |
375 | (progn (makunbound 'a) |
376 | (defvar a 1) | |
d158fa62 DK |
377 | a)) |
378 | (pass-if-equal "defvar value" 'a | |
379 | (defvar a))) | |
380 | ||
381 | ||
382 | ; Functions and lambda expressions. | |
383 | ; ================================= | |
384 | ||
385 | (with-test-prefix/compile "Lambda Expressions" | |
386 | ||
387 | (pass-if-equal "required arguments" 3 | |
388 | ((lambda (a b c) c) 1 2 3)) | |
389 | ||
390 | (pass-if-equal "optional argument" 3 | |
48489836 | 391 | ((lambda (a &optional b c) c) 1 2 3)) |
d158fa62 DK |
392 | (pass-if-equal "optional missing" nil-value |
393 | ((lambda (&optional a) a))) | |
394 | ||
395 | (pass-if-equal "rest argument" '(3 4 5) | |
396 | ((lambda (a b &rest c) c) 1 2 3 4 5)) | |
16318179 BT |
397 | (pass-if "rest missing" |
398 | (null ((lambda (a b &rest c) c) 1 2))) | |
d5ac6923 BT |
399 | |
400 | (pass-if-equal "empty lambda" #nil | |
401 | ((lambda ())))) | |
d158fa62 DK |
402 | |
403 | (with-test-prefix/compile "Function Definitions" | |
404 | ||
405 | (pass-if-equal "defun" 3 | |
406 | (progn (defun test (a b) (+ a b)) | |
407 | (test 1 2))) | |
408 | (pass-if-equal "defun value" 'test | |
37099846 DK |
409 | (defun test (a b) (+ a b))) |
410 | ||
411 | (pass-if "fset and symbol-function" | |
412 | (progn (setq myfunc 'x x 5) | |
413 | (and (= (fset myfunc 42) 42) | |
414 | (= (symbol-function myfunc) 42) | |
415 | (= x 5)))) | |
416 | (pass-if "void function values" | |
417 | (progn (setq a 1) | |
418 | (defun test (a b) (+ a b)) | |
419 | (fmakunbound 'a) | |
420 | (fset 'b 5) | |
421 | (and (fboundp 'b) (fboundp 'test) | |
422 | (not (fboundp 'a)) | |
e8f18b3f DK |
423 | (= a 1)))) |
424 | ||
0a32abc4 | 425 | (pass-if "flet" |
e8f18b3f DK |
426 | (progn (defun foobar () 42) |
427 | (defun test () (foobar)) | |
428 | (and (= (test) 42) | |
0e5b7e74 BT |
429 | (flet ((foobar () 0) |
430 | (myfoo () | |
431 | (funcall (symbol-function 'foobar)))) | |
e8f18b3f | 432 | (and (= (myfoo) 42) |
c6920dc8 | 433 | (= (test) 42))) |
0e5b7e74 | 434 | (flet ((foobar () nil)) |
e8f18b3f | 435 | (defun foobar () 0) |
c6920dc8 | 436 | (= (test) 42)) |
e8f18b3f | 437 | (= (test) 42))))) |
d158fa62 DK |
438 | |
439 | (with-test-prefix/compile "Calling Functions" | |
440 | ||
441 | (pass-if-equal "recursion" 120 | |
442 | (progn (defun factorial (n prod) | |
443 | (if (zerop n) | |
444 | prod | |
445 | (factorial (1- n) (* prod n)))) | |
446 | (factorial 5 1))) | |
447 | ||
448 | (pass-if "dynamic scoping" | |
449 | (progn (setq a 0) | |
450 | (defun foo () | |
451 | (setq a (1+ a)) | |
452 | a) | |
453 | (defun bar (a) | |
454 | (foo)) | |
455 | (and (= 43 (bar 42)) | |
e96a9591 DK |
456 | (zerop a)))) |
457 | ||
458 | (pass-if "funcall and apply argument handling" | |
459 | (and (defun allid (&rest args) args) | |
460 | (setq allid-var (symbol-function 'allid)) | |
461 | (equal (funcall allid-var 1 2 3) '(1 2 3)) | |
462 | (equal (funcall allid-var) nil) | |
463 | (equal (funcall allid-var 1 2 '(3 4)) '(1 2 (3 4))) | |
464 | (equal (funcall allid-var '()) '(())) | |
465 | (equal (apply allid-var 1 2 '(3 4)) '(1 2 3 4)) | |
466 | (equal (apply allid-var '(1 2)) '(1 2)) | |
467 | (equal (apply allid-var '()) nil))) | |
468 | ||
469 | (pass-if "raw functions with funcall" | |
470 | (and (= (funcall '+ 1 2) 3) | |
471 | (= (funcall (lambda (a b) (+ a b)) 1 2) 3) | |
472 | (= (funcall '(lambda (a b) (+ a b)) 1 2) 3)))) | |
b6b9d596 DK |
473 | |
474 | ||
9b5ff6a6 DK |
475 | ; Quoting and Backquotation. |
476 | ; ========================== | |
477 | ||
478 | (with-test-prefix/compile "Quotation" | |
479 | ||
480 | (pass-if "quote" | |
481 | (and (equal '42 42) (equal '"abc" "abc") | |
482 | (equal '(1 2 (3 (4) x)) '(1 2 (3 (4) x))) | |
483 | (not (equal '(1 2 (3 4 (x))) '(1 2 3 4 x))) | |
484 | (equal '(1 2 . 3) '(1 2 . 3)))) | |
485 | ||
486 | (pass-if "simple backquote" | |
0dbfdeef BT |
487 | (and (equal (#{`}# 42) 42) |
488 | (equal (#{`}# (1 (a))) '(1 (a))) | |
489 | (equal (#{`}# (1 . 2)) '(1 . 2)))) | |
9b5ff6a6 DK |
490 | (pass-if "unquote" |
491 | (progn (setq a 42 l '(18 12)) | |
0dbfdeef BT |
492 | (and (equal (#{`}# (#{,}# a)) 42) |
493 | (equal (#{`}# (1 a ((#{,}# l)) . (#{,}# a))) '(1 a ((18 12)) . 42))))) | |
9b5ff6a6 DK |
494 | (pass-if "unquote splicing" |
495 | (progn (setq l '(18 12) empty '()) | |
0dbfdeef BT |
496 | (and (equal (#{`}# (#{,@}# l)) '(18 12)) |
497 | (equal (#{`}# (l 2 (3 (#{,@}# l)) ((#{,@}# l)) (#{,@}# l))) | |
9b5ff6a6 | 498 | '(l 2 (3 18 12) (18 12) 18 12)) |
0dbfdeef | 499 | (equal (#{`}# (1 2 (#{,@}# empty) 3)) '(1 2 3)))))) |
9b5ff6a6 DK |
500 | |
501 | ||
502 | ||
74c009da DK |
503 | ; Macros. |
504 | ; ======= | |
505 | ||
506 | (with-test-prefix/compile "Macros" | |
507 | ||
508 | (pass-if-equal "defmacro value" 'magic-number | |
509 | (defmacro magic-number () 42)) | |
510 | ||
511 | (pass-if-equal "macro expansion" 1 | |
512 | (progn (defmacro take-first (a b) a) | |
513 | (take-first 1 (/ 1 0))))) | |
514 | ||
515 | ||
b6b9d596 DK |
516 | ; Test the built-ins. |
517 | ; =================== | |
518 | ||
e905e490 DK |
519 | (with-test-prefix/compile "Equivalence Predicates" |
520 | ||
521 | (pass-if "equal" | |
522 | (and (equal 2 2) (not (equal 1 2)) | |
523 | (equal "abc" "abc") (not (equal "abc" "ABC")) | |
524 | (equal 'abc 'abc) (not (equal 'abc 'def)) | |
525 | (equal '(1 2 (3 4) 5) '(1 2 (3 4) 5)) | |
526 | (not (equal '(1 2 3 4 5) '(1 2 (3 4) 5))))) | |
527 | ||
528 | (pass-if "eq" | |
529 | (progn (setq some-list '(1 2)) | |
530 | (setq some-string "abc") | |
531 | (and (eq 2 2) (not (eq 1 2)) | |
532 | (eq 'abc 'abc) (not (eq 'abc 'def)) | |
59e46065 BT |
533 | (eq some-string some-string) (not (eq some-string (string 97 98 99))) |
534 | (eq some-list some-list) (not (eq some-list (list 1 2))))))) | |
e905e490 | 535 | |
b6b9d596 DK |
536 | (with-test-prefix/compile "Number Built-Ins" |
537 | ||
538 | (pass-if "floatp" | |
539 | (and (floatp 1.0) (not (floatp 1)) (not (floatp 'a)))) | |
540 | (pass-if "integerp" | |
541 | (and (integerp 42) (integerp -2) (not (integerp 1.0)))) | |
542 | (pass-if "numberp" | |
543 | (and (numberp 1.0) (numberp -2) (not (numberp 'a)))) | |
544 | (pass-if "wholenump" | |
545 | (and (wholenump 0) (not (wholenump -2)) (not (wholenump 1.0)))) | |
546 | (pass-if "zerop" | |
547 | (and (zerop 0) (zerop 0.0) (not (zerop 1)))) | |
548 | ||
549 | (pass-if "comparisons" | |
550 | (and (= 1 1.0) (/= 0 1) | |
551 | (< 1 2) (> 2 1) (>= 1 1) (<= 1 1) | |
552 | (not (< 1 1)) (not (<= 2 1)))) | |
553 | ||
554 | (pass-if "max and min" | |
555 | (and (= (max -5 2 4.0 1) 4.0) (= (min -5 2 4.0 1) -5) | |
556 | (= (max 1) 1) (= (min 1) 1))) | |
557 | (pass-if "abs" | |
558 | (and (= (abs 1.0) 1.0) (= (abs -5) 5))) | |
559 | ||
560 | (pass-if "float" | |
561 | (and (= (float 1) 1) (= (float 5.5) 5.5) | |
562 | (floatp (float 1)))) | |
563 | ||
564 | (pass-if-equal "basic arithmetic operators" -8.5 | |
565 | (+ (1+ 0) (1- 0) (- 5.5) (* 2 -2) (- 2 1))) | |
566 | (pass-if "modulo" | |
567 | (= (% 5 3) 2)) | |
568 | ||
569 | (pass-if "floating point rounding" | |
570 | (and (= (ffloor 1.7) 1.0) (= (ffloor -1.2) -2.0) (= (ffloor 1.0) 1.0) | |
571 | (= (fceiling 1.2) 2.0) (= (fceiling -1.7) -1.0) (= (fceiling 1.0) 1.0) | |
572 | (= (ftruncate 1.6) 1.0) (= (ftruncate -1.7) -1.0) | |
573 | (= (fround 1.2) 1.0) (= (fround 1.7) 2.0) (= (fround -1.7) -2.0)))) | |
f614ca12 DK |
574 | |
575 | (with-test-prefix/compile "List Built-Ins" | |
576 | ||
16254e5a | 577 | (pass-if "consp and atom" |
f614ca12 DK |
578 | (and (consp '(1 2 3)) (consp '(1 2 . 3)) (consp '(a . b)) |
579 | (not (consp '())) (not (consp 1)) (not (consp "abc")) | |
16254e5a BT |
580 | (atom 'a) (atom '()) (atom -1.5) (atom "abc") |
581 | (not (atom '(1 . 2))) (not (atom '(1))))) | |
f614ca12 DK |
582 | (pass-if "listp and nlistp" |
583 | (and (listp '(1 2 3)) (listp '(1)) (listp '()) (listp '(1 . 2)) | |
584 | (not (listp 'a)) (not (listp 42)) (nlistp 42) | |
585 | (not (nlistp '())) (not (nlistp '(1 2 3))) (not (nlistp '(1 . 2))))) | |
586 | (pass-if "null" | |
587 | (and (null '()) (not (null 1)) (not (null '(1 2))) (not (null '(1 . 2))))) | |
588 | ||
589 | (pass-if "car and cdr" | |
590 | (and (equal (car '(1 2 3)) 1) (equal (cdr '(1 2 3)) '(2 3)) | |
591 | (equal (car '()) nil) (equal (cdr '()) nil) | |
592 | (equal (car '(1 . 2)) 1) (equal (cdr '(1 . 2)) 2) | |
593 | (null (cdr '(1))))) | |
594 | (pass-if "car-safe and cdr-safe" | |
595 | (and (equal (car-safe '(1 2)) 1) (equal (cdr-safe '(1 2)) '(2)) | |
596 | (equal (car-safe 5) nil) (equal (cdr-safe 5) nil))) | |
597 | ||
f614ca12 DK |
598 | (pass-if "nth and nthcdr" |
599 | (and (equal (nth -5 '(1 2 3)) 1) (equal (nth 3 '(1 2 3)) nil) | |
600 | (equal (nth 0 '(1 2 3)) 1) (equal (nth 2 '(1 2 3)) 3) | |
601 | (equal (nthcdr -5 '(1 2 3)) '(1 2 3)) | |
602 | (equal (nthcdr 4 '(1 2 3)) nil) | |
603 | (equal (nthcdr 1 '(1 2 3)) '(2 3)) | |
604 | (equal (nthcdr 2 '(1 2 3)) '(3)))) | |
605 | ||
c2c7c277 DK |
606 | (pass-if "length" |
607 | (and (= (length '()) 0) | |
608 | (= (length '(1 2 3 4 5)) 5) | |
609 | (= (length '(1 2 (3 4 (5)) 6)) 4))) | |
610 | ||
f614ca12 DK |
611 | (pass-if "cons, list and make-list" |
612 | (and (equal (cons 1 2) '(1 . 2)) (equal (cons 1 '(2 3)) '(1 2 3)) | |
613 | (equal (cons 1 '()) '(1)) | |
614 | (equal (list 'a) '(a)) (equal (list) '()) (equal (list 1 2) '(1 2)) | |
615 | (equal (make-list 3 42) '(42 42 42)) | |
616 | (equal (make-list 0 1) '()))) | |
617 | (pass-if "append" | |
618 | (and (equal (append '(1 2) '(3 4) '(5)) '(1 2 3 4 5)) | |
619 | (equal (append '(1 2) 3) '(1 2 . 3)))) | |
620 | (pass-if "reverse" | |
621 | (and (equal (reverse '(5 4 3 2 1)) '(1 2 3 4 5)) | |
622 | (equal (reverse '()) '()))) | |
f614ca12 DK |
623 | (pass-if "setcar and setcdr" |
624 | (progn (setq pair '(1 . 2)) | |
625 | (setq copy pair) | |
626 | (setq a (setcar copy 3)) | |
627 | (setq b (setcdr copy 4)) | |
628 | (and (= a 3) (= b 4) | |
629 | (equal pair '(3 . 4)))))) |