ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.6
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.5: +53 -56 lines
Log Message:
improve tck javadocs; rename and add a few tests

File Contents

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