--- jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/05/21 19:11:16 1.9 +++ jsr166/src/test/tck/ConcurrentHashMap8Test.java 2016/08/24 22:22:39 1.31 @@ -4,15 +4,28 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; -import java.util.*; -import java.util.function.*; -import java.util.concurrent.atomic.LongAdder; +import static java.util.Spliterator.CONCURRENT; +import static java.util.Spliterator.DISTINCT; +import static java.util.Spliterator.NONNULL; + +import java.util.AbstractMap; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.Spliterator; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.LongAdder; +import java.util.function.BiFunction; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ConcurrentHashMap8Test extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ConcurrentHashMap8Test.class); @@ -53,7 +66,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(); @@ -70,7 +83,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(); @@ -87,7 +100,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(); @@ -149,8 +162,8 @@ public class ConcurrentHashMap8Test exte Set a = ConcurrentHashMap.newKeySet(); assertTrue(a.isEmpty()); for (int i = 0; i < n; i++) - a.add(i); - assertFalse(a.isEmpty()); + assertTrue(a.add(i)); + assertEquals(n == 0, a.isEmpty()); assertEquals(n, a.size()); return a; } @@ -159,13 +172,26 @@ public class ConcurrentHashMap8Test exte Set a = ConcurrentHashMap.newKeySet(); assertTrue(a.isEmpty()); for (int i = 0; i < elements.length; i++) - a.add(elements[i]); + assertTrue(a.add(elements[i])); assertFalse(a.isEmpty()); assertEquals(elements.length, a.size()); 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 */ public void testNewKeySet() { @@ -174,15 +200,35 @@ 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(((ConcurrentHashMap.KeySetView)set2).getMap() == map); + assertTrue(((ConcurrentHashMap.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() { Set full = populatedSet(3); - Vector v = new Vector(); - v.add(three); - v.add(four); - v.add(five); - full.addAll(v); + assertTrue(full.addAll(Arrays.asList(three, four, five))); + assertEquals(6, full.size()); + assertFalse(full.addAll(Arrays.asList(three, four, five))); assertEquals(6, full.size()); } @@ -192,11 +238,10 @@ public class ConcurrentHashMap8Test exte */ public void testAddAll2() { Set full = populatedSet(3); - Vector v = new Vector(); - v.add(three); - v.add(four); - v.add(one); // will not add this element - full.addAll(v); + // "one" is duplicate and will not be added + assertTrue(full.addAll(Arrays.asList(three, four, one))); + assertEquals(5, full.size()); + assertFalse(full.addAll(Arrays.asList(three, four, one))); assertEquals(5, full.size()); } @@ -205,7 +250,7 @@ public class ConcurrentHashMap8Test exte */ public void testAdd2() { Set full = populatedSet(3); - full.add(one); + assertFalse(full.add(one)); assertEquals(3, full.size()); } @@ -214,11 +259,81 @@ public class ConcurrentHashMap8Test exte */ public void testAdd3() { Set full = populatedSet(3); - full.add(three); + assertTrue(full.add(three)); + assertTrue(full.contains(three)); + assertFalse(full.add(three)); assertTrue(full.contains(three)); } /** + * keySet.add throws UnsupportedOperationException if no default + * mapped value + */ + public void testAdd4() { + Set full = map5().keySet(); + try { + full.add(three); + shouldThrow(); + } catch (UnsupportedOperationException success) {} + } + + /** + * keySet.add throws NullPointerException if the specified key is + * null + */ + public void testAdd5() { + Set full = populatedSet(3); + try { + full.add(null); + shouldThrow(); + } catch (NullPointerException success) {} + } + + /** + * 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 success) {} + ConcurrentHashMap.KeySetView set = map.keySet(one); + assertFalse(set.add(one)); + assertTrue(set.add(six)); + assertTrue(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() { @@ -258,23 +373,20 @@ public class ConcurrentHashMap8Test exte * KeySet.containsAll returns true for collections with subset of elements */ public void testContainsAll() { - Set full = populatedSet(3); - Vector v = new Vector(); - v.add(one); - v.add(two); - assertTrue(full.containsAll(v)); - v.add(six); - assertFalse(full.containsAll(v)); + Collection full = populatedSet(3); + assertTrue(full.containsAll(Arrays.asList())); + assertTrue(full.containsAll(Arrays.asList(one))); + assertTrue(full.containsAll(Arrays.asList(one, two))); + assertFalse(full.containsAll(Arrays.asList(one, two, six))); + assertFalse(full.containsAll(Arrays.asList(six))); } /** * KeySet.isEmpty is true when empty, else false */ public void testIsEmpty() { - Set empty = ConcurrentHashMap.newKeySet(); - Set full = populatedSet(3); - assertTrue(empty.isEmpty()); - assertFalse(full.isEmpty()); + assertTrue(populatedSet(0).isEmpty()); + assertFalse(populatedSet(3).isEmpty()); } /** @@ -293,7 +405,7 @@ public class ConcurrentHashMap8Test exte Integer[] elements = new Integer[size]; for (int i = 0; i < size; i++) elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); + shuffle(elements); Collection full = populatedSet(elements); Iterator it = full.iterator(); @@ -301,11 +413,17 @@ public class ConcurrentHashMap8Test exte assertTrue(it.hasNext()); it.next(); } - assertFalse(it.hasNext()); - try { - it.next(); - shouldThrow(); - } catch (NoSuchElementException success) {} + assertIteratorExhausted(it); + } + + /** + * iterator of empty collections has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(ConcurrentHashMap.newKeySet().iterator()); + assertIteratorExhausted(new ConcurrentHashMap().entrySet().iterator()); + assertIteratorExhausted(new ConcurrentHashMap().values().iterator()); + assertIteratorExhausted(new ConcurrentHashMap().keySet().iterator()); } /** @@ -339,10 +457,9 @@ public class ConcurrentHashMap8Test exte */ public void testRemoveAll() { Set full = populatedSet(3); - Vector v = new Vector(); - v.add(one); - v.add(two); - full.removeAll(v); + assertTrue(full.removeAll(Arrays.asList(one, two))); + assertEquals(1, full.size()); + assertFalse(full.removeAll(Arrays.asList(one, two))); assertEquals(1, full.size()); } @@ -378,7 +495,7 @@ public class ConcurrentHashMap8Test exte Integer[] elements = new Integer[size]; for (int i = 0; i < size; i++) elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); + shuffle(elements); Collection full = populatedSet(elements); assertTrue(Arrays.asList(elements).containsAll(Arrays.asList(full.toArray()))); @@ -398,7 +515,7 @@ public class ConcurrentHashMap8Test exte a = new Integer[0]; assertSame(a, empty.toArray(a)); - a = new Integer[size/2]; + a = new Integer[size / 2]; Arrays.fill(a, 42); assertSame(a, empty.toArray(a)); assertNull(a[0]); @@ -408,7 +525,7 @@ public class ConcurrentHashMap8Test exte Integer[] elements = new Integer[size]; for (int i = 0; i < size; i++) elements[i] = i; - Collections.shuffle(Arrays.asList(elements)); + shuffle(elements); Collection full = populatedSet(elements); Arrays.fill(a, 42); @@ -431,15 +548,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; @@ -637,7 +751,6 @@ public class ConcurrentHashMap8Test exte assertEquals(adder.sum(), 3 * SIZE * (SIZE - 1) / 2); } - /** * reduceKeysSequentially accumulates across all keys, */ @@ -658,7 +771,6 @@ public class ConcurrentHashMap8Test exte assertEquals((long)r, (long)SIZE * (SIZE - 1) / 2); } - /** * reduceEntriesSequentially accumulates across all entries */ @@ -761,7 +873,6 @@ public class ConcurrentHashMap8Test exte assertEquals((long)r, (long)3 * SIZE * (SIZE - 1) / 2); } - /** * reduceKeysToLongSequentially accumulates mapped keys */ @@ -890,9 +1001,11 @@ public class ConcurrentHashMap8Test exte public void testSearchValuesSequentially() { ConcurrentHashMap m = longMap(); Long r; - r = m.searchValues(Long.MAX_VALUE, (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.searchValues(Long.MAX_VALUE, (Long x) -> x.longValue() < 0L ? x : null); + r = m.searchValues(Long.MAX_VALUE, + (Long x) -> (x.longValue() < 0L) ? x : null); assertNull(r); }