ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 00:31:57 2003 UTC vs.
Revision 1.6 by dl, Thu Sep 25 11:02:41 2003 UTC

# Line 14 | Line 14 | public class PriorityBlockingQueueTest e
14      public static void main(String[] args) {
15          junit.textui.TestRunner.run (suite());  
16      }
17
17      public static Test suite() {
18          return new TestSuite(PriorityBlockingQueueTest.class);
19      }
# Line 32 | Line 31 | public class PriorityBlockingQueueTest e
31          }
32      }
33  
35
34      /**
35       * Create a queue of given size containing consecutive
36       * Integers 0 ... n.
# Line 50 | Line 48 | public class PriorityBlockingQueueTest e
48          return q;
49      }
50  
51 <    public void testConstructor1(){
51 >    /**
52 >     * A new queue has unbounded capacity
53 >     */
54 >    public void testConstructor1() {
55          assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
56      }
57  
58 <    public void testConstructor2(){
58 >    /**
59 >     * Constructor throws IAE if  capacity argument nonpositive
60 >     */
61 >    public void testConstructor2() {
62          try {
63              PriorityBlockingQueue q = new PriorityBlockingQueue(0);
64 <            fail("Cannot make zero-sized");
64 >            shouldThrow();
65          }
66          catch (IllegalArgumentException success) {}
67      }
68  
69 <    public void testConstructor3(){
70 <
69 >    /**
70 >     * Initializing from null Collection throws NPE
71 >     */
72 >    public void testConstructor3() {
73          try {
74              PriorityBlockingQueue q = new PriorityBlockingQueue(null);
75 <            fail("Cannot make from null collection");
75 >            shouldThrow();
76          }
77          catch (NullPointerException success) {}
78      }
79  
80 <    public void testConstructor4(){
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 <            fail("Cannot make with null elements");
87 >            shouldThrow();
88          }
89          catch (NullPointerException success) {}
90      }
91  
92 <    public void testConstructor5(){
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 <            fail("Cannot make with null elements");
101 >            shouldThrow();
102          }
103          catch (NullPointerException success) {}
104      }
105  
106 <    public void testConstructor6(){
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)
# Line 103 | Line 118 | public class PriorityBlockingQueueTest e
118          finally {}
119      }
120  
121 <    public void testConstructor7(){
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);
# Line 118 | Line 136 | public class PriorityBlockingQueueTest e
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(new Integer(1));
146 >        q.add(one);
147          assertFalse(q.isEmpty());
148 <        q.add(new Integer(2));
148 >        q.add(two);
149          q.remove();
150          q.remove();
151          assertTrue(q.isEmpty());
152      }
153  
154 <    public void testRemainingCapacity(){
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());
# Line 144 | Line 169 | public class PriorityBlockingQueueTest e
169          }
170      }
171  
172 <    public void testOfferNull(){
172 >    /**
173 >     * offer(null) throws NPE
174 >     */
175 >    public void testOfferNull() {
176          try {
177              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
178              q.offer(null);
179 <            fail("should throw NPE");
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(new Integer(0)));
189 <        assertTrue(q.offer(new Integer(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 <            fail("should throw CCE");
201 >            shouldThrow();
202          }
203          catch(ClassCastException success) {}
204      }
205  
206 <    public void testAdd(){
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());
# Line 177 | Line 214 | public class PriorityBlockingQueueTest e
214          }
215      }
216  
217 <    public void testAddAll1(){
217 >    /**
218 >     * addAll(null) throws NPE
219 >     */
220 >    public void testAddAll1() {
221          try {
222              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
223              q.addAll(null);
224 <            fail("Cannot add null collection");
224 >            shouldThrow();
225          }
226          catch (NullPointerException success) {}
227      }
228 <    public void testAddAll2(){
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 <            fail("Cannot add null elements");
236 >            shouldThrow();
237          }
238          catch (NullPointerException success) {}
239      }
240 <    public void testAddAll3(){
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 <            fail("Cannot add null elements");
251 >            shouldThrow();
252          }
253          catch (NullPointerException success) {}
254      }
255  
256 <    public void testAddAll5(){
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];
# Line 221 | Line 271 | public class PriorityBlockingQueueTest e
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 <            fail("put should throw NPE");
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);
# Line 245 | Line 301 | public class PriorityBlockingQueueTest e
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(){
310 >                public void run() {
311                      int added = 0;
312                      try {
313                          q.put(new Integer(0));
# Line 271 | Line 330 | public class PriorityBlockingQueueTest e
330              t.interrupt();
331              t.join();
332          } catch (Exception e){
333 <            fail("Unexpected exception");
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(){
343 >                public void run() {
344                      try {
345                          q.put(new Integer(0));
346                          q.put(new Integer(0));
# Line 294 | Line 356 | public class PriorityBlockingQueueTest e
356              t.interrupt();
357              t.join();
358          } catch (Exception e){
359 <            fail("Unexpected exception");
359 >            unexpectedException();
360          }
361      }
362  
363 <    public void testTake(){
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 <            fail("Unexpected exception");
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(){
383 >                public void run() {
384                      try {
385                          q.take();
386 <                        threadFail("Should block");
386 >                        threadShouldThrow();
387                      } catch (InterruptedException success){ }                
388                  }
389              });
# Line 325 | Line 393 | public class PriorityBlockingQueueTest e
393              t.interrupt();
394              t.join();
395          } catch (Exception e){
396 <            fail("Unexpected exception");
396 >            unexpectedException();
397          }
398      }
399  
400 <    public void testBlockingTake(){
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 {
# Line 338 | Line 409 | public class PriorityBlockingQueueTest e
409                              threadAssertEquals(i, ((Integer)q.take()).intValue());
410                          }
411                          q.take();
412 <                        threadFail("take should block");
412 >                        threadShouldThrow();
413                      } catch (InterruptedException success){
414                      }  
415                  }});
# Line 349 | Line 420 | public class PriorityBlockingQueueTest e
420             t.join();
421          }
422          catch (InterruptedException ie) {
423 <            fail("Unexpected exception");
423 >            unexpectedException();
424          }
425      }
426  
427  
428 <    public void testPoll(){
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());
# Line 362 | Line 436 | public class PriorityBlockingQueueTest e
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);
# Line 370 | Line 447 | public class PriorityBlockingQueueTest e
447              }
448              assertNull(q.poll(0, TimeUnit.MILLISECONDS));
449          } catch (InterruptedException e){
450 <            fail("Unexpected exception");
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);
# Line 382 | Line 462 | public class PriorityBlockingQueueTest e
462              }
463              assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
464          } catch (InterruptedException e){
465 <            fail("Unexpected exception");
465 >            unexpectedException();
466          }  
467      }
468  
469 <    public void testInterruptedTimedPoll(){
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 {
# Line 405 | Line 489 | public class PriorityBlockingQueueTest e
489             t.join();
490          }
491          catch (InterruptedException ie) {
492 <            fail("Unexpected exception");
492 >            unexpectedException();
493          }
494      }
495  
496 <    public void testTimedPollWithOffer(){
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(){
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 <                        threadFail("Should block");
508 >                        threadShouldThrow();
509                      } catch (InterruptedException success) { }                
510                  }
511              });
# Line 428 | Line 516 | public class PriorityBlockingQueueTest e
516              t.interrupt();
517              t.join();
518          } catch (Exception e){
519 <            fail("Unexpected exception");
519 >            unexpectedException();
520          }
521      }  
522  
523  
524 <    public void testPeek(){
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());
# Line 444 | Line 535 | public class PriorityBlockingQueueTest e
535          assertNull(q.peek());
536      }
537  
538 <    public void testElement(){
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());
# Line 452 | Line 546 | public class PriorityBlockingQueueTest e
546          }
547          try {
548              q.element();
549 <            fail("no such element");
549 >            shouldThrow();
550          }
551          catch (NoSuchElementException success) {}
552      }
553  
554 <    public void testRemove(){
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 <            fail("remove should throw");
564 >            shouldThrow();
565          } catch (NoSuchElementException success){
566          }  
567      }
568  
569 <    public void testRemoveElement(){
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)));
# Line 481 | Line 581 | public class PriorityBlockingQueueTest e
581          assertTrue(q.isEmpty());
582      }
583          
584 <    public void testContains(){
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)));
# Line 490 | Line 593 | public class PriorityBlockingQueueTest e
593          }
594      }
595  
596 <    public void testClear(){
596 >    /**
597 >     * clear removes all elements
598 >     */
599 >    public void testClear() {
600          PriorityBlockingQueue q = populatedQueue(SIZE);
601          q.clear();
602          assertTrue(q.isEmpty());
# Line 502 | Line 608 | public class PriorityBlockingQueueTest e
608          assertTrue(q.isEmpty());
609      }
610  
611 <    public void testContainsAll(){
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) {
# Line 513 | Line 622 | public class PriorityBlockingQueueTest e
622          assertTrue(p.containsAll(q));
623      }
624  
625 <    public void testRetainAll(){
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) {
# Line 529 | Line 641 | public class PriorityBlockingQueueTest e
641          }
642      }
643  
644 <    public void testRemoveAll(){
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);
# Line 542 | Line 657 | public class PriorityBlockingQueueTest e
657          }
658      }
659  
660 <    public void testToArray(){
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);
# Line 550 | Line 668 | public class PriorityBlockingQueueTest e
668          for(int i = 0; i < o.length; i++)
669              assertEquals(o[i], q.take());
670          } catch (InterruptedException e){
671 <            fail("Unexpected exception");
671 >            unexpectedException();
672          }    
673      }
674  
675 <    public void testToArray2(){
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);
# Line 563 | Line 684 | public class PriorityBlockingQueueTest e
684              for(int i = 0; i < ints.length; i++)
685                  assertEquals(ints[i], q.take());
686          } catch (InterruptedException e){
687 <            fail("Unexpected exception");
687 >            unexpectedException();
688          }    
689      }
690      
691 <    public void testIterator(){
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();
# Line 578 | Line 702 | public class PriorityBlockingQueueTest e
702          assertEquals(i, SIZE);
703      }
704  
705 +    /**
706 +     * iterator.remove removes current element
707 +     */
708      public void testIteratorRemove () {
582
709          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
584
710          q.add(new Integer(2));
711          q.add(new Integer(1));
712          q.add(new Integer(3));
# Line 597 | Line 722 | public class PriorityBlockingQueueTest e
722      }
723  
724  
725 <    public void testToString(){
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) {
# Line 605 | Line 733 | public class PriorityBlockingQueueTest e
733          }
734      }        
735  
736 +    /**
737 +     * offer transfers elements across Executor tasks
738 +     */
739      public void testPollInExecutor() {
609
740          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
611
741          ExecutorService executor = Executors.newFixedThreadPool(2);
613
742          executor.execute(new Runnable() {
743              public void run() {
744                  threadAssertNull(q.poll());
# Line 619 | Line 747 | public class PriorityBlockingQueueTest e
747                      threadAssertTrue(q.isEmpty());
748                  }
749                  catch (InterruptedException e) {
750 <                    threadFail("should not be interrupted");
750 >                    threadUnexpectedException();
751                  }
752              }
753          });
# Line 631 | Line 759 | public class PriorityBlockingQueueTest e
759                      q.put(new Integer(1));
760                  }
761                  catch (InterruptedException e) {
762 <                    threadFail("should not be interrupted");
762 >                    threadUnexpectedException();
763                  }
764              }
765          });
766          
767          joinPool(executor);
640
768      }
769  
770 +    /**
771 +     * A deserialized serialized queue has same elements
772 +     */
773      public void testSerialization() {
774          PriorityBlockingQueue q = populatedQueue(SIZE);
775          try {
# Line 655 | Line 785 | public class PriorityBlockingQueueTest e
785              while (!q.isEmpty())
786                  assertEquals(q.remove(), r.remove());
787          } catch(Exception e){
788 <            fail("unexpected exception");
788 >            unexpectedException();
789          }
790      }
791  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines