File: test-amap.scm

package info (click to toggle)
mit-scheme 12.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,300 kB
  • sloc: lisp: 781,881; xml: 425,435; ansic: 86,059; sh: 10,135; makefile: 2,501; asm: 2,121; csh: 1,143
file content (493 lines) | stat: -rw-r--r-- 13,959 bytes parent folder | download | duplicates (2)
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
#| -*-Scheme-*-

Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
    2017, 2018, 2019, 2020, 2021, 2022 Massachusetts Institute of
    Technology

This file is part of MIT/GNU Scheme.

MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.

|#

;;;; Tests of associative-map implementation

(declare (usual-integrations))

(define test-alist
  '((2 . -11)
    (3 . -12)
    (5 . -13)
    (7 . -14)
    (11 . -15)))

(define test-reversed-alist
  '((2 . -15)
    (3 . -14)
    (5 . -13)
    (7 . -12)
    (11 . -11)))

(define test-keys
  (map car test-alist))

(define test-values
  (map cdr test-alist))

(define (test-value key)
  (cdr (assv key test-alist)))

(define (test-reversed-value key)
  (cdr (assv key test-reversed-alist)))

(define other-keys
  '(0 1))

(define test-key-comparator
  fixnum-comparator)

(define test-value-comparator
  fixnum-comparator)

(define (fail)
  1000)

(define (succeed value)
  (* value 3))

(define (test-pred key value)
  (eqv? value (test-value key)))

(define (test-kons key value acc)
  (cons* key (square value) acc))

(define test-knil
  '())

(define (test-map-proc value)
  (- value))

(define (test-map!-proc key value)
  (declare (ignore key))
  (- value))

(define amap-types
  (filter (lambda (name)
	    (and (amap-implementation-supports-comparator? name
							   test-key-comparator)
		 (amap-implementation-supports-args? name '())))
	  (amap-implementation-names)))

(define ordered-and-hashable-comparator fixnum-comparator)
(define hashable-comparator (make-eqv-comparator))
(define ordered-comparator (make-comparator symbol? eq? symbol<? #f))
(define neither-comparator (make-comparator any-object? eqv? #f #f))

(define base-key-comparators
  (list ordered-and-hashable-comparator
	hashable-comparator
	ordered-comparator
	neither-comparator))

(define list-key-comparators
  (map uniform-list-comparator base-key-comparators))

(define lset-key-comparators		;always ordered
  (map lset-comparator base-key-comparators))

(define test-key-comparators
  (append base-key-comparators
	  list-key-comparators
	  lset-key-comparators))

(define (at-least-one-of items)
  (delq '() (all-choices-of items)))

(define (at-most-one-of items)
  (cons '() (map list items)))

(define (all-choices-of items)

  (define (choose k items)
    (if (> k 0)
	(if (< k (length items))
	    (append (map (lambda (tail)
			   (cons (car items) tail))
			 (choose (- k 1) (cdr items)))
		    (choose k (cdr items)))
	    (list items))
	'(())))

  (let ((n (length items)))
    (let loop ((k 0))
      (if (< k n)
	  (append (choose k items)
		  (loop (+ k 1)))
	  (choose n items)))))

(define (good-comparator-pred name)
  (case name
    ((hash-table) comparator-hashable?)
    ((alist) any-object?)
    ((trie) uniform-list-comparator?)
    ((red/black-tree) comparator-ordered?)
    (else (error "Unknown implementation type:" name))))

(define (all-choices)
  (all-choices-of (subst-args (all-amap-args))))

(define (subst-args args)
  (map (lambda (arg)
	 (if (eqv? exact-nonnegative-integer? arg)
	     7
	     arg))
       args))

(define (good-choices name)
  (let ((supported (amap-implementation-supported-args name)))
    (append-choices (good-kv-choices supported)
		    (good-time-choices supported)
		    (good-other-choices supported))))

(define (good-kv-choices supported)
  (append '(())
	  (at-least-one-of (match-args supported kv-weak-args))
	  (at-least-one-of (match-args supported kv-ephemeral-args))))

(define kv-weak-args
  '(weak-keys weak-values))

(define kv-ephemeral-args
  '(ephemeral-keys ephemeral-values))

(define (good-time-choices supported)
  (at-most-one-of (match-args supported time-args)))

(define time-args
  '(amortized-constant-time log-time sublinear-time linear-time))

(define (good-other-choices supported)
  (all-choices-of (subst-args (match-args supported other-args))))

(define other-args
  `(thread-safe ordered-by-key ,exact-nonnegative-integer?))

(define (match-args a1 a2)
  (lset-intersection eqv? a1 a2))

(define (append-choices . choices-list)
  (reduce-right (lambda (prefixes suffixes)
		  (append-map (lambda (prefix)
				(map (lambda (suffix)
				       (append prefix suffix))
				     suffixes))
			      prefixes))
		'()
		choices-list))

(define (explicit-construction-tests name)
  (lambda ()

    (define (test-make comparator args)
      (if (and (good-comparator? comparator) (good-args? args))
	  (test-good comparator args)
	  (test-bad comparator args)))

    (define (test-good comparator args)
      (let ((amap (run-make comparator args)))
	(assert-true (amap? amap))
	(assert-equal (amap-implementation-name amap) name)
	(assert-eqv (amap-comparator amap) comparator)
	(assert-equal (amap-args amap) (massage-args args))))

    (define (test-bad comparator args)
      (assert-error (lambda () (run-make comparator args))
		    (default-object)
		    'expression `(make ,name ,comparator ,args)))

    (define (good-comparator? comparator)
      (amap-implementation-supports-comparator? name comparator))

    (define (good-args? args)
      (amap-implementation-supports-args? name args))

    (define (run-make comparator args)
      (apply make-amap comparator (massage-args args)))

    (define (massage-args args)
      (cons* name
	     'not-an-arg
	     '(not-an-arg)
	     "not-an-arg"
	     (subst-args args)))

    (define (test-good-args args expected)
      (assert-boolean= (good-args? (subst-args args)) expected
		       'expression `(good-args? ,name ,(subst-args args))))

    (let ((all-args (all-choices-of (amap-implementation-supported-args name))))
      (for-each (lambda (comparator)
		  (for-each (lambda (args)
			      (test-make comparator args))
			    all-args))
		test-key-comparators))
    (let-values (((good-comparators bad-comparators)
		  (partition (good-comparator-pred name)
			     test-key-comparators)))
      (let* ((good-args (good-choices name))
	     (bad-args
	      (lset-difference (lambda (a b)
				 (lset= equal? a b))
			       (all-choices)
			       good-args))
	     (bad-args (if keep-it-fast!? (take bad-args 256) bad-args)))
	(for-each (lambda (comparator)
		    (assert-true (good-comparator? comparator))
		    (for-each (lambda (args)
				(test-good-args args #t)
				(test-good comparator args))
			      good-args)
		    (for-each (lambda (args)
				(test-good-args args #f)
				(test-bad comparator args))
			      bad-args))
		  good-comparators)
	(for-each (lambda (comparator)
		    (assert-false (good-comparator? comparator))
		    (for-each (lambda (args)
				(test-good-args args #t)
				(test-bad comparator args))
			      good-args)
		    (for-each (lambda (args)
				(test-good-args args #f)
				(test-bad comparator args))
			      bad-args))
		  bad-comparators)))))

(define-test 'explicit-construction
  (map explicit-construction-tests (amap-implementation-names)))

(define-test 'implicit-construction
  (lambda ()

    (define (test-make comparator args impls)
	(if (null? impls)
	    (assert-error (lambda () (apply make-amap comparator args)))
	    (let ((impl (apply make-amap comparator args)))
	      (assert-memv (amap-implementation-name impl) impls
			   'expression `(make-amap ,comparator ,args)))))

    (define (supporting-impls comparator args)
      (filter (lambda (name)
		(and (amap-implementation-supports-comparator? name comparator)
		     (amap-implementation-supports-args? name args)))
              (amap-implementation-names)))

    (for-each
     (lambda (comp)
       (let ((max-expected
	      (let ((opt
		     (lambda (pred impl)
		       (if (pred comp) (list impl) '()))))
		`(alist ,@(opt comparator-hashable? 'hash-table)
			,@(opt comparator-ordered? 'red/black-tree)
			,@(opt uniform-list-comparator? 'trie)))))

	 (define (test-impls best . arg-lists)
	   (for-each (lambda (args)
		       (let ((impls (supporting-impls comp args)))
			 (assert-lset= eq?
				       impls
				       (lset-intersection eq? best max-expected)
				       'expression
				       `(supporting-impls ,comp ,args))
			 (test-make comp args impls)))
		     arg-lists))

	 (test-impls '(alist hash-table red/black-tree trie)
		     '())

	 (test-impls '(hash-table)
		     '(weak-values)
		     '(weak-keys weak-values)
		     '(ephemeral-keys)
		     '(ephemeral-values)
		     '(ephemeral-keys ephemeral-values)
		     '(7)
		     '(amortized-constant-time))

	 (test-impls '(red/black-tree)
		     '(log-time)
		     '(ordered-by-key))

	 (test-impls '(alist)
		     '(linear-time))

	 (test-impls '(alist hash-table) '(weak-keys))
	 (test-impls '(hash-table red/black-tree) '(sublinear-time))

	 ;; Illegal
	 (test-impls '()
		     '(weak-keys ephemeral-keys)
		     '(weak-values ephemeral-values))

	 ;; Not supported but not illegal
	 (test-impls '()
		     '(weak-keys ephemeral-values)
		     '(ephemeral-keys weak-values))))
     test-key-comparators)))

(define (empty-nondestructive-tests impl-name)
  (lambda ()
    (let ((amap (make-amap test-key-comparator impl-name)))
      (assert-true (amap=? test-value-comparator amap amap))
      (assert-true (amap-mutable? amap))
      (assert-eqv (amap-find (lambda (key value)
			       (declare (ignore key value))
			       #t)
			     amap
			     (lambda () -1))
		  -1)
      (assert-true (amap-empty? (amap-map cons amap)))
      (amap-map! (lambda (key value)
		   (error "procedure called:" key value))
		 amap)
      (amap-prune! (lambda (key value)
		     (error "procedure called:" key value))
		   amap)
      (amap-assertions amap '())
      (amap-copy-assertions amap '()))))

(define-test 'empty-nondestructive-tests
  (map empty-nondestructive-tests amap-types))

(define (simple-tests impl-name)
  (lambda ()
    (let ((amap (make-amap test-key-comparator impl-name))
	  (alist))
      (for-each (lambda (p)
		  (amap-set! amap (car p) (cdr p)))
		test-alist)
      (set! alist test-alist)
      (amap-assertions amap alist)
      (amap-copy-assertions amap alist)

      (for-each (lambda (p)
		  (amap-set! amap (car p) (cdr p)))
		test-reversed-alist)
      (set! alist test-reversed-alist)
      (amap-assertions amap alist)

      (for-each (lambda (key)
		  (amap-update! amap key test-map-proc))
		test-keys)
      (set! alist
	    (map (lambda (p)
		   (cons (car p)
			 (test-map-proc (cdr p))))
		 alist))
      (amap-assertions amap alist)

      (amap-prune! test-pred amap)
      (set! alist
	    (remove (lambda (p)
		      (test-pred (car p) (cdr p)))
		    alist))
      (amap-assertions amap alist)

      (amap-map! test-map!-proc amap)
      (set! alist
	    (map (lambda (p)
		   (cons (car p)
			 (test-map!-proc (car p) (cdr p))))
		 alist))
      (amap-assertions amap alist))))

(define-test 'simple-tests
  (map simple-tests amap-types))

(define (amap-assertions amap alist)
  (assert-boolean= (amap-empty? amap) (null? alist))
  (assert-= (amap-size amap) (length alist))
  (assert-lset= equal? (amap->alist amap) alist)
  (assert-lset= eqv? (amap-keys amap) (map car alist))
  (assert-lset= eqv? (amap-values amap) (map cdr alist))
  (let-values (((keys values) (amap-entries amap)))
    (assert-lset= eqv? keys (map car alist))
    (assert-lset= eqv? values (map cdr alist)))
  (assert-= (amap-count test-pred amap)
	    (count (lambda (p)
		     (test-pred (car p) (cdr p)))
		   alist))
  (assert-lset= equal?
		(amap-fold test-kons test-knil amap)
		(alist-fold test-kons test-knil alist))
  (assert-lset= equal?
		(amap-map->list xcons amap)
		(map (lambda (p)
		       (xcons (car p) (cdr p)))
		     alist))
  (let ((n1 0)
	(n2 0))
    (amap-for-each (lambda (key value)
		     (declare (ignore value))
		     (set! n1 (+ key n1))
		     unspecific)
		   amap)
    (for-each (lambda (p)
		(set! n2 (+ (car p) n2))
		unspecific)
	      alist)
    (assert-= n1 n2))
  (for-each (lambda (key)
	      (let ((p (assv key alist)))
		(if p
		    (let ((value (cdr p)))
		      (assert-true (amap-contains? amap key))
		      (assert-eqv (amap-ref amap key) value)
		      (assert-eqv (amap-ref amap key fail) value)
		      (assert-eqv (amap-ref amap key fail succeed)
				  (succeed value))
		      (assert-eqv (amap-ref/default amap key (fail)) value))
		    (begin
		      (assert-false (amap-contains? amap key))
		      (assert-error (lambda () (amap-ref amap key)))
		      (assert-eqv (amap-ref amap key fail) (fail))
		      (assert-eqv (amap-ref amap key fail succeed) (fail))
		      (assert-eqv (amap-ref/default amap key (fail)) (fail))))))
	    test-keys))

(define (amap-copy-assertions amap alist)
  (let ((amap* (amap-copy amap)))
    (amap-assertions amap* alist)
    (assert-true (amap=? test-value-comparator amap amap*)))
  (let ((amap* (amap-empty-copy amap)))
    (amap-assertions amap* '())
    (assert-boolean= (amap=? test-value-comparator amap amap*)
		     (null? alist)))
  (let ((amap*
	 (alist->amap alist
		      test-key-comparator
		      (amap-implementation-name amap))))
    (amap-assertions amap* alist)
    (assert-true (amap=? test-value-comparator amap amap*)))
  (amap-assertions (amap-map test-map-proc amap)
		   (map (lambda (p)
			  (cons (car p)
				(test-map-proc (cdr p))))
			alist)))