This file is indexed.

/usr/share/scheme48-1.9/big/tlc-table-check.scm is in scheme48 1.9-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
; Part of Scheme 48 1.9.  See file COPYING for notices and license.

; Authors: Marcus Crestani, Robert Ransom

(define-test-suite tlc-table-tests)
(define-test-suite tlc-table-weak-tests)
(define-test-suite tlc-table-string-tests)

;;; most of the test cases are adapted from Eric Knauel's test cases
;;; he wrote for his id-tables

(define max-table-size 1023)
(define table-step 23)
(define min-collect-times 2)
(define max-collect-times 5)

;;; helper functions

(define verbose-output? #f)

(define (msg . args)
  (if verbose-output?
      (begin
	(display (apply format args))
	(newline))))

(define (collect-n-times n)
  (msg "Doing ~a garbage collections" n)
  (do-ec (:range k 1 n) (collect)))

(define (random-number a b)
  (+ a (random-integer b)))

(define (collect-random-times)
  (do-ec (:range k 0 (random-number 5))
	 (collect)))

(define (random-numbers n a b)
  (list-ec (:range i 1 n) (random-number a b)))

(define (random-subset lst)
  (let ((len (length lst)))
    (list-ec (:list k (random-numbers (random-number 1 len) 0 (- len 1)))
      (list-ref lst k))))

(define-record-type rec :rec
  (really-make-rec a b)
  rec?
  (a rec-a set-rec-a!)
  (b rec-b))

(define (make-rec)
  (really-make-rec (random-value-from-set some-values)
		   (random-value-from-set some-values)))

(define some-values
  (list (lambda () (cons 1 2))
	(lambda () 23)
	(lambda () #t)
	(lambda () #f)
	(lambda () 23.42)
	(lambda () #\a)
	(lambda () 'symbol)
	(lambda () (vector 1 2 3))
	(lambda () (lambda (x) x))
	(lambda () (current-output-port))
	(lambda () "Uns ist in alten maeren wunders vil geseit")
	(lambda () (make-rec))))

(define some-other-values
  (list (lambda () (cons 23 42))
	(lambda () 42)
	(lambda () #t)
	(lambda () #f)
	(lambda () 42.23)
	(lambda () #\a)
	(lambda () 'symbol)
	(lambda () (vector 11 12 13))
	(lambda () (lambda (y) y))
	(lambda () (current-output-port))
	(lambda () "Reise, reise, levt das Kottchen, zurrt, zurrt, Hängematten")
	(lambda () (make-rec))))

(define (random-value-from-set set)
  (let ((max-index (- (length set) 1)))
    ((list-ref set (random-integer max-index)))))

(define (random-value)
  (random-value-from-set some-values))

(define (random-other-value)
  (random-value-from-set some-other-values))

;;; tests

;; very basic test
(define-test-case constructor-predicate tlc-table-tests
  (check-that
   (tlc-table? (make-tlc-table 23))
   (is-true)))

;; create empty tables
(define-test-case empty-tables tlc-table-tests
  (do-ec
   (:range size 1 max-table-size 64)
   (check-that
    (tlc-table? (make-tlc-table size))
    (is-true))))

;; create empty tables and collect
(define-test-case empty-tables-collect tlc-table-tests
  (do-ec
    (:range size 1 max-table-size 64)
    (let ((t (make-tlc-table size)))
      (collect)
      (check-that (tlc-table? t) (is-true)))))

;; create empty tables and collect n times
(define-test-case empty-tables-collect-n-times tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let ((t (make-tlc-table size)))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (check-that (tlc-table? t) (is-true)))))

;; basic test for set!, contains?, and ref
(define-test-case set/ref tlc-table-tests
  (let ((t (make-tlc-table 23))
	(obj (cons 1 2)))
    (tlc-table-set! t obj obj)
    (let ((res-1 (tlc-table-ref t obj #f))
	  (res-2 (tlc-table-ref t (cons 1 2) #f)))
      (check-that (tlc-table-contains? t obj) (is-true))
      (check-that (tlc-table-contains? t (cons 1 2)) (is-false))
      (check res-1 => obj)
      (check-that res-2 (is-false)))))

;; create empty table and call ref a few times
(define-test-case empty/ref tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let ((t (make-tlc-table size)))
     (do-ec
      (:list v some-values)
      (begin
	(check-that (tlc-table-contains? t (v)) (is-false))
	(check-that (tlc-table-ref t (v) #f) (is-false)))))))

;; create table and fill it a bit
(define-test-case empty/set tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (check-that
    (let ((t (make-tlc-table size)))
      (do-ec
       (:list v some-values)
       (tlc-table-set! t (v) (cons 23 42)))
	#t)
    (is-true))))

;; create a table, fill it, and read entries
(define-test-case many/set/ref tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let ((t (make-tlc-table size))
	 (values (map (lambda (v) (v)) some-values)))
     (do-ec
      (:list v values)
      (begin
	(tlc-table-set! t v v)
	(check-that (tlc-table-contains? t v) (is-true))
	(check (tlc-table-ref t v #f) => v))))))

;; update one entry multiple times in a row
(define-test-case update-often tlc-table-tests
  (let ((t (make-tlc-table 23))
	(obj (cons 23 42)))
    (do-ec
     (:range i 1 1024)
     (tlc-table-set! t obj i))
    (tlc-table-set! t obj obj)
    (check-that (tlc-table-contains? t obj) (is-true))
    (check (tlc-table-ref t obj #f) => obj)))

;; one collection to ref the heap ready for the tests with many
;; collections
(collect)

;; create a table with one entry, collect, find it again
(define-test-case set/collect/ref tlc-table-tests
  (let ((table (make-tlc-table 23))
	(obj (cons 23 42))
	(val (cons 65 99)))
    (tlc-table-set! table obj val)
    (collect)
    (check (tlc-table-ref table obj #f) => val)))

;; fill a table with objects and retrieve them after one collection
(define-test-case set-n/collect/ref-n tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n))))
     (do-ec
      (:list o objs)
      (tlc-table-set! table o o))
     (collect)
     (do-ec
      (:list o objs)
      (check(tlc-table-ref table o #f) => o)))))

;; fill a table with objects and retrieve them after n collections
(define-test-case set-n/collect-n/ref-n tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n))))
     (do-ec
      (:list o objs)
      (tlc-table-set! table o o))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o objs)
      (check (tlc-table-ref table o #f) => o)))))

;; create a table with no entry, delete, and try to find it
(define-test-case delete/ref tlc-table-tests
  (let ((table (make-tlc-table 23))
	(obj (cons 23 42))
	(val (cons 65 99)))
    (check-that
     (tlc-table-delete! table obj #f)
     (is-false))
    (check-that 
     (tlc-table-ref table obj #f)
     (is-false))))

;; create a table with one entry, delete, and try to find it again
(define-test-case set/delete/ref tlc-table-tests
  (let ((table (make-tlc-table 23))
	(obj (cons 23 42))
	(val (cons 65 99)))
    (tlc-table-set! table obj val)
    (check-that
     (tlc-table-delete! table obj #f)
     (opposite (is-false)))
    (check-that
     (tlc-table-ref table obj #f)
     (is-false))))

;; create a table with some entries that all go into the same bucket,
;; delete them, and try to find them again
(define-test-case set-n-in-one-bucket/delete-n/ref-n tlc-table-tests
  (let ((table (make-tlc-table 1))
	(val (cons 65 99)))
    (do-ec
     (:range n 1 23)
     (tlc-table-set! table n val))
    (do-ec
     (:range n 1 23)
     (check (tlc-table-ref table n #f) => val))
    (do-ec
     (:range n 1 23)
     (check-that
      (tlc-table-delete! table n #f)
      (opposite (is-false))))
    (do-ec
     (:range n 1 23)
     (check (tlc-table-ref table n #f) => #f))))

;; create a table with one entry, collect, delete, and try to find it
;; again
(define-test-case set/collect/delete/ref tlc-table-tests
  (let ((table (make-tlc-table 23))
	(obj (cons 23 42))
	(val (cons 65 99)))
    (tlc-table-set! table obj val)
    (collect)
    (check-that
     (tlc-table-delete! table obj #f)
     (opposite (is-false)))
    (check-that
     (tlc-table-ref table obj #f)
     (is-false))))

;; fill a table with objects, delete some, and retrieve them
(define-test-case set-n/delete-n/ref-n tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (delobjs (list-ec (: i n) (cons (+ i max-table-size) n))))
     (do-ec
      (:list o delobjs)
      (tlc-table-set! table o o))
     (do-ec
      (:list o objs)
      (tlc-table-set! table o o))
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-delete! table o #f)
       (opposite (is-false))))
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-ref table o #f)
       (is-false)))
     (do-ec
      (:list o objs)
      (check (tlc-table-ref table o #f) => o)))))

;; fill a table with objects, delete some, and retrieve them after one
;; collection
(define-test-case set-n/collect/delete-n/ref-n tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (delobjs (list-ec (: i n) (cons (+ i max-table-size) n))))
     (do-ec
      (:list o delobjs)
      (tlc-table-set! table o o))
     (collect)
     (do-ec
      (:list o objs)
      (tlc-table-set! table o o))
     (collect)
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-delete! table o #f)
       (opposite (is-false))))
     (collect)
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-ref table o #f)
       (is-false)))
     (do-ec
      (:list o objs)
      (check (tlc-table-ref table o #f) => o)))))

;; fill a table with objects, delete some, and retrieve the others
;; after n collections
(define-test-case set-n/collect-n/delete-n/ref-n tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (delobjs (list-ec (: i n) (cons i n))))
     (do-ec
      (:list o delobjs)
      (tlc-table-set! table o o))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o objs)
      (tlc-table-set! table o o))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-delete! table o #f)
       (opposite (is-false))))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-ref table o #f)
       (is-false)))
     (do-ec
      (:list o objs)
      (check (tlc-table-ref table o #f) => o)))))

;; helper function for checking entries: check if the order and number
;; of keys and values returned by tlc-table-entries is correct.
(define (check-entries t n)
  (call-with-values
      (lambda ()
	(tlc-table-entries t))
    (lambda (keys values)
      (for-each
       (lambda (key value)
	 (check (tlc-table-ref t key #f)
		=> value))
       (vector->list keys)
       (vector->list values))
      (check 
       (vector-length keys)
       => (vector-length (tlc-table-keys t)))
      (check 
       (vector-length keys)
       => (tlc-table-size t))
      (check (tlc-table-size t) => n))))

;; check entries for empty tables
(define-test-case empty-entries tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let ((t (make-tlc-table size)))
     (check-entries t 0))))

;; check entries for one-element tables
(define-test-case one-element-entries tlc-table-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let ((t (make-tlc-table size))
	 (p (cons 23 42)))
     (tlc-table-set! t p p)
     (check-entries t 1))))

;; check entries for filled tables (unmovable keys)
(define-test-case set-entries/unmovable tlc-table-tests
  (do-ec
   (:range size 1 (quotient max-table-size 3) table-step)
   (let ((t (make-tlc-table size)))
     (check-entries t 0)
     (do-ec
      (:range i 1 (* 3 size))
      (begin
	(tlc-table-set! t i (cons i i))
	(check-entries t i))))))

;; check entries with set and delete (unmovable keys)
(define-test-case set-delete-entries/unmovable tlc-table-tests
  (do-ec
   (:range size 1 (quotient max-table-size 3) table-step)
   (let ((t (make-tlc-table size)))
     (check-entries t 0)
     (do-ec
      (:range i 1 (* 3 size))
      (begin
	(tlc-table-set! t i (cons i i))
	(tlc-table-set! t (+ i (* 3 size)) (cons i i))
	(check-that (tlc-table-delete! t i #f)
		    (opposite (is-false)))
	(check-entries t i))))))

;; check entries with set, delete, and clear (unmovable keys)
(define-test-case set-delete-clear-entries/unmovable tlc-table-tests
  (do-ec
   (:range size 1 (quotient max-table-size 3) table-step)
   (let ((t (make-tlc-table size)))
     (check-entries t 0)
     (do-ec
      (:range i 1 (* 3 size))
      (begin
	(tlc-table-clear! t)
	(check (tlc-table-size t) => 0)
	(tlc-table-set! t i (cons i i))
	(tlc-table-set! t (+ i (* 3 size)) (cons i i))
	(check-that (tlc-table-delete! t i #f)
		    (opposite (is-false)))
	(check-entries t 1))))))

;; check entries for filled and cleared tables
(define-test-case set-entries tlc-table-tests
  (do-ec
   (:range size 1 (quotient max-table-size 3) table-step)
   (let ((t (make-tlc-table size)))
     (tlc-table-clear! t)
     (check-entries t 0)
     (do-ec
      (:range i 1 (* 3 size))
      (begin
	(tlc-table-set! t (cons i i) (cons i i))
	(check-entries t i))))))

;; fill a table with objects, delete some, and retrieve the others
;; after n collections
(define-test-case set-collect-delete-entries tlc-table-tests
  (do-ec
   (:range size 1 (quotient max-table-size 3) table-step)
   (let* ((t (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (delobjs (list-ec (: i n) (cons i n))))
     (do-ec
      (:list o delobjs)
      (tlc-table-set! t o o))
     (check-entries t n)
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o objs)
      (tlc-table-set! t o o))
     (check-entries t (* 2 n))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-delete! t o #f)
       (opposite (is-false))))
     (check-entries t n)
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o delobjs)
      (check-that
       (tlc-table-ref t o #f)
       (is-false)))
     (check-entries t n)
     (do-ec
      (:list o objs)
      (check (tlc-table-ref t o #f) => o))
     (check-entries t n))))

;; WEAK TESTS
;; create a table with one weak entry and find it again
(define-test-case weak-set/ref tlc-table-weak-tests
  (let* ((table (make-tlc-table 23))
	 (obj (cons 23 42))
	 (wp (make-weak-pointer obj))
	 (val (cons 65 99)))
    (tlc-table-set! table wp val)
    (check (tlc-table-ref table wp #f) => val)))

;; create a table with one weak entry, collect and find it again
(define-test-case weak-set/collect/ref tlc-table-weak-tests
  (let* ((table (make-tlc-table 23))
	 (obj (cons 23 42))
	 (wp (make-weak-pointer obj))
	 (val (cons 65 99)))
    (tlc-table-set! table wp val)
    (collect)
    (check (tlc-table-ref table wp #f) => val)))

;; fill a table with weak objects and retrieve them after one
;; collection
(define-test-case weak-set-n/collect/ref-n tlc-table-weak-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (wobjs (map make-weak-pointer objs)))
     (do-ec
      (:list o wobjs)
      (tlc-table-set! table o o))
     (collect)
     (do-ec
      (:list o wobjs)
      (check (tlc-table-ref table o #f) => o)))))

;; fill a table with weak objects and retrieve them after n
;; collections
(define-test-case weak-set-n/collect-n/ref-n tlc-table-weak-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (wobjs (map make-weak-pointer objs)))
     (do-ec
      (:list o wobjs)
      (tlc-table-set! table o o))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o wobjs)
      (check (tlc-table-ref table o #f) => o)))))

;; create a table with no weak entry, delete, and try to find it
(define-test-case weak-delete/ref tlc-table-weak-tests
  (let* ((table (make-tlc-table 23))
	 (obj (cons 23 42))
	 (wobj (make-weak-pointer obj))
	 (val (cons 65 99)))
    (check-that
     (tlc-table-delete! table wobj #f)
     (is-false))
    (check-that
     (tlc-table-ref table wobj #f)
     (is-false))))

;; create a table with one weak entry, delete, and try to find it
;; again
(define-test-case weak-set/delete/ref tlc-table-weak-tests
  (let* ((table (make-tlc-table 23))
	 (obj (cons 23 42))
	 (wobj (make-weak-pointer obj))
	 (val (cons 65 99)))
    (tlc-table-set! table wobj val)
    (check-that
     (tlc-table-delete! table wobj #f)
     (opposite (is-false)))
    (check-that
     (tlc-table-ref table wobj #f)
     (is-false))))

;; create a table with one weak entry, collect, delete, and try to
;; find it again
(define-test-case weak-set/collect/delete/ref tlc-table-weak-tests
  (let* ((table (make-tlc-table 23))
	 (obj (cons 23 42))
	 (wobj (make-weak-pointer obj))
	 (val (cons 65 99)))
    (tlc-table-set! table wobj val)
    (collect)
    (check-that
     (tlc-table-delete! table wobj #f)
     (opposite (is-false)))
    (check-that
     (tlc-table-ref table wobj #f)
     (is-false))))

;; fill a table with weak objects, delete some, and retrieve the
;; others after one collection
(define-test-case weak-set-n/collect/delete-n/ref-n tlc-table-weak-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (wobjs (map make-weak-pointer objs))
	  (delobjs (list-ec (: i n) (cons (+ i 100) 42)))
	  (delwobjs (map make-weak-pointer delobjs)))
     (do-ec
      (:list o wobjs)
      (tlc-table-set! table o o))
     (do-ec
      (:list o delwobjs)
      (tlc-table-set! table o o))
     (do-ec
      (:list o delwobjs)
      (check (tlc-table-ref table o #f) => o))
     (do-ec
      (:list o wobjs)
      (check (tlc-table-ref table o #f) => o))
     (collect)
     (do-ec
      (:list o delwobjs)
      (check-that
       (tlc-table-delete! table o #f)
       (opposite (is-false))))
     (do-ec
      (:list o delwobjs)
      (check-that
       (tlc-table-ref table o #f)
       (is-false)))
     (do-ec
      (:list o wobjs)
      (check (tlc-table-ref table o #f) => o)))))

;; fill a table with weak objects, delete some, and retrieve the
;; others after n collections
(define-test-case weak-set-n/collect-n/delete-n/ref-n tlc-table-weak-tests
  (do-ec
   (:range size 1 max-table-size table-step)
   (let* ((table (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (wobjs (map make-weak-pointer objs))
	  (delobjs (list-ec (: i n) (cons (+ i 100) 42)))
	  (delwobjs (map make-weak-pointer delobjs)))
     (do-ec
      (:list o wobjs)
      (tlc-table-set! table o o))
     (do-ec
      (:list o delwobjs)
      (tlc-table-set! table o o))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o delwobjs)
      (check-that
       (tlc-table-delete! table o #f)
       (opposite (is-false))))
     (do-ec
      (:list o wobjs)
      (check (tlc-table-ref table o #f) => o))
     (do-ec
      (:list o delwobjs)
      (check-that
       (tlc-table-ref table o #f)
       (is-false))))))

;; fill a table with weak objects, delete some, and
;; retrieve the others after n collections
(define-test-case weak-set-collect-delete-entries tlc-table-weak-tests
  (do-ec
   (:range size 1 (quotient max-table-size 3) table-step)
   (let* ((t (make-tlc-table size))
	  (n (* 3 size))
	  (objs (list-ec (: i n) (cons i n)))
	  (delobjs (list-ec (: i n) (cons i n)))
	  (wobjs (map make-weak-pointer objs))
	  (delwobjs (map make-weak-pointer delobjs)))
     (do-ec
      (:list o delwobjs)
      (tlc-table-set! t o o))
     (check-entries t n)
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o wobjs)
      (tlc-table-set! t o o))
     (check-entries t (* 2 n))
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o delwobjs)
      (check-that
       (tlc-table-delete! t o #f)
       (opposite (is-false))))
     (check-entries t n)
     (collect-n-times (random-number min-collect-times max-collect-times))
     (do-ec
      (:list o delwobjs)
      (check-that
       (tlc-table-ref t o #f)
       (is-false)))
     (check-entries t n)
     (do-ec
      (:list o wobjs)
      (check (tlc-table-ref t o #f) => o))
     (check-entries t n))))


;; string table with strings as keys
(define-test-case string-table tlc-table-string-tests
  (let* ((t (make-non-default-tlc-table string-hash string= 23 #f)))
    (tlc-table-set! t "Key" "Value")
    (check (tlc-table-ref t "Key" #f) => "Value")
    (check-that (tlc-table-ref t "Value" #f) (is-false))))


; eqv? table with various objects as keys
(define-test-case eqv-table tlc-table-tests
  (let ((t (make-eqv-tlc-table 23))
        (ns '(1 10 100 1000 10000 100000)) ; must not contain 0
        (ms '(1 10 100 1000)))             ; (exp 1000) is infinite
    ; initialize table
    (let-syntax ((stuff-table
                  (syntax-rules ()
                    ((stuff-table xs var expr)
                     (for-each (lambda (x)
                                 (tlc-table-set! t (let ((var x)) expr) x))
                               xs)))))
      (stuff-table ns n (expt 2 n))
      (stuff-table ns n (/ (expt 3 n) (expt 2 n)))
      (stuff-table ns n (+ (expt 2 n) (* 0+1i (expt 3 n))))
      (stuff-table ms n (exp n))
      (stuff-table ns n (exp (* n 0+1i)))
      (stuff-table ns n (make-string n #\x)))
    ; check its contents
    (let-syntax ((check-stuffed
                  (syntax-rules ()
                    ((check-stuffed xs var expr)
                     (for-each (lambda (x)
                                 (check
                                  (tlc-table-ref t (let ((var x)) expr) #f)
                                  => x))
                               xs))))
                 (check-not-stuffed
                  (syntax-rules ()
                    ((check-not-stuffed xs var expr)
                     (for-each (lambda (x)
                                 (check
                                  (tlc-table-ref t (let ((var x)) expr) #f)
                                  => #f))
                               xs)))))
      (check-stuffed ns n (expt 2 n))
      (check-stuffed ns n (/ (expt 3 n) (expt 2 n)))
      (check-stuffed ns n (+ (expt 2 n) (* 0+1i (expt 3 n))))
      (check-stuffed ms n (exp n))
      (check-stuffed ns n (exp (* n 0+1i)))
      (check-not-stuffed ns n (expt 3 n))
      (check-not-stuffed ns n (/ (expt 2 n) (expt 3 n)))
      (check-not-stuffed ns n (+ (expt 3 n) (* 0+1i (expt 2 n))))
      (check-not-stuffed ms n (exp (- n)))
      (check-not-stuffed ns n (exp (* n 0-1i)))
      (check-not-stuffed ns n (make-string n #\x))))) ; not eqv? to the strings above


(define-test-case has-tconc-queue? tlc-table-tests
  (check
   (tlc-table-has-tconc-queue? (make-eq-tlc-table 23))
   => #t)
  (check
   (tlc-table-has-tconc-queue? (make-eqv-tlc-table 23))
   => #t)
  (check
   (tlc-table-has-tconc-queue?
    (make-non-default-tlc-table string-hash string=? 23 #f))
   => #f))