ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
Revision: 1.5
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.4: +200 -67 lines
Log Message:
Documentation scaffolding

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