ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.21
Committed: Sat Nov 21 21:00:34 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +3 -3 lines
Log Message:
reduce scope of check for IE in test*WithOffer*

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.16 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     int i = ((Integer)x).intValue();
29     int j = ((Integer)y).intValue();
30     if (i < j) return 1;
31     if (i > j) return -1;
32     return 0;
33     }
34     }
35    
36     /**
37     * Create a queue of given size containing consecutive
38     * Integers 0 ... n.
39     */
40 dl 1.3 private PriorityBlockingQueue populatedQueue(int n) {
41 dl 1.1 PriorityBlockingQueue q = new PriorityBlockingQueue(n);
42     assertTrue(q.isEmpty());
43 jsr166 1.16 for (int i = n-1; i >= 0; i-=2)
44     assertTrue(q.offer(new Integer(i)));
45     for (int i = (n & 1); i < n; i+=2)
46     assertTrue(q.offer(new Integer(i)));
47 dl 1.1 assertFalse(q.isEmpty());
48     assertEquals(NOCAP, q.remainingCapacity());
49 jsr166 1.16 assertEquals(n, q.size());
50 dl 1.1 return q;
51     }
52 jsr166 1.12
53 dl 1.5 /**
54 dl 1.6 * A new queue has unbounded capacity
55 dl 1.5 */
56     public void testConstructor1() {
57 dl 1.3 assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
58 dl 1.1 }
59    
60 dl 1.5 /**
61 jsr166 1.19 * Constructor throws IAE if capacity argument nonpositive
62 dl 1.5 */
63     public void testConstructor2() {
64 dl 1.1 try {
65     PriorityBlockingQueue q = new PriorityBlockingQueue(0);
66 dl 1.5 shouldThrow();
67 jsr166 1.15 } catch (IllegalArgumentException success) {}
68 dl 1.1 }
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 jsr166 1.15 } catch (NullPointerException success) {}
78 dl 1.1 }
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 jsr166 1.15 } catch (NullPointerException success) {}
89 dl 1.1 }
90    
91 dl 1.5 /**
92 dl 1.6 * Initializing from Collection with some null elements throws NPE
93 dl 1.5 */
94     public void testConstructor5() {
95 dl 1.1 try {
96 dl 1.3 Integer[] ints = new Integer[SIZE];
97     for (int i = 0; i < SIZE-1; ++i)
98 dl 1.1 ints[i] = new Integer(i);
99     PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
100 dl 1.5 shouldThrow();
101 jsr166 1.15 } catch (NullPointerException success) {}
102 dl 1.1 }
103    
104 dl 1.5 /**
105 dl 1.6 * Queue contains all elements of collection used to initialize
106 dl 1.5 */
107     public void testConstructor6() {
108 jsr166 1.15 Integer[] ints = new Integer[SIZE];
109     for (int i = 0; i < SIZE; ++i)
110     ints[i] = new Integer(i);
111     PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
112     for (int i = 0; i < SIZE; ++i)
113     assertEquals(ints[i], q.poll());
114 dl 1.1 }
115    
116 dl 1.5 /**
117 dl 1.6 * The comparator used in constructor is used
118 dl 1.5 */
119     public void testConstructor7() {
120 jsr166 1.15 MyReverseComparator cmp = new MyReverseComparator();
121     PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp);
122     assertEquals(cmp, q.comparator());
123     Integer[] ints = new Integer[SIZE];
124     for (int i = 0; i < SIZE; ++i)
125     ints[i] = new Integer(i);
126     q.addAll(Arrays.asList(ints));
127     for (int i = SIZE-1; i >= 0; --i)
128     assertEquals(ints[i], q.poll());
129 dl 1.1 }
130    
131 dl 1.5 /**
132 dl 1.6 * isEmpty is true before add, false after
133 dl 1.5 */
134 dl 1.1 public void testEmpty() {
135     PriorityBlockingQueue q = new PriorityBlockingQueue(2);
136     assertTrue(q.isEmpty());
137     assertEquals(NOCAP, q.remainingCapacity());
138 dl 1.6 q.add(one);
139 dl 1.1 assertFalse(q.isEmpty());
140 dl 1.6 q.add(two);
141 dl 1.1 q.remove();
142     q.remove();
143     assertTrue(q.isEmpty());
144     }
145    
146 dl 1.5 /**
147 dl 1.9 * remainingCapacity does not change when elements added or removed,
148 dl 1.6 * but size does
149 dl 1.5 */
150     public void testRemainingCapacity() {
151 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
152     for (int i = 0; i < SIZE; ++i) {
153 dl 1.1 assertEquals(NOCAP, q.remainingCapacity());
154 dl 1.3 assertEquals(SIZE-i, q.size());
155 dl 1.1 q.remove();
156     }
157 dl 1.3 for (int i = 0; i < SIZE; ++i) {
158 dl 1.1 assertEquals(NOCAP, q.remainingCapacity());
159     assertEquals(i, q.size());
160     q.add(new Integer(i));
161     }
162     }
163    
164 dl 1.5 /**
165 dl 1.6 * offer(null) throws NPE
166 dl 1.5 */
167     public void testOfferNull() {
168 jsr166 1.16 try {
169 dl 1.1 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
170     q.offer(null);
171 dl 1.5 shouldThrow();
172 jsr166 1.15 } catch (NullPointerException success) {}
173 dl 1.1 }
174    
175 dl 1.5 /**
176 dl 1.7 * add(null) throws NPE
177     */
178     public void testAddNull() {
179 jsr166 1.16 try {
180 dl 1.7 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
181     q.add(null);
182     shouldThrow();
183 jsr166 1.15 } catch (NullPointerException success) {}
184 dl 1.7 }
185    
186     /**
187 dl 1.6 * Offer of comparable element succeeds
188 dl 1.5 */
189 dl 1.1 public void testOffer() {
190     PriorityBlockingQueue q = new PriorityBlockingQueue(1);
191 dl 1.6 assertTrue(q.offer(zero));
192     assertTrue(q.offer(one));
193 dl 1.1 }
194    
195 dl 1.5 /**
196 dl 1.6 * Offer of non-Comparable throws CCE
197 dl 1.5 */
198 dl 1.1 public void testOfferNonComparable() {
199     try {
200     PriorityBlockingQueue q = new PriorityBlockingQueue(1);
201     q.offer(new Object());
202     q.offer(new Object());
203     q.offer(new Object());
204 dl 1.5 shouldThrow();
205 jsr166 1.15 } catch (ClassCastException success) {}
206 dl 1.1 }
207    
208 dl 1.5 /**
209 dl 1.6 * add of comparable succeeds
210 dl 1.5 */
211     public void testAdd() {
212 dl 1.3 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
213     for (int i = 0; i < SIZE; ++i) {
214 dl 1.1 assertEquals(i, q.size());
215     assertTrue(q.add(new Integer(i)));
216     }
217     }
218    
219 dl 1.5 /**
220 dl 1.6 * addAll(null) throws NPE
221 dl 1.5 */
222     public void testAddAll1() {
223 dl 1.1 try {
224     PriorityBlockingQueue q = new PriorityBlockingQueue(1);
225     q.addAll(null);
226 dl 1.5 shouldThrow();
227 jsr166 1.15 } catch (NullPointerException success) {}
228 dl 1.1 }
229 dl 1.7
230     /**
231     * addAll(this) throws IAE
232     */
233     public void testAddAllSelf() {
234     try {
235     PriorityBlockingQueue q = populatedQueue(SIZE);
236     q.addAll(q);
237     shouldThrow();
238 jsr166 1.15 } catch (IllegalArgumentException success) {}
239 dl 1.7 }
240    
241 dl 1.5 /**
242 dl 1.6 * addAll of a collection with null elements throws NPE
243 dl 1.5 */
244     public void testAddAll2() {
245 dl 1.1 try {
246 dl 1.3 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
247     Integer[] ints = new Integer[SIZE];
248 dl 1.1 q.addAll(Arrays.asList(ints));
249 dl 1.5 shouldThrow();
250 jsr166 1.15 } catch (NullPointerException success) {}
251 dl 1.1 }
252 dl 1.5 /**
253 dl 1.6 * addAll of a collection with any null elements throws NPE after
254     * possibly adding some elements
255 dl 1.5 */
256     public void testAddAll3() {
257 dl 1.1 try {
258 dl 1.3 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
259     Integer[] ints = new Integer[SIZE];
260     for (int i = 0; i < SIZE-1; ++i)
261 dl 1.1 ints[i] = new Integer(i);
262     q.addAll(Arrays.asList(ints));
263 dl 1.5 shouldThrow();
264 jsr166 1.15 } catch (NullPointerException success) {}
265 dl 1.1 }
266    
267 dl 1.5 /**
268 dl 1.6 * Queue contains all elements of successful addAll
269 dl 1.5 */
270     public void testAddAll5() {
271 jsr166 1.15 Integer[] empty = new Integer[0];
272     Integer[] ints = new Integer[SIZE];
273     for (int i = SIZE-1; i >= 0; --i)
274     ints[i] = new Integer(i);
275     PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
276     assertFalse(q.addAll(Arrays.asList(empty)));
277     assertTrue(q.addAll(Arrays.asList(ints)));
278     for (int i = 0; i < SIZE; ++i)
279     assertEquals(ints[i], q.poll());
280 dl 1.1 }
281    
282 dl 1.5 /**
283 dl 1.6 * put(null) throws NPE
284 dl 1.5 */
285 dl 1.1 public void testPutNull() {
286 jsr166 1.16 try {
287 dl 1.3 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
288 dl 1.1 q.put(null);
289 dl 1.5 shouldThrow();
290 jsr166 1.15 } catch (NullPointerException success) {}
291 dl 1.1 }
292    
293 dl 1.5 /**
294 dl 1.6 * all elements successfully put are contained
295 dl 1.5 */
296 dl 1.1 public void testPut() {
297 jsr166 1.15 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
298     for (int i = 0; i < SIZE; ++i) {
299     Integer I = new Integer(i);
300     q.put(I);
301     assertTrue(q.contains(I));
302 dl 1.1 }
303 jsr166 1.15 assertEquals(SIZE, q.size());
304 dl 1.1 }
305    
306 dl 1.5 /**
307 dl 1.6 * put doesn't block waiting for take
308 dl 1.5 */
309 jsr166 1.15 public void testPutWithTake() throws InterruptedException {
310 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
311 jsr166 1.15 final int size = 4;
312     Thread t = new Thread(new CheckedRunnable() {
313     public void realRun() {
314     for (int i = 0; i < size; i++)
315     q.put(new Integer(0));
316     }});
317    
318     t.start();
319     Thread.sleep(SHORT_DELAY_MS);
320     assertEquals(q.size(), size);
321     q.take();
322     t.interrupt();
323     t.join();
324 dl 1.1 }
325    
326 dl 1.5 /**
327 dl 1.6 * timed offer does not time out
328 dl 1.5 */
329 jsr166 1.15 public void testTimedOffer() throws InterruptedException {
330 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
331 jsr166 1.18 Thread t = new Thread(new CheckedRunnable() {
332     public void realRun() {
333     q.put(new Integer(0));
334     q.put(new Integer(0));
335     threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
336     threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
337     }});
338 jsr166 1.12
339 jsr166 1.15 t.start();
340     Thread.sleep(SMALL_DELAY_MS);
341     t.interrupt();
342     t.join();
343 dl 1.1 }
344    
345 dl 1.5 /**
346 dl 1.6 * take retrieves elements in priority order
347 dl 1.5 */
348 jsr166 1.15 public void testTake() throws InterruptedException {
349     PriorityBlockingQueue q = populatedQueue(SIZE);
350     for (int i = 0; i < SIZE; ++i) {
351     assertEquals(i, ((Integer)q.take()).intValue());
352     }
353 dl 1.1 }
354    
355 dl 1.5 /**
356 dl 1.6 * take blocks interruptibly when empty
357 dl 1.5 */
358 jsr166 1.15 public void testTakeFromEmpty() throws InterruptedException {
359 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
360 jsr166 1.15 Thread t = new Thread(new CheckedInterruptedRunnable() {
361     public void realRun() throws InterruptedException {
362     q.take();
363     }});
364    
365     t.start();
366     Thread.sleep(SHORT_DELAY_MS);
367     t.interrupt();
368     t.join();
369 dl 1.1 }
370    
371 dl 1.5 /**
372 dl 1.6 * Take removes existing elements until empty, then blocks interruptibly
373 dl 1.5 */
374 jsr166 1.15 public void testBlockingTake() throws InterruptedException {
375     Thread t = new Thread(new CheckedInterruptedRunnable() {
376     public void realRun() throws InterruptedException {
377     PriorityBlockingQueue q = populatedQueue(SIZE);
378     for (int i = 0; i < SIZE; ++i) {
379     threadAssertEquals(i, ((Integer)q.take()).intValue());
380     }
381     q.take();
382     }});
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 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
398     }
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.17 assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
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.17 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
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.18 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
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 jsr166 1.18 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
456 jsr166 1.21 assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
457 jsr166 1.15 try {
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 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
478     q.poll();
479     assertTrue(q.peek() == null ||
480     i != ((Integer)q.peek()).intValue());
481     }
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 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
492     q.poll();
493     }
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 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
507     }
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.16 try {
633 dl 1.7 PriorityBlockingQueue q = populatedQueue(SIZE);
634 jsr166 1.16 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.16 try {
644 dl 1.7 PriorityBlockingQueue q = populatedQueue(SIZE);
645 jsr166 1.16 Object o[] = q.toArray(new String[10] );
646     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 dl 1.1 public void testIteratorRemove () {
668     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 dl 1.3 threadAssertNull(q.poll());
704 jsr166 1.17 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
705 jsr166 1.15 threadAssertTrue(q.isEmpty());
706     }});
707    
708     executor.execute(new CheckedRunnable() {
709     public void realRun() throws InterruptedException {
710     Thread.sleep(SMALL_DELAY_MS);
711     q.put(new Integer(1));
712     }});
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 }