summaryrefslogtreecommitdiffstats
path: root/srfi-225-test.scm
blob: 5f4553ac881bc134eae17c4b3194f0c93cf2a451 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
(import (scheme base)
        (scheme case-lambda)
        (scheme write)
        (srfi 1)
        (srfi 128)
        (srfi 158)
        (srfi 225))

(cond-expand
  ((library (srfi 69))
   (import (prefix (srfi 69) t69-)))
  (else))

(cond-expand
  ((library (srfi 125))
   (import (prefix (srfi 125) t125-)))
  (else))

(cond-expand
  ((library (srfi 126))
   (import (prefix (srfi 126) t126-)))
  (else))

(cond-expand
  ((and (library (srfi 146))
        (library (srfi 146 hash)))
   (import (srfi 146)
           (srfi 146 hash)))
  (else))

(cond-expand
  (chibi
   (import (rename (except (chibi test) test-equal)
                   (test test-equal)
                   (test-group test-group*)))
   (define test-skip-count 0)
   (define (test-skip n)
     (set! test-skip-count n))
   (define-syntax test-group
     (syntax-rules ()
       ((_ name body ...)
        (test-group*
          name
          (if (> test-skip-count 0)
              (set! test-skip-count (- test-skip-count 1))
              (let ()
               body ...)))))))
  (else
   (import (srfi 64))))

;; returns new wrapper dto
;; which counts how often each dto's method was called
;; verify that all functions were tested
(define (wrap-dto dto)
  (define proc-count (+ 1 dict-adjoin-accumulator-id))
  (define counter (make-vector proc-count 0))
  (define wrapper-dto-args
    (let loop ((indexes (iota proc-count))
               (args '()))
      (if (null? indexes)
          args
          (let* ((index (car indexes))
                 (real-proc (dto-ref dto index))
                 (wrapper-proc (lambda args
                                 (vector-set! counter index (+ 1 (vector-ref counter index)))
                                 (apply real-proc args))))
            (loop (cdr indexes)
                  (append (list index wrapper-proc)
                          args))))))
  (values
   (apply make-dto wrapper-dto-args)
   counter))

(define (test-for-each expect-success for-each-proc expected-keys)
  (call/cc (lambda (cont)
             (with-exception-handler
               (lambda (err)
                 (when expect-success
                   (display err)
                   (newline))
                 (unless expect-success
                   (cont #t)))
               (lambda ()
                 (define lst '())
                 (for-each-proc
                   (lambda (key value)
                     (set! lst (append lst (list key)))))
                 (test-equal (length lst) (length expected-keys))
                 (for-each
                   (lambda (key)
                     (test-assert (find (lambda (key*) (equal? key key*))
                                        expected-keys)))
                   lst))))))

(define (do-test real-dto alist->dict comparator mutable?)

  (define-values
      (dto counter)
    (wrap-dto real-dto))

  (test-group
   "dictionary?"
   (test-assert (not (dictionary? dto 'foo)))
   (test-assert (dictionary? dto (alist->dict '())))
   (test-assert (dictionary? dto (alist->dict '((a . b))))))

  (test-group
   "dict-empty?"
   (test-assert (dict-empty? dto (alist->dict '())))
   (test-assert (not (dict-empty? dto (alist->dict '((a . b)))))))

  (test-group
   "dict-contains?"
   (test-assert (not (dict-contains? dto (alist->dict '()) 'a)))
   (test-assert (not (dict-contains? dto (alist->dict '((b . c))) 'a)))
   (test-assert (dict-contains? dto (alist->dict '((a . b))) 'a)))

  (test-group
    "dict=?"
    (define dict1 (alist->dict '((a . 1) (b . 2))))
    (define dict2 (alist->dict '((b . 2) (a . 1))))
    (define dict3 (alist->dict '((a . 1))))
    (define dict4 (alist->dict '((a . 2) (b . 2))))

    (test-assert (dict=? dto = dict1 dict2))
    (test-assert (not (dict=? dto = dict1 dict3)))
    (test-assert (not (dict=? dto = dict3 dict1)))
    (test-assert (not (dict=? dto = dict1 dict4)))
    (test-assert (not (dict=? dto = dict4 dict1))))

  (test-group
   "dict-ref"
   (test-assert (dict-ref dto (alist->dict '((a . b))) 'a (lambda () #f) (lambda (x) #t)))
   (test-assert (dict-ref dto (alist->dict '((a . b))) 'b (lambda () #t) (lambda (x) #f))))

  (test-group
   "dict-ref/default"
   (test-equal (dict-ref/default dto (alist->dict '((a . b))) 'a 'c) 'b)
   (test-equal (dict-ref/default dto (alist->dict '((a* . b))) 'a 'c) 'c))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-set"
   (define dict-original (alist->dict '((a . b))))
   (define d (dict-set dto dict-original 'a 'c 'a2 'b2))
   (test-equal 'c (dict-ref dto d 'a ))
   (test-equal 'b2 (dict-ref dto d 'a2))
   (test-equal 'b (dict-ref dto dict-original' a))
   (test-equal #f (dict-ref/default dto dict-original 'a2 #f)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-set"
   (define d (alist->dict '((a . b))))
   (dict-set dto d 'a 'c 'a2 'b2)
   (test-equal 'c (dict-ref dto d 'a ))
   (test-equal 'b2 (dict-ref dto d 'a2)))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-adjoin"
   (define dict-original (alist->dict '((a . b))))
   (define d (dict-adjoin dto dict-original 'a 'c 'a2 'b2))
   (test-equal 'b (dict-ref dto d 'a))
   (test-equal 'b2 (dict-ref dto d 'a2))
   (test-equal #f (dict-ref/default dto dict-original 'a2 #f)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-adjoin"
   (define d (alist->dict '((a . b))))
   (dict-adjoin dto d 'a 'c 'a2 'b2)
   (test-equal 'b (dict-ref dto d 'a))
   (test-equal 'b2 (dict-ref dto d 'a2)))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-delete"
   (define dict-original (alist->dict '((a . b) (c . d))))
   (define d (dict-delete dto dict-original 'a 'b))
   (test-equal (dict->alist dto d) '((c . d)))
   (test-equal 'b (dict-ref dto dict-original 'a)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-delete"
   (define d (alist->dict '((a . b) (c . d))))
   (dict-delete dto d 'a 'b)
   (test-equal (dict->alist dto d) '((c . d))))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-delete-all"
   (define dict-original (alist->dict '((a . b) (c . d))))
   (define d (dict-delete-all dto dict-original '(a b)))
   (test-equal (dict->alist dto d) '((c . d)))
   (test-equal 'b (dict-ref dto dict-original 'a)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-delete-all"
   (define d (alist->dict '((a . b) (c . d))))
   (dict-delete-all dto d '(a b))
   (test-equal (dict->alist dto d) '((c . d))))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-replace"
   (define dict-original (alist->dict '((a . b) (c . d))))
   (define d (dict-replace dto dict-original 'a 'b2))
   (test-equal 'b2 (dict-ref dto d 'a))
   (test-equal 'd (dict-ref dto d 'c))
   (test-equal 'b (dict-ref dto dict-original 'a)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-replace"
   (define d (alist->dict '((a . b) (c . d))))
   (dict-replace dto d 'a 'b2)
   (test-equal 'b2 (dict-ref dto d 'a))
   (test-equal 'd (dict-ref dto d 'c)))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-intern"
   ;; intern existing
   (let ()
     (define-values
         (d value)
       (dict-intern dto (alist->dict '((a . b))) 'a (lambda () 'd)))
     (test-equal 'b (dict-ref dto d 'a))
     (test-equal 'b value))
   ;; intern missing
   (let ()
     (define dict-original (alist->dict '((a . b))))
     (define-values
         (d value)
       (dict-intern dto dict-original 'c (lambda () 'd)))
     (test-equal 'b (dict-ref dto d 'a))
     (test-equal 'd (dict-ref dto d 'c))
     (test-equal 'd value)
     (test-equal #f (dict-ref/default dto dict-original 'c #f))))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-intern"
   ;; intern existing
   (let ()
    (define d (alist->dict '((a . b))))
    (define-values (new-dict value) (dict-intern dto d 'a (lambda () 'd)))
    (test-equal 'b (dict-ref dto d 'a))
    (test-equal 'b value))
   ;; intern missing
   (let ()
    (define d (alist->dict '((a . b))))
    (define-values (new-dict value) (dict-intern dto d 'c (lambda () 'd)))
    (test-equal 'b (dict-ref dto d 'a))
    (test-equal 'd (dict-ref dto d 'c))
    (test-equal 'd value)))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-update"
   ;; update existing
   (define dict-original (alist->dict '((a . "b"))))
   (let ()
     (define d (dict-update dto dict-original 'a
                            (lambda (value)
                              (string-append value "2"))
                            error
                            (lambda (x) (string-append x "1"))))
     (test-equal "b12" (dict-ref dto d 'a))
     (test-equal "b" (dict-ref dto dict-original 'a)))
   ;; update missing
   (let ()
     (define d (dict-update dto dict-original 'c
                            (lambda (value)
                              (string-append value "2"))
                            (lambda () "d1")
                            (lambda (x) (string-append x "1"))))
     (test-equal "d12" (dict-ref dto d 'c))
     (test-equal #f (dict-ref/default dto dict-original 'c #f))))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-update"
   ;; update existing
   (let ()
    (define d (alist->dict '((a . "b"))))
    (dict-update dto d 'a
                  (lambda (value)
                    (string-append value "2"))
                  error
                  (lambda (x) (string-append x "1")))
    (test-equal "b12" (dict-ref dto d 'a)))
   ;; update missing
   (let ()
    (define d (alist->dict '((a . "b"))))
    (dict-update dto d 'c
                  (lambda (value)
                    (string-append value "2"))
                  (lambda () "d1")
                  (lambda (x) (string-append x "1")))
     (test-equal "d12" (dict-ref dto d 'c))))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-update/default"
   ;; update existing
   (define dict-original (alist->dict '((a . "b"))))
   (let ()
     (define d (dict-update/default dto dict-original 'a
                                    (lambda (value)
                                      (string-append value "2"))
                                    "d1"))
     (test-equal "b2" (dict-ref dto d 'a))
     (test-equal "b" (dict-ref dto dict-original 'a)))

   ;; update missing
   (let ()
     (define d (dict-update/default dto dict-original 'c
                                    (lambda (value)
                                      (string-append value "2"))
                                    "d1"))
     (test-equal "d12" (dict-ref dto d 'c))
     (test-equal #f (dict-ref/default dto dict-original 'c #f))))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-update/default"
   ;; update existing
   (let ()
    (define d (alist->dict '((a . "b"))))
    (dict-update/default dto d 'a
                          (lambda (value)
                            (string-append value "2"))
                          "d1")
     (test-equal "b2" (dict-ref dto d 'a)))

   ;; update missing
   (let ()
    (define d (alist->dict '((a . "b"))))
    (dict-update/default dto d 'c
                          (lambda (value)
                            (string-append value "2"))
                          "d1")
    (test-equal "d12" (dict-ref dto d 'c))))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-pop"
   (define dict-original (alist->dict '((a . b) (c . d))))
   (define-values
       (new-dict key value)
     (dict-pop dto dict-original))
   (test-assert
       (or
        (and (equal? (dict->alist dto new-dict) '((c . d)))
             (equal? key 'a)
             (equal? value 'b))

        (and (equal? (dict->alist dto new-dict) '((a . b)))
             (equal? key 'c)
             (equal? value 'd))))
   (test-assert 'b (dict-ref dto dict-original 'a))
   (test-assert 'd (dict-ref dto dict-original 'c)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-pop"
   (define d (alist->dict '((a . b) (c . d))))
   (define-values
       (new-dict key value)
     (dict-pop dto d))
   (test-assert (eq? new-dict d))
   (test-assert
       (or
        (and (equal? (dict->alist dto d) '((c . d)))
             (equal? key 'a)
             (equal? value 'b))

        (and (equal? (dict->alist dto d) '((a . b)))
             (equal? key 'c)
             (equal? value 'd)))))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-map"
   (define dict-original (alist->dict '((a . "a") (b . "b"))))
   (define d (dict-map dto
                       (lambda (key value)
                         (string-append value "2"))
                       dict-original))
   (test-equal "a2" (dict-ref dto d 'a))
   (test-equal "b2" (dict-ref dto d 'b))
   (test-equal "a" (dict-ref dto dict-original 'a))
   (test-equal "b" (dict-ref dto dict-original 'b)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-map"
   (define d (alist->dict '((a . "a") (b . "b"))))
   (dict-map dto
              (lambda (key value)
                (string-append value "2"))
              d)
   (test-equal "a2" (dict-ref dto d 'a))
   (test-equal "b2" (dict-ref dto d 'b)))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-filter"
   (define dict-original (alist->dict '((a . b) (c . d))))

   (define d (dict-filter dto
                          (lambda (key value)
                            (equal? value 'b))
                          dict-original))
   (test-equal '((a . b)) (dict->alist dto d))
   (test-equal 'd (dict-ref dto dict-original 'c)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-filter"
   (define d (alist->dict '((a . b) (c . d))))
   (dict-filter dto
                 (lambda (key value)
                   (equal? value 'b))
                 d)
   (test-equal '((a . b)) (dict->alist dto d)))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-remove"
   (define dict-original (alist->dict '((a . b) (c . d))))
   (define d (dict-remove dto
                          (lambda (key value)
                            (equal? value 'b))
                          dict-original))
   (test-equal '((c . d)) (dict->alist dto d))
   (test-equal 'b (dict-ref dto dict-original 'a)))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-remove"
   (define d (alist->dict '((a . b) (c . d))))
   (dict-remove dto
                 (lambda (key value)
                   (equal? value 'b))
                 d)
   (test-equal '((c . d)) (dict->alist dto d)))

  (when mutable?
    (test-skip 1))
  (test-group
   "dict-find-update"
   ;; ignore
   (let ()
     (define dict (dict-find-update dto (alist->dict '((a . b))) 'c
                              (lambda (insert ignore)
                                (ignore))
                              (lambda args
                                (error "shouldn't happen"))))
     (test-equal '((a . b)) (dict->alist dto dict)))

   ;; insert
   (let ()
     (define dict-original (alist->dict '((a . b))))
     (define dict (dict-find-update dto dict-original 'c
                    (lambda (insert ignore)
                      (insert 'd))
                    (lambda args
                      (error "shouldn't happen"))))
     (test-equal 'b (dict-ref dto dict 'a))
     (test-equal 'd (dict-ref dto dict 'c))
     (test-equal #f (dict-ref/default dto dict-original 'c #f)))

   ;; update
   (let ()
     (define dict-original (alist->dict '((a . b))))
     (define dict (dict-find-update dto dict-original 'a
                              (lambda args
                                (error "shouldn't happen"))
                              (lambda (key value update delete)
                                (update 'a2 'b2))))
     (test-equal '((a2 . b2)) (dict->alist dto dict))
     (test-equal #f (dict-ref/default dto dict-original 'a2 #f))
     (test-equal 'b (dict-ref dto dict-original 'a)))

   ;; delete
   (let ()
     (define dict-original (alist->dict '((a . b) (c . d))))
     (define dict (dict-find-update dto dict-original 'a
                              (lambda args
                                (error "shouldn't happen"))
                              (lambda (key value update delete)
                                (delete))))
     (test-equal '((c . d)) (dict->alist dto dict))
     (test-equal 'b (dict-ref dto dict-original 'a))))

  (unless mutable?
    (test-skip 1))
  (test-group
   "dict-find-update"
   ;; ignore
   (let ()
    (define dict (alist->dict '((a . b))))
    (dict-find-update dto dict 'c
                              (lambda (insert ignore)
                                (ignore))
                              (lambda args
                                (error "shouldn't happen")))
    (test-equal '((a . b)) (dict->alist dto dict)))

   ;; insert
   (let ()
    (define dict (alist->dict '((a . b))))
    (dict-find-update dto dict 'c
                       (lambda (insert ignore)
                         (insert 'd))
                       (lambda args
                         (error "shouldn't happen")))
    (test-equal 'b (dict-ref dto dict 'a))
    (test-equal 'd (dict-ref dto dict 'c)))

   ;; update
   (let ()
    (define dict (alist->dict '((a . b))))
    (dict-find-update dto dict 'a
                       (lambda args
                         (error "shouldn't happen"))
                       (lambda (key value update delete)
                         (update 'a2 'b2)))
     (test-equal '((a2 . b2)) (dict->alist dto dict)))

   ;; delete
   (let ()
    (define dict (alist->dict '((a . b) (c . d))))
    (dict-find-update dto dict 'a
                       (lambda args
                         (error "shouldn't happen"))
                       (lambda (key value update delete)
                         (delete)))
    (test-equal '((c . d)) (dict->alist dto dict))))

  (test-group
   "dict-size"
   (test-equal 2 (dict-size dto (alist->dict '((a . b) (c . d)))))
   (test-equal 0 (dict-size dto (alist->dict '()))))

  (test-group
   "dict-count"
   (define count (dict-count dto
                             (lambda (key value)
                               (equal? value 'b))
                             (alist->dict '((a . b) (c . d)))))
   (test-equal count 1))

  (test-group
   "dict-any"

   (let ()
     (define value
       (dict-any dto
                 (lambda (key value)
                   (if (equal? 'b value) 'foo #f))
                 (alist->dict '((a . b) (c . d)))))
     (test-equal value 'foo))

   (let ()
     (define value
       (dict-any dto
                 (lambda (key value)
                   (if (equal? 'e value) 'foo #f))
                 (alist->dict '((a . b) (c . d)))))
     (test-equal value #f)))

  (test-group
   "dict-every"
   (let ()
     (define value
       (dict-every dto
                   (lambda (key value)
                     (if (equal? 'b value) 'foo #f))
                   (alist->dict '((a . b) (c . b)))))
     (test-equal value 'foo))

   (let ()
     (define value
       (dict-every dto
                   (lambda (key value)
                     (if (equal? 'b value) 'foo #f))
                   (alist->dict '())))
     (test-equal value #t))

   (let ()
     (define value
       (dict-every dto
                   (lambda (key value)
                     (if (equal? 'b value) 'foo #f))
                   (alist->dict '((a . b) (c . d)))))
     (test-equal value #f)))

  (test-group
   "dict-keys"
   (define keys
     (dict-keys dto (alist->dict '((a . b) (c . d)))))
   (test-assert
       (or (equal? '(a c) keys)
           (equal? '(c a) keys))))

  (test-group
   "dict-values"
   (define vals
     (dict-values dto (alist->dict '((a . b) (c . d)))))
   (test-assert
       (or (equal? '(b d) vals)
           (equal? '(d b) vals))))

  (test-group
   "dict-entries"
   (define-values
       (keys vals)
     (dict-entries dto (alist->dict '((a . b) (c . d)))))
   (test-assert
       (or (and (equal? '(a c) keys)
                (equal? '(b d) vals))
           (and (equal? '(c a) keys)
                (equal? '(d b) vals)))))

  (test-group
   "dict-fold"
   
   ;; simple case
   (let ()
    (define value
      (dict-fold dto
                 (lambda (key value acc)
                   (append acc (list key value)))
                 '()
                 (alist->dict '((a . b) (c . d)))))
    (test-assert
      (or (equal? '(a b c d) value)
          (equal? '(c d a b) value))))
   
   (let ()

     ;; continuation captured in a middle of fold
    (define k #f)
    (define pass 0)

    (define value
      (dict-fold dto
                 (lambda (key value acc)
                   ;; check fold only starts once -- further passes enter in a middle
                   (test-assert (not (and k
                                          (null? acc))))
                   ;; capture continuation on second fold iteration
                   (when (and (not k)
                              (not (null? acc)))
                     (test-assert
                       (or (equal? '(a b) acc)
                           (equal? '(c d) acc)))
                     (call/cc (lambda (cont) (set! k cont))))
                   (append acc (list key value)))
                 '()
                 (alist->dict '((a . b) (c . d)))))

    (test-assert
      (or (equal? '(a b c d) value)
          (equal? '(c d a b) value)))

    (when (< pass 3)
      (set! pass (+ 1 pass))
      (k #t))))

  (test-group
   "dict-map->list"
   (define lst
     (dict-map->list dto
                     (lambda (key value)
                       (string-append (symbol->string key)
                                      value))
                     (alist->dict '((a . "b") (c . "d")))))
   (test-assert
       (or (equal? '("ab" "cd") lst)
           (equal? '("cd" "ab") lst))))

  (test-group
   "dict->alist"
   (define alist
     (dict->alist dto (alist->dict '((a . b) (c . d)))))
   (test-assert
       (or (equal? '((a . b) (c . d)) alist)
           (equal? '((c . d) (a . b)) alist))))

  (test-group
   "dict-comparator"
   ;; extremelly basic generic test; more useful specific tests defined separately
   ;; for each dto
   (let ((cmp (dict-comparator dto (alist->dict '((a . b))))))
     (test-assert (or (not cmp)
                      (comparator? cmp)))))

  (test-group
    "dict-for-each"
    (test-for-each #t
      (lambda (proc)
        (dict-for-each dto
                       proc
                       (alist->dict '((1 . a)
                                      (2 . b)
                                      (3 . c)
                                      (4 . d)))))
      '(1 2 3 4))
    
    (test-for-each (let* ((cmp (dict-comparator dto (alist->dict '())))
                          (ordering (and cmp (comparator-ordered? cmp))))
                     ordering)
                   (lambda (proc)
                     (dict-for-each dto
                                    proc
                                    (alist->dict '((1 . a)
                                                   (2 . b)
                                                   (3 . c)
                                                   (4 . d)))
                                    2))
                   '(2 3 4))

    (test-for-each (let* ((cmp (dict-comparator dto (alist->dict '())))
                          (ordering (and cmp (comparator-ordered? cmp))))
                     ordering)
                   (lambda (proc)
                     (dict-for-each dto
                                    proc
                                    (alist->dict '((1 . a)
                                                   (2 . b)
                                                   (3 . c)
                                                   (4 . d)))
                                    2
                                    3))
                   '(2 3)))

  (test-group
    "dict->generator"
    (test-for-each #t
                   (lambda (proc)
                     (generator-for-each
                       (lambda (entry)
                         (proc (car entry) (cdr entry)))
                       (dict->generator dto (alist->dict '((1 . a)
                                                           (2 . b)
                                                           (3 . c)
                                                           (4 . d))))))
                   '(1 2 3 4))

    (test-for-each (let* ((cmp (dict-comparator dto (alist->dict '())))
                          (ordering (and cmp (comparator-ordered? cmp))))
                     ordering)
                   (lambda (proc)
                     (generator-for-each
                       (lambda (entry)
                         (proc (car entry) (cdr entry)))
                       (dict->generator dto (alist->dict '((1 . a)
                                                           (2 . b)
                                                           (3 . c)
                                                           (4 . d)))
                                        2)))
                   '(2 3 4))

    (test-for-each (let* ((cmp (dict-comparator dto (alist->dict '())))
                          (ordering (and cmp (comparator-ordered? cmp))))
                     ordering)
                   (lambda (proc)
                     (generator-for-each
                       (lambda (entry)
                         (proc (car entry) (cdr entry)))
                       (dict->generator dto (alist->dict '((1 . a)
                                                           (2 . b)
                                                           (3 . c)
                                                           (4 . d)))
                                        2 3)))
                   '(2 3)))

  (when mutable?
    (test-skip 1))
  (test-group
    "dict-set-accumulator"
    (define acc (dict-set-accumulator dto (alist->dict '())))
    (acc (cons 1 'a))
    (acc (cons 2 'b))
    (acc (cons 2 'c))
    (test-assert (dict=? dto equal? (acc (eof-object)) (alist->dict '((1 . a) (2 . c))))))
  
  (unless mutable?
    (test-skip 1))
  (test-group
    "dict-set-accumulator"
    (define acc (dict-set-accumulator dto (alist->dict '())))
    (acc (cons 1 'a))
    (acc (cons 2 'b))
    (acc (cons 2 'c))
    (test-assert (dict=? dto equal? (acc (eof-object)) (alist->dict '((1 . a) (2 . c))))))

  (when mutable?
    (test-skip 1))
  (test-group
    "dict-adjoin-accumulator"
    (define acc (dict-adjoin-accumulator dto (alist->dict '())))
    (acc (cons 1 'a))
    (acc (cons 2 'b))
    (acc (cons 2 'c))
    (test-assert (dict=? dto equal? (acc (eof-object)) (alist->dict '((1 . a) (2 . b))))))
  
  (unless mutable?
    (test-skip 1))
  (test-group
    "dict-adjoin-accumulator"
    (define acc (dict-adjoin-accumulator dto (alist->dict '())))
    (acc (cons 1 'a))
    (acc (cons 2 'b))
    (acc (cons 2 'c))
    (test-assert (dict=? dto equal? (acc (eof-object)) (alist->dict '((1 . a) (2 . b))))))

  ;; check all procs were called
  (for-each
   (lambda (index)
     (when (> 0 (vector-ref counter index))
       (error "Untested procedure" index)))
   (iota (vector-length counter))))

(test-begin "Dictionaries")

(test-group
 "default"
 ;; test defaults by overring only procedures that raise error otherwise

  (define (alist-find-update dto alist key failure success)
    (define (handle-success pair)
      (define old-key (car pair))
      (define old-value (cdr pair))
      (define (update new-key new-value)
        (cond
          ((and (eq? old-key
                     new-key)
                (eq? old-value
                     new-value))
           alist)
          (else
            (let ((new-list
                    (alist-cons
                      new-key new-value
                      (alist-delete old-key alist))))
              new-list))))
      (define (remove)
        (alist-delete old-key alist))
      (success old-key old-value update remove))

    (define (handle-failure)
      (define (insert value)
        (alist-cons key value alist))
      (define (ignore)
        alist)
      (failure insert ignore))
    (cond
      ((assoc key alist equal?) => handle-success)
      (else (handle-failure))))

  (define (alist-map dto proc alist)
    (map
     (lambda (e)
       (define key (car e))
       (define value (cdr e))
       (cons key (proc key value)))
     alist))

 (define minimal-alist-dto
   (make-dto
    dictionary?-id (lambda (dto obj) (list? obj))
    dict-pure?-id (lambda _ #t)
    dict-size-id (lambda (dto alist) (length alist))
    dict-find-update-id alist-find-update
    dict-map-id alist-map
    dict-comparator-id (lambda _ #f)))
 (do-test
  minimal-alist-dto
  alist-copy
  #f
  #f))

(cond-expand
  ((and (library (srfi 69))
        (not gauche) ;; gauche has bug with comparator retrieval from srfi 69 table
        )
   (test-group
     "srfi-69"
     (do-test
       srfi-69-dto
       (lambda (alist)
         (define table (t69-make-hash-table equal?))
         (for-each
           (lambda (pair)
             (t69-hash-table-set! table (car pair) (cdr pair)))
           alist)
         table)
       (make-default-comparator)
       #t)))
  (else))

(cond-expand
  ((library (srfi 125))
   (test-group
     "srfi-125"
     (do-test
       hash-table-dto
       (lambda (alist)
         (define table (t125-hash-table-empty-copy (t125-make-hash-table equal?)))
         (for-each
           (lambda (pair)
             (t125-hash-table-set! table (car pair) (cdr pair)))
           alist)
         table)
       (make-default-comparator)
       #t)))
  (else))

(cond-expand
  ((library (srfi 126))
   (test-group
     "srfi-126 (r6rs)"
     (do-test
       srfi-126-dto
       (lambda (alist)
         (define table (t126-make-eqv-hashtable))
         (for-each
           (lambda (pair)
             (t126-hashtable-set! table (car pair) (cdr pair)))
           alist)
         table)
       (make-default-comparator)
       #t)))
  (else))

(cond-expand
  ((and (library (srfi 146))
        (library (srfi 146 hash)))
   (test-group
     "srfi-146"
     (define cmp (make-default-comparator))
     (do-test
       mapping-dto
       (lambda (alist)
         (let loop ((table (mapping cmp))
                    (entries alist))
           (if (null? entries)
               table
               (loop (mapping-set! table (caar entries) (cdar entries))
                     (cdr entries)))))
       cmp
       #f)
     (test-group
       "srfi-146 dict-comparator"
       (test-equal cmp (dict-comparator mapping-dto (mapping cmp)))))

   (test-group
     "srfi-146 hash"
     (define cmp (make-default-comparator))
     (do-test
       hash-mapping-dto
       (lambda (alist)
         (let loop ((table (hashmap cmp))
                    (entries alist))
           (if (null? entries)
               table
               (loop (hashmap-set! table (caar entries) (cdar entries))
                     (cdr entries)))))
       cmp
       #f)
     (test-group
       "srfi-146 hash dict-comparator"
       (test-equal cmp (dict-comparator hash-mapping-dto (hashmap cmp))))))
  (else))

(test-end)