ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityQueueTest.java
Revision: 1.49
Committed: Wed Jan 27 01:57:24 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.48: +14 -14 lines
Log Message:
use diamond <> pervasively

File Contents

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