* Provide and use new convenience macros to test for exceptions.
[bpt/guile.git] / test-suite / tests / alist.test
1 ;;;; alist.test --- tests guile's alists -*- scheme -*-
2 ;;;; Copyright (C) 1999 Free Software Foundation, Inc.
3 ;;;;
4 ;;;; This program is free software; you can redistribute it and/or modify
5 ;;;; it under the terms of the GNU General Public License as published by
6 ;;;; the Free Software Foundation; either version 2, or (at your option)
7 ;;;; any later version.
8 ;;;;
9 ;;;; This program 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
12 ;;;; GNU General Public License for more details.
13 ;;;;
14 ;;;; You should have received a copy of the GNU General Public License
15 ;;;; along with this software; see the file COPYING. If not, write to
16 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 ;;;; Boston, MA 02111-1307 USA
18 ;;;;
19 ;;;; As a special exception, the Free Software Foundation gives permission
20 ;;;; for additional uses of the text contained in its release of GUILE.
21 ;;;;
22 ;;;; The exception is that, if you link the GUILE library with other files
23 ;;;; to produce an executable, this does not by itself cause the
24 ;;;; resulting executable to be covered by the GNU General Public License.
25 ;;;; Your use of that executable is in no way restricted on account of
26 ;;;; linking the GUILE library code into it.
27 ;;;;
28 ;;;; This exception does not however invalidate any other reasons why
29 ;;;; the executable file might be covered by the GNU General Public License.
30 ;;;;
31 ;;;; This exception applies only to the code released by the
32 ;;;; Free Software Foundation under the name GUILE. If you copy
33 ;;;; code from other Free Software Foundation releases into a copy of
34 ;;;; GUILE, as the General Public License permits, the exception does
35 ;;;; not apply to the code that you add in this way. To avoid misleading
36 ;;;; anyone as to the status of such modified files, you must delete
37 ;;;; this exception notice from them.
38 ;;;;
39 ;;;; If you write modifications of your own for GUILE, it is your choice
40 ;;;; whether to permit this exception to apply to your modifications.
41 ;;;; If you do not wish that, delete this exception notice.
42
43 (use-modules (test-suite lib))
44
45 ;;; (gbh) some of these are duplicated in r4rs. This is probably a bit
46 ;;; more thorough, though (maybe overkill? I need it, anyway).
47 ;;;
48 ;;;
49 ;;; Also: it will fail on the ass*-ref & remove functions.
50 ;;; Sloppy versions should be added with the current behaviour
51 ;;; (it's the only set of 'ref functions that won't cause an
52 ;;; error on an incorrect arg); they aren't actually used anywhere
53 ;;; so changing's not a big deal.
54
55 ;;; Misc
56
57 (define-macro (pass-if-not str form)
58 `(pass-if ,str (not ,form)))
59
60 (define (safe-assq-ref alist elt)
61 (let ((x (assq elt alist)))
62 (if x (cdr x) x)))
63
64 (define (safe-assv-ref alist elt)
65 (let ((x (assv elt alist)))
66 (if x (cdr x) x)))
67
68 (define (safe-assoc-ref alist elt)
69 (let ((x (assoc elt alist)))
70 (if x (cdr x) x)))
71
72 ;;; Creators, getters
73 (let ((a (acons 'a 'b (acons 'c 'd (acons 'e 'f ()))))
74 (b (acons "this" "is" (acons "a" "test" ())))
75 (deformed '(a b c d e f g)))
76 (pass-if "acons"
77 (and (equal? a '((a . b) (c . d) (e . f)))
78 (equal? b '(("this" . "is") ("a" . "test")))))
79 (pass-if "sloppy-assq"
80 (let ((x (sloppy-assq 'c a)))
81 (and (pair? x)
82 (eq? (car x) 'c)
83 (eq? (cdr x) 'd))))
84 (pass-if "sloppy-assq not"
85 (let ((x (sloppy-assq "this" b)))
86 (not x)))
87 (pass-if "sloppy-assv"
88 (let ((x (sloppy-assv 'c a)))
89 (and (pair? x)
90 (eq? (car x) 'c)
91 (eq? (cdr x) 'd))))
92 (pass-if "sloppy-assv not"
93 (let ((x (sloppy-assv "this" b)))
94 (not x)))
95 (pass-if "sloppy-assoc"
96 (let ((x (sloppy-assoc "this" b)))
97 (and (pair? x)
98 (string=? (cdr x) "is"))))
99 (pass-if "sloppy-assoc not"
100 (let ((x (sloppy-assoc "heehee" b)))
101 (not x)))
102 (pass-if "assq"
103 (let ((x (assq 'c a)))
104 (and (pair? x)
105 (eq? (car x) 'c)
106 (eq? (cdr x) 'd))))
107 (pass-if-exception "assq deformed"
108 exception:wrong-type-arg
109 (assq 'x deformed))
110 (pass-if-not "assq not" (assq 'r a))
111 (pass-if "assv"
112 (let ((x (assv 'a a)))
113 (and (pair? x)
114 (eq? (car x) 'a)
115 (eq? (cdr x) 'b))))
116 (pass-if-exception "assv deformed"
117 exception:wrong-type-arg
118 (assv 'x deformed))
119 (pass-if-not "assv not" (assq "this" b))
120
121 (pass-if "assoc"
122 (let ((x (assoc "this" b)))
123 (and (pair? x)
124 (string=? (car x) "this")
125 (string=? (cdr x) "is"))))
126 (pass-if-exception "assoc deformed"
127 exception:wrong-type-arg
128 (assoc 'x deformed))
129 (pass-if-not "assoc not" (assoc "this isn't" b)))
130
131
132 ;;; Refers
133 (let ((a '((foo bar) (baz quux)))
134 (b '(("one" 2 3) ("four" 5 6) ("seven" 8 9)))
135 (deformed '(thats a real sloppy assq you got there)))
136 (pass-if "assq-ref"
137 (let ((x (assq-ref a 'foo)))
138 (and (list? x)
139 (eq? (car x) 'bar))))
140
141 (pass-if-not "assq-ref not" (assq-ref b "one"))
142 (pass-if "assv-ref"
143 (let ((x (assv-ref a 'baz)))
144 (and (list? x)
145 (eq? (car x) 'quux))))
146
147 (pass-if-not "assv-ref not" (assv-ref b "one"))
148
149 (pass-if "assoc-ref"
150 (let ((x (assoc-ref b "one")))
151 (and (list? x)
152 (eq? (car x) 2)
153 (eq? (cadr x) 3))))
154
155
156 (pass-if-not "assoc-ref not" (assoc-ref a 'testing))
157
158 (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
159
160 (pass-if-exception "assv-ref deformed"
161 exception:wrong-type-arg
162 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
163 (assv-ref deformed 'sloppy))
164
165 (pass-if-exception "assoc-ref deformed"
166 exception:wrong-type-arg
167 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
168 (assoc-ref deformed 'sloppy))
169
170 (pass-if-exception "assq-ref deformed"
171 exception:wrong-type-arg
172 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
173 (assq-ref deformed 'sloppy))))
174
175
176 ;;; Setters
177 (let ((a '((another . silly) (alist . test-case)))
178 (b '(("this" "one" "has") ("strings" "!")))
179 (deformed '(canada is a cold nation)))
180 (pass-if "assq-set!"
181 (begin
182 (set! a (assq-set! a 'another 'stupid))
183 (let ((x (safe-assq-ref a 'another)))
184 (and x
185 (symbol? x) (eq? x 'stupid)))))
186
187 (pass-if "assq-set! add"
188 (begin
189 (set! a (assq-set! a 'fickle 'pickle))
190 (let ((x (safe-assq-ref a 'fickle)))
191 (and x (symbol? x)
192 (eq? x 'pickle)))))
193
194 (pass-if "assv-set!"
195 (begin
196 (set! a (assv-set! a 'another 'boring))
197 (let ((x (safe-assv-ref a 'another)))
198 (and x
199 (eq? x 'boring)))))
200 (pass-if "assv-set! add"
201 (begin
202 (set! a (assv-set! a 'whistle '(while you work)))
203 (let ((x (safe-assv-ref a 'whistle)))
204 (and x (equal? x '(while you work))))))
205
206 (pass-if "assoc-set!"
207 (begin
208 (set! b (assoc-set! b "this" "has"))
209 (let ((x (safe-assoc-ref b "this")))
210 (and x (string? x)
211 (string=? x "has")))))
212 (pass-if "assoc-set! add"
213 (begin
214 (set! b (assoc-set! b "flugle" "horn"))
215 (let ((x (safe-assoc-ref b "flugle")))
216 (and x (string? x)
217 (string=? x "horn")))))
218
219 (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
220
221 (pass-if-exception "assq-set! deformed"
222 exception:wrong-type-arg
223 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
224 (assq-set! deformed 'cold '(very cold)))
225
226 (pass-if-exception "assv-set! deformed"
227 exception:wrong-type-arg
228 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
229 (assv-set! deformed 'canada 'Canada))
230
231 (pass-if-exception "assoc-set! deformed"
232 exception:wrong-type-arg
233 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
234 (assoc-set! deformed 'canada '(Iceland hence the name)))))
235
236 ;;; Removers
237
238 (let ((a '((a b) (c d) (e boring)))
239 (b '(("what" . "else") ("could" . "I") ("say" . "here")))
240 (deformed 1))
241 (pass-if "assq-remove!"
242 (begin
243 (set! a (assq-remove! a 'a))
244 (equal? a '((c d) (e boring)))))
245 (pass-if "assv-remove!"
246 (begin
247 (set! a (assv-remove! a 'c))
248 (equal? a '((e boring)))))
249 (pass-if "assoc-remove!"
250 (begin
251 (set! b (assoc-remove! b "what"))
252 (equal? b '(("could" . "I") ("say" . "here")))))
253
254 (let* ((have-sloppy-assq-remove? (defined? 'sloppy-assq-remove)))
255
256 (pass-if-exception "assq-remove! deformed"
257 exception:wrong-type-arg
258 (if (not have-sloppy-assq-remove?) (throw 'unsupported))
259 (assq-remove! deformed 'puddle))
260
261 (pass-if-exception "assv-remove! deformed"
262 exception:wrong-type-arg
263 (if (not have-sloppy-assq-remove?) (throw 'unsupported))
264 (assv-remove! deformed 'splashing))
265
266 (pass-if-exception "assoc-remove! deformed"
267 exception:wrong-type-arg
268 (if (not have-sloppy-assq-remove?) (throw 'unsupported))
269 (assoc-remove! deformed 'fun))))