--- jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2009/11/21 02:07:26 1.8 +++ jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java 2014/12/31 20:09:08 1.27 @@ -1,17 +1,22 @@ /* * 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) { - junit.textui.TestRunner.run (suite()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(ConcurrentSkipListSubSetTest.class); @@ -19,25 +24,22 @@ public class ConcurrentSkipListSubSetTes static class MyReverseComparator implements Comparator { public int compare(Object x, Object y) { - int i = ((Integer)x).intValue(); - int j = ((Integer)y).intValue(); - if (i < j) return 1; - if (i > j) return -1; - return 0; + return ((Comparable)y).compareTo(x); } } /** - * 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) + 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))); @@ -48,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(); @@ -66,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(); @@ -100,7 +102,6 @@ public class ConcurrentSkipListSubSetTes assertEquals(0, set0().size()); } - /** * isEmpty is true before add, false after */ @@ -138,7 +139,7 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = set0(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** @@ -168,11 +169,9 @@ public class ConcurrentSkipListSubSetTes q.add(new Object()); q.add(new Object()); shouldThrow(); - } - catch (ClassCastException success) {} + } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -181,9 +180,9 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = set0(); q.addAll(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** * addAll of a collection with null elements throws NPE */ @@ -193,9 +192,9 @@ public class ConcurrentSkipListSubSetTes Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -208,26 +207,22 @@ public class ConcurrentSkipListSubSetTes ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * Set contains all elements of successful addAll */ public void testAddAll5() { - try { - Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(SIZE-1- i); - NavigableSet q = set0(); - assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < SIZE; ++i) - assertEquals(new Integer(i), q.pollFirst()); - } - finally {} + Integer[] empty = new Integer[0]; + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(SIZE-1- i); + NavigableSet q = set0(); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) + assertEquals(new Integer(i), q.pollFirst()); } /** @@ -236,7 +231,7 @@ public class ConcurrentSkipListSubSetTes public void testPoll() { NavigableSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst()).intValue()); + assertEquals(i, q.pollFirst()); } assertNull(q.pollFirst()); } @@ -246,12 +241,18 @@ public class ConcurrentSkipListSubSetTes */ public void testRemoveElement() { NavigableSet q = populatedSet(SIZE); - for (int i = 1; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - } - for (int i = 0; i < SIZE; i+=2) { - assertTrue(q.remove(new Integer(i))); - assertFalse(q.remove(new Integer(i+1))); + 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)); } assertTrue(q.isEmpty()); } @@ -331,8 +332,6 @@ public class ConcurrentSkipListSubSetTes } } - - /** * lower returns preceding element */ @@ -349,7 +348,6 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.lower(zero); assertNull(e4); - } /** @@ -368,7 +366,6 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.higher(six); assertNull(e4); - } /** @@ -387,7 +384,6 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.floor(zero); assertNull(e4); - } /** @@ -406,30 +402,28 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.ceiling(six); assertNull(e4); - } /** - * 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()); } /** @@ -457,13 +451,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)); @@ -479,7 +473,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -487,31 +480,26 @@ 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))); } } /** * A deserialized serialized set has same elements */ - public void testSerialization() { - NavigableSet q = populatedSet(SIZE); - try { - 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()); - } catch (Exception e) { - e.printStackTrace(); - unexpectedException(); + public void testSerialization() throws Exception { + 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()); } /** @@ -656,7 +644,7 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = dset0(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** @@ -686,11 +674,9 @@ public class ConcurrentSkipListSubSetTes q.add(new Object()); q.add(new Object()); shouldThrow(); - } - catch (ClassCastException success) {} + } catch (ClassCastException success) {} } - /** * addAll(null) throws NPE */ @@ -699,9 +685,9 @@ public class ConcurrentSkipListSubSetTes NavigableSet q = dset0(); q.addAll(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** * addAll of a collection with null elements throws NPE */ @@ -711,9 +697,9 @@ public class ConcurrentSkipListSubSetTes Integer[] ints = new Integer[SIZE]; q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -726,26 +712,22 @@ public class ConcurrentSkipListSubSetTes ints[i] = new Integer(i+SIZE); q.addAll(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * Set contains all elements of successful addAll */ public void testDescendingAddAll5() { - try { - Integer[] empty = new Integer[0]; - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(SIZE-1- i); - NavigableSet q = dset0(); - assertFalse(q.addAll(Arrays.asList(empty))); - assertTrue(q.addAll(Arrays.asList(ints))); - for (int i = 0; i < SIZE; ++i) - assertEquals(new Integer(i), q.pollFirst()); - } - finally {} + Integer[] empty = new Integer[0]; + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(SIZE-1- i); + NavigableSet q = dset0(); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) + assertEquals(new Integer(i), q.pollFirst()); } /** @@ -754,7 +736,7 @@ public class ConcurrentSkipListSubSetTes public void testDescendingPoll() { NavigableSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst()).intValue()); + assertEquals(i, q.pollFirst()); } assertNull(q.pollFirst()); } @@ -764,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))); } @@ -849,8 +831,6 @@ public class ConcurrentSkipListSubSetTes } } - - /** * lower returns preceding element */ @@ -867,7 +847,6 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.lower(zero); assertNull(e4); - } /** @@ -886,7 +865,6 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.higher(m6); assertNull(e4); - } /** @@ -905,7 +883,6 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.floor(zero); assertNull(e4); - } /** @@ -924,7 +901,6 @@ public class ConcurrentSkipListSubSetTes Object e4 = q.ceiling(m6); assertNull(e4); - } /** @@ -944,7 +920,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()); @@ -975,13 +951,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)); @@ -997,7 +973,6 @@ public class ConcurrentSkipListSubSetTes assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -1005,31 +980,26 @@ 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))); } } /** * A deserialized serialized set has same elements */ - public void testDescendingSerialization() { - NavigableSet q = populatedSet(SIZE); - try { - 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()); - } catch (Exception e) { - e.printStackTrace(); - unexpectedException(); + public void testDescendingSerialization() throws Exception { + 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()); } /**