--- jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/03/22 18:09:52 1.5 +++ jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/04/11 19:54:13 1.7 @@ -34,6 +34,105 @@ public class ConcurrentHashMap8Test exte return map; } + // 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 Integer.compare(value, other.value); + } + public boolean equals(Object x) { + return (x instanceof BI) && ((BI)x).value == value; + } + public int hashCode() { return 42; } + } + static class CI extends BI { CI(int value) { super(value); } } + static class DI extends BI { DI(int value) { super(value); } } + + static class BS implements Comparable { + private final String value; + BS(String value) { this.value = value; } + public int compareTo(BS other) { + return value.compareTo(other.value); + } + public boolean equals(Object x) { + return (x instanceof BS) && value.equals(((BS)x).value); + } + public int hashCode() { return 42; } + } + + static class LexicographicList> extends ArrayList + implements Comparable> { + LexicographicList(Collection c) { super(c); } + LexicographicList(E e) { super(Collections.singleton(e)); } + public int compareTo(LexicographicList other) { + int common = Math.min(size(), other.size()); + int r = 0; + for (int i = 0; i < common; i++) { + if ((r = get(i).compareTo(other.get(i))) != 0) + break; + } + if (r == 0) + r = Integer.compare(size(), other.size()); + return r; + } + private static final long serialVersionUID = 0; + } + + /** + * Inserted elements that are subclasses of the same Comparable + * class are found. + */ + public void testComparableFamily() { + ConcurrentHashMap m = new ConcurrentHashMap<>(); + for (int i = 0; i < 1000; i++) { + assertTrue(m.put(new CI(i), true) == null); + } + for (int i = 0; i < 1000; i++) { + assertTrue(m.containsKey(new CI(i))); + assertTrue(m.containsKey(new DI(i))); + } + } + + /** + * Elements of classes with erased generic type parameters based + * on Comparable can be inserted and found. + */ + public void testGenericComparable() { + ConcurrentHashMap m = new ConcurrentHashMap<>(); + for (int i = 0; i < 1000; 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); + assertTrue(m.containsKey(bis)); + if (m.putIfAbsent(bss, true) == null) + 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))))); + } + } + + /** + * Elements of non-comparable classes equal to those of classes + * with erased generic type parameters based on Comparable can be + * inserted and found. + */ + public void testGenericComparable2() { + 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 < 1000; i++) { + LexicographicList bis = new LexicographicList(new BI(i)); + assertTrue(m.containsKey(bis)); + } + } + /** * getOrDefault returns value if present, else default */ @@ -699,7 +798,7 @@ public class ConcurrentHashMap8Test exte assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); } - /* + /** * Mapped reduceKeysSequentially accumulates mapped keys */ public void testMapReduceKeysSequentially() { @@ -709,7 +808,7 @@ public class ConcurrentHashMap8Test exte assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); } - /* + /** * Mapped reduceValuesSequentially accumulates mapped values */ public void testMapReduceValuesSequentially() { @@ -730,7 +829,7 @@ public class ConcurrentHashMap8Test exte assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); } - /* + /** * Mapped reduceKeysInParallel, accumulates mapped keys */ public void testMapReduceKeysInParallel() { @@ -740,7 +839,7 @@ public class ConcurrentHashMap8Test exte assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); } - /* + /** * Mapped reduceValuesInParallel accumulates mapped values */ public void testMapReduceValuesInParallel() { @@ -762,7 +861,7 @@ public class ConcurrentHashMap8Test exte } - /* + /** * reduceKeysToLongSequentially accumulates mapped keys */ public void testReduceKeysToLongSequentially() { @@ -771,7 +870,7 @@ public class ConcurrentHashMap8Test exte assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToIntSequentially accumulates mapped keys */ public void testReduceKeysToIntSequentially() { @@ -780,7 +879,7 @@ public class ConcurrentHashMap8Test exte assertEquals(ir, SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToDoubleSequentially accumulates mapped keys */ public void testReduceKeysToDoubleSequentially() { @@ -789,7 +888,7 @@ public class ConcurrentHashMap8Test exte assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceValuesToLongSequentially accumulates mapped values */ public void testReduceValuesToLongSequentially() { @@ -798,7 +897,7 @@ public class ConcurrentHashMap8Test exte assertEquals(lr, (long)SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToIntSequentially accumulates mapped values */ public void testReduceValuesToIntSequentially() { @@ -807,7 +906,7 @@ public class ConcurrentHashMap8Test exte assertEquals(ir, SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToDoubleSequentially accumulates mapped values */ public void testReduceValuesToDoubleSequentially() { @@ -816,7 +915,7 @@ public class ConcurrentHashMap8Test exte assertEquals(dr, (double)SIZE * (SIZE - 1)); } - /* + /** * reduceKeysToLongInParallel accumulates mapped keys */ public void testReduceKeysToLongInParallel() { @@ -825,7 +924,7 @@ public class ConcurrentHashMap8Test exte assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToIntInParallel accumulates mapped keys */ public void testReduceKeysToIntInParallel() { @@ -834,7 +933,7 @@ public class ConcurrentHashMap8Test exte assertEquals(ir, SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToDoubleInParallel accumulates mapped values */ public void testReduceKeysToDoubleInParallel() { @@ -843,7 +942,7 @@ public class ConcurrentHashMap8Test exte assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceValuesToLongInParallel accumulates mapped values */ public void testReduceValuesToLongInParallel() { @@ -852,7 +951,7 @@ public class ConcurrentHashMap8Test exte assertEquals(lr, (long)SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToIntInParallel accumulates mapped values */ public void testReduceValuesToIntInParallel() { @@ -861,7 +960,7 @@ public class ConcurrentHashMap8Test exte assertEquals(ir, SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToDoubleInParallel accumulates mapped values */ public void testReduceValuesToDoubleInParallel() {