--- jsr166/src/test/tck/PriorityQueueTest.java 2009/11/22 18:57:17 1.15 +++ jsr166/src/test/tck/PriorityQueueTest.java 2014/12/31 19:05:43 1.27 @@ -1,19 +1,25 @@ /* * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at - * http://creativecommons.org/licenses/publicdomain + * http://creativecommons.org/publicdomain/zero/1.0/ * Other contributors include Andrew Wright, Jeffrey Hayes, * Pat Fisher, Mike Judd. */ -import junit.framework.*; -import java.util.*; -import java.util.concurrent.*; -import java.io.*; +import java.util.Arrays; +import java.util.Collection; +import java.util.Comparator; +import java.util.Iterator; +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()); + junit.textui.TestRunner.run(suite()); } public static Test suite() { return new TestSuite(PriorityQueueTest.class); @@ -26,11 +32,11 @@ public class PriorityQueueTest extends J } /** - * Create a queue of given size containing consecutive + * Returns a new queue of given size containing consecutive * Integers 0 ... n. */ - private PriorityQueue populatedQueue(int n) { - PriorityQueue q = new PriorityQueue(n); + private PriorityQueue populatedQueue(int n) { + PriorityQueue q = new PriorityQueue(n); assertTrue(q.isEmpty()); for (int i = n-1; i >= 0; i-=2) assertTrue(q.offer(new Integer(i))); @@ -213,6 +219,7 @@ public class PriorityQueueTest extends J shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with null elements throws NPE */ @@ -224,6 +231,7 @@ public class PriorityQueueTest extends J shouldThrow(); } catch (NullPointerException success) {} } + /** * addAll of a collection with any null elements throws NPE after * possibly adding some elements @@ -314,11 +322,17 @@ public class PriorityQueueTest extends J public void testRemoveElement() { PriorityQueue q = populatedQueue(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()); } @@ -406,19 +420,20 @@ public class PriorityQueueTest extends J Object[] o = q.toArray(); Arrays.sort(o); for (int i = 0; i < o.length; i++) - assertEquals(o[i], q.poll()); + assertSame(o[i], q.poll()); } /** * toArray(a) contains all elements */ public void testToArray2() { - PriorityQueue q = populatedQueue(SIZE); + PriorityQueue q = populatedQueue(SIZE); Integer[] ints = new Integer[SIZE]; - ints = (Integer[])q.toArray(ints); + Integer[] array = q.toArray(ints); + assertSame(ints, array); Arrays.sort(ints); for (int i = 0; i < ints.length; i++) - assertEquals(ints[i], q.poll()); + assertSame(ints[i], q.poll()); } /** @@ -438,7 +453,7 @@ public class PriorityQueueTest extends J /** * iterator.remove removes current element */ - public void testIteratorRemove () { + public void testIteratorRemove() { final PriorityQueue q = new PriorityQueue(3); q.add(new Integer(2)); q.add(new Integer(1)); @@ -454,7 +469,6 @@ public class PriorityQueueTest extends J assertFalse(it.hasNext()); } - /** * toString contains toStrings of elements */ @@ -462,7 +476,7 @@ public class PriorityQueueTest extends J PriorityQueue q = populatedQueue(SIZE); String s = q.toString(); for (int i = 0; i < SIZE; ++i) { - assertTrue(s.indexOf(String.valueOf(i)) >= 0); + assertTrue(s.contains(String.valueOf(i))); } } @@ -470,17 +484,15 @@ public class PriorityQueueTest extends J * A deserialized serialized queue has same elements */ public void testSerialization() throws Exception { - PriorityQueue q = populatedQueue(SIZE); - ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); - ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); - out.writeObject(q); - out.close(); - - ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); - ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); - PriorityQueue r = (PriorityQueue)in.readObject(); - assertEquals(q.size(), r.size()); - while (!q.isEmpty()) - assertEquals(q.remove(), r.remove()); + Queue x = populatedQueue(SIZE); + Queue y = serialClone(x); + + assertNotSame(x, y); + assertEquals(x.size(), y.size()); + while (!x.isEmpty()) { + assertFalse(y.isEmpty()); + assertEquals(x.remove(), y.remove()); + } + assertTrue(y.isEmpty()); } }