--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2015/07/27 03:12:26 1.47 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2018/01/08 03:37:59 1.58 @@ -1,6 +1,7 @@ /* - * Written by Doug Lea with assistance from members of JCP JSR-166 - * Expert Group and released to the public domain, as explained at + * Written by Doug Lea and Martin Buchholz with assistance from + * members of JCP JSR-166 Expert Group and released to the public + * domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. @@ -18,21 +19,32 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import junit.framework.Test; -import junit.framework.TestSuite; public class ConcurrentHashMapTest extends JSR166TestCase { public static void main(String[] args) { main(suite(), args); } public static Test suite() { - return new TestSuite(ConcurrentHashMapTest.class); + class Implementation implements MapImplementation { + public Class klazz() { return ConcurrentHashMap.class; } + public Map emptyMap() { return new ConcurrentHashMap(); } + public Object makeKey(int i) { return i; } + public Object makeValue(int i) { return i; } + public boolean isConcurrent() { return true; } + public boolean permitsNullKeys() { return false; } + public boolean permitsNullValues() { return false; } + public boolean supportsSetValue() { return true; } + } + return newTestSuite( + ConcurrentHashMapTest.class, + MapTest.testSuite(new Implementation())); } /** * Returns a new map from Integers 1-5 to Strings "A"-"E". */ private static ConcurrentHashMap map5() { - ConcurrentHashMap map = new ConcurrentHashMap(5); + ConcurrentHashMap map = new ConcurrentHashMap<>(5); assertTrue(map.isEmpty()); map.put(one, "A"); map.put(two, "B"); @@ -44,17 +56,12 @@ public class ConcurrentHashMapTest exten return map; } - /** Re-implement Integer.compare for old java versions */ - static int compare(int x, int y) { - return (x < y) ? -1 : (x > y) ? 1 : 0; - } - // classes for testing Comparable fallbacks static class BI implements Comparable { private final int value; BI(int value) { this.value = value; } public int compareTo(BI other) { - return compare(value, other.value); + return Integer.compare(value, other.value); } public boolean equals(Object x) { return (x instanceof BI) && ((BI)x).value == value; @@ -88,7 +95,7 @@ public class ConcurrentHashMapTest exten break; } if (r == 0) - r = compare(size(), other.size()); + r = Integer.compare(size(), other.size()); return r; } private static final long serialVersionUID = 0; @@ -116,10 +123,9 @@ public class ConcurrentHashMapTest exten */ public void testComparableFamily() { int size = 500; // makes measured test run time -> 60ms - ConcurrentHashMap m = - new ConcurrentHashMap(); + ConcurrentHashMap m = new ConcurrentHashMap<>(); for (int i = 0; i < size; i++) { - assertTrue(m.put(new CI(i), true) == null); + assertNull(m.put(new CI(i), true)); } for (int i = 0; i < size; i++) { assertTrue(m.containsKey(new CI(i))); @@ -133,14 +139,13 @@ public class ConcurrentHashMapTest exten */ public void testGenericComparable() { int size = 120; // makes measured test run time -> 60ms - ConcurrentHashMap m = - new ConcurrentHashMap(); + ConcurrentHashMap m = new ConcurrentHashMap<>(); for (int i = 0; i < size; i++) { BI bi = new BI(i); BS bs = new BS(String.valueOf(i)); - LexicographicList bis = new LexicographicList(bi); - LexicographicList bss = new LexicographicList(bs); - assertTrue(m.putIfAbsent(bis, true) == null); + LexicographicList bis = new LexicographicList<>(bi); + LexicographicList bss = new LexicographicList<>(bs); + assertNull(m.putIfAbsent(bis, true)); assertTrue(m.containsKey(bis)); if (m.putIfAbsent(bss, true) == null) assertTrue(m.containsKey(bss)); @@ -158,14 +163,13 @@ public class ConcurrentHashMapTest exten */ public void testGenericComparable2() { int size = 500; // makes measured test run time -> 60ms - ConcurrentHashMap m = - new ConcurrentHashMap(); + ConcurrentHashMap m = new ConcurrentHashMap<>(); for (int i = 0; i < size; i++) { m.put(Collections.singletonList(new BI(i)), true); } for (int i = 0; i < size; i++) { - LexicographicList bis = new LexicographicList(new BI(i)); + LexicographicList bis = new LexicographicList<>(new BI(i)); assertTrue(m.containsKey(bis)); } } @@ -176,8 +180,7 @@ public class ConcurrentHashMapTest exten */ public void testMixedComparable() { int size = 1200; // makes measured test run time -> 35ms - ConcurrentHashMap map = - new ConcurrentHashMap(); + ConcurrentHashMap map = new ConcurrentHashMap<>(); Random rng = new Random(); for (int i = 0; i < size; i++) { Object x; @@ -331,6 +334,21 @@ public class ConcurrentHashMapTest exten } /** + * Test keySet().removeAll on empty map + */ + public void testKeySet_empty_removeAll() { + ConcurrentHashMap map = new ConcurrentHashMap<>(); + Set set = map.keySet(); + set.removeAll(Collections.emptyList()); + assertTrue(map.isEmpty()); + assertTrue(set.isEmpty()); + // following is test for JDK-8163353 + set.removeAll(Collections.emptySet()); + assertTrue(map.isEmpty()); + assertTrue(set.isEmpty()); + } + + /** * keySet.toArray returns contains all keys */ public void testKeySetToArray() { @@ -769,7 +787,7 @@ public class ConcurrentHashMapTest exten } /** - * A deserialized map equals original + * A deserialized/reserialized map equals original */ public void testSerialization() throws Exception { Map x = map5(); @@ -803,4 +821,21 @@ public class ConcurrentHashMapTest exten } } + /** + * Tests performance of removeAll when the other collection is much smaller. + * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testRemoveAll_performance -Djsr166.expensiveTests=true tck + */ + public void testRemoveAll_performance() { + final int mapSize = expensiveTests ? 1_000_000 : 100; + final int iterations = expensiveTests ? 500 : 2; + final ConcurrentHashMap map = new ConcurrentHashMap<>(); + for (int i = 0; i < mapSize; i++) + map.put(i, i); + Set keySet = map.keySet(); + Collection removeMe = Arrays.asList(new Integer[] { -99, -86 }); + for (int i = 0; i < iterations; i++) + assertFalse(keySet.removeAll(removeMe)); + assertEquals(mapSize, map.size()); + } + }