--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2017/03/18 20:42:20 1.54 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2018/10/05 19:01:35 1.59 @@ -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,14 +19,25 @@ 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())); } /** @@ -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,8 +123,7 @@ 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++) { assertNull(m.put(new CI(i), true)); } @@ -133,13 +139,12 @@ 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); + 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) @@ -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; @@ -784,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(); @@ -835,4 +838,23 @@ public class ConcurrentHashMapTest exten assertEquals(mapSize, map.size()); } + + + public void testReentrantComputeIfAbsent() { + ConcurrentHashMap map = new ConcurrentHashMap<>(16); + try { + for (int i = 0; i < 100; i++) { // force a resize + map.computeIfAbsent(i, key -> findValue(map, key)); + } + fail("recursive computeIfAbsent"); + } catch (IllegalStateException ex) { + } + } + + private Integer findValue(ConcurrentHashMap map, + Integer key) { + return (key % 5 == 0) ? key : + map.computeIfAbsent(key + 1, k -> findValue(map, k)); + } + }