--- jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2014/12/31 20:09:08 1.27 +++ jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2018/05/28 21:19:50 1.39 @@ -16,7 +16,7 @@ import junit.framework.TestSuite; public class ConcurrentSkipListSubSetTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ConcurrentSkipListSubSetTest.class); @@ -30,14 +30,13 @@ public class ConcurrentSkipListSubSetTes /** * Returns a new set of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ - private NavigableSet populatedSet(int n) { - ConcurrentSkipListSet q = - new ConcurrentSkipListSet(); + private static NavigableSet populatedSet(int n) { + ConcurrentSkipListSet q = new ConcurrentSkipListSet<>(); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i -= 2) + for (int i = n - 1; i >= 0; i -= 2) assertTrue(q.add(new Integer(i))); for (int i = (n & 1); i < n; i += 2) assertTrue(q.add(new Integer(i))); @@ -52,7 +51,7 @@ public class ConcurrentSkipListSubSetTes /** * Returns a new set of first 5 ints. */ - private NavigableSet set5() { + private static NavigableSet set5() { ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertTrue(q.isEmpty()); q.add(one); @@ -70,7 +69,7 @@ public class ConcurrentSkipListSubSetTes /** * Returns a new set of first 5 negative ints. */ - private NavigableSet dset5() { + private static NavigableSet dset5() { ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertTrue(q.isEmpty()); q.add(m1); @@ -122,7 +121,7 @@ public class ConcurrentSkipListSubSetTes public void testSize() { NavigableSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); q.pollFirst(); } for (int i = 0; i < SIZE; ++i) { @@ -135,8 +134,8 @@ public class ConcurrentSkipListSubSetTes * add(null) throws NPE */ public void testAddNull() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -163,9 +162,8 @@ public class ConcurrentSkipListSubSetTes * Add of non-Comparable throws CCE */ public void testAddNonComparable() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -176,8 +174,8 @@ public class ConcurrentSkipListSubSetTes * addAll(null) throws NPE */ public void testAddAll1() { + NavigableSet q = set0(); try { - NavigableSet q = set0(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -187,9 +185,9 @@ public class ConcurrentSkipListSubSetTes * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + NavigableSet q = set0(); + Integer[] ints = new Integer[SIZE]; try { - NavigableSet q = set0(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -200,11 +198,11 @@ public class ConcurrentSkipListSubSetTes * possibly adding some elements */ public void testAddAll3() { + NavigableSet q = set0(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i + SIZE); try { - NavigableSet q = set0(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -217,7 +215,7 @@ public class ConcurrentSkipListSubSetTes Integer[] empty = new Integer[0]; Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(SIZE-1- i); + ints[i] = new Integer(SIZE - 1 - i); NavigableSet q = set0(); assertFalse(q.addAll(Arrays.asList(empty))); assertTrue(q.addAll(Arrays.asList(ints))); @@ -245,14 +243,14 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); - assertTrue(q.contains(i-1)); + assertTrue(q.contains(i - 1)); } for (int i = 0; i < SIZE; i += 2) { assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); - assertFalse(q.remove(i+1)); - assertFalse(q.contains(i+1)); + assertFalse(q.remove(i + 1)); + assertFalse(q.contains(i + 1)); } assertTrue(q.isEmpty()); } @@ -311,7 +309,7 @@ public class ConcurrentSkipListSubSetTes assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.pollFirst(); } } @@ -324,10 +322,10 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = populatedSet(SIZE); NavigableSet p = populatedSet(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } @@ -409,9 +407,11 @@ public class ConcurrentSkipListSubSetTes */ public void testToArray() { NavigableSet q = populatedSet(SIZE); - Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.pollFirst()); + Object[] a = q.toArray(); + assertSame(Object[].class, a.getClass()); + for (Object o : a) + assertSame(o, q.pollFirst()); + assertTrue(q.isEmpty()); } /** @@ -422,8 +422,9 @@ public class ConcurrentSkipListSubSetTes Integer[] ints = new Integer[SIZE]; Integer[] array = q.toArray(ints); assertSame(ints, array); - for (int i = 0; i < ints.length; i++) - assertSame(ints[i], q.pollFirst()); + for (Integer o : ints) + assertSame(o, q.pollFirst()); + assertTrue(q.isEmpty()); } /** @@ -431,27 +432,19 @@ public class ConcurrentSkipListSubSetTes */ public void testIterator() { NavigableSet q = populatedSet(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); } /** * iterator of empty set has no elements */ public void testEmptyIterator() { - NavigableSet q = set0(); - int i = 0; - Iterator it = q.iterator(); - while (it.hasNext()) { - assertTrue(q.contains(it.next())); - ++i; - } - assertEquals(0, i); + assertIteratorExhausted(set0().iterator()); } /** @@ -485,7 +478,7 @@ public class ConcurrentSkipListSubSetTes } /** - * A deserialized serialized set has same elements + * A deserialized/reserialized set equals original */ public void testSerialization() throws Exception { NavigableSet x = populatedSet(SIZE); @@ -627,7 +620,7 @@ public class ConcurrentSkipListSubSetTes public void testDescendingSize() { NavigableSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); q.pollFirst(); } for (int i = 0; i < SIZE; ++i) { @@ -640,8 +633,8 @@ public class ConcurrentSkipListSubSetTes * add(null) throws NPE */ public void testDescendingAddNull() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -668,9 +661,8 @@ public class ConcurrentSkipListSubSetTes * Add of non-Comparable throws CCE */ public void testDescendingAddNonComparable() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); @@ -681,8 +673,8 @@ public class ConcurrentSkipListSubSetTes * addAll(null) throws NPE */ public void testDescendingAddAll1() { + NavigableSet q = dset0(); try { - NavigableSet q = dset0(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -692,9 +684,9 @@ public class ConcurrentSkipListSubSetTes * addAll of a collection with null elements throws NPE */ public void testDescendingAddAll2() { + NavigableSet q = dset0(); + Integer[] ints = new Integer[SIZE]; try { - NavigableSet q = dset0(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -705,11 +697,11 @@ public class ConcurrentSkipListSubSetTes * possibly adding some elements */ public void testDescendingAddAll3() { + NavigableSet q = dset0(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i + SIZE); try { - NavigableSet q = dset0(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -722,7 +714,7 @@ public class ConcurrentSkipListSubSetTes Integer[] empty = new Integer[0]; Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(SIZE-1- i); + ints[i] = new Integer(SIZE - 1 - i); NavigableSet q = dset0(); assertFalse(q.addAll(Arrays.asList(empty))); assertTrue(q.addAll(Arrays.asList(ints))); @@ -751,7 +743,7 @@ public class ConcurrentSkipListSubSetTes } for (int i = 0; i < SIZE; i += 2 ) { assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); + assertFalse(q.remove(new Integer(i + 1))); } assertTrue(q.isEmpty()); } @@ -810,7 +802,7 @@ public class ConcurrentSkipListSubSetTes assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.pollFirst(); } } @@ -823,10 +815,10 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = populatedSet(SIZE); NavigableSet p = populatedSet(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.pollFirst()); + assertFalse(q.contains(x)); } } } @@ -985,7 +977,7 @@ public class ConcurrentSkipListSubSetTes } /** - * A deserialized serialized set has same elements + * A deserialized/reserialized set equals original */ public void testDescendingSerialization() throws Exception { NavigableSet x = dset5();