--- jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2010/11/18 20:21:53 1.17 +++ jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2014/12/31 20:17:39 1.28 @@ -1,13 +1,18 @@ /* * 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; + +import junit.framework.Test; +import junit.framework.TestSuite; public class ConcurrentSkipListSubSetTest extends JSR166TestCase { public static void main(String[] args) { @@ -24,7 +29,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) { @@ -32,9 +37,9 @@ public class ConcurrentSkipListSubSetTes 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))); assertTrue(q.add(new Integer(-n))); assertTrue(q.add(new Integer(n))); @@ -45,7 +50,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 +68,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 +102,6 @@ public class ConcurrentSkipListSubSetTes assertEquals(0, set0().size()); } - /** * isEmpty is true before add, false after */ @@ -168,7 +172,6 @@ public class ConcurrentSkipListSubSetTes } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -238,13 +241,13 @@ public class ConcurrentSkipListSubSetTes */ public void testRemoveElement() { NavigableSet 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)); } - 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)); @@ -323,14 +326,12 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.removeAll(p)); 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)); } } } - - /** * lower returns preceding element */ @@ -450,7 +451,7 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, 0); + assertEquals(0, i); } /** @@ -472,7 +473,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -480,7 +480,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 +488,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 +677,6 @@ public class ConcurrentSkipListSubSetTes } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -747,10 +746,10 @@ public class ConcurrentSkipListSubSetTes */ public void testDescendingRemoveElement() { NavigableSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { + for (int i = 1; i < SIZE; i += 2) { assertTrue(q.remove(new Integer(i))); } - for (int i = 0; i < SIZE; i+=2) { + for (int i = 0; i < SIZE; i += 2 ) { assertTrue(q.remove(new Integer(i))); assertFalse(q.remove(new Integer(i+1))); } @@ -826,14 +825,12 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.removeAll(p)); 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)); } } } - - /** * lower returns preceding element */ @@ -954,7 +951,7 @@ public class ConcurrentSkipListSubSetTes assertTrue(q.contains(it.next())); ++i; } - assertEquals(i, 0); + assertEquals(0, i); } /** @@ -976,7 +973,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -984,7 +980,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 +988,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()); } /**