--- jsr166/src/test/tck/PriorityQueueTest.java 2015/01/17 22:55:06 1.30 +++ jsr166/src/test/tck/PriorityQueueTest.java 2016/10/16 20:44:18 1.38 @@ -19,7 +19,7 @@ import junit.framework.TestSuite; public class PriorityQueueTest extends JSR166TestCase { public static void main(String[] args) { - junit.textui.TestRunner.run(suite()); + main(suite(), args); } public static Test suite() { return new TestSuite(PriorityQueueTest.class); @@ -33,17 +33,18 @@ public class PriorityQueueTest extends J /** * Returns a new queue of given size containing consecutive - * Integers 0 ... n. + * Integers 0 ... n - 1. */ private PriorityQueue populatedQueue(int n) { PriorityQueue q = new PriorityQueue(n); assertTrue(q.isEmpty()); - for (int i = n-1; i >= 0; i -= 2) + for (int i = n - 1; i >= 0; i -= 2) assertTrue(q.offer(new Integer(i))); for (int i = (n & 1); i < n; i += 2) assertTrue(q.offer(new Integer(i))); assertFalse(q.isEmpty()); assertEquals(n, q.size()); + assertEquals((Integer) 0, q.peek()); return q; } @@ -59,7 +60,7 @@ public class PriorityQueueTest extends J */ public void testConstructor2() { try { - PriorityQueue q = new PriorityQueue(0); + new PriorityQueue(0); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -69,7 +70,7 @@ public class PriorityQueueTest extends J */ public void testConstructor3() { try { - PriorityQueue q = new PriorityQueue((Collection)null); + new PriorityQueue((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -79,8 +80,7 @@ public class PriorityQueueTest extends J */ public void testConstructor4() { try { - Integer[] ints = new Integer[SIZE]; - PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); + new PriorityQueue(Arrays.asList(new Integer[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -89,11 +89,11 @@ public class PriorityQueueTest extends J * Initializing from Collection with some null elements throws NPE */ public void testConstructor5() { + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); - PriorityQueue q = new PriorityQueue(Arrays.asList(ints)); + new PriorityQueue(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -121,7 +121,7 @@ public class PriorityQueueTest extends J 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) + for (int i = SIZE - 1; i >= 0; --i) assertEquals(ints[i], q.poll()); } @@ -145,7 +145,7 @@ public class PriorityQueueTest extends J public void testSize() { PriorityQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); q.remove(); } for (int i = 0; i < SIZE; ++i) { @@ -158,8 +158,8 @@ public class PriorityQueueTest extends J * offer(null) throws NPE */ public void testOfferNull() { + PriorityQueue q = new PriorityQueue(1); try { - PriorityQueue q = new PriorityQueue(1); q.offer(null); shouldThrow(); } catch (NullPointerException success) {} @@ -169,8 +169,8 @@ public class PriorityQueueTest extends J * add(null) throws NPE */ public void testAddNull() { + PriorityQueue q = new PriorityQueue(1); try { - PriorityQueue q = new PriorityQueue(1); q.add(null); shouldThrow(); } catch (NullPointerException success) {} @@ -189,13 +189,15 @@ public class PriorityQueueTest extends J * Offer of non-Comparable throws CCE */ public void testOfferNonComparable() { + PriorityQueue q = new PriorityQueue(1); try { - PriorityQueue q = new PriorityQueue(1); - q.offer(new Object()); - q.offer(new Object()); q.offer(new Object()); shouldThrow(); - } catch (ClassCastException success) {} + } catch (ClassCastException success) { + assertTrue(q.isEmpty()); + assertEquals(0, q.size()); + assertNull(q.poll()); + } } /** @@ -213,8 +215,8 @@ public class PriorityQueueTest extends J * addAll(null) throws NPE */ public void testAddAll1() { + PriorityQueue q = new PriorityQueue(1); try { - PriorityQueue q = new PriorityQueue(1); q.addAll(null); shouldThrow(); } catch (NullPointerException success) {} @@ -224,10 +226,9 @@ public class PriorityQueueTest extends J * addAll of a collection with null elements throws NPE */ public void testAddAll2() { + PriorityQueue q = new PriorityQueue(SIZE); try { - PriorityQueue q = new PriorityQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - q.addAll(Arrays.asList(ints)); + q.addAll(Arrays.asList(new Integer[SIZE])); shouldThrow(); } catch (NullPointerException success) {} } @@ -237,11 +238,11 @@ public class PriorityQueueTest extends J * possibly adding some elements */ public void testAddAll3() { + PriorityQueue q = new PriorityQueue(SIZE); + Integer[] ints = new Integer[SIZE]; + for (int i = 0; i < SIZE - 1; ++i) + ints[i] = new Integer(i); try { - PriorityQueue q = new PriorityQueue(SIZE); - Integer[] ints = new Integer[SIZE]; - for (int i = 0; i < SIZE-1; ++i) - ints[i] = new Integer(i); q.addAll(Arrays.asList(ints)); shouldThrow(); } catch (NullPointerException success) {} @@ -254,7 +255,7 @@ public class PriorityQueueTest extends J Integer[] empty = new Integer[0]; Integer[] ints = new Integer[SIZE]; for (int i = 0; i < SIZE; ++i) - ints[i] = new Integer(SIZE-1-i); + ints[i] = new Integer(SIZE - 1 - i); PriorityQueue q = new PriorityQueue(SIZE); assertFalse(q.addAll(Arrays.asList(empty))); assertTrue(q.addAll(Arrays.asList(ints))); @@ -325,14 +326,14 @@ public class PriorityQueueTest extends J assertTrue(q.contains(i)); assertTrue(q.remove(i)); assertFalse(q.contains(i)); - assertTrue(q.contains(i-1)); + 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)); + assertFalse(q.remove(i + 1)); + assertFalse(q.contains(i + 1)); } assertTrue(q.isEmpty()); } @@ -391,7 +392,7 @@ public class PriorityQueueTest extends J assertTrue(changed); assertTrue(q.containsAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); p.remove(); } } @@ -404,7 +405,7 @@ public class PriorityQueueTest extends J PriorityQueue q = populatedQueue(SIZE); PriorityQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); - assertEquals(SIZE-i, q.size()); + assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { Integer x = (Integer)(p.remove()); assertFalse(q.contains(x));