ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.26
Committed: Wed Aug 25 00:07:03 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +2 -2 lines
Log Message:
whitespace

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