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*)))
26 (define *old-%load-should-autocompile* %load-should-autocompile)
27 (set! %load-should-autocompile #f)
35 (with-test-prefix "scheme"
37 (with-test-prefix "nil value is a boolean"
44 (with-test-prefix "nil value is false"
53 (eq? (and %nil #t) #f))
56 (eq? (or %nil #f) #f))
59 (cond (%nil #f) (else #t)))
62 (call-with-current-continuation
71 (with-test-prefix "nil value as an empty list"
80 (eq? (sort %nil <) %nil))
84 (with-test-prefix "lists formed using nil value"
87 (list? (cons 'a %nil)))
89 (pass-if "length of %nil"
93 (= (length (cons 'a (cons 'b (cons 'c %nil)))) 3))
95 (pass-if "length (with backquoted list)"
96 (= (length `(a b c . ,%nil)) 3))
98 (pass-if "write (%nil)"
99 (string=? (with-output-to-string
100 (lambda () (write %nil)))
101 "#nil")) ; Hmmm... should be "()" ?
103 (pass-if "display (%nil)"
104 (string=? (with-output-to-string
105 (lambda () (display %nil)))
108 (pass-if "write (list)"
109 (string=? (with-output-to-string
110 (lambda () (write (cons 'a %nil))))
113 (pass-if "display (list)"
114 (string=? (with-output-to-string
115 (lambda () (display (cons 'a %nil))))
119 (and (equal? (assq 1 `((1 one) (2 two) . ,%nil))
121 (equal? (assq 3 `((1 one) (2 two) . ,%nil))
125 (and (equal? (assv 1 `((1 one) (2 two) . ,%nil))
127 (equal? (assv 3 `((1 one) (2 two) . ,%nil))
131 (and (equal? (assoc 1 `((1 one) (2 two) . ,%nil))
133 (equal? (assoc 3 `((1 one) (2 two) . ,%nil))
136 (pass-if "with-fluids*"
137 (let ((f (make-fluid))
139 (with-fluids* (cons f (cons g %nil))
142 (and (eq? (fluid-ref f) 3)
143 (eq? (fluid-ref g) 4))))))
146 (let ((a (copy-tree '(1 2 3)))
147 (b (copy-tree `(4 5 6 . ,%nil)))
148 (c (copy-tree '(7 8 9)))
149 (d (copy-tree `(a b c . ,%nil))))
150 (equal? (append! a b c d)
151 `(1 2 3 4 5 6 7 8 9 a b c . ,%nil))))
154 (equal? (last-pair `(1 2 3 4 5 . ,%nil))
158 (equal? (reverse `(1 2 3 4 5 . ,%nil))
159 '(5 4 3 2 1))) ; Hmmm... is this OK, or
161 ; `(5 4 3 2 1 . ,%nil) ?
164 (equal? (reverse! (copy-tree `(1 2 3 4 5 . ,%nil)))
165 '(5 4 3 2 1))) ; Ditto.
168 (eq? (list-ref `(0 1 2 3 4 . ,%nil) 4) 4))
170 (pass-if-exception "list-ref"
171 exception:out-of-range
172 (eq? (list-ref `(0 1 2 3 4 . ,%nil) 6) 6))
175 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
177 (= (list-ref l 4) 44)))
179 (pass-if-exception "list-set!"
180 exception:out-of-range
181 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
183 (= (list-ref l 6) 44)))
185 (pass-if "list-cdr-set!"
186 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
188 (list-cdr-set! l 4 44)
189 (equal? l '(0 1 2 3 4 . 44)))
191 (list-cdr-set! l 3 `(new . ,%nil))
192 (equal? l `(0 1 2 3 new . ,%nil))))))
194 (pass-if-exception "list-cdr-set!"
195 exception:out-of-range
196 (let ((l (copy-tree `(0 1 2 3 4 . ,%nil))))
197 (list-cdr-set! l 6 44)))
200 (equal? (memq 'c `(a b c d . ,%nil)) `(c d . ,%nil)))
203 (equal? (memv 'c `(a b c d . ,%nil)) `(c d . ,%nil)))
206 (equal? (member "c" `("a" "b" "c" "d" . ,%nil)) `("c" "d" . ,%nil)))
208 (pass-if "list->vector"
209 (equal? '#(1 2 3) (list->vector `(1 2 3 . ,%nil))))
211 (pass-if "list->vector"
212 (equal? '#(1 2 3) (list->vector `(1 2 3 . ,%nil))))
214 (pass-if "list->weak-vector"
215 (equal? (weak-vector 1 2 3) (list->weak-vector `(1 2 3 . ,%nil))))
218 (and (sorted? `(1 2 3 . ,%nil) <)
219 (not (sorted? `(1 6 3 . ,%nil) <))))
222 (equal? (merge '(1 4 7 10)
223 (merge `(2 5 8 11 . ,%nil)
227 `(1 2 3 4 5 6 7 8 9 10 11 12 . ,%nil)))
230 (equal? (merge! (copy-tree '(1 4 7 10))
231 (merge! (copy-tree `(2 5 8 11 . ,%nil))
232 (copy-tree `(3 6 9 12 . ,%nil))
235 `(1 2 3 4 5 6 7 8 9 10 11 12 . ,%nil)))
238 (equal? (sort `(1 5 3 8 4 . ,%nil) <) '(1 3 4 5 8)))
240 (pass-if "stable-sort"
241 (equal? (stable-sort `(1 5 3 8 4 . ,%nil) <) '(1 3 4 5 8)))
244 (equal? (sort! (copy-tree `(1 5 3 8 4 . ,%nil)) <)
247 (pass-if "stable-sort!"
248 (equal? (stable-sort! (copy-tree `(1 5 3 8 4 . ,%nil)) <)
253 (with-test-prefix "value preservation"
256 (eq? (car (cons %nil 'a)) %nil))
259 (eq? (cdr (cons 'a %nil)) %nil))
261 (pass-if "vector-ref"
262 (eq? (vector-ref (vector %nil) 0) %nil))
269 (use-modules (lang elisp interface)))
273 (with-test-prefix "elisp"
275 (define (elisp-pass-if expr expected)
276 (pass-if (with-output-to-string
279 (let ((calc (with-output-to-string
281 (write (eval-elisp expr))))))
282 (string=? calc expected))))
284 (define (elisp-pass-if/maybe-error key expr expected)
285 (pass-if (with-output-to-string (lambda () (write expr)))
289 (with-output-to-string
290 (lambda () (write (eval-elisp expr)))))
292 (format (current-error-port)
293 "warning: caught ~a: ~a\n" k args)
294 (throw 'unresolved)))
297 (elisp-pass-if '(and #f) "#f")
298 (elisp-pass-if '(and #t) "#t")
299 (elisp-pass-if '(and nil) "#nil")
300 (elisp-pass-if '(and t) "#t")
301 (elisp-pass-if '(and) "#t")
302 (elisp-pass-if '(cond (nil t) (t 3)) "3")
303 (elisp-pass-if '(cond (nil t) (t)) "#t")
304 (elisp-pass-if '(cond (nil)) "#nil")
305 (elisp-pass-if '(cond) "#nil")
306 (elisp-pass-if '(if #f 'a 'b) "b")
307 (elisp-pass-if '(if #t 'a 'b) "a")
308 (elisp-pass-if '(if '() 'a 'b) "b")
309 (elisp-pass-if '(if nil 'a 'b) "b")
310 (elisp-pass-if '(if nil 1 2 3 4) "4")
311 (elisp-pass-if '(if nil 1 2) "2")
312 (elisp-pass-if '(if nil 1) "#nil")
313 (elisp-pass-if '(if t 1 2) "1")
314 (elisp-pass-if '(if t 1) "1")
315 (elisp-pass-if '(let (a) a) "#nil")
316 (elisp-pass-if '(let* (a) a) "#nil")
317 (elisp-pass-if '(let* ((a 1) (b (* a 2))) b) "2")
318 (elisp-pass-if '(memq '() '(())) "(())")
319 (elisp-pass-if '(memq '() '(nil)) "(#nil)")
320 (elisp-pass-if '(memq '() '(t)) "#nil")
321 (elisp-pass-if '(memq nil '(())) "(())")
322 (elisp-pass-if '(memq nil '(nil)) "(#nil)")
323 (elisp-pass-if '(memq nil (list nil)) "(#nil)")
324 (elisp-pass-if '(null '#f) "#t")
325 (elisp-pass-if '(null '()) "#t")
326 (elisp-pass-if '(null 'nil) "#t")
327 (elisp-pass-if '(null nil) "#t")
328 (elisp-pass-if '(or 1 2 3) "1")
329 (elisp-pass-if '(or nil t nil) "#t")
330 (elisp-pass-if '(or nil) "#nil")
331 (elisp-pass-if '(or t nil t) "#t")
332 (elisp-pass-if '(or t) "#t")
333 (elisp-pass-if '(or) "#nil")
334 (elisp-pass-if '(prog1 1 2 3) "1")
335 (elisp-pass-if '(prog2 1 2 3) "2")
336 (elisp-pass-if '(progn 1 2 3) "3")
337 (elisp-pass-if '(while nil 1) "#nil")
339 (elisp-pass-if '(defun testf (x y &optional o &rest r) (list x y o r)) "testf")
340 (elisp-pass-if '(testf 1 2) "(1 2 #nil #nil)")
341 (elisp-pass-if '(testf 1 2 3 4 5 56) "(1 2 3 (4 5 56))")
342 ;; NB `lambda' in Emacs is self-quoting, but that's only after
343 ;; loading the macro definition of lambda in subr.el.
344 (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))")
345 (elisp-pass-if '(funcall (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 4) "(1 2 3 (4))")
347 (elisp-pass-if '(apply (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 nil)
350 (elisp-pass-if '(setq x 3) "3")
351 (elisp-pass-if '(defvar x 4) "x")
352 (elisp-pass-if 'x "3")
356 (set! %load-should-autocompile *old-%load-should-autocompile*)
357 (debug-set! stack *old-stack-level*)
359 ;;; elisp.test ends here