ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.85
Committed: Tue Jan 26 13:33:06 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.84: +150 -157 lines
Log Message:
Replace Integer with Item class

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     PriorityBlockingQueue<Item> q =
92     new PriorityBlockingQueue<Item>(n);
93 dl 1.1 assertTrue(q.isEmpty());
94 jsr166 1.64 for (int i = n - 1; i >= 0; i -= 2)
95 dl 1.85 mustOffer(q, i);
96 jsr166 1.56 for (int i = (n & 1); i < n; i += 2)
97 dl 1.85 mustOffer(q, i);
98 dl 1.1 assertFalse(q.isEmpty());
99 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
100     mustEqual(n, q.size());
101     mustEqual(0, q.peek());
102 dl 1.1 return q;
103     }
104 jsr166 1.12
105 dl 1.5 /**
106 dl 1.6 * A new queue has unbounded capacity
107 dl 1.5 */
108     public void testConstructor1() {
109 dl 1.85 mustEqual(Integer.MAX_VALUE,
110     new PriorityBlockingQueue<Item>(SIZE).remainingCapacity());
111 dl 1.1 }
112    
113 dl 1.5 /**
114 jsr166 1.78 * Constructor throws IllegalArgumentException if capacity argument nonpositive
115 dl 1.5 */
116     public void testConstructor2() {
117 dl 1.1 try {
118 dl 1.85 new PriorityBlockingQueue<Item>(0);
119 dl 1.5 shouldThrow();
120 jsr166 1.15 } catch (IllegalArgumentException success) {}
121 dl 1.1 }
122    
123 dl 1.5 /**
124 dl 1.6 * Initializing from null Collection throws NPE
125 dl 1.5 */
126     public void testConstructor3() {
127 dl 1.1 try {
128 dl 1.85 new PriorityBlockingQueue<Item>(null);
129 dl 1.5 shouldThrow();
130 jsr166 1.15 } catch (NullPointerException success) {}
131 dl 1.1 }
132    
133 dl 1.5 /**
134 dl 1.6 * Initializing from Collection of null elements throws NPE
135 dl 1.5 */
136     public void testConstructor4() {
137 dl 1.85 Collection<Item> elements = Arrays.asList(new Item[SIZE]);
138 dl 1.1 try {
139 dl 1.85 new PriorityBlockingQueue<Item>(elements);
140 dl 1.5 shouldThrow();
141 jsr166 1.15 } catch (NullPointerException success) {}
142 dl 1.1 }
143    
144 dl 1.5 /**
145 dl 1.6 * Initializing from Collection with some null elements throws NPE
146 dl 1.5 */
147     public void testConstructor5() {
148 dl 1.85 Item[] items = new Item[2]; items[0] = zero;
149     Collection<Item> elements = Arrays.asList(items);
150 dl 1.1 try {
151 dl 1.85 new PriorityBlockingQueue<Item>(elements);
152 dl 1.5 shouldThrow();
153 jsr166 1.15 } catch (NullPointerException success) {}
154 dl 1.1 }
155    
156 dl 1.5 /**
157 dl 1.6 * Queue contains all elements of collection used to initialize
158 dl 1.5 */
159     public void testConstructor6() {
160 dl 1.85 Item[] items = defaultItems;
161     PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(Arrays.asList(items));
162 jsr166 1.15 for (int i = 0; i < SIZE; ++i)
163 dl 1.85 mustEqual(items[i], q.poll());
164 dl 1.1 }
165    
166 dl 1.5 /**
167 dl 1.6 * The comparator used in constructor is used
168 dl 1.5 */
169     public void testConstructor7() {
170 jsr166 1.15 MyReverseComparator cmp = new MyReverseComparator();
171 dl 1.85 @SuppressWarnings("unchecked")
172     PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE, cmp);
173     mustEqual(cmp, q.comparator());
174     Item[] items = defaultItems;
175     q.addAll(Arrays.asList(items));
176 jsr166 1.63 for (int i = SIZE - 1; i >= 0; --i)
177 dl 1.85 mustEqual(items[i], q.poll());
178 dl 1.1 }
179    
180 dl 1.5 /**
181 dl 1.6 * isEmpty is true before add, false after
182 dl 1.5 */
183 dl 1.1 public void testEmpty() {
184 dl 1.85 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(2);
185 dl 1.1 assertTrue(q.isEmpty());
186 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
187 dl 1.6 q.add(one);
188 dl 1.1 assertFalse(q.isEmpty());
189 dl 1.6 q.add(two);
190 dl 1.1 q.remove();
191     q.remove();
192     assertTrue(q.isEmpty());
193     }
194    
195 dl 1.5 /**
196 jsr166 1.59 * remainingCapacity() always returns Integer.MAX_VALUE
197 dl 1.5 */
198     public void testRemainingCapacity() {
199 dl 1.85 BlockingQueue<Item> q = populatedQueue(SIZE);
200 dl 1.3 for (int i = 0; i < SIZE; ++i) {
201 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
202     mustEqual(SIZE - i, q.size());
203     mustEqual(i, q.remove());
204 dl 1.1 }
205 dl 1.3 for (int i = 0; i < SIZE; ++i) {
206 dl 1.85 mustEqual(Integer.MAX_VALUE, q.remainingCapacity());
207     mustEqual(i, q.size());
208     mustAdd(q, i);
209 dl 1.1 }
210     }
211    
212 dl 1.5 /**
213 dl 1.6 * Offer of comparable element succeeds
214 dl 1.5 */
215 dl 1.1 public void testOffer() {
216 dl 1.85 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(1);
217 dl 1.6 assertTrue(q.offer(zero));
218     assertTrue(q.offer(one));
219 dl 1.1 }
220    
221 dl 1.5 /**
222 dl 1.6 * Offer of non-Comparable throws CCE
223 dl 1.5 */
224 dl 1.1 public void testOfferNonComparable() {
225 dl 1.85 PriorityBlockingQueue<Object> q = new PriorityBlockingQueue<Object>(1);
226 dl 1.1 try {
227     q.offer(new Object());
228 dl 1.5 shouldThrow();
229 jsr166 1.67 } catch (ClassCastException success) {
230     assertTrue(q.isEmpty());
231 dl 1.85 mustEqual(0, q.size());
232 jsr166 1.67 assertNull(q.poll());
233     }
234 dl 1.1 }
235    
236 dl 1.5 /**
237 dl 1.6 * add of comparable succeeds
238 dl 1.5 */
239     public void testAdd() {
240 dl 1.85 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE);
241 dl 1.3 for (int i = 0; i < SIZE; ++i) {
242 dl 1.85 mustEqual(i, q.size());
243     mustAdd(q, i);
244 dl 1.1 }
245     }
246    
247 dl 1.5 /**
248 jsr166 1.78 * addAll(this) throws IllegalArgumentException
249 dl 1.7 */
250     public void testAddAllSelf() {
251 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
252 dl 1.7 try {
253     q.addAll(q);
254     shouldThrow();
255 jsr166 1.15 } catch (IllegalArgumentException success) {}
256 dl 1.7 }
257    
258 dl 1.5 /**
259 dl 1.6 * addAll of a collection with any null elements throws NPE after
260     * possibly adding some elements
261 dl 1.5 */
262     public void testAddAll3() {
263 dl 1.85 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE);
264     Item[] items = new Item[2];
265     items[0] = zero;
266 dl 1.1 try {
267 dl 1.85 q.addAll(Arrays.asList(items));
268 dl 1.5 shouldThrow();
269 jsr166 1.15 } catch (NullPointerException success) {}
270 dl 1.1 }
271    
272 dl 1.5 /**
273 dl 1.6 * Queue contains all elements of successful addAll
274 dl 1.5 */
275     public void testAddAll5() {
276 dl 1.85 Item[] empty = new Item[0];
277     Item[] items = defaultItems;
278     PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE);
279 jsr166 1.15 assertFalse(q.addAll(Arrays.asList(empty)));
280 dl 1.85 assertTrue(q.addAll(Arrays.asList(items)));
281 jsr166 1.15 for (int i = 0; i < SIZE; ++i)
282 dl 1.85 mustEqual(items[i], q.poll());
283 dl 1.1 }
284    
285 dl 1.5 /**
286 dl 1.6 * all elements successfully put are contained
287 dl 1.5 */
288 jsr166 1.42 public void testPut() {
289 dl 1.85 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(SIZE);
290 jsr166 1.42 for (int i = 0; i < SIZE; ++i) {
291 dl 1.85 Item x = itemFor(i);
292 jsr166 1.57 q.put(x);
293 dl 1.85 mustContain(q, x);
294 jsr166 1.42 }
295 dl 1.85 mustEqual(SIZE, q.size());
296 dl 1.1 }
297    
298 dl 1.5 /**
299 dl 1.6 * put doesn't block waiting for take
300 dl 1.5 */
301 jsr166 1.15 public void testPutWithTake() throws InterruptedException {
302 dl 1.85 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(2);
303 jsr166 1.15 final int size = 4;
304 jsr166 1.44 Thread t = newStartedThread(new CheckedRunnable() {
305 jsr166 1.15 public void realRun() {
306     for (int i = 0; i < size; i++)
307 dl 1.85 q.put(zero);
308 jsr166 1.15 }});
309    
310 jsr166 1.44 awaitTermination(t);
311 dl 1.85 mustEqual(size, q.size());
312 jsr166 1.15 q.take();
313 dl 1.1 }
314    
315 dl 1.5 /**
316 jsr166 1.83 * Queue is unbounded, so timed offer never times out
317 dl 1.5 */
318 jsr166 1.77 public void testTimedOffer() {
319 dl 1.85 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(2);
320 jsr166 1.44 Thread t = newStartedThread(new CheckedRunnable() {
321 jsr166 1.18 public void realRun() {
322 dl 1.85 q.put(one);
323     q.put(two);
324     assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
325     assertTrue(q.offer(zero, LONG_DELAY_MS, MILLISECONDS));
326 jsr166 1.18 }});
327 jsr166 1.12
328 jsr166 1.44 awaitTermination(t);
329 dl 1.1 }
330    
331 dl 1.5 /**
332 dl 1.6 * take retrieves elements in priority order
333 dl 1.5 */
334 jsr166 1.15 public void testTake() throws InterruptedException {
335 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
336 jsr166 1.15 for (int i = 0; i < SIZE; ++i) {
337 dl 1.85 mustEqual(i, q.take());
338 jsr166 1.15 }
339 dl 1.1 }
340    
341 dl 1.5 /**
342 dl 1.6 * Take removes existing elements until empty, then blocks interruptibly
343 dl 1.5 */
344 jsr166 1.15 public void testBlockingTake() throws InterruptedException {
345 dl 1.85 final PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
346 jsr166 1.44 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
347     Thread t = newStartedThread(new CheckedRunnable() {
348 jsr166 1.15 public void realRun() throws InterruptedException {
349 dl 1.85 for (int i = 0; i < SIZE; i++) mustEqual(i, q.take());
350 jsr166 1.44
351     Thread.currentThread().interrupt();
352 jsr166 1.23 try {
353     q.take();
354     shouldThrow();
355     } catch (InterruptedException success) {}
356 jsr166 1.44 assertFalse(Thread.interrupted());
357    
358     pleaseInterrupt.countDown();
359     try {
360     q.take();
361     shouldThrow();
362     } catch (InterruptedException success) {}
363     assertFalse(Thread.interrupted());
364 jsr166 1.15 }});
365    
366 jsr166 1.44 await(pleaseInterrupt);
367 jsr166 1.82 if (randomBoolean()) assertThreadBlocks(t, Thread.State.WAITING);
368 jsr166 1.15 t.interrupt();
369 jsr166 1.44 awaitTermination(t);
370 dl 1.1 }
371    
372 dl 1.5 /**
373 dl 1.6 * poll succeeds unless empty
374 dl 1.5 */
375     public void testPoll() {
376 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
377 dl 1.3 for (int i = 0; i < SIZE; ++i) {
378 dl 1.85 mustEqual(i, q.poll());
379 dl 1.1 }
380 jsr166 1.16 assertNull(q.poll());
381 dl 1.1 }
382    
383 dl 1.5 /**
384 jsr166 1.34 * timed poll with zero timeout succeeds when non-empty, else times out
385 dl 1.5 */
386 jsr166 1.15 public void testTimedPoll0() throws InterruptedException {
387 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
388 jsr166 1.15 for (int i = 0; i < SIZE; ++i) {
389 dl 1.85 mustEqual(i, q.poll(0, MILLISECONDS));
390 jsr166 1.15 }
391 jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
392 dl 1.1 }
393    
394 dl 1.5 /**
395 jsr166 1.34 * timed poll with nonzero timeout succeeds when non-empty, else times out
396 dl 1.5 */
397 jsr166 1.15 public void testTimedPoll() throws InterruptedException {
398 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
399 jsr166 1.15 for (int i = 0; i < SIZE; ++i) {
400 jsr166 1.44 long startTime = System.nanoTime();
401 dl 1.85 mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
402 jsr166 1.44 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
403     }
404     long startTime = System.nanoTime();
405     assertNull(q.poll(timeoutMillis(), MILLISECONDS));
406     assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
407     checkEmpty(q);
408 dl 1.1 }
409    
410 dl 1.5 /**
411 dl 1.6 * Interrupted timed poll throws InterruptedException instead of
412     * returning timeout status
413 dl 1.5 */
414 jsr166 1.15 public void testInterruptedTimedPoll() throws InterruptedException {
415 dl 1.85 final BlockingQueue<Item> q = populatedQueue(SIZE);
416 jsr166 1.75 final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
417 jsr166 1.40 Thread t = newStartedThread(new CheckedRunnable() {
418 jsr166 1.15 public void realRun() throws InterruptedException {
419 jsr166 1.76 for (int i = 0; i < SIZE; i++)
420 dl 1.85 mustEqual(i, q.poll(LONG_DELAY_MS, MILLISECONDS));
421 jsr166 1.76
422     Thread.currentThread().interrupt();
423     try {
424 jsr166 1.82 q.poll(randomTimeout(), randomTimeUnit());
425 jsr166 1.76 shouldThrow();
426     } catch (InterruptedException success) {}
427     assertFalse(Thread.interrupted());
428 jsr166 1.75
429     pleaseInterrupt.countDown();
430 jsr166 1.15 try {
431 jsr166 1.84 q.poll(LONGER_DELAY_MS, MILLISECONDS);
432 jsr166 1.18 shouldThrow();
433 jsr166 1.75 } catch (InterruptedException success) {}
434     assertFalse(Thread.interrupted());
435 jsr166 1.15 }});
436    
437 jsr166 1.75 await(pleaseInterrupt);
438 jsr166 1.82 if (randomBoolean()) assertThreadBlocks(t, Thread.State.TIMED_WAITING);
439 jsr166 1.15 t.interrupt();
440 jsr166 1.66 awaitTermination(t);
441 dl 1.1 }
442    
443 dl 1.5 /**
444 dl 1.6 * peek returns next element, or null if empty
445 dl 1.5 */
446     public void testPeek() {
447 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
448 dl 1.3 for (int i = 0; i < SIZE; ++i) {
449 dl 1.85 mustEqual(i, q.peek());
450     mustEqual(i, q.poll());
451 dl 1.1 assertTrue(q.peek() == null ||
452 jsr166 1.24 !q.peek().equals(i));
453 dl 1.1 }
454 jsr166 1.16 assertNull(q.peek());
455 dl 1.1 }
456    
457 dl 1.5 /**
458 dl 1.6 * element returns next element, or throws NSEE if empty
459 dl 1.5 */
460     public void testElement() {
461 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
462 dl 1.3 for (int i = 0; i < SIZE; ++i) {
463 dl 1.85 mustEqual(i, q.element());
464     mustEqual(i, q.poll());
465 dl 1.1 }
466     try {
467     q.element();
468 dl 1.5 shouldThrow();
469 jsr166 1.15 } catch (NoSuchElementException success) {}
470 dl 1.1 }
471    
472 dl 1.5 /**
473 dl 1.6 * remove removes next element, or throws NSEE if empty
474 dl 1.5 */
475     public void testRemove() {
476 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
477 dl 1.3 for (int i = 0; i < SIZE; ++i) {
478 dl 1.85 mustEqual(i, q.remove());
479 dl 1.1 }
480     try {
481     q.remove();
482 dl 1.5 shouldThrow();
483 jsr166 1.15 } catch (NoSuchElementException success) {}
484 dl 1.1 }
485    
486 dl 1.5 /**
487 dl 1.6 * contains(x) reports true when elements added but not yet removed
488 dl 1.5 */
489     public void testContains() {
490 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
491 dl 1.3 for (int i = 0; i < SIZE; ++i) {
492 dl 1.85 mustContain(q, i);
493 dl 1.1 q.poll();
494 dl 1.85 mustNotContain(q, i);
495 dl 1.1 }
496     }
497    
498 dl 1.5 /**
499 dl 1.6 * clear removes all elements
500 dl 1.5 */
501     public void testClear() {
502 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
503 dl 1.1 q.clear();
504     assertTrue(q.isEmpty());
505 dl 1.85 mustEqual(0, q.size());
506 dl 1.11 q.add(one);
507 dl 1.1 assertFalse(q.isEmpty());
508 dl 1.85 mustContain(q, one);
509 dl 1.1 q.clear();
510     assertTrue(q.isEmpty());
511     }
512    
513 dl 1.5 /**
514 dl 1.6 * containsAll(c) is true when c contains a subset of elements
515 dl 1.5 */
516     public void testContainsAll() {
517 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
518     PriorityBlockingQueue<Item> p = new PriorityBlockingQueue<Item>(SIZE);
519 dl 1.3 for (int i = 0; i < SIZE; ++i) {
520 dl 1.1 assertTrue(q.containsAll(p));
521     assertFalse(p.containsAll(q));
522 dl 1.85 mustAdd(p, i);
523 dl 1.1 }
524     assertTrue(p.containsAll(q));
525     }
526    
527 dl 1.5 /**
528 dl 1.6 * retainAll(c) retains only those elements of c and reports true if changed
529 dl 1.5 */
530     public void testRetainAll() {
531 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
532     PriorityBlockingQueue<Item> p = populatedQueue(SIZE);
533 dl 1.3 for (int i = 0; i < SIZE; ++i) {
534 dl 1.1 boolean changed = q.retainAll(p);
535     if (i == 0)
536     assertFalse(changed);
537     else
538     assertTrue(changed);
539    
540     assertTrue(q.containsAll(p));
541 dl 1.85 mustEqual(SIZE - i, q.size());
542 dl 1.1 p.remove();
543     }
544     }
545    
546 dl 1.5 /**
547 dl 1.6 * removeAll(c) removes only those elements of c and reports true if changed
548 dl 1.5 */
549     public void testRemoveAll() {
550 dl 1.3 for (int i = 1; i < SIZE; ++i) {
551 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
552     PriorityBlockingQueue<Item> p = populatedQueue(i);
553 dl 1.1 assertTrue(q.removeAll(p));
554 dl 1.85 mustEqual(SIZE - i, q.size());
555 dl 1.1 for (int j = 0; j < i; ++j) {
556 dl 1.85 Item x = p.remove();
557     mustNotContain(q, x);
558 dl 1.1 }
559     }
560     }
561    
562 dl 1.5 /**
563 jsr166 1.30 * toArray contains all elements
564 dl 1.5 */
565 jsr166 1.15 public void testToArray() throws InterruptedException {
566 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
567 jsr166 1.81 Object[] a = q.toArray();
568     assertSame(Object[].class, a.getClass());
569     Arrays.sort(a);
570     for (Object o : a)
571     assertSame(o, q.take());
572     assertTrue(q.isEmpty());
573 dl 1.1 }
574    
575 dl 1.5 /**
576 dl 1.6 * toArray(a) contains all elements
577 dl 1.5 */
578 jsr166 1.15 public void testToArray2() throws InterruptedException {
579 dl 1.85 PriorityBlockingQueue<Item> q = populatedQueue(SIZE);
580     Item[] items = new Item[SIZE];
581     Item[] array = q.toArray(items);
582     assertSame(items, array);
583     Arrays.sort(items);
584     for (Item o : items)
585 jsr166 1.81 assertSame(o, q.take());
586     assertTrue(q.isEmpty());
587 dl 1.1 }
588 dl 1.7
589     /**
590 jsr166 1.35 * toArray(incompatible array type) throws ArrayStoreException
591 dl 1.7 */
592     public void testToArray1_BadArg() {
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 dl 1.85 assertIteratorExhausted(new PriorityBlockingQueue<Item>().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 dl 1.85 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(3);
625     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 dl 1.85 final PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(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     ArrayList<Item> l = new ArrayList<Item>();
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 dl 1.85 ArrayList<Item> l = new ArrayList<Item>();
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 dl 1.85 PriorityBlockingQueue<Item> q = new PriorityBlockingQueue<Item>(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     ArrayList<Item> l = new ArrayList<Item>();
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     new PriorityBlockingQueue<Object>(),
758     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 }