ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.17
Committed: Sat Nov 21 02:33:20 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.16: +14 -13 lines
Log Message:
import static TimeUnit.MILLISECONDS

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 dl 1.6 * 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     Thread t = new Thread(new Runnable() {
332 dl 1.5 public void run() {
333 dl 1.1 try {
334     q.put(new Integer(0));
335     q.put(new Integer(0));
336 jsr166 1.17 threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
337     threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
338 dl 1.1 } finally { }
339     }
340     });
341 jsr166 1.12
342 jsr166 1.15 t.start();
343     Thread.sleep(SMALL_DELAY_MS);
344     t.interrupt();
345     t.join();
346 dl 1.1 }
347    
348 dl 1.5 /**
349 dl 1.6 * take retrieves elements in priority order
350 dl 1.5 */
351 jsr166 1.15 public void testTake() throws InterruptedException {
352     PriorityBlockingQueue q = populatedQueue(SIZE);
353     for (int i = 0; i < SIZE; ++i) {
354     assertEquals(i, ((Integer)q.take()).intValue());
355     }
356 dl 1.1 }
357    
358 dl 1.5 /**
359 dl 1.6 * take blocks interruptibly when empty
360 dl 1.5 */
361 jsr166 1.15 public void testTakeFromEmpty() throws InterruptedException {
362 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
363 jsr166 1.15 Thread t = new Thread(new CheckedInterruptedRunnable() {
364     public void realRun() throws InterruptedException {
365     q.take();
366     }});
367    
368     t.start();
369     Thread.sleep(SHORT_DELAY_MS);
370     t.interrupt();
371     t.join();
372 dl 1.1 }
373    
374 dl 1.5 /**
375 dl 1.6 * Take removes existing elements until empty, then blocks interruptibly
376 dl 1.5 */
377 jsr166 1.15 public void testBlockingTake() throws InterruptedException {
378     Thread t = new Thread(new CheckedInterruptedRunnable() {
379     public void realRun() throws InterruptedException {
380     PriorityBlockingQueue q = populatedQueue(SIZE);
381     for (int i = 0; i < SIZE; ++i) {
382     threadAssertEquals(i, ((Integer)q.take()).intValue());
383     }
384     q.take();
385     }});
386    
387 dl 1.1 t.start();
388 jsr166 1.15 Thread.sleep(SHORT_DELAY_MS);
389     t.interrupt();
390     t.join();
391 dl 1.1 }
392    
393    
394 dl 1.5 /**
395 dl 1.6 * poll succeeds unless empty
396 dl 1.5 */
397     public void testPoll() {
398 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
399     for (int i = 0; i < SIZE; ++i) {
400 dl 1.1 assertEquals(i, ((Integer)q.poll()).intValue());
401     }
402 jsr166 1.16 assertNull(q.poll());
403 dl 1.1 }
404    
405 dl 1.5 /**
406 dl 1.6 * timed pool with zero timeout succeeds when non-empty, else times out
407 dl 1.5 */
408 jsr166 1.15 public void testTimedPoll0() throws InterruptedException {
409     PriorityBlockingQueue q = populatedQueue(SIZE);
410     for (int i = 0; i < SIZE; ++i) {
411 jsr166 1.17 assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
412 jsr166 1.15 }
413 jsr166 1.17 assertNull(q.poll(0, MILLISECONDS));
414 dl 1.1 }
415    
416 dl 1.5 /**
417 dl 1.6 * timed pool with nonzero timeout succeeds when non-empty, else times out
418 dl 1.5 */
419 jsr166 1.15 public void testTimedPoll() throws InterruptedException {
420     PriorityBlockingQueue q = populatedQueue(SIZE);
421     for (int i = 0; i < SIZE; ++i) {
422 jsr166 1.17 assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
423 jsr166 1.15 }
424 jsr166 1.17 assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
425 dl 1.1 }
426    
427 dl 1.5 /**
428 dl 1.6 * Interrupted timed poll throws InterruptedException instead of
429     * returning timeout status
430 dl 1.5 */
431 jsr166 1.15 public void testInterruptedTimedPoll() throws InterruptedException {
432     Thread t = new Thread(new CheckedRunnable() {
433     public void realRun() throws InterruptedException {
434     PriorityBlockingQueue q = populatedQueue(SIZE);
435     for (int i = 0; i < SIZE; ++i) {
436 jsr166 1.17 threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
437 jsr166 1.15 }
438     try {
439 jsr166 1.17 q.poll(SMALL_DELAY_MS, MILLISECONDS);
440 jsr166 1.15 threadShouldThrow();
441     } catch (InterruptedException success) {}
442     }});
443    
444 dl 1.1 t.start();
445 jsr166 1.15 Thread.sleep(SHORT_DELAY_MS);
446     t.interrupt();
447     t.join();
448 dl 1.1 }
449    
450 dl 1.5 /**
451 dl 1.6 * timed poll before a delayed offer fails; after offer succeeds;
452     * on interruption throws
453 dl 1.5 */
454 jsr166 1.15 public void testTimedPollWithOffer() throws InterruptedException {
455 dl 1.1 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
456 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
457     public void realRun() throws InterruptedException {
458 jsr166 1.17 threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
459     threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
460 jsr166 1.15 try {
461 jsr166 1.17 q.poll(LONG_DELAY_MS, MILLISECONDS);
462 jsr166 1.15 threadShouldThrow();
463     } catch (InterruptedException success) {}
464     }});
465    
466     t.start();
467     Thread.sleep(SMALL_DELAY_MS);
468 jsr166 1.17 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
469 jsr166 1.15 t.interrupt();
470     t.join();
471 jsr166 1.12 }
472 dl 1.1
473    
474 dl 1.5 /**
475 dl 1.6 * peek returns next element, or null if empty
476 dl 1.5 */
477     public void testPeek() {
478 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
479     for (int i = 0; i < SIZE; ++i) {
480 dl 1.1 assertEquals(i, ((Integer)q.peek()).intValue());
481     q.poll();
482     assertTrue(q.peek() == null ||
483     i != ((Integer)q.peek()).intValue());
484     }
485 jsr166 1.16 assertNull(q.peek());
486 dl 1.1 }
487    
488 dl 1.5 /**
489 dl 1.6 * element returns next element, or throws NSEE if empty
490 dl 1.5 */
491     public void testElement() {
492 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
493     for (int i = 0; i < SIZE; ++i) {
494 dl 1.1 assertEquals(i, ((Integer)q.element()).intValue());
495     q.poll();
496     }
497     try {
498     q.element();
499 dl 1.5 shouldThrow();
500 jsr166 1.15 } catch (NoSuchElementException success) {}
501 dl 1.1 }
502    
503 dl 1.5 /**
504 dl 1.6 * remove removes next element, or throws NSEE if empty
505 dl 1.5 */
506     public void testRemove() {
507 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
508     for (int i = 0; i < SIZE; ++i) {
509 dl 1.1 assertEquals(i, ((Integer)q.remove()).intValue());
510     }
511     try {
512     q.remove();
513 dl 1.5 shouldThrow();
514 jsr166 1.15 } catch (NoSuchElementException success) {}
515 dl 1.1 }
516    
517 dl 1.5 /**
518 dl 1.6 * remove(x) removes x and returns true if present
519 dl 1.5 */
520     public void testRemoveElement() {
521 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
522     for (int i = 1; i < SIZE; i+=2) {
523 dl 1.1 assertTrue(q.remove(new Integer(i)));
524     }
525 dl 1.3 for (int i = 0; i < SIZE; i+=2) {
526 dl 1.1 assertTrue(q.remove(new Integer(i)));
527     assertFalse(q.remove(new Integer(i+1)));
528     }
529 dl 1.2 assertTrue(q.isEmpty());
530 dl 1.1 }
531 jsr166 1.12
532 dl 1.5 /**
533 dl 1.6 * contains(x) reports true when elements added but not yet removed
534 dl 1.5 */
535     public void testContains() {
536 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
537     for (int i = 0; i < SIZE; ++i) {
538 dl 1.1 assertTrue(q.contains(new Integer(i)));
539     q.poll();
540     assertFalse(q.contains(new Integer(i)));
541     }
542     }
543    
544 dl 1.5 /**
545 dl 1.6 * clear removes all elements
546 dl 1.5 */
547     public void testClear() {
548 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
549 dl 1.1 q.clear();
550     assertTrue(q.isEmpty());
551     assertEquals(0, q.size());
552 dl 1.11 q.add(one);
553 dl 1.1 assertFalse(q.isEmpty());
554 dl 1.11 assertTrue(q.contains(one));
555 dl 1.1 q.clear();
556     assertTrue(q.isEmpty());
557     }
558    
559 dl 1.5 /**
560 dl 1.6 * containsAll(c) is true when c contains a subset of elements
561 dl 1.5 */
562     public void testContainsAll() {
563 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
564     PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE);
565     for (int i = 0; i < SIZE; ++i) {
566 dl 1.1 assertTrue(q.containsAll(p));
567     assertFalse(p.containsAll(q));
568     p.add(new Integer(i));
569     }
570     assertTrue(p.containsAll(q));
571     }
572    
573 dl 1.5 /**
574 dl 1.6 * retainAll(c) retains only those elements of c and reports true if changed
575 dl 1.5 */
576     public void testRetainAll() {
577 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
578     PriorityBlockingQueue p = populatedQueue(SIZE);
579     for (int i = 0; i < SIZE; ++i) {
580 dl 1.1 boolean changed = q.retainAll(p);
581     if (i == 0)
582     assertFalse(changed);
583     else
584     assertTrue(changed);
585    
586     assertTrue(q.containsAll(p));
587 dl 1.3 assertEquals(SIZE-i, q.size());
588 dl 1.1 p.remove();
589     }
590     }
591    
592 dl 1.5 /**
593 dl 1.6 * removeAll(c) removes only those elements of c and reports true if changed
594 dl 1.5 */
595     public void testRemoveAll() {
596 dl 1.3 for (int i = 1; i < SIZE; ++i) {
597     PriorityBlockingQueue q = populatedQueue(SIZE);
598     PriorityBlockingQueue p = populatedQueue(i);
599 dl 1.1 assertTrue(q.removeAll(p));
600 dl 1.3 assertEquals(SIZE-i, q.size());
601 dl 1.1 for (int j = 0; j < i; ++j) {
602     Integer I = (Integer)(p.remove());
603     assertFalse(q.contains(I));
604     }
605     }
606     }
607    
608 dl 1.5 /**
609 dl 1.6 * toArray contains all elements
610 dl 1.5 */
611 jsr166 1.15 public void testToArray() throws InterruptedException {
612 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
613 jsr166 1.16 Object[] o = q.toArray();
614 dl 1.1 Arrays.sort(o);
615 jsr166 1.16 for (int i = 0; i < o.length; i++)
616     assertEquals(o[i], q.take());
617 dl 1.1 }
618    
619 dl 1.5 /**
620 dl 1.6 * toArray(a) contains all elements
621 dl 1.5 */
622 jsr166 1.15 public void testToArray2() throws InterruptedException {
623 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
624 jsr166 1.16 Integer[] ints = new Integer[SIZE];
625     ints = (Integer[])q.toArray(ints);
626 dl 1.1 Arrays.sort(ints);
627 jsr166 1.15 for (int i = 0; i < ints.length; i++)
628     assertEquals(ints[i], q.take());
629 dl 1.1 }
630 dl 1.7
631     /**
632     * toArray(null) throws NPE
633     */
634     public void testToArray_BadArg() {
635 jsr166 1.16 try {
636 dl 1.7 PriorityBlockingQueue q = populatedQueue(SIZE);
637 jsr166 1.16 Object o[] = q.toArray(null);
638     shouldThrow();
639     } catch (NullPointerException success) {}
640 dl 1.7 }
641    
642     /**
643 dl 1.9 * toArray with incompatible array type throws CCE
644 dl 1.7 */
645     public void testToArray1_BadArg() {
646 jsr166 1.16 try {
647 dl 1.7 PriorityBlockingQueue q = populatedQueue(SIZE);
648 jsr166 1.16 Object o[] = q.toArray(new String[10] );
649     shouldThrow();
650     } catch (ArrayStoreException success) {}
651 dl 1.7 }
652 jsr166 1.12
653 dl 1.5 /**
654 dl 1.6 * iterator iterates through all elements
655 dl 1.5 */
656     public void testIterator() {
657 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
658 dl 1.1 int i = 0;
659 jsr166 1.16 Iterator it = q.iterator();
660 jsr166 1.13 while (it.hasNext()) {
661 dl 1.1 assertTrue(q.contains(it.next()));
662     ++i;
663     }
664 dl 1.3 assertEquals(i, SIZE);
665 dl 1.1 }
666    
667 dl 1.5 /**
668 dl 1.6 * iterator.remove removes current element
669 dl 1.5 */
670 dl 1.1 public void testIteratorRemove () {
671     final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
672     q.add(new Integer(2));
673     q.add(new Integer(1));
674     q.add(new Integer(3));
675    
676     Iterator it = q.iterator();
677     it.next();
678     it.remove();
679    
680     it = q.iterator();
681     assertEquals(it.next(), new Integer(2));
682     assertEquals(it.next(), new Integer(3));
683     assertFalse(it.hasNext());
684     }
685    
686    
687 dl 1.5 /**
688 dl 1.6 * toString contains toStrings of elements
689 dl 1.5 */
690     public void testToString() {
691 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
692 dl 1.1 String s = q.toString();
693 dl 1.3 for (int i = 0; i < SIZE; ++i) {
694 dl 1.1 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
695     }
696 jsr166 1.12 }
697 dl 1.1
698 dl 1.5 /**
699 dl 1.6 * offer transfers elements across Executor tasks
700 dl 1.5 */
701 dl 1.1 public void testPollInExecutor() {
702     final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
703     ExecutorService executor = Executors.newFixedThreadPool(2);
704 jsr166 1.15 executor.execute(new CheckedRunnable() {
705     public void realRun() throws InterruptedException {
706 dl 1.3 threadAssertNull(q.poll());
707 jsr166 1.17 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
708 jsr166 1.15 threadAssertTrue(q.isEmpty());
709     }});
710    
711     executor.execute(new CheckedRunnable() {
712     public void realRun() throws InterruptedException {
713     Thread.sleep(SMALL_DELAY_MS);
714     q.put(new Integer(1));
715     }});
716 jsr166 1.12
717 dl 1.3 joinPool(executor);
718 dl 1.2 }
719    
720 dl 1.5 /**
721 jsr166 1.12 * A deserialized serialized queue has same elements
722 dl 1.5 */
723 jsr166 1.15 public void testSerialization() throws Exception {
724 dl 1.3 PriorityBlockingQueue q = populatedQueue(SIZE);
725 jsr166 1.15 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
726     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
727     out.writeObject(q);
728     out.close();
729    
730     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
731     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
732     PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
733     assertEquals(q.size(), r.size());
734     while (!q.isEmpty())
735     assertEquals(q.remove(), r.remove());
736 dl 1.1 }
737 dl 1.7
738     /**
739     * drainTo(null) throws NPE
740 jsr166 1.12 */
741 dl 1.7 public void testDrainToNull() {
742     PriorityBlockingQueue q = populatedQueue(SIZE);
743     try {
744     q.drainTo(null);
745     shouldThrow();
746 jsr166 1.15 } catch (NullPointerException success) {}
747 dl 1.7 }
748    
749     /**
750     * drainTo(this) throws IAE
751 jsr166 1.12 */
752 dl 1.7 public void testDrainToSelf() {
753     PriorityBlockingQueue q = populatedQueue(SIZE);
754     try {
755     q.drainTo(q);
756     shouldThrow();
757 jsr166 1.15 } catch (IllegalArgumentException success) {}
758 dl 1.7 }
759    
760     /**
761     * drainTo(c) empties queue into another collection c
762 jsr166 1.12 */
763 dl 1.7 public void testDrainTo() {
764     PriorityBlockingQueue q = populatedQueue(SIZE);
765     ArrayList l = new ArrayList();
766     q.drainTo(l);
767     assertEquals(q.size(), 0);
768     assertEquals(l.size(), SIZE);
769 jsr166 1.12 for (int i = 0; i < SIZE; ++i)
770 dl 1.7 assertEquals(l.get(i), new Integer(i));
771 dl 1.11 q.add(zero);
772     q.add(one);
773     assertFalse(q.isEmpty());
774     assertTrue(q.contains(zero));
775     assertTrue(q.contains(one));
776     l.clear();
777     q.drainTo(l);
778     assertEquals(q.size(), 0);
779     assertEquals(l.size(), 2);
780 jsr166 1.12 for (int i = 0; i < 2; ++i)
781 dl 1.11 assertEquals(l.get(i), new Integer(i));
782 dl 1.7 }
783    
784     /**
785     * drainTo empties queue
786 jsr166 1.12 */
787 jsr166 1.15 public void testDrainToWithActivePut() throws InterruptedException {
788 dl 1.7 final PriorityBlockingQueue q = populatedQueue(SIZE);
789 jsr166 1.15 Thread t = new Thread(new CheckedRunnable() {
790     public void realRun() {
791     q.put(new Integer(SIZE+1));
792     }});
793    
794     t.start();
795     ArrayList l = new ArrayList();
796     q.drainTo(l);
797     assertTrue(l.size() >= SIZE);
798     for (int i = 0; i < SIZE; ++i)
799     assertEquals(l.get(i), new Integer(i));
800     t.join();
801     assertTrue(q.size() + l.size() >= SIZE);
802 dl 1.7 }
803    
804     /**
805     * drainTo(null, n) throws NPE
806 jsr166 1.12 */
807 dl 1.7 public void testDrainToNullN() {
808     PriorityBlockingQueue q = populatedQueue(SIZE);
809     try {
810     q.drainTo(null, 0);
811     shouldThrow();
812 jsr166 1.15 } catch (NullPointerException success) {}
813 dl 1.7 }
814    
815     /**
816     * drainTo(this, n) throws IAE
817 jsr166 1.12 */
818 dl 1.7 public void testDrainToSelfN() {
819     PriorityBlockingQueue q = populatedQueue(SIZE);
820     try {
821     q.drainTo(q, 0);
822     shouldThrow();
823 jsr166 1.15 } catch (IllegalArgumentException success) {}
824 dl 1.7 }
825    
826     /**
827     * drainTo(c, n) empties first max {n, size} elements of queue into c
828 jsr166 1.12 */
829 dl 1.7 public void testDrainToN() {
830 dl 1.11 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
831 dl 1.7 for (int i = 0; i < SIZE + 2; ++i) {
832 jsr166 1.13 for (int j = 0; j < SIZE; j++)
833 dl 1.11 assertTrue(q.offer(new Integer(j)));
834 dl 1.7 ArrayList l = new ArrayList();
835     q.drainTo(l, i);
836     int k = (i < SIZE)? i : SIZE;
837 dl 1.11 assertEquals(l.size(), k);
838 dl 1.7 assertEquals(q.size(), SIZE-k);
839 jsr166 1.12 for (int j = 0; j < k; ++j)
840 dl 1.11 assertEquals(l.get(j), new Integer(j));
841     while (q.poll() != null) ;
842 dl 1.7 }
843     }
844    
845 dl 1.1 }