--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2017/01/04 06:09:58 1.52 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2021/01/27 01:57:24 1.65 @@ -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. @@ -16,25 +17,32 @@ import java.util.Map; import java.util.Random; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; 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 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". + * Returns a new map from Items 1-5 to Strings "A"-"E". */ - private static ConcurrentHashMap map5() { - ConcurrentHashMap map = new ConcurrentHashMap<>(5); + private static ConcurrentHashMap map5() { + ConcurrentHashMap map = new ConcurrentHashMap<>(5); assertTrue(map.isEmpty()); map.put(one, "A"); map.put(two, "B"); @@ -42,21 +50,16 @@ public class ConcurrentHashMapTest exten map.put(four, "D"); map.put(five, "E"); assertFalse(map.isEmpty()); - assertEquals(5, map.size()); + mustEqual(5, map.size()); 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; @@ -90,7 +93,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; @@ -118,10 +121,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))); @@ -135,14 +137,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)); @@ -160,14 +161,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)); } } @@ -178,8 +178,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; @@ -197,13 +196,13 @@ public class ConcurrentHashMapTest exten } int count = 0; for (Object k : map.keySet()) { - assertEquals(map.get(k), k); + mustEqual(map.get(k), k); ++count; } - assertEquals(count, size); - assertEquals(map.size(), size); + mustEqual(count, size); + mustEqual(map.size(), size); for (Object k : map.keySet()) { - assertEquals(map.put(k, k), k); + mustEqual(map.put(k, k), k); } } @@ -211,19 +210,19 @@ public class ConcurrentHashMapTest exten * clear removes all pairs */ public void testClear() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); map.clear(); - assertEquals(0, map.size()); + mustEqual(0, map.size()); } /** * Maps with same contents are equal */ public void testEquals() { - ConcurrentHashMap map1 = map5(); - ConcurrentHashMap map2 = map5(); - assertEquals(map1, map2); - assertEquals(map2, map1); + ConcurrentHashMap map1 = map5(); + ConcurrentHashMap map2 = map5(); + mustEqual(map1, map2); + mustEqual(map2, map1); map1.clear(); assertFalse(map1.equals(map2)); assertFalse(map2.equals(map1)); @@ -233,18 +232,18 @@ public class ConcurrentHashMapTest exten * hashCode() equals sum of each key.hashCode ^ value.hashCode */ public void testHashCode() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); int sum = 0; - for (Map.Entry e : map.entrySet()) + for (Map.Entry e : map.entrySet()) sum += e.getKey().hashCode() ^ e.getValue().hashCode(); - assertEquals(sum, map.hashCode()); + mustEqual(sum, map.hashCode()); } /** * contains returns true for contained value */ public void testContains() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); assertTrue(map.contains("A")); assertFalse(map.contains("Z")); } @@ -253,7 +252,7 @@ public class ConcurrentHashMapTest exten * containsKey returns true for contained key */ public void testContainsKey() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); assertTrue(map.containsKey(one)); assertFalse(map.containsKey(zero)); } @@ -262,7 +261,7 @@ public class ConcurrentHashMapTest exten * containsValue returns true for held values */ public void testContainsValue() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); assertTrue(map.containsValue("A")); assertFalse(map.containsValue("Z")); } @@ -272,14 +271,14 @@ public class ConcurrentHashMapTest exten * elements */ public void testEnumeration() { - ConcurrentHashMap map = map5(); - Enumeration e = map.elements(); + ConcurrentHashMap map = map5(); + Enumeration e = map.elements(); int count = 0; while (e.hasMoreElements()) { count++; e.nextElement(); } - assertEquals(5, count); + mustEqual(5, count); } /** @@ -287,9 +286,9 @@ public class ConcurrentHashMapTest exten * or null if not present */ public void testGet() { - ConcurrentHashMap map = map5(); - assertEquals("A", (String)map.get(one)); - ConcurrentHashMap empty = new ConcurrentHashMap(); + ConcurrentHashMap map = map5(); + mustEqual("A", map.get(one)); + ConcurrentHashMap empty = new ConcurrentHashMap<>(); assertNull(map.get("anything")); assertNull(empty.get("anything")); } @@ -298,8 +297,8 @@ public class ConcurrentHashMapTest exten * isEmpty is true of empty map and false for non-empty */ public void testIsEmpty() { - ConcurrentHashMap empty = new ConcurrentHashMap(); - ConcurrentHashMap map = map5(); + ConcurrentHashMap empty = new ConcurrentHashMap<>(); + ConcurrentHashMap map = map5(); assertTrue(empty.isEmpty()); assertFalse(map.isEmpty()); } @@ -308,36 +307,36 @@ public class ConcurrentHashMapTest exten * keys returns an enumeration containing all the keys from the map */ public void testKeys() { - ConcurrentHashMap map = map5(); - Enumeration e = map.keys(); + ConcurrentHashMap map = map5(); + Enumeration e = map.keys(); int count = 0; while (e.hasMoreElements()) { count++; e.nextElement(); } - assertEquals(5, count); + mustEqual(5, count); } /** * keySet returns a Set containing all the keys */ public void testKeySet() { - ConcurrentHashMap map = map5(); - Set s = map.keySet(); - assertEquals(5, s.size()); - assertTrue(s.contains(one)); - assertTrue(s.contains(two)); - assertTrue(s.contains(three)); - assertTrue(s.contains(four)); - assertTrue(s.contains(five)); + ConcurrentHashMap map = map5(); + Set s = map.keySet(); + mustEqual(5, s.size()); + mustContain(s, one); + mustContain(s, two); + mustContain(s, three); + mustContain(s, four); + mustContain(s, five); } /** * Test keySet().removeAll on empty map */ public void testKeySet_empty_removeAll() { - ConcurrentHashMap map = new ConcurrentHashMap<>(); - Set set = map.keySet(); + ConcurrentHashMap map = new ConcurrentHashMap<>(); + Set set = map.keySet(); set.removeAll(Collections.emptyList()); assertTrue(map.isEmpty()); assertTrue(set.isEmpty()); @@ -351,12 +350,12 @@ public class ConcurrentHashMapTest exten * keySet.toArray returns contains all keys */ public void testKeySetToArray() { - ConcurrentHashMap map = map5(); - Set s = map.keySet(); + ConcurrentHashMap map = map5(); + Set s = map.keySet(); Object[] ar = s.toArray(); assertTrue(s.containsAll(Arrays.asList(ar))); - assertEquals(5, ar.length); - ar[0] = m10; + mustEqual(5, ar.length); + ar[0] = minusTen; assertFalse(s.containsAll(Arrays.asList(ar))); } @@ -364,11 +363,11 @@ public class ConcurrentHashMapTest exten * Values.toArray contains all values */ public void testValuesToArray() { - ConcurrentHashMap map = map5(); - Collection v = map.values(); - Object[] ar = v.toArray(); - ArrayList s = new ArrayList(Arrays.asList(ar)); - assertEquals(5, ar.length); + ConcurrentHashMap map = map5(); + Collection v = map.values(); + String[] ar = v.toArray(new String[0]); + ArrayList s = new ArrayList<>(Arrays.asList(ar)); + mustEqual(5, ar.length); assertTrue(s.contains("A")); assertTrue(s.contains("B")); assertTrue(s.contains("C")); @@ -380,10 +379,10 @@ public class ConcurrentHashMapTest exten * entrySet.toArray contains all entries */ public void testEntrySetToArray() { - ConcurrentHashMap map = map5(); - Set s = map.entrySet(); + ConcurrentHashMap map = map5(); + Set> s = map.entrySet(); Object[] ar = s.toArray(); - assertEquals(5, ar.length); + mustEqual(5, ar.length); for (int i = 0; i < 5; ++i) { assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); @@ -394,9 +393,9 @@ public class ConcurrentHashMapTest exten * values collection contains all values */ public void testValues() { - ConcurrentHashMap map = map5(); - Collection s = map.values(); - assertEquals(5, s.size()); + ConcurrentHashMap map = map5(); + Collection s = map.values(); + mustEqual(5, s.size()); assertTrue(s.contains("A")); assertTrue(s.contains("B")); assertTrue(s.contains("C")); @@ -408,12 +407,12 @@ public class ConcurrentHashMapTest exten * entrySet contains all pairs */ public void testEntrySet() { - ConcurrentHashMap map = map5(); - Set s = map.entrySet(); - assertEquals(5, s.size()); - Iterator it = s.iterator(); + ConcurrentHashMap map = map5(); + Set> s = map.entrySet(); + mustEqual(5, s.size()); + Iterator> it = s.iterator(); while (it.hasNext()) { - Map.Entry e = (Map.Entry) it.next(); + Map.Entry e = it.next(); assertTrue( (e.getKey().equals(one) && e.getValue().equals("A")) || (e.getKey().equals(two) && e.getValue().equals("B")) || @@ -427,22 +426,22 @@ public class ConcurrentHashMapTest exten * putAll adds all key-value pairs from the given map */ public void testPutAll() { - ConcurrentHashMap empty = new ConcurrentHashMap(); - ConcurrentHashMap map = map5(); - empty.putAll(map); - assertEquals(5, empty.size()); - assertTrue(empty.containsKey(one)); - assertTrue(empty.containsKey(two)); - assertTrue(empty.containsKey(three)); - assertTrue(empty.containsKey(four)); - assertTrue(empty.containsKey(five)); + ConcurrentHashMap p = new ConcurrentHashMap<>(); + ConcurrentHashMap map = map5(); + p.putAll(map); + mustEqual(5, p.size()); + assertTrue(p.containsKey(one)); + assertTrue(p.containsKey(two)); + assertTrue(p.containsKey(three)); + assertTrue(p.containsKey(four)); + assertTrue(p.containsKey(five)); } /** * putIfAbsent works when the given key is not present */ public void testPutIfAbsent() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); map.putIfAbsent(six, "Z"); assertTrue(map.containsKey(six)); } @@ -451,15 +450,15 @@ public class ConcurrentHashMapTest exten * putIfAbsent does not add the pair if the key is already present */ public void testPutIfAbsent2() { - ConcurrentHashMap map = map5(); - assertEquals("A", map.putIfAbsent(one, "Z")); + ConcurrentHashMap map = map5(); + mustEqual("A", map.putIfAbsent(one, "Z")); } /** * replace fails when the given key is not present */ public void testReplace() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); assertNull(map.replace(six, "Z")); assertFalse(map.containsKey(six)); } @@ -468,38 +467,38 @@ public class ConcurrentHashMapTest exten * replace succeeds if the key is already present */ public void testReplace2() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); assertNotNull(map.replace(one, "Z")); - assertEquals("Z", map.get(one)); + mustEqual("Z", map.get(one)); } /** * replace value fails when the given key not mapped to expected value */ public void testReplaceValue() { - ConcurrentHashMap map = map5(); - assertEquals("A", map.get(one)); + ConcurrentHashMap map = map5(); + mustEqual("A", map.get(one)); assertFalse(map.replace(one, "Z", "Z")); - assertEquals("A", map.get(one)); + mustEqual("A", map.get(one)); } /** * replace value succeeds when the given key mapped to expected value */ public void testReplaceValue2() { - ConcurrentHashMap map = map5(); - assertEquals("A", map.get(one)); + ConcurrentHashMap map = map5(); + mustEqual("A", map.get(one)); assertTrue(map.replace(one, "A", "Z")); - assertEquals("Z", map.get(one)); + mustEqual("Z", map.get(one)); } /** * remove removes the correct key-value pair from the map */ public void testRemove() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); map.remove(five); - assertEquals(4, map.size()); + mustEqual(4, map.size()); assertFalse(map.containsKey(five)); } @@ -507,12 +506,12 @@ public class ConcurrentHashMapTest exten * remove(key,value) removes only if pair present */ public void testRemove2() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); map.remove(five, "E"); - assertEquals(4, map.size()); + mustEqual(4, map.size()); assertFalse(map.containsKey(five)); map.remove(four, "A"); - assertEquals(4, map.size()); + mustEqual(4, map.size()); assertTrue(map.containsKey(four)); } @@ -520,17 +519,17 @@ public class ConcurrentHashMapTest exten * size returns the correct values */ public void testSize() { - ConcurrentHashMap map = map5(); - ConcurrentHashMap empty = new ConcurrentHashMap(); - assertEquals(0, empty.size()); - assertEquals(5, map.size()); + ConcurrentHashMap map = map5(); + ConcurrentHashMap empty = new ConcurrentHashMap<>(); + mustEqual(0, empty.size()); + mustEqual(5, map.size()); } /** * toString contains toString of elements */ public void testToString() { - ConcurrentHashMap map = map5(); + ConcurrentHashMap map = map5(); String s = map.toString(); for (int i = 1; i <= 5; ++i) { assertTrue(s.contains(String.valueOf(i))); @@ -544,7 +543,7 @@ public class ConcurrentHashMapTest exten */ public void testConstructor1() { try { - new ConcurrentHashMap(-1); + new ConcurrentHashMap(-1); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -555,12 +554,12 @@ public class ConcurrentHashMapTest exten */ public void testConstructor2() { try { - new ConcurrentHashMap(-1, .75f); + new ConcurrentHashMap(-1, .75f); shouldThrow(); } catch (IllegalArgumentException success) {} try { - new ConcurrentHashMap(16, -1); + new ConcurrentHashMap(16, -1); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -571,17 +570,17 @@ public class ConcurrentHashMapTest exten */ public void testConstructor3() { try { - new ConcurrentHashMap(-1, .75f, 1); + new ConcurrentHashMap(-1, .75f, 1); shouldThrow(); } catch (IllegalArgumentException success) {} try { - new ConcurrentHashMap(16, -1, 1); + new ConcurrentHashMap(16, -1, 1); shouldThrow(); } catch (IllegalArgumentException success) {} try { - new ConcurrentHashMap(16, .75f, -1); + new ConcurrentHashMap(16, .75f, -1); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -592,7 +591,7 @@ public class ConcurrentHashMapTest exten */ public void testConstructor4() { try { - new ConcurrentHashMap(null); + new ConcurrentHashMap(null); shouldThrow(); } catch (NullPointerException success) {} } @@ -602,8 +601,8 @@ public class ConcurrentHashMapTest exten * as the given map */ public void testConstructor5() { - ConcurrentHashMap map1 = map5(); - ConcurrentHashMap map2 = new ConcurrentHashMap(map5()); + ConcurrentHashMap map1 = map5(); + ConcurrentHashMap map2 = new ConcurrentHashMap<>(map1); assertTrue(map2.equals(map1)); map2.put(one, "F"); assertFalse(map2.equals(map1)); @@ -613,7 +612,7 @@ public class ConcurrentHashMapTest exten * get(null) throws NPE */ public void testGet_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { c.get(null); shouldThrow(); @@ -624,7 +623,7 @@ public class ConcurrentHashMapTest exten * containsKey(null) throws NPE */ public void testContainsKey_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { c.containsKey(null); shouldThrow(); @@ -635,7 +634,7 @@ public class ConcurrentHashMapTest exten * containsValue(null) throws NPE */ public void testContainsValue_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { c.containsValue(null); shouldThrow(); @@ -646,7 +645,7 @@ public class ConcurrentHashMapTest exten * contains(null) throws NPE */ public void testContains_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { c.contains(null); shouldThrow(); @@ -657,7 +656,7 @@ public class ConcurrentHashMapTest exten * put(null,x) throws NPE */ public void testPut1_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { c.put(null, "whatever"); shouldThrow(); @@ -668,9 +667,9 @@ public class ConcurrentHashMapTest exten * put(x, null) throws NPE */ public void testPut2_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { - c.put("whatever", null); + c.put(zero, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -679,7 +678,7 @@ public class ConcurrentHashMapTest exten * putIfAbsent(null, x) throws NPE */ public void testPutIfAbsent1_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { c.putIfAbsent(null, "whatever"); shouldThrow(); @@ -690,7 +689,7 @@ public class ConcurrentHashMapTest exten * replace(null, x) throws NPE */ public void testReplace_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { c.replace(null, "whatever"); shouldThrow(); @@ -701,9 +700,9 @@ public class ConcurrentHashMapTest exten * replace(null, x, y) throws NPE */ public void testReplaceValue_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { - c.replace(null, one, "whatever"); + c.replace(null, "A", "B"); shouldThrow(); } catch (NullPointerException success) {} } @@ -712,9 +711,9 @@ public class ConcurrentHashMapTest exten * putIfAbsent(x, null) throws NPE */ public void testPutIfAbsent2_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { - c.putIfAbsent("whatever", null); + c.putIfAbsent(zero, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -723,9 +722,9 @@ public class ConcurrentHashMapTest exten * replace(x, null) throws NPE */ public void testReplace2_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { - c.replace("whatever", null); + c.replace(one, null); shouldThrow(); } catch (NullPointerException success) {} } @@ -734,9 +733,9 @@ public class ConcurrentHashMapTest exten * replace(x, null, y) throws NPE */ public void testReplaceValue2_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { - c.replace("whatever", null, "A"); + c.replace(one, null, "A"); shouldThrow(); } catch (NullPointerException success) {} } @@ -745,9 +744,9 @@ public class ConcurrentHashMapTest exten * replace(x, y, null) throws NPE */ public void testReplaceValue3_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); try { - c.replace("whatever", one, null); + c.replace(zero, "A", null); shouldThrow(); } catch (NullPointerException success) {} } @@ -756,8 +755,8 @@ public class ConcurrentHashMapTest exten * remove(null) throws NPE */ public void testRemove1_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); + c.put(one, "asdads"); try { c.remove(null); shouldThrow(); @@ -768,8 +767,8 @@ public class ConcurrentHashMapTest exten * remove(null, x) throws NPE */ public void testRemove2_NullPointerException() { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); + c.put(one, "asdads"); try { c.remove(null, "whatever"); shouldThrow(); @@ -780,41 +779,43 @@ public class ConcurrentHashMapTest exten * remove(x, null) returns false */ public void testRemove3() { - ConcurrentHashMap c = new ConcurrentHashMap(5); - c.put("sadsdf", "asdads"); - assertFalse(c.remove("sadsdf", null)); + ConcurrentHashMap c = new ConcurrentHashMap<>(5); + c.put(one, "asdads"); + assertFalse(c.remove(one, null)); } /** - * A deserialized map equals original + * A deserialized/reserialized map equals original */ public void testSerialization() throws Exception { - Map x = map5(); - Map y = serialClone(x); + Map x = map5(); + Map y = serialClone(x); assertNotSame(x, y); - assertEquals(x.size(), y.size()); - assertEquals(x, y); - assertEquals(y, x); + mustEqual(x.size(), y.size()); + mustEqual(x, y); + mustEqual(y, x); } /** * SetValue of an EntrySet entry sets value in the map. */ + @SuppressWarnings("unchecked") public void testSetValueWriteThrough() { // Adapted from a bug report by Eric Zoerner - ConcurrentHashMap map = new ConcurrentHashMap(2, 5.0f, 1); + ConcurrentHashMap map = new ConcurrentHashMap<>(2, 5.0f, 1); assertTrue(map.isEmpty()); for (int i = 0; i < 20; i++) - map.put(new Integer(i), new Integer(i)); + map.put(itemFor(i), itemFor(i)); assertFalse(map.isEmpty()); - Map.Entry entry1 = (Map.Entry)map.entrySet().iterator().next(); + Item key = itemFor(16); + Map.Entry entry1 = map.entrySet().iterator().next(); // Unless it happens to be first (in which case remainder of // test is skipped), remove a possibly-colliding key from map // which, under some implementations, may cause entry1 to be // cloned in map - if (!entry1.getKey().equals(new Integer(16))) { - map.remove(new Integer(16)); + if (!entry1.getKey().equals(key)) { + map.remove(key); entry1.setValue("XYZ"); assertTrue(map.containsValue("XYZ")); // fails if write-through broken } @@ -827,14 +828,31 @@ public class ConcurrentHashMapTest exten 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 }); + final ConcurrentHashMap map = new ConcurrentHashMap<>(); + for (int i = 0; i < mapSize; i++) { + Item I = itemFor(i); + map.put(I, I); + } + Set keySet = map.keySet(); + Collection removeMe = Arrays.asList(new Item[] { minusOne, minusTwo }); for (int i = 0; i < iterations; i++) assertFalse(keySet.removeAll(removeMe)); - assertEquals(mapSize, map.size()); + mustEqual(mapSize, map.size()); } + public void testReentrantComputeIfAbsent() { + ConcurrentHashMap map = new ConcurrentHashMap<>(16); + try { + for (int i = 0; i < 100; i++) { // force a resize + map.computeIfAbsent(new Item(i), key -> new Item(findValue(map, key))); + } + fail("recursive computeIfAbsent should throw IllegalStateException"); + } catch (IllegalStateException success) {} + } + + private static Item findValue(ConcurrentHashMap map, + Item key) { + return (key.value % 5 == 0) ? key : + map.computeIfAbsent(new Item(key.value + 1), k -> new Item(findValue(map, k))); + } }