ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.44
Committed: Fri May 27 20:07:24 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.43: +37 -27 lines
Log Message:
performance and robustness improvements to queue tests

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