Debian Codegraph @

test/include/sets-test.scm

raw
1 (use test)
2 (use sets)
3 (use comparators)
4
5 (test-group "sets"
6 (define (big x) (> x 5))
7
8 (test-group "sets"
9 (test-group "sets/simple"
10 (define nums (set number-comparator))
11 ;; nums is now {}
12 (define syms (set eq-comparator 'a 'b 'c 'd))
13 ;; syms is now {a, b, c, d}
14 (define nums2 (set-copy nums))
15 ;; nums2 is now {}
16 (define syms2 (set-copy syms))
17 ;; syms2 is now {a, b, c, d}
18 (define esyms (set eq-comparator))
19 ;; esyms is now {}
20 (test-assert (set-empty? esyms))
21 (define total 0)
22 (test-assert (set? nums))
23 (test-assert (set? syms))
24 (test-assert (set? nums2))
25 (test-assert (set? syms2))
26 (test-assert (not (set? 'a)))
27 (set-adjoin! nums 2)
28 (set-adjoin! nums 3)
29 (set-adjoin! nums 4)
30 (set-adjoin! nums 4)
31 ;; nums is now {2, 3, 4}
32 (test 4 (set-size (set-adjoin nums 5)))
33 (test 3 (set-size nums))
34 (test 3 (set-size (set-delete syms 'd)))
35 (test 2 (set-size (set-delete-all syms '(c d))))
36 (test 4 (set-size syms))
37 (set-adjoin! syms 'e 'f)
38 ;; syms is now {a, b, c, d, e, f}
39 (test 4 (set-size (set-delete-all! syms '(e f))))
40 ;; syms is now {a, b, c, d}
41 (test 0 (set-size nums2))
42 (test 4 (set-size syms2))
43 (set-delete! nums 2)
44 ;; nums is now {3, 4}
45 (test 2 (set-size nums))
46 (set-delete! nums 1)
47 (test 2 (set-size nums))
48 (set! nums2 (set-map number-comparator (lambda (x) (* 10 x)) nums))
49 ;; nums2 is now {30, 40}
50 (test-assert (set-contains? nums2 30))
51 (test-assert (not (set-contains? nums2 3)))
52 (set-for-each (lambda (x) (set! total (+ total x))) nums2)
53 (test 70 total)
54 (test 10 (set-fold + 3 nums))
55 (set! nums (set eqv-comparator 10 20 30 40 50))
56 ;; nums is now {10, 20, 30, 40, 50}
57 (test-assert
58 (set=? nums (set-unfold
59 (lambda (i) (= i 0))
60 (lambda (i) (* i 10))
61 (lambda (i) (- i 1))
62 5
63 eqv-comparator)))
64 (test '(a) (set->list (set eq-comparator 'a)))
65 (set! syms2 (list->set eq-comparator '(e f)))
66 ;; syms2 is now {e, f}
67 (test 2 (set-size syms2))
68 (test-assert (set-contains? syms2 'e))
69 (test-assert (set-contains? syms2 'f))
70 (list->set! syms2 '(a b))
71 (test 4 (set-size syms2))
72 ) ; end sets/simple
73
74 (test-group "sets/search"
75 (define yam (set char-comparator #\y #\a #\m))
76 (define (failure/insert insert ignore)
77 (insert 1))
78 (define (failure/ignore insert ignore)
79 (ignore 2))
80 (define (success/update element update remove)
81 (update #\b 3))
82 (define (success/remove element update remove)
83 (remove 4))
84 (define yam! (set char-comparator #\y #\a #\m #\!))
85 (define bam (set char-comparator #\b #\a #\m))
86 (define ym (set char-comparator #\y #\m))
87 (define-values (set1 obj1)
88 (set-search! (set-copy yam) #\! failure/insert error))
89 (test-assert (set=? yam! set1))
90 (test 1 obj1)
91 (define-values (set2 obj2)
92 (set-search! (set-copy yam) #\! failure/ignore error))
93 (test-assert (set=? yam set2))
94 (test 2 obj2)
95 (define-values (set3 obj3)
96 (set-search! (set-copy yam) #\y error success/update))
97 (test-assert (set=? bam set3))
98 (test 3 obj3)
99 (define-values (set4 obj4)
100 (set-search! (set-copy yam) #\a error success/remove))
101 (test-assert (set=? ym set4))
102 (test 4 obj4)
103 ) ; end sets/search
104
105 (test-group "sets/subsets"
106 (define set2 (set number-comparator 1 2))
107 (define other-set2 (set number-comparator 1 2))
108 (define set3 (set number-comparator 1 2 3))
109 (define set4 (set number-comparator 1 2 3 4))
110 (define sety (set number-comparator 1 2 4 5))
111 (define setx (set number-comparator 10 20 30 40))
112 (test-assert (set=? set2 other-set2))
113 (test-assert (not (set=? set2 set3)))
114 (test-assert (not (set=? set2 set3 other-set2)))
115 (test-assert (set<? set2 set3 set4))
116 (test-assert (not (set<? set2 other-set2)))
117 (test-assert (set<=? set2 other-set2 set3))
118 (test-assert (not (set<=? set2 set3 other-set2)))
119 (test-assert (set>? set4 set3 set2))
120 (test-assert (not (set>? set2 other-set2)))
121 (test-assert (set>=? set3 other-set2 set2))
122 (test-assert (not (set>=? other-set2 set3 set2)))
123 (test-assert (not (set<? set2 other-set2)))
124 (test-assert (not (set<? set2 setx)))
125 (test-assert (not (set<=? set2 setx)))
126 (test-assert (not (set>? set2 setx)))
127 (test-assert (not (set>=? set2 setx)))
128 (test-assert (not (set<? set3 sety)))
129 (test-assert (not (set<=? set3 sety)))
130 (test-assert (not (set>? set3 sety)))
131 (test-assert (not (set>=? set3 sety)))
132 ) ; end sets/subsets
133
134 (test-group "sets/ops"
135 ;; Potentially mutable
136 (define abcd (set eq-comparator 'a 'b 'c 'd))
137 (define efgh (set eq-comparator 'e 'f 'g 'h))
138 (define abgh (set eq-comparator 'a 'b 'g 'h))
139 ;; Never get a chance to be mutated
140 (define other-abcd (set eq-comparator 'a 'b 'c 'd))
141 (define other-efgh (set eq-comparator 'e 'f 'g 'h))
142 (define other-abgh (set eq-comparator 'a 'b 'g 'h))
143 (define all (set eq-comparator 'a 'b 'c 'd 'e 'f 'g 'h))
144 (define none (set eq-comparator))
145 (define ab (set eq-comparator 'a 'b))
146 (define cd (set eq-comparator 'c 'd))
147 (define ef (set eq-comparator 'e 'f))
148 (define gh (set eq-comparator 'g 'h))
149 (define cdgh (set eq-comparator 'c 'd 'g 'h))
150 (define abcdgh (set eq-comparator 'a 'b 'c 'd 'g 'h))
151 (define abefgh (set eq-comparator 'a 'b 'e 'f 'g 'h))
152 (test-assert (set-disjoint? abcd efgh))
153 (test-assert (not (set-disjoint? abcd ab)))
154 (parameterize ((current-test-comparator set=?))
155 (test abcd (set-union abcd))
156 (test all (set-union abcd efgh))
157 (test abcdgh (set-union abcd abgh))
158 (test abefgh (set-union efgh abgh))
159 (define efgh2 (set-copy efgh))
160 (set-union! efgh2)
161 (test efgh efgh2)
162 (set-union! efgh2 abgh)
163 (test abefgh efgh2)
164 (test abcd (set-intersection abcd))
165 (test none (set-intersection abcd efgh))
166 (define abcd2 (set-copy abcd))
167 (set-intersection! abcd2)
168 (test abcd abcd2)
169 (set-intersection! abcd2 efgh)
170 (test none abcd2)
171 (test ab (set-intersection abcd abgh))
172 (test ab (set-intersection abgh abcd))
173 (test abcd (set-difference abcd))
174 (test cd (set-difference abcd ab))
175 (test abcd (set-difference abcd gh))
176 (test none (set-difference abcd abcd))
177 (define abcd3 (set-copy abcd))
178 (set-difference! abcd3)
179 (test abcd abcd3)
180 (set-difference! abcd3 abcd)
181 (test none abcd3)
182 (test cdgh (set-xor abcd abgh))
183 (test all (set-xor abcd efgh))
184 (test none (set-xor abcd other-abcd))
185 (define abcd4 (set-copy abcd))
186 ;; don't test xor! effect
187 (test none (set-xor! abcd4 other-abcd))
188 (test "abcd smashed?" other-abcd abcd)
189 (test "efgh smashed?" other-efgh efgh)
190 (test "abgh smashed?" other-abgh abgh))
191 ) ; end sets/subsets
192
193 (test-group "sets/mismatch"
194 (define nums (set number-comparator 1 2 3))
195 (define syms (set eq-comparator 'a 'b 'c))
196 (test-error (set=? nums syms))
197 (test-error (set<? nums syms))
198 (test-error (set<=? nums syms))
199 (test-error (set>? nums syms))
200 (test-error (set>=? nums syms))
201 (test-error (set-union nums syms))
202 (test-error (set-intersection nums syms))
203 (test-error (set-difference nums syms))
204 (test-error (set-xor nums syms))
205 (test-error (set-union! nums syms))
206 (test-error (set-intersection! nums syms))
207 (test-error (set-difference! nums syms))
208 (test-error (set-xor! nums syms))
209 ) ; end sets/mismatch
210
211 (test-group "sets/whole"
212 (define whole (set eqv-comparator 1 2 3 4 5 6 7 8 9 10))
213 (define whole2 (set-copy whole))
214 (define whole3 (set-copy whole))
215 (define whole4 (set-copy whole))
216 (define bottom (set eqv-comparator 1 2 3 4 5))
217 (define top (set eqv-comparator 6 7 8 9 10))
218 (define-values (topx bottomx)
219 (set-partition big whole))
220 (set-partition! big whole4)
221 (parameterize ((current-test-comparator set=?))
222 (test top (set-filter big whole))
223 (test bottom (set-remove big whole))
224 (set-filter! big whole2)
225 (test-assert (not (set-contains? whole2 1)))
226 (set-remove! big whole3)
227 (test-assert (not (set-contains? whole3 10)))
228 (test top topx)
229 (test bottom bottomx)
230 (test top whole4))
231 (test 5 (set-count big whole))
232 (define hetero (set eqv-comparator 1 2 'a 3 4))
233 (define homo (set eqv-comparator 1 2 3 4 5))
234 (test 'a (set-find symbol? hetero (lambda () (error "wrong"))))
235 (test-error (set-find symbol? homo (lambda () (error "wrong"))))
236 (test-assert (set-any? symbol? hetero))
237 (test-assert (set-any? number? hetero))
238 (test-assert (not (set-every? symbol? hetero)))
239 (test-assert (not (set-every? number? hetero)))
240 (test-assert (not (set-any? symbol? homo)))
241 (test-assert (set-every? number? homo))
242 ) ; end sets/whole
243
244 (test-group "sets/lowlevel"
245 (define bucket (set string-ci-comparator "abc" "def"))
246 (test string-ci-comparator (set-element-comparator bucket))
247 (test-assert (set-contains? bucket "abc"))
248 (test-assert (set-contains? bucket "ABC"))
249 (test "def" (set-member bucket "DEF" "fqz"))
250 (test "fqz" (set-member bucket "lmn" "fqz"))
251 (define nums (set number-comparator 1 2 3))
252 ;; nums is now {1, 2, 3}
253 (define nums2 (set-replace nums 2.0))
254 ;; nums2 is now {1, 2.0, 3}
255 (test-assert (set-any? inexact? nums2))
256 (set-replace! nums 2.0)
257 ;; nums is now {1, 2.0, 3}
258 (test-assert (set-any? inexact? nums))
259 (define sos
260 (set set-comparator
261 (set equal-comparator '(2 . 1) '(1 . 1) '(0 . 2) '(0 . 0))
262 (set equal-comparator '(2 . 1) '(1 . 1) '(0 . 0) '(0 . 2))))
263 (test 1 (set-size sos))
264 ) ; end sets/lowlevel
265
266 ) ; end sets
267
268 (test-group "bags"
269 (test-group "bags/simple"
270 (define nums (bag number-comparator))
271 ;; nums is now {}
272 (define syms (bag eq-comparator 'a 'b 'c 'd))
273 ;; syms is now {a, b, c, d}
274 (define nums2 (bag-copy nums))
275 ;; nums2 is now {}
276 (define syms2 (bag-copy syms))
277 ;; syms2 is now {a, b, c, d}
278 (define esyms (bag eq-comparator))
279 ;; esyms is now {}
280 (test-assert (bag-empty? esyms))
281 (define total 0)
282 (test-assert (bag? nums))
283 (test-assert (bag? syms))
284 (test-assert (bag? nums2))
285 (test-assert (bag? syms2))
286 (test-assert (not (bag? 'a)))
287 (bag-adjoin! nums 2)
288 (bag-adjoin! nums 3)
289 (bag-adjoin! nums 4)
290 ;; nums is now {2, 3, 4}
291 (test 4 (bag-size (bag-adjoin nums 5)))
292 (test 3 (bag-size nums))
293 (test 3 (bag-size (bag-delete syms 'd)))
294 (test 2 (bag-size (bag-delete-all syms '(c d))))
295 (test 4 (bag-size syms))
296 (bag-adjoin! syms 'e 'f)
297 ;; syms is now {a, b, c, d, e, f}
298 (test 4 (bag-size (bag-delete-all! syms '(e f))))
299 ;; syms is now {a, b, c, d}
300 (test 3 (bag-size nums))
301 (bag-delete! nums 1)
302 (test 3 (bag-size nums))
303 (set! nums2 (bag-map number-comparator (lambda (x) (* 10 x)) nums))
304 ;; nums2 is now {20, 30, 40}
305 (test-assert (bag-contains? nums2 30))
306 (test-assert (not (bag-contains? nums2 3)))
307 (bag-for-each (lambda (x) (set! total (+ total x))) nums2)
308 (test 90 total)
309 (test 12 (bag-fold + 3 nums))
310 (set! nums (bag eqv-comparator 10 20 30 40 50))
311 ;; nums is now {10, 20, 30, 40, 50}
312 (test-assert
313 (bag=? nums (bag-unfold
314 (lambda (i) (= i 0))
315 (lambda (i) (* i 10))
316 (lambda (i) (- i 1))
317 5
318 eqv-comparator)))
319 (test '(a) (bag->list (bag eq-comparator 'a)))
320 (set! syms2 (list->bag eq-comparator '(e f)))
321 ;; syms2 is now {e, f}
322 (test 2 (bag-size syms2))
323 (test-assert (bag-contains? syms2 'e))
324 (test-assert (bag-contains? syms2 'f))
325 (list->bag! syms2 '(e f))
326 ;; syms2 is now {e, e, f, f}
327 (test 4 (bag-size syms2))
328 ) ; end bags/simple
329
330 (test-group "bags/search"
331 (define yam (bag char-comparator #\y #\a #\m))
332 (define (failure/insert insert ignore)
333 (insert 1))
334 (define (failure/ignore insert ignore)
335 (ignore 2))
336 (define (success/update element update remove)
337 (update #\b 3))
338 (define (success/remove element update remove)
339 (remove 4))
340 (define yam! (bag char-comparator #\y #\a #\m #\!))
341 (define bam (bag char-comparator #\b #\a #\m))
342 (define ym (bag char-comparator #\y #\m))
343 (define-values (bag1 obj1)
344 (bag-search! (bag-copy yam) #\! failure/insert error))
345 (test-assert (bag=? yam! bag1))
346 (test 1 obj1)
347 (define-values (bag2 obj2)
348 (bag-search! (bag-copy yam) #\! failure/ignore error))
349 (test-assert (bag=? yam bag2))
350 (test 2 obj2)
351 (define-values (bag3 obj3)
352 (bag-search! (bag-copy yam) #\y error success/update))
353 (test-assert (bag=? bam bag3))
354 (test 3 obj3)
355 (define-values (bag4 obj4)
356 (bag-search! (bag-copy yam) #\a error success/remove))
357 (test-assert (bag=? ym bag4))
358 (test 4 obj4)
359 ) ; end bags/search
360
361 (test-group "bags/elemcount"
362 (define mybag (bag eqv-comparator 1 1 1 1 1 2 2))
363 (test 5 (bag-element-count mybag 1))
364 (test 0 (bag-element-count mybag 3))
365 ) ; end bags/elemcount
366
367 (test-group "bags/subbags"
368 (define bag2 (bag number-comparator 1 2))
369 (define other-bag2 (bag number-comparator 1 2))
370 (define bag3 (bag number-comparator 1 2 3))
371 (define bag4 (bag number-comparator 1 2 3 4))
372 (define bagx (bag number-comparator 10 20 30 40))
373 (define bagy (bag number-comparator 10 20 20 30 40))
374 (test-assert (bag=? bag2 other-bag2))
375 (test-assert (not (bag=? bag2 bag3)))
376 (test-assert (not (bag=? bag2 bag3 other-bag2)))
377 (test-assert (bag<? bag2 bag3 bag4))
378 (test-assert (not (bag<? bag2 other-bag2)))
379 (test-assert (bag<=? bag2 other-bag2 bag3))
380 (test-assert (not (bag<=? bag2 bag3 other-bag2)))
381 (test-assert (bag>? bag4 bag3 bag2))
382 (test-assert (not (bag>? bag2 other-bag2)))
383 (test-assert (bag>=? bag3 other-bag2 bag2))
384 (test-assert (not (bag>=? other-bag2 bag3 bag2)))
385 (test-assert (not (bag<? bag2 other-bag2)))
386 (test-assert (bag<=? bagx bagy))
387 (test-assert (not (bag<=? bagy bagx)))
388 (test-assert (bag<? bagx bagy))
389 (test-assert (not (bag<? bagy bagx)))
390 (test-assert (bag>=? bagy bagx))
391 (test-assert (not (bag<=? bagy bagx)))
392 (test-assert (bag>? bagy bagx))
393 (test-assert (not (bag<? bagy bagx)))
394 ) ; end bags/subbags
395
396 (test-group "bags/multi"
397 (define one (bag eqv-comparator 10))
398 (define two (bag eqv-comparator 10 10))
399 (test-assert (not (bag=? one two)))
400 (test-assert (bag<? one two))
401 (test-assert (not (bag>? one two)))
402 (test-assert (bag<=? one two))
403 (test-assert (not (bag>? one two)))
404 (test-assert (bag=? two two))
405 (test-assert (not (bag<? two two)))
406 (test-assert (not (bag>? two two)))
407 (test-assert (bag<=? two two))
408 (test-assert (bag>=? two two))
409 (test '((10 . 2))
410 (let ((result '()))
411 (bag-for-each-unique
412 (lambda (x y) (set! result (cons (cons x y) result)))
413 two)
414 result))
415 (test 25 (bag-fold + 5 two))
416 (test 12 (bag-fold-unique (lambda (k n r) (+ k n r)) 0 two))
417 ) ; end bags/multi
418
419 (test-group "bags/ops"
420 ;; Potentially mutable
421 (define abcd (bag eq-comparator 'a 'b 'c 'd))
422 (define efgh (bag eq-comparator 'e 'f 'g 'h))
423 (define abgh (bag eq-comparator 'a 'b 'g 'h))
424 ;; Never get a chance to be mutated
425 (define other-abcd (bag eq-comparator 'a 'b 'c 'd))
426 (define other-efgh (bag eq-comparator 'e 'f 'g 'h))
427 (define other-abgh (bag eq-comparator 'a 'b 'g 'h))
428 (define all (bag eq-comparator 'a 'b 'c 'd 'e 'f 'g 'h))
429 (define none (bag eq-comparator))
430 (define ab (bag eq-comparator 'a 'b))
431 (define cd (bag eq-comparator 'c 'd))
432 (define ef (bag eq-comparator 'e 'f))
433 (define gh (bag eq-comparator 'g 'h))
434 (define cdgh (bag eq-comparator 'c 'd 'g 'h))
435 (define abcdgh (bag eq-comparator 'a 'b 'c 'd 'g 'h))
436 (define abefgh (bag eq-comparator 'a 'b 'e 'f 'g 'h))
437 (test-assert (bag-disjoint? abcd efgh))
438 (test-assert (not (bag-disjoint? abcd ab)))
439 (parameterize ((current-test-comparator bag=?))
440 (test abcd (bag-union abcd))
441 (test all (bag-union abcd efgh))
442 (test abcdgh (bag-union abcd abgh))
443 (test abefgh (bag-union efgh abgh))
444 (define efgh2 (bag-copy efgh))
445 (bag-union! efgh2)
446 (test efgh efgh2)
447 (bag-union! efgh2 abgh)
448 (test abefgh efgh2)
449 (test abcd (bag-intersection abcd))
450 (test none (bag-intersection abcd efgh))
451 (define abcd2 (bag-copy abcd))
452 (bag-intersection! abcd2)
453 (test abcd abcd2)
454 (bag-intersection! abcd2 efgh)
455 (test none abcd2)
456 (test ab (bag-intersection abcd abgh))
457 (test ab (bag-intersection abgh abcd))
458 (test abcd (bag-difference abcd))
459 (test cd (bag-difference abcd ab))
460 (test abcd (bag-difference abcd gh))
461 (test none (bag-difference abcd abcd))
462 (define abcd3 (bag-copy abcd))
463 (bag-difference! abcd3)
464 (test abcd abcd3)
465 (bag-difference! abcd3 abcd)
466 (test none abcd3)
467 (test cdgh (bag-xor abcd abgh))
468 (test all (bag-xor abcd efgh))
469 (test none (bag-xor abcd other-abcd))
470 (define abcd4 (bag-copy abcd))
471 (test none (bag-xor! abcd4 other-abcd))
472 (define abab (bag eq-comparator 'a 'b 'a 'b))
473 (test ab (bag-sum ab))
474 (define ab2 (bag-copy ab))
475 (test ab (bag-sum! ab2))
476 (test abab (bag-sum! ab2 ab))
477 (test abab ab2)
478 (test abab (bag-product 2 ab))
479 (define ab3 (bag-copy ab))
480 (bag-product! 2 ab3)
481 (test abab ab3)
482 (test "abcd smashed?" other-abcd abcd)
483 (test "abcd smashed?" other-abcd abcd)
484 (test "efgh smashed?" other-efgh efgh)
485 (test "abgh smashed?" other-abgh abgh))
486 ) ; end bags/ops
487
488 (test-group "bags/mismatch"
489 (define nums (bag number-comparator 1 2 3))
490 (define syms (bag eq-comparator 'a 'b 'c))
491 (test-error (bag=? nums syms))
492 (test-error (bag<? nums syms))
493 (test-error (bag<=? nums syms))
494 (test-error (bag>? nums syms))
495 (test-error (bag>=? nums syms))
496 (test-error (bag-union nums syms))
497 (test-error (bag-intersection nums syms))
498 (test-error (bag-difference nums syms))
499 (test-error (bag-xor nums syms))
500 (test-error (bag-union! nums syms))
501 (test-error (bag-intersection! nums syms))
502 (test-error (bag-difference! nums syms))
503 ) ; end bags/mismatch
504
505 (test-group "bags/whole"
506 (define whole (bag eqv-comparator 1 2 3 4 5 6 7 8 9 10))
507 (define whole2 (bag-copy whole))
508 (define whole3 (bag-copy whole))
509 (define whole4 (bag-copy whole))
510 (define bottom (bag eqv-comparator 1 2 3 4 5))
511 (define top (bag eqv-comparator 6 7 8 9 10))
512 (define-values (topx bottomx)
513 (bag-partition big whole))
514 (bag-partition! big whole4)
515 (parameterize ((current-test-comparator bag=?))
516 (test top (bag-filter big whole))
517 (test bottom (bag-remove big whole))
518 (bag-filter! big whole2)
519 (test-assert (not (bag-contains? whole2 1)))
520 (bag-remove! big whole3)
521 (test-assert (not (bag-contains? whole3 10)))
522 (test top topx)
523 (test bottom bottomx)
524 (test top whole4))
525 (test 5 (bag-count big whole))
526 (define hetero (bag eqv-comparator 1 2 'a 3 4))
527 (define homo (bag eqv-comparator 1 2 3 4 5))
528 (test 'a (bag-find symbol? hetero (lambda () (error "wrong"))))
529 (test-error (bag-find symbol? homo (lambda () (error "wrong"))))
530 (test-assert (bag-any? symbol? hetero))
531 (test-assert (bag-any? number? hetero))
532 (test-assert (not (bag-every? symbol? hetero)))
533 (test-assert (not (bag-every? number? hetero)))
534 (test-assert (not (bag-any? symbol? homo)))
535 (test-assert (bag-every? number? homo))
536 ) ; end bags/whole
537
538 (test-group "bags/lowlevel"
539 (define bucket (bag string-ci-comparator "abc" "def"))
540 (test string-ci-comparator (bag-element-comparator bucket))
541 (test-assert (bag-contains? bucket "abc"))
542 (test-assert (bag-contains? bucket "ABC"))
543 (test "def" (bag-member bucket "DEF" "fqz"))
544 (test "fqz" (bag-member bucket "lmn" "fqz"))
545 (define nums (bag number-comparator 1 2 3))
546 ;; nums is now {1, 2, 3}
547 (define nums2 (bag-replace nums 2.0))
548 ;; nums2 is now {1, 2.0, 3}
549 (test-assert (bag-any? inexact? nums2))
550 (bag-replace! nums 2.0)
551 ;; nums is now {1, 2.0, 3}
552 (test-assert (bag-any? inexact? nums))
553 (define bob
554 (bag bag-comparator
555 (bag eqv-comparator 1 2)
556 (bag eqv-comparator 1 2)))
557 (test 2 (bag-size bob))
558 ) ; end bags/lowlevel
559
560
561 (test-group "bags/semantics"
562 (define mybag (bag number-comparator 1 2))
563 ;; mybag is {1, 2}
564 (test 2 (bag-size mybag))
565 (bag-adjoin! mybag 1)
566 ;; mybag is {1, 1, 2}
567 (test 3 (bag-size mybag))
568 (test 2 (bag-unique-size mybag))
569 (bag-delete! mybag 2)
570 ;; mybag is {1, 1}
571 (bag-delete! mybag 2)
572 (test 2 (bag-size mybag))
573 (bag-increment! mybag 1 3)
574 ;; mybag is {1, 1, 1, 1, 1}
575 (test 5 (bag-size mybag))
576 (test-assert (bag-decrement! mybag 1 2))
577 ;; mybag is {1, 1, 1}
578 (test 3 (bag-size mybag))
579 (bag-decrement! mybag 1 5)
580 ;; mybag is {}
581 (test 0 (bag-size mybag))
582 ) ; end bags/semantics
583
584 (test-group "bags/convert"
585 (define multi (bag eqv-comparator 1 2 2 3 3 3))
586 (define single (bag eqv-comparator 1 2 3))
587 (define singleset (set eqv-comparator 1 2 3))
588 (define minibag (bag eqv-comparator 'a 'a))
589 (define alist '((a . 2)))
590 (test alist (bag->alist minibag))
591 (test-assert (bag=? minibag (alist->bag eqv-comparator alist)))
592 (test-assert (set=? singleset (bag->set single)))
593 (test-assert (set=? singleset (bag->set multi)))
594 (test-assert (bag=? single (set->bag singleset)))
595 (test-assert (not (bag=? multi (set->bag singleset))))
596 (set->bag! minibag singleset)
597 ;; minibag is now {a, a, a, a, 1, 2, 3}
598 (test-assert (bag-contains? minibag 1))
599 ) ; end bags/convert
600
601 (test-group "bags/sumprod"
602 (define abb (bag eq-comparator 'a 'b 'b))
603 (define aab (bag eq-comparator 'a 'a 'b))
604 (define total (bag-sum abb aab))
605 (test 3 (bag-count (lambda (x) (eqv? x 'a)) total))
606 (test 3 (bag-count (lambda (x) (eqv? x 'b)) total))
607 (test 12 (bag-size (bag-product 2 total)))
608 (define bag1 (bag eqv-comparator 1))
609 (bag-sum! bag1 bag1)
610 (test 2 (bag-size bag1))
611 (bag-product! 2 bag1)
612 (test 4 (bag-size bag1))
613 ) ; end bag/sumprod
614
615 ) ; end bags
616
617
618
619 (test-group "comparators"
620 (define a (set number-comparator 1 2 3))
621 (define b (set number-comparator 1 2 4))
622 (define aa (bag number-comparator 1 2 3))
623 (define bb (bag number-comparator 1 2 4))
624 (test-assert (not (=? set-comparator a b)))
625 (test-assert (=? set-comparator a (set-copy a)))
626 (test-error (<? set-comparator a b))
627 (test-assert (not (=? bag-comparator aa bb)))
628 (test-assert (=? bag-comparator aa (bag-copy aa)))
629 (test-error (<? bag-comparator aa bb))
630 (test-assert (not (=? (make-default-comparator) a aa)))
631 ) ; end comparators
632
633 ) ; end r7rs-sets
634
635 (test-exit)