1 ;;;; alist.test --- tests guile's alists -*- scheme -*-
2 ;;;; Copyright (C) 1999, 2001, 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 (use-modules (test-suite lib))
20 ;;; (gbh) some of these are duplicated in r4rs. This is probably a bit
21 ;;; more thorough, though (maybe overkill? I need it, anyway).
24 ;;; Also: it will fail on the ass*-ref & remove functions.
25 ;;; Sloppy versions should be added with the current behaviour
26 ;;; (it's the only set of 'ref functions that won't cause an
27 ;;; error on an incorrect arg); they aren't actually used anywhere
28 ;;; so changing's not a big deal.
32 (define-macro (pass-if-not str form)
33 `(pass-if ,str (not ,form)))
35 (define (safe-assq-ref alist elt)
36 (let ((x (assq elt alist)))
39 (define (safe-assv-ref alist elt)
40 (let ((x (assv elt alist)))
43 (define (safe-assoc-ref alist elt)
44 (let ((x (assoc elt alist)))
48 (let ((a (acons 'a 'b (acons 'c 'd (acons 'e 'f '()))))
49 (b (acons "this" "is" (acons "a" "test" '())))
50 (deformed '(a b c d e f g)))
52 (and (equal? a '((a . b) (c . d) (e . f)))
53 (equal? b '(("this" . "is") ("a" . "test")))))
54 (pass-if "sloppy-assq"
55 (let ((x (sloppy-assq 'c a)))
59 (pass-if "sloppy-assq not"
60 (let ((x (sloppy-assq "this" b)))
62 (pass-if "sloppy-assv"
63 (let ((x (sloppy-assv 'c a)))
67 (pass-if "sloppy-assv not"
68 (let ((x (sloppy-assv "this" b)))
70 (pass-if "sloppy-assoc"
71 (let ((x (sloppy-assoc "this" b)))
73 (string=? (cdr x) "is"))))
74 (pass-if "sloppy-assoc not"
75 (let ((x (sloppy-assoc "heehee" b)))
78 (let ((x (assq 'c a)))
82 (pass-if-exception "assq deformed"
83 exception:wrong-type-arg
85 (pass-if-not "assq not" (assq 'r a))
87 (let ((x (assv 'a a)))
91 (pass-if-exception "assv deformed"
92 exception:wrong-type-arg
94 (pass-if-not "assv not" (assq "this" b))
97 (let ((x (assoc "this" b)))
99 (string=? (car x) "this")
100 (string=? (cdr x) "is"))))
101 (pass-if-exception "assoc deformed"
102 exception:wrong-type-arg
104 (pass-if-not "assoc not" (assoc "this isn't" b)))
108 (let ((a '((foo bar) (baz quux)))
109 (b '(("one" 2 3) ("four" 5 6) ("seven" 8 9)))
110 (deformed '(thats a real sloppy assq you got there)))
112 (let ((x (assq-ref a 'foo)))
114 (eq? (car x) 'bar))))
116 (pass-if-not "assq-ref not" (assq-ref b "one"))
118 (let ((x (assv-ref a 'baz)))
120 (eq? (car x) 'quux))))
122 (pass-if-not "assv-ref not" (assv-ref b "one"))
125 (let ((x (assoc-ref b "one")))
131 (pass-if-not "assoc-ref not" (assoc-ref a 'testing))
133 (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
135 (pass-if-exception "assv-ref deformed"
136 exception:wrong-type-arg
137 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
138 (assv-ref deformed 'sloppy))
140 (pass-if-exception "assoc-ref deformed"
141 exception:wrong-type-arg
142 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
143 (assoc-ref deformed 'sloppy))
145 (pass-if-exception "assq-ref deformed"
146 exception:wrong-type-arg
147 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
148 (assq-ref deformed 'sloppy))))
152 (let ((a '((another . silly) (alist . test-case)))
153 (b '(("this" "one" "has") ("strings" "!")))
154 (deformed '(canada is a cold nation)))
157 (set! a (assq-set! a 'another 'stupid))
158 (let ((x (safe-assq-ref a 'another)))
160 (symbol? x) (eq? x 'stupid)))))
162 (pass-if "assq-set! add"
164 (set! a (assq-set! a 'fickle 'pickle))
165 (let ((x (safe-assq-ref a 'fickle)))
171 (set! a (assv-set! a 'another 'boring))
172 (let ((x (safe-assv-ref a 'another)))
175 (pass-if "assv-set! add"
177 (set! a (assv-set! a 'whistle '(while you work)))
178 (let ((x (safe-assv-ref a 'whistle)))
179 (and x (equal? x '(while you work))))))
181 (pass-if "assoc-set!"
183 (set! b (assoc-set! b "this" "has"))
184 (let ((x (safe-assoc-ref b "this")))
186 (string=? x "has")))))
187 (pass-if "assoc-set! add"
189 (set! b (assoc-set! b "flugle" "horn"))
190 (let ((x (safe-assoc-ref b "flugle")))
192 (string=? x "horn")))))
194 (let* ((have-sloppy-assv-ref? (defined? 'sloppy-assv-ref)))
196 (pass-if-exception "assq-set! deformed"
197 exception:wrong-type-arg
198 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
199 (assq-set! deformed 'cold '(very cold)))
201 (pass-if-exception "assv-set! deformed"
202 exception:wrong-type-arg
203 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
204 (assv-set! deformed 'canada 'Canada))
206 (pass-if-exception "assoc-set! deformed"
207 exception:wrong-type-arg
208 (if (not have-sloppy-assv-ref?) (throw 'unsupported))
209 (assoc-set! deformed 'canada '(Iceland hence the name)))))
213 (let ((a '((a b) (c d) (e boring)))
214 (b '(("what" . "else") ("could" . "I") ("say" . "here")))
216 (pass-if "assq-remove!"
218 (set! a (assq-remove! a 'a))
219 (equal? a '((c d) (e boring)))))
220 (pass-if "assv-remove!"
222 (set! a (assv-remove! a 'c))
223 (equal? a '((e boring)))))
224 (pass-if "assoc-remove!"
226 (set! b (assoc-remove! b "what"))
227 (equal? b '(("could" . "I") ("say" . "here")))))
229 (let* ((have-sloppy-assq-remove? (defined? 'sloppy-assq-remove)))
231 (pass-if-exception "assq-remove! deformed"
232 exception:wrong-type-arg
233 (if (not have-sloppy-assq-remove?) (throw 'unsupported))
234 (assq-remove! deformed 'puddle))
236 (pass-if-exception "assv-remove! deformed"
237 exception:wrong-type-arg
238 (if (not have-sloppy-assq-remove?) (throw 'unsupported))
239 (assv-remove! deformed 'splashing))
241 (pass-if-exception "assoc-remove! deformed"
242 exception:wrong-type-arg
243 (if (not have-sloppy-assq-remove?) (throw 'unsupported))
244 (assoc-remove! deformed 'fun))))