1 ;;;; elisp.test --- tests guile's elisp support -*- scheme -*-
2 ;;;; Copyright (C) 2002, 2003, 2006, 2009, 2010 Free Software Foundation, Inc.
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.
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.
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
18 (define-module (test-suite test-elisp)
19 #:use-module (test-suite lib)
20 #:use-module (system base compile)
21 #:use-module (ice-9 weak-vector))
23 (with-test-prefix "scheme"
25 (with-test-prefix "nil value is a boolean"
31 (with-test-prefix "nil value is false"
40 (eq? (and #nil #t) #f))
43 (eq? (or #nil #f) #f))
46 (cond (#nil #f) (else #t)))
49 (call-with-current-continuation
57 (with-test-prefix "nil value as an empty list"
66 (eq? (sort #nil <) #nil)))
69 (with-test-prefix "lists formed using nil value"
72 (list? (cons 'a #nil)))
74 (pass-if "length of #nil"
78 (= (length (cons 'a (cons 'b (cons 'c #nil)))) 3))
80 (pass-if "length (with backquoted list)"
81 (= (length '(a b c . #nil)) 3))
83 (pass-if "write (#nil)"
84 (string=? (with-output-to-string
85 (lambda () (write #nil)))
86 "#nil")) ; Hmmm... should be "()" ?
88 (pass-if "display (#nil)"
89 (string=? (with-output-to-string
90 (lambda () (display #nil)))
93 (pass-if "write (list)"
94 (string=? (with-output-to-string
95 (lambda () (write (cons 'a #nil))))
98 (pass-if "display (list)"
99 (string=? (with-output-to-string
100 (lambda () (display (cons 'a #nil))))
104 (and (equal? (assq 1 '((1 one) (2 two) . #nil))
106 (equal? (assq 3 '((1 one) (2 two) . #nil))
110 (and (equal? (assv 1 '((1 one) (2 two) . #nil))
112 (equal? (assv 3 '((1 one) (2 two) . #nil))
116 (and (equal? (assoc 1 '((1 one) (2 two) . #nil))
118 (equal? (assoc 3 '((1 one) (2 two) . #nil))
121 (pass-if "with-fluids*"
122 (let ((f (make-fluid))
124 (with-fluids* (cons f (cons g #nil))
127 (and (eqv? (fluid-ref f) 3)
128 (eqv? (fluid-ref g) 4))))))
131 (let ((a (copy-tree '(1 2 3)))
132 (b (copy-tree '(4 5 6 . #nil)))
133 (c (copy-tree '(7 8 9)))
134 (d (copy-tree '(a b c . #nil))))
135 (equal? (append! a b c d)
136 '(1 2 3 4 5 6 7 8 9 a b c . #nil))))
139 (equal? (last-pair '(1 2 3 4 5 . #nil))
143 (equal? (reverse '(1 2 3 4 5 . #nil))
144 '(5 4 3 2 1))) ; Hmmm... is this OK, or
146 ; '(5 4 3 2 1 . #nil) ?
149 (equal? (reverse! (copy-tree '(1 2 3 4 5 . #nil)))
150 '(5 4 3 2 1))) ; Ditto.
153 (eqv? (list-ref '(0 1 2 3 4 . #nil) 4) 4))
155 (pass-if-exception "list-ref"
156 exception:out-of-range
157 (eqv? (list-ref '(0 1 2 3 4 . #nil) 6) 6))
160 (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
162 (= (list-ref l 4) 44)))
164 (pass-if-exception "list-set!"
165 exception:out-of-range
166 (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
168 (= (list-ref l 6) 44)))
170 (pass-if "list-cdr-set!"
171 (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
173 (list-cdr-set! l 4 44)
174 (equal? l '(0 1 2 3 4 . 44)))
176 (list-cdr-set! l 3 '(new . #nil))
177 (equal? l '(0 1 2 3 new . #nil))))))
179 (pass-if-exception "list-cdr-set!"
180 exception:out-of-range
181 (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
182 (list-cdr-set! l 6 44)))
185 (equal? (memq 'c '(a b c d . #nil)) '(c d . #nil)))
188 (equal? (memv 'c '(a b c d . #nil)) '(c d . #nil)))
191 (equal? (member "c" '("a" "b" "c" "d" . #nil)) '("c" "d" . #nil)))
193 (pass-if "list->vector"
194 (equal? '#(1 2 3) (list->vector '(1 2 3 . #nil))))
196 (pass-if "list->vector"
197 (equal? '#(1 2 3) (list->vector '(1 2 3 . #nil))))
199 (pass-if "list->weak-vector"
200 (equal? (weak-vector 1 2 3) (list->weak-vector '(1 2 3 . #nil))))
203 (and (sorted? '(1 2 3 . #nil) <)
204 (not (sorted? '(1 6 3 . #nil) <))))
207 (equal? (merge '(1 4 7 10)
208 (merge '(2 5 8 11 . #nil)
212 '(1 2 3 4 5 6 7 8 9 10 11 12 . #nil)))
215 (equal? (merge! (copy-tree '(1 4 7 10))
216 (merge! (copy-tree '(2 5 8 11 . #nil))
217 (copy-tree '(3 6 9 12 . #nil))
220 '(1 2 3 4 5 6 7 8 9 10 11 12 . #nil)))
223 (equal? (sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8)))
225 (pass-if "stable-sort"
226 (equal? (stable-sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8)))
229 (equal? (sort! (copy-tree '(1 5 3 8 4 . #nil)) <)
232 (pass-if "stable-sort!"
233 (equal? (stable-sort! (copy-tree '(1 5 3 8 4 . #nil)) <)
237 (with-test-prefix "value preservation"
240 (eq? (car (cons #nil 'a)) #nil))
243 (eq? (cdr (cons 'a #nil)) #nil))
245 (pass-if "vector-ref"
246 (eq? (vector-ref (vector #nil) 0) #nil))))
253 (with-test-prefix "elisp"
255 (define (elisp-pass-if expr expected)
256 (pass-if (with-output-to-string
259 (let ((calc (with-output-to-string
261 (write (compile expr #:from 'elisp #:to 'value))))))
262 (string=? calc expected))))
264 (define (elisp-pass-if/maybe-error key expr expected)
265 (pass-if (with-output-to-string (lambda () (write expr)))
269 (with-output-to-string
270 (lambda () (write (eval-elisp expr)))))
272 (format (current-error-port)
273 "warning: caught ~a: ~a\n" k args)
274 (throw 'unresolved)))
277 (elisp-pass-if '(and #f) "#f")
278 (elisp-pass-if '(and #t) "#t")
279 (elisp-pass-if '(and nil) "#nil")
280 (elisp-pass-if '(and t) "#t")
281 (elisp-pass-if '(and) "#t")
282 (elisp-pass-if '(cond (nil t) (t 3)) "3")
283 (elisp-pass-if '(cond (nil t) (t)) "#t")
284 (elisp-pass-if '(cond (nil)) "#nil")
285 (elisp-pass-if '(cond) "#nil")
286 (elisp-pass-if '(if #f 'a 'b) "b")
287 (elisp-pass-if '(if #t 'a 'b) "a")
288 (elisp-pass-if '(if nil 'a 'b) "b")
289 (elisp-pass-if '(if nil 1 2 3 4) "4")
290 (elisp-pass-if '(if nil 1 2) "2")
291 (elisp-pass-if '(if nil 1) "#nil")
292 (elisp-pass-if '(if t 1 2) "1")
293 (elisp-pass-if '(if t 1) "1")
294 (elisp-pass-if '(let (a) a) "#nil")
295 (elisp-pass-if '(let* (a) a) "#nil")
296 (elisp-pass-if '(let* ((a 1) (b (* a 2))) b) "2")
297 (elisp-pass-if '(null nil) "#t")
298 (elisp-pass-if '(or 1 2 3) "1")
299 (elisp-pass-if '(or nil t nil) "#t")
300 (elisp-pass-if '(or nil) "#nil")
301 (elisp-pass-if '(or t nil t) "#t")
302 (elisp-pass-if '(or t) "#t")
303 (elisp-pass-if '(or) "#nil")
304 (elisp-pass-if '(prog1 1 2 3) "1")
305 (elisp-pass-if '(prog2 1 2 3) "2")
306 (elisp-pass-if '(progn 1 2 3) "3")
307 (elisp-pass-if '(while nil 1) "#nil")
309 (elisp-pass-if '(defun testf (x y &optional o &rest r) (list x y o r)) "testf")
310 (elisp-pass-if '(testf 1 2) "(1 2 #nil #nil)")
311 (elisp-pass-if '(testf 1 2 3 4 5 56) "(1 2 3 (4 5 56))")
312 ;; NB `lambda' in Emacs is self-quoting, but that's only after
313 ;; loading the macro definition of lambda in subr.el.
314 (elisp-pass-if '(funcall (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 4) "(1 2 3 (4))")
316 (elisp-pass-if '(apply (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 nil)
319 (elisp-pass-if '(setq x 3) "3")
320 (elisp-pass-if '(defvar x 4) "x")
321 (elisp-pass-if 'x "3")
323 ;; wingo 9 april 2010: the following 10 tests are currently failing. the if &
324 ;; null tests are good, but I think some of the memq tests are bogus, given
325 ;; our current thoughts on equalty and nil; though they should succeed with
326 ;; memv and member in the elisp case. Also I think the function test is bogus.
328 (elisp-pass-if '(if '() 'a 'b) "b")
330 (elisp-pass-if '(null '#f) "#t")
332 (elisp-pass-if '(null '()) "#t")
334 (elisp-pass-if '(null 'nil) "#t")
336 (elisp-pass-if '(memq '() '(())) "(())")
338 (elisp-pass-if '(memq '() '(nil)) "(#nil)")
340 (elisp-pass-if '(memq '() '(t)) "#nil")
342 (elisp-pass-if '(memq nil '(())) "(())")
344 (elisp-pass-if '(memq nil '(nil)) "(#nil)")
346 (elisp-pass-if '(memq nil (list nil)) "(#nil)")
348 (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))")
352 ;;; elisp.test ends here