elisp.test 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ;;;; elisp.test --- tests guile's elisp support -*- scheme -*-
  2. ;;;; Copyright (C) 2002, 2003, 2006 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 2.1 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 (ice-9 weak-vector))
  20. ;;;
  21. ;;; elisp
  22. ;;;
  23. (if (defined? '%nil)
  24. (with-test-prefix "scheme"
  25. (with-test-prefix "nil value is a boolean"
  26. (pass-if "boolean?"
  27. (boolean? %nil))
  28. )
  29. (with-test-prefix "nil value is false"
  30. (pass-if "not"
  31. (eq? (not %nil) #t))
  32. (pass-if "if"
  33. (if %nil #f #t))
  34. (pass-if "and"
  35. (eq? (and %nil #t) #f))
  36. (pass-if "or"
  37. (eq? (or %nil #f) #f))
  38. (pass-if "cond"
  39. (cond (%nil #f) (else #t)))
  40. (pass-if "do"
  41. (call-with-current-continuation
  42. (lambda (exit)
  43. (do ((i 0 (+ i 1)))
  44. (%nil (exit #f))
  45. (if (> i 10)
  46. (exit #t))))))
  47. )
  48. (with-test-prefix "nil value as an empty list"
  49. (pass-if "list?"
  50. (list? %nil))
  51. (pass-if "null?"
  52. (null? %nil))
  53. (pass-if "sort"
  54. (eq? (sort %nil <) %nil))
  55. )
  56. (with-test-prefix "lists formed using nil value"
  57. (pass-if "list?"
  58. (list? (cons 'a %nil)))
  59. (pass-if "length of %nil"
  60. (= (length %nil) 0))
  61. (pass-if "length"
  62. (= (length (cons 'a (cons 'b (cons 'c %nil)))) 3))
  63. (pass-if "length (with backquoted list)"
  64. (= (length `(a b c . ,%nil)) 3))
  65. (pass-if "write (%nil)"
  66. (string=? (with-output-to-string
  67. (lambda () (write %nil)))
  68. "#nil")) ; Hmmm... should be "()" ?
  69. (pass-if "display (%nil)"
  70. (string=? (with-output-to-string
  71. (lambda () (display %nil)))
  72. "#nil")) ; Ditto.
  73. (pass-if "write (list)"
  74. (string=? (with-output-to-string
  75. (lambda () (write (cons 'a %nil))))
  76. "(a)"))
  77. (pass-if "display (list)"
  78. (string=? (with-output-to-string
  79. (lambda () (display (cons 'a %nil))))
  80. "(a)"))
  81. (pass-if "assq"
  82. (and (equal? (assq 1 `((1 one) (2 two) . ,%nil))
  83. '(1 one))
  84. (equal? (assq 3 `((1 one) (2 two) . ,%nil))
  85. #f)))
  86. (pass-if "assv"
  87. (and (equal? (assv 1 `((1 one) (2 two) . ,%nil))
  88. '(1 one))
  89. (equal? (assv 3 `((1 one) (2 two) . ,%nil))
  90. #f)))
  91. (pass-if "assoc"
  92. (and (equal? (assoc 1 `((1 one) (2 two) . ,%nil))
  93. '(1 one))
  94. (equal? (assoc 3 `((1 one) (2 two) . ,%nil))
  95. #f)))
  96. (pass-if "with-fluids*"
  97. (let ((f (make-fluid))
  98. (g (make-fluid)))
  99. (with-fluids* (cons f (cons g %nil))
  100. '(3 4)
  101. (lambda ()
  102. (and (eq? (fluid-ref f) 3)
  103. (eq? (fluid-ref g) 4))))))
  104. (pass-if "append!"
  105. (let ((a (copy-tree '(1 2 3)))
  106. (b (copy-tree `(4 5 6 . ,%nil)))
  107. (c (copy-tree '(7 8 9)))
  108. (d (copy-tree `(a b c . ,%nil))))
  109. (equal? (append! a b c d)
  110. `(1 2 3 4 5 6 7 8 9 a b c . ,%nil))))
  111. (pass-if "last-pair"
  112. (equal? (last-pair `(1 2 3 4 5 . ,%nil))
  113. (cons 5 %nil)))
  114. (pass-if "reverse"
  115. (equal? (reverse `(1 2 3 4 5 . ,%nil))
  116. '(5 4 3 2 1))) ; Hmmm... is this OK, or
  117. ; should it be
  118. ; `(5 4 3 2 1 . ,%nil) ?
  119. (pass-if "reverse!"
  120. (equal? (reverse! (copy-tree `(1 2 3 4 5 . ,%nil)))
  121. '(5 4 3 2 1))) ; Ditto.
  122. (pass-if "list-ref"
  123. (eq? (list-ref `(0 1 2 3 4 . ,%nil) 4) 4))
  124. (pass-if-exception "list-ref"
  125. exception:out-of-range
  126. (eq? (list-ref `(0 1 2 3 4 . ,%nil) 6) 6))
  127. (pass-if "list-set!"
  128. (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
  129. (list-set! l 4 44)
  130. (= (list-ref l 4) 44)))
  131. (pass-if-exception "list-set!"
  132. exception:out-of-range
  133. (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
  134. (list-set! l 6 44)
  135. (= (list-ref l 6) 44)))
  136. (pass-if "list-cdr-set!"
  137. (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
  138. (and (begin
  139. (list-cdr-set! l 4 44)
  140. (equal? l '(0 1 2 3 4 . 44)))
  141. (begin
  142. (list-cdr-set! l 3 `(new . ,%nil))
  143. (equal? l `(0 1 2 3 new . ,%nil))))))
  144. (pass-if-exception "list-cdr-set!"
  145. exception:out-of-range
  146. (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
  147. (list-cdr-set! l 6 44)))
  148. (pass-if "memq"
  149. (equal? (memq 'c `(a b c d . ,%nil)) `(c d . ,%nil)))
  150. (pass-if "memv"
  151. (equal? (memv 'c `(a b c d . ,%nil)) `(c d . ,%nil)))
  152. (pass-if "member"
  153. (equal? (member "c" `("a" "b" "c" "d" . ,%nil)) `("c" "d" . ,%nil)))
  154. (pass-if "list->vector"
  155. (equal? '#(1 2 3) (list->vector `(1 2 3 . ,%nil))))
  156. (pass-if "list->vector"
  157. (equal? '#(1 2 3) (list->vector `(1 2 3 . ,%nil))))
  158. (pass-if "list->weak-vector"
  159. (equal? (weak-vector 1 2 3) (list->weak-vector `(1 2 3 . ,%nil))))
  160. (pass-if "sorted?"
  161. (and (sorted? `(1 2 3 . ,%nil) <)
  162. (not (sorted? `(1 6 3 . ,%nil) <))))
  163. (pass-if "merge"
  164. (equal? (merge '(1 4 7 10)
  165. (merge `(2 5 8 11 . ,%nil)
  166. `(3 6 9 12 . ,%nil)
  167. <)
  168. <)
  169. `(1 2 3 4 5 6 7 8 9 10 11 12 . ,%nil)))
  170. (pass-if "merge!"
  171. (equal? (merge! (copy-tree '(1 4 7 10))
  172. (merge! (copy-tree `(2 5 8 11 . ,%nil))
  173. (copy-tree `(3 6 9 12 . ,%nil))
  174. <)
  175. <)
  176. `(1 2 3 4 5 6 7 8 9 10 11 12 . ,%nil)))
  177. (pass-if "sort"
  178. (equal? (sort `(1 5 3 8 4 . ,%nil) <) '(1 3 4 5 8)))
  179. (pass-if "stable-sort"
  180. (equal? (stable-sort `(1 5 3 8 4 . ,%nil) <) '(1 3 4 5 8)))
  181. (pass-if "sort!"
  182. (equal? (sort! (copy-tree `(1 5 3 8 4 . ,%nil)) <)
  183. '(1 3 4 5 8)))
  184. (pass-if "stable-sort!"
  185. (equal? (stable-sort! (copy-tree `(1 5 3 8 4 . ,%nil)) <)
  186. '(1 3 4 5 8)))
  187. )
  188. (with-test-prefix "value preservation"
  189. (pass-if "car"
  190. (eq? (car (cons %nil 'a)) %nil))
  191. (pass-if "cdr"
  192. (eq? (cdr (cons 'a %nil)) %nil))
  193. (pass-if "vector-ref"
  194. (eq? (vector-ref (vector %nil) 0) %nil))
  195. )
  196. ))
  197. (if (defined? '%nil)
  198. (use-modules (lang elisp interface)))
  199. (if (defined? '%nil)
  200. (with-test-prefix "elisp"
  201. (define (elisp-pass-if expr expected)
  202. (pass-if (with-output-to-string
  203. (lambda ()
  204. (write expr)))
  205. (let ((calc (with-output-to-string
  206. (lambda ()
  207. (write (eval-elisp expr))))))
  208. (string=? calc expected))))
  209. (elisp-pass-if '(and #f) "#f")
  210. (elisp-pass-if '(and #t) "#t")
  211. (elisp-pass-if '(and nil) "#nil")
  212. (elisp-pass-if '(and t) "#t")
  213. (elisp-pass-if '(and) "#t")
  214. (elisp-pass-if '(cond (nil t) (t 3)) "3")
  215. (elisp-pass-if '(cond (nil t) (t)) "#t")
  216. (elisp-pass-if '(cond (nil)) "#nil")
  217. (elisp-pass-if '(cond) "#nil")
  218. (elisp-pass-if '(if #f 'a 'b) "b")
  219. (elisp-pass-if '(if #t 'a 'b) "a")
  220. (elisp-pass-if '(if '() 'a 'b) "b")
  221. (elisp-pass-if '(if nil 'a 'b) "b")
  222. (elisp-pass-if '(if nil 1 2 3 4) "4")
  223. (elisp-pass-if '(if nil 1 2) "2")
  224. (elisp-pass-if '(if nil 1) "#nil")
  225. (elisp-pass-if '(if t 1 2) "1")
  226. (elisp-pass-if '(if t 1) "1")
  227. (elisp-pass-if '(let (a) a) "#nil")
  228. (elisp-pass-if '(let* (a) a) "#nil")
  229. (elisp-pass-if '(let* ((a 1) (b (* a 2))) b) "2")
  230. (elisp-pass-if '(memq '() '(())) "(())")
  231. (elisp-pass-if '(memq '() '(nil)) "(#nil)")
  232. (elisp-pass-if '(memq '() '(t)) "#nil")
  233. (elisp-pass-if '(memq nil '(())) "(())")
  234. (elisp-pass-if '(memq nil '(nil)) "(#nil)")
  235. (elisp-pass-if '(memq nil (list nil)) "(#nil)")
  236. (elisp-pass-if '(null '#f) "#t")
  237. (elisp-pass-if '(null '()) "#t")
  238. (elisp-pass-if '(null 'nil) "#t")
  239. (elisp-pass-if '(null nil) "#t")
  240. (elisp-pass-if '(or 1 2 3) "1")
  241. (elisp-pass-if '(or nil t nil) "#t")
  242. (elisp-pass-if '(or nil) "#nil")
  243. (elisp-pass-if '(or t nil t) "#t")
  244. (elisp-pass-if '(or t) "#t")
  245. (elisp-pass-if '(or) "#nil")
  246. (elisp-pass-if '(prog1 1 2 3) "1")
  247. (elisp-pass-if '(prog2 1 2 3) "2")
  248. (elisp-pass-if '(progn 1 2 3) "3")
  249. (elisp-pass-if '(while nil 1) "#nil")
  250. (elisp-pass-if '(defun testf (x y &optional o &rest r) (list x y o r)) "testf")
  251. (elisp-pass-if '(testf 1 2) "(1 2 #nil #nil)")
  252. (elisp-pass-if '(testf 1 2 3 4 5 56) "(1 2 3 (4 5 56))")
  253. ;; NB `lambda' in Emacs is self-quoting, but that's only after
  254. ;; loading the macro definition of lambda in subr.el.
  255. (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))")
  256. (elisp-pass-if '(funcall (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 4) "(1 2 3 (4))")
  257. (elisp-pass-if '(apply (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 nil) "(1 2 3 #nil)")
  258. (elisp-pass-if '(setq x 3) "3")
  259. (elisp-pass-if '(defvar x 4) "x")
  260. (elisp-pass-if 'x "3")
  261. ))
  262. ;;; elisp.test ends here