--- jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2011/11/26 05:42:14 1.25 +++ jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2017/08/04 04:59:23 1.48 @@ -4,7 +4,6 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ -import junit.framework.*; import java.util.Arrays; import java.util.BitSet; import java.util.Collection; @@ -17,9 +16,12 @@ import java.util.Set; import java.util.SortedSet; import java.util.concurrent.ConcurrentSkipListSet; +import junit.framework.Test; +import junit.framework.TestSuite; + public class ConcurrentSkipListSetTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(ConcurrentSkipListSetTest.class); @@ -32,16 +34,16 @@ public class ConcurrentSkipListSetTest e } /** - * Create a set of given size containing consecutive - * Integers 0 ... n. + * Returns a new set of given size containing consecutive + * Integers 0 ... n - 1. */ - private ConcurrentSkipListSet populatedSet(int n) { + private static ConcurrentSkipListSet 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) + for (int i = (n & 1); i < n; i += 2) assertTrue(q.add(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(n, q.size()); @@ -49,9 +51,9 @@ public class ConcurrentSkipListSetTest e } /** - * Create set of first 5 ints + * Returns a new set of first 5 ints. */ - private ConcurrentSkipListSet set5() { + private static ConcurrentSkipListSet set5() { ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertTrue(q.isEmpty()); q.add(one); @@ -75,7 +77,7 @@ public class ConcurrentSkipListSetTest e */ public void testConstructor3() { try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); + new ConcurrentSkipListSet((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -85,8 +87,7 @@ public class ConcurrentSkipListSetTest e */ public void testConstructor4() { try { - Integer[] ints = new Integer[SIZE]; - ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); + new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -95,11 +96,11 @@ public class ConcurrentSkipListSetTest e * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); + new ConcurrentSkipListSet(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -127,7 +128,7 @@ public class ConcurrentSkipListSetTest e for (int i = 0; i < SIZE; ++i) ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); - for (int i = SIZE-1; i >= 0; --i) + for (int i = SIZE - 1; i >= 0; --i) assertEquals(ints[i], q.pollFirst()); } @@ -151,7 +152,7 @@ public class ConcurrentSkipListSetTest e public void testSize() { ConcurrentSkipListSet 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) { @@ -164,8 +165,8 @@ public class ConcurrentSkipListSetTest e * add(null) throws NPE */ public void testAddNull() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -193,21 +194,27 @@ public class ConcurrentSkipListSetTest e * Add of non-Comparable throws CCE */ public void testAddNonComparable() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - q.add(new Object()); q.add(new Object()); q.add(new Object()); shouldThrow(); - } catch (ClassCastException success) {} + } catch (ClassCastException success) { + assertTrue(q.size() < 2); + for (int i = 0, size = q.size(); i < size; i++) + assertSame(Object.class, q.pollFirst().getClass()); + assertNull(q.pollFirst()); + assertTrue(q.isEmpty()); + assertEquals(0, q.size()); + } } /** * addAll(null) throws NPE */ public void testAddAll1() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -217,9 +224,9 @@ public class ConcurrentSkipListSetTest e * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + Integer[] ints = new Integer[SIZE]; try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -230,11 +237,11 @@ public class ConcurrentSkipListSetTest e * possibly adding some elements */ public void testAddAll3() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -247,7 +254,7 @@ public class ConcurrentSkipListSetTest e 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); ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertFalse(q.addAll(Arrays.asList(empty))); assertTrue(q.addAll(Arrays.asList(ints))); @@ -271,7 +278,7 @@ public class ConcurrentSkipListSetTest e */ public void testPollLast() { ConcurrentSkipListSet q = populatedSet(SIZE); - for (int i = SIZE-1; i >= 0; --i) { + for (int i = SIZE - 1; i >= 0; --i) { assertEquals(i, q.pollLast()); } assertNull(q.pollFirst()); @@ -282,18 +289,18 @@ public class ConcurrentSkipListSetTest e */ public void testRemoveElement() { ConcurrentSkipListSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { 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) { + 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()); } @@ -352,7 +359,7 @@ public class ConcurrentSkipListSetTest e assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.pollFirst(); } } @@ -365,10 +372,10 @@ public class ConcurrentSkipListSetTest e ConcurrentSkipListSet q = populatedSet(SIZE); ConcurrentSkipListSet 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)); } } } @@ -461,8 +468,7 @@ public class ConcurrentSkipListSetTest e public void testToArray2() { ConcurrentSkipListSet q = populatedSet(SIZE); Integer[] ints = new Integer[SIZE]; - Integer[] array = q.toArray(ints); - assertSame(ints, array); + assertSame(ints, q.toArray(ints)); for (int i = 0; i < ints.length; i++) assertSame(ints[i], q.pollFirst()); } @@ -472,27 +478,21 @@ public class ConcurrentSkipListSetTest e */ public void testIterator() { ConcurrentSkipListSet 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() { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - int i = 0; - Iterator it = q.iterator(); - while (it.hasNext()) { - assertTrue(q.contains(it.next())); - ++i; - } - assertEquals(0, i); + NavigableSet s = new ConcurrentSkipListSet(); + assertIteratorExhausted(s.iterator()); + assertIteratorExhausted(s.descendingSet().iterator()); } /** @@ -526,13 +526,31 @@ public class ConcurrentSkipListSetTest e } /** - * A deserialized serialized set has same elements + * A cloned set equals original + */ + public void testClone() { + ConcurrentSkipListSet x = populatedSet(SIZE); + ConcurrentSkipListSet y = x.clone(); + + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + assertEquals(x, y); + assertEquals(y, x); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + assertEquals(x.pollFirst(), y.pollFirst()); + } + assertTrue(y.isEmpty()); + } + + /** + * A deserialized/reserialized set equals original */ public void testSerialization() throws Exception { NavigableSet x = populatedSet(SIZE); NavigableSet y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); assertEquals(x, y); assertEquals(y, x); @@ -686,8 +704,20 @@ public class ConcurrentSkipListSetTest e 0, setSize - 1, true, bs); } + /** + * addAll is idempotent + */ + public void testAddAll_idempotent() throws Exception { + Set x = populatedSet(SIZE); + Set y = new ConcurrentSkipListSet(x); + y.addAll(x); + assertEquals(x, y); + assertEquals(y, x); + } + static NavigableSet newSet(Class cl) throws Exception { - NavigableSet result = (NavigableSet) cl.newInstance(); + NavigableSet result = + (NavigableSet) cl.getConstructor().newInstance(); assertEquals(0, result.size()); assertFalse(result.iterator().hasNext()); return result; @@ -720,7 +750,7 @@ public class ConcurrentSkipListSetTest e // Add entries till we're back to original size while (set.size() < size) { int element = min + rnd.nextInt(rangeSize); - assertTrue(element >= min && element<= max); + assertTrue(element >= min && element <= max); put(set, element, bs); } } @@ -746,7 +776,7 @@ public class ConcurrentSkipListSetTest e // Add entries till we're back to original size while (set.size() < size) { int element = min - 5 + rnd.nextInt(rangeSize + 10); - if (element >= min && element<= max) { + if (element >= min && element <= max) { put(set, element, bs); } else { try { @@ -970,7 +1000,7 @@ public class ConcurrentSkipListSetTest e } static boolean eq(Integer i, int j) { - return i == null ? j == -1 : i == j; + return (i == null) ? j == -1 : i == j; } }