elisp test fixes
[bpt/guile.git] / test-suite / tests / elisp-compiler.test
CommitLineData
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
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)
59e46065 172 (= (catch mylist (catch (list 1 2) (throw mylist 1) 2) 3) 1)))
33da12ee
DK
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
e96a9591
DK
185(with-test-prefix/compile "Eval"
186
187 (pass-if-equal "basic eval" 3
188 (progn (setq code '(+ 1 2))
189 (eval code)))
190
191 (pass-if "real dynamic code"
192 (and (setq a 1 b 1 c 1)
193 (defun set-code (var val)
194 (list 'setq var val))
195 (= a 1) (= b 1) (= c 1)
196 (eval (set-code 'a '(+ 2 3)))
197 (eval (set-code 'c 42))
198 (= a 5) (= b 1) (= c 42)))
199
200 ; Build code that recursively again and again calls eval. What we want is
201 ; something like:
202 ; (eval '(1+ (eval '(1+ (eval 1)))))
203 (pass-if "recursive eval"
204 (progn (setq depth 10 i depth)
205 (setq code '(eval 0))
206 (while (not (zerop i))
0dbfdeef 207 (setq code (#{`}# (eval (quote (1+ (#{,}# code))))))
e96a9591
DK
208 (setq i (1- i)))
209 (= (eval code) depth))))
210
d158fa62
DK
211
212; Test handling of variables.
213; ===========================
214
215(with-test-prefix/compile "Variable Setting/Referencing"
216
217 ; TODO: Check for variable-void error
218
219 (pass-if-equal "setq and reference" 6
570c12ac
DK
220 (progn (setq a 1 b 2 c 3)
221 (+ a b c)))
e96a9591
DK
222 (pass-if-equal "setq evaluation order" 1
223 (progn (setq a 0 b 0)
224 (setq a 1 b a)))
570c12ac 225 (pass-if-equal "setq value" 2
37099846
DK
226 (progn (setq a 1 b 2)))
227
228 (pass-if "set and symbol-value"
229 (progn (setq myvar 'a)
230 (and (= (set myvar 42) 42)
231 (= a 42)
232 (= (symbol-value myvar) 42))))
233 (pass-if "void variables"
234 (progn (setq a 1 b 2)
235 (and (eq (makunbound 'b) 'b)
236 (boundp 'a)
3f70b2dc 237 (not (boundp 'b))))))
d158fa62
DK
238
239(with-test-prefix/compile "Let and Let*"
240
241 (pass-if-equal "let without value" nil-value
242 (let (a (b 5)) a))
243 (pass-if-equal "basic let" 0
244 (progn (setq a 0)
245 (let ((a 1)
246 (b a))
247 b)))
fd40f371
DK
248
249 (pass-if "let*"
d158fa62 250 (progn (setq a 0)
fd40f371
DK
251 (and (let* ((a 1)
252 (b a))
253 (= b 1))
254 (let* (a b)
255 (setq a 1 b 2)
256 (and (= a 1) (= b 2)))
257 (= a 0)
258 (not (boundp 'b)))))
d158fa62
DK
259
260 (pass-if "local scope"
261 (progn (setq a 0)
262 (setq b (let (a)
263 (setq a 1)
264 a))
265 (and (= a 0)
266 (= b 1)))))
267
a6a5cf03
DK
268(with-test-prefix/compile "Lexical Scoping"
269
270 (pass-if "basic let semantics"
271 (and (setq a 1)
272 (lexical-let ((a 2) (b a))
273 (and (= a 2) (= b 1)))
274 (lexical-let* ((a 2) (b a))
275 (and (= a 2) (= b 2) (setq a 42) (= a 42)))
276 (= a 1)))
277
278 (pass-if "lexical scope with lexical-let's"
279 (and (setq a 1)
280 (defun dyna () a)
281 (lexical-let (a)
282 (setq a 2)
283 (and (= a 2) (= (dyna) 1)))
284 (= a 1)
285 (lexical-let* (a)
286 (setq a 2)
287 (and (= a 2) (= (dyna) 1)))
288 (= a 1)))
289
290 (pass-if "lexical scoping vs. symbol-value / set"
291 (and (setq a 1)
292 (lexical-let ((a 2))
293 (and (= a 2)
294 (= (symbol-value 'a) 1)
295 (set 'a 3)
296 (= a 2)
297 (= (symbol-value 'a) 3)))
298 (= a 3)))
299
300 (pass-if "let inside lexical-let"
301 (and (setq a 1 b 1)
302 (defun dynvals () (cons a b))
303 (lexical-let ((a 2))
304 (and (= a 2) (equal (dynvals) '(1 . 1))
305 (let ((a 3) (b a))
306 (and (= a 3) (= b 2)
307 (equal (dynvals) '(1 . 2))))
308 (let* ((a 4) (b a))
309 (and (= a 4) (= b 4)
310 (equal (dynvals) '(1 . 4))))
311 (= a 2)))
312 (= a 1)))
313
314 (pass-if "lambda args inside lexical-let"
315 (and (setq a 1)
316 (defun dyna () a)
317 (lexical-let ((a 2) (b 42))
318 (and (= a 2) (= (dyna) 1)
319 ((lambda (a) (and (= a 3) (= b 42) (= (dyna) 3))) 3)
dfbc6e9d
DK
320 ((lambda () (let ((a 3))
321 (and (= a 3) (= (dyna) 1)))))
a6a5cf03
DK
322 (= a 2) (= (dyna) 1)))
323 (= a 1)))
324
325 (pass-if "closures"
326 (and (defun make-counter ()
327 (lexical-let ((cnt 0))
328 (lambda ()
329 (setq cnt (1+ cnt)))))
330 (setq c1 (make-counter) c2 (make-counter))
ce305387
DK
331 (= (funcall c1) 1)
332 (= (funcall c1) 2)
333 (= (funcall c1) 3)
334 (= (funcall c2) 1)
335 (= (funcall c2) 2)
336 (= (funcall c1) 4)
c808c926
DK
337 (= (funcall c2) 3)))
338
339 (pass-if "always lexical option (all)"
340 (progn (setq a 0)
341 (defun dyna () a)
342 (let ((a 1))
343 (and (= a 1) (= (dyna) 0))))
344 #:opts '(#:always-lexical all))
345 (pass-if "always lexical option (list)"
346 (progn (setq a 0 b 0)
347 (defun dyna () a)
348 (defun dynb () b)
349 (let ((a 1)
350 (b 1))
351 (and (= a 1) (= (dyna) 0)
352 (= b 1) (= (dynb) 1))))
353 #:opts '(#:always-lexical (a)))
354 (pass-if "with-always-lexical"
355 (progn (setq a 0)
356 (defun dyna () a)
357 (with-always-lexical (a)
358 (let ((a 1))
dfbc6e9d
DK
359 (and (= a 1) (= (dyna) 0))))))
360
361 (pass-if "lexical lambda args"
362 (progn (setq a 1 b 1)
363 (defun dyna () a)
364 (defun dynb () b)
365 (with-always-lexical (a c)
366 ((lambda (a b &optional c)
367 (and (= a 3) (= (dyna) 1)
368 (= b 2) (= (dynb) 2)
369 (= c 1)))
370 3 2 1))))
371
372 ; Check if a lambda without dynamically bound arguments
373 ; is tail-optimized by doing a deep recursion that would otherwise overflow
374 ; the stack.
375 (pass-if "lexical lambda tail-recursion"
376 (with-always-lexical (i)
377 (setq to 1000000)
378 (defun iteration-1 (i)
379 (if (< i to)
380 (iteration-1 (1+ i))))
381 (iteration-1 0)
382 (setq x 0)
383 (defun iteration-2 ()
384 (if (< x to)
385 (setq x (1+ x))
386 (iteration-2)))
387 (iteration-2)
388 t)))
389
a6a5cf03 390
d158fa62
DK
391(with-test-prefix/compile "defconst and defvar"
392
393 (pass-if-equal "defconst without docstring" 3.141
394 (progn (setq pi 3)
395 (defconst pi 3.141)
396 pi))
397 (pass-if-equal "defconst value" 'pi
398 (defconst pi 3.141 "Pi"))
399
400 (pass-if-equal "defvar without value" 42
401 (progn (setq a 42)
402 (defvar a)
403 a))
404 (pass-if-equal "defvar on already defined variable" 42
405 (progn (setq a 42)
406 (defvar a 1 "Some docstring is also ok")
407 a))
d158fa62 408 (pass-if-equal "defvar on undefined variable" 1
37099846
DK
409 (progn (makunbound 'a)
410 (defvar a 1)
d158fa62
DK
411 a))
412 (pass-if-equal "defvar value" 'a
413 (defvar a)))
414
415
416; Functions and lambda expressions.
417; =================================
418
419(with-test-prefix/compile "Lambda Expressions"
420
421 (pass-if-equal "required arguments" 3
422 ((lambda (a b c) c) 1 2 3))
423
424 (pass-if-equal "optional argument" 3
425 ((function (lambda (a &optional b c) c)) 1 2 3))
426 (pass-if-equal "optional missing" nil-value
427 ((lambda (&optional a) a)))
428
429 (pass-if-equal "rest argument" '(3 4 5)
430 ((lambda (a b &rest c) c) 1 2 3 4 5))
431 (pass-if-equal "rest missing" nil-value
432 ((lambda (a b &rest c) c) 1 2)))
433
434(with-test-prefix/compile "Function Definitions"
435
436 (pass-if-equal "defun" 3
437 (progn (defun test (a b) (+ a b))
438 (test 1 2)))
439 (pass-if-equal "defun value" 'test
37099846
DK
440 (defun test (a b) (+ a b)))
441
442 (pass-if "fset and symbol-function"
443 (progn (setq myfunc 'x x 5)
444 (and (= (fset myfunc 42) 42)
445 (= (symbol-function myfunc) 42)
446 (= x 5))))
447 (pass-if "void function values"
448 (progn (setq a 1)
449 (defun test (a b) (+ a b))
450 (fmakunbound 'a)
451 (fset 'b 5)
452 (and (fboundp 'b) (fboundp 'test)
453 (not (fboundp 'a))
e8f18b3f
DK
454 (= a 1))))
455
456 (pass-if "flet and flet*"
457 (progn (defun foobar () 42)
458 (defun test () (foobar))
459 (and (= (test) 42)
460 (flet ((foobar (lambda () 0))
461 (myfoo (symbol-function 'foobar)))
462 (and (= (myfoo) 42)
c6920dc8 463 (= (test) 42)))
e8f18b3f
DK
464 (flet* ((foobar (lambda () 0))
465 (myfoo (symbol-function 'foobar)))
c6920dc8 466 (= (myfoo) 42))
e8f18b3f
DK
467 (flet (foobar)
468 (defun foobar () 0)
c6920dc8 469 (= (test) 42))
e8f18b3f 470 (= (test) 42)))))
d158fa62
DK
471
472(with-test-prefix/compile "Calling Functions"
473
474 (pass-if-equal "recursion" 120
475 (progn (defun factorial (n prod)
476 (if (zerop n)
477 prod
478 (factorial (1- n) (* prod n))))
479 (factorial 5 1)))
480
481 (pass-if "dynamic scoping"
482 (progn (setq a 0)
483 (defun foo ()
484 (setq a (1+ a))
485 a)
486 (defun bar (a)
487 (foo))
488 (and (= 43 (bar 42))
e96a9591
DK
489 (zerop a))))
490
491 (pass-if "funcall and apply argument handling"
492 (and (defun allid (&rest args) args)
493 (setq allid-var (symbol-function 'allid))
494 (equal (funcall allid-var 1 2 3) '(1 2 3))
495 (equal (funcall allid-var) nil)
496 (equal (funcall allid-var 1 2 '(3 4)) '(1 2 (3 4)))
497 (equal (funcall allid-var '()) '(()))
498 (equal (apply allid-var 1 2 '(3 4)) '(1 2 3 4))
499 (equal (apply allid-var '(1 2)) '(1 2))
500 (equal (apply allid-var '()) nil)))
501
502 (pass-if "raw functions with funcall"
503 (and (= (funcall '+ 1 2) 3)
504 (= (funcall (lambda (a b) (+ a b)) 1 2) 3)
505 (= (funcall '(lambda (a b) (+ a b)) 1 2) 3))))
b6b9d596
DK
506
507
9b5ff6a6
DK
508; Quoting and Backquotation.
509; ==========================
510
511(with-test-prefix/compile "Quotation"
512
513 (pass-if "quote"
514 (and (equal '42 42) (equal '"abc" "abc")
515 (equal '(1 2 (3 (4) x)) '(1 2 (3 (4) x)))
516 (not (equal '(1 2 (3 4 (x))) '(1 2 3 4 x)))
517 (equal '(1 2 . 3) '(1 2 . 3))))
518
519 (pass-if "simple backquote"
0dbfdeef
BT
520 (and (equal (#{`}# 42) 42)
521 (equal (#{`}# (1 (a))) '(1 (a)))
522 (equal (#{`}# (1 . 2)) '(1 . 2))))
9b5ff6a6
DK
523 (pass-if "unquote"
524 (progn (setq a 42 l '(18 12))
0dbfdeef
BT
525 (and (equal (#{`}# (#{,}# a)) 42)
526 (equal (#{`}# (1 a ((#{,}# l)) . (#{,}# a))) '(1 a ((18 12)) . 42)))))
9b5ff6a6
DK
527 (pass-if "unquote splicing"
528 (progn (setq l '(18 12) empty '())
0dbfdeef
BT
529 (and (equal (#{`}# (#{,@}# l)) '(18 12))
530 (equal (#{`}# (l 2 (3 (#{,@}# l)) ((#{,@}# l)) (#{,@}# l)))
9b5ff6a6 531 '(l 2 (3 18 12) (18 12) 18 12))
0dbfdeef 532 (equal (#{`}# (1 2 (#{,@}# empty) 3)) '(1 2 3))))))
9b5ff6a6
DK
533
534
535
74c009da
DK
536; Macros.
537; =======
538
539(with-test-prefix/compile "Macros"
540
541 (pass-if-equal "defmacro value" 'magic-number
542 (defmacro magic-number () 42))
543
544 (pass-if-equal "macro expansion" 1
545 (progn (defmacro take-first (a b) a)
546 (take-first 1 (/ 1 0)))))
547
548
b6b9d596
DK
549; Test the built-ins.
550; ===================
551
e905e490
DK
552(with-test-prefix/compile "Equivalence Predicates"
553
554 (pass-if "equal"
555 (and (equal 2 2) (not (equal 1 2))
556 (equal "abc" "abc") (not (equal "abc" "ABC"))
557 (equal 'abc 'abc) (not (equal 'abc 'def))
558 (equal '(1 2 (3 4) 5) '(1 2 (3 4) 5))
559 (not (equal '(1 2 3 4 5) '(1 2 (3 4) 5)))))
560
561 (pass-if "eq"
562 (progn (setq some-list '(1 2))
563 (setq some-string "abc")
564 (and (eq 2 2) (not (eq 1 2))
565 (eq 'abc 'abc) (not (eq 'abc 'def))
59e46065
BT
566 (eq some-string some-string) (not (eq some-string (string 97 98 99)))
567 (eq some-list some-list) (not (eq some-list (list 1 2)))))))
e905e490 568
b6b9d596
DK
569(with-test-prefix/compile "Number Built-Ins"
570
571 (pass-if "floatp"
572 (and (floatp 1.0) (not (floatp 1)) (not (floatp 'a))))
573 (pass-if "integerp"
574 (and (integerp 42) (integerp -2) (not (integerp 1.0))))
575 (pass-if "numberp"
576 (and (numberp 1.0) (numberp -2) (not (numberp 'a))))
577 (pass-if "wholenump"
578 (and (wholenump 0) (not (wholenump -2)) (not (wholenump 1.0))))
579 (pass-if "zerop"
580 (and (zerop 0) (zerop 0.0) (not (zerop 1))))
581
582 (pass-if "comparisons"
583 (and (= 1 1.0) (/= 0 1)
584 (< 1 2) (> 2 1) (>= 1 1) (<= 1 1)
585 (not (< 1 1)) (not (<= 2 1))))
586
587 (pass-if "max and min"
588 (and (= (max -5 2 4.0 1) 4.0) (= (min -5 2 4.0 1) -5)
589 (= (max 1) 1) (= (min 1) 1)))
590 (pass-if "abs"
591 (and (= (abs 1.0) 1.0) (= (abs -5) 5)))
592
593 (pass-if "float"
594 (and (= (float 1) 1) (= (float 5.5) 5.5)
595 (floatp (float 1))))
596
597 (pass-if-equal "basic arithmetic operators" -8.5
598 (+ (1+ 0) (1- 0) (- 5.5) (* 2 -2) (- 2 1)))
599 (pass-if "modulo"
600 (= (% 5 3) 2))
601
602 (pass-if "floating point rounding"
603 (and (= (ffloor 1.7) 1.0) (= (ffloor -1.2) -2.0) (= (ffloor 1.0) 1.0)
604 (= (fceiling 1.2) 2.0) (= (fceiling -1.7) -1.0) (= (fceiling 1.0) 1.0)
605 (= (ftruncate 1.6) 1.0) (= (ftruncate -1.7) -1.0)
606 (= (fround 1.2) 1.0) (= (fround 1.7) 2.0) (= (fround -1.7) -2.0))))
f614ca12
DK
607
608(with-test-prefix/compile "List Built-Ins"
609
16254e5a 610 (pass-if "consp and atom"
f614ca12
DK
611 (and (consp '(1 2 3)) (consp '(1 2 . 3)) (consp '(a . b))
612 (not (consp '())) (not (consp 1)) (not (consp "abc"))
16254e5a
BT
613 (atom 'a) (atom '()) (atom -1.5) (atom "abc")
614 (not (atom '(1 . 2))) (not (atom '(1)))))
f614ca12
DK
615 (pass-if "listp and nlistp"
616 (and (listp '(1 2 3)) (listp '(1)) (listp '()) (listp '(1 . 2))
617 (not (listp 'a)) (not (listp 42)) (nlistp 42)
618 (not (nlistp '())) (not (nlistp '(1 2 3))) (not (nlistp '(1 . 2)))))
619 (pass-if "null"
620 (and (null '()) (not (null 1)) (not (null '(1 2))) (not (null '(1 . 2)))))
621
622 (pass-if "car and cdr"
623 (and (equal (car '(1 2 3)) 1) (equal (cdr '(1 2 3)) '(2 3))
624 (equal (car '()) nil) (equal (cdr '()) nil)
625 (equal (car '(1 . 2)) 1) (equal (cdr '(1 . 2)) 2)
626 (null (cdr '(1)))))
627 (pass-if "car-safe and cdr-safe"
628 (and (equal (car-safe '(1 2)) 1) (equal (cdr-safe '(1 2)) '(2))
629 (equal (car-safe 5) nil) (equal (cdr-safe 5) nil)))
630
631 (pass-if "pop"
632 (progn (setq mylist '(a b c))
633 (setq value (pop mylist))
634 (and (equal value 'a)
635 (equal mylist '(b c)))))
636 (pass-if-equal "push" '(a b c)
637 (progn (setq mylist '(b c))
638 (push 'a mylist)))
639
640 (pass-if "nth and nthcdr"
641 (and (equal (nth -5 '(1 2 3)) 1) (equal (nth 3 '(1 2 3)) nil)
642 (equal (nth 0 '(1 2 3)) 1) (equal (nth 2 '(1 2 3)) 3)
643 (equal (nthcdr -5 '(1 2 3)) '(1 2 3))
644 (equal (nthcdr 4 '(1 2 3)) nil)
645 (equal (nthcdr 1 '(1 2 3)) '(2 3))
646 (equal (nthcdr 2 '(1 2 3)) '(3))))
647
c2c7c277
DK
648 (pass-if "length"
649 (and (= (length '()) 0)
650 (= (length '(1 2 3 4 5)) 5)
651 (= (length '(1 2 (3 4 (5)) 6)) 4)))
652
f614ca12
DK
653 (pass-if "cons, list and make-list"
654 (and (equal (cons 1 2) '(1 . 2)) (equal (cons 1 '(2 3)) '(1 2 3))
655 (equal (cons 1 '()) '(1))
656 (equal (list 'a) '(a)) (equal (list) '()) (equal (list 1 2) '(1 2))
657 (equal (make-list 3 42) '(42 42 42))
658 (equal (make-list 0 1) '())))
659 (pass-if "append"
660 (and (equal (append '(1 2) '(3 4) '(5)) '(1 2 3 4 5))
661 (equal (append '(1 2) 3) '(1 2 . 3))))
662 (pass-if "reverse"
663 (and (equal (reverse '(5 4 3 2 1)) '(1 2 3 4 5))
664 (equal (reverse '()) '())))
f614ca12
DK
665 (pass-if "setcar and setcdr"
666 (progn (setq pair '(1 . 2))
667 (setq copy pair)
668 (setq a (setcar copy 3))
669 (setq b (setcdr copy 4))
670 (and (= a 3) (= b 4)
671 (equal pair '(3 . 4))))))