--- jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2009/12/01 09:48:13 1.12 +++ jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2013/05/30 03:28:55 1.24 @@ -1,17 +1,25 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ */ import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; -import java.io.*; +import java.util.Arrays; +import java.util.BitSet; +import java.util.Collection; +import java.util.Comparator; +import java.util.Iterator; +import java.util.NavigableSet; +import java.util.NoSuchElementException; +import java.util.Random; +import java.util.Set; +import java.util.SortedSet; +import java.util.concurrent.ConcurrentSkipListSet; public class ConcurrentSkipListSubSetTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(ConcurrentSkipListSubSetTest.class); @@ -24,11 +32,12 @@ public class ConcurrentSkipListSubSetTes } /** - * Create a set of given size containing consecutive + * Returns a new set of given size containing consecutive * Integers 0 ... n. */ - private NavigableSet populatedSet(int n) { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + private NavigableSet populatedSet(int n) { + ConcurrentSkipListSet q = + new ConcurrentSkipListSet(); assertTrue(q.isEmpty()); for (int i = n-1; i >= 0; i-=2) @@ -44,7 +53,7 @@ public class ConcurrentSkipListSubSetTes } /** - * Create set of first 5 ints + * Returns a new set of first 5 ints. */ private NavigableSet set5() { ConcurrentSkipListSet q = new ConcurrentSkipListSet(); @@ -62,7 +71,7 @@ public class ConcurrentSkipListSubSetTes } /** - * Create set of first 5 negative ints + * Returns a new set of first 5 negative ints. */ private NavigableSet dset5() { ConcurrentSkipListSet q = new ConcurrentSkipListSet(); @@ -96,7 +105,6 @@ public class ConcurrentSkipListSubSetTes assertEquals(0, set0().size()); } - /** * isEmpty is true before add, false after */ @@ -167,7 +175,6 @@ public class ConcurrentSkipListSubSetTes } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -178,6 +185,7 @@ public class ConcurrentSkipListSubSetTes shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with null elements throws NPE */ @@ -189,6 +197,7 @@ public class ConcurrentSkipListSubSetTes shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -236,11 +245,17 @@ public class ConcurrentSkipListSubSetTes public void testRemoveElement() { NavigableSet q = populatedSet(SIZE); for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); + 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.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); + assertTrue(q.contains(i)); + assertTrue(q.remove(i)); + assertFalse(q.contains(i)); + assertFalse(q.remove(i+1)); + assertFalse(q.contains(i+1)); } assertTrue(q.isEmpty()); } @@ -320,8 +335,6 @@ public class ConcurrentSkipListSubSetTes } } - - /** * lower returns preceding element */ @@ -395,26 +408,25 @@ public class ConcurrentSkipListSubSetTes } /** - * toArray contains all elements + * toArray contains all elements in sorted order */ public void testToArray() { NavigableSet q = populatedSet(SIZE); Object[] o = q.toArray(); - Arrays.sort(o); for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.pollFirst()); + assertSame(o[i], q.pollFirst()); } /** - * toArray(a) contains all elements + * toArray(a) contains all elements in sorted order */ public void testToArray2() { - NavigableSet q = populatedSet(SIZE); + NavigableSet q = populatedSet(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); - Arrays.sort(ints); + Integer[] array = q.toArray(ints); + assertSame(ints, array); for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.pollFirst()); + assertSame(ints[i], q.pollFirst()); } /** @@ -442,13 +454,13 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, 0); + assertEquals(0, i); } /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final NavigableSet q = set0(); q.add(new Integer(2)); q.add(new Integer(1)); @@ -464,7 +476,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -472,7 +483,7 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = populatedSet(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } @@ -480,18 +491,18 @@ public class ConcurrentSkipListSubSetTes * A deserialized serialized set has same elements */ public void testSerialization() throws Exception { - NavigableSet q = populatedSet(SIZE); - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - NavigableSet r = (NavigableSet)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.pollFirst(), r.pollFirst()); + NavigableSet x = populatedSet(SIZE); + NavigableSet y = serialClone(x); + + assertNotSame(y, x); + 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()); } /** @@ -669,7 +680,6 @@ public class ConcurrentSkipListSubSetTes } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -680,6 +690,7 @@ public class ConcurrentSkipListSubSetTes shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with null elements throws NPE */ @@ -691,6 +702,7 @@ public class ConcurrentSkipListSubSetTes shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -822,8 +834,6 @@ public class ConcurrentSkipListSubSetTes } } - - /** * lower returns preceding element */ @@ -913,7 +923,7 @@ public class ConcurrentSkipListSubSetTes public void testDescendingToArray2() { NavigableSet q = populatedSet(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); + assertSame(ints, q.toArray(ints)); Arrays.sort(ints); for (int i = 0; i < ints.length; i++) assertEquals(ints[i], q.pollFirst()); @@ -944,13 +954,13 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, 0); + assertEquals(0, i); } /** * iterator.remove removes current element */ - public void testDescendingIteratorRemove () { + public void testDescendingIteratorRemove() { final NavigableSet q = dset0(); q.add(new Integer(2)); q.add(new Integer(1)); @@ -966,7 +976,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -974,7 +983,7 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = populatedSet(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } @@ -982,18 +991,18 @@ public class ConcurrentSkipListSubSetTes * A deserialized serialized set has same elements */ public void testDescendingSerialization() throws Exception { - NavigableSet q = populatedSet(SIZE); - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - NavigableSet r = (NavigableSet)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.pollFirst(), r.pollFirst()); + NavigableSet x = dset5(); + NavigableSet y = serialClone(x); + + assertNotSame(y, x); + 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()); } /**