--- jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/03/22 14:11:32 1.3 +++ jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/04/11 19:15:20 1.6 @@ -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 */ @@ -68,7 +167,7 @@ public class ConcurrentHashMap8Test exte map.computeIfAbsent(six, (x) -> null); assertFalse(map.containsKey(six)); } - + /** * computeIfPresent does not replace if the key is already present */ @@ -380,7 +479,7 @@ public class ConcurrentHashMap8Test exte elements[i] = i; Collections.shuffle(Arrays.asList(elements)); Collection full = populatedSet(elements); - + assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray()))); assertTrue(full.containsAll(Arrays.asList(full.toArray()))); assertSame(Object[].class, full.toArray().getClass()); @@ -442,7 +541,7 @@ public class ConcurrentHashMap8Test exte static final int SIZE = 10000; static ConcurrentHashMap longMap; - + static ConcurrentHashMap longMap() { if (longMap == null) { longMap = new ConcurrentHashMap(SIZE); @@ -456,7 +555,7 @@ public class ConcurrentHashMap8Test exte static class AddKeys implements BiFunction, Map.Entry, Map.Entry> { public Map.Entry apply(Map.Entry x, Map.Entry y) { return new AbstractMap.SimpleEntry - (Long.valueOf(x.getKey().longValue() + y.getKey().longValue()), + (Long.valueOf(x.getKey().longValue() + y.getKey().longValue()), Long.valueOf(1L)); } } @@ -726,7 +825,7 @@ public class ConcurrentHashMap8Test exte ConcurrentHashMap m = longMap(); Long r = m.reduceSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); - + assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); } @@ -777,7 +876,7 @@ public class ConcurrentHashMap8Test exte public void testReduceKeysToIntSequentially() { ConcurrentHashMap m = longMap(); int ir = m.reduceKeysToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); - assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); + assertEquals(ir, SIZE * (SIZE - 1) / 2); } /* @@ -804,7 +903,7 @@ public class ConcurrentHashMap8Test exte public void testReduceValuesToIntSequentially() { ConcurrentHashMap m = longMap(); int ir = m.reduceValuesToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); - assertEquals(ir, (int)SIZE * (SIZE - 1)); + assertEquals(ir, SIZE * (SIZE - 1)); } /* @@ -831,7 +930,7 @@ public class ConcurrentHashMap8Test exte public void testReduceKeysToIntInParallel() { ConcurrentHashMap m = longMap(); int ir = m.reduceKeysToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); - assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); + assertEquals(ir, SIZE * (SIZE - 1) / 2); } /* @@ -858,7 +957,7 @@ public class ConcurrentHashMap8Test exte public void testReduceValuesToIntInParallel() { ConcurrentHashMap m = longMap(); int ir = m.reduceValuesToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); - assertEquals(ir, (int)SIZE * (SIZE - 1)); + assertEquals(ir, SIZE * (SIZE - 1)); } /* @@ -877,9 +976,9 @@ public class ConcurrentHashMap8Test exte public void testSearchKeysSequentially() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); + r = m.searchKeysSequentially((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchKeysSequentially((Long x) -> x.longValue() < 0L? x : null); + r = m.searchKeysSequentially((Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -892,7 +991,7 @@ public class ConcurrentHashMap8Test exte Long r; r = m.searchValuesSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchValuesSequentially((Long x) -> x.longValue() < 0L? x : null); + r = m.searchValuesSequentially((Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -903,9 +1002,9 @@ public class ConcurrentHashMap8Test exte public void testSearchSequentially() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null); + r = m.searchSequentially((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchSequentially((Long x, Long y) -> x.longValue() < 0L? x : null); + r = m.searchSequentially((Long x, Long y) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -916,9 +1015,9 @@ public class ConcurrentHashMap8Test exte public void testSearchEntriesSequentially() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchEntriesSequentially((Map.Entry e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null); + r = m.searchEntriesSequentially((Map.Entry e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchEntriesSequentially((Map.Entry e) -> e.getKey().longValue() < 0L? e.getKey() : null); + r = m.searchEntriesSequentially((Map.Entry e) -> e.getKey().longValue() < 0L ? e.getKey() : null); assertNull(r); } @@ -929,9 +1028,9 @@ public class ConcurrentHashMap8Test exte public void testSearchKeysInParallel() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); + r = m.searchKeysInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L? x : null); + r = m.searchKeysInParallel((Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -942,9 +1041,9 @@ public class ConcurrentHashMap8Test exte public void testSearchValuesInParallel() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); + r = m.searchValuesInParallel((Long x) -> x.longValue() == (long)(SIZE/2) ? x : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L? x : null); + r = m.searchValuesInParallel((Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -955,9 +1054,9 @@ public class ConcurrentHashMap8Test exte public void testSearchInParallel() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null); + r = m.searchInParallel((Long x, Long y) -> x.longValue() == (long)(SIZE/2) ? x : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchInParallel((Long x, Long y) -> x.longValue() < 0L? x : null); + r = m.searchInParallel((Long x, Long y) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -968,9 +1067,9 @@ public class ConcurrentHashMap8Test exte public void testSearchEntriesInParallel() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchEntriesInParallel((Map.Entry e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null); + r = m.searchEntriesInParallel((Map.Entry e) -> e.getKey().longValue() == (long)(SIZE/2) ? e.getKey() : null); assertEquals((long)r, (long)(SIZE/2)); - r = m.searchEntriesInParallel((Map.Entry e) -> e.getKey().longValue() < 0L? e.getKey() : null); + r = m.searchEntriesInParallel((Map.Entry e) -> e.getKey().longValue() < 0L ? e.getKey() : null); assertNull(r); } @@ -994,7 +1093,7 @@ public class ConcurrentHashMap8Test exte assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); adder.reset(); ConcurrentHashMap.ForkJoinTasks.forEachEntry - (m, + (m, (Map.Entry e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())).invoke(); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); adder.reset(); @@ -1043,7 +1142,7 @@ public class ConcurrentHashMap8Test exte assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); ir = ConcurrentHashMap.ForkJoinTasks.reduceKeysToInt (m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); - assertEquals(ir, (int)SIZE * (SIZE - 1) / 2); + assertEquals(ir, SIZE * (SIZE - 1) / 2); dr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToDouble (m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); @@ -1056,7 +1155,7 @@ public class ConcurrentHashMap8Test exte assertEquals(lr, (long)SIZE * (SIZE - 1)); ir = ConcurrentHashMap.ForkJoinTasks.reduceValuesToInt (m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); - assertEquals(ir, (int)SIZE * (SIZE - 1)); + assertEquals(ir, SIZE * (SIZE - 1)); dr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToDouble (m, (Long x) -> x.doubleValue(), 0.0, Double::sum).invoke(); assertEquals(dr, (double)SIZE * (SIZE - 1)); @@ -1072,5 +1171,5 @@ public class ConcurrentHashMap8Test exte r = ConcurrentHashMap.ForkJoinTasks.searchEntries (m, (Map.Entry e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null).invoke(); assertEquals((long)r, (long)(SIZE/2)); - } + } }