--- jsr166/src/test/tck/ConcurrentHashMapTest.java 2017/08/23 05:33:00 1.56 +++ jsr166/src/test/tck/ConcurrentHashMapTest.java 2018/10/05 22:23:34 1.61 @@ -56,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; @@ -100,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; @@ -128,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)); } @@ -145,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) @@ -170,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)); } } @@ -188,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; @@ -847,4 +838,20 @@ 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 should throw IllegalStateException"); + } catch (IllegalStateException success) {} + } + + private Integer findValue(ConcurrentHashMap map, + Integer key) { + return (key % 5 == 0) ? key : + map.computeIfAbsent(key + 1, k -> findValue(map, k)); + } + }