--- jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2009/11/21 10:25:05 1.8 +++ jsr166/src/test/tck/ConcurrentSkipListSetTest.java 2010/11/18 20:21:53 1.19 @@ -11,7 +11,7 @@ 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); @@ -19,11 +19,7 @@ public class ConcurrentSkipListSetTest e 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); } } @@ -31,8 +27,9 @@ public class ConcurrentSkipListSetTest e * Create a set of given size containing consecutive * Integers 0 ... n. */ - private ConcurrentSkipListSet populatedSet(int n) { - ConcurrentSkipListSet q = new ConcurrentSkipListSet(); + 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))); @@ -115,18 +112,15 @@ public class ConcurrentSkipListSetTest e * 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()); } /** @@ -210,6 +204,7 @@ public class ConcurrentSkipListSetTest e shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with null elements throws NPE */ @@ -221,6 +216,7 @@ public class ConcurrentSkipListSetTest e shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -240,18 +236,15 @@ public class ConcurrentSkipListSetTest e * 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()); } /** @@ -260,7 +253,7 @@ 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()); } @@ -271,7 +264,7 @@ 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()); } @@ -283,11 +276,17 @@ public class ConcurrentSkipListSetTest e public void testRemoveElement() { ConcurrentSkipListSet 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()); } @@ -385,7 +384,6 @@ public class ConcurrentSkipListSetTest e Object e4 = q.lower(zero); assertNull(e4); - } /** @@ -404,7 +402,6 @@ public class ConcurrentSkipListSetTest e Object e4 = q.higher(six); assertNull(e4); - } /** @@ -423,7 +420,6 @@ public class ConcurrentSkipListSetTest e Object e4 = q.floor(zero); assertNull(e4); - } /** @@ -442,30 +438,28 @@ public class ConcurrentSkipListSetTest e Object e4 = q.ceiling(six); assertNull(e4); - } /** - * toArray contains all elements + * toArray contains all elements in sorted order */ public void testToArray() { ConcurrentSkipListSet 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() { - ConcurrentSkipListSet q = populatedSet(SIZE); + ConcurrentSkipListSet 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()); } /** @@ -499,7 +493,7 @@ public class ConcurrentSkipListSetTest e /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final ConcurrentSkipListSet q = new ConcurrentSkipListSet(); q.add(new Integer(2)); q.add(new Integer(1)); @@ -670,8 +664,8 @@ public class ConcurrentSkipListSetTest e /** * Subsets of subsets subdivide correctly */ - public void testRecursiveSubSets() { - int setSize = 1000; + public void testRecursiveSubSets() throws Exception { + int setSize = expensiveTests ? 1000 : 100; Class cl = ConcurrentSkipListSet.class; NavigableSet set = newSet(cl); @@ -689,13 +683,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; @@ -758,10 +747,8 @@ public class ConcurrentSkipListSetTest e } else { try { set.add(element); - fail(); - } catch (IllegalArgumentException e) { - // expected - } + shouldThrow(); + } catch (IllegalArgumentException success) {} } } } @@ -894,7 +881,7 @@ public class ConcurrentSkipListSetTest e // BitSet should support this! Test would run much faster while (element >= min) { if (bs.get(element)) - return(element); + return element; element--; } return -1; @@ -960,16 +947,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) {} } }