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

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.6 by dl, Sun Oct 5 23:00:39 2003 UTC

# Line 35 | Line 35 | public class ArrayBlockingQueueTest exte
35      }
36  
37      /**
38 <     *
38 >     * A new queue has the indicated capacity
39       */
40      public void testConstructor1() {
41          assertEquals(SIZE, new ArrayBlockingQueue(SIZE).remainingCapacity());
42      }
43  
44      /**
45 <     *
45 >     * Constructor throws IAE if  capacity argument nonpositive
46       */
47      public void testConstructor2() {
48          try {
# Line 53 | Line 53 | public class ArrayBlockingQueueTest exte
53      }
54  
55      /**
56 <     *
56 >     * Initializing from null Collection throws NPE
57       */
58      public void testConstructor3() {
59
59          try {
60              ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
61              shouldThrow();
# Line 65 | Line 64 | public class ArrayBlockingQueueTest exte
64      }
65  
66      /**
67 <     *
67 >     * Initializing from Collection of null elements throws NPE
68       */
69      public void testConstructor4() {
70          try {
# Line 77 | Line 76 | public class ArrayBlockingQueueTest exte
76      }
77  
78      /**
79 <     *
79 >     * Initializing from Collection with some null elements throws NPE
80       */
81      public void testConstructor5() {
82          try {
# Line 91 | Line 90 | public class ArrayBlockingQueueTest exte
90      }
91  
92      /**
93 <     *
93 >     * Initializing from too large collection throws IAE
94       */
95      public void testConstructor6() {
96          try {
# Line 105 | Line 104 | public class ArrayBlockingQueueTest exte
104      }
105  
106      /**
107 <     *
107 >     * Queue contains all elements of collection used to initialize
108       */
109      public void testConstructor7() {
110          try {
# Line 120 | Line 119 | public class ArrayBlockingQueueTest exte
119      }
120  
121      /**
122 <     *
122 >     * Queue transitions from empty to full when elements added
123       */
124      public void testEmptyFull() {
125          ArrayBlockingQueue q = new ArrayBlockingQueue(2);
# Line 135 | Line 134 | public class ArrayBlockingQueueTest exte
134      }
135  
136      /**
137 <     *
137 >     * remainingCapacity decreases on add, increases on remove
138       */
139      public void testRemainingCapacity() {
140          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 152 | Line 151 | public class ArrayBlockingQueueTest exte
151      }
152  
153      /**
154 <     *
154 >     *  offer(null) throws NPE
155       */
156      public void testOfferNull() {
157          try {
# Line 163 | Line 162 | public class ArrayBlockingQueueTest exte
162      }
163  
164      /**
165 <     *
165 >     *  add(null) throws NPE
166 >     */
167 >    public void testAddNull() {
168 >        try {
169 >            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
170 >            q.add(null);
171 >            shouldThrow();
172 >        } catch (NullPointerException success) { }  
173 >    }
174 >
175 >    /**
176 >     * Offer succeeds if not full; fails if full
177       */
178      public void testOffer() {
179          ArrayBlockingQueue q = new ArrayBlockingQueue(1);
# Line 172 | Line 182 | public class ArrayBlockingQueueTest exte
182      }
183  
184      /**
185 <     *
185 >     * add succeeds if not full; throws ISE if full
186       */
187      public void testAdd() {
188          try {
# Line 187 | Line 197 | public class ArrayBlockingQueueTest exte
197      }
198  
199      /**
200 <     *
200 >     *  addAll(null) throws NPE
201       */
202      public void testAddAll1() {
203          try {
# Line 197 | Line 207 | public class ArrayBlockingQueueTest exte
207          }
208          catch (NullPointerException success) {}
209      }
210 +
211      /**
212 <     *
212 >     * addAll(this) throws IAE
213 >     */
214 >    public void testAddAllSelf() {
215 >        try {
216 >            ArrayBlockingQueue q = populatedQueue(SIZE);
217 >            q.addAll(q);
218 >            shouldThrow();
219 >        }
220 >        catch (IllegalArgumentException success) {}
221 >    }
222 >
223 >
224 >    /**
225 >     *  addAll of a collection with null elements throws NPE
226       */
227      public void testAddAll2() {
228          try {
# Line 210 | Line 234 | public class ArrayBlockingQueueTest exte
234          catch (NullPointerException success) {}
235      }
236      /**
237 <     *
237 >     * addAll of a collection with any null elements throws NPE after
238 >     * possibly adding some elements
239       */
240      public void testAddAll3() {
241          try {
# Line 224 | Line 249 | public class ArrayBlockingQueueTest exte
249          catch (NullPointerException success) {}
250      }
251      /**
252 <     *
252 >     * addAll throws ISE if not enough room
253       */
254      public void testAddAll4() {
255          try {
# Line 238 | Line 263 | public class ArrayBlockingQueueTest exte
263          catch (IllegalStateException success) {}
264      }
265      /**
266 <     *
266 >     * Queue contains all elements, in traversal order, of successful addAll
267       */
268      public void testAddAll5() {
269          try {
# Line 256 | Line 281 | public class ArrayBlockingQueueTest exte
281      }
282  
283      /**
284 <     *
284 >     *  put(null) throws NPE
285       */
286       public void testPutNull() {
287          try {
# Line 272 | Line 297 | public class ArrayBlockingQueueTest exte
297       }
298  
299      /**
300 <     *
300 >     * all elements successfully put are contained
301       */
302       public void testPut() {
303           try {
# Line 290 | Line 315 | public class ArrayBlockingQueueTest exte
315      }
316  
317      /**
318 <     *
318 >     * put blocks interruptibly if full
319       */
320      public void testBlockingPut() {
321          Thread t = new Thread(new Runnable() {
# Line 320 | Line 345 | public class ArrayBlockingQueueTest exte
345      }
346  
347      /**
348 <     *
348 >     * put blocks waiting for take when full
349       */
350      public void testPutWithTake() {
351          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
# Line 354 | Line 379 | public class ArrayBlockingQueueTest exte
379      }
380  
381      /**
382 <     *
382 >     * timed offer times out if full and elements not taken
383       */
384      public void testTimedOffer() {
385          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
# Line 381 | Line 406 | public class ArrayBlockingQueueTest exte
406      }
407  
408      /**
409 <     *
409 >     * take retrieves elements in FIFO order
410       */
411      public void testTake() {
412          try {
# Line 395 | Line 420 | public class ArrayBlockingQueueTest exte
420      }
421  
422      /**
423 <     *
423 >     * take blocks interruptibly when empty
424       */
425      public void testTakeFromEmpty() {
426          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
# Line 418 | Line 443 | public class ArrayBlockingQueueTest exte
443      }
444  
445      /**
446 <     *
446 >     * Take removes existing elements until empty, then blocks interruptibly
447       */
448      public void testBlockingTake() {
449          Thread t = new Thread(new Runnable() {
# Line 446 | Line 471 | public class ArrayBlockingQueueTest exte
471  
472  
473      /**
474 <     *
474 >     * poll succeeds unless empty
475       */
476      public void testPoll() {
477          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 457 | Line 482 | public class ArrayBlockingQueueTest exte
482      }
483  
484      /**
485 <     *
485 >     * timed pool with zero timeout succeeds when non-empty, else times out
486       */
487      public void testTimedPoll0() {
488          try {
# Line 472 | Line 497 | public class ArrayBlockingQueueTest exte
497      }
498  
499      /**
500 <     *
500 >     * timed pool with nonzero timeout succeeds when non-empty, else times out
501       */
502      public void testTimedPoll() {
503          try {
# Line 487 | Line 512 | public class ArrayBlockingQueueTest exte
512      }
513  
514      /**
515 <     *
515 >     * Interrupted timed poll throws InterruptedException instead of
516 >     * returning timeout status
517       */
518      public void testInterruptedTimedPoll() {
519          Thread t = new Thread(new Runnable() {
# Line 513 | Line 539 | public class ArrayBlockingQueueTest exte
539      }
540  
541      /**
542 <     *
542 >     *  timed poll before a delayed offer fails; after offer succeeds;
543 >     *  on interruption throws
544       */
545      public void testTimedPollWithOffer() {
546          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
# Line 540 | Line 567 | public class ArrayBlockingQueueTest exte
567  
568  
569      /**
570 <     *
570 >     * peek returns next element, or null if empty
571       */
572      public void testPeek() {
573          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 554 | Line 581 | public class ArrayBlockingQueueTest exte
581      }
582  
583      /**
584 <     *
584 >     * element returns next element, or throws NSEE if empty
585       */
586      public void testElement() {
587          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 570 | Line 597 | public class ArrayBlockingQueueTest exte
597      }
598  
599      /**
600 <     *
600 >     * remove removes next element, or throws NSEE if empty
601       */
602      public void testRemove() {
603          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 585 | Line 612 | public class ArrayBlockingQueueTest exte
612      }
613  
614      /**
615 <     *
615 >     * remove(x) removes x and returns true if present
616       */
617      public void testRemoveElement() {
618          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 600 | Line 627 | public class ArrayBlockingQueueTest exte
627      }
628          
629      /**
630 <     *
630 >     * contains(x) reports true when elements added but not yet removed
631       */
632      public void testContains() {
633          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 612 | Line 639 | public class ArrayBlockingQueueTest exte
639      }
640  
641      /**
642 <     *
642 >     * clear removes all elements
643       */
644      public void testClear() {
645          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 627 | Line 654 | public class ArrayBlockingQueueTest exte
654      }
655  
656      /**
657 <     *
657 >     * containsAll(c) is true when c contains a subset of elements
658       */
659      public void testContainsAll() {
660          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 641 | Line 668 | public class ArrayBlockingQueueTest exte
668      }
669  
670      /**
671 <     *
671 >     * retainAll(c) retains only those elements of c and reports true if changed
672       */
673      public void testRetainAll() {
674          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 660 | Line 687 | public class ArrayBlockingQueueTest exte
687      }
688  
689      /**
690 <     *
690 >     * removeAll(c) removes only those elements of c and reports true if changed
691       */
692      public void testRemoveAll() {
693          for (int i = 1; i < SIZE; ++i) {
# Line 675 | Line 702 | public class ArrayBlockingQueueTest exte
702          }
703      }
704  
678
705      /**
706 <     *
706 >     *  toArray contains all elements
707       */
708      public void testToArray() {
709          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 691 | Line 717 | public class ArrayBlockingQueueTest exte
717      }
718  
719      /**
720 <     *
720 >     * toArray(a) contains all elements
721       */
722      public void testToArray2() {
723          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 704 | Line 730 | public class ArrayBlockingQueueTest exte
730              unexpectedException();
731          }    
732      }
733 +
734 +    /**
735 +     * toArray(null) throws NPE
736 +     */
737 +    public void testToArray_BadArg() {
738 +        try {
739 +            ArrayBlockingQueue q = populatedQueue(SIZE);
740 +            Object o[] = q.toArray(null);
741 +            shouldThrow();
742 +        } catch(NullPointerException success){}
743 +    }
744 +
745 +    /**
746 +     * toArray with incompatable array type throws CCE
747 +     */
748 +    public void testToArray1_BadArg() {
749 +        try {
750 +            ArrayBlockingQueue q = populatedQueue(SIZE);
751 +            Object o[] = q.toArray(new String[10] );
752 +            shouldThrow();
753 +        } catch(ArrayStoreException  success){}
754 +    }
755 +
756      
757      /**
758 <     *
758 >     * iterator iterates through all elements
759       */
760      public void testIterator() {
761          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 721 | Line 770 | public class ArrayBlockingQueueTest exte
770      }
771  
772      /**
773 <     *
773 >     * iterator.remove removes current element
774 >     */
775 >    public void testIteratorRemove () {
776 >        final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
777 >        q.add(two);
778 >        q.add(one);
779 >        q.add(three);
780 >
781 >        Iterator it = q.iterator();
782 >        it.next();
783 >        it.remove();
784 >        
785 >        it = q.iterator();
786 >        assertEquals(it.next(), one);
787 >        assertEquals(it.next(), three);
788 >        assertFalse(it.hasNext());
789 >    }
790 >
791 >    /**
792 >     * iterator ordering is FIFO
793       */
794      public void testIteratorOrdering() {
795          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
# Line 740 | Line 808 | public class ArrayBlockingQueueTest exte
808      }
809  
810      /**
811 <     *
811 >     * Modifications do not cause iterators to fail
812       */
813      public void testWeaklyConsistentIteration () {
814          final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
# Line 756 | Line 824 | public class ArrayBlockingQueueTest exte
824          catch (ConcurrentModificationException e) {
825              unexpectedException();
826          }
759
827          assertEquals(0, q.size());
828      }
829  
830  
831      /**
832 <     *
832 >     * toString contains toStrings of elements
833       */
834      public void testToString() {
835          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 774 | Line 841 | public class ArrayBlockingQueueTest exte
841  
842  
843      /**
844 <     *
844 >     * offer transfers elements across Executor tasks
845       */
846      public void testOfferInExecutor() {
780
847          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
782
848          q.add(one);
849          q.add(two);
785
850          ExecutorService executor = Executors.newFixedThreadPool(2);
787
851          executor.execute(new Runnable() {
852              public void run() {
853                  threadAssertFalse(q.offer(three));
# Line 815 | Line 878 | public class ArrayBlockingQueueTest exte
878      }
879  
880      /**
881 <     *
881 >     * poll retrieves elements across Executor threads
882       */
883      public void testPollInExecutor() {
821
884          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
823
885          ExecutorService executor = Executors.newFixedThreadPool(2);
825
886          executor.execute(new Runnable() {
887              public void run() {
888                  threadAssertNull(q.poll());
# Line 849 | Line 909 | public class ArrayBlockingQueueTest exte
909          });
910          
911          joinPool(executor);
852
912      }
913  
914      /**
915 <     *
915 >     * A deserialized serialized queue has same elements in same order
916       */
917      public void testSerialization() {
918          ArrayBlockingQueue q = populatedQueue(SIZE);
# Line 875 | Line 934 | public class ArrayBlockingQueueTest exte
934          }
935      }
936  
937 +    /**
938 +     * drainTo(null) throws NPE
939 +     */
940 +    public void testDrainToNull() {
941 +        ArrayBlockingQueue q = populatedQueue(SIZE);
942 +        try {
943 +            q.drainTo(null);
944 +            shouldThrow();
945 +        } catch(NullPointerException success) {
946 +        }
947 +    }
948 +
949 +    /**
950 +     * drainTo(this) throws IAE
951 +     */
952 +    public void testDrainToSelf() {
953 +        ArrayBlockingQueue q = populatedQueue(SIZE);
954 +        try {
955 +            q.drainTo(q);
956 +            shouldThrow();
957 +        } catch(IllegalArgumentException success) {
958 +        }
959 +    }
960 +
961 +    /**
962 +     * drainTo(c) empties queue into another collection c
963 +     */
964 +    public void testDrainTo() {
965 +        ArrayBlockingQueue q = populatedQueue(SIZE);
966 +        ArrayList l = new ArrayList();
967 +        q.drainTo(l);
968 +        assertEquals(q.size(), 0);
969 +        assertEquals(l.size(), SIZE);
970 +        for (int i = 0; i < SIZE; ++i)
971 +            assertEquals(l.get(i), new Integer(i));
972 +    }
973 +
974 +    /**
975 +     * drainTo empties full queue, unblocking a waiting put.
976 +     */
977 +    public void testDrainToWithActivePut() {
978 +        final ArrayBlockingQueue q = populatedQueue(SIZE);
979 +        Thread t = new Thread(new Runnable() {
980 +                public void run() {
981 +                    try {
982 +                        q.put(new Integer(SIZE+1));
983 +                    } catch (InterruptedException ie){
984 +                        threadUnexpectedException();
985 +                    }
986 +                }
987 +            });
988 +        try {
989 +            t.start();
990 +            ArrayList l = new ArrayList();
991 +            q.drainTo(l);
992 +            assertTrue(l.size() >= SIZE);
993 +            for (int i = 0; i < SIZE; ++i)
994 +                assertEquals(l.get(i), new Integer(i));
995 +            t.join();
996 +            assertTrue(q.size() + l.size() == SIZE+1);
997 +        } catch(Exception e){
998 +            unexpectedException();
999 +        }
1000 +    }
1001 +
1002 +    /**
1003 +     * drainTo(null, n) throws NPE
1004 +     */
1005 +    public void testDrainToNullN() {
1006 +        ArrayBlockingQueue q = populatedQueue(SIZE);
1007 +        try {
1008 +            q.drainTo(null, 0);
1009 +            shouldThrow();
1010 +        } catch(NullPointerException success) {
1011 +        }
1012 +    }
1013 +
1014 +    /**
1015 +     * drainTo(this, n) throws IAE
1016 +     */
1017 +    public void testDrainToSelfN() {
1018 +        ArrayBlockingQueue q = populatedQueue(SIZE);
1019 +        try {
1020 +            q.drainTo(q, 0);
1021 +            shouldThrow();
1022 +        } catch(IllegalArgumentException success) {
1023 +        }
1024 +    }
1025 +
1026 +    /**
1027 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
1028 +     */
1029 +    public void testDrainToN() {
1030 +        for (int i = 0; i < SIZE + 2; ++i) {
1031 +            ArrayBlockingQueue q = populatedQueue(SIZE);
1032 +            ArrayList l = new ArrayList();
1033 +            q.drainTo(l, i);
1034 +            int k = (i < SIZE)? i : SIZE;
1035 +            assertEquals(q.size(), SIZE-k);
1036 +            assertEquals(l.size(), k);
1037 +            for (int j = 0; j < k; ++j)
1038 +                assertEquals(l.get(j), new Integer(j));
1039 +        }
1040 +    }
1041 +
1042  
1043   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines