elisp-compiler.test 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. ;;;; elisp-compiler.test --- Test the compiler for Elisp. -*- scheme -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2009, 2010 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. (define-module (test-elisp-compiler)
  20. :use-module (test-suite lib)
  21. :use-module (system base compile)
  22. :use-module (language elisp runtime))
  23. ; Macros to handle the compilation conveniently.
  24. (define-syntax compile-test
  25. (syntax-rules (pass-if pass-if-equal pass-if-exception)
  26. ((_ (pass-if test-name exp))
  27. (pass-if test-name (compile 'exp #:from 'elisp #:to 'value)))
  28. ((_ (pass-if test-name exp #:opts opts))
  29. (pass-if test-name (compile 'exp #:from 'elisp #:to 'value #:opts opts)))
  30. ((_ (pass-if-equal test-name result exp))
  31. (pass-if test-name (equal? result
  32. (compile 'exp #:from 'elisp #:to 'value))))
  33. ((_ (pass-if-exception test-name exc exp))
  34. (pass-if-exception test-name exc
  35. (compile 'exp #:from 'elisp #:to 'value)))))
  36. (define-syntax with-test-prefix/compile
  37. (syntax-rules ()
  38. ((_ section-name exp ...)
  39. (with-test-prefix section-name (compile-test exp) ...))))
  40. ; Test control structures.
  41. ; ========================
  42. (compile '(%set-lexical-binding-mode #nil) #:from 'elisp #:to 'value)
  43. (with-test-prefix/compile "Sequencing"
  44. (pass-if-equal "progn" 1
  45. (progn (setq a 0)
  46. (setq a (1+ a))
  47. a))
  48. (pass-if-equal "empty progn" #nil
  49. (progn))
  50. (pass-if "prog1"
  51. (progn (setq a 0)
  52. (setq b (prog1 a (setq a (1+ a))))
  53. (and (= a 1) (= b 0))))
  54. (pass-if "prog2"
  55. (progn (setq a 0)
  56. (setq b (prog2 (setq a (1+ a))
  57. (setq a (1+ a))
  58. (setq a (1+ a))))
  59. (and (= a 3) (= b 2)))))
  60. (with-test-prefix/compile "Conditionals"
  61. (pass-if-equal "succeeding if" 1
  62. (if t 1 2))
  63. (pass-if "failing if"
  64. (and (= (if nil
  65. 1
  66. (setq a 2) (setq a (1+ a)) a)
  67. 3)
  68. (equal (if nil 1) nil)))
  69. (pass-if-equal "if with no else" #nil
  70. (if nil t))
  71. (pass-if-equal "empty cond" nil-value
  72. (cond))
  73. (pass-if-equal "all failing cond" nil-value
  74. (cond (nil) (nil)))
  75. (pass-if-equal "only condition" 5
  76. (cond (nil) (5)))
  77. (pass-if-equal "succeeding cond value" 42
  78. (cond (nil) (t 42) (t 0)))
  79. (pass-if-equal "succeeding cond side-effect" 42
  80. (progn (setq a 0)
  81. (cond (nil) (t (setq a 42) 1) (t (setq a 0)))
  82. a)))
  83. (with-test-prefix/compile "Combining Conditions"
  84. (pass-if-equal "empty and" t-value (and))
  85. (pass-if-equal "failing and" nil-value (and 1 2 nil 3))
  86. (pass-if-equal "succeeding and" 3 (and 1 2 3))
  87. (pass-if-equal "empty or" nil-value (or))
  88. (pass-if-equal "failing or" nil-value (or nil nil nil))
  89. (pass-if-equal "succeeding or" 1 (or nil 1 nil 2 nil 3))
  90. (pass-if-equal "not true" nil-value (not 1))
  91. (pass-if-equal "not false" t-value (not nil)))
  92. (with-test-prefix/compile "Iteration"
  93. (pass-if-equal "failing while" 0
  94. (progn (setq a 0)
  95. (while nil (setq a 1))
  96. a))
  97. (pass-if-equal "running while" 120
  98. (progn (setq prod 1
  99. i 1)
  100. (while (<= i 5)
  101. (setq prod (* i prod))
  102. (setq i (1+ i)))
  103. prod)))
  104. (with-test-prefix/compile "Exceptions"
  105. (pass-if "catch without exception"
  106. (and (setq a 0)
  107. (= (catch 'foobar
  108. (setq a (1+ a))
  109. (setq a (1+ a))
  110. a)
  111. 2)
  112. (= (catch (+ 1 2) a) 2)))
  113. ; FIXME: Figure out how to do this...
  114. ;(pass-if-exception "uncaught exception" 'elisp-exception
  115. ; (throw 'abc 1))
  116. (pass-if "catch and throw"
  117. (and (setq mylist '(1 2))
  118. (= (catch 'abc (throw 'abc 2) 1) 2)
  119. (= (catch 'abc (catch 'def (throw 'abc (1+ 0)) 2) 3) 1)
  120. (= (catch 'abc (catch 'def (throw 'def 1) 2) 3) 3)
  121. (= (catch mylist (catch (list 1 2) (throw mylist 1) 2) 3) 1)))
  122. (pass-if "unwind-protect"
  123. (progn (setq a 0 b 1 c 1)
  124. (catch 'exc
  125. (unwind-protect (progn (setq a 1)
  126. (throw 'exc 0))
  127. (setq a 0)
  128. (setq b 0)))
  129. (unwind-protect nil (setq c 0))
  130. (and (= a 0) (= b 0) (= c 0)
  131. (= (unwind-protect 42 1 2 3) 42)))))
  132. (with-test-prefix/compile "Eval"
  133. (pass-if-equal "basic eval" 3
  134. (progn (setq code '(+ 1 2))
  135. (eval code)))
  136. (pass-if "real dynamic code"
  137. (and (setq a 1 b 1 c 1)
  138. (defun set-code (var val)
  139. (list 'setq var val))
  140. (= a 1) (= b 1) (= c 1)
  141. (eval (set-code 'a '(+ 2 3)))
  142. (eval (set-code 'c 42))
  143. (= a 5) (= b 1) (= c 42)))
  144. ; Build code that recursively again and again calls eval. What we want is
  145. ; something like:
  146. ; (eval '(1+ (eval '(1+ (eval 1)))))
  147. (pass-if "recursive eval"
  148. (progn (setq depth 10 i depth)
  149. (setq code '(eval 0))
  150. (while (not (zerop i))
  151. (setq code (#{`}# (eval (quote (1+ (#{,}# code))))))
  152. (setq i (1- i)))
  153. (= (eval code) depth))))
  154. ; Test handling of variables.
  155. ; ===========================
  156. (with-test-prefix/compile "Variable Setting/Referencing"
  157. ; TODO: Check for variable-void error
  158. (pass-if-equal "setq and reference" 6
  159. (progn (setq a 1 b 2 c 3)
  160. (+ a b c)))
  161. (pass-if-equal "setq evaluation order" 1
  162. (progn (setq a 0 b 0)
  163. (setq a 1 b a)))
  164. (pass-if-equal "setq value" 2
  165. (progn (setq a 1 b 2)))
  166. (pass-if "set and symbol-value"
  167. (progn (setq myvar 'a)
  168. (and (= (set myvar 42) 42)
  169. (= a 42)
  170. (= (symbol-value myvar) 42))))
  171. (pass-if "void variables"
  172. (progn (setq a 1 b 2)
  173. (and (eq (makunbound 'b) 'b)
  174. (boundp 'a)
  175. (not (boundp 'b))))))
  176. (with-test-prefix/compile "Let and Let*"
  177. (pass-if-equal "let without value" nil-value
  178. (let (a (b 5)) a))
  179. (pass-if-equal "basic let" 0
  180. (progn (setq a 0)
  181. (let ((a 1)
  182. (b a))
  183. b)))
  184. (pass-if-equal "empty let" #nil (let ()))
  185. (pass-if "let*"
  186. (progn (setq a 0)
  187. (and (let* ((a 1)
  188. (b a))
  189. (= b 1))
  190. (let* (a b)
  191. (setq a 1 b 2)
  192. (and (= a 1) (= b 2)))
  193. (= a 0)
  194. (not (boundp 'b)))))
  195. (pass-if-equal "empty let*" #nil
  196. (let* ()))
  197. (pass-if "local scope"
  198. (progn (setq a 0)
  199. (setq b (let (a)
  200. (setq a 1)
  201. a))
  202. (and (= a 0)
  203. (= b 1)))))
  204. (with-test-prefix/compile "Lexical Scoping"
  205. (pass-if "basic let semantics"
  206. (and (setq a 1)
  207. (lexical-let ((a 2) (b a))
  208. (and (= a 2) (= b 1)))
  209. (lexical-let* ((a 2) (b a))
  210. (and (= a 2) (= b 2) (setq a 42) (= a 42)))
  211. (= a 1)))
  212. (pass-if "lexical scope with lexical-let's"
  213. (and (setq a 1)
  214. (defun dyna () a)
  215. (lexical-let (a)
  216. (setq a 2)
  217. (and (= a 2) (= (dyna) 1)))
  218. (= a 1)
  219. (lexical-let* (a)
  220. (setq a 2)
  221. (and (= a 2) (= (dyna) 1)))
  222. (= a 1)))
  223. (pass-if "lexical scoping vs. symbol-value / set"
  224. (and (setq a 1)
  225. (lexical-let ((a 2))
  226. (and (= a 2)
  227. (= (symbol-value 'a) 1)
  228. (set 'a 3)
  229. (= a 2)
  230. (= (symbol-value 'a) 3)))
  231. (= a 3)))
  232. (pass-if "let inside lexical-let"
  233. (and (setq a 1 b 1)
  234. (defun dynvals () (cons a b))
  235. (lexical-let ((a 2))
  236. (and (= a 2) (equal (dynvals) '(1 . 1))
  237. (let ((a 3) (b a))
  238. (declare (lexical a))
  239. (and (= a 3) (= b 2)
  240. (equal (dynvals) '(1 . 2))))
  241. (let* ((a 4) (b a))
  242. (declare (lexical a))
  243. (and (= a 4) (= b 4)
  244. (equal (dynvals) '(1 . 4))))
  245. (= a 2)))
  246. (= a 1)))
  247. (pass-if "lambda args inside lexical-let"
  248. (and (setq a 1)
  249. (defun dyna () a)
  250. (lexical-let ((a 2) (b 42))
  251. (and (= a 2) (= (dyna) 1)
  252. ((lambda (a)
  253. (declare (lexical a))
  254. (and (= a 3) (= b 42) (= (dyna) 1))) 3)
  255. ((lambda () (let ((a 3))
  256. (declare (lexical a))
  257. (and (= a 3) (= (dyna) 1)))))
  258. (= a 2) (= (dyna) 1)))
  259. (= a 1)))
  260. (pass-if "closures"
  261. (and (defun make-counter ()
  262. (lexical-let ((cnt 0))
  263. (lambda ()
  264. (setq cnt (1+ cnt)))))
  265. (setq c1 (make-counter) c2 (make-counter))
  266. (= (funcall c1) 1)
  267. (= (funcall c1) 2)
  268. (= (funcall c1) 3)
  269. (= (funcall c2) 1)
  270. (= (funcall c2) 2)
  271. (= (funcall c1) 4)
  272. (= (funcall c2) 3)))
  273. (pass-if "lexical lambda args"
  274. (progn (setq a 1 b 1)
  275. (defun dyna () a)
  276. (defun dynb () b)
  277. (lexical-let (a c)
  278. ((lambda (a b &optional c)
  279. (declare (lexical a c))
  280. (and (= a 3) (= (dyna) 1)
  281. (= b 2) (= (dynb) 2)
  282. (= c 1)))
  283. 3 2 1))))
  284. ; Check if a lambda without dynamically bound arguments
  285. ; is tail-optimized by doing a deep recursion that would otherwise overflow
  286. ; the stack.
  287. (pass-if "lexical lambda tail-recursion"
  288. (lexical-let (i)
  289. (setq to 1000000)
  290. (defun iteration-1 (i)
  291. (declare (lexical i))
  292. (if (< i to)
  293. (iteration-1 (1+ i))))
  294. (iteration-1 0)
  295. (setq x 0)
  296. (defun iteration-2 ()
  297. (if (< x to)
  298. (setq x (1+ x))
  299. (iteration-2)))
  300. (iteration-2)
  301. t)))
  302. (with-test-prefix/compile "defconst and defvar"
  303. (pass-if-equal "defconst without docstring" 3.141
  304. (progn (setq pi 3)
  305. (defconst pi 3.141)
  306. pi))
  307. (pass-if-equal "defconst value" 'pi
  308. (defconst pi 3.141 "Pi"))
  309. (pass-if-equal "defvar without value" 42
  310. (progn (setq a 42)
  311. (defvar a)
  312. a))
  313. (pass-if-equal "defvar on already defined variable" 42
  314. (progn (setq a 42)
  315. (defvar a 1 "Some docstring is also ok")
  316. a))
  317. (pass-if-equal "defvar on undefined variable" 1
  318. (progn (makunbound 'a)
  319. (defvar a 1)
  320. a))
  321. (pass-if-equal "defvar value" 'a
  322. (defvar a)))
  323. ; Functions and lambda expressions.
  324. ; =================================
  325. (with-test-prefix/compile "Lambda Expressions"
  326. (pass-if-equal "required arguments" 3
  327. ((lambda (a b c) c) 1 2 3))
  328. (pass-if-equal "optional argument" 3
  329. ((lambda (a &optional b c) c) 1 2 3))
  330. (pass-if-equal "optional missing" nil-value
  331. ((lambda (&optional a) a)))
  332. (pass-if-equal "rest argument" '(3 4 5)
  333. ((lambda (a b &rest c) c) 1 2 3 4 5))
  334. (pass-if "rest missing"
  335. (null ((lambda (a b &rest c) c) 1 2)))
  336. (pass-if-equal "empty lambda" #nil
  337. ((lambda ()))))
  338. (with-test-prefix/compile "Function Definitions"
  339. (pass-if-equal "defun" 3
  340. (progn (defun test (a b) (+ a b))
  341. (test 1 2)))
  342. (pass-if-equal "defun value" 'test
  343. (defun test (a b) (+ a b)))
  344. (pass-if "fset and symbol-function"
  345. (progn (setq myfunc 'x x 5)
  346. (and (= (fset myfunc 42) 42)
  347. (= (symbol-function myfunc) 42)
  348. (= x 5))))
  349. (pass-if "void function values"
  350. (progn (setq a 1)
  351. (defun test (a b) (+ a b))
  352. (fmakunbound 'a)
  353. (fset 'b 5)
  354. (and (fboundp 'b) (fboundp 'test)
  355. (not (fboundp 'a))
  356. (= a 1))))
  357. (pass-if "flet"
  358. (progn (defun foobar () 42)
  359. (defun test () (foobar))
  360. (and (= (test) 42)
  361. (flet ((foobar () 0)
  362. (myfoo ()
  363. (funcall (symbol-function 'foobar))))
  364. (and (= (myfoo) 42)
  365. (= (test) 42)))
  366. (flet ((foobar () nil))
  367. (defun foobar () 0)
  368. (= (test) 42))
  369. (= (test) 42)))))
  370. (with-test-prefix/compile "Calling Functions"
  371. (pass-if-equal "recursion" 120
  372. (progn (defun factorial (n prod)
  373. (if (zerop n)
  374. prod
  375. (factorial (1- n) (* prod n))))
  376. (factorial 5 1)))
  377. (pass-if "dynamic scoping"
  378. (progn (setq a 0)
  379. (defun foo ()
  380. (setq a (1+ a))
  381. a)
  382. (defun bar (a)
  383. (foo))
  384. (and (= 43 (bar 42))
  385. (zerop a))))
  386. (pass-if "funcall and apply argument handling"
  387. (and (defun allid (&rest args) args)
  388. (setq allid-var (symbol-function 'allid))
  389. (equal (funcall allid-var 1 2 3) '(1 2 3))
  390. (equal (funcall allid-var) nil)
  391. (equal (funcall allid-var 1 2 '(3 4)) '(1 2 (3 4)))
  392. (equal (funcall allid-var '()) '(()))
  393. (equal (apply allid-var 1 2 '(3 4)) '(1 2 3 4))
  394. (equal (apply allid-var '(1 2)) '(1 2))
  395. (equal (apply allid-var '()) nil)))
  396. (pass-if "raw functions with funcall"
  397. (and (= (funcall '+ 1 2) 3)
  398. (= (funcall (lambda (a b) (+ a b)) 1 2) 3)
  399. (= (funcall '(lambda (a b) (+ a b)) 1 2) 3))))
  400. ; Quoting and Backquotation.
  401. ; ==========================
  402. (with-test-prefix/compile "Quotation"
  403. (pass-if "quote"
  404. (and (equal '42 42) (equal '"abc" "abc")
  405. (equal '(1 2 (3 (4) x)) '(1 2 (3 (4) x)))
  406. (not (equal '(1 2 (3 4 (x))) '(1 2 3 4 x)))
  407. (equal '(1 2 . 3) '(1 2 . 3))))
  408. (pass-if "simple backquote"
  409. (and (equal (#{`}# 42) 42)
  410. (equal (#{`}# (1 (a))) '(1 (a)))
  411. (equal (#{`}# (1 . 2)) '(1 . 2))))
  412. (pass-if "unquote"
  413. (progn (setq a 42 l '(18 12))
  414. (and (equal (#{`}# (#{,}# a)) 42)
  415. (equal (#{`}# (1 a ((#{,}# l)) . (#{,}# a))) '(1 a ((18 12)) . 42)))))
  416. (pass-if "unquote splicing"
  417. (progn (setq l '(18 12) empty '())
  418. (and (equal (#{`}# (#{,@}# l)) '(18 12))
  419. (equal (#{`}# (l 2 (3 (#{,@}# l)) ((#{,@}# l)) (#{,@}# l)))
  420. '(l 2 (3 18 12) (18 12) 18 12))
  421. (equal (#{`}# (1 2 (#{,@}# empty) 3)) '(1 2 3))))))
  422. ; Macros.
  423. ; =======
  424. (with-test-prefix/compile "Macros"
  425. (pass-if-equal "defmacro value" 'magic-number
  426. (defmacro magic-number () 42))
  427. (pass-if-equal "macro expansion" 1
  428. (progn (defmacro take-first (a b) a)
  429. (take-first 1 (/ 1 0)))))
  430. ; Test the built-ins.
  431. ; ===================
  432. (with-test-prefix/compile "Equivalence Predicates"
  433. (pass-if "equal"
  434. (and (equal 2 2) (not (equal 1 2))
  435. (equal "abc" "abc") (not (equal "abc" "ABC"))
  436. (equal 'abc 'abc) (not (equal 'abc 'def))
  437. (equal '(1 2 (3 4) 5) '(1 2 (3 4) 5))
  438. (not (equal '(1 2 3 4 5) '(1 2 (3 4) 5)))))
  439. (pass-if "eq"
  440. (progn (setq some-list '(1 2))
  441. (setq some-string "abc")
  442. (and (eq 2 2) (not (eq 1 2))
  443. (eq 'abc 'abc) (not (eq 'abc 'def))
  444. (eq some-string some-string) (not (eq some-string (string 97 98 99)))
  445. (eq some-list some-list) (not (eq some-list (list 1 2)))))))
  446. (with-test-prefix/compile "Number Built-Ins"
  447. (pass-if "floatp"
  448. (and (floatp 1.0) (not (floatp 1)) (not (floatp 'a))))
  449. (pass-if "integerp"
  450. (and (integerp 42) (integerp -2) (not (integerp 1.0))))
  451. (pass-if "numberp"
  452. (and (numberp 1.0) (numberp -2) (not (numberp 'a))))
  453. (pass-if "wholenump"
  454. (and (wholenump 0) (not (wholenump -2)) (not (wholenump 1.0))))
  455. (pass-if "zerop"
  456. (and (zerop 0) (zerop 0.0) (not (zerop 1))))
  457. (pass-if "comparisons"
  458. (and (= 1 1.0) (/= 0 1)
  459. (< 1 2) (> 2 1) (>= 1 1) (<= 1 1)
  460. (not (< 1 1)) (not (<= 2 1))))
  461. (pass-if "max and min"
  462. (and (= (max -5 2 4.0 1) 4.0) (= (min -5 2 4.0 1) -5)
  463. (= (max 1) 1) (= (min 1) 1)))
  464. (pass-if "abs"
  465. (and (= (abs 1.0) 1.0) (= (abs -5) 5)))
  466. (pass-if "float"
  467. (and (= (float 1) 1) (= (float 5.5) 5.5)
  468. (floatp (float 1))))
  469. (pass-if-equal "basic arithmetic operators" -8.5
  470. (+ (1+ 0) (1- 0) (- 5.5) (* 2 -2) (- 2 1)))
  471. (pass-if "modulo"
  472. (= (% 5 3) 2))
  473. (pass-if "floating point rounding"
  474. (and (= (ffloor 1.7) 1.0) (= (ffloor -1.2) -2.0) (= (ffloor 1.0) 1.0)
  475. (= (fceiling 1.2) 2.0) (= (fceiling -1.7) -1.0) (= (fceiling 1.0) 1.0)
  476. (= (ftruncate 1.6) 1.0) (= (ftruncate -1.7) -1.0)
  477. (= (fround 1.2) 1.0) (= (fround 1.7) 2.0) (= (fround -1.7) -2.0))))
  478. (with-test-prefix/compile "List Built-Ins"
  479. (pass-if "consp and atom"
  480. (and (consp '(1 2 3)) (consp '(1 2 . 3)) (consp '(a . b))
  481. (not (consp '())) (not (consp 1)) (not (consp "abc"))
  482. (atom 'a) (atom '()) (atom -1.5) (atom "abc")
  483. (not (atom '(1 . 2))) (not (atom '(1)))))
  484. (pass-if "listp and nlistp"
  485. (and (listp '(1 2 3)) (listp '(1)) (listp '()) (listp '(1 . 2))
  486. (not (listp 'a)) (not (listp 42)) (nlistp 42)
  487. (not (nlistp '())) (not (nlistp '(1 2 3))) (not (nlistp '(1 . 2)))))
  488. (pass-if "null"
  489. (and (null '()) (not (null 1)) (not (null '(1 2))) (not (null '(1 . 2)))))
  490. (pass-if "car and cdr"
  491. (and (equal (car '(1 2 3)) 1) (equal (cdr '(1 2 3)) '(2 3))
  492. (equal (car '()) nil) (equal (cdr '()) nil)
  493. (equal (car '(1 . 2)) 1) (equal (cdr '(1 . 2)) 2)
  494. (null (cdr '(1)))))
  495. (pass-if "car-safe and cdr-safe"
  496. (and (equal (car-safe '(1 2)) 1) (equal (cdr-safe '(1 2)) '(2))
  497. (equal (car-safe 5) nil) (equal (cdr-safe 5) nil)))
  498. (pass-if "nth and nthcdr"
  499. (and (equal (nth -5 '(1 2 3)) 1) (equal (nth 3 '(1 2 3)) nil)
  500. (equal (nth 0 '(1 2 3)) 1) (equal (nth 2 '(1 2 3)) 3)
  501. (equal (nthcdr -5 '(1 2 3)) '(1 2 3))
  502. (equal (nthcdr 4 '(1 2 3)) nil)
  503. (equal (nthcdr 1 '(1 2 3)) '(2 3))
  504. (equal (nthcdr 2 '(1 2 3)) '(3))))
  505. (pass-if "length"
  506. (and (= (length '()) 0)
  507. (= (length '(1 2 3 4 5)) 5)
  508. (= (length '(1 2 (3 4 (5)) 6)) 4)))
  509. (pass-if "cons, list and make-list"
  510. (and (equal (cons 1 2) '(1 . 2)) (equal (cons 1 '(2 3)) '(1 2 3))
  511. (equal (cons 1 '()) '(1))
  512. (equal (list 'a) '(a)) (equal (list) '()) (equal (list 1 2) '(1 2))
  513. (equal (make-list 3 42) '(42 42 42))
  514. (equal (make-list 0 1) '())))
  515. (pass-if "append"
  516. (and (equal (append '(1 2) '(3 4) '(5)) '(1 2 3 4 5))
  517. (equal (append '(1 2) 3) '(1 2 . 3))))
  518. (pass-if "reverse"
  519. (and (equal (reverse '(5 4 3 2 1)) '(1 2 3 4 5))
  520. (equal (reverse '()) '())))
  521. (pass-if "setcar and setcdr"
  522. (progn (setq pair (cons 1 2))
  523. (setq copy pair)
  524. (setq a (setcar copy 3))
  525. (setq b (setcdr copy 4))
  526. (and (= a 3) (= b 4)
  527. (equal pair '(3 . 4))))))