--- jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2010/11/18 20:21:53 1.17 +++ jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2014/12/31 16:44:01 1.25 @@ -1,13 +1,16 @@ /* * 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.Comparator; +import java.util.Iterator; +import java.util.NavigableSet; +import java.util.SortedSet; +import java.util.concurrent.ConcurrentSkipListSet; public class ConcurrentSkipListSubSetTest extends JSR166TestCase { public static void main(String[] args) { @@ -24,7 +27,7 @@ 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) { @@ -45,7 +48,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(); @@ -63,7 +66,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(); @@ -97,7 +100,6 @@ public class ConcurrentSkipListSubSetTes assertEquals(0, set0().size()); } - /** * isEmpty is true before add, false after */ @@ -168,7 +170,6 @@ public class ConcurrentSkipListSubSetTes } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -329,8 +330,6 @@ public class ConcurrentSkipListSubSetTes } } - - /** * lower returns preceding element */ @@ -450,7 +449,7 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, 0); + assertEquals(0, i); } /** @@ -472,7 +471,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -480,7 +478,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))); } } @@ -488,18 +486,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()); } /** @@ -677,7 +675,6 @@ public class ConcurrentSkipListSubSetTes } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -832,8 +829,6 @@ public class ConcurrentSkipListSubSetTes } } - - /** * lower returns preceding element */ @@ -954,7 +949,7 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, 0); + assertEquals(0, i); } /** @@ -976,7 +971,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -984,7 +978,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))); } } @@ -992,18 +986,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()); } /**