ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.87
Committed: Wed Jan 27 02:55:18 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.86: +2 -1 lines
Log Message:
Suppress all new errorprone "errors"

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.41 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.12 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.54 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10    
11     import java.util.ArrayList;
12 jsr166 1.45 import java.util.Arrays;
13     import java.util.Collection;
14     import java.util.Comparator;
15     import java.util.Iterator;
16     import java.util.NoSuchElementException;
17 jsr166 1.46 import java.util.Queue;
18 jsr166 1.45 import java.util.concurrent.BlockingQueue;
19     import java.util.concurrent.CountDownLatch;
20     import java.util.concurrent.Executors;
21     import java.util.concurrent.ExecutorService;
22 jsr166 1.54 import java.util.concurrent.PriorityBlockingQueue;
23 jsr166 1.80 import java.util.concurrent.ThreadLocalRandom;
24 jsr166 1.54
25     import junit.framework.Test;
26 dl 1.1
27 dl 1.3 public class PriorityBlockingQueueTest extends JSR166TestCase {
28 jsr166 1.29
29     public static class Generic extends BlockingQueueTest {
30     protected BlockingQueue emptyCollection() {
31     return new PriorityBlockingQueue();
32     }
33     }
34    
35     public static class InitialCapacity extends BlockingQueueTest {
36     protected BlockingQueue emptyCollection() {
37 jsr166 1.80 ThreadLocalRandom rnd = ThreadLocalRandom.current();
38     int initialCapacity = rnd.nextInt(1, SIZE);
39     return new PriorityBlockingQueue(initialCapacity);
40 jsr166 1.29 }
41     }
42    
43 dl 1.1 public static void main(String[] args) {
44 jsr166 1.61 main(suite(), args);
45 dl 1.1 }
46 jsr166 1.29
47 dl 1.1 public static Test suite() {
48 jsr166 1.70 class Implementation implements CollectionImplementation {
49     public Class<?> klazz() { return PriorityBlockingQueue.class; }
50 jsr166 1.80 public Collection emptyCollection() {
51     return new PriorityBlockingQueue();
52     }
53 dl 1.85 public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
54 jsr166 1.80 public boolean isConcurrent() { return true; }
55     public boolean permitsNulls() { return false; }
56     }
57     class ComparatorImplementation implements CollectionImplementation {
58     public Class<?> klazz() { return PriorityBlockingQueue.class; }
59 dl 1.85 @SuppressWarnings("unchecked")
60 jsr166 1.80 public Collection emptyCollection() {
61     ThreadLocalRandom rnd = ThreadLocalRandom.current();
62     int initialCapacity = rnd.nextInt(1, 10);
63     return new PriorityBlockingQueue(
64     initialCapacity, new MyReverseComparator());
65     }
66 dl 1.85 public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
67 jsr166 1.70 public boolean isConcurrent() { return true; }
68     public boolean permitsNulls() { return false; }
69     }
70 jsr166 1.80 return newTestSuite(
71     PriorityBlockingQueueTest.class,
72     new Generic().testSuite(),
73     new InitialCapacity().testSuite(),
74     CollectionTest.testSuite(new Implementation()),
75     CollectionTest.testSuite(new ComparatorImplementation()));
76 dl 1.1 }
77    
78 dl 1.3 /** Sample Comparator */
79 jsr166 1.80 static class MyReverseComparator implements Comparator, java.io.Serializable {
80 dl 1.85 @SuppressWarnings("unchecked")
81 dl 1.1 public int compare(Object x, Object y) {
82 jsr166 1.24 return ((Comparable)y).compareTo(x);
83 dl 1.1 }
84     }
85    
86     /**
87 jsr166 1.51 * Returns a new queue of given size containing consecutive
88 dl 1.85 * Items 0 ... n - 1.
89 dl 1.1 */
90 dl 1.85 private static PriorityBlockingQueue<Item> populatedQueue(int n) {
91 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(n);
92 dl 1.1 assertTrue(q.isEmpty());
93 jsr166 1.64 for (int i = n - 1; i >= 0; i -= 2)
94 dl 1.85 mustOffer(q, i);
95 jsr166 1.56 for (int i = (n & 1); i < n; i += 2)
96 dl 1.85 mustOffer(q, i);
97 dl 1.1 assertFalse(q.isEmpty());
98 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
99     mustEqual(n, q.size());
100     mustEqual(0, q.peek());
101 dl 1.1 return q;
102     }
103 jsr166 1.12
104 dl 1.5 /**
105 dl 1.6 * A new queue has unbounded capacity
106 dl 1.5 */
107     public void testConstructor1() {
108 dl 1.85 mustEqual(Integer.MAX_VALUE,
109     new PriorityBlockingQueue<Item>(SIZE).remainingCapacity());
110 dl 1.1 }
111    
112 dl 1.5 /**
113 jsr166 1.78 * Constructor throws IllegalArgumentException if capacity argument nonpositive
114 dl 1.5 */
115     public void testConstructor2() {
116 dl 1.1 try {
117 dl 1.85 new PriorityBlockingQueue<Item>(0);
118 dl 1.5 shouldThrow();
119 jsr166 1.15 } catch (IllegalArgumentException success) {}
120 dl 1.1 }
121    
122 dl 1.5 /**
123 dl 1.6 * Initializing from null Collection throws NPE
124 dl 1.5 */
125     public void testConstructor3() {
126 dl 1.1 try {
127 dl 1.85 new PriorityBlockingQueue<Item>(null);
128 dl 1.5 shouldThrow();
129 jsr166 1.15 } catch (NullPointerException success) {}
130 dl 1.1 }
131    
132 dl 1.5 /**
133 dl 1.6 * Initializing from Collection of null elements throws NPE
134 dl 1.5 */
135     public void testConstructor4() {
136 dl 1.85 Collection<Item> elements = Arrays.asList(new Item[SIZE]);
137 dl 1.1 try {
138 dl 1.85 new PriorityBlockingQueue<Item>(elements);
139 dl 1.5 shouldThrow();
140 jsr166 1.15 } catch (NullPointerException success) {}
141 dl 1.1 }
142    
143 dl 1.5 /**
144 dl 1.6 * Initializing from Collection with some null elements throws NPE
145 dl 1.5 */
146     public void testConstructor5() {
147 dl 1.85 Item[] items = new Item[2]; items[0] = zero;
148     Collection<Item> elements = Arrays.asList(items);
149 dl 1.1 try {
150 dl 1.85 new PriorityBlockingQueue<Item>(elements);
151 dl 1.5 shouldThrow();
152 jsr166 1.15 } catch (NullPointerException success) {}
153 dl 1.1 }
154    
155 dl 1.5 /**
156 dl 1.6 * Queue contains all elements of collection used to initialize
157 dl 1.5 */
158     public void testConstructor6() {
159 dl 1.85 Item[] items = defaultItems;
160 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(Arrays.asList(items));
161 jsr166 1.15 for (int i = 0; i < SIZE; ++i)
162 dl 1.85 mustEqual(items[i], q.poll());
163 dl 1.1 }
164    
165 dl 1.5 /**
166 dl 1.6 * The comparator used in constructor is used
167 dl 1.5 */
168     public void testConstructor7() {
169 jsr166 1.15 MyReverseComparator cmp = new MyReverseComparator();
170 dl 1.85 @SuppressWarnings("unchecked")
171 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(SIZE, cmp);
172 dl 1.85 mustEqual(cmp, q.comparator());
173     Item[] items = defaultItems;
174     q.addAll(Arrays.asList(items));
175 jsr166 1.63 for (int i = SIZE - 1; i >= 0; --i)
176 dl 1.85 mustEqual(items[i], q.poll());
177 dl 1.1 }
178    
179 dl 1.5 /**
180 dl 1.6 * isEmpty is true before add, false after
181 dl 1.5 */
182 dl 1.1 public void testEmpty() {
183 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(2);
184 dl 1.1 assertTrue(q.isEmpty());
185 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
186 dl 1.6 q.add(one);
187 dl 1.1 assertFalse(q.isEmpty());
188 dl 1.6 q.add(two);
189 dl 1.1 q.remove();
190     q.remove();
191     assertTrue(q.isEmpty());
192     }
193    
194 dl 1.5 /**
195 jsr166 1.59 * remainingCapacity() always returns Integer.MAX_VALUE
196 dl 1.5 */
197     public void testRemainingCapacity() {
198 dl 1.85 BlockingQueue<Item> q = populatedQueue(SIZE);
199 dl 1.3 for (int i = 0; i < SIZE; ++i) {
200 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
201     mustEqual(SIZE - i, q.size());
202     mustEqual(i, q.remove());
203 dl 1.1 }
204 dl 1.3 for (int i = 0; i < SIZE; ++i) {
205 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
206     mustEqual(i, q.size());
207     mustAdd(q, i);
208 dl 1.1 }
209     }
210    
211 dl 1.5 /**
212 dl 1.6 * Offer of comparable element succeeds
213 dl 1.5 */
214 dl 1.1 public void testOffer() {
215 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(1);
216 dl 1.6 assertTrue(q.offer(zero));
217     assertTrue(q.offer(one));
218 dl 1.1 }
219    
220 dl 1.5 /**
221 dl 1.6 * Offer of non-Comparable throws CCE
222 dl 1.5 */
223 dl 1.1 public void testOfferNonComparable() {
224 jsr166 1.86 PriorityBlockingQueue<Object> q = new PriorityBlockingQueue<>(1);
225 dl 1.1 try {
226     q.offer(new Object());
227 dl 1.5 shouldThrow();
228 jsr166 1.67 } catch (ClassCastException success) {
229     assertTrue(q.isEmpty());
230 dl 1.85 mustEqual(0, q.size());
231 jsr166 1.67 assertNull(q.poll());
232     }
233 dl 1.1 }
234    
235 dl 1.5 /**
236 dl 1.6 * add of comparable succeeds
237 dl 1.5 */
238     public void testAdd() {
239 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(SIZE);
240 dl 1.3 for (int i = 0; i < SIZE; ++i) {
241 dl 1.85 mustEqual(i, q.size());
242     mustAdd(q, i);
243 dl 1.1 }
244     }
245    
246 dl 1.5 /**
247 jsr166 1.78 * addAll(this) throws IllegalArgumentException
248 dl 1.7 */
249     public void testAddAllSelf() {
250 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
251 dl 1.7 try {
252     q.addAll(q);
253     shouldThrow();
254 jsr166 1.15 } catch (IllegalArgumentException success) {}
255 dl 1.7 }
256    
257 dl 1.5 /**
258 dl 1.6 * addAll of a collection with any null elements throws NPE after
259     * possibly adding some elements
260 dl 1.5 */
261     public void testAddAll3() {
262 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(SIZE);
263 dl 1.85 Item[] items = new Item[2];
264     items[0] = zero;
265 dl 1.1 try {
266 dl 1.85 q.addAll(Arrays.asList(items));
267 dl 1.5 shouldThrow();
268 jsr166 1.15 } catch (NullPointerException success) {}
269 dl 1.1 }
270    
271 dl 1.5 /**
272 dl 1.6 * Queue contains all elements of successful addAll
273 dl 1.5 */
274     public void testAddAll5() {
275 dl 1.85 Item[] empty = new Item[0];
276     Item[] items = defaultItems;
277 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(SIZE);
278 jsr166 1.15 assertFalse(q.addAll(Arrays.asList(empty)));
279 dl 1.85 assertTrue(q.addAll(Arrays.asList(items)));
280 jsr166 1.15 for (int i = 0; i < SIZE; ++i)
281 dl 1.85 mustEqual(items[i], q.poll());
282 dl 1.1 }
283    
284 dl 1.5 /**
285 dl 1.6 * all elements successfully put are contained
286 dl 1.5 */
287 jsr166 1.42 public void testPut() {
288 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(SIZE);
289 jsr166 1.42 for (int i = 0; i < SIZE; ++i) {
290 dl 1.85 Item x = itemFor(i);
291 jsr166 1.57 q.put(x);
292 dl 1.85 mustContain(q, x);
293 jsr166 1.42 }
294 dl 1.85 mustEqual(SIZE, q.size());
295 dl 1.1 }
296    
297 dl 1.5 /**
298 dl 1.6 * put doesn't block waiting for take
299 dl 1.5 */
300 jsr166 1.15 public void testPutWithTake() throws InterruptedException {
301 jsr166 1.86 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(2);
302 jsr166 1.15 final int size = 4;
303 jsr166 1.44 Thread t = newStartedThread(new CheckedRunnable() {
304 jsr166 1.15 public void realRun() {
305     for (int i = 0; i < size; i++)
306 dl 1.85 q.put(zero);
307 jsr166 1.15 }});
308    
309 jsr166 1.44 awaitTermination(t);
310 dl 1.85 mustEqual(size, q.size());
311 jsr166 1.15 q.take();
312 dl 1.1 }
313    
314 dl 1.5 /**
315 jsr166 1.83 * Queue is unbounded, so timed offer never times out
316 dl 1.5 */
317 jsr166 1.77 public void testTimedOffer() {
318 jsr166 1.86 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(2);
319 jsr166 1.44 Thread t = newStartedThread(new CheckedRunnable() {
320 jsr166 1.18 public void realRun() {
321 dl 1.85 q.put(one);
322     q.put(two);
323     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
324     assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
325 jsr166 1.18 }});
326 jsr166 1.12
327 jsr166 1.44 awaitTermination(t);
328 dl 1.1 }
329    
330 dl 1.5 /**
331 dl 1.6 * take retrieves elements in priority order
332 dl 1.5 */
333 jsr166 1.15 public void testTake() throws InterruptedException {
334 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
335 jsr166 1.15 for (int i = 0; i < SIZE; ++i) {
336 dl 1.85 mustEqual(i, q.take());
337 jsr166 1.15 }
338 dl 1.1 }
339    
340 dl 1.5 /**
341 dl 1.6 * Take removes existing elements until empty, then blocks interruptibly
342 dl 1.5 */
343 jsr166 1.15 public void testBlockingTake() throws InterruptedException {
344 dl 1.85 final PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
345 jsr166 1.44 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
346     Thread t = newStartedThread(new CheckedRunnable() {
347 jsr166 1.15 public void realRun() throws InterruptedException {
348 dl 1.85 for (int i = 0; i < SIZE; i++) mustEqual(i, q.take());
349 jsr166 1.44
350     Thread.currentThread().interrupt();
351 jsr166 1.23 try {
352     q.take();
353     shouldThrow();
354     } catch (InterruptedException success) {}
355 jsr166 1.44 assertFalse(Thread.interrupted());
356    
357     pleaseInterrupt.countDown();
358     try {
359     q.take();
360     shouldThrow();
361     } catch (InterruptedException success) {}
362     assertFalse(Thread.interrupted());
363 jsr166 1.15 }});
364    
365 jsr166 1.44 await(pleaseInterrupt);
366 jsr166 1.82 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
367 jsr166 1.15 t.interrupt();
368 jsr166 1.44 awaitTermination(t);
369 dl 1.1 }
370    
371 dl 1.5 /**
372 dl 1.6 * poll succeeds unless empty
373 dl 1.5 */
374     public void testPoll() {
375 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
376 dl 1.3 for (int i = 0; i < SIZE; ++i) {
377 dl 1.85 mustEqual(i, q.poll());
378 dl 1.1 }
379 jsr166 1.16 assertNull(q.poll());
380 dl 1.1 }
381    
382 dl 1.5 /**
383 jsr166 1.34 * timed poll with zero timeout succeeds when non-empty, else times out
384 dl 1.5 */
385 jsr166 1.15 public void testTimedPoll0() throws InterruptedException {
386 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
387 jsr166 1.15 for (int i = 0; i < SIZE; ++i) {
388 dl 1.85 mustEqual(i, q.poll(0, MILLISECONDS));
389 jsr166 1.15 }
390 jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
391 dl 1.1 }
392    
393 dl 1.5 /**
394 jsr166 1.34 * timed poll with nonzero timeout succeeds when non-empty, else times out
395 dl 1.5 */
396 jsr166 1.15 public void testTimedPoll() throws InterruptedException {
397 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
398 jsr166 1.15 for (int i = 0; i < SIZE; ++i) {
399 jsr166 1.44 long startTime = System.nanoTime();
400 dl 1.85 mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
401 jsr166 1.44 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
402     }
403     long startTime = System.nanoTime();
404     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
405     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
406     checkEmpty(q);
407 dl 1.1 }
408    
409 dl 1.5 /**
410 dl 1.6 * Interrupted timed poll throws InterruptedException instead of
411     * returning timeout status
412 dl 1.5 */
413 jsr166 1.15 public void testInterruptedTimedPoll() throws InterruptedException {
414 dl 1.85 final BlockingQueue<Item> q = populatedQueue(SIZE);
415 jsr166 1.75 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
416 jsr166 1.40 Thread t = newStartedThread(new CheckedRunnable() {
417 jsr166 1.15 public void realRun() throws InterruptedException {
418 jsr166 1.76 for (int i = 0; i < SIZE; i++)
419 dl 1.85 mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
420 jsr166 1.76
421     Thread.currentThread().interrupt();
422     try {
423 jsr166 1.82 q.poll(randomTimeout(), randomTimeUnit());
424 jsr166 1.76 shouldThrow();
425     } catch (InterruptedException success) {}
426     assertFalse(Thread.interrupted());
427 jsr166 1.75
428     pleaseInterrupt.countDown();
429 jsr166 1.15 try {
430 jsr166 1.84 q.poll(LONGER_DELAY_MS, MILLISECONDS);
431 jsr166 1.18 shouldThrow();
432 jsr166 1.75 } catch (InterruptedException success) {}
433     assertFalse(Thread.interrupted());
434 jsr166 1.15 }});
435    
436 jsr166 1.75 await(pleaseInterrupt);
437 jsr166 1.82 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
438 jsr166 1.15 t.interrupt();
439 jsr166 1.66 awaitTermination(t);
440 dl 1.1 }
441    
442 dl 1.5 /**
443 dl 1.6 * peek returns next element, or null if empty
444 dl 1.5 */
445     public void testPeek() {
446 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
447 dl 1.3 for (int i = 0; i < SIZE; ++i) {
448 dl 1.85 mustEqual(i, q.peek());
449     mustEqual(i, q.poll());
450 dl 1.1 assertTrue(q.peek() == null ||
451 jsr166 1.24 !q.peek().equals(i));
452 dl 1.1 }
453 jsr166 1.16 assertNull(q.peek());
454 dl 1.1 }
455    
456 dl 1.5 /**
457 dl 1.6 * element returns next element, or throws NSEE if empty
458 dl 1.5 */
459     public void testElement() {
460 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
461 dl 1.3 for (int i = 0; i < SIZE; ++i) {
462 dl 1.85 mustEqual(i, q.element());
463     mustEqual(i, q.poll());
464 dl 1.1 }
465     try {
466     q.element();
467 dl 1.5 shouldThrow();
468 jsr166 1.15 } catch (NoSuchElementException success) {}
469 dl 1.1 }
470    
471 dl 1.5 /**
472 dl 1.6 * remove removes next element, or throws NSEE if empty
473 dl 1.5 */
474     public void testRemove() {
475 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
476 dl 1.3 for (int i = 0; i < SIZE; ++i) {
477 dl 1.85 mustEqual(i, q.remove());
478 dl 1.1 }
479     try {
480     q.remove();
481 dl 1.5 shouldThrow();
482 jsr166 1.15 } catch (NoSuchElementException success) {}
483 dl 1.1 }
484    
485 dl 1.5 /**
486 dl 1.6 * contains(x) reports true when elements added but not yet removed
487 dl 1.5 */
488     public void testContains() {
489 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
490 dl 1.3 for (int i = 0; i < SIZE; ++i) {
491 dl 1.85 mustContain(q, i);
492 dl 1.1 q.poll();
493 dl 1.85 mustNotContain(q, i);
494 dl 1.1 }
495     }
496    
497 dl 1.5 /**
498 dl 1.6 * clear removes all elements
499 dl 1.5 */
500     public void testClear() {
501 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
502 dl 1.1 q.clear();
503     assertTrue(q.isEmpty());
504 dl 1.85 mustEqual(0, q.size());
505 dl 1.11 q.add(one);
506 dl 1.1 assertFalse(q.isEmpty());
507 dl 1.85 mustContain(q, one);
508 dl 1.1 q.clear();
509     assertTrue(q.isEmpty());
510     }
511    
512 dl 1.5 /**
513 dl 1.6 * containsAll(c) is true when c contains a subset of elements
514 dl 1.5 */
515     public void testContainsAll() {
516 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
517 jsr166 1.86 PriorityBlockingQueue<Item> p = new PriorityBlockingQueue<>(SIZE);
518 dl 1.3 for (int i = 0; i < SIZE; ++i) {
519 dl 1.1 assertTrue(q.containsAll(p));
520     assertFalse(p.containsAll(q));
521 dl 1.85 mustAdd(p, i);
522 dl 1.1 }
523     assertTrue(p.containsAll(q));
524     }
525    
526 dl 1.5 /**
527 dl 1.6 * retainAll(c) retains only those elements of c and reports true if changed
528 dl 1.5 */
529     public void testRetainAll() {
530 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
531     PriorityBlockingQueue<Item> p = populatedQueue(SIZE);
532 dl 1.3 for (int i = 0; i < SIZE; ++i) {
533 dl 1.1 boolean changed = q.retainAll(p);
534     if (i == 0)
535     assertFalse(changed);
536     else
537     assertTrue(changed);
538    
539     assertTrue(q.containsAll(p));
540 dl 1.85 mustEqual(SIZE - i, q.size());
541 dl 1.1 p.remove();
542     }
543     }
544    
545 dl 1.5 /**
546 dl 1.6 * removeAll(c) removes only those elements of c and reports true if changed
547 dl 1.5 */
548     public void testRemoveAll() {
549 dl 1.3 for (int i = 1; i < SIZE; ++i) {
550 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
551     PriorityBlockingQueue<Item> p = populatedQueue(i);
552 dl 1.1 assertTrue(q.removeAll(p));
553 dl 1.85 mustEqual(SIZE - i, q.size());
554 dl 1.1 for (int j = 0; j < i; ++j) {
555 dl 1.85 Item x = p.remove();
556     mustNotContain(q, x);
557 dl 1.1 }
558     }
559     }
560    
561 dl 1.5 /**
562 jsr166 1.30 * toArray contains all elements
563 dl 1.5 */
564 jsr166 1.15 public void testToArray() throws InterruptedException {
565 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
566 jsr166 1.81 Object[] a = q.toArray();
567     assertSame(Object[].class, a.getClass());
568     Arrays.sort(a);
569     for (Object o : a)
570     assertSame(o, q.take());
571     assertTrue(q.isEmpty());
572 dl 1.1 }
573    
574 dl 1.5 /**
575 dl 1.6 * toArray(a) contains all elements
576 dl 1.5 */
577 jsr166 1.15 public void testToArray2() throws InterruptedException {
578 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
579     Item[] items = new Item[SIZE];
580     Item[] array = q.toArray(items);
581     assertSame(items, array);
582     Arrays.sort(items);
583     for (Item o : items)
584 jsr166 1.81 assertSame(o, q.take());
585     assertTrue(q.isEmpty());
586 dl 1.1 }
587 dl 1.7
588     /**
589 jsr166 1.35 * toArray(incompatible array type) throws ArrayStoreException
590 dl 1.7 */
591 jsr166 1.87 @SuppressWarnings("CollectionToArraySafeParameter")
592     public void testToArray_incompatibleArrayType() {
593 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
594 jsr166 1.16 try {
595 jsr166 1.35 q.toArray(new String[10]);
596 jsr166 1.16 shouldThrow();
597 jsr166 1.19 } catch (ArrayStoreException success) {}
598 dl 1.7 }
599 jsr166 1.12
600 dl 1.5 /**
601 dl 1.6 * iterator iterates through all elements
602 dl 1.5 */
603     public void testIterator() {
604 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
605     Iterator<? extends Item> it = q.iterator();
606 jsr166 1.58 int i;
607     for (i = 0; it.hasNext(); i++)
608 dl 1.85 mustContain(q, it.next());
609     mustEqual(i, SIZE);
610 jsr166 1.58 assertIteratorExhausted(it);
611     }
612    
613     /**
614     * iterator of empty collection has no elements
615     */
616     public void testEmptyIterator() {
617 jsr166 1.86 assertIteratorExhausted(new PriorityBlockingQueue<>().iterator());
618 dl 1.1 }
619    
620 dl 1.5 /**
621 dl 1.6 * iterator.remove removes current element
622 dl 1.5 */
623 jsr166 1.26 public void testIteratorRemove() {
624 jsr166 1.86 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(3);
625 dl 1.85 q.add(two);
626     q.add(one);
627     q.add(three);
628 dl 1.1
629 dl 1.85 Iterator<? extends Item> it = q.iterator();
630 dl 1.1 it.next();
631     it.remove();
632    
633     it = q.iterator();
634 dl 1.85 mustEqual(it.next(), two);
635     mustEqual(it.next(), three);
636 dl 1.1 assertFalse(it.hasNext());
637     }
638    
639 dl 1.5 /**
640 dl 1.6 * toString contains toStrings of elements
641 dl 1.5 */
642     public void testToString() {
643 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
644 dl 1.1 String s = q.toString();
645 dl 1.3 for (int i = 0; i < SIZE; ++i) {
646 jsr166 1.44 assertTrue(s.contains(String.valueOf(i)));
647 dl 1.1 }
648 jsr166 1.12 }
649 dl 1.1
650 dl 1.5 /**
651 jsr166 1.44 * timed poll transfers elements across Executor tasks
652 dl 1.5 */
653 dl 1.1 public void testPollInExecutor() {
654 jsr166 1.86 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(2);
655 jsr166 1.44 final CheckedBarrier threadsStarted = new CheckedBarrier(2);
656 jsr166 1.65 final ExecutorService executor = Executors.newFixedThreadPool(2);
657     try (PoolCleaner cleaner = cleaner(executor)) {
658     executor.execute(new CheckedRunnable() {
659     public void realRun() throws InterruptedException {
660     assertNull(q.poll());
661     threadsStarted.await();
662     assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
663     checkEmpty(q);
664     }});
665    
666     executor.execute(new CheckedRunnable() {
667     public void realRun() throws InterruptedException {
668     threadsStarted.await();
669     q.put(one);
670     }});
671     }
672 dl 1.2 }
673    
674 dl 1.5 /**
675 jsr166 1.79 * A deserialized/reserialized queue has same elements
676 dl 1.5 */
677 jsr166 1.15 public void testSerialization() throws Exception {
678 dl 1.85 Queue<Item> x = populatedQueue(SIZE);
679     Queue<Item> y = serialClone(x);
680 jsr166 1.46
681 jsr166 1.52 assertNotSame(x, y);
682 dl 1.85 mustEqual(x.size(), y.size());
683 jsr166 1.46 while (!x.isEmpty()) {
684     assertFalse(y.isEmpty());
685 dl 1.85 mustEqual(x.remove(), y.remove());
686 jsr166 1.46 }
687     assertTrue(y.isEmpty());
688 dl 1.1 }
689 dl 1.7
690     /**
691     * drainTo(c) empties queue into another collection c
692 jsr166 1.12 */
693 dl 1.7 public void testDrainTo() {
694 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
695 jsr166 1.86 ArrayList<Item> l = new ArrayList<>();
696 dl 1.7 q.drainTo(l);
697 dl 1.85 mustEqual(0, q.size());
698     mustEqual(SIZE, l.size());
699 jsr166 1.12 for (int i = 0; i < SIZE; ++i)
700 dl 1.85 mustEqual(l.get(i), i);
701 dl 1.11 q.add(zero);
702     q.add(one);
703     assertFalse(q.isEmpty());
704 dl 1.85 mustContain(q, one);
705 dl 1.11 l.clear();
706     q.drainTo(l);
707 dl 1.85 mustEqual(0, q.size());
708     mustEqual(2, l.size());
709 jsr166 1.12 for (int i = 0; i < 2; ++i)
710 dl 1.85 mustEqual(l.get(i), i);
711 dl 1.7 }
712    
713     /**
714     * drainTo empties queue
715 jsr166 1.12 */
716 jsr166 1.15 public void testDrainToWithActivePut() throws InterruptedException {
717 dl 1.85 final PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
718 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
719     public void realRun() {
720 dl 1.85 q.put(new Item(SIZE + 1));
721 jsr166 1.15 }});
722    
723     t.start();
724 jsr166 1.86 ArrayList<Item> l = new ArrayList<>();
725 jsr166 1.15 q.drainTo(l);
726     assertTrue(l.size() >= SIZE);
727     for (int i = 0; i < SIZE; ++i)
728 dl 1.85 mustEqual(l.get(i), i);
729 jsr166 1.15 t.join();
730     assertTrue(q.size() + l.size() >= SIZE);
731 dl 1.7 }
732    
733     /**
734 jsr166 1.31 * drainTo(c, n) empties first min(n, size) elements of queue into c
735 jsr166 1.12 */
736 dl 1.7 public void testDrainToN() {
737 jsr166 1.86 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<>(SIZE * 2);
738 dl 1.7 for (int i = 0; i < SIZE + 2; ++i) {
739 jsr166 1.13 for (int j = 0; j < SIZE; j++)
740 dl 1.85 mustOffer(q, j);
741 jsr166 1.86 ArrayList<Item> l = new ArrayList<>();
742 dl 1.7 q.drainTo(l, i);
743 jsr166 1.32 int k = (i < SIZE) ? i : SIZE;
744 dl 1.85 mustEqual(k, l.size());
745     mustEqual(SIZE - k, q.size());
746 jsr166 1.12 for (int j = 0; j < k; ++j)
747 dl 1.85 mustEqual(l.get(j), j);
748 jsr166 1.55 do {} while (q.poll() != null);
749 dl 1.7 }
750     }
751    
752 jsr166 1.53 /**
753     * remove(null), contains(null) always return false
754     */
755     public void testNeverContainsNull() {
756     Collection<?>[] qs = {
757 jsr166 1.86 new PriorityBlockingQueue<>(),
758 jsr166 1.53 populatedQueue(2),
759     };
760    
761     for (Collection<?> q : qs) {
762     assertFalse(q.contains(null));
763     assertFalse(q.remove(null));
764     }
765     }
766    
767 dl 1.1 }