--- jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2011/11/26 05:42:14 1.25 +++ jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2021/01/26 13:33:05 1.51 @@ -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,49 +16,52 @@ 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); } static class MyReverseComparator implements Comparator { + @SuppressWarnings("unchecked") public int compare(Object x, Object y) { return ((Comparable)y).compareTo(x); } } /** - * Create a set of given size containing consecutive - * Integers 0 ... n. + * Returns a new set of given size containing consecutive + * Items 0 ... n - 1. */ - private ConcurrentSkipListSet populatedSet(int n) { - ConcurrentSkipListSet q = - new ConcurrentSkipListSet(); + private static ConcurrentSkipListSet populatedSet(int n) { + ConcurrentSkipListSet q = new ConcurrentSkipListSet<>(); assertTrue(q.isEmpty()); - 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))); + for (int i = n - 1; i >= 0; i -= 2) + mustAdd(q, i); + for (int i = (n & 1); i < n; i += 2) + mustAdd(q, i); assertFalse(q.isEmpty()); - assertEquals(n, q.size()); + mustEqual(n, q.size()); return q; } /** - * Create set of first 5 ints + * Returns a new set of first 5 ints. */ - private ConcurrentSkipListSet set5() { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + private static ConcurrentSkipListSet set5() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertTrue(q.isEmpty()); q.add(one); q.add(two); q.add(three); q.add(four); q.add(five); - assertEquals(5, q.size()); + mustEqual(5, q.size()); return q; } @@ -67,7 +69,7 @@ public class ConcurrentSkipListSetTest e * A new set has unbounded capacity */ public void testConstructor1() { - assertEquals(0, new ConcurrentSkipListSet().size()); + mustEqual(0, new ConcurrentSkipListSet().size()); } /** @@ -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 Item[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -95,11 +96,10 @@ public class ConcurrentSkipListSetTest e * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + Item[] items = new Item[2]; + items[0] = zero; 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(items)); shouldThrow(); } catch (NullPointerException success) {} } @@ -108,12 +108,10 @@ public class ConcurrentSkipListSetTest e * Set contains all elements of collection used to initialize */ public void testConstructor6() { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); + Item[] items = defaultItems; + ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(items)); for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.pollFirst()); + mustEqual(items[i], q.pollFirst()); } /** @@ -121,25 +119,24 @@ public class ConcurrentSkipListSetTest e */ public void testConstructor7() { MyReverseComparator cmp = new MyReverseComparator(); - ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); - assertEquals(cmp, q.comparator()); - Integer[] ints = new Integer[SIZE]; - 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) - assertEquals(ints[i], q.pollFirst()); + @SuppressWarnings("unchecked") + ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp); + mustEqual(cmp, q.comparator()); + Item[] items = defaultItems; + q.addAll(Arrays.asList(items)); + for (int i = SIZE - 1; i >= 0; --i) + mustEqual(items[i], q.pollFirst()); } /** * isEmpty is true before add, false after */ public void testEmpty() { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertTrue(q.isEmpty()); - q.add(new Integer(1)); + mustAdd(q, one); assertFalse(q.isEmpty()); - q.add(new Integer(2)); + mustAdd(q, two); q.pollFirst(); q.pollFirst(); assertTrue(q.isEmpty()); @@ -149,14 +146,14 @@ public class ConcurrentSkipListSetTest e * size changes when elements added and removed */ public void testSize() { - ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); q.pollFirst(); } for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.size()); - q.add(new Integer(i)); + mustEqual(i, q.size()); + mustAdd(q, i); } } @@ -164,8 +161,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) {} @@ -175,7 +172,7 @@ public class ConcurrentSkipListSetTest e * Add of comparable element succeeds */ public void testAdd() { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertTrue(q.add(zero)); assertTrue(q.add(one)); } @@ -184,7 +181,7 @@ public class ConcurrentSkipListSetTest e * Add of duplicate element fails */ public void testAddDup() { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertTrue(q.add(zero)); assertFalse(q.add(zero)); } @@ -193,21 +190,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()); + mustEqual(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,10 +220,10 @@ public class ConcurrentSkipListSetTest e * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + Item[] items = new Item[SIZE]; try { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); + q.addAll(Arrays.asList(items)); shouldThrow(); } catch (NullPointerException success) {} } @@ -230,12 +233,10 @@ public class ConcurrentSkipListSetTest e * possibly adding some elements */ public void testAddAll3() { + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + Item[] items = new Item[2]; items[0] = zero; 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)); + q.addAll(Arrays.asList(items)); shouldThrow(); } catch (NullPointerException success) {} } @@ -244,24 +245,22 @@ public class ConcurrentSkipListSetTest e * Set contains all elements of successful addAll */ public void testAddAll5() { - Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(SIZE-1-i); - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + Item[] empty = new Item[0]; + Item[] items = defaultItems; + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); + assertTrue(q.addAll(Arrays.asList(items))); for (int i = 0; i < SIZE; ++i) - assertEquals(i, q.pollFirst()); + mustEqual(i, q.pollFirst()); } /** * pollFirst succeeds unless empty */ public void testPollFirst() { - ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, q.pollFirst()); + mustEqual(i, q.pollFirst()); } assertNull(q.pollFirst()); } @@ -270,9 +269,9 @@ public class ConcurrentSkipListSetTest e * pollLast succeeds unless empty */ public void testPollLast() { - ConcurrentSkipListSet q = populatedSet(SIZE); - for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, q.pollLast()); + ConcurrentSkipListSet q = populatedSet(SIZE); + for (int i = SIZE - 1; i >= 0; --i) { + mustEqual(i, q.pollLast()); } assertNull(q.pollFirst()); } @@ -281,19 +280,19 @@ public class ConcurrentSkipListSetTest e * remove(x) removes x and returns true if present */ public void testRemoveElement() { - ConcurrentSkipListSet q = populatedSet(SIZE); - 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)); - } - 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)); + ConcurrentSkipListSet q = populatedSet(SIZE); + for (int i = 1; i < SIZE; i += 2) { + mustContain(q, i); + mustRemove(q, i); + mustNotContain(q, i); + mustContain(q, i - 1); + } + for (int i = 0; i < SIZE; i += 2) { + mustContain(q, i); + mustRemove(q, i); + mustNotContain(q, i); + mustNotRemove(q, i + 1); + mustNotContain(q, i + 1); } assertTrue(q.isEmpty()); } @@ -302,11 +301,11 @@ public class ConcurrentSkipListSetTest e * contains(x) reports true when elements added but not yet removed */ public void testContains() { - ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertTrue(q.contains(new Integer(i))); + mustContain(q, i); q.pollFirst(); - assertFalse(q.contains(new Integer(i))); + mustNotContain(q, i); } } @@ -314,11 +313,11 @@ public class ConcurrentSkipListSetTest e * clear removes all elements */ public void testClear() { - ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet q = populatedSet(SIZE); q.clear(); assertTrue(q.isEmpty()); - assertEquals(0, q.size()); - q.add(new Integer(1)); + mustEqual(0, q.size()); + mustAdd(q, one); assertFalse(q.isEmpty()); q.clear(); assertTrue(q.isEmpty()); @@ -328,12 +327,12 @@ public class ConcurrentSkipListSetTest e * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { - ConcurrentSkipListSet q = populatedSet(SIZE); - ConcurrentSkipListSet p = new ConcurrentSkipListSet(); + ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet p = new ConcurrentSkipListSet(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); - p.add(new Integer(i)); + mustAdd(p, i); } assertTrue(p.containsAll(q)); } @@ -342,8 +341,8 @@ public class ConcurrentSkipListSetTest e * retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { - ConcurrentSkipListSet q = populatedSet(SIZE); - ConcurrentSkipListSet p = populatedSet(SIZE); + ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet p = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { boolean changed = q.retainAll(p); if (i == 0) @@ -352,7 +351,7 @@ public class ConcurrentSkipListSetTest e assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); p.pollFirst(); } } @@ -362,13 +361,12 @@ public class ConcurrentSkipListSetTest e */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { - ConcurrentSkipListSet q = populatedSet(SIZE); - ConcurrentSkipListSet p = populatedSet(i); + ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet p = populatedSet(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + mustEqual(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { - Integer I = (Integer)(p.pollFirst()); - assertFalse(q.contains(I)); + mustNotContain(q, p.pollFirst()); } } } @@ -377,12 +375,12 @@ public class ConcurrentSkipListSetTest e * lower returns preceding element */ public void testLower() { - ConcurrentSkipListSet q = set5(); + ConcurrentSkipListSet q = set5(); Object e1 = q.lower(three); - assertEquals(two, e1); + mustEqual(two, e1); Object e2 = q.lower(six); - assertEquals(five, e2); + mustEqual(five, e2); Object e3 = q.lower(one); assertNull(e3); @@ -395,12 +393,12 @@ public class ConcurrentSkipListSetTest e * higher returns next element */ public void testHigher() { - ConcurrentSkipListSet q = set5(); + ConcurrentSkipListSet q = set5(); Object e1 = q.higher(three); - assertEquals(four, e1); + mustEqual(four, e1); Object e2 = q.higher(zero); - assertEquals(one, e2); + mustEqual(one, e2); Object e3 = q.higher(five); assertNull(e3); @@ -413,15 +411,15 @@ public class ConcurrentSkipListSetTest e * floor returns preceding element */ public void testFloor() { - ConcurrentSkipListSet q = set5(); + ConcurrentSkipListSet q = set5(); Object e1 = q.floor(three); - assertEquals(three, e1); + mustEqual(three, e1); Object e2 = q.floor(six); - assertEquals(five, e2); + mustEqual(five, e2); Object e3 = q.floor(one); - assertEquals(one, e3); + mustEqual(one, e3); Object e4 = q.floor(zero); assertNull(e4); @@ -431,15 +429,15 @@ public class ConcurrentSkipListSetTest e * ceiling returns next element */ public void testCeiling() { - ConcurrentSkipListSet q = set5(); + ConcurrentSkipListSet q = set5(); Object e1 = q.ceiling(three); - assertEquals(three, e1); + mustEqual(three, e1); Object e2 = q.ceiling(zero); - assertEquals(one, e2); + mustEqual(one, e2); Object e3 = q.ceiling(five); - assertEquals(five, e3); + mustEqual(five, e3); Object e4 = q.ceiling(six); assertNull(e4); @@ -449,68 +447,64 @@ public class ConcurrentSkipListSetTest e * toArray contains all elements in sorted order */ public void testToArray() { - ConcurrentSkipListSet q = populatedSet(SIZE); - Object[] o = q.toArray(); - for (int i = 0; i < o.length; i++) - assertSame(o[i], q.pollFirst()); + ConcurrentSkipListSet q = populatedSet(SIZE); + Object[] a = q.toArray(); + assertSame(Object[].class, a.getClass()); + for (Object o : a) + assertSame(o, q.pollFirst()); + assertTrue(q.isEmpty()); } /** * toArray(a) contains all elements in sorted order */ public void testToArray2() { - ConcurrentSkipListSet q = populatedSet(SIZE); - 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()); + ConcurrentSkipListSet q = populatedSet(SIZE); + Item[] items = new Item[SIZE]; + assertSame(items, q.toArray(items)); + for (Item o : items) + assertSame(o, q.pollFirst()); + assertTrue(q.isEmpty()); } /** * iterator iterates through all elements */ public void testIterator() { - ConcurrentSkipListSet q = populatedSet(SIZE); - int i = 0; - Iterator it = q.iterator(); - while (it.hasNext()) { - assertTrue(q.contains(it.next())); - ++i; - } - assertEquals(i, SIZE); + ConcurrentSkipListSet q = populatedSet(SIZE); + Iterator it = q.iterator(); + int i; + for (i = 0; it.hasNext(); i++) + mustContain(q, it.next()); + mustEqual(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()); } /** * iterator.remove removes current element */ public void testIteratorRemove() { - final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - q.add(new Integer(2)); - q.add(new Integer(1)); - q.add(new Integer(3)); + final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + q.add(two); + q.add(one); + q.add(three); - Iterator it = q.iterator(); + Iterator it = q.iterator(); it.next(); it.remove(); it = q.iterator(); - assertEquals(it.next(), new Integer(2)); - assertEquals(it.next(), new Integer(3)); + mustEqual(it.next(), two); + mustEqual(it.next(), three); assertFalse(it.hasNext()); } @@ -518,7 +512,7 @@ public class ConcurrentSkipListSetTest e * toString contains toStrings of elements */ public void testToString() { - ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet q = populatedSet(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { assertTrue(s.contains(String.valueOf(i))); @@ -526,19 +520,37 @@ 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); + mustEqual(x.size(), y.size()); + mustEqual(x, y); + mustEqual(y, x); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + mustEqual(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); + NavigableSet x = populatedSet(SIZE); + NavigableSet y = serialClone(x); - assertTrue(x != y); - assertEquals(x.size(), y.size()); - assertEquals(x, y); - assertEquals(y, x); + assertNotSame(x, y); + mustEqual(x.size(), y.size()); + mustEqual(x, y); + mustEqual(y, x); while (!x.isEmpty()) { assertFalse(y.isEmpty()); - assertEquals(x.pollFirst(), y.pollFirst()); + mustEqual(x.pollFirst(), y.pollFirst()); } assertTrue(y.isEmpty()); } @@ -547,119 +559,116 @@ public class ConcurrentSkipListSetTest e * subSet returns set with keys in requested range */ public void testSubSetContents() { - ConcurrentSkipListSet set = set5(); - SortedSet sm = set.subSet(two, four); - assertEquals(two, sm.first()); - assertEquals(three, sm.last()); - assertEquals(2, sm.size()); - assertFalse(sm.contains(one)); - assertTrue(sm.contains(two)); - assertTrue(sm.contains(three)); - assertFalse(sm.contains(four)); - assertFalse(sm.contains(five)); - Iterator i = sm.iterator(); - Object k; - k = (Integer)(i.next()); - assertEquals(two, k); - k = (Integer)(i.next()); - assertEquals(three, k); + ConcurrentSkipListSet set = set5(); + SortedSet sm = set.subSet(two, four); + mustEqual(two, sm.first()); + mustEqual(three, sm.last()); + mustEqual(2, sm.size()); + mustNotContain(sm, one); + mustContain(sm, two); + mustContain(sm, three); + mustNotContain(sm, four); + mustNotContain(sm, five); + Iterator i = sm.iterator(); + Item k = i.next(); + mustEqual(two, k); + k = i.next(); + mustEqual(three, k); assertFalse(i.hasNext()); - Iterator j = sm.iterator(); + Iterator j = sm.iterator(); j.next(); j.remove(); - assertFalse(set.contains(two)); - assertEquals(4, set.size()); - assertEquals(1, sm.size()); - assertEquals(three, sm.first()); - assertEquals(three, sm.last()); - assertTrue(sm.remove(three)); + mustNotContain(set, two); + mustEqual(4, set.size()); + mustEqual(1, sm.size()); + mustEqual(three, sm.first()); + mustEqual(three, sm.last()); + mustRemove(sm, three); assertTrue(sm.isEmpty()); - assertEquals(3, set.size()); + mustEqual(3, set.size()); } public void testSubSetContents2() { - ConcurrentSkipListSet set = set5(); - SortedSet sm = set.subSet(two, three); - assertEquals(1, sm.size()); - assertEquals(two, sm.first()); - assertEquals(two, sm.last()); - assertFalse(sm.contains(one)); - assertTrue(sm.contains(two)); - assertFalse(sm.contains(three)); - assertFalse(sm.contains(four)); - assertFalse(sm.contains(five)); - Iterator i = sm.iterator(); - Object k; - k = (Integer)(i.next()); - assertEquals(two, k); + ConcurrentSkipListSet set = set5(); + SortedSet sm = set.subSet(two, three); + mustEqual(1, sm.size()); + mustEqual(two, sm.first()); + mustEqual(two, sm.last()); + mustNotContain(sm, one); + mustContain(sm, two); + mustNotContain(sm, three); + mustNotContain(sm, four); + mustNotContain(sm, five); + Iterator i = sm.iterator(); + Item k = i.next(); + mustEqual(two, k); assertFalse(i.hasNext()); - Iterator j = sm.iterator(); + Iterator j = sm.iterator(); j.next(); j.remove(); - assertFalse(set.contains(two)); - assertEquals(4, set.size()); - assertEquals(0, sm.size()); + mustNotContain(set, two); + mustEqual(4, set.size()); + mustEqual(0, sm.size()); assertTrue(sm.isEmpty()); - assertFalse(sm.remove(three)); - assertEquals(4, set.size()); + mustNotRemove(sm, three); + mustEqual(4, set.size()); } /** * headSet returns set with keys in requested range */ public void testHeadSetContents() { - ConcurrentSkipListSet set = set5(); - SortedSet sm = set.headSet(four); - assertTrue(sm.contains(one)); - assertTrue(sm.contains(two)); - assertTrue(sm.contains(three)); - assertFalse(sm.contains(four)); - assertFalse(sm.contains(five)); - Iterator i = sm.iterator(); - Object k; - k = (Integer)(i.next()); - assertEquals(one, k); - k = (Integer)(i.next()); - assertEquals(two, k); - k = (Integer)(i.next()); - assertEquals(three, k); + ConcurrentSkipListSet set = set5(); + SortedSet sm = set.headSet(four); + mustContain(sm, one); + mustContain(sm, two); + mustContain(sm, three); + mustNotContain(sm, four); + mustNotContain(sm, five); + Iterator i = sm.iterator(); + Item k = i.next(); + mustEqual(one, k); + k = i.next(); + mustEqual(two, k); + k = i.next(); + mustEqual(three, k); assertFalse(i.hasNext()); sm.clear(); assertTrue(sm.isEmpty()); - assertEquals(2, set.size()); - assertEquals(four, set.first()); + mustEqual(2, set.size()); + mustEqual(four, set.first()); } /** * tailSet returns set with keys in requested range */ public void testTailSetContents() { - ConcurrentSkipListSet set = set5(); - SortedSet sm = set.tailSet(two); - assertFalse(sm.contains(one)); - assertTrue(sm.contains(two)); - assertTrue(sm.contains(three)); - assertTrue(sm.contains(four)); - assertTrue(sm.contains(five)); - Iterator i = sm.iterator(); - Object k; - k = (Integer)(i.next()); - assertEquals(two, k); - k = (Integer)(i.next()); - assertEquals(three, k); - k = (Integer)(i.next()); - assertEquals(four, k); - k = (Integer)(i.next()); - assertEquals(five, k); + ConcurrentSkipListSet set = set5(); + SortedSet sm = set.tailSet(two); + mustNotContain(sm, one); + mustContain(sm, two); + mustContain(sm, three); + mustContain(sm, four); + mustContain(sm, five); + mustContain(sm, two); + Iterator i = sm.iterator(); + Item k = i.next(); + mustEqual(two, k); + k = i.next(); + mustEqual(three, k); + k = i.next(); + mustEqual(four, k); + k = i.next(); + mustEqual(five, k); assertFalse(i.hasNext()); - SortedSet ssm = sm.tailSet(four); - assertEquals(four, ssm.first()); - assertEquals(five, ssm.last()); - assertTrue(ssm.remove(four)); - assertEquals(1, ssm.size()); - assertEquals(3, sm.size()); - assertEquals(4, set.size()); + SortedSet ssm = sm.tailSet(four); + mustEqual(four, ssm.first()); + mustEqual(five, ssm.last()); + mustRemove(ssm, four); + mustEqual(1, ssm.size()); + mustEqual(3, sm.size()); + mustEqual(4, set.size()); } Random rnd = new Random(666); @@ -669,9 +678,9 @@ public class ConcurrentSkipListSetTest e */ public void testRecursiveSubSets() throws Exception { int setSize = expensiveTests ? 1000 : 100; - Class cl = ConcurrentSkipListSet.class; + Class cl = ConcurrentSkipListSet.class; - NavigableSet set = newSet(cl); + NavigableSet set = newSet(cl); BitSet bs = new BitSet(setSize); populate(set, setSize, bs); @@ -682,25 +691,38 @@ public class ConcurrentSkipListSetTest e check(set, 0, setSize - 1, true, bs); check(set.descendingSet(), 0, setSize - 1, false, bs); - bashSubSet(set.subSet(0, true, setSize, false), + bashSubSet(set.subSet(zero, true, itemFor(setSize), false), 0, setSize - 1, true, bs); } - static NavigableSet newSet(Class cl) throws Exception { - NavigableSet result = (NavigableSet) cl.newInstance(); - assertEquals(0, result.size()); + /** + * addAll is idempotent + */ + public void testAddAll_idempotent() throws Exception { + Set x = populatedSet(SIZE); + Set y = new ConcurrentSkipListSet(x); + y.addAll(x); + mustEqual(x, y); + mustEqual(y, x); + } + + static NavigableSet newSet(Class cl) throws Exception { + @SuppressWarnings("unchecked") + NavigableSet result = + (NavigableSet) cl.getConstructor().newInstance(); + mustEqual(0, result.size()); assertFalse(result.iterator().hasNext()); return result; } - void populate(NavigableSet set, int limit, BitSet bs) { + void populate(NavigableSet set, int limit, BitSet bs) { for (int i = 0, n = 2 * limit / 3; i < n; i++) { int element = rnd.nextInt(limit); put(set, element, bs); } } - void mutateSet(NavigableSet set, int min, int max, BitSet bs) { + void mutateSet(NavigableSet set, int min, int max, BitSet bs) { int size = set.size(); int rangeSize = max - min + 1; @@ -710,9 +732,9 @@ public class ConcurrentSkipListSetTest e } // Remove a bunch of entries with iterator - for (Iterator it = set.iterator(); it.hasNext(); ) { + for (Iterator it = set.iterator(); it.hasNext(); ) { if (rnd.nextBoolean()) { - bs.clear(it.next()); + bs.clear(it.next().value); it.remove(); } } @@ -720,12 +742,12 @@ 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); } } - void mutateSubSet(NavigableSet set, int min, int max, + void mutateSubSet(NavigableSet set, int min, int max, BitSet bs) { int size = set.size(); int rangeSize = max - min + 1; @@ -736,9 +758,9 @@ public class ConcurrentSkipListSetTest e } // Remove a bunch of entries with iterator - for (Iterator it = set.iterator(); it.hasNext(); ) { + for (Iterator it = set.iterator(); it.hasNext(); ) { if (rnd.nextBoolean()) { - bs.clear(it.next()); + bs.clear(it.next().value); it.remove(); } } @@ -746,28 +768,28 @@ 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 { - set.add(element); + set.add(itemFor(element)); shouldThrow(); } catch (IllegalArgumentException success) {} } } } - void put(NavigableSet set, int element, BitSet bs) { - if (set.add(element)) + void put(NavigableSet set, int element, BitSet bs) { + if (set.add(itemFor(element))) bs.set(element); } - void remove(NavigableSet set, int element, BitSet bs) { - if (set.remove(element)) + void remove(NavigableSet set, int element, BitSet bs) { + if (set.remove(itemFor(element))) bs.clear(element); } - void bashSubSet(NavigableSet set, + void bashSubSet(NavigableSet set, int min, int max, boolean ascending, BitSet bs) { check(set, min, max, ascending, bs); @@ -784,7 +806,7 @@ public class ConcurrentSkipListSetTest e // headSet - pick direction and endpoint inclusion randomly boolean incl = rnd.nextBoolean(); - NavigableSet hm = set.headSet(midPoint, incl); + NavigableSet hm = set.headSet(itemFor(midPoint), incl); if (ascending) { if (rnd.nextBoolean()) bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs); @@ -801,7 +823,7 @@ public class ConcurrentSkipListSetTest e // tailSet - pick direction and endpoint inclusion randomly incl = rnd.nextBoolean(); - NavigableSet tm = set.tailSet(midPoint,incl); + NavigableSet tm = set.tailSet(itemFor(midPoint),incl); if (ascending) { if (rnd.nextBoolean()) bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs); @@ -826,8 +848,8 @@ public class ConcurrentSkipListSetTest e boolean lowIncl = rnd.nextBoolean(); boolean highIncl = rnd.nextBoolean(); if (ascending) { - NavigableSet sm = set.subSet( - endpoints[0], lowIncl, endpoints[1], highIncl); + NavigableSet sm = set.subSet( + itemFor(endpoints[0]), lowIncl, itemFor(endpoints[1]), highIncl); if (rnd.nextBoolean()) bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), endpoints[1] - (highIncl ? 0 : 1), true, bs); @@ -835,8 +857,8 @@ public class ConcurrentSkipListSetTest e bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1), endpoints[1] - (highIncl ? 0 : 1), false, bs); } else { - NavigableSet sm = set.subSet( - endpoints[1], highIncl, endpoints[0], lowIncl); + NavigableSet sm = set.subSet( + itemFor(endpoints[1]), highIncl, itemFor(endpoints[0]), lowIncl); if (rnd.nextBoolean()) bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1), endpoints[1] - (highIncl ? 0 : 1), false, bs); @@ -849,7 +871,7 @@ public class ConcurrentSkipListSetTest e /** * min and max are both inclusive. If max < min, interval is empty. */ - void check(NavigableSet set, + void check(NavigableSet set, final int min, final int max, final boolean ascending, final BitSet bs) { class ReferenceSet { @@ -918,30 +940,31 @@ public class ConcurrentSkipListSetTest e int size = 0; for (int i = min; i <= max; i++) { boolean bsContainsI = bs.get(i); - assertEquals(bsContainsI, set.contains(i)); + mustEqual(bsContainsI, set.contains(itemFor(i))); if (bsContainsI) size++; } - assertEquals(size, set.size()); + mustEqual(size, set.size()); // Test contents using contains elementSet iterator int size2 = 0; int previousElement = -1; - for (int element : set) { - assertTrue(bs.get(element)); + for (Item element : set) { + assertTrue(bs.get(element.value)); size2++; assertTrue(previousElement < 0 || (ascending ? - element - previousElement > 0 : element - previousElement < 0)); - previousElement = element; + element.value - previousElement > 0 : element.value - previousElement < 0)); + previousElement = element.value; } - assertEquals(size2, size); + mustEqual(size2, size); // Test navigation ops for (int element = min - 1; element <= max + 1; element++) { - assertEq(set.lower(element), rs.lower(element)); - assertEq(set.floor(element), rs.floor(element)); - assertEq(set.higher(element), rs.higher(element)); - assertEq(set.ceiling(element), rs.ceiling(element)); + Item e = itemFor(element); + assertEq(set.lower(e), rs.lower(element)); + assertEq(set.floor(e), rs.floor(element)); + assertEq(set.higher(e), rs.higher(element)); + assertEq(set.ceiling(e), rs.ceiling(element)); } // Test extrema @@ -949,8 +972,8 @@ public class ConcurrentSkipListSetTest e assertEq(set.first(), rs.first()); assertEq(set.last(), rs.last()); } else { - assertEq(rs.first(), -1); - assertEq(rs.last(), -1); + mustEqual(rs.first(), -1); + mustEqual(rs.last(), -1); try { set.first(); shouldThrow(); @@ -962,15 +985,15 @@ public class ConcurrentSkipListSetTest e } } - static void assertEq(Integer i, int j) { + static void assertEq(Item i, int j) { if (i == null) - assertEquals(j, -1); + mustEqual(j, -1); else - assertEquals((int) i, j); + mustEqual(i, j); } - static boolean eq(Integer i, int j) { - return i == null ? j == -1 : i == j; + static boolean eq(Item i, int j) { + return (i == null) ? j == -1 : i.value == j; } }