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