elisp-compiler.test 19 KB

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