elisp.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. ;;;; elisp.test --- tests guile's elisp support -*- scheme -*-
  2. ;;;; Copyright (C) 2002, 2003, 2006, 2009, 2010 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. (define-module (test-suite test-elisp)
  18. #:use-module (test-suite lib)
  19. #:use-module (system base compile)
  20. #:use-module (ice-9 weak-vector))
  21. (with-test-prefix "scheme"
  22. (with-test-prefix "nil value is a boolean"
  23. (pass-if "boolean?"
  24. (boolean? #nil)))
  25. (with-test-prefix "nil value is false"
  26. (pass-if "not"
  27. (eq? (not #nil) #t))
  28. (pass-if "if"
  29. (if #nil #f #t))
  30. (pass-if "and"
  31. (eq? (and #nil #t) #f))
  32. (pass-if "or"
  33. (eq? (or #nil #f) #f))
  34. (pass-if "cond"
  35. (cond (#nil #f) (else #t)))
  36. (pass-if "do"
  37. (call-with-current-continuation
  38. (lambda (exit)
  39. (do ((i 0 (+ i 1)))
  40. (#nil (exit #f))
  41. (if (> i 10)
  42. (exit #t)))))))
  43. (with-test-prefix "nil value as an empty list"
  44. (pass-if "list?"
  45. (list? #nil))
  46. (pass-if "null?"
  47. (null? #nil))
  48. (pass-if "sort"
  49. (eq? (sort #nil <) #nil)))
  50. (with-test-prefix "lists formed using nil value"
  51. (pass-if "list?"
  52. (list? (cons 'a #nil)))
  53. (pass-if "length of #nil"
  54. (= (length #nil) 0))
  55. (pass-if "length"
  56. (= (length (cons 'a (cons 'b (cons 'c #nil)))) 3))
  57. (pass-if "length (with backquoted list)"
  58. (= (length '(a b c . #nil)) 3))
  59. (pass-if "write (#nil)"
  60. (string=? (with-output-to-string
  61. (lambda () (write #nil)))
  62. "#nil")) ; Hmmm... should be "()" ?
  63. (pass-if "display (#nil)"
  64. (string=? (with-output-to-string
  65. (lambda () (display #nil)))
  66. "#nil")) ; Ditto.
  67. (pass-if "write (list)"
  68. (string=? (with-output-to-string
  69. (lambda () (write (cons 'a #nil))))
  70. "(a)"))
  71. (pass-if "display (list)"
  72. (string=? (with-output-to-string
  73. (lambda () (display (cons 'a #nil))))
  74. "(a)"))
  75. (pass-if "assq"
  76. (and (equal? (assq 1 '((1 one) (2 two) . #nil))
  77. '(1 one))
  78. (equal? (assq 3 '((1 one) (2 two) . #nil))
  79. #f)))
  80. (pass-if "assv"
  81. (and (equal? (assv 1 '((1 one) (2 two) . #nil))
  82. '(1 one))
  83. (equal? (assv 3 '((1 one) (2 two) . #nil))
  84. #f)))
  85. (pass-if "assoc"
  86. (and (equal? (assoc 1 '((1 one) (2 two) . #nil))
  87. '(1 one))
  88. (equal? (assoc 3 '((1 one) (2 two) . #nil))
  89. #f)))
  90. (pass-if "with-fluids*"
  91. (let ((f (make-fluid))
  92. (g (make-fluid)))
  93. (with-fluids* (cons f (cons g #nil))
  94. '(3 4)
  95. (lambda ()
  96. (and (eqv? (fluid-ref f) 3)
  97. (eqv? (fluid-ref g) 4))))))
  98. (pass-if "append!"
  99. (let ((a (copy-tree '(1 2 3)))
  100. (b (copy-tree '(4 5 6 . #nil)))
  101. (c (copy-tree '(7 8 9)))
  102. (d (copy-tree '(a b c . #nil))))
  103. (equal? (append! a b c d)
  104. '(1 2 3 4 5 6 7 8 9 a b c . #nil))))
  105. (pass-if "last-pair"
  106. (equal? (last-pair '(1 2 3 4 5 . #nil))
  107. (cons 5 #nil)))
  108. (pass-if "reverse"
  109. (equal? (reverse '(1 2 3 4 5 . #nil))
  110. '(5 4 3 2 1))) ; Hmmm... is this OK, or
  111. ; should it be
  112. ; '(5 4 3 2 1 . #nil) ?
  113. (pass-if "reverse!"
  114. (equal? (reverse! (copy-tree '(1 2 3 4 5 . #nil)))
  115. '(5 4 3 2 1))) ; Ditto.
  116. (pass-if "list-ref"
  117. (eqv? (list-ref '(0 1 2 3 4 . #nil) 4) 4))
  118. (pass-if-exception "list-ref"
  119. exception:out-of-range
  120. (eqv? (list-ref '(0 1 2 3 4 . #nil) 6) 6))
  121. (pass-if "list-set!"
  122. (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
  123. (list-set! l 4 44)
  124. (= (list-ref l 4) 44)))
  125. (pass-if-exception "list-set!"
  126. exception:out-of-range
  127. (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
  128. (list-set! l 6 44)
  129. (= (list-ref l 6) 44)))
  130. (pass-if "list-cdr-set!"
  131. (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
  132. (and (begin
  133. (list-cdr-set! l 4 44)
  134. (equal? l '(0 1 2 3 4 . 44)))
  135. (begin
  136. (list-cdr-set! l 3 '(new . #nil))
  137. (equal? l '(0 1 2 3 new . #nil))))))
  138. (pass-if-exception "list-cdr-set!"
  139. exception:out-of-range
  140. (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
  141. (list-cdr-set! l 6 44)))
  142. (pass-if "memq"
  143. (equal? (memq 'c '(a b c d . #nil)) '(c d . #nil)))
  144. (pass-if "memv"
  145. (equal? (memv 'c '(a b c d . #nil)) '(c d . #nil)))
  146. (pass-if "member"
  147. (equal? (member "c" '("a" "b" "c" "d" . #nil)) '("c" "d" . #nil)))
  148. (pass-if "list->vector"
  149. (equal? '#(1 2 3) (list->vector '(1 2 3 . #nil))))
  150. (pass-if "list->vector"
  151. (equal? '#(1 2 3) (list->vector '(1 2 3 . #nil))))
  152. (pass-if "list->weak-vector"
  153. (equal? (weak-vector 1 2 3) (list->weak-vector '(1 2 3 . #nil))))
  154. (pass-if "sorted?"
  155. (and (sorted? '(1 2 3 . #nil) <)
  156. (not (sorted? '(1 6 3 . #nil) <))))
  157. (pass-if "merge"
  158. (equal? (merge '(1 4 7 10)
  159. (merge '(2 5 8 11 . #nil)
  160. '(3 6 9 12 . #nil)
  161. <)
  162. <)
  163. '(1 2 3 4 5 6 7 8 9 10 11 12 . #nil)))
  164. (pass-if "merge!"
  165. (equal? (merge! (copy-tree '(1 4 7 10))
  166. (merge! (copy-tree '(2 5 8 11 . #nil))
  167. (copy-tree '(3 6 9 12 . #nil))
  168. <)
  169. <)
  170. '(1 2 3 4 5 6 7 8 9 10 11 12 . #nil)))
  171. (pass-if "sort"
  172. (equal? (sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8)))
  173. (pass-if "stable-sort"
  174. (equal? (stable-sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8)))
  175. (pass-if "sort!"
  176. (equal? (sort! (copy-tree '(1 5 3 8 4 . #nil)) <)
  177. '(1 3 4 5 8)))
  178. (pass-if "stable-sort!"
  179. (equal? (stable-sort! (copy-tree '(1 5 3 8 4 . #nil)) <)
  180. '(1 3 4 5 8))))
  181. (with-test-prefix "value preservation"
  182. (pass-if "car"
  183. (eq? (car (cons #nil 'a)) #nil))
  184. (pass-if "cdr"
  185. (eq? (cdr (cons 'a #nil)) #nil))
  186. (pass-if "vector-ref"
  187. (eq? (vector-ref (vector #nil) 0) #nil))))
  188. ;;;
  189. ;;; elisp
  190. ;;;
  191. (with-test-prefix "elisp"
  192. (define (elisp-pass-if expr expected)
  193. (pass-if (with-output-to-string
  194. (lambda ()
  195. (write expr)))
  196. (let ((calc (with-output-to-string
  197. (lambda ()
  198. (write (compile expr #:from 'elisp #:to 'value))))))
  199. (string=? calc expected))))
  200. (define (elisp-pass-if/maybe-error key expr expected)
  201. (pass-if (with-output-to-string (lambda () (write expr)))
  202. (string=?
  203. (catch key
  204. (lambda ()
  205. (with-output-to-string
  206. (lambda () (write (eval-elisp expr)))))
  207. (lambda (k . args)
  208. (format (current-error-port)
  209. "warning: caught ~a: ~a\n" k args)
  210. (throw 'unresolved)))
  211. expected)))
  212. (elisp-pass-if '(and #f) "#f")
  213. (elisp-pass-if '(and #t) "#t")
  214. (elisp-pass-if '(and nil) "#nil")
  215. (elisp-pass-if '(and t) "#t")
  216. (elisp-pass-if '(and) "#t")
  217. (elisp-pass-if '(cond (nil t) (t 3)) "3")
  218. (elisp-pass-if '(cond (nil t) (t)) "#t")
  219. (elisp-pass-if '(cond (nil)) "#nil")
  220. (elisp-pass-if '(cond) "#nil")
  221. (elisp-pass-if '(if #f 'a 'b) "b")
  222. (elisp-pass-if '(if #t 'a 'b) "a")
  223. (elisp-pass-if '(if nil 'a 'b) "b")
  224. (elisp-pass-if '(if nil 1 2 3 4) "4")
  225. (elisp-pass-if '(if nil 1 2) "2")
  226. (elisp-pass-if '(if nil 1) "#nil")
  227. (elisp-pass-if '(if t 1 2) "1")
  228. (elisp-pass-if '(if t 1) "1")
  229. (elisp-pass-if '(let (a) a) "#nil")
  230. (elisp-pass-if '(let* (a) a) "#nil")
  231. (elisp-pass-if '(let* ((a 1) (b (* a 2))) b) "2")
  232. (elisp-pass-if '(null nil) "#t")
  233. (elisp-pass-if '(or 1 2 3) "1")
  234. (elisp-pass-if '(or nil t nil) "#t")
  235. (elisp-pass-if '(or nil) "#nil")
  236. (elisp-pass-if '(or t nil t) "#t")
  237. (elisp-pass-if '(or t) "#t")
  238. (elisp-pass-if '(or) "#nil")
  239. (elisp-pass-if '(prog1 1 2 3) "1")
  240. (elisp-pass-if '(prog2 1 2 3) "2")
  241. (elisp-pass-if '(progn 1 2 3) "3")
  242. (elisp-pass-if '(while nil 1) "#nil")
  243. (elisp-pass-if '(defun testf (x y &optional o &rest r) (list x y o r)) "testf")
  244. (elisp-pass-if '(testf 1 2) "(1 2 #nil #nil)")
  245. (elisp-pass-if '(testf 1 2 3 4 5 56) "(1 2 3 (4 5 56))")
  246. ;; NB `lambda' in Emacs is self-quoting, but that's only after
  247. ;; loading the macro definition of lambda in subr.el.
  248. (elisp-pass-if '(funcall (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 4) "(1 2 3 (4))")
  249. (elisp-pass-if '(apply (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 nil)
  250. "(1 2 3 #nil)")
  251. (elisp-pass-if '(setq x 3) "3")
  252. (elisp-pass-if '(defvar x 4) "x")
  253. (elisp-pass-if 'x "3")
  254. ;; wingo 9 april 2010: the following 10 tests are currently failing. the if &
  255. ;; null tests are good, but I think some of the memq tests are bogus, given
  256. ;; our current thoughts on equalty and nil; though they should succeed with
  257. ;; memv and member in the elisp case. Also I think the function test is bogus.
  258. #;
  259. (elisp-pass-if '(if '() 'a 'b) "b")
  260. #;
  261. (elisp-pass-if '(null '#f) "#t")
  262. #;
  263. (elisp-pass-if '(null '()) "#t")
  264. #;
  265. (elisp-pass-if '(null 'nil) "#t")
  266. #;
  267. (elisp-pass-if '(memq '() '(())) "(())")
  268. #;
  269. (elisp-pass-if '(memq '() '(nil)) "(#nil)")
  270. #;
  271. (elisp-pass-if '(memq '() '(t)) "#nil")
  272. #;
  273. (elisp-pass-if '(memq nil '(())) "(())")
  274. #;
  275. (elisp-pass-if '(memq nil '(nil)) "(#nil)")
  276. #;
  277. (elisp-pass-if '(memq nil (list nil)) "(#nil)")
  278. #;
  279. (elisp-pass-if '(function (lambda (x y &optional o &rest r) (list x y o r))) "(lambda (x y &optional o &rest r) (list x y o r))")
  280. )
  281. ;;; elisp.test ends here