--- jsr166/src/test/tck/PriorityQueueTest.java 2012/02/21 01:54:04 1.24 +++ jsr166/src/test/tck/PriorityQueueTest.java 2016/10/16 20:44:18 1.38 @@ -6,7 +6,6 @@ * Pat Fisher, Mike Judd. */ -import junit.framework.*; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; @@ -15,9 +14,12 @@ import java.util.NoSuchElementException; import java.util.PriorityQueue; import java.util.Queue; +import junit.framework.Test; +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); @@ -30,18 +32,19 @@ public class PriorityQueueTest extends J } /** - * Creates a queue of given size containing consecutive - * Integers 0 ... n. + * Returns a new queue of given size containing consecutive + * 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) + 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; } @@ -57,7 +60,7 @@ public class PriorityQueueTest extends J */ public void testConstructor2() { try { - PriorityQueue q = new PriorityQueue(0); + new PriorityQueue(0); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -67,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) {} } @@ -77,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) {} } @@ -87,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) {} } @@ -119,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()); } @@ -143,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) { @@ -156,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) {} @@ -167,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) {} @@ -187,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()); + } } /** @@ -211,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) {} @@ -222,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) {} } @@ -235,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) {} @@ -252,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))); @@ -319,18 +322,18 @@ public class PriorityQueueTest extends J */ public void testRemoveElement() { PriorityQueue q = populatedQueue(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)); + 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)); - assertFalse(q.remove(i+1)); - assertFalse(q.contains(i+1)); + assertFalse(q.remove(i + 1)); + assertFalse(q.contains(i + 1)); } assertTrue(q.isEmpty()); } @@ -389,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(); } } @@ -402,10 +405,10 @@ 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 I = (Integer)(p.remove()); - assertFalse(q.contains(I)); + Integer x = (Integer)(p.remove()); + assertFalse(q.contains(x)); } } } @@ -439,13 +442,19 @@ public class PriorityQueueTest extends J */ public void testIterator() { PriorityQueue q = populatedQueue(SIZE); - int i = 0; Iterator it = q.iterator(); - while (it.hasNext()) { + int i; + for (i = 0; it.hasNext(); i++) assertTrue(q.contains(it.next())); - ++i; - } assertEquals(i, SIZE); + assertIteratorExhausted(it); + } + + /** + * iterator of empty collection has no elements + */ + public void testEmptyIterator() { + assertIteratorExhausted(new PriorityQueue().iterator()); } /** @@ -485,7 +494,7 @@ public class PriorityQueueTest extends J Queue x = populatedQueue(SIZE); Queue y = serialClone(x); - assertTrue(x != y); + assertNotSame(x, y); assertEquals(x.size(), y.size()); while (!x.isEmpty()) { assertFalse(y.isEmpty());