--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2013/07/21 22:24:18 1.36 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2016/07/17 03:40:07 1.48 @@ -6,13 +6,23 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.Iterator; +import java.util.Map; +import java.util.Random; +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) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ConcurrentHashMapTest.class); @@ -35,7 +45,9 @@ public class ConcurrentHashMapTest exten } /** Re-implement Integer.compare for old java versions */ - static int compare(int x, int y) { return x < y ? -1 : x > y ? 1 : 0; } + 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 { @@ -103,12 +115,13 @@ public class ConcurrentHashMapTest exten * class are found. */ public void testComparableFamily() { + int size = 500; // makes measured test run time -> 60ms ConcurrentHashMap m = new ConcurrentHashMap(); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < size; i++) { assertTrue(m.put(new CI(i), true) == null); } - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < size; i++) { assertTrue(m.containsKey(new CI(i))); assertTrue(m.containsKey(new DI(i))); } @@ -119,9 +132,10 @@ public class ConcurrentHashMapTest exten * on Comparable can be inserted and found. */ public void testGenericComparable() { + int size = 120; // makes measured test run time -> 60ms ConcurrentHashMap m = new ConcurrentHashMap(); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < size; i++) { BI bi = new BI(i); BS bs = new BS(String.valueOf(i)); LexicographicList bis = new LexicographicList(bi); @@ -132,8 +146,8 @@ public class ConcurrentHashMapTest exten assertTrue(m.containsKey(bss)); assertTrue(m.containsKey(bis)); } - for (int i = 0; i < 1000; i++) { - assertTrue(m.containsKey(new ArrayList(Collections.singleton(new BI(i))))); + for (int i = 0; i < size; i++) { + assertTrue(m.containsKey(Collections.singletonList(new BI(i)))); } } @@ -143,13 +157,14 @@ public class ConcurrentHashMapTest exten * inserted and found. */ public void testGenericComparable2() { + int size = 500; // makes measured test run time -> 60ms ConcurrentHashMap m = new ConcurrentHashMap(); - for (int i = 0; i < 1000; i++) { - m.put(new ArrayList(Collections.singleton(new BI(i))), true); + for (int i = 0; i < size; i++) { + m.put(Collections.singletonList(new BI(i)), true); } - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < size; i++) { LexicographicList bis = new LexicographicList(new BI(i)); assertTrue(m.containsKey(bis)); } @@ -160,10 +175,10 @@ public class ConcurrentHashMapTest exten * can be inserted and found. */ public void testMixedComparable() { - int size = 10000; + int size = 1200; // makes measured test run time -> 35ms ConcurrentHashMap map = new ConcurrentHashMap(); - Random rng = new Random(1370014958369218000L); + Random rng = new Random(); for (int i = 0; i < size; i++) { Object x; switch (rng.nextInt(4)) { @@ -212,10 +227,9 @@ public class ConcurrentHashMapTest exten assertFalse(map2.equals(map1)); } - /** - * hashCode() equals sum of each key.hashCode ^ value.hashCode - */ + * hashCode() equals sum of each key.hashCode ^ value.hashCode + */ public void testHashCode() { ConcurrentHashMap map = map5(); int sum = 0; @@ -275,6 +289,7 @@ public class ConcurrentHashMapTest exten assertEquals("A", (String)map.get(one)); ConcurrentHashMap empty = new ConcurrentHashMap(); assertNull(map.get("anything")); + assertNull(empty.get("anything")); } /** @@ -520,39 +535,39 @@ public class ConcurrentHashMapTest exten /** * Constructor (initialCapacity, loadFactor) throws * IllegalArgumentException if either argument is negative - */ + */ public void testConstructor2() { try { new ConcurrentHashMap(-1, .75f); shouldThrow(); - } catch (IllegalArgumentException e) {} - + } catch (IllegalArgumentException success) {} + try { new ConcurrentHashMap(16, -1); shouldThrow(); - } catch (IllegalArgumentException e) {} + } catch (IllegalArgumentException success) {} } - - /** - * Constructor (initialCapacity, loadFactor, concurrencyLevel) - * throws IllegalArgumentException if any argument is negative - */ + + /** + * Constructor (initialCapacity, loadFactor, concurrencyLevel) + * throws IllegalArgumentException if any argument is negative + */ public void testConstructor3() { - try { - new ConcurrentHashMap(-1, .75f, 1); - shouldThrow(); - } catch (IllegalArgumentException e) {} - - try { - new ConcurrentHashMap(16, -1, 1); - shouldThrow(); - } catch (IllegalArgumentException e) {} - - try { - new ConcurrentHashMap(16, .75f, -1); - shouldThrow(); - } catch (IllegalArgumentException e) {} - } + try { + new ConcurrentHashMap(-1, .75f, 1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + + try { + new ConcurrentHashMap(16, -1, 1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + + try { + new ConcurrentHashMap(16, .75f, -1); + shouldThrow(); + } catch (IllegalArgumentException success) {} + } /** * ConcurrentHashMap(map) throws NullPointerException if the given @@ -562,7 +577,7 @@ public class ConcurrentHashMapTest exten try { new ConcurrentHashMap(null); shouldThrow(); - } catch (NullPointerException e) {} + } catch (NullPointerException success) {} } /** @@ -577,13 +592,12 @@ public class ConcurrentHashMapTest exten assertFalse(map2.equals(map1)); } - /** * get(null) throws NPE */ public void testGet_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.get(null); shouldThrow(); } catch (NullPointerException success) {} @@ -593,8 +607,8 @@ public class ConcurrentHashMapTest exten * containsKey(null) throws NPE */ public void testContainsKey_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsKey(null); shouldThrow(); } catch (NullPointerException success) {} @@ -604,8 +618,8 @@ public class ConcurrentHashMapTest exten * containsValue(null) throws NPE */ public void testContainsValue_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.containsValue(null); shouldThrow(); } catch (NullPointerException success) {} @@ -615,8 +629,8 @@ public class ConcurrentHashMapTest exten * contains(null) throws NPE */ public void testContains_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.contains(null); shouldThrow(); } catch (NullPointerException success) {} @@ -626,8 +640,8 @@ public class ConcurrentHashMapTest exten * put(null,x) throws NPE */ public void testPut1_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.put(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -637,8 +651,8 @@ public class ConcurrentHashMapTest exten * put(x, null) throws NPE */ public void testPut2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.put("whatever", null); shouldThrow(); } catch (NullPointerException success) {} @@ -648,8 +662,8 @@ public class ConcurrentHashMapTest exten * putIfAbsent(null, x) throws NPE */ public void testPutIfAbsent1_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -659,8 +673,8 @@ public class ConcurrentHashMapTest exten * replace(null, x) throws NPE */ public void testReplace_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -670,8 +684,8 @@ public class ConcurrentHashMapTest exten * replace(null, x, y) throws NPE */ public void testReplaceValue_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace(null, one, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -681,8 +695,8 @@ public class ConcurrentHashMapTest exten * putIfAbsent(x, null) throws NPE */ public void testPutIfAbsent2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.putIfAbsent("whatever", null); shouldThrow(); } catch (NullPointerException success) {} @@ -692,8 +706,8 @@ public class ConcurrentHashMapTest exten * replace(x, null) throws NPE */ public void testReplace2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null); shouldThrow(); } catch (NullPointerException success) {} @@ -703,8 +717,8 @@ public class ConcurrentHashMapTest exten * replace(x, null, y) throws NPE */ public void testReplaceValue2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", null, "A"); shouldThrow(); } catch (NullPointerException success) {} @@ -714,8 +728,8 @@ public class ConcurrentHashMapTest exten * replace(x, y, null) throws NPE */ public void testReplaceValue3_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); c.replace("whatever", one, null); shouldThrow(); } catch (NullPointerException success) {} @@ -725,9 +739,9 @@ public class ConcurrentHashMapTest exten * remove(null) throws NPE */ public void testRemove1_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); c.remove(null); shouldThrow(); } catch (NullPointerException success) {} @@ -737,9 +751,9 @@ public class ConcurrentHashMapTest exten * remove(null, x) throws NPE */ public void testRemove2_NullPointerException() { + ConcurrentHashMap c = new ConcurrentHashMap(5); + c.put("sadsdf", "asdads"); try { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); c.remove(null, "whatever"); shouldThrow(); } catch (NullPointerException success) {} @@ -789,4 +803,20 @@ 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() { + int mapSize = expensiveTests ? 1_000_000 : 100; + int iterations = expensiveTests ? 500 : 2; + 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()); + } }