ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentHashMapTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentHashMapTest.java (file contents):
Revision 1.44 by jsr166, Sat Feb 28 18:27:31 2015 UTC vs.
Revision 1.60 by jsr166, Fri Oct 5 22:12:45 2018 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain, as explained at
2 > * Written by Doug Lea and Martin Buchholz with assistance from
3 > * members of JCP JSR-166 Expert Group and released to the public
4 > * domain, as explained at
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   * Other contributors include Andrew Wright, Jeffrey Hayes,
7   * Pat Fisher, Mike Judd.
# Line 18 | Line 19 | import java.util.Set;
19   import java.util.concurrent.ConcurrentHashMap;
20  
21   import junit.framework.Test;
21 import junit.framework.TestSuite;
22  
23   public class ConcurrentHashMapTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run(suite());
25 >        main(suite(), args);
26      }
27      public static Test suite() {
28 <        return new TestSuite(ConcurrentHashMapTest.class);
28 >        class Implementation implements MapImplementation {
29 >            public Class<?> klazz() { return ConcurrentHashMap.class; }
30 >            public Map emptyMap() { return new ConcurrentHashMap(); }
31 >            public Object makeKey(int i) { return i; }
32 >            public Object makeValue(int i) { return i; }
33 >            public boolean isConcurrent() { return true; }
34 >            public boolean permitsNullKeys() { return false; }
35 >            public boolean permitsNullValues() { return false; }
36 >            public boolean supportsSetValue() { return true; }
37 >        }
38 >        return newTestSuite(
39 >            ConcurrentHashMapTest.class,
40 >            MapTest.testSuite(new Implementation()));
41      }
42  
43      /**
44       * Returns a new map from Integers 1-5 to Strings "A"-"E".
45       */
46      private static ConcurrentHashMap<Integer, String> map5() {
47 <        ConcurrentHashMap map = new ConcurrentHashMap<Integer, String>(5);
47 >        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>(5);
48          assertTrue(map.isEmpty());
49          map.put(one, "A");
50          map.put(two, "B");
# Line 44 | Line 56 | public class ConcurrentHashMapTest exten
56          return map;
57      }
58  
47    /** Re-implement Integer.compare for old java versions */
48    static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; }
49
59      // classes for testing Comparable fallbacks
60      static class BI implements Comparable<BI> {
61          private final int value;
62          BI(int value) { this.value = value; }
63          public int compareTo(BI other) {
64 <            return compare(value, other.value);
64 >            return Integer.compare(value, other.value);
65          }
66          public boolean equals(Object x) {
67              return (x instanceof BI) && ((BI)x).value == value;
# Line 86 | Line 95 | public class ConcurrentHashMapTest exten
95                      break;
96              }
97              if (r == 0)
98 <                r = compare(size(), other.size());
98 >                r = Integer.compare(size(), other.size());
99              return r;
100          }
101          private static final long serialVersionUID = 0;
# Line 114 | Line 123 | public class ConcurrentHashMapTest exten
123       */
124      public void testComparableFamily() {
125          int size = 500;         // makes measured test run time -> 60ms
126 <        ConcurrentHashMap<BI, Boolean> m =
118 <            new ConcurrentHashMap<BI, Boolean>();
126 >        ConcurrentHashMap<BI, Boolean> m = new ConcurrentHashMap<>();
127          for (int i = 0; i < size; i++) {
128 <            assertTrue(m.put(new CI(i), true) == null);
128 >            assertNull(m.put(new CI(i), true));
129          }
130          for (int i = 0; i < size; i++) {
131              assertTrue(m.containsKey(new CI(i)));
# Line 131 | Line 139 | public class ConcurrentHashMapTest exten
139       */
140      public void testGenericComparable() {
141          int size = 120;         // makes measured test run time -> 60ms
142 <        ConcurrentHashMap<Object, Boolean> m =
135 <            new ConcurrentHashMap<Object, Boolean>();
142 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
143          for (int i = 0; i < size; i++) {
144              BI bi = new BI(i);
145              BS bs = new BS(String.valueOf(i));
146 <            LexicographicList<BI> bis = new LexicographicList<BI>(bi);
147 <            LexicographicList<BS> bss = new LexicographicList<BS>(bs);
148 <            assertTrue(m.putIfAbsent(bis, true) == null);
146 >            LexicographicList<BI> bis = new LexicographicList<>(bi);
147 >            LexicographicList<BS> bss = new LexicographicList<>(bs);
148 >            assertNull(m.putIfAbsent(bis, true));
149              assertTrue(m.containsKey(bis));
150              if (m.putIfAbsent(bss, true) == null)
151                  assertTrue(m.containsKey(bss));
# Line 156 | Line 163 | public class ConcurrentHashMapTest exten
163       */
164      public void testGenericComparable2() {
165          int size = 500;         // makes measured test run time -> 60ms
166 <        ConcurrentHashMap<Object, Boolean> m =
160 <            new ConcurrentHashMap<Object, Boolean>();
166 >        ConcurrentHashMap<Object, Boolean> m = new ConcurrentHashMap<>();
167          for (int i = 0; i < size; i++) {
168              m.put(Collections.singletonList(new BI(i)), true);
169          }
170  
171          for (int i = 0; i < size; i++) {
172 <            LexicographicList<BI> bis = new LexicographicList<BI>(new BI(i));
172 >            LexicographicList<BI> bis = new LexicographicList<>(new BI(i));
173              assertTrue(m.containsKey(bis));
174          }
175      }
# Line 174 | Line 180 | public class ConcurrentHashMapTest exten
180       */
181      public void testMixedComparable() {
182          int size = 1200;        // makes measured test run time -> 35ms
183 <        ConcurrentHashMap<Object, Object> map =
178 <            new ConcurrentHashMap<Object, Object>();
183 >        ConcurrentHashMap<Object, Object> map = new ConcurrentHashMap<>();
184          Random rng = new Random();
185          for (int i = 0; i < size; i++) {
186              Object x;
# Line 329 | Line 334 | public class ConcurrentHashMapTest exten
334      }
335  
336      /**
337 +     * Test keySet().removeAll on empty map
338 +     */
339 +    public void testKeySet_empty_removeAll() {
340 +        ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<>();
341 +        Set<Integer> set = map.keySet();
342 +        set.removeAll(Collections.emptyList());
343 +        assertTrue(map.isEmpty());
344 +        assertTrue(set.isEmpty());
345 +        // following is test for JDK-8163353
346 +        set.removeAll(Collections.emptySet());
347 +        assertTrue(map.isEmpty());
348 +        assertTrue(set.isEmpty());
349 +    }
350 +
351 +    /**
352       * keySet.toArray returns contains all keys
353       */
354      public void testKeySetToArray() {
# Line 533 | Line 553 | public class ConcurrentHashMapTest exten
553      /**
554       * Constructor (initialCapacity, loadFactor) throws
555       * IllegalArgumentException if either argument is negative
556 <      */
556 >     */
557      public void testConstructor2() {
558          try {
559              new ConcurrentHashMap(-1, .75f);
# Line 767 | Line 787 | public class ConcurrentHashMapTest exten
787      }
788  
789      /**
790 <     * A deserialized map equals original
790 >     * A deserialized/reserialized map equals original
791       */
792      public void testSerialization() throws Exception {
793          Map x = map5();
# Line 801 | Line 821 | public class ConcurrentHashMapTest exten
821          }
822      }
823  
824 +    /**
825 +     * Tests performance of removeAll when the other collection is much smaller.
826 +     * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck
827 +     */
828 +    public void testRemoveAll_performance() {
829 +        final int mapSize = expensiveTests ? 1_000_000 : 100;
830 +        final int iterations = expensiveTests ? 500 : 2;
831 +        final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
832 +        for (int i = 0; i < mapSize; i++)
833 +            map.put(i, i);
834 +        Set<Integer> keySet = map.keySet();
835 +        Collection<Integer> removeMe = Arrays.asList(new Integer[] { -99, -86 });
836 +        for (int i = 0; i < iterations; i++)
837 +            assertFalse(keySet.removeAll(removeMe));
838 +        assertEquals(mapSize, map.size());
839 +    }
840 +
841 +    public void testReentrantComputeIfAbsent() {
842 +        ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>(16);
843 +        try {
844 +            for (int i = 0; i < 100; i++) { // force a resize
845 +                map.computeIfAbsent(i, key -> findValue(map, key));
846 +            }
847 +            fail("recursive computeIfAbsent");
848 +        } catch (IllegalStateException ex) {
849 +        }
850 +    }
851 +
852 +    private Integer findValue(ConcurrentHashMap<Integer, Integer> map,
853 +                              Integer key) {
854 +        return (key % 5 == 0) ?  key :
855 +            map.computeIfAbsent(key + 1, k -> findValue(map, k));
856 +    }
857 +
858   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines