ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.28
Committed: Wed Sep 29 12:33:48 2010 UTC (13 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.27: +2 -2 lines
Log Message:
Allow InterruptedException during initial checks in *TimedPollWithOffer

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 jsr166 1.27
249 dl 1.5 /**
250 dl 1.6 * addAll of a collection with any null elements throws NPE after
251     * possibly adding some elements
252 dl 1.5 */
253     public void testAddAll3() {
254 dl 1.1 try {
255 dl 1.3 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
256     Integer[] ints = new Integer[SIZE];
257     for (int i = 0; i < SIZE-1; ++i)
258 dl 1.1 ints[i] = new Integer(i);
259     q.addAll(Arrays.asList(ints));
260 dl 1.5 shouldThrow();
261 jsr166 1.15 } catch (NullPointerException success) {}
262 dl 1.1 }
263    
264 dl 1.5 /**
265 dl 1.6 * Queue contains all elements of successful addAll
266 dl 1.5 */
267     public void testAddAll5() {
268 jsr166 1.15 Integer[] empty = new Integer[0];
269     Integer[] ints = new Integer[SIZE];
270     for (int i = SIZE-1; i >= 0; --i)
271     ints[i] = new Integer(i);
272     PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
273     assertFalse(q.addAll(Arrays.asList(empty)));
274     assertTrue(q.addAll(Arrays.asList(ints)));
275     for (int i = 0; i < SIZE; ++i)
276     assertEquals(ints[i], q.poll());
277 dl 1.1 }
278    
279 dl 1.5 /**
280 dl 1.6 * put(null) throws NPE
281 dl 1.5 */
282 dl 1.1 public void testPutNull() {
283 jsr166 1.16 try {
284 dl 1.3 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
285 dl 1.1 q.put(null);
286 dl 1.5 shouldThrow();
287 jsr166 1.15 } catch (NullPointerException success) {}
288 dl 1.1 }
289    
290 dl 1.5 /**
291 dl 1.6 * all elements successfully put are contained
292 dl 1.5 */
293 dl 1.1 public void testPut() {
294 jsr166 1.15 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
295     for (int i = 0; i < SIZE; ++i) {
296     Integer I = new Integer(i);
297     q.put(I);
298     assertTrue(q.contains(I));
299 dl 1.1 }
300 jsr166 1.15 assertEquals(SIZE, q.size());
301 dl 1.1 }
302    
303 dl 1.5 /**
304 dl 1.6 * put doesn't block waiting for take
305 dl 1.5 */
306 jsr166 1.15 public void testPutWithTake() throws InterruptedException {
307 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
308 jsr166 1.15 final int size = 4;
309     Thread t = new Thread(new CheckedRunnable() {
310     public void realRun() {
311     for (int i = 0; i < size; i++)
312     q.put(new Integer(0));
313     }});
314    
315     t.start();
316     Thread.sleep(SHORT_DELAY_MS);
317     assertEquals(q.size(), size);
318     q.take();
319     t.interrupt();
320     t.join();
321 dl 1.1 }
322    
323 dl 1.5 /**
324 dl 1.6 * timed offer does not time out
325 dl 1.5 */
326 jsr166 1.15 public void testTimedOffer() throws InterruptedException {
327 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
328 jsr166 1.18 Thread t = new Thread(new CheckedRunnable() {
329     public void realRun() {
330     q.put(new Integer(0));
331     q.put(new Integer(0));
332 jsr166 1.22 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
333     assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
334 jsr166 1.18 }});
335 jsr166 1.12
336 jsr166 1.15 t.start();
337     Thread.sleep(SMALL_DELAY_MS);
338     t.interrupt();
339     t.join();
340 dl 1.1 }
341    
342 dl 1.5 /**
343 dl 1.6 * take retrieves elements in priority order
344 dl 1.5 */
345 jsr166 1.15 public void testTake() throws InterruptedException {
346     PriorityBlockingQueue q = populatedQueue(SIZE);
347     for (int i = 0; i < SIZE; ++i) {
348 jsr166 1.24 assertEquals(i, q.take());
349 jsr166 1.15 }
350 dl 1.1 }
351    
352 dl 1.5 /**
353 dl 1.6 * take blocks interruptibly when empty
354 dl 1.5 */
355 jsr166 1.15 public void testTakeFromEmpty() throws InterruptedException {
356 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
357 jsr166 1.15 Thread t = new Thread(new CheckedInterruptedRunnable() {
358     public void realRun() throws InterruptedException {
359     q.take();
360     }});
361    
362     t.start();
363     Thread.sleep(SHORT_DELAY_MS);
364     t.interrupt();
365     t.join();
366 dl 1.1 }
367    
368 dl 1.5 /**
369 dl 1.6 * Take removes existing elements until empty, then blocks interruptibly
370 dl 1.5 */
371 jsr166 1.15 public void testBlockingTake() throws InterruptedException {
372 jsr166 1.23 final PriorityBlockingQueue q = populatedQueue(SIZE);
373     Thread t = new Thread(new CheckedRunnable() {
374 jsr166 1.15 public void realRun() throws InterruptedException {
375     for (int i = 0; i < SIZE; ++i) {
376 jsr166 1.23 assertEquals(i, q.take());
377 jsr166 1.15 }
378 jsr166 1.23 try {
379     q.take();
380     shouldThrow();
381     } catch (InterruptedException success) {}
382 jsr166 1.15 }});
383    
384 dl 1.1 t.start();
385 jsr166 1.15 Thread.sleep(SHORT_DELAY_MS);
386     t.interrupt();
387     t.join();
388 dl 1.1 }
389    
390    
391 dl 1.5 /**
392 dl 1.6 * poll succeeds unless empty
393 dl 1.5 */
394     public void testPoll() {
395 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
396     for (int i = 0; i < SIZE; ++i) {
397 jsr166 1.24 assertEquals(i, q.poll());
398 dl 1.1 }
399 jsr166 1.16 assertNull(q.poll());
400 dl 1.1 }
401    
402 dl 1.5 /**
403 dl 1.6 * timed pool with zero timeout succeeds when non-empty, else times out
404 dl 1.5 */
405 jsr166 1.15 public void testTimedPoll0() throws InterruptedException {
406     PriorityBlockingQueue q = populatedQueue(SIZE);
407     for (int i = 0; i < SIZE; ++i) {
408 jsr166 1.24 assertEquals(i, q.poll(0, MILLISECONDS));
409 jsr166 1.15 }
410 jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
411 dl 1.1 }
412    
413 dl 1.5 /**
414 dl 1.6 * timed pool with nonzero timeout succeeds when non-empty, else times out
415 dl 1.5 */
416 jsr166 1.15 public void testTimedPoll() throws InterruptedException {
417     PriorityBlockingQueue q = populatedQueue(SIZE);
418     for (int i = 0; i < SIZE; ++i) {
419 jsr166 1.24 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
420 jsr166 1.15 }
421 jsr166 1.17 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
422 dl 1.1 }
423    
424 dl 1.5 /**
425 dl 1.6 * Interrupted timed poll throws InterruptedException instead of
426     * returning timeout status
427 dl 1.5 */
428 jsr166 1.15 public void testInterruptedTimedPoll() throws InterruptedException {
429     Thread t = new Thread(new CheckedRunnable() {
430     public void realRun() throws InterruptedException {
431     PriorityBlockingQueue q = populatedQueue(SIZE);
432     for (int i = 0; i < SIZE; ++i) {
433 jsr166 1.24 assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
434 jsr166 1.15 }
435     try {
436 jsr166 1.20 q.poll(SMALL_DELAY_MS, MILLISECONDS);
437 jsr166 1.18 shouldThrow();
438 jsr166 1.15 } catch (InterruptedException success) {}
439     }});
440    
441 dl 1.1 t.start();
442 jsr166 1.15 Thread.sleep(SHORT_DELAY_MS);
443     t.interrupt();
444     t.join();
445 dl 1.1 }
446    
447 dl 1.5 /**
448 dl 1.6 * timed poll before a delayed offer fails; after offer succeeds;
449     * on interruption throws
450 dl 1.5 */
451 jsr166 1.15 public void testTimedPollWithOffer() throws InterruptedException {
452 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
453 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
454     public void realRun() throws InterruptedException {
455     try {
456 dl 1.28 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
457     assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
458 jsr166 1.17 q.poll(LONG_DELAY_MS, MILLISECONDS);
459 jsr166 1.21 shouldThrow();
460 jsr166 1.15 } catch (InterruptedException success) {}
461     }});
462    
463     t.start();
464     Thread.sleep(SMALL_DELAY_MS);
465 jsr166 1.21 assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
466 jsr166 1.15 t.interrupt();
467     t.join();
468 jsr166 1.12 }
469 dl 1.1
470    
471 dl 1.5 /**
472 dl 1.6 * peek returns next element, or null if empty
473 dl 1.5 */
474     public void testPeek() {
475 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
476     for (int i = 0; i < SIZE; ++i) {
477 jsr166 1.24 assertEquals(i, q.peek());
478     assertEquals(i, q.poll());
479 dl 1.1 assertTrue(q.peek() == null ||
480 jsr166 1.24 !q.peek().equals(i));
481 dl 1.1 }
482 jsr166 1.16 assertNull(q.peek());
483 dl 1.1 }
484    
485 dl 1.5 /**
486 dl 1.6 * element returns next element, or throws NSEE if empty
487 dl 1.5 */
488     public void testElement() {
489 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
490     for (int i = 0; i < SIZE; ++i) {
491 jsr166 1.24 assertEquals(i, q.element());
492     assertEquals(i, q.poll());
493 dl 1.1 }
494     try {
495     q.element();
496 dl 1.5 shouldThrow();
497 jsr166 1.15 } catch (NoSuchElementException success) {}
498 dl 1.1 }
499    
500 dl 1.5 /**
501 dl 1.6 * remove removes next element, or throws NSEE if empty
502 dl 1.5 */
503     public void testRemove() {
504 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
505     for (int i = 0; i < SIZE; ++i) {
506 jsr166 1.24 assertEquals(i, q.remove());
507 dl 1.1 }
508     try {
509     q.remove();
510 dl 1.5 shouldThrow();
511 jsr166 1.15 } catch (NoSuchElementException success) {}
512 dl 1.1 }
513    
514 dl 1.5 /**
515 dl 1.6 * remove(x) removes x and returns true if present
516 dl 1.5 */
517     public void testRemoveElement() {
518 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
519     for (int i = 1; i < SIZE; i+=2) {
520 dl 1.1 assertTrue(q.remove(new Integer(i)));
521     }
522 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
523 dl 1.1 assertTrue(q.remove(new Integer(i)));
524     assertFalse(q.remove(new Integer(i+1)));
525     }
526 dl 1.2 assertTrue(q.isEmpty());
527 dl 1.1 }
528 jsr166 1.12
529 dl 1.5 /**
530 dl 1.6 * contains(x) reports true when elements added but not yet removed
531 dl 1.5 */
532     public void testContains() {
533 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
534     for (int i = 0; i < SIZE; ++i) {
535 dl 1.1 assertTrue(q.contains(new Integer(i)));
536     q.poll();
537     assertFalse(q.contains(new Integer(i)));
538     }
539     }
540    
541 dl 1.5 /**
542 dl 1.6 * clear removes all elements
543 dl 1.5 */
544     public void testClear() {
545 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
546 dl 1.1 q.clear();
547     assertTrue(q.isEmpty());
548     assertEquals(0, q.size());
549 dl 1.11 q.add(one);
550 dl 1.1 assertFalse(q.isEmpty());
551 dl 1.11 assertTrue(q.contains(one));
552 dl 1.1 q.clear();
553     assertTrue(q.isEmpty());
554     }
555    
556 dl 1.5 /**
557 dl 1.6 * containsAll(c) is true when c contains a subset of elements
558 dl 1.5 */
559     public void testContainsAll() {
560 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
561     PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE);
562     for (int i = 0; i < SIZE; ++i) {
563 dl 1.1 assertTrue(q.containsAll(p));
564     assertFalse(p.containsAll(q));
565     p.add(new Integer(i));
566     }
567     assertTrue(p.containsAll(q));
568     }
569    
570 dl 1.5 /**
571 dl 1.6 * retainAll(c) retains only those elements of c and reports true if changed
572 dl 1.5 */
573     public void testRetainAll() {
574 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
575     PriorityBlockingQueue p = populatedQueue(SIZE);
576     for (int i = 0; i < SIZE; ++i) {
577 dl 1.1 boolean changed = q.retainAll(p);
578     if (i == 0)
579     assertFalse(changed);
580     else
581     assertTrue(changed);
582    
583     assertTrue(q.containsAll(p));
584 dl 1.3 assertEquals(SIZE-i, q.size());
585 dl 1.1 p.remove();
586     }
587     }
588    
589 dl 1.5 /**
590 dl 1.6 * removeAll(c) removes only those elements of c and reports true if changed
591 dl 1.5 */
592     public void testRemoveAll() {
593 dl 1.3 for (int i = 1; i < SIZE; ++i) {
594     PriorityBlockingQueue q = populatedQueue(SIZE);
595     PriorityBlockingQueue p = populatedQueue(i);
596 dl 1.1 assertTrue(q.removeAll(p));
597 dl 1.3 assertEquals(SIZE-i, q.size());
598 dl 1.1 for (int j = 0; j < i; ++j) {
599     Integer I = (Integer)(p.remove());
600     assertFalse(q.contains(I));
601     }
602     }
603     }
604    
605 dl 1.5 /**
606 dl 1.6 * toArray contains all elements
607 dl 1.5 */
608 jsr166 1.15 public void testToArray() throws InterruptedException {
609 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
610 jsr166 1.16 Object[] o = q.toArray();
611 dl 1.1 Arrays.sort(o);
612 jsr166 1.16 for (int i = 0; i < o.length; i++)
613     assertEquals(o[i], q.take());
614 dl 1.1 }
615    
616 dl 1.5 /**
617 dl 1.6 * toArray(a) contains all elements
618 dl 1.5 */
619 jsr166 1.15 public void testToArray2() throws InterruptedException {
620 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
621 jsr166 1.16 Integer[] ints = new Integer[SIZE];
622     ints = (Integer[])q.toArray(ints);
623 dl 1.1 Arrays.sort(ints);
624 jsr166 1.15 for (int i = 0; i < ints.length; i++)
625     assertEquals(ints[i], q.take());
626 dl 1.1 }
627 dl 1.7
628     /**
629     * toArray(null) throws NPE
630     */
631     public void testToArray_BadArg() {
632 jsr166 1.24 PriorityBlockingQueue q = populatedQueue(SIZE);
633 jsr166 1.16 try {
634     Object o[] = q.toArray(null);
635     shouldThrow();
636     } catch (NullPointerException success) {}
637 dl 1.7 }
638    
639     /**
640 dl 1.9 * toArray with incompatible array type throws CCE
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.24 Object o[] = 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    
684 dl 1.5 /**
685 dl 1.6 * toString contains toStrings of elements
686 dl 1.5 */
687     public void testToString() {
688 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
689 dl 1.1 String s = q.toString();
690 dl 1.3 for (int i = 0; i < SIZE; ++i) {
691 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
692     }
693 jsr166 1.12 }
694 dl 1.1
695 dl 1.5 /**
696 dl 1.6 * offer transfers elements across Executor tasks
697 dl 1.5 */
698 dl 1.1 public void testPollInExecutor() {
699     final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
700     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     assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
705     assertTrue(q.isEmpty());
706 jsr166 1.15 }});
707    
708     executor.execute(new CheckedRunnable() {
709     public void realRun() throws InterruptedException {
710     Thread.sleep(SMALL_DELAY_MS);
711 jsr166 1.25 q.put(one);
712 jsr166 1.15 }});
713 jsr166 1.12
714 dl 1.3 joinPool(executor);
715 dl 1.2 }
716    
717 dl 1.5 /**
718 jsr166 1.12 * A deserialized serialized queue has same elements
719 dl 1.5 */
720 jsr166 1.15 public void testSerialization() throws Exception {
721 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
722 jsr166 1.15 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
723     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
724     out.writeObject(q);
725     out.close();
726    
727     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
728     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
729     PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
730     assertEquals(q.size(), r.size());
731     while (!q.isEmpty())
732     assertEquals(q.remove(), r.remove());
733 dl 1.1 }
734 dl 1.7
735     /**
736     * drainTo(null) throws NPE
737 jsr166 1.12 */
738 dl 1.7 public void testDrainToNull() {
739     PriorityBlockingQueue q = populatedQueue(SIZE);
740     try {
741     q.drainTo(null);
742     shouldThrow();
743 jsr166 1.15 } catch (NullPointerException success) {}
744 dl 1.7 }
745    
746     /**
747     * drainTo(this) throws IAE
748 jsr166 1.12 */
749 dl 1.7 public void testDrainToSelf() {
750     PriorityBlockingQueue q = populatedQueue(SIZE);
751     try {
752     q.drainTo(q);
753     shouldThrow();
754 jsr166 1.15 } catch (IllegalArgumentException success) {}
755 dl 1.7 }
756    
757     /**
758     * drainTo(c) empties queue into another collection c
759 jsr166 1.12 */
760 dl 1.7 public void testDrainTo() {
761     PriorityBlockingQueue q = populatedQueue(SIZE);
762     ArrayList l = new ArrayList();
763     q.drainTo(l);
764     assertEquals(q.size(), 0);
765     assertEquals(l.size(), SIZE);
766 jsr166 1.12 for (int i = 0; i < SIZE; ++i)
767 dl 1.7 assertEquals(l.get(i), new Integer(i));
768 dl 1.11 q.add(zero);
769     q.add(one);
770     assertFalse(q.isEmpty());
771     assertTrue(q.contains(zero));
772     assertTrue(q.contains(one));
773     l.clear();
774     q.drainTo(l);
775     assertEquals(q.size(), 0);
776     assertEquals(l.size(), 2);
777 jsr166 1.12 for (int i = 0; i < 2; ++i)
778 dl 1.11 assertEquals(l.get(i), new Integer(i));
779 dl 1.7 }
780    
781     /**
782     * drainTo empties queue
783 jsr166 1.12 */
784 jsr166 1.15 public void testDrainToWithActivePut() throws InterruptedException {
785 dl 1.7 final PriorityBlockingQueue q = populatedQueue(SIZE);
786 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
787     public void realRun() {
788     q.put(new Integer(SIZE+1));
789     }});
790    
791     t.start();
792     ArrayList l = new ArrayList();
793     q.drainTo(l);
794     assertTrue(l.size() >= SIZE);
795     for (int i = 0; i < SIZE; ++i)
796     assertEquals(l.get(i), new Integer(i));
797     t.join();
798     assertTrue(q.size() + l.size() >= SIZE);
799 dl 1.7 }
800    
801     /**
802     * drainTo(null, n) throws NPE
803 jsr166 1.12 */
804 dl 1.7 public void testDrainToNullN() {
805     PriorityBlockingQueue q = populatedQueue(SIZE);
806     try {
807     q.drainTo(null, 0);
808     shouldThrow();
809 jsr166 1.15 } catch (NullPointerException success) {}
810 dl 1.7 }
811    
812     /**
813     * drainTo(this, n) throws IAE
814 jsr166 1.12 */
815 dl 1.7 public void testDrainToSelfN() {
816     PriorityBlockingQueue q = populatedQueue(SIZE);
817     try {
818     q.drainTo(q, 0);
819     shouldThrow();
820 jsr166 1.15 } catch (IllegalArgumentException success) {}
821 dl 1.7 }
822    
823     /**
824     * drainTo(c, n) empties first max {n, size} elements of queue into c
825 jsr166 1.12 */
826 dl 1.7 public void testDrainToN() {
827 dl 1.11 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
828 dl 1.7 for (int i = 0; i < SIZE + 2; ++i) {
829 jsr166 1.13 for (int j = 0; j < SIZE; j++)
830 dl 1.11 assertTrue(q.offer(new Integer(j)));
831 dl 1.7 ArrayList l = new ArrayList();
832     q.drainTo(l, i);
833     int k = (i < SIZE)? i : SIZE;
834 dl 1.11 assertEquals(l.size(), k);
835 dl 1.7 assertEquals(q.size(), SIZE-k);
836 jsr166 1.12 for (int j = 0; j < k; ++j)
837 dl 1.11 assertEquals(l.get(j), new Integer(j));
838     while (q.poll() != null) ;
839 dl 1.7 }
840     }
841    
842 dl 1.1 }