--- jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2009/11/02 20:28:31 1.4 +++ jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2009/12/01 09:48:13 1.12 @@ -11,19 +11,15 @@ import java.io.*; public class ConcurrentSkipListSetTest 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(ConcurrentSkipListSetTest.class); + return new TestSuite(ConcurrentSkipListSetTest.class); } 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); } } @@ -34,12 +30,12 @@ public class ConcurrentSkipListSetTest e private 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) + assertTrue(q.add(new Integer(i))); + for (int i = (n & 1); i < n; i+=2) + assertTrue(q.add(new Integer(i))); assertFalse(q.isEmpty()); - assertEquals(n, q.size()); + assertEquals(n, q.size()); return q; } @@ -54,7 +50,7 @@ public class ConcurrentSkipListSetTest e q.add(three); q.add(four); q.add(five); - assertEquals(5, q.size()); + assertEquals(5, q.size()); return q; } @@ -72,8 +68,7 @@ public class ConcurrentSkipListSetTest e try { ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -84,8 +79,7 @@ public class ConcurrentSkipListSetTest e Integer[] ints = new Integer[SIZE]; ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** @@ -98,41 +92,34 @@ public class ConcurrentSkipListSetTest e ints[i] = new Integer(i); ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * Set contains all elements of collection used to initialize */ public void testConstructor6() { - try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(i); - ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); - for (int i = 0; i < SIZE; ++i) - assertEquals(ints[i], q.pollFirst()); - } - finally {} + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE; ++i) + ints[i] = new Integer(i); + ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints)); + for (int i = 0; i < SIZE; ++i) + assertEquals(ints[i], q.pollFirst()); } /** * The comparator used in constructor is used */ public void testConstructor7() { - try { - 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()); - } - finally {} + 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()); } /** @@ -168,11 +155,11 @@ public class ConcurrentSkipListSetTest e * add(null) throws NPE */ public void testAddNull() { - try { + try { ConcurrentSkipListSet q = new ConcurrentSkipListSet(); q.add(null); shouldThrow(); - } catch (NullPointerException success) { } + } catch (NullPointerException success) {} } /** @@ -203,8 +190,7 @@ public class ConcurrentSkipListSetTest e q.add(new Object()); q.add(new Object()); shouldThrow(); - } - catch(ClassCastException success) {} + } catch (ClassCastException success) {} } /** @@ -215,8 +201,7 @@ public class ConcurrentSkipListSetTest e ConcurrentSkipListSet q = new ConcurrentSkipListSet(); q.addAll(null); shouldThrow(); - } - catch (NullPointerException success) {} + } catch (NullPointerException success) {} } /** * addAll of a collection with null elements throws NPE @@ -227,8 +212,7 @@ public class ConcurrentSkipListSetTest e 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 @@ -242,26 +226,22 @@ public class ConcurrentSkipListSetTest e ints[i] = new Integer(i); 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); - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); - 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); + ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + assertFalse(q.addAll(Arrays.asList(empty))); + assertTrue(q.addAll(Arrays.asList(ints))); + for (int i = 0; i < SIZE; ++i) + assertEquals(i, q.pollFirst()); } /** @@ -270,9 +250,9 @@ public class ConcurrentSkipListSetTest e public void testPollFirst() { ConcurrentSkipListSet q = populatedSet(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(i, ((Integer)q.pollFirst()).intValue()); + assertEquals(i, q.pollFirst()); } - assertNull(q.pollFirst()); + assertNull(q.pollFirst()); } /** @@ -281,9 +261,9 @@ public class ConcurrentSkipListSetTest e public void testPollLast() { ConcurrentSkipListSet q = populatedSet(SIZE); for (int i = SIZE-1; i >= 0; --i) { - assertEquals(i, ((Integer)q.pollLast()).intValue()); + assertEquals(i, q.pollLast()); } - assertNull(q.pollFirst()); + assertNull(q.pollFirst()); } @@ -395,7 +375,6 @@ public class ConcurrentSkipListSetTest e Object e4 = q.lower(zero); assertNull(e4); - } /** @@ -414,7 +393,6 @@ public class ConcurrentSkipListSetTest e Object e4 = q.higher(six); assertNull(e4); - } /** @@ -433,7 +411,6 @@ public class ConcurrentSkipListSetTest e Object e4 = q.floor(zero); assertNull(e4); - } /** @@ -452,7 +429,6 @@ public class ConcurrentSkipListSetTest e Object e4 = q.ceiling(six); assertNull(e4); - } /** @@ -460,10 +436,10 @@ public class ConcurrentSkipListSetTest e */ public void testToArray() { ConcurrentSkipListSet q = populatedSet(SIZE); - Object[] o = q.toArray(); + Object[] o = q.toArray(); Arrays.sort(o); - for(int i = 0; i < o.length; i++) - assertEquals(o[i], q.pollFirst()); + for (int i = 0; i < o.length; i++) + assertEquals(o[i], q.pollFirst()); } /** @@ -471,10 +447,10 @@ public class ConcurrentSkipListSetTest e */ public void testToArray2() { ConcurrentSkipListSet q = populatedSet(SIZE); - Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); + Integer[] ints = new Integer[SIZE]; + ints = (Integer[])q.toArray(ints); Arrays.sort(ints); - for(int i = 0; i < ints.length; i++) + for (int i = 0; i < ints.length; i++) assertEquals(ints[i], q.pollFirst()); } @@ -484,8 +460,8 @@ public class ConcurrentSkipListSetTest e public void testIterator() { ConcurrentSkipListSet q = populatedSet(SIZE); int i = 0; - Iterator it = q.iterator(); - while(it.hasNext()) { + Iterator it = q.iterator(); + while (it.hasNext()) { assertTrue(q.contains(it.next())); ++i; } @@ -498,8 +474,8 @@ public class ConcurrentSkipListSetTest e public void testEmptyIterator() { ConcurrentSkipListSet q = new ConcurrentSkipListSet(); int i = 0; - Iterator it = q.iterator(); - while(it.hasNext()) { + Iterator it = q.iterator(); + while (it.hasNext()) { assertTrue(q.contains(it.next())); ++i; } @@ -540,24 +516,19 @@ public class ConcurrentSkipListSetTest e /** * A deserialized serialized set has same elements */ - public void testSerialization() { + public void testSerialization() throws Exception { ConcurrentSkipListSet 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)); - ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.pollFirst(), r.pollFirst()); - } catch(Exception e){ - e.printStackTrace(); - unexpectedException(); - } + 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)); + ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject(); + assertEquals(q.size(), r.size()); + while (!q.isEmpty()) + assertEquals(q.pollFirst(), r.pollFirst()); } /** @@ -685,9 +656,9 @@ public class ConcurrentSkipListSetTest e /** * Subsets of subsets subdivide correctly */ - public void testRecursiveSubSets() { - int setSize = 1000; - Class cl = ConcurrentSkipListSet.class; + public void testRecursiveSubSets() throws Exception { + int setSize = 1000; + Class cl = ConcurrentSkipListSet.class; NavigableSet set = newSet(cl); bs = new BitSet(setSize); @@ -704,13 +675,8 @@ public class ConcurrentSkipListSetTest e 0, setSize - 1, true); } - static NavigableSet newSet(Class cl) { - NavigableSet result = null; - try { - result = (NavigableSet) cl.newInstance(); - } catch(Exception e) { - fail(); - } + static NavigableSet newSet(Class cl) throws Exception { + NavigableSet result = (NavigableSet) cl.newInstance(); assertEquals(result.size(), 0); assertFalse(result.iterator().hasNext()); return result; @@ -733,7 +699,7 @@ 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()); it.remove(); @@ -758,7 +724,7 @@ 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()); it.remove(); @@ -773,10 +739,8 @@ public class ConcurrentSkipListSetTest e } else { try { set.add(element); - fail(); - } catch(IllegalArgumentException e) { - // expected - } + shouldThrow(); + } catch (IllegalArgumentException success) {} } } } @@ -975,16 +939,12 @@ public class ConcurrentSkipListSetTest e assertEq(rs.last(), -1); try { set.first(); - fail(); - } catch(NoSuchElementException e) { - // expected - } + shouldThrow(); + } catch (NoSuchElementException success) {} try { set.last(); - fail(); - } catch(NoSuchElementException e) { - // expected - } + shouldThrow(); + } catch (NoSuchElementException success) {} } }