aboutsummaryrefslogtreecommitdiff
path: root/core/test/com/google/inject/internal/util/MapMakerTestSuite.java
blob: e04e5d90fa82e1f199b2d2cc10a7c040c85d82bd (plain)
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
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
/*
 * Copyright (C) 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.inject.internal.util;

import com.google.inject.internal.util.ComputationException;
import com.google.inject.internal.util.CustomConcurrentHashMap.Impl;
import com.google.inject.internal.util.ExpirationTimer;
import com.google.inject.internal.util.MapMaker;
import com.google.inject.internal.util.Maps;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit tests for MapMaker. Also less directly serves as the test suite for
 * CustomConcurrentHashMap.
 */
public class MapMakerTestSuite extends TestCase {

  public static Test suite() {
    TestSuite suite = new TestSuite();

    suite.addTestSuite(MakerTest.class);
    suite.addTestSuite(RecursiveComputationTest.class);
    suite.addTestSuite(ReferenceMapTest.class);
    suite.addTestSuite(ComputingTest.class);
    suite.addTest(ReferenceCombinationTestSuite.suite());
    suite.addTestSuite(ExpiringReferenceMapTest.class);
    suite.addTestSuite(ExpiringComputingReferenceMapTest.class);

    return suite;
  }

  public static class MakerTest extends TestCase {
    public void testSizingDefaults() {
      Impl<?, ?, ?> map = makeCustomMap(new MapMaker());
      assertEquals(16, map.segments.length); // concurrency level
      assertEquals(1, map.segments[0].table.length()); // capacity / conc level
      assertEquals(0.75f, map.loadFactor);
    }

    public void testInitialCapacity_small() {
      MapMaker maker = new MapMaker().initialCapacity(17);
      Impl<?, ?, ?> map = makeCustomMap(maker);

      assertEquals(2, map.segments[0].table.length());
    }

    public void testInitialCapacity_smallest() {
      MapMaker maker = new MapMaker().initialCapacity(0);
      Impl<?, ?, ?> map = makeCustomMap(maker);

      // 1 is as low as it goes, not 0. it feels dirty to know this/test this.
      assertEquals(1, map.segments[0].table.length());
    }

    public void testInitialCapacity_large() {
      new MapMaker().initialCapacity(Integer.MAX_VALUE);
      // that the maker didn't blow up is enough;
      // don't actually create this monster!
    }

    public void testInitialCapacity_negative() {
      MapMaker maker = new MapMaker();
      try {
        maker.initialCapacity(-1);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    }

    // TODO: enable when ready
    public void xtestInitialCapacity_setTwice() {
      MapMaker maker = new MapMaker().initialCapacity(16);
      try {
        // even to the same value is not allowed
        maker.initialCapacity(16);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    }

    public void testLoadFactor_small() {
      MapMaker maker = new MapMaker().loadFactor(Float.MIN_VALUE);
      Impl<?, ?, ?> map = makeCustomMap(maker);
      assertEquals(Float.MIN_VALUE, map.loadFactor);

      // has no other effect until we add an entry (which would be bad)
      assertEquals(1, map.segments[0].table.length());
    }

    public void testLoadFactor_large() {
      MapMaker maker = new MapMaker().loadFactor(Float.MAX_VALUE);
      Impl<?, ?, ?> map = makeCustomMap(maker);
      assertEquals(Float.MAX_VALUE, map.loadFactor);

      // these tables will never grow... we could add a ton of entries to
      // check that if we wanted to.
      assertEquals(1, map.segments[0].table.length());
    }

    public void testLoadFactor_zero() {
      MapMaker maker = new MapMaker();
      try {
        maker.loadFactor(0);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    }

    // TODO: enable when ready
    public void xtestLoadFactor_setTwice() {
      MapMaker maker = new MapMaker().loadFactor(0.75f);
      try {
        // even to the same value is not allowed
        maker.loadFactor(0.75f);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    }

    public void testConcurrencyLevel_small() {
      MapMaker maker = new MapMaker().concurrencyLevel(1);
      Impl<?, ?, ?> map = makeCustomMap(maker);
      assertEquals(1, map.segments.length);
    }

    public void testConcurrencyLevel_large() {
      new MapMaker().concurrencyLevel(Integer.MAX_VALUE);
      // don't actually build this beast
    }

    public void testConcurrencyLevel_zero() {
      MapMaker maker = new MapMaker();
      try {
        maker.concurrencyLevel(0);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    }

    // TODO: enable when ready
    public void xtestConcurrencyLevel_setTwice() {
      MapMaker maker = new MapMaker().concurrencyLevel(16);
      try {
        // even to the same value is not allowed
        maker.concurrencyLevel(16);
        fail();
      } catch (IllegalArgumentException expected) {
      }
    }

    public void testKeyStrengthSetTwice() {
      MapMaker maker1 = new MapMaker().weakKeys();
      try {
        maker1.weakKeys();
        fail();
      } catch (IllegalStateException expected) {
      }

      MapMaker maker2 = new MapMaker().softKeys();
      try {
        maker2.softKeys();
        fail();
      } catch (IllegalStateException expected) {
      }

      MapMaker maker3 = new MapMaker().weakKeys();
      try {
        maker3.softKeys();
        fail();
      } catch (IllegalStateException expected) {
      }
    }

    public void testValueStrengthSetTwice() {
      MapMaker maker1 = new MapMaker().weakValues();
      try {
        maker1.weakValues();
        fail();
      } catch (IllegalStateException expected) {
      }

      MapMaker maker2 = new MapMaker().softValues();
      try {
        maker2.softValues();
        fail();
      } catch (IllegalStateException expected) {
      }

      MapMaker maker3 = new MapMaker().weakValues();
      try {
        maker3.softValues();
        fail();
      } catch (IllegalStateException expected) {
      }
    }

    public void testExpiration_small() {
      new MapMaker().expiration(1, NANOSECONDS);
      // well, it didn't blow up.
    }

    public void testExpiration_setTwice() {
      MapMaker maker = new MapMaker().expiration(3600, SECONDS);
      try {
        // even to the same value is not allowed
        maker.expiration(3600, SECONDS);
        fail();
      } catch (IllegalStateException expected) {
      }
    }

    public void testReturnsPlainConcurrentHashMapWhenPossible() {
      Map<?, ?> map = new MapMaker()
          .concurrencyLevel(5)
          .loadFactor(0.5f)
          .initialCapacity(5)
          .makeMap();
      assertTrue(map instanceof ConcurrentHashMap);
    }

    private static Impl<?, ?, ?> makeCustomMap(MapMaker maker) {
      // Use makeComputingMap() to force it to return CCHM.Impl, not
      // ConcurrentHashMap.
      return (Impl<?, ?, ?>) maker.makeComputingMap(new Function<Object, Object>() {
        public Object apply(@Nullable Object from) {
          return from;
        }
      });
    }
  }

  public static class RecursiveComputationTest extends TestCase {

    Function<Integer, String> recursiveComputer
        = new Function<Integer, String>() {
      public String apply(Integer key) {
        if (key > 0) {
          return key + ", " + recursiveMap.get(key - 1);
        } else {
          return "0";
        }
      }
    };

    ConcurrentMap<Integer, String> recursiveMap = new MapMaker()
        .weakKeys()
        .weakValues()
        .makeComputingMap(recursiveComputer);

    public void testRecursiveComputation() {
      assertEquals("3, 2, 1, 0", recursiveMap.get(3));
    }
  }

  /**
   * Tests for basic map functionality.
   */
  public static class ReferenceMapTest extends TestCase {

    public void testValueCleanupWithWeakKey() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().weakKeys().makeMap();
      map.put(new Object(), new Object());
      assertCleanup(map);
    }

    public void testValueCleanupWithSoftKey() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().softKeys().makeMap();
      map.put(new Object(), new Object());
      assertCleanup(map);
    }

    public void testKeyCleanupWithWeakValue() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().weakValues().makeMap();
      map.put(new Object(), new Object());
      assertCleanup(map);
    }

    public void testKeyCleanupWithSoftValue() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().softValues().makeMap();
      map.put(new Object(), new Object());
      assertCleanup(map);
    }

    public void testInternedValueCleanupWithWeakKey() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().weakKeys().makeMap();
      map.put(new Integer(5), "foo");
      assertCleanup(map);
    }

    public void testInternedValueCleanupWithSoftKey() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().softKeys().makeMap();
      map.put(new Integer(5), "foo");
      assertCleanup(map);
    }

    public void testInternedKeyCleanupWithWeakValue() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().weakValues().makeMap();
      map.put(5, new String("foo"));
      assertCleanup(map);
    }

    public void testInternedKeyCleanupWithSoftValue() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().softValues().makeMap();
      map.put(5, new String("foo"));
      assertCleanup(map);
    }

    public void testReplace() {
      ConcurrentMap<Object, Object> map =
          new MapMaker().makeMap();
      assertNull(map.replace("one", 1));
    }

    private static void assertCleanup(ConcurrentMap<?, ?> map) {
      assertEquals(1, map.size());

      // wait up to 5s
      byte[] filler = new byte[1024];
      for (int i = 0; i < 500; i++) {
        System.gc();
        if (map.isEmpty()) {
          return;
        }
        try {
          Thread.sleep(10);
        } catch (InterruptedException e) { /* ignore */ }
        try {
          // Fill up heap so soft references get cleared.
          filler = new byte[filler.length * 2];
        } catch (OutOfMemoryError e) {}
      }

      fail();
    }

    public void testWeakKeyIdentityLookup() {
      ConcurrentMap<Integer, String> map =
          new MapMaker().weakKeys().makeMap();
      Integer key1 = new Integer(12357);
      Integer key2 = new Integer(12357);
      map.put(key1, "a");
      assertTrue(map.containsKey(key1));
      assertFalse(map.containsKey(key2));
    }

    public void testSoftKeyIdentityLookup() {
      ConcurrentMap<Integer, String> map =
          new MapMaker().softKeys().makeMap();
      Integer key1 = new Integer(12357);
      Integer key2 = new Integer(12357);
      map.put(key1, "a");
      assertTrue(map.containsKey(key1));
      assertFalse(map.containsKey(key2));
    }

    public void testWeakValueIdentityLookup() {
      ConcurrentMap<String, Integer> map =
          new MapMaker().weakValues().makeMap();
      Integer value1 = new Integer(12357);
      Integer value2 = new Integer(12357);
      map.put("a", value1);
      assertTrue(map.containsValue(value1));
      assertFalse(map.containsValue(value2));
    }

    public void testSoftValueIdentityLookup() {
      ConcurrentMap<String, Integer> map =
          new MapMaker().softValues().makeMap();
      Integer value1 = new Integer(12357);
      Integer value2 = new Integer(12357);
      map.put("a", value1);
      assertTrue(map.containsValue(value1));
      assertFalse(map.containsValue(value2));
    }

    public void testWeakKeyEntrySetRemove() {
      ConcurrentMap<Integer, String> map =
          new MapMaker().weakKeys().makeMap();
      Integer key1 = new Integer(12357);
      Integer key2 = new Integer(12357);
      map.put(key1, "a");
      assertFalse(map.entrySet().remove(Maps.immutableEntry(key2, "a")));
      assertEquals(1, map.size());
      assertTrue(map.entrySet().remove(Maps.immutableEntry(key1, "a")));
      assertEquals(0, map.size());
    }

    public void testEntrySetIteratorRemove() {
      ConcurrentMap<String, Integer> map =
          new MapMaker().makeMap();
      map.put("foo", 1);
      map.put("bar", 2);
      assertEquals(2, map.size());
      Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
      try {
        iterator.remove();
        fail();
      } catch (IllegalStateException expected) {}
      iterator.next();
      iterator.remove();
      assertEquals(1, map.size());
      try {
        iterator.remove();
        fail();
      } catch (IllegalStateException expected) {}
      iterator.next();
      iterator.remove();
      assertEquals(0, map.size());
    }
  }

  /**
   * Tests for computing functionality.
   */
  public static class ComputingTest extends TestCase {

    public void testComputerThatReturnsNull() {
      ConcurrentMap<Integer, String> map = new MapMaker()
          .makeComputingMap(new Function<Integer, String>() {
            public String apply(Integer key) {
              return null;
            }
          });
      try {
        map.get(1);
        fail();
      } catch (NullPointerException e) { /* expected */ }
    }

    public void testRecomputeAfterReclamation()
        throws InterruptedException {
      ConcurrentMap<Integer, String> map = new MapMaker()
          .weakValues()
          .makeComputingMap(new Function<Integer, String>() {
            @SuppressWarnings("RedundantStringConstructorCall")
            public String apply(Integer key) {
              return new String("one");
            }
          });

      for (int i = 0; i < 10; i++) {
        // The entry should get garbage collected and recomputed.
        assertEquals("on iteration " + i, "one", map.get(1));
        Thread.sleep(i);
        System.gc();
      }
    }

    public void testRuntimeException() {
      final RuntimeException e = new RuntimeException();

      ConcurrentMap<Object, Object> map = new MapMaker().makeComputingMap(
          new Function<Object, Object>() {
        public Object apply(Object from) {
          throw e;
        }
      });

      try {
        map.get(new Object());
        fail();
      } catch (ComputationException ce) {
        assertSame(e, ce.getCause());
      }
    }

    public void testSleepConcurrency() throws InterruptedException {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .weakKeys().weakValues().makeComputingMap(new SleepFunction());
      assertConcurrency(cache, false);
    }

    public void testBusyConcurrency() throws InterruptedException {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .weakKeys().weakValues().makeComputingMap(new BusyFunction());
      assertConcurrency(cache, false);
    }

    public void testFastConcurrency() throws InterruptedException {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .weakKeys().weakValues().makeComputingMap(new SomeFunction());
      assertConcurrency(cache, false);
    }

    public void testSleepCanonical() throws InterruptedException {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .softValues().makeComputingMap(new SleepFunction());
      assertConcurrency(cache, true);
    }

    public void testBusyCanonical() throws InterruptedException {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .softValues().makeComputingMap(new BusyFunction());
      assertConcurrency(cache, true);
    }

    public void testFastCanonical() throws InterruptedException {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .softValues().makeComputingMap(new SomeFunction());
      assertConcurrency(cache, true);
    }

    private static void assertConcurrency(
        final ConcurrentMap<String, Integer> cache,
        final boolean simulateAliasing) throws InterruptedException {
      final int n = 20;
      final CountDownLatch startSignal = new CountDownLatch(1);
      final CountDownLatch doneSignal = new CountDownLatch(n);
      for (int i = 0; i < n; i++) {
        new Thread() {
          @Override public void run() {
            try {
              startSignal.await();
              for (int j = 0; j < n; j++) {
                cache.get(simulateAliasing ? new String("foo") : "foo");
              }
              doneSignal.countDown();
            } catch (InterruptedException ignored) {}
          }
        }.start();
      }

      startSignal.countDown();
      doneSignal.await();
      assertEquals(Integer.valueOf(1), cache.get("foo"));
      assertEquals(Integer.valueOf(2), cache.get("bar"));
    }

    private static class SomeFunction implements Function<String, Integer> {
      private int numApplies = 0;
      public Integer apply(String s) {
        return ++numApplies;
      }
    }

    private static class SleepFunction implements Function<String, Integer> {
      private int numApplies = 0;
      public Integer apply(String s) {
        try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
        return ++numApplies;
      }
    }

    private static class BusyFunction implements Function<String, Integer> {
      private int numApplies = 0;
      public Integer apply(String s) {
        for (int i = 0; i < 1000; i++) {
          Math.sqrt(i);
        }
        return ++numApplies;
      }
    }
  }

  /**
   * Tests combinations of key and value reference types.
   */
  public static class ReferenceCombinationTestSuite extends TestCase {

    interface BuilderOption {
      void applyTo(MapMaker maker);
    }

    public static Test suite() {
      TestSuite suite = new TestSuite();

      BuilderOption[] keyOptions = {
        new BuilderOption() {
          public void applyTo(MapMaker maker) {
            // strong keys
          }
          @Override public String toString() {
            return "Strong";
          }
        },
        new BuilderOption() {
          public void applyTo(MapMaker maker) {
            maker.weakKeys();
          }
          @Override public String toString() {
            return "Weak";
          }
        },
        new BuilderOption() {
          public void applyTo(MapMaker maker) {
            maker.softKeys();
          }
          @Override public String toString() {
            return "Soft";
          }
        },
      };

      BuilderOption[] valueOptions = {
        new BuilderOption() {
          public void applyTo(MapMaker maker) {
            // strong values
          }
          @Override public String toString() {
            return "Strong";
          }
        },
        new BuilderOption() {
          public void applyTo(MapMaker maker) {
            maker.weakValues();
          }
          @Override public String toString() {
            return "Weak";
          }
        },
        new BuilderOption() {
          public void applyTo(MapMaker maker) {
            maker.softValues();
          }
          @Override public String toString() {
            return "Soft";
          }
        },
      };

      // create test cases for each key and value type.
      for (Method method : MapTest.class.getMethods()) {
        String name = method.getName();
        if (name.startsWith("test")) {
          for (BuilderOption keyOption : keyOptions) {
            for (BuilderOption valueOption : valueOptions) {
              suite.addTest(new MapTest(name, keyOption, valueOption));
            }
          }
        }
      }

      return suite;
    }

    public static class MapTest extends TestCase {

      final BuilderOption keyOption;
      final BuilderOption valueOption;

      public MapTest(String name,
          BuilderOption keyOption,
          BuilderOption valueOption) {
        super(name);
        this.keyOption = keyOption;
        this.valueOption = valueOption;
      }

      @Override public String getName() {
        return super.getName() + "For" + keyOption + valueOption;
      }

      MapMaker newBuilder() {
        MapMaker maker = new MapMaker();
        keyOption.applyTo(maker);
        valueOption.applyTo(maker);
        return maker;
      }

      <K, V> ConcurrentMap<K, V> newMap() {
        MapMaker maker = new MapMaker();
        keyOption.applyTo(maker);
        valueOption.applyTo(maker);
        return maker.makeMap();
      }

      public void testContainsKey() {
        ConcurrentMap<Object, String> map = newMap();
        Object k = "key";
        map.put(k, "value");
        assertTrue(map.containsKey(k));
      }

      public void testClear() {
        ConcurrentMap<String, String> map = newMap();
        String k = "key";
        map.put(k, "value");
        assertFalse(map.isEmpty());
        map.clear();
        assertTrue(map.isEmpty());
        assertNull(map.get(k));
      }

      public void testKeySet() {
        ConcurrentMap<String, String> map = newMap();
        map.put("a", "foo");
        map.put("b", "foo");
        Set<String> expected = set("a", "b");
        assertEquals(expected, map.keySet());
      }

      public void testValues() {
        ConcurrentMap<String, String> map = newMap();
        map.put("a", "1");
        map.put("b", "2");
        Set<String> expected = set("1", "2");
        Set<String> actual = new HashSet<String>();
        actual.addAll(map.values());
        assertEquals(expected, actual);
      }

      public void testPutIfAbsent() {
        ConcurrentMap<String, String> map = newMap();
        map.putIfAbsent("a", "1");
        assertEquals("1", map.get("a"));
        map.putIfAbsent("a", "2");
        assertEquals("1", map.get("a"));
      }

      public void testReplace() {
        ConcurrentMap<String, String> map = newMap();
        map.put("a", "1");
        map.replace("a", "2", "2");
        assertEquals("1", map.get("a"));
        map.replace("a", "1", "2");
        assertEquals("2", map.get("a"));
      }

      public void testContainsValue() {
        ConcurrentMap<String, Object> map = newMap();
        Object v = "value";
        map.put("key", v);
        assertTrue(map.containsValue(v));
      }

      public void testEntrySet() {
        final ConcurrentMap<String, String> map = newMap();
        map.put("a", "1");
        map.put("b", "2");
        @SuppressWarnings("unchecked")
        Set<Map.Entry<String, String>> expected
            = set(Maps.immutableEntry("a", "1"), Maps.immutableEntry("b", "2"));
        assertEquals(expected, map.entrySet());
      }

      public void testPutAll() {
        ConcurrentMap<Object, Object> map = newMap();
        Object k = "key";
        Object v = "value";
        map.putAll(Collections.singletonMap(k, v));
        assertSame(v, map.get(k));
      }

      public void testRemove() {
        ConcurrentMap<Object, String> map = newMap();
        Object k = "key";
        map.put(k, "value");
        map.remove(k);
        assertFalse(map.containsKey(k));
      }

      public void testPutGet() {
        final Object k = new Object();
        final Object v = new Object();
        ConcurrentMap<Object, Object> map = newMap();
        map.put(k, v);
        assertEquals(1, map.size());
        assertSame(v, map.get(k));
        assertEquals(1, map.size());
        assertNull(map.get(new Object()));
      }

      public void testCompute() {
        final Object k = new Object();
        final Object v = new Object();
        ConcurrentMap<?, ?> map = newBuilder().makeComputingMap(
            new Function<Object, Object>() {
          public Object apply(Object key) {
            return key == k ? v : null;
          }
        });

        assertEquals(0, map.size());
        assertSame(v, map.get(k));
        assertSame(v, map.get(k));
        assertEquals(1, map.size());

        try {
          map.get(new Object());
          fail();
        } catch (NullPointerException e) { /* expected */ }
        assertEquals(1, map.size());
      }

      static class MockFunction implements Function<Object, Object>,
          Serializable {
        int count;
        public Object apply(Object key) {
          count++;
          return Value.valueOf(key.toString());
        }
        private static final long serialVersionUID = 0;
      }
    }

    /**
     * Enums conveniently maintain instance identity across serialization.
     */
    enum Key {
      FOO, BAR, TEE
    }

    enum Value {
      FOO, BAR, TEE
    }
  }

  public static class ExpiringReferenceMapTest extends TestCase {

    private static final long EXPIRING_TIME = 10;
    private static final int VALUE_PREFIX = 12345;
    private static final String KEY_PREFIX = "key prefix:";

    Timer oldTimer;
    final List<TimerTask> tasks = new ArrayList<TimerTask>();

    @Override
    protected void setUp() throws Exception {
      oldTimer = ExpirationTimer.instance;
      ExpirationTimer.instance = new Timer() {
        @Override
        public void schedule(TimerTask task, long delay) {
          tasks.add(task);
        }
      };
    }

    @Override
    protected void tearDown() throws Exception {
      ExpirationTimer.instance = oldTimer;
    }

    private void runTasks() {
      for (TimerTask task : tasks) {
        task.run();
      }
      tasks.clear();
    }

    public void testExpiringPut() {
      ConcurrentMap<String, Integer> map = new MapMaker()
          .expiration(EXPIRING_TIME, TimeUnit.MILLISECONDS).makeMap();

      for (int i = 0; i < 10; i++) {
        map.put(KEY_PREFIX + i, VALUE_PREFIX + i);
        assertEquals(Integer.valueOf(VALUE_PREFIX + i),
            map.get(KEY_PREFIX + i));
      }

      runTasks();

      assertEquals("Map must be empty by now", 0, map.size());
    }

    public void testExpiringPutIfAbsent() {
      ConcurrentMap<String, Integer> map = new MapMaker()
          .expiration(EXPIRING_TIME, TimeUnit.MILLISECONDS).makeMap();

      for (int i = 0; i < 10; i++) {
        map.putIfAbsent(KEY_PREFIX + i, VALUE_PREFIX + i);
        assertEquals(Integer.valueOf(VALUE_PREFIX + i),
            map.get(KEY_PREFIX + i));
      }

      runTasks();

      assertEquals("Map must be empty by now", 0, map.size());
    }

    public void testExpiringGetForSoft() {
      ConcurrentMap<String, Integer> map = new MapMaker()
          .softValues().expiration(EXPIRING_TIME, TimeUnit.MILLISECONDS)
          .makeMap();

      runExpirationTest(map);
    }

    public void testExpiringGetForStrong() {
      ConcurrentMap<String, Integer> map = new MapMaker()
          .expiration(EXPIRING_TIME, TimeUnit.MILLISECONDS).makeMap();

      runExpirationTest(map);
    }

    public void testRemovalSchedulerForStrong() {
      ConcurrentMap<String, Integer> map = new MapMaker()
          .expiration(EXPIRING_TIME, TimeUnit.MILLISECONDS).makeMap();

      runRemovalScheduler(map, KEY_PREFIX, EXPIRING_TIME);
    }

    public void testRemovalSchedulerForSoft() {
      ConcurrentMap<String, Integer> map = new MapMaker()
          .softValues().expiration(EXPIRING_TIME, TimeUnit.MILLISECONDS)
          .makeMap();

      runRemovalScheduler(map, KEY_PREFIX, EXPIRING_TIME);
    }

    private void runExpirationTest(ConcurrentMap<String, Integer> map) {
      for (int i = 0; i < 10; i++) {
        map.put(KEY_PREFIX + i, VALUE_PREFIX + i);
        assertEquals(Integer.valueOf(VALUE_PREFIX + i),
            map.get(KEY_PREFIX + i));
      }

      for (int i = 0; i < 10; i++) {
        assertEquals(Integer.valueOf(i + VALUE_PREFIX),
            map.get(KEY_PREFIX + i));
      }

      runTasks();

      for (int i = 0; i < 10; i++) {
        assertEquals(null, map.get(KEY_PREFIX + i));
      }
    }

    private void runRemovalScheduler(ConcurrentMap<String, Integer> map,
        String keyPrefix, long ttl) {

      int shift1 = 10 + VALUE_PREFIX;
      // fill with initial data
      for (int i = 0; i < 10; i++) {
        map.put(keyPrefix + i, i + shift1);
        assertEquals(Integer.valueOf(i + shift1), map.get(keyPrefix + i));
      }

      // wait, so that entries have just 10 ms to live
      try {
        Thread.sleep(ttl * 2 / 3);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }

      int shift2 = shift1 + 10;
      // fill with new data - has to live for 20 ms more
      for (int i = 0; i < 10; i++) {
        map.put(keyPrefix + i, i + shift2);
        assertEquals("key: " + keyPrefix + i,
            Integer.valueOf(i + shift2), map.get(keyPrefix + i));
      }

      // old timeouts must expire after this wait
      try {
        Thread.sleep(ttl * 2 / 3);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }

      // check that new values are still there - they still have 10 ms to live
      for (int i = 0; i < 10; i++) {
        assertEquals(Integer.valueOf(i + shift2), map.get(keyPrefix + i));
      }
    }
  }

  public static class ExpiringComputingReferenceMapTest extends TestCase {

    static final long VERY_LONG = 100000L;
    static final String KEY_PREFIX = "THIS IS AN ARBITRARY KEY PREFIX";
    static final int VALUE_SUFFIX = 77777;

    Timer oldTimer;
    final List<TimerTask> tasks = new ArrayList<TimerTask>();

    @Override
    protected void setUp() throws Exception {
      oldTimer = ExpirationTimer.instance;
      ExpirationTimer.instance = new Timer() {
        @Override
        public void schedule(TimerTask task, long delay) {
          tasks.add(task);
        }
      };
    }

    @Override
    protected void tearDown() throws Exception {
      ExpirationTimer.instance = oldTimer;
    }

    private void runTasks() {
      for (TimerTask task : tasks) {
        task.run();
      }
      tasks.clear();
    }

    public void testExpiringPut() {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .expiration(50, TimeUnit.MILLISECONDS)
          .makeComputingMap(WATCHED_CREATOR);

      for (int i = 0; i < 10; i++) {
        cache.put(KEY_PREFIX + i, i + VALUE_SUFFIX);
        assertEquals(Integer.valueOf(i + VALUE_SUFFIX),
            cache.get(KEY_PREFIX + i));
      }

      for (int i = 0; i < 10; i++) {
        WATCHED_CREATOR.reset();
        assertEquals(Integer.valueOf(i + VALUE_SUFFIX),
            cache.get(KEY_PREFIX + i));
        assertFalse("Creator should not have been called @#" + i,
            WATCHED_CREATOR.wasCalled());
      }

      runTasks();

      assertEquals("Cache must be empty by now", 0, cache.size());
    }

    public void testExpiringPutIfAbsent() {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .expiration(50, TimeUnit.MILLISECONDS)
          .makeComputingMap(WATCHED_CREATOR);

      for (int i = 0; i < 10; i++) {
        cache.putIfAbsent(KEY_PREFIX + i, i + VALUE_SUFFIX);
        assertEquals(Integer.valueOf(i + VALUE_SUFFIX),
            cache.get(KEY_PREFIX + i));
      }

      runTasks();

      assertEquals("Cache must be empty by now", 0, cache.size());
    }

    public void testExpiringGetForSoft() {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .expiration(5, TimeUnit.MILLISECONDS)
          .softValues().makeComputingMap(WATCHED_CREATOR);

      runExpirationTest(cache);
    }

    public void testExpiringGetForStrong() {
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .expiration(10, TimeUnit.MILLISECONDS)
          .makeComputingMap(WATCHED_CREATOR);

      runExpirationTest(cache);
    }

    private void runExpirationTest(ConcurrentMap<String, Integer> cache) {
      for (int i = 0; i < 10; i++) {
        assertEquals(Integer.valueOf(i + VALUE_SUFFIX),
            cache.get(KEY_PREFIX + i));
      }

      for (int i = 0; i < 10; i++) {
        WATCHED_CREATOR.reset();
        assertEquals(Integer.valueOf(i + VALUE_SUFFIX),
            cache.get(KEY_PREFIX + i));
        assertFalse("Creator should NOT have been called @#" + i,
            WATCHED_CREATOR.wasCalled());
      }

      runTasks();

      for (int i = 0; i < 10; i++) {
        WATCHED_CREATOR.reset();
        assertEquals(Integer.valueOf(i + VALUE_SUFFIX),
            cache.get(KEY_PREFIX + i));
        assertTrue("Creator should have been called @#" + i,
            WATCHED_CREATOR.wasCalled());
      }
    }

    public void testRemovalSchedulerForStrong() {
      String keyPrefix = "TRSTRONG_";
      int ttl = 300;
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .expiration(ttl, TimeUnit.MILLISECONDS)
          .makeComputingMap(new WatchedCreatorFunction(keyPrefix));
      runRemovalScheduler(cache, keyPrefix, ttl);
    }

    public void testRemovalSchedulerForSoft() {
      String keyPrefix = "TRSOFT_";
      int ttl = 300;
      ConcurrentMap<String, Integer> cache = new MapMaker()
          .expiration(ttl, TimeUnit.MILLISECONDS).softValues()
          .makeComputingMap(new WatchedCreatorFunction(keyPrefix));
      runRemovalScheduler(cache, keyPrefix, ttl);
    }

    private void runRemovalScheduler(ConcurrentMap<String, Integer> cache,
        String keyPrefix, int ttl) {
      int shift1 = 10 + VALUE_SUFFIX;
      // fill with initial data
      for (int i = 0; i < 10; i++) {
        cache.put(keyPrefix + i, i + shift1);
        assertEquals(Integer.valueOf(i + shift1), cache.get(keyPrefix + i));
      }

      // wait, so that entries have just 10 ms to live
      try {
        Thread.sleep(ttl * 2 / 3);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }

      int shift2 = shift1 + 10;
      // fill with new data - has to live for 20 ms more
      for (int i = 0; i < 10; i++) {
        cache.put(keyPrefix + i, i + shift2);
        assertEquals("key: " + keyPrefix + i,
            Integer.valueOf(i + shift2), cache.get(keyPrefix + i));
      }

      // old timeouts must expire after this wait
      try {
        Thread.sleep(ttl * 2 / 3);
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      }

      // check that new values are still there - they still have 10 ms to live
      for (int i = 0; i < 10; i++) {
        assertEquals(Integer.valueOf(i + shift2), cache.get(keyPrefix + i));
      }
    }

    private static class WatchedCreatorFunction
        implements Function<String, Integer> {
      boolean wasCalled = false; // must be set in apply()

      public WatchedCreatorFunction() {
        this(KEY_PREFIX);
      }

      public WatchedCreatorFunction(String prefix) {
        setPrefix(prefix);
      }

      public void reset() {
        wasCalled = false;
      }

      protected String prefix = KEY_PREFIX;

      public void setPrefix(String prefix) {
        this.prefix = prefix;
      }

      public boolean wasCalled() {
        return wasCalled;
      }

      public Integer apply(String s) {
        wasCalled = true;
        return Integer.parseInt(s.substring(prefix.length())) + VALUE_SUFFIX;
      }
    }

    static final WatchedCreatorFunction WATCHED_CREATOR =
        new WatchedCreatorFunction();
  }

  static <E> Set<E> set(E... elements) {
    return new HashSet<E>(Arrays.asList(elements));
  }
}