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.7 by dl, Sat Dec 27 19:26:42 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines