Add call-with-stack-overflow-handler tests
[bpt/guile.git] / test-suite / tests / elisp.test
1 ;;;; elisp.test --- tests guile's elisp support -*- scheme -*-
2 ;;;; Copyright (C) 2002, 2003, 2006, 2009, 2010 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
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))
22
23 (with-test-prefix "scheme"
24
25 (with-test-prefix "nil value is a boolean"
26
27 (pass-if "boolean?"
28 (boolean? #nil)))
29
30
31 (with-test-prefix "nil value is false"
32
33 (pass-if "not"
34 (eq? (not #nil) #t))
35
36 (pass-if "if"
37 (if #nil #f #t))
38
39 (pass-if "and"
40 (eq? (and #nil #t) #f))
41
42 (pass-if "or"
43 (eq? (or #nil #f) #f))
44
45 (pass-if "cond"
46 (cond (#nil #f) (else #t)))
47
48 (pass-if "do"
49 (call-with-current-continuation
50 (lambda (exit)
51 (do ((i 0 (+ i 1)))
52 (#nil (exit #f))
53 (if (> i 10)
54 (exit #t)))))))
55
56
57 (with-test-prefix "nil value as an empty list"
58
59 (pass-if "list?"
60 (list? #nil))
61
62 (pass-if "null?"
63 (null? #nil))
64
65 (pass-if "sort"
66 (eq? (sort #nil <) #nil)))
67
68
69 (with-test-prefix "lists formed using nil value"
70
71 (pass-if "list?"
72 (list? (cons 'a #nil)))
73
74 (pass-if "length of #nil"
75 (= (length #nil) 0))
76
77 (pass-if "length"
78 (= (length (cons 'a (cons 'b (cons 'c #nil)))) 3))
79
80 (pass-if "length (with backquoted list)"
81 (= (length '(a b c . #nil)) 3))
82
83 (pass-if "write (#nil)"
84 (string=? (with-output-to-string
85 (lambda () (write #nil)))
86 "#nil")) ; Hmmm... should be "()" ?
87
88 (pass-if "display (#nil)"
89 (string=? (with-output-to-string
90 (lambda () (display #nil)))
91 "#nil")) ; Ditto.
92
93 (pass-if "write (list)"
94 (string=? (with-output-to-string
95 (lambda () (write (cons 'a #nil))))
96 "(a)"))
97
98 (pass-if "display (list)"
99 (string=? (with-output-to-string
100 (lambda () (display (cons 'a #nil))))
101 "(a)"))
102
103 (pass-if "assq"
104 (and (equal? (assq 1 '((1 one) (2 two) . #nil))
105 '(1 one))
106 (equal? (assq 3 '((1 one) (2 two) . #nil))
107 #f)))
108
109 (pass-if "assv"
110 (and (equal? (assv 1 '((1 one) (2 two) . #nil))
111 '(1 one))
112 (equal? (assv 3 '((1 one) (2 two) . #nil))
113 #f)))
114
115 (pass-if "assoc"
116 (and (equal? (assoc 1 '((1 one) (2 two) . #nil))
117 '(1 one))
118 (equal? (assoc 3 '((1 one) (2 two) . #nil))
119 #f)))
120
121 (pass-if "with-fluids*"
122 (let ((f (make-fluid))
123 (g (make-fluid)))
124 (with-fluids* (cons f (cons g #nil))
125 '(3 4)
126 (lambda ()
127 (and (eqv? (fluid-ref f) 3)
128 (eqv? (fluid-ref g) 4))))))
129
130 (pass-if "append!"
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))))
137
138 (pass-if "last-pair"
139 (equal? (last-pair '(1 2 3 4 5 . #nil))
140 (cons 5 #nil)))
141
142 (pass-if "reverse"
143 (equal? (reverse '(1 2 3 4 5 . #nil))
144 '(5 4 3 2 1))) ; Hmmm... is this OK, or
145 ; should it be
146 ; '(5 4 3 2 1 . #nil) ?
147
148 (pass-if "reverse!"
149 (equal? (reverse! (copy-tree '(1 2 3 4 5 . #nil)))
150 '(5 4 3 2 1))) ; Ditto.
151
152 (pass-if "list-ref"
153 (eqv? (list-ref '(0 1 2 3 4 . #nil) 4) 4))
154
155 (pass-if-exception "list-ref"
156 exception:out-of-range
157 (eqv? (list-ref '(0 1 2 3 4 . #nil) 6) 6))
158
159 (pass-if "list-set!"
160 (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
161 (list-set! l 4 44)
162 (= (list-ref l 4) 44)))
163
164 (pass-if-exception "list-set!"
165 exception:out-of-range
166 (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
167 (list-set! l 6 44)
168 (= (list-ref l 6) 44)))
169
170 (pass-if "list-cdr-set!"
171 (let ((l (copy-tree '(0 1 2 3 4 . #nil))))
172 (and (begin
173 (list-cdr-set! l 4 44)
174 (equal? l '(0 1 2 3 4 . 44)))
175 (begin
176 (list-cdr-set! l 3 '(new . #nil))
177 (equal? l '(0 1 2 3 new . #nil))))))
178
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)))
183
184 (pass-if "memq"
185 (equal? (memq 'c '(a b c d . #nil)) '(c d . #nil)))
186
187 (pass-if "memv"
188 (equal? (memv 'c '(a b c d . #nil)) '(c d . #nil)))
189
190 (pass-if "member"
191 (equal? (member "c" '("a" "b" "c" "d" . #nil)) '("c" "d" . #nil)))
192
193 (pass-if "list->vector"
194 (equal? '#(1 2 3) (list->vector '(1 2 3 . #nil))))
195
196 (pass-if "list->vector"
197 (equal? '#(1 2 3) (list->vector '(1 2 3 . #nil))))
198
199 (pass-if "list->weak-vector"
200 (equal? (weak-vector 1 2 3) (list->weak-vector '(1 2 3 . #nil))))
201
202 (pass-if "sorted?"
203 (and (sorted? '(1 2 3 . #nil) <)
204 (not (sorted? '(1 6 3 . #nil) <))))
205
206 (pass-if "merge"
207 (equal? (merge '(1 4 7 10)
208 (merge '(2 5 8 11 . #nil)
209 '(3 6 9 12 . #nil)
210 <)
211 <)
212 '(1 2 3 4 5 6 7 8 9 10 11 12 . #nil)))
213
214 (pass-if "merge!"
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))
218 <)
219 <)
220 '(1 2 3 4 5 6 7 8 9 10 11 12 . #nil)))
221
222 (pass-if "sort"
223 (equal? (sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8)))
224
225 (pass-if "stable-sort"
226 (equal? (stable-sort '(1 5 3 8 4 . #nil) <) '(1 3 4 5 8)))
227
228 (pass-if "sort!"
229 (equal? (sort! (copy-tree '(1 5 3 8 4 . #nil)) <)
230 '(1 3 4 5 8)))
231
232 (pass-if "stable-sort!"
233 (equal? (stable-sort! (copy-tree '(1 5 3 8 4 . #nil)) <)
234 '(1 3 4 5 8))))
235
236
237 (with-test-prefix "value preservation"
238
239 (pass-if "car"
240 (eq? (car (cons #nil 'a)) #nil))
241
242 (pass-if "cdr"
243 (eq? (cdr (cons 'a #nil)) #nil))
244
245 (pass-if "vector-ref"
246 (eq? (vector-ref (vector #nil) 0) #nil))))
247
248
249 ;;;
250 ;;; elisp
251 ;;;
252
253 (with-test-prefix "elisp"
254
255 (define (elisp-pass-if expr expected)
256 (pass-if (with-output-to-string
257 (lambda ()
258 (write expr)))
259 (let ((calc (with-output-to-string
260 (lambda ()
261 (write (compile expr #:from 'elisp #:to 'value))))))
262 (string=? calc expected))))
263
264 (define (elisp-pass-if/maybe-error key expr expected)
265 (pass-if (with-output-to-string (lambda () (write expr)))
266 (string=?
267 (catch key
268 (lambda ()
269 (with-output-to-string
270 (lambda () (write (eval-elisp expr)))))
271 (lambda (k . args)
272 (format (current-error-port)
273 "warning: caught ~a: ~a\n" k args)
274 (throw 'unresolved)))
275 expected)))
276
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")
308
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))")
315
316 (elisp-pass-if '(apply (lambda (x y &optional o &rest r) (list x y o r)) 1 2 3 nil)
317 "(1 2 3 #nil)")
318
319 (elisp-pass-if '(setq x 3) "3")
320 (elisp-pass-if '(defvar x 4) "x")
321 (elisp-pass-if 'x "3")
322
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.
327 #;
328 (elisp-pass-if '(if '() 'a 'b) "b")
329 #;
330 (elisp-pass-if '(null '#f) "#t")
331 #;
332 (elisp-pass-if '(null '()) "#t")
333 #;
334 (elisp-pass-if '(null 'nil) "#t")
335 #;
336 (elisp-pass-if '(memq '() '(())) "(())")
337 #;
338 (elisp-pass-if '(memq '() '(nil)) "(#nil)")
339 #;
340 (elisp-pass-if '(memq '() '(t)) "#nil")
341 #;
342 (elisp-pass-if '(memq nil '(())) "(())")
343 #;
344 (elisp-pass-if '(memq nil '(nil)) "(#nil)")
345 #;
346 (elisp-pass-if '(memq nil (list nil)) "(#nil)")
347 #;
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))")
349 )
350
351
352 ;;; elisp.test ends here