ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.7
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.6: +148 -0 lines
Log Message:
Added tests and documentation

File Contents

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