--- jsr166/src/test/tck/ConcurrentHashMap8Test.java 2013/07/21 22:24:18 1.12 +++ jsr166/src/test/tck/ConcurrentHashMap8Test.java 2015/01/17 22:55:06 1.22 @@ -4,12 +4,26 @@ * 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.Collections; +import java.util.Iterator; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; +import java.util.Spliterator; +import java.util.Vector; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentHashMap.KeySetView; +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) { @@ -54,7 +68,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(); @@ -71,7 +85,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(); @@ -88,7 +102,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(); @@ -166,12 +180,12 @@ 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;}); + map.replaceAll((x, y) -> { return x > 3 ? "Z" : y; }); assertEquals("A", map.get(one)); assertEquals("B", map.get(two)); assertEquals("C", map.get(three)); @@ -196,8 +210,8 @@ public class ConcurrentHashMap8Test exte Set set1 = map.keySet(); Set set2 = map.keySet(true); set2.add(six); - assertTrue(((KeySetView)set2).getMap() == map); - assertTrue(((KeySetView)set1).getMap() == map); + 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)); @@ -209,7 +223,6 @@ public class ConcurrentHashMap8Test exte assertFalse(set2.contains(six)); } - /** * keySet.addAll adds each element from the given collection */ @@ -255,68 +268,73 @@ public class ConcurrentHashMap8Test exte 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 e){} + } - /** - * 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); - } - - /** - * 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(); - 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.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) {} + ConcurrentHashMap.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 @@ -401,11 +419,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()); } /** @@ -533,8 +557,6 @@ public class ConcurrentHashMap8Test exte 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); } @@ -986,9 +1008,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); }