ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/PriorityQueueTest.java (file contents):
Revision 1.13 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.21 by jsr166, Tue Mar 15 19:47:07 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
# Line 13 | Line 13 | import java.io.*;
13  
14   public class PriorityQueueTest extends JSR166TestCase {
15      public static void main(String[] args) {
16 <        junit.textui.TestRunner.run (suite());
16 >        junit.textui.TestRunner.run(suite());
17      }
18      public static Test suite() {
19          return new TestSuite(PriorityQueueTest.class);
# Line 21 | Line 21 | public class PriorityQueueTest extends J
21  
22      static class MyReverseComparator implements Comparator {
23          public int compare(Object x, Object y) {
24 <            int i = ((Integer)x).intValue();
25 <            int j = ((Integer)y).intValue();
26 <            if (i < j) return 1;
27 <            if (i > j) return -1;
28 <            return 0;
24 >            return ((Comparable)y).compareTo(x);
25          }
26      }
27  
# Line 33 | Line 29 | public class PriorityQueueTest extends J
29       * Create a queue of given size containing consecutive
30       * Integers 0 ... n.
31       */
32 <    private PriorityQueue populatedQueue(int n) {
33 <        PriorityQueue q = new PriorityQueue(n);
32 >    private PriorityQueue<Integer> populatedQueue(int n) {
33 >        PriorityQueue<Integer> q = new PriorityQueue<Integer>(n);
34          assertTrue(q.isEmpty());
35          for (int i = n-1; i >= 0; i-=2)
36              assertTrue(q.offer(new Integer(i)));
# Line 100 | Line 96 | public class PriorityQueueTest extends J
96       * Queue contains all elements of collection used to initialize
97       */
98      public void testConstructor6() {
99 <        try {
100 <            Integer[] ints = new Integer[SIZE];
101 <            for (int i = 0; i < SIZE; ++i)
102 <                ints[i] = new Integer(i);
103 <            PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
104 <            for (int i = 0; i < SIZE; ++i)
109 <                assertEquals(ints[i], q.poll());
110 <        }
111 <        finally {}
99 >        Integer[] ints = new Integer[SIZE];
100 >        for (int i = 0; i < SIZE; ++i)
101 >            ints[i] = new Integer(i);
102 >        PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
103 >        for (int i = 0; i < SIZE; ++i)
104 >            assertEquals(ints[i], q.poll());
105      }
106  
107      /**
108       * The comparator used in constructor is used
109       */
110      public void testConstructor7() {
111 <        try {
112 <            MyReverseComparator cmp = new MyReverseComparator();
113 <            PriorityQueue q = new PriorityQueue(SIZE, cmp);
114 <            assertEquals(cmp, q.comparator());
115 <            Integer[] ints = new Integer[SIZE];
116 <            for (int i = 0; i < SIZE; ++i)
117 <                ints[i] = new Integer(i);
118 <            q.addAll(Arrays.asList(ints));
119 <            for (int i = SIZE-1; i >= 0; --i)
127 <                assertEquals(ints[i], q.poll());
128 <        }
129 <        finally {}
111 >        MyReverseComparator cmp = new MyReverseComparator();
112 >        PriorityQueue q = new PriorityQueue(SIZE, cmp);
113 >        assertEquals(cmp, q.comparator());
114 >        Integer[] ints = new Integer[SIZE];
115 >        for (int i = 0; i < SIZE; ++i)
116 >            ints[i] = new Integer(i);
117 >        q.addAll(Arrays.asList(ints));
118 >        for (int i = SIZE-1; i >= 0; --i)
119 >            assertEquals(ints[i], q.poll());
120      }
121  
122      /**
# Line 223 | Line 213 | public class PriorityQueueTest extends J
213              shouldThrow();
214          } catch (NullPointerException success) {}
215      }
216 +
217      /**
218       * addAll of a collection with null elements throws NPE
219       */
# Line 234 | Line 225 | public class PriorityQueueTest extends J
225              shouldThrow();
226          } catch (NullPointerException success) {}
227      }
228 +
229      /**
230       * addAll of a collection with any null elements throws NPE after
231       * possibly adding some elements
# Line 253 | Line 245 | public class PriorityQueueTest extends J
245       * Queue contains all elements of successful addAll
246       */
247      public void testAddAll5() {
248 <        try {
249 <            Integer[] empty = new Integer[0];
250 <            Integer[] ints = new Integer[SIZE];
251 <            for (int i = 0; i < SIZE; ++i)
252 <                ints[i] = new Integer(SIZE-1-i);
253 <            PriorityQueue q = new PriorityQueue(SIZE);
254 <            assertFalse(q.addAll(Arrays.asList(empty)));
255 <            assertTrue(q.addAll(Arrays.asList(ints)));
256 <            for (int i = 0; i < SIZE; ++i)
265 <                assertEquals(new Integer(i), q.poll());
266 <        }
267 <        finally {}
248 >        Integer[] empty = new Integer[0];
249 >        Integer[] ints = new Integer[SIZE];
250 >        for (int i = 0; i < SIZE; ++i)
251 >            ints[i] = new Integer(SIZE-1-i);
252 >        PriorityQueue q = new PriorityQueue(SIZE);
253 >        assertFalse(q.addAll(Arrays.asList(empty)));
254 >        assertTrue(q.addAll(Arrays.asList(ints)));
255 >        for (int i = 0; i < SIZE; ++i)
256 >            assertEquals(new Integer(i), q.poll());
257      }
258  
259      /**
# Line 273 | Line 262 | public class PriorityQueueTest extends J
262      public void testPoll() {
263          PriorityQueue q = populatedQueue(SIZE);
264          for (int i = 0; i < SIZE; ++i) {
265 <            assertEquals(i, ((Integer)q.poll()).intValue());
265 >            assertEquals(i, q.poll());
266          }
267          assertNull(q.poll());
268      }
# Line 284 | Line 273 | public class PriorityQueueTest extends J
273      public void testPeek() {
274          PriorityQueue q = populatedQueue(SIZE);
275          for (int i = 0; i < SIZE; ++i) {
276 <            assertEquals(i, ((Integer)q.peek()).intValue());
277 <            q.poll();
276 >            assertEquals(i, q.peek());
277 >            assertEquals(i, q.poll());
278              assertTrue(q.peek() == null ||
279 <                       i != ((Integer)q.peek()).intValue());
279 >                       !q.peek().equals(i));
280          }
281          assertNull(q.peek());
282      }
# Line 298 | Line 287 | public class PriorityQueueTest extends J
287      public void testElement() {
288          PriorityQueue q = populatedQueue(SIZE);
289          for (int i = 0; i < SIZE; ++i) {
290 <            assertEquals(i, ((Integer)q.element()).intValue());
291 <            q.poll();
290 >            assertEquals(i, q.element());
291 >            assertEquals(i, q.poll());
292          }
293          try {
294              q.element();
# Line 313 | Line 302 | public class PriorityQueueTest extends J
302      public void testRemove() {
303          PriorityQueue q = populatedQueue(SIZE);
304          for (int i = 0; i < SIZE; ++i) {
305 <            assertEquals(i, ((Integer)q.remove()).intValue());
305 >            assertEquals(i, q.remove());
306          }
307          try {
308              q.remove();
# Line 327 | Line 316 | public class PriorityQueueTest extends J
316      public void testRemoveElement() {
317          PriorityQueue q = populatedQueue(SIZE);
318          for (int i = 1; i < SIZE; i+=2) {
319 <            assertTrue(q.remove(new Integer(i)));
319 >            assertTrue(q.contains(i));
320 >            assertTrue(q.remove(i));
321 >            assertFalse(q.contains(i));
322 >            assertTrue(q.contains(i-1));
323          }
324          for (int i = 0; i < SIZE; i+=2) {
325 <            assertTrue(q.remove(new Integer(i)));
326 <            assertFalse(q.remove(new Integer(i+1)));
325 >            assertTrue(q.contains(i));
326 >            assertTrue(q.remove(i));
327 >            assertFalse(q.contains(i));
328 >            assertFalse(q.remove(i+1));
329 >            assertFalse(q.contains(i+1));
330          }
331          assertTrue(q.isEmpty());
332      }
# Line 419 | Line 414 | public class PriorityQueueTest extends J
414          Object[] o = q.toArray();
415          Arrays.sort(o);
416          for (int i = 0; i < o.length; i++)
417 <            assertEquals(o[i], q.poll());
417 >            assertSame(o[i], q.poll());
418      }
419  
420      /**
421       * toArray(a) contains all elements
422       */
423      public void testToArray2() {
424 <        PriorityQueue q = populatedQueue(SIZE);
424 >        PriorityQueue<Integer> q = populatedQueue(SIZE);
425          Integer[] ints = new Integer[SIZE];
426 <        ints = (Integer[])q.toArray(ints);
426 >        Integer[] array = q.toArray(ints);
427 >        assertSame(ints, array);
428          Arrays.sort(ints);
429          for (int i = 0; i < ints.length; i++)
430 <            assertEquals(ints[i], q.poll());
430 >            assertSame(ints[i], q.poll());
431      }
432  
433      /**
# Line 451 | Line 447 | public class PriorityQueueTest extends J
447      /**
448       * iterator.remove removes current element
449       */
450 <    public void testIteratorRemove () {
450 >    public void testIteratorRemove() {
451          final PriorityQueue q = new PriorityQueue(3);
452          q.add(new Integer(2));
453          q.add(new Integer(1));

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines