elisp.test 11 KB

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