ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.35
Committed: Wed Nov 3 07:54:52 2010 UTC (13 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.34: +2 -2 lines
Log Message:
fix javadoc of testToArray1_BadArg

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