• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/publicdomain/zero/1.0/
5  * Other contributors include Andrew Wright, Jeffrey Hayes,
6  * Pat Fisher, Mike Judd.
7  */
8 
9 package jsr166;
10 
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.Comparator;
14 import java.util.Iterator;
15 import java.util.NoSuchElementException;
16 import java.util.PriorityQueue;
17 import java.util.Queue;
18 
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21 
22 public class PriorityQueueTest extends JSR166TestCase {
23     // android-note: Removed because the CTS runner does a bad job of
24     // retrying tests that have suite() declarations.
25     //
26     // public static void main(String[] args) {
27     //     main(suite(), args);
28     // }
29     // public static Test suite() {
30     //     return new TestSuite(...);
31     // }
32 
33     static class MyReverseComparator implements Comparator {
compare(Object x, Object y)34         public int compare(Object x, Object y) {
35             return ((Comparable)y).compareTo(x);
36         }
37     }
38 
39     /**
40      * Returns a new queue of given size containing consecutive
41      * Integers 0 ... n.
42      */
populatedQueue(int n)43     private PriorityQueue<Integer> populatedQueue(int n) {
44         PriorityQueue<Integer> q = new PriorityQueue<Integer>(n);
45         assertTrue(q.isEmpty());
46         for (int i = n-1; i >= 0; i -= 2)
47             assertTrue(q.offer(new Integer(i)));
48         for (int i = (n & 1); i < n; i += 2)
49             assertTrue(q.offer(new Integer(i)));
50         assertFalse(q.isEmpty());
51         assertEquals(n, q.size());
52         return q;
53     }
54 
55     /**
56      * A new queue has unbounded capacity
57      */
testConstructor1()58     public void testConstructor1() {
59         assertEquals(0, new PriorityQueue(SIZE).size());
60     }
61 
62     /**
63      * Constructor throws IAE if capacity argument nonpositive
64      */
testConstructor2()65     public void testConstructor2() {
66         try {
67             new PriorityQueue(0);
68             shouldThrow();
69         } catch (IllegalArgumentException success) {}
70     }
71 
72     /**
73      * Initializing from null Collection throws NPE
74      */
testConstructor3()75     public void testConstructor3() {
76         try {
77             new PriorityQueue((Collection)null);
78             shouldThrow();
79         } catch (NullPointerException success) {}
80     }
81 
82     /**
83      * Initializing from Collection of null elements throws NPE
84      */
testConstructor4()85     public void testConstructor4() {
86         try {
87             Integer[] ints = new Integer[SIZE];
88             new PriorityQueue(Arrays.asList(ints));
89             shouldThrow();
90         } catch (NullPointerException success) {}
91     }
92 
93     /**
94      * Initializing from Collection with some null elements throws NPE
95      */
testConstructor5()96     public void testConstructor5() {
97         try {
98             Integer[] ints = new Integer[SIZE];
99             for (int i = 0; i < SIZE-1; ++i)
100                 ints[i] = new Integer(i);
101             new PriorityQueue(Arrays.asList(ints));
102             shouldThrow();
103         } catch (NullPointerException success) {}
104     }
105 
106     /**
107      * Queue contains all elements of collection used to initialize
108      */
testConstructor6()109     public void testConstructor6() {
110         Integer[] ints = new Integer[SIZE];
111         for (int i = 0; i < SIZE; ++i)
112             ints[i] = new Integer(i);
113         PriorityQueue q = new PriorityQueue(Arrays.asList(ints));
114         for (int i = 0; i < SIZE; ++i)
115             assertEquals(ints[i], q.poll());
116     }
117 
118     /**
119      * The comparator used in constructor is used
120      */
testConstructor7()121     public void testConstructor7() {
122         MyReverseComparator cmp = new MyReverseComparator();
123         PriorityQueue q = new PriorityQueue(SIZE, cmp);
124         assertEquals(cmp, q.comparator());
125         Integer[] ints = new Integer[SIZE];
126         for (int i = 0; i < SIZE; ++i)
127             ints[i] = new Integer(i);
128         q.addAll(Arrays.asList(ints));
129         for (int i = SIZE-1; i >= 0; --i)
130             assertEquals(ints[i], q.poll());
131     }
132 
133     /**
134      * isEmpty is true before add, false after
135      */
testEmpty()136     public void testEmpty() {
137         PriorityQueue q = new PriorityQueue(2);
138         assertTrue(q.isEmpty());
139         q.add(new Integer(1));
140         assertFalse(q.isEmpty());
141         q.add(new Integer(2));
142         q.remove();
143         q.remove();
144         assertTrue(q.isEmpty());
145     }
146 
147     /**
148      * size changes when elements added and removed
149      */
testSize()150     public void testSize() {
151         PriorityQueue q = populatedQueue(SIZE);
152         for (int i = 0; i < SIZE; ++i) {
153             assertEquals(SIZE-i, q.size());
154             q.remove();
155         }
156         for (int i = 0; i < SIZE; ++i) {
157             assertEquals(i, q.size());
158             q.add(new Integer(i));
159         }
160     }
161 
162     /**
163      * offer(null) throws NPE
164      */
testOfferNull()165     public void testOfferNull() {
166         try {
167             PriorityQueue q = new PriorityQueue(1);
168             q.offer(null);
169             shouldThrow();
170         } catch (NullPointerException success) {}
171     }
172 
173     /**
174      * add(null) throws NPE
175      */
testAddNull()176     public void testAddNull() {
177         try {
178             PriorityQueue q = new PriorityQueue(1);
179             q.add(null);
180             shouldThrow();
181         } catch (NullPointerException success) {}
182     }
183 
184     /**
185      * Offer of comparable element succeeds
186      */
testOffer()187     public void testOffer() {
188         PriorityQueue q = new PriorityQueue(1);
189         assertTrue(q.offer(zero));
190         assertTrue(q.offer(one));
191     }
192 
193     /**
194      * Offer of non-Comparable throws CCE
195      */
testOfferNonComparable()196     public void testOfferNonComparable() {
197         PriorityQueue q = new PriorityQueue(1);
198         try {
199             q.offer(new Object());
200             q.offer(new Object());
201             shouldThrow();
202         } catch (ClassCastException success) {}
203     }
204 
205     /**
206      * add of comparable succeeds
207      */
testAdd()208     public void testAdd() {
209         PriorityQueue q = new PriorityQueue(SIZE);
210         for (int i = 0; i < SIZE; ++i) {
211             assertEquals(i, q.size());
212             assertTrue(q.add(new Integer(i)));
213         }
214     }
215 
216     /**
217      * addAll(null) throws NPE
218      */
testAddAll1()219     public void testAddAll1() {
220         try {
221             PriorityQueue q = new PriorityQueue(1);
222             q.addAll(null);
223             shouldThrow();
224         } catch (NullPointerException success) {}
225     }
226 
227     /**
228      * addAll of a collection with null elements throws NPE
229      */
testAddAll2()230     public void testAddAll2() {
231         try {
232             PriorityQueue q = new PriorityQueue(SIZE);
233             Integer[] ints = new Integer[SIZE];
234             q.addAll(Arrays.asList(ints));
235             shouldThrow();
236         } catch (NullPointerException success) {}
237     }
238 
239     /**
240      * addAll of a collection with any null elements throws NPE after
241      * possibly adding some elements
242      */
testAddAll3()243     public void testAddAll3() {
244         try {
245             PriorityQueue q = new PriorityQueue(SIZE);
246             Integer[] ints = new Integer[SIZE];
247             for (int i = 0; i < SIZE-1; ++i)
248                 ints[i] = new Integer(i);
249             q.addAll(Arrays.asList(ints));
250             shouldThrow();
251         } catch (NullPointerException success) {}
252     }
253 
254     /**
255      * Queue contains all elements of successful addAll
256      */
testAddAll5()257     public void testAddAll5() {
258         Integer[] empty = new Integer[0];
259         Integer[] ints = new Integer[SIZE];
260         for (int i = 0; i < SIZE; ++i)
261             ints[i] = new Integer(SIZE-1-i);
262         PriorityQueue q = new PriorityQueue(SIZE);
263         assertFalse(q.addAll(Arrays.asList(empty)));
264         assertTrue(q.addAll(Arrays.asList(ints)));
265         for (int i = 0; i < SIZE; ++i)
266             assertEquals(new Integer(i), q.poll());
267     }
268 
269     /**
270      * poll succeeds unless empty
271      */
testPoll()272     public void testPoll() {
273         PriorityQueue q = populatedQueue(SIZE);
274         for (int i = 0; i < SIZE; ++i) {
275             assertEquals(i, q.poll());
276         }
277         assertNull(q.poll());
278     }
279 
280     /**
281      * peek returns next element, or null if empty
282      */
testPeek()283     public void testPeek() {
284         PriorityQueue q = populatedQueue(SIZE);
285         for (int i = 0; i < SIZE; ++i) {
286             assertEquals(i, q.peek());
287             assertEquals(i, q.poll());
288             assertTrue(q.peek() == null ||
289                        !q.peek().equals(i));
290         }
291         assertNull(q.peek());
292     }
293 
294     /**
295      * element returns next element, or throws NSEE if empty
296      */
testElement()297     public void testElement() {
298         PriorityQueue q = populatedQueue(SIZE);
299         for (int i = 0; i < SIZE; ++i) {
300             assertEquals(i, q.element());
301             assertEquals(i, q.poll());
302         }
303         try {
304             q.element();
305             shouldThrow();
306         } catch (NoSuchElementException success) {}
307     }
308 
309     /**
310      * remove removes next element, or throws NSEE if empty
311      */
testRemove()312     public void testRemove() {
313         PriorityQueue q = populatedQueue(SIZE);
314         for (int i = 0; i < SIZE; ++i) {
315             assertEquals(i, q.remove());
316         }
317         try {
318             q.remove();
319             shouldThrow();
320         } catch (NoSuchElementException success) {}
321     }
322 
323     /**
324      * remove(x) removes x and returns true if present
325      */
testRemoveElement()326     public void testRemoveElement() {
327         PriorityQueue q = populatedQueue(SIZE);
328         for (int i = 1; i < SIZE; i += 2) {
329             assertTrue(q.contains(i));
330             assertTrue(q.remove(i));
331             assertFalse(q.contains(i));
332             assertTrue(q.contains(i-1));
333         }
334         for (int i = 0; i < SIZE; i += 2) {
335             assertTrue(q.contains(i));
336             assertTrue(q.remove(i));
337             assertFalse(q.contains(i));
338             assertFalse(q.remove(i+1));
339             assertFalse(q.contains(i+1));
340         }
341         assertTrue(q.isEmpty());
342     }
343 
344     /**
345      * contains(x) reports true when elements added but not yet removed
346      */
testContains()347     public void testContains() {
348         PriorityQueue q = populatedQueue(SIZE);
349         for (int i = 0; i < SIZE; ++i) {
350             assertTrue(q.contains(new Integer(i)));
351             q.poll();
352             assertFalse(q.contains(new Integer(i)));
353         }
354     }
355 
356     /**
357      * clear removes all elements
358      */
testClear()359     public void testClear() {
360         PriorityQueue q = populatedQueue(SIZE);
361         q.clear();
362         assertTrue(q.isEmpty());
363         assertEquals(0, q.size());
364         q.add(new Integer(1));
365         assertFalse(q.isEmpty());
366         q.clear();
367         assertTrue(q.isEmpty());
368     }
369 
370     /**
371      * containsAll(c) is true when c contains a subset of elements
372      */
testContainsAll()373     public void testContainsAll() {
374         PriorityQueue q = populatedQueue(SIZE);
375         PriorityQueue p = new PriorityQueue(SIZE);
376         for (int i = 0; i < SIZE; ++i) {
377             assertTrue(q.containsAll(p));
378             assertFalse(p.containsAll(q));
379             p.add(new Integer(i));
380         }
381         assertTrue(p.containsAll(q));
382     }
383 
384     /**
385      * retainAll(c) retains only those elements of c and reports true if changed
386      */
testRetainAll()387     public void testRetainAll() {
388         PriorityQueue q = populatedQueue(SIZE);
389         PriorityQueue p = populatedQueue(SIZE);
390         for (int i = 0; i < SIZE; ++i) {
391             boolean changed = q.retainAll(p);
392             if (i == 0)
393                 assertFalse(changed);
394             else
395                 assertTrue(changed);
396 
397             assertTrue(q.containsAll(p));
398             assertEquals(SIZE-i, q.size());
399             p.remove();
400         }
401     }
402 
403     /**
404      * removeAll(c) removes only those elements of c and reports true if changed
405      */
testRemoveAll()406     public void testRemoveAll() {
407         for (int i = 1; i < SIZE; ++i) {
408             PriorityQueue q = populatedQueue(SIZE);
409             PriorityQueue p = populatedQueue(i);
410             assertTrue(q.removeAll(p));
411             assertEquals(SIZE-i, q.size());
412             for (int j = 0; j < i; ++j) {
413                 Integer x = (Integer)(p.remove());
414                 assertFalse(q.contains(x));
415             }
416         }
417     }
418 
419     /**
420      * toArray contains all elements
421      */
testToArray()422     public void testToArray() {
423         PriorityQueue q = populatedQueue(SIZE);
424         Object[] o = q.toArray();
425         Arrays.sort(o);
426         for (int i = 0; i < o.length; i++)
427             assertSame(o[i], q.poll());
428     }
429 
430     /**
431      * toArray(a) contains all elements
432      */
testToArray2()433     public void testToArray2() {
434         PriorityQueue<Integer> q = populatedQueue(SIZE);
435         Integer[] ints = new Integer[SIZE];
436         Integer[] array = q.toArray(ints);
437         assertSame(ints, array);
438         Arrays.sort(ints);
439         for (int i = 0; i < ints.length; i++)
440             assertSame(ints[i], q.poll());
441     }
442 
443     /**
444      * iterator iterates through all elements
445      */
testIterator()446     public void testIterator() {
447         PriorityQueue q = populatedQueue(SIZE);
448         Iterator it = q.iterator();
449         int i;
450         for (i = 0; it.hasNext(); i++)
451             assertTrue(q.contains(it.next()));
452         assertEquals(i, SIZE);
453         assertIteratorExhausted(it);
454     }
455 
456     /**
457      * iterator of empty collection has no elements
458      */
testEmptyIterator()459     public void testEmptyIterator() {
460         assertIteratorExhausted(new PriorityQueue().iterator());
461     }
462 
463     /**
464      * iterator.remove removes current element
465      */
testIteratorRemove()466     public void testIteratorRemove() {
467         final PriorityQueue q = new PriorityQueue(3);
468         q.add(new Integer(2));
469         q.add(new Integer(1));
470         q.add(new Integer(3));
471 
472         Iterator it = q.iterator();
473         it.next();
474         it.remove();
475 
476         it = q.iterator();
477         assertEquals(it.next(), new Integer(2));
478         assertEquals(it.next(), new Integer(3));
479         assertFalse(it.hasNext());
480     }
481 
482     /**
483      * toString contains toStrings of elements
484      */
testToString()485     public void testToString() {
486         PriorityQueue q = populatedQueue(SIZE);
487         String s = q.toString();
488         for (int i = 0; i < SIZE; ++i) {
489             assertTrue(s.contains(String.valueOf(i)));
490         }
491     }
492 
493     /**
494      * A deserialized serialized queue has same elements
495      */
testSerialization()496     public void testSerialization() throws Exception {
497         Queue x = populatedQueue(SIZE);
498         Queue y = serialClone(x);
499 
500         assertNotSame(x, y);
501         assertEquals(x.size(), y.size());
502         while (!x.isEmpty()) {
503             assertFalse(y.isEmpty());
504             assertEquals(x.remove(), y.remove());
505         }
506         assertTrue(y.isEmpty());
507     }
508 }
509