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 |
import java.io.*; |
12 |
|
13 |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
14 |
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 |
private static final int NOCAP = Integer.MAX_VALUE; |
22 |
|
23 |
/** Sample Comparator */ |
24 |
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 |
private PriorityBlockingQueue populatedQueue(int n) { |
39 |
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 |
/** |
52 |
* A new queue has unbounded capacity |
53 |
*/ |
54 |
public void testConstructor1() { |
55 |
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
56 |
} |
57 |
|
58 |
/** |
59 |
* Constructor throws IAE if capacity argument nonpositive |
60 |
*/ |
61 |
public void testConstructor2() { |
62 |
try { |
63 |
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
64 |
shouldThrow(); |
65 |
} |
66 |
catch (IllegalArgumentException success) {} |
67 |
} |
68 |
|
69 |
/** |
70 |
* Initializing from null Collection throws NPE |
71 |
*/ |
72 |
public void testConstructor3() { |
73 |
try { |
74 |
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
75 |
shouldThrow(); |
76 |
} |
77 |
catch (NullPointerException success) {} |
78 |
} |
79 |
|
80 |
/** |
81 |
* Initializing from Collection of null elements throws NPE |
82 |
*/ |
83 |
public void testConstructor4() { |
84 |
try { |
85 |
Integer[] ints = new Integer[SIZE]; |
86 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
87 |
shouldThrow(); |
88 |
} |
89 |
catch (NullPointerException success) {} |
90 |
} |
91 |
|
92 |
/** |
93 |
* Initializing from Collection with some null elements throws NPE |
94 |
*/ |
95 |
public void testConstructor5() { |
96 |
try { |
97 |
Integer[] ints = new Integer[SIZE]; |
98 |
for (int i = 0; i < SIZE-1; ++i) |
99 |
ints[i] = new Integer(i); |
100 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
101 |
shouldThrow(); |
102 |
} |
103 |
catch (NullPointerException success) {} |
104 |
} |
105 |
|
106 |
/** |
107 |
* Queue contains all elements of collection used to initialize |
108 |
*/ |
109 |
public void testConstructor6() { |
110 |
try { |
111 |
Integer[] ints = new Integer[SIZE]; |
112 |
for (int i = 0; i < SIZE; ++i) |
113 |
ints[i] = new Integer(i); |
114 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
115 |
for (int i = 0; i < SIZE; ++i) |
116 |
assertEquals(ints[i], q.poll()); |
117 |
} |
118 |
finally {} |
119 |
} |
120 |
|
121 |
/** |
122 |
* The comparator used in constructor is used |
123 |
*/ |
124 |
public void testConstructor7() { |
125 |
try { |
126 |
MyReverseComparator cmp = new MyReverseComparator(); |
127 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
128 |
assertEquals(cmp, q.comparator()); |
129 |
Integer[] ints = new Integer[SIZE]; |
130 |
for (int i = 0; i < SIZE; ++i) |
131 |
ints[i] = new Integer(i); |
132 |
q.addAll(Arrays.asList(ints)); |
133 |
for (int i = SIZE-1; i >= 0; --i) |
134 |
assertEquals(ints[i], q.poll()); |
135 |
} |
136 |
finally {} |
137 |
} |
138 |
|
139 |
/** |
140 |
* isEmpty is true before add, false after |
141 |
*/ |
142 |
public void testEmpty() { |
143 |
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
144 |
assertTrue(q.isEmpty()); |
145 |
assertEquals(NOCAP, q.remainingCapacity()); |
146 |
q.add(one); |
147 |
assertFalse(q.isEmpty()); |
148 |
q.add(two); |
149 |
q.remove(); |
150 |
q.remove(); |
151 |
assertTrue(q.isEmpty()); |
152 |
} |
153 |
|
154 |
/** |
155 |
* remainingCapacity does not change when elementa added or removed, |
156 |
* but size does |
157 |
*/ |
158 |
public void testRemainingCapacity() { |
159 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
160 |
for (int i = 0; i < SIZE; ++i) { |
161 |
assertEquals(NOCAP, q.remainingCapacity()); |
162 |
assertEquals(SIZE-i, q.size()); |
163 |
q.remove(); |
164 |
} |
165 |
for (int i = 0; i < SIZE; ++i) { |
166 |
assertEquals(NOCAP, q.remainingCapacity()); |
167 |
assertEquals(i, q.size()); |
168 |
q.add(new Integer(i)); |
169 |
} |
170 |
} |
171 |
|
172 |
/** |
173 |
* offer(null) throws NPE |
174 |
*/ |
175 |
public void testOfferNull() { |
176 |
try { |
177 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
178 |
q.offer(null); |
179 |
shouldThrow(); |
180 |
} catch (NullPointerException success) { } |
181 |
} |
182 |
|
183 |
/** |
184 |
* 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 |
* Offer of comparable element succeeds |
196 |
*/ |
197 |
public void testOffer() { |
198 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
199 |
assertTrue(q.offer(zero)); |
200 |
assertTrue(q.offer(one)); |
201 |
} |
202 |
|
203 |
/** |
204 |
* Offer of non-Comparable throws CCE |
205 |
*/ |
206 |
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 |
shouldThrow(); |
213 |
} |
214 |
catch(ClassCastException success) {} |
215 |
} |
216 |
|
217 |
/** |
218 |
* add of comparable succeeds |
219 |
*/ |
220 |
public void testAdd() { |
221 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
222 |
for (int i = 0; i < SIZE; ++i) { |
223 |
assertEquals(i, q.size()); |
224 |
assertTrue(q.add(new Integer(i))); |
225 |
} |
226 |
} |
227 |
|
228 |
/** |
229 |
* addAll(null) throws NPE |
230 |
*/ |
231 |
public void testAddAll1() { |
232 |
try { |
233 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
234 |
q.addAll(null); |
235 |
shouldThrow(); |
236 |
} |
237 |
catch (NullPointerException success) {} |
238 |
} |
239 |
|
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 |
/** |
253 |
* addAll of a collection with null elements throws NPE |
254 |
*/ |
255 |
public void testAddAll2() { |
256 |
try { |
257 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
258 |
Integer[] ints = new Integer[SIZE]; |
259 |
q.addAll(Arrays.asList(ints)); |
260 |
shouldThrow(); |
261 |
} |
262 |
catch (NullPointerException success) {} |
263 |
} |
264 |
/** |
265 |
* addAll of a collection with any null elements throws NPE after |
266 |
* possibly adding some elements |
267 |
*/ |
268 |
public void testAddAll3() { |
269 |
try { |
270 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
271 |
Integer[] ints = new Integer[SIZE]; |
272 |
for (int i = 0; i < SIZE-1; ++i) |
273 |
ints[i] = new Integer(i); |
274 |
q.addAll(Arrays.asList(ints)); |
275 |
shouldThrow(); |
276 |
} |
277 |
catch (NullPointerException success) {} |
278 |
} |
279 |
|
280 |
/** |
281 |
* Queue contains all elements of successful addAll |
282 |
*/ |
283 |
public void testAddAll5() { |
284 |
try { |
285 |
Integer[] empty = new Integer[0]; |
286 |
Integer[] ints = new Integer[SIZE]; |
287 |
for (int i = SIZE-1; i >= 0; --i) |
288 |
ints[i] = new Integer(i); |
289 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
290 |
assertFalse(q.addAll(Arrays.asList(empty))); |
291 |
assertTrue(q.addAll(Arrays.asList(ints))); |
292 |
for (int i = 0; i < SIZE; ++i) |
293 |
assertEquals(ints[i], q.poll()); |
294 |
} |
295 |
finally {} |
296 |
} |
297 |
|
298 |
/** |
299 |
* put(null) throws NPE |
300 |
*/ |
301 |
public void testPutNull() { |
302 |
try { |
303 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
304 |
q.put(null); |
305 |
shouldThrow(); |
306 |
} |
307 |
catch (NullPointerException success){ |
308 |
} |
309 |
} |
310 |
|
311 |
/** |
312 |
* all elements successfully put are contained |
313 |
*/ |
314 |
public void testPut() { |
315 |
try { |
316 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
317 |
for (int i = 0; i < SIZE; ++i) { |
318 |
Integer I = new Integer(i); |
319 |
q.put(I); |
320 |
assertTrue(q.contains(I)); |
321 |
} |
322 |
assertEquals(SIZE, q.size()); |
323 |
} |
324 |
finally { |
325 |
} |
326 |
} |
327 |
|
328 |
/** |
329 |
* put doesn't block waiting for take |
330 |
*/ |
331 |
public void testPutWithTake() { |
332 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
333 |
Thread t = new Thread(new Runnable() { |
334 |
public void run() { |
335 |
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 |
threadAssertTrue(added == 4); |
346 |
} 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 |
unexpectedException(); |
358 |
} |
359 |
} |
360 |
|
361 |
/** |
362 |
* timed offer does not time out |
363 |
*/ |
364 |
public void testTimedOffer() { |
365 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
366 |
Thread t = new Thread(new Runnable() { |
367 |
public void run() { |
368 |
try { |
369 |
q.put(new Integer(0)); |
370 |
q.put(new Integer(0)); |
371 |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
372 |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
373 |
} finally { } |
374 |
} |
375 |
}); |
376 |
|
377 |
try { |
378 |
t.start(); |
379 |
Thread.sleep(SMALL_DELAY_MS); |
380 |
t.interrupt(); |
381 |
t.join(); |
382 |
} catch (Exception e){ |
383 |
unexpectedException(); |
384 |
} |
385 |
} |
386 |
|
387 |
/** |
388 |
* take retrieves elements in priority order |
389 |
*/ |
390 |
public void testTake() { |
391 |
try { |
392 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
393 |
for (int i = 0; i < SIZE; ++i) { |
394 |
assertEquals(i, ((Integer)q.take()).intValue()); |
395 |
} |
396 |
} catch (InterruptedException e){ |
397 |
unexpectedException(); |
398 |
} |
399 |
} |
400 |
|
401 |
/** |
402 |
* take blocks interruptibly when empty |
403 |
*/ |
404 |
public void testTakeFromEmpty() { |
405 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
406 |
Thread t = new Thread(new Runnable() { |
407 |
public void run() { |
408 |
try { |
409 |
q.take(); |
410 |
threadShouldThrow(); |
411 |
} 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 |
unexpectedException(); |
421 |
} |
422 |
} |
423 |
|
424 |
/** |
425 |
* Take removes existing elements until empty, then blocks interruptibly |
426 |
*/ |
427 |
public void testBlockingTake() { |
428 |
Thread t = new Thread(new Runnable() { |
429 |
public void run() { |
430 |
try { |
431 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
432 |
for (int i = 0; i < SIZE; ++i) { |
433 |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
434 |
} |
435 |
q.take(); |
436 |
threadShouldThrow(); |
437 |
} 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 |
unexpectedException(); |
448 |
} |
449 |
} |
450 |
|
451 |
|
452 |
/** |
453 |
* poll succeeds unless empty |
454 |
*/ |
455 |
public void testPoll() { |
456 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
457 |
for (int i = 0; i < SIZE; ++i) { |
458 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
459 |
} |
460 |
assertNull(q.poll()); |
461 |
} |
462 |
|
463 |
/** |
464 |
* timed pool with zero timeout succeeds when non-empty, else times out |
465 |
*/ |
466 |
public void testTimedPoll0() { |
467 |
try { |
468 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
469 |
for (int i = 0; i < SIZE; ++i) { |
470 |
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
471 |
} |
472 |
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
473 |
} catch (InterruptedException e){ |
474 |
unexpectedException(); |
475 |
} |
476 |
} |
477 |
|
478 |
/** |
479 |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
480 |
*/ |
481 |
public void testTimedPoll() { |
482 |
try { |
483 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
484 |
for (int i = 0; i < SIZE; ++i) { |
485 |
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 |
unexpectedException(); |
490 |
} |
491 |
} |
492 |
|
493 |
/** |
494 |
* Interrupted timed poll throws InterruptedException instead of |
495 |
* returning timeout status |
496 |
*/ |
497 |
public void testInterruptedTimedPoll() { |
498 |
Thread t = new Thread(new Runnable() { |
499 |
public void run() { |
500 |
try { |
501 |
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 |
} |
505 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
506 |
} 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 |
unexpectedException(); |
517 |
} |
518 |
} |
519 |
|
520 |
/** |
521 |
* timed poll before a delayed offer fails; after offer succeeds; |
522 |
* on interruption throws |
523 |
*/ |
524 |
public void testTimedPollWithOffer() { |
525 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
526 |
Thread t = new Thread(new Runnable() { |
527 |
public void run() { |
528 |
try { |
529 |
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
530 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
531 |
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
532 |
threadShouldThrow(); |
533 |
} catch (InterruptedException success) { } |
534 |
} |
535 |
}); |
536 |
try { |
537 |
t.start(); |
538 |
Thread.sleep(SMALL_DELAY_MS); |
539 |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
540 |
t.interrupt(); |
541 |
t.join(); |
542 |
} catch (Exception e){ |
543 |
unexpectedException(); |
544 |
} |
545 |
} |
546 |
|
547 |
|
548 |
/** |
549 |
* peek returns next element, or null if empty |
550 |
*/ |
551 |
public void testPeek() { |
552 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
553 |
for (int i = 0; i < SIZE; ++i) { |
554 |
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 |
/** |
563 |
* element returns next element, or throws NSEE if empty |
564 |
*/ |
565 |
public void testElement() { |
566 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
567 |
for (int i = 0; i < SIZE; ++i) { |
568 |
assertEquals(i, ((Integer)q.element()).intValue()); |
569 |
q.poll(); |
570 |
} |
571 |
try { |
572 |
q.element(); |
573 |
shouldThrow(); |
574 |
} |
575 |
catch (NoSuchElementException success) {} |
576 |
} |
577 |
|
578 |
/** |
579 |
* remove removes next element, or throws NSEE if empty |
580 |
*/ |
581 |
public void testRemove() { |
582 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
583 |
for (int i = 0; i < SIZE; ++i) { |
584 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
585 |
} |
586 |
try { |
587 |
q.remove(); |
588 |
shouldThrow(); |
589 |
} catch (NoSuchElementException success){ |
590 |
} |
591 |
} |
592 |
|
593 |
/** |
594 |
* remove(x) removes x and returns true if present |
595 |
*/ |
596 |
public void testRemoveElement() { |
597 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
598 |
for (int i = 1; i < SIZE; i+=2) { |
599 |
assertTrue(q.remove(new Integer(i))); |
600 |
} |
601 |
for (int i = 0; i < SIZE; i+=2) { |
602 |
assertTrue(q.remove(new Integer(i))); |
603 |
assertFalse(q.remove(new Integer(i+1))); |
604 |
} |
605 |
assertTrue(q.isEmpty()); |
606 |
} |
607 |
|
608 |
/** |
609 |
* contains(x) reports true when elements added but not yet removed |
610 |
*/ |
611 |
public void testContains() { |
612 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
613 |
for (int i = 0; i < SIZE; ++i) { |
614 |
assertTrue(q.contains(new Integer(i))); |
615 |
q.poll(); |
616 |
assertFalse(q.contains(new Integer(i))); |
617 |
} |
618 |
} |
619 |
|
620 |
/** |
621 |
* clear removes all elements |
622 |
*/ |
623 |
public void testClear() { |
624 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
625 |
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 |
/** |
636 |
* containsAll(c) is true when c contains a subset of elements |
637 |
*/ |
638 |
public void testContainsAll() { |
639 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
640 |
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
641 |
for (int i = 0; i < SIZE; ++i) { |
642 |
assertTrue(q.containsAll(p)); |
643 |
assertFalse(p.containsAll(q)); |
644 |
p.add(new Integer(i)); |
645 |
} |
646 |
assertTrue(p.containsAll(q)); |
647 |
} |
648 |
|
649 |
/** |
650 |
* retainAll(c) retains only those elements of c and reports true if changed |
651 |
*/ |
652 |
public void testRetainAll() { |
653 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
654 |
PriorityBlockingQueue p = populatedQueue(SIZE); |
655 |
for (int i = 0; i < SIZE; ++i) { |
656 |
boolean changed = q.retainAll(p); |
657 |
if (i == 0) |
658 |
assertFalse(changed); |
659 |
else |
660 |
assertTrue(changed); |
661 |
|
662 |
assertTrue(q.containsAll(p)); |
663 |
assertEquals(SIZE-i, q.size()); |
664 |
p.remove(); |
665 |
} |
666 |
} |
667 |
|
668 |
/** |
669 |
* removeAll(c) removes only those elements of c and reports true if changed |
670 |
*/ |
671 |
public void testRemoveAll() { |
672 |
for (int i = 1; i < SIZE; ++i) { |
673 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
674 |
PriorityBlockingQueue p = populatedQueue(i); |
675 |
assertTrue(q.removeAll(p)); |
676 |
assertEquals(SIZE-i, q.size()); |
677 |
for (int j = 0; j < i; ++j) { |
678 |
Integer I = (Integer)(p.remove()); |
679 |
assertFalse(q.contains(I)); |
680 |
} |
681 |
} |
682 |
} |
683 |
|
684 |
/** |
685 |
* toArray contains all elements |
686 |
*/ |
687 |
public void testToArray() { |
688 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
689 |
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 |
unexpectedException(); |
696 |
} |
697 |
} |
698 |
|
699 |
/** |
700 |
* toArray(a) contains all elements |
701 |
*/ |
702 |
public void testToArray2() { |
703 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
704 |
Integer[] ints = new Integer[SIZE]; |
705 |
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 |
unexpectedException(); |
712 |
} |
713 |
} |
714 |
|
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 |
|
737 |
/** |
738 |
* iterator iterates through all elements |
739 |
*/ |
740 |
public void testIterator() { |
741 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
742 |
int i = 0; |
743 |
Iterator it = q.iterator(); |
744 |
while(it.hasNext()) { |
745 |
assertTrue(q.contains(it.next())); |
746 |
++i; |
747 |
} |
748 |
assertEquals(i, SIZE); |
749 |
} |
750 |
|
751 |
/** |
752 |
* iterator.remove removes current element |
753 |
*/ |
754 |
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 |
/** |
772 |
* toString contains toStrings of elements |
773 |
*/ |
774 |
public void testToString() { |
775 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
776 |
String s = q.toString(); |
777 |
for (int i = 0; i < SIZE; ++i) { |
778 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
779 |
} |
780 |
} |
781 |
|
782 |
/** |
783 |
* offer transfers elements across Executor tasks |
784 |
*/ |
785 |
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 |
threadAssertNull(q.poll()); |
791 |
try { |
792 |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS)); |
793 |
threadAssertTrue(q.isEmpty()); |
794 |
} |
795 |
catch (InterruptedException e) { |
796 |
threadUnexpectedException(); |
797 |
} |
798 |
} |
799 |
}); |
800 |
|
801 |
executor.execute(new Runnable() { |
802 |
public void run() { |
803 |
try { |
804 |
Thread.sleep(SMALL_DELAY_MS); |
805 |
q.put(new Integer(1)); |
806 |
} |
807 |
catch (InterruptedException e) { |
808 |
threadUnexpectedException(); |
809 |
} |
810 |
} |
811 |
}); |
812 |
|
813 |
joinPool(executor); |
814 |
} |
815 |
|
816 |
/** |
817 |
* A deserialized serialized queue has same elements |
818 |
*/ |
819 |
public void testSerialization() { |
820 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
821 |
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 |
unexpectedException(); |
835 |
} |
836 |
} |
837 |
|
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 |
|
940 |
} |