--- jsr166/src/test/tck/PriorityQueueTest.java 2013/05/30 03:28:55 1.26 +++ jsr166/src/test/tck/PriorityQueueTest.java 2015/04/25 04:55:31 1.33 @@ -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); @@ -36,9 +38,9 @@ public class PriorityQueueTest extends J 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()); @@ -57,7 +59,7 @@ public class PriorityQueueTest extends J */ public void testConstructor2() { try { - PriorityQueue q = new PriorityQueue(0); + new PriorityQueue(0); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -67,7 +69,7 @@ public class PriorityQueueTest extends J */ public void testConstructor3() { try { - PriorityQueue q = new PriorityQueue((Collection)null); + new PriorityQueue((Collection)null); shouldThrow(); } catch (NullPointerException success) {} } @@ -78,7 +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(ints)); shouldThrow(); } catch (NullPointerException success) {} } @@ -91,7 +93,7 @@ public class PriorityQueueTest extends J 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) {} } @@ -187,9 +189,8 @@ 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(); @@ -319,13 +320,13 @@ 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)); } - 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)); @@ -404,8 +405,8 @@ public class PriorityQueueTest extends J assertTrue(q.removeAll(p)); 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 +440,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()); } /**