--- jsr166/src/test/tck/ConcurrentHashMap8Test.java 2014/12/31 19:05:42 1.18 +++ jsr166/src/test/tck/ConcurrentHashMap8Test.java 2015/02/27 19:47:57 1.23 @@ -17,7 +17,6 @@ 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.atomic.LongAdder; import java.util.function.BiFunction; @@ -165,7 +164,7 @@ public class ConcurrentHashMap8Test exte assertTrue(a.isEmpty()); for (int i = 0; i < n; i++) a.add(i); - assertFalse(a.isEmpty()); + assertEquals(n == 0, a.isEmpty()); assertEquals(n, a.size()); return a; } @@ -180,12 +179,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)); @@ -223,17 +222,14 @@ public class ConcurrentHashMap8Test exte 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()); } @@ -243,11 +239,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()); } @@ -377,23 +372,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()); } /** @@ -420,11 +412,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()); } /** @@ -458,10 +456,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()); } @@ -1003,9 +1000,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); }