--- jsr166/src/test/tck/PriorityQueueTest.java 2014/12/31 19:05:43 1.27 +++ jsr166/src/test/tck/PriorityQueueTest.java 2018/05/06 22:09:42 1.45 @@ -15,17 +15,32 @@ 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); + class Implementation implements CollectionImplementation { + public Class klazz() { return PriorityQueue.class; } + public Collection emptyCollection() { return new PriorityQueue(); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return false; } + public boolean permitsNulls() { return false; } + } + class ComparatorImplementation implements CollectionImplementation { + public Class klazz() { return PriorityQueue.class; } + public Collection emptyCollection() { return new PriorityQueue(new MyReverseComparator()); } + public Object makeElement(int i) { return i; } + public boolean isConcurrent() { return false; } + public boolean permitsNulls() { return false; } + } + return newTestSuite(PriorityQueueTest.class, + CollectionTest.testSuite(new Implementation()), + CollectionTest.testSuite(new ComparatorImplementation())); } - static class MyReverseComparator implements Comparator { + static class MyReverseComparator implements Comparator, java.io.Serializable { public int compare(Object x, Object y) { return ((Comparable)y).compareTo(x); } @@ -33,17 +48,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); + private static 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; } @@ -55,11 +71,11 @@ public class PriorityQueueTest extends J } /** - * Constructor throws IAE if capacity argument nonpositive + * Constructor throws IllegalArgumentException if capacity argument nonpositive */ public void testConstructor2() { try { - PriorityQueue q = new PriorityQueue(0); + new PriorityQueue(0); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -69,7 +85,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 +95,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 +104,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 +136,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 +160,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 +173,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 +184,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 +204,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 +230,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 +241,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 +253,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 +270,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))); @@ -321,18 +337,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()); } @@ -391,7 +407,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,10 +420,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)); } } } @@ -441,13 +457,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()); } /** @@ -481,7 +503,7 @@ public class PriorityQueueTest extends J } /** - * A deserialized serialized queue has same elements + * A deserialized/reserialized queue has same elements */ public void testSerialization() throws Exception { Queue x = populatedQueue(SIZE);