--- jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/04/11 19:15:20 1.6 +++ jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/12/01 12:19:59 1.17 @@ -6,9 +6,11 @@ import junit.framework.*; import java.util.*; +import static java.util.Spliterator.*; import java.util.function.*; import java.util.concurrent.atomic.LongAdder; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentHashMap.KeySetView; public class ConcurrentHashMap8Test extends JSR166TestCase { public static void main(String[] args) { @@ -34,105 +36,6 @@ 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 */ @@ -152,7 +55,7 @@ public class ConcurrentHashMap8Test exte } /** - * computeIfAbsent does not replace if the key is already present + * computeIfAbsent does not replace if the key is already present */ public void testComputeIfAbsent2() { ConcurrentHashMap map = map5(); @@ -169,7 +72,7 @@ public class ConcurrentHashMap8Test exte } /** - * computeIfPresent does not replace if the key is already present + * computeIfPresent does not replace if the key is already present */ public void testComputeIfPresent() { ConcurrentHashMap map = map5(); @@ -186,7 +89,7 @@ public class ConcurrentHashMap8Test exte } /** - * compute does not replace if the function returns null + * compute does not replace if the function returns null */ public void testCompute() { ConcurrentHashMap map = map5(); @@ -264,6 +167,19 @@ public class ConcurrentHashMap8Test exte return a; } + /* + * replaceAll replaces all matching values. + */ + public void testReplaceAll() { + ConcurrentHashMap map = map5(); + map.replaceAll((x, y) -> {return x > 3 ? "Z" : y;}); + assertEquals("A", map.get(one)); + assertEquals("B", map.get(two)); + assertEquals("C", map.get(three)); + assertEquals("Z", map.get(four)); + assertEquals("Z", map.get(five)); + } + /** * Default-constructed set is empty */ @@ -273,6 +189,29 @@ public class ConcurrentHashMap8Test exte } /** + * keySet.add adds the key with the established value to the map; + * remove removes it. + */ + public void testKeySetAddRemove() { + ConcurrentHashMap map = map5(); + Set set1 = map.keySet(); + Set set2 = map.keySet(true); + set2.add(six); + assertTrue(((KeySetView)set2).getMap() == map); + assertTrue(((KeySetView)set1).getMap() == map); + assertEquals(set2.size(), map.size()); + assertEquals(set1.size(), map.size()); + assertTrue((Boolean)map.get(six)); + assertTrue(set1.contains(six)); + assertTrue(set2.contains(six)); + set2.remove(six); + assertNull(map.get(six)); + assertFalse(set1.contains(six)); + assertFalse(set2.contains(six)); + } + + + /** * keySet.addAll adds each element from the given collection */ public void testAddAll() { @@ -318,6 +257,74 @@ public class ConcurrentHashMap8Test exte } /** + * keySet.add throws UnsupportedOperationException if no default + * mapped value + */ + public void testAdd4() { + Set full = map5().keySet(); + try { + full.add(three); + shouldThrow(); + } catch (UnsupportedOperationException e){} + } + + /** + * keySet.add throws NullPointerException if the specified key is + * null + */ + public void testAdd5() { + Set full = populatedSet(3); + try { + full.add(null); + shouldThrow(); + } catch (NullPointerException e){} + } + + /** + * KeySetView.getMappedValue returns the map's mapped value + */ + public void testGetMappedValue() { + ConcurrentHashMap map = map5(); + assertNull(map.keySet().getMappedValue()); + try { + map.keySet(null); + shouldThrow(); + } catch (NullPointerException e) {} + KeySetView set = map.keySet(one); + set.add(one); + set.add(six); + set.add(seven); + assertTrue(set.getMappedValue() == one); + assertTrue(map.get(one) != one); + assertTrue(map.get(six) == one); + assertTrue(map.get(seven) == one); + } + + void checkSpliteratorCharacteristics(Spliterator sp, + int requiredCharacteristics) { + assertEquals(requiredCharacteristics, + requiredCharacteristics & sp.characteristics()); + } + + /** + * KeySetView.spliterator returns spliterator over the elements in this set + */ + public void testKeySetSpliterator() { + LongAdder adder = new LongAdder(); + ConcurrentHashMap map = map5(); + Set set = map.keySet(); + Spliterator sp = set.spliterator(); + checkSpliteratorCharacteristics(sp, CONCURRENT | DISTINCT | NONNULL); + assertEquals(sp.estimateSize(), map.size()); + Spliterator sp2 = sp.trySplit(); + sp.forEachRemaining((Integer x) -> adder.add(x.longValue())); + long v = adder.sumThenReset(); + sp2.forEachRemaining((Integer x) -> adder.add(x.longValue())); + long v2 = adder.sum(); + assertEquals(v + v2, 15); + } + + /** * keyset.clear removes all elements from the set */ public void testClear() { @@ -530,15 +537,12 @@ public class ConcurrentHashMap8Test exte Set x = populatedSet(size); Set y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); - assertEquals(x.toString(), y.toString()); - assertTrue(Arrays.equals(x.toArray(), y.toArray())); assertEquals(x, y); assertEquals(y, x); } - static final int SIZE = 10000; static ConcurrentHashMap longMap; @@ -566,7 +570,7 @@ public class ConcurrentHashMap8Test exte public void testForEachKeySequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachKeySequentially((Long x) -> adder.add(x.longValue())); + m.forEachKey(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); } @@ -576,7 +580,7 @@ public class ConcurrentHashMap8Test exte public void testForEachValueSequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachValueSequentially((Long x) -> adder.add(x.longValue())); + m.forEachValue(Long.MAX_VALUE, (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), SIZE * (SIZE - 1)); } @@ -586,7 +590,7 @@ public class ConcurrentHashMap8Test exte public void testForEachSequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachSequentially((Long x, Long y) -> adder.add(x.longValue() + y.longValue())); + m.forEach(Long.MAX_VALUE, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } @@ -596,7 +600,7 @@ public class ConcurrentHashMap8Test exte public void testForEachEntrySequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachEntrySequentially((Map.Entry e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); + m.forEachEntry(Long.MAX_VALUE, (Map.Entry e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } @@ -606,7 +610,7 @@ public class ConcurrentHashMap8Test exte public void testForEachKeyInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachKeyInParallel((Long x) -> adder.add(x.longValue())); + m.forEachKey(1L, (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); } @@ -616,7 +620,7 @@ public class ConcurrentHashMap8Test exte public void testForEachValueInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachValueInParallel((Long x) -> adder.add(x.longValue())); + m.forEachValue(1L, (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), SIZE * (SIZE - 1)); } @@ -626,7 +630,7 @@ public class ConcurrentHashMap8Test exte public void testForEachInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachInParallel((Long x, Long y) -> adder.add(x.longValue() + y.longValue())); + m.forEach(1L, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } @@ -636,7 +640,7 @@ public class ConcurrentHashMap8Test exte public void testForEachEntryInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachEntryInParallel((Map.Entry e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); + m.forEachEntry(1L, (Map.Entry e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } @@ -647,7 +651,7 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachKeySequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachKeySequentially((Long x) -> Long.valueOf(4 * x.longValue()), + m.forEachKey(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); } @@ -659,7 +663,7 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachValueSequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachValueSequentially((Long x) -> Long.valueOf(4 * x.longValue()), + m.forEachValue(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); } @@ -671,7 +675,7 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachSequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), + m.forEach(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } @@ -683,7 +687,7 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachEntrySequentially() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachEntrySequentially((Map.Entry e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), + m.forEachEntry(Long.MAX_VALUE, (Map.Entry e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } @@ -695,7 +699,7 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachKeyInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachKeyInParallel((Long x) -> Long.valueOf(4 * x.longValue()), + m.forEachKey(1L, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); } @@ -707,7 +711,7 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachValueInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachValueInParallel((Long x) -> Long.valueOf(4 * x.longValue()), + m.forEachValue(1L, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); } @@ -719,7 +723,7 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), + m.forEach(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } @@ -731,19 +735,18 @@ public class ConcurrentHashMap8Test exte public void testMappedForEachEntryInParallel() { LongAdder adder = new LongAdder(); ConcurrentHashMap m = longMap(); - m.forEachEntryInParallel((Map.Entry e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), + m.forEachEntry(1L, (Map.Entry e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), (Long x) -> adder.add(x.longValue())); assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } - /** * reduceKeysSequentially accumulates across all keys, */ public void testReduceKeysSequentially() { ConcurrentHashMap m = longMap(); Long r; - r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); + r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); } @@ -753,18 +756,17 @@ public class ConcurrentHashMap8Test exte public void testReduceValuesSequentially() { ConcurrentHashMap m = longMap(); Long r; - r = m.reduceKeysSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); + r = m.reduceKeys(Long.MAX_VALUE, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); } - /** * reduceEntriesSequentially accumulates across all entries */ public void testReduceEntriesSequentially() { ConcurrentHashMap m = longMap(); Map.Entry r; - r = m.reduceEntriesSequentially(new AddKeys()); + r = m.reduceEntries(Long.MAX_VALUE, new AddKeys()); assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); } @@ -774,7 +776,7 @@ public class ConcurrentHashMap8Test exte public void testReduceKeysInParallel() { ConcurrentHashMap m = longMap(); Long r; - r = m.reduceKeysInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); + r = m.reduceKeys(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); } @@ -784,7 +786,7 @@ public class ConcurrentHashMap8Test exte public void testReduceValuesInParallel() { ConcurrentHashMap m = longMap(); Long r; - r = m.reduceValuesInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); + r = m.reduceValues(1L, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)SIZE * (SIZE - 1)); } @@ -794,26 +796,26 @@ public class ConcurrentHashMap8Test exte public void testReduceEntriesInParallel() { ConcurrentHashMap m = longMap(); Map.Entry r; - r = m.reduceEntriesInParallel(new AddKeys()); + r = m.reduceEntries(1L, new AddKeys()); assertEquals(r.getKey().longValue(), (long)SIZE * (SIZE - 1) / 2); } - /* + /** * Mapped reduceKeysSequentially accumulates mapped keys */ public void testMapReduceKeysSequentially() { ConcurrentHashMap m = longMap(); - Long r = m.reduceKeysSequentially((Long x) -> Long.valueOf(4 * x.longValue()), + Long r = m.reduceKeys(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); } - /* + /** * Mapped reduceValuesSequentially accumulates mapped values */ public void testMapReduceValuesSequentially() { ConcurrentHashMap m = longMap(); - Long r = m.reduceValuesSequentially((Long x) -> Long.valueOf(4 * x.longValue()), + Long r = m.reduceValues(Long.MAX_VALUE, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); } @@ -823,28 +825,28 @@ public class ConcurrentHashMap8Test exte */ public void testMappedReduceSequentially() { ConcurrentHashMap m = longMap(); - Long r = m.reduceSequentially((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), + Long r = m.reduce(Long.MAX_VALUE, (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); } - /* + /** * Mapped reduceKeysInParallel, accumulates mapped keys */ public void testMapReduceKeysInParallel() { ConcurrentHashMap m = longMap(); - Long r = m.reduceKeysInParallel((Long x) -> Long.valueOf(4 * x.longValue()), + Long r = m.reduceKeys(1L, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); } - /* + /** * Mapped reduceValuesInParallel accumulates mapped values */ public void testMapReduceValuesInParallel() { ConcurrentHashMap m = longMap(); - Long r = m.reduceValuesInParallel((Long x) -> Long.valueOf(4 * x.longValue()), + Long r = m.reduceValues(1L, (Long x) -> Long.valueOf(4 * x.longValue()), (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())); assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); } @@ -855,117 +857,116 @@ public class ConcurrentHashMap8Test exte public void testMappedReduceInParallel() { ConcurrentHashMap m = longMap(); Long r; - r = m.reduceInParallel((Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), + r = m.reduce(1L, (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); } - - /* + /** * reduceKeysToLongSequentially accumulates mapped keys */ public void testReduceKeysToLongSequentially() { ConcurrentHashMap m = longMap(); - long lr = m.reduceKeysToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum); + long lr = m.reduceKeysToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum); assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToIntSequentially accumulates mapped keys */ public void testReduceKeysToIntSequentially() { ConcurrentHashMap m = longMap(); - int ir = m.reduceKeysToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); + int ir = m.reduceKeysToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); assertEquals(ir, SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToDoubleSequentially accumulates mapped keys */ public void testReduceKeysToDoubleSequentially() { ConcurrentHashMap m = longMap(); - double dr = m.reduceKeysToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum); + double dr = m.reduceKeysToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum); assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceValuesToLongSequentially accumulates mapped values */ public void testReduceValuesToLongSequentially() { ConcurrentHashMap m = longMap(); - long lr = m.reduceValuesToLongSequentially((Long x) -> x.longValue(), 0L, Long::sum); + long lr = m.reduceValuesToLong(Long.MAX_VALUE, (Long x) -> x.longValue(), 0L, Long::sum); assertEquals(lr, (long)SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToIntSequentially accumulates mapped values */ public void testReduceValuesToIntSequentially() { ConcurrentHashMap m = longMap(); - int ir = m.reduceValuesToIntSequentially((Long x) -> x.intValue(), 0, Integer::sum); + int ir = m.reduceValuesToInt(Long.MAX_VALUE, (Long x) -> x.intValue(), 0, Integer::sum); assertEquals(ir, SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToDoubleSequentially accumulates mapped values */ public void testReduceValuesToDoubleSequentially() { ConcurrentHashMap m = longMap(); - double dr = m.reduceValuesToDoubleSequentially((Long x) -> x.doubleValue(), 0.0, Double::sum); + double dr = m.reduceValuesToDouble(Long.MAX_VALUE, (Long x) -> x.doubleValue(), 0.0, Double::sum); assertEquals(dr, (double)SIZE * (SIZE - 1)); } - /* + /** * reduceKeysToLongInParallel accumulates mapped keys */ public void testReduceKeysToLongInParallel() { ConcurrentHashMap m = longMap(); - long lr = m.reduceKeysToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum); + long lr = m.reduceKeysToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToIntInParallel accumulates mapped keys */ public void testReduceKeysToIntInParallel() { ConcurrentHashMap m = longMap(); - int ir = m.reduceKeysToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); + int ir = m.reduceKeysToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); assertEquals(ir, SIZE * (SIZE - 1) / 2); } - /* + /** * reduceKeysToDoubleInParallel accumulates mapped values */ public void testReduceKeysToDoubleInParallel() { ConcurrentHashMap m = longMap(); - double dr = m.reduceKeysToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum); + double dr = m.reduceKeysToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); assertEquals(dr, (double)SIZE * (SIZE - 1) / 2); } - /* + /** * reduceValuesToLongInParallel accumulates mapped values */ public void testReduceValuesToLongInParallel() { ConcurrentHashMap m = longMap(); - long lr = m.reduceValuesToLongInParallel((Long x) -> x.longValue(), 0L, Long::sum); + long lr = m.reduceValuesToLong(1L, (Long x) -> x.longValue(), 0L, Long::sum); assertEquals(lr, (long)SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToIntInParallel accumulates mapped values */ public void testReduceValuesToIntInParallel() { ConcurrentHashMap m = longMap(); - int ir = m.reduceValuesToIntInParallel((Long x) -> x.intValue(), 0, Integer::sum); + int ir = m.reduceValuesToInt(1L, (Long x) -> x.intValue(), 0, Integer::sum); assertEquals(ir, SIZE * (SIZE - 1)); } - /* + /** * reduceValuesToDoubleInParallel accumulates mapped values */ public void testReduceValuesToDoubleInParallel() { ConcurrentHashMap m = longMap(); - double dr = m.reduceValuesToDoubleInParallel((Long x) -> x.doubleValue(), 0.0, Double::sum); + double dr = m.reduceValuesToDouble(1L, (Long x) -> x.doubleValue(), 0.0, Double::sum); assertEquals(dr, (double)SIZE * (SIZE - 1)); } @@ -976,9 +977,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.searchKeys(Long.MAX_VALUE, (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.searchKeys(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -989,9 +990,9 @@ public class ConcurrentHashMap8Test exte public void testSearchValuesSequentially() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchValuesSequentially((Long x) -> x.longValue() == (long)(SIZE/2)? x : null); + r = m.searchValues(Long.MAX_VALUE, (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.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -1002,9 +1003,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.search(Long.MAX_VALUE, (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.search(Long.MAX_VALUE, (Long x, Long y) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -1015,9 +1016,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.searchEntries(Long.MAX_VALUE, (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.searchEntries(Long.MAX_VALUE, (Map.Entry e) -> e.getKey().longValue() < 0L ? e.getKey() : null); assertNull(r); } @@ -1028,9 +1029,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.searchKeys(1L, (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.searchKeys(1L, (Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -1041,9 +1042,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.searchValues(1L, (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.searchValues(1L, (Long x) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -1054,9 +1055,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.search(1L, (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.search(1L, (Long x, Long y) -> x.longValue() < 0L ? x : null); assertNull(r); } @@ -1067,109 +1068,10 @@ 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.searchEntries(1L, (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.searchEntries(1L, (Map.Entry e) -> e.getKey().longValue() < 0L ? e.getKey() : null); assertNull(r); } - /** - * Invoking task versions of bulk methods has same effect as - * parallel methods - */ - public void testForkJoinTasks() { - LongAdder adder = new LongAdder(); - ConcurrentHashMap m = longMap(); - ConcurrentHashMap.ForkJoinTasks.forEachKey - (m, (Long x) -> adder.add(x.longValue())).invoke(); - assertEquals(adder.sum(), SIZE * (SIZE - 1) / 2); - adder.reset(); - ConcurrentHashMap.ForkJoinTasks.forEachValue - (m, (Long x) -> adder.add(x.longValue())).invoke(); - assertEquals(adder.sum(), SIZE * (SIZE - 1)); - adder.reset(); - ConcurrentHashMap.ForkJoinTasks.forEach - (m, (Long x, Long y) -> adder.add(x.longValue() + y.longValue())).invoke(); - assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); - adder.reset(); - ConcurrentHashMap.ForkJoinTasks.forEachEntry - (m, - (Map.Entry e) -> adder.add(e.getKey().longValue() + e.getValue().longValue())).invoke(); - assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); - adder.reset(); - ConcurrentHashMap.ForkJoinTasks.forEachKey - (m, (Long x) -> Long.valueOf(4 * x.longValue()), - (Long x) -> adder.add(x.longValue())).invoke(); - assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1) / 2); - adder.reset(); - ConcurrentHashMap.ForkJoinTasks.forEachValue - (m, (Long x) -> Long.valueOf(4 * x.longValue()), - (Long x) -> adder.add(x.longValue())).invoke(); - assertEquals(adder.sum(), 4 * SIZE * (SIZE - 1)); - adder.reset(); - ConcurrentHashMap.ForkJoinTasks.forEach - (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), - (Long x) -> adder.add(x.longValue())).invoke(); - assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); - adder.reset(); - ConcurrentHashMap.ForkJoinTasks.forEachEntry - (m, (Map.Entry e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), - (Long x) -> adder.add(x.longValue())).invoke(); - assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); - adder.reset(); - - Long r; long lr; int ir; double dr; - r = ConcurrentHashMap.ForkJoinTasks.reduceKeys - (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); - assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); - r = ConcurrentHashMap.ForkJoinTasks.reduceValues - (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); - assertEquals((long)r, (long)SIZE * (SIZE - 1)); - r = ConcurrentHashMap.ForkJoinTasks.reduce - (m, (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue()), - (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); - assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); - r = ConcurrentHashMap.ForkJoinTasks.reduceEntries - (m, (Map.Entry e) -> Long.valueOf(e.getKey().longValue() + e.getValue().longValue()), - (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); - assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); - r = ConcurrentHashMap.ForkJoinTasks.reduceKeys - (m, (Long x) -> Long.valueOf(4 * x.longValue()), - (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); - assertEquals((long)r, (long)4 * SIZE * (SIZE - 1) / 2); - lr = ConcurrentHashMap.ForkJoinTasks.reduceKeysToLong - (m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); - assertEquals(lr, (long)SIZE * (SIZE - 1) / 2); - ir = ConcurrentHashMap.ForkJoinTasks.reduceKeysToInt - (m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); - 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); - r = ConcurrentHashMap.ForkJoinTasks.reduceValues - (m, (Long x) -> Long.valueOf(4 * x.longValue()), - (Long x, Long y) -> Long.valueOf(x.longValue() + y.longValue())).invoke(); - assertEquals((long)r, (long)4 * SIZE * (SIZE - 1)); - lr = ConcurrentHashMap.ForkJoinTasks.reduceValuesToLong - (m, (Long x) -> x.longValue(), 0L, Long::sum).invoke(); - assertEquals(lr, (long)SIZE * (SIZE - 1)); - ir = ConcurrentHashMap.ForkJoinTasks.reduceValuesToInt - (m, (Long x) -> x.intValue(), 0, Integer::sum).invoke(); - 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)); - r = ConcurrentHashMap.ForkJoinTasks.searchKeys - (m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); - assertEquals((long)r, (long)(SIZE/2)); - r = ConcurrentHashMap.ForkJoinTasks.searchValues - (m, (Long x) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); - assertEquals((long)r, (long)(SIZE/2)); - r = ConcurrentHashMap.ForkJoinTasks.search - (m, (Long x, Long y) -> x.longValue() == (long)(SIZE/2)? x : null).invoke(); - assertEquals((long)r, (long)(SIZE/2)); - r = ConcurrentHashMap.ForkJoinTasks.searchEntries - (m, (Map.Entry e) -> e.getKey().longValue() == (long)(SIZE/2)? e.getKey() : null).invoke(); - assertEquals((long)r, (long)(SIZE/2)); - } }