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

# Content
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 * Offer of comparable element succeeds
185 */
186 public void testOffer() {
187 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
188 assertTrue(q.offer(zero));
189 assertTrue(q.offer(one));
190 }
191
192 /**
193 * Offer of non-Comparable throws CCE
194 */
195 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 shouldThrow();
202 }
203 catch(ClassCastException success) {}
204 }
205
206 /**
207 * add of comparable succeeds
208 */
209 public void testAdd() {
210 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
211 for (int i = 0; i < SIZE; ++i) {
212 assertEquals(i, q.size());
213 assertTrue(q.add(new Integer(i)));
214 }
215 }
216
217 /**
218 * addAll(null) throws NPE
219 */
220 public void testAddAll1() {
221 try {
222 PriorityBlockingQueue q = new PriorityBlockingQueue(1);
223 q.addAll(null);
224 shouldThrow();
225 }
226 catch (NullPointerException success) {}
227 }
228 /**
229 * addAll of a collection with null elements throws NPE
230 */
231 public void testAddAll2() {
232 try {
233 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
234 Integer[] ints = new Integer[SIZE];
235 q.addAll(Arrays.asList(ints));
236 shouldThrow();
237 }
238 catch (NullPointerException success) {}
239 }
240 /**
241 * addAll of a collection with any null elements throws NPE after
242 * possibly adding some elements
243 */
244 public void testAddAll3() {
245 try {
246 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
247 Integer[] ints = new Integer[SIZE];
248 for (int i = 0; i < SIZE-1; ++i)
249 ints[i] = new Integer(i);
250 q.addAll(Arrays.asList(ints));
251 shouldThrow();
252 }
253 catch (NullPointerException success) {}
254 }
255
256 /**
257 * Queue contains all elements of successful addAll
258 */
259 public void testAddAll5() {
260 try {
261 Integer[] empty = new Integer[0];
262 Integer[] ints = new Integer[SIZE];
263 for (int i = SIZE-1; i >= 0; --i)
264 ints[i] = new Integer(i);
265 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
266 assertFalse(q.addAll(Arrays.asList(empty)));
267 assertTrue(q.addAll(Arrays.asList(ints)));
268 for (int i = 0; i < SIZE; ++i)
269 assertEquals(ints[i], q.poll());
270 }
271 finally {}
272 }
273
274 /**
275 * put(null) throws NPE
276 */
277 public void testPutNull() {
278 try {
279 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
280 q.put(null);
281 shouldThrow();
282 }
283 catch (NullPointerException success){
284 }
285 }
286
287 /**
288 * all elements successfully put are contained
289 */
290 public void testPut() {
291 try {
292 PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
293 for (int i = 0; i < SIZE; ++i) {
294 Integer I = new Integer(i);
295 q.put(I);
296 assertTrue(q.contains(I));
297 }
298 assertEquals(SIZE, q.size());
299 }
300 finally {
301 }
302 }
303
304 /**
305 * put doesn't block waiting for take
306 */
307 public void testPutWithTake() {
308 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
309 Thread t = new Thread(new Runnable() {
310 public void run() {
311 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 threadAssertTrue(added == 4);
322 } 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 unexpectedException();
334 }
335 }
336
337 /**
338 * timed offer does not time out
339 */
340 public void testTimedOffer() {
341 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
342 Thread t = new Thread(new Runnable() {
343 public void run() {
344 try {
345 q.put(new Integer(0));
346 q.put(new Integer(0));
347 threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
348 threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
349 } finally { }
350 }
351 });
352
353 try {
354 t.start();
355 Thread.sleep(SMALL_DELAY_MS);
356 t.interrupt();
357 t.join();
358 } catch (Exception e){
359 unexpectedException();
360 }
361 }
362
363 /**
364 * take retrieves elements in priority order
365 */
366 public void testTake() {
367 try {
368 PriorityBlockingQueue q = populatedQueue(SIZE);
369 for (int i = 0; i < SIZE; ++i) {
370 assertEquals(i, ((Integer)q.take()).intValue());
371 }
372 } catch (InterruptedException e){
373 unexpectedException();
374 }
375 }
376
377 /**
378 * take blocks interruptibly when empty
379 */
380 public void testTakeFromEmpty() {
381 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
382 Thread t = new Thread(new Runnable() {
383 public void run() {
384 try {
385 q.take();
386 threadShouldThrow();
387 } 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 unexpectedException();
397 }
398 }
399
400 /**
401 * Take removes existing elements until empty, then blocks interruptibly
402 */
403 public void testBlockingTake() {
404 Thread t = new Thread(new Runnable() {
405 public void run() {
406 try {
407 PriorityBlockingQueue q = populatedQueue(SIZE);
408 for (int i = 0; i < SIZE; ++i) {
409 threadAssertEquals(i, ((Integer)q.take()).intValue());
410 }
411 q.take();
412 threadShouldThrow();
413 } 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 unexpectedException();
424 }
425 }
426
427
428 /**
429 * poll succeeds unless empty
430 */
431 public void testPoll() {
432 PriorityBlockingQueue q = populatedQueue(SIZE);
433 for (int i = 0; i < SIZE; ++i) {
434 assertEquals(i, ((Integer)q.poll()).intValue());
435 }
436 assertNull(q.poll());
437 }
438
439 /**
440 * timed pool with zero timeout succeeds when non-empty, else times out
441 */
442 public void testTimedPoll0() {
443 try {
444 PriorityBlockingQueue q = populatedQueue(SIZE);
445 for (int i = 0; i < SIZE; ++i) {
446 assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
447 }
448 assertNull(q.poll(0, TimeUnit.MILLISECONDS));
449 } catch (InterruptedException e){
450 unexpectedException();
451 }
452 }
453
454 /**
455 * timed pool with nonzero timeout succeeds when non-empty, else times out
456 */
457 public void testTimedPoll() {
458 try {
459 PriorityBlockingQueue q = populatedQueue(SIZE);
460 for (int i = 0; i < SIZE; ++i) {
461 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 unexpectedException();
466 }
467 }
468
469 /**
470 * Interrupted timed poll throws InterruptedException instead of
471 * returning timeout status
472 */
473 public void testInterruptedTimedPoll() {
474 Thread t = new Thread(new Runnable() {
475 public void run() {
476 try {
477 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 }
481 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
482 } 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 unexpectedException();
493 }
494 }
495
496 /**
497 * timed poll before a delayed offer fails; after offer succeeds;
498 * on interruption throws
499 */
500 public void testTimedPollWithOffer() {
501 final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
502 Thread t = new Thread(new Runnable() {
503 public void run() {
504 try {
505 threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
506 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
507 q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
508 threadShouldThrow();
509 } catch (InterruptedException success) { }
510 }
511 });
512 try {
513 t.start();
514 Thread.sleep(SMALL_DELAY_MS);
515 assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
516 t.interrupt();
517 t.join();
518 } catch (Exception e){
519 unexpectedException();
520 }
521 }
522
523
524 /**
525 * peek returns next element, or null if empty
526 */
527 public void testPeek() {
528 PriorityBlockingQueue q = populatedQueue(SIZE);
529 for (int i = 0; i < SIZE; ++i) {
530 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 /**
539 * element returns next element, or throws NSEE if empty
540 */
541 public void testElement() {
542 PriorityBlockingQueue q = populatedQueue(SIZE);
543 for (int i = 0; i < SIZE; ++i) {
544 assertEquals(i, ((Integer)q.element()).intValue());
545 q.poll();
546 }
547 try {
548 q.element();
549 shouldThrow();
550 }
551 catch (NoSuchElementException success) {}
552 }
553
554 /**
555 * remove removes next element, or throws NSEE if empty
556 */
557 public void testRemove() {
558 PriorityBlockingQueue q = populatedQueue(SIZE);
559 for (int i = 0; i < SIZE; ++i) {
560 assertEquals(i, ((Integer)q.remove()).intValue());
561 }
562 try {
563 q.remove();
564 shouldThrow();
565 } catch (NoSuchElementException success){
566 }
567 }
568
569 /**
570 * remove(x) removes x and returns true if present
571 */
572 public void testRemoveElement() {
573 PriorityBlockingQueue q = populatedQueue(SIZE);
574 for (int i = 1; i < SIZE; i+=2) {
575 assertTrue(q.remove(new Integer(i)));
576 }
577 for (int i = 0; i < SIZE; i+=2) {
578 assertTrue(q.remove(new Integer(i)));
579 assertFalse(q.remove(new Integer(i+1)));
580 }
581 assertTrue(q.isEmpty());
582 }
583
584 /**
585 * contains(x) reports true when elements added but not yet removed
586 */
587 public void testContains() {
588 PriorityBlockingQueue q = populatedQueue(SIZE);
589 for (int i = 0; i < SIZE; ++i) {
590 assertTrue(q.contains(new Integer(i)));
591 q.poll();
592 assertFalse(q.contains(new Integer(i)));
593 }
594 }
595
596 /**
597 * clear removes all elements
598 */
599 public void testClear() {
600 PriorityBlockingQueue q = populatedQueue(SIZE);
601 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 /**
612 * containsAll(c) is true when c contains a subset of elements
613 */
614 public void testContainsAll() {
615 PriorityBlockingQueue q = populatedQueue(SIZE);
616 PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE);
617 for (int i = 0; i < SIZE; ++i) {
618 assertTrue(q.containsAll(p));
619 assertFalse(p.containsAll(q));
620 p.add(new Integer(i));
621 }
622 assertTrue(p.containsAll(q));
623 }
624
625 /**
626 * retainAll(c) retains only those elements of c and reports true if changed
627 */
628 public void testRetainAll() {
629 PriorityBlockingQueue q = populatedQueue(SIZE);
630 PriorityBlockingQueue p = populatedQueue(SIZE);
631 for (int i = 0; i < SIZE; ++i) {
632 boolean changed = q.retainAll(p);
633 if (i == 0)
634 assertFalse(changed);
635 else
636 assertTrue(changed);
637
638 assertTrue(q.containsAll(p));
639 assertEquals(SIZE-i, q.size());
640 p.remove();
641 }
642 }
643
644 /**
645 * removeAll(c) removes only those elements of c and reports true if changed
646 */
647 public void testRemoveAll() {
648 for (int i = 1; i < SIZE; ++i) {
649 PriorityBlockingQueue q = populatedQueue(SIZE);
650 PriorityBlockingQueue p = populatedQueue(i);
651 assertTrue(q.removeAll(p));
652 assertEquals(SIZE-i, q.size());
653 for (int j = 0; j < i; ++j) {
654 Integer I = (Integer)(p.remove());
655 assertFalse(q.contains(I));
656 }
657 }
658 }
659
660 /**
661 * toArray contains all elements
662 */
663 public void testToArray() {
664 PriorityBlockingQueue q = populatedQueue(SIZE);
665 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 unexpectedException();
672 }
673 }
674
675 /**
676 * toArray(a) contains all elements
677 */
678 public void testToArray2() {
679 PriorityBlockingQueue q = populatedQueue(SIZE);
680 Integer[] ints = new Integer[SIZE];
681 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 unexpectedException();
688 }
689 }
690
691 /**
692 * iterator iterates through all elements
693 */
694 public void testIterator() {
695 PriorityBlockingQueue q = populatedQueue(SIZE);
696 int i = 0;
697 Iterator it = q.iterator();
698 while(it.hasNext()) {
699 assertTrue(q.contains(it.next()));
700 ++i;
701 }
702 assertEquals(i, SIZE);
703 }
704
705 /**
706 * iterator.remove removes current element
707 */
708 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 /**
726 * toString contains toStrings of elements
727 */
728 public void testToString() {
729 PriorityBlockingQueue q = populatedQueue(SIZE);
730 String s = q.toString();
731 for (int i = 0; i < SIZE; ++i) {
732 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
733 }
734 }
735
736 /**
737 * offer transfers elements across Executor tasks
738 */
739 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 threadAssertNull(q.poll());
745 try {
746 threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
747 threadAssertTrue(q.isEmpty());
748 }
749 catch (InterruptedException e) {
750 threadUnexpectedException();
751 }
752 }
753 });
754
755 executor.execute(new Runnable() {
756 public void run() {
757 try {
758 Thread.sleep(SMALL_DELAY_MS);
759 q.put(new Integer(1));
760 }
761 catch (InterruptedException e) {
762 threadUnexpectedException();
763 }
764 }
765 });
766
767 joinPool(executor);
768 }
769
770 /**
771 * A deserialized serialized queue has same elements
772 */
773 public void testSerialization() {
774 PriorityBlockingQueue q = populatedQueue(SIZE);
775 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 unexpectedException();
789 }
790 }
791
792 }