1 ;;;; elisp.test --- tests guile's elisp support -*- scheme -*-
2 ;;;; Copyright (C) 2002, 2003, 2006 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 2.1 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 (ice-9 weak-vector))
22 (define *old-stack-level* (and=> (memq 'stack (debug-options)) cadr))
24 (debug-set! stack (* 2 *old-stack-level*)))
32 (with-test-prefix "scheme"
34 (with-test-prefix "nil value is a boolean"
41 (with-test-prefix "nil value is false"
50 (eq? (and %nil #t) #f))
53 (eq? (or %nil #f) #f))
56 (cond (%nil #f) (else #t)))
59 (call-with-current-continuation
68 (with-test-prefix "nil value as an empty list"
77 (eq? (sort %nil <) %nil))
81 (with-test-prefix "lists formed using nil value"
84 (list? (cons 'a %nil)))
86 (pass-if "length of %nil"
90 (= (length (cons 'a (cons 'b (cons 'c %nil)))) 3))
92 (pass-if "length (with backquoted list)"
93 (= (length `(a b c . ,%nil)) 3))
95 (pass-if "write (%nil)"
96 (string=? (with-output-to-string
97 (lambda () (write %nil)))
98 "#nil")) ; Hmmm... should be "()" ?
100 (pass-if "display (%nil)"
101 (string=? (with-output-to-string
102 (lambda () (display %nil)))
105 (pass-if "write (list)"
106 (string=? (with-output-to-string
107 (lambda () (write (cons 'a %nil))))
110 (pass-if "display (list)"
111 (string=? (with-output-to-string
112 (lambda () (display (cons 'a %nil))))
116 (and (equal? (assq 1 `((1 one) (2 two) . ,%nil))
118 (equal? (assq 3 `((1 one) (2 two) . ,%nil))
122 (and (equal? (assv 1 `((1 one) (2 two) . ,%nil))
124 (equal? (assv 3 `((1 one) (2 two) . ,%nil))
128 (and (equal? (assoc 1 `((1 one) (2 two) . ,%nil))
130 (equal? (assoc 3 `((1 one) (2 two) . ,%nil))
133 (pass-if "with-fluids*"
134 (let ((f (make-fluid))
136 (with-fluids* (cons f (cons g %nil))
139 (and (eq? (fluid-ref f) 3)
140 (eq? (fluid-ref g) 4))))))
143 (let ((a (copy-tree '(1 2 3)))
144 (b (copy-tree `(4 5 6 . ,%nil)))
145 (c (copy-tree '(7 8 9)))
146 (d (copy-tree `(a b c . ,%nil))))
147 (equal? (append! a b c d)
148 `(1 2 3 4 5 6 7 8 9 a b c . ,%nil))))
151 (equal? (last-pair `(1 2 3 4 5 . ,%nil))
155 (equal? (reverse `(1 2 3 4 5 . ,%nil))
156 '(5 4 3 2 1))) ; Hmmm... is this OK, or
158 ; `(5 4 3 2 1 . ,%nil) ?
161 (equal? (reverse! (copy-tree `(1 2 3 4 5 . ,%nil)))
162 '(5 4 3 2 1))) ; Ditto.
165 (eq? (list-ref `(0 1 2 3 4 . ,%nil) 4) 4))
167 (pass-if-exception "list-ref"
168 exception:out-of-range
169 (eq? (list-ref `(0 1 2 3 4 . ,%nil) 6) 6))
172 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
174 (= (list-ref l 4) 44)))
176 (pass-if-exception "list-set!"
177 exception:out-of-range
178 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
180 (= (list-ref l 6) 44)))
182 (pass-if "list-cdr-set!"
183 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
185 (list-cdr-set! l 4 44)
186 (equal? l '(0 1 2 3 4 . 44)))
188 (list-cdr-set! l 3 `(new . ,%nil))
189 (equal? l `(0 1 2 3 new . ,%nil))))))
191 (pass-if-exception "list-cdr-set!"
192 exception:out-of-range
193 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
194 (list-cdr-set! l 6 44)))
197 (equal? (memq 'c `(a b c d . ,%nil)) `(c d . ,%nil)))
200 (equal? (memv 'c `(a b c d . ,%nil)) `(c d . ,%nil)))
203 (equal? (member "c" `("a" "b" "c" "d" . ,%nil)) `("c" "d" . ,%nil)))
205 (pass-if "list->vector"
206 (equal? '#(1 2 3) (list->vector `(1 2 3 . ,%nil))))
208 (pass-if "list->vector"
209 (equal? '#(1 2 3) (list->vector `(1 2 3 . ,%nil))))
211 (pass-if "list->weak-vector"
212 (equal? (weak-vector 1 2 3) (list->weak-vector `(1 2 3 . ,%nil))))
215 (and (sorted? `(1 2 3 . ,%nil) <)
216 (not (sorted? `(1 6 3 . ,%nil) <))))
219 (equal? (merge '(1 4 7 10)
220 (merge `(2 5 8 11 . ,%nil)
224 `(1 2 3 4 5 6 7 8 9 10 11 12 . ,%nil)))
227 (equal? (merge! (copy-tree '(1 4 7 10))
228 (merge! (copy-tree `(2 5 8 11 . ,%nil))
229 (copy-tree `(3 6 9 12 . ,%nil))
232 `(1 2 3 4 5 6 7 8 9 10 11 12 . ,%nil)))
235 (equal? (sort `(1 5 3 8 4 . ,%nil) <) '(1 3 4 5 8)))
237 (pass-if "stable-sort"
238 (equal? (stable-sort `(1 5 3 8 4 . ,%nil) <) '(1 3 4 5 8)))
241 (equal? (sort! (copy-tree `(1 5 3 8 4 . ,%nil)) <)
244 (pass-if "stable-sort!"
245 (equal? (stable-sort! (copy-tree `(1 5 3 8 4 . ,%nil)) <)
250 (with-test-prefix "value preservation"
253 (eq? (car (cons %nil 'a)) %nil))
256 (eq? (cdr (cons 'a %nil)) %nil))
258 (pass-if "vector-ref"
259 (eq? (vector-ref (vector %nil) 0) %nil))
266 (use-modules (lang elisp interface)))
270 (with-test-prefix "elisp"
272 (define (elisp-pass-if expr expected)
273 (pass-if (with-output-to-string
276 (let ((calc (with-output-to-string
278 (write (eval-elisp expr))))))
279 (string=? calc expected))))
281 (define (elisp-pass-if/maybe-error key expr expected)
282 (pass-if (with-output-to-string (lambda () (write expr)))
286 (with-output-to-string
287 (lambda () (write (eval-elisp expr)))))
289 (format (current-error-port)
290 "warning: caught ~a: ~a\n" k args)
291 (throw 'unresolved)))
294 (elisp-pass-if '(and #f) "#f")
295 (elisp-pass-if '(and #t) "#t")
296 (elisp-pass-if '(and nil) "#nil")
297 (elisp-pass-if '(and t) "#t")
298 (elisp-pass-if '(and) "#t")
299 (elisp-pass-if '(cond (nil t) (t 3)) "3")
300 (elisp-pass-if '(cond (nil t) (t)) "#t")
301 (elisp-pass-if '(cond (nil)) "#nil")
302 (elisp-pass-if '(cond) "#nil")
303 (elisp-pass-if '(if #f 'a 'b) "b")
304 (elisp-pass-if '(if #t 'a 'b) "a")
305 (elisp-pass-if '(if '() 'a 'b) "b")
306 (elisp-pass-if '(if nil 'a 'b) "b")
307 (elisp-pass-if '(if nil 1 2 3 4) "4")
308 (elisp-pass-if '(if nil 1 2) "2")
309 (elisp-pass-if '(if nil 1) "#nil")
310 (elisp-pass-if '(if t 1 2) "1")
311 (elisp-pass-if '(if t 1) "1")
312 (elisp-pass-if '(let (a) a) "#nil")
313 (elisp-pass-if '(let* (a) a) "#nil")
314 (elisp-pass-if '(let* ((a 1) (b (* a 2))) b) "2")
315 (elisp-pass-if '(memq '() '(())) "(())")
316 (elisp-pass-if '(memq '() '(nil)) "(#nil)")
317 (elisp-pass-if '(memq '() '(t)) "#nil")
318 (elisp-pass-if '(memq nil '(())) "(())")
319 (elisp-pass-if '(memq nil '(nil)) "(#nil)")
320 (elisp-pass-if '(memq nil (list nil)) "(#nil)")
321 (elisp-pass-if '(null '#f) "#t")
322 (elisp-pass-if '(null '()) "#t")
323 (elisp-pass-if '(null 'nil) "#t")
324 (elisp-pass-if '(null nil) "#t")
325 (elisp-pass-if '(or 1 2 3) "1")
326 (elisp-pass-if '(or nil t nil) "#t")
327 (elisp-pass-if '(or nil) "#nil")
328 (elisp-pass-if '(or t nil t) "#t")
329 (elisp-pass-if '(or t) "#t")
330 (elisp-pass-if '(or) "#nil")
331 (elisp-pass-if '(prog1 1 2 3) "1")
332 (elisp-pass-if '(prog2 1 2 3) "2")
333 (elisp-pass-if '(progn 1 2 3) "3")
334 (elisp-pass-if '(while nil 1) "#nil")
336 (elisp-pass-if '(defun testf (x y &optional o &rest r) (list x y o r)) "testf")
337 (elisp-pass-if '(testf 1 2) "(1 2 #nil #nil)")
338 (elisp-pass-if '(testf 1 2 3 4 5 56) "(1 2 3 (4 5 56))")
339 ;; NB `lambda' in Emacs is self-quoting, but that's only after
340 ;; loading the macro definition of lambda in subr.el.
341 (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))")
342 (elisp-pass-if '(funcall (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 4) "(1 2 3 (4))")
344 (elisp-pass-if '(apply (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 nil)
347 (elisp-pass-if '(setq x 3) "3")
348 (elisp-pass-if '(defvar x 4) "x")
349 (elisp-pass-if 'x "3")
353 (debug-set! stack *old-stack-level*)
355 ;;; elisp.test ends here