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

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.4 by dl, Sat Sep 20 18:20:08 2003 UTC vs.
Revision 1.9 by dl, Wed Jan 7 01:13:50 2004 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   import junit.framework.*;
# Line 37 | Line 38 | public class LinkedBlockingQueueTest ext
38      }
39  
40      /**
41 <     *
41 >     * A new queue has the indicated capacity, or Integer.MAX_VALUE if
42 >     * none given
43       */
44      public void testConstructor1() {
45          assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
46 +        assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
47      }
48  
49      /**
50 <     *
50 >     * Constructor throws IAE if  capacity argument nonpositive
51       */
52      public void testConstructor2() {
53          try {
# Line 55 | Line 58 | public class LinkedBlockingQueueTest ext
58      }
59  
60      /**
61 <     *
61 >     * Initializing from null Collection throws NPE
62       */
63      public void testConstructor3() {
61
64          try {
65              LinkedBlockingQueue q = new LinkedBlockingQueue(null);
66              shouldThrow();
# Line 67 | Line 69 | public class LinkedBlockingQueueTest ext
69      }
70  
71      /**
72 <     *
72 >     * Initializing from Collection of null elements throws NPE
73       */
74      public void testConstructor4() {
75          try {
# Line 79 | Line 81 | public class LinkedBlockingQueueTest ext
81      }
82  
83      /**
84 <     *
84 >     * Initializing from Collection with some null elements throws NPE
85       */
86      public void testConstructor5() {
87          try {
# Line 93 | Line 95 | public class LinkedBlockingQueueTest ext
95      }
96  
97      /**
98 <     *
98 >     * Queue contains all elements of collection used to initialize
99       */
100      public void testConstructor6() {
101          try {
# Line 108 | Line 110 | public class LinkedBlockingQueueTest ext
110      }
111  
112      /**
113 <     *
113 >     * Queue transitions from empty to full when elements added
114       */
115      public void testEmptyFull() {
116          LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 123 | Line 125 | public class LinkedBlockingQueueTest ext
125      }
126  
127      /**
128 <     *
128 >     * remainingCapacity decreases on add, increases on remove
129       */
130      public void testRemainingCapacity() {
131          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 140 | Line 142 | public class LinkedBlockingQueueTest ext
142      }
143  
144      /**
145 <     *
145 >     * offer(null) throws NPE
146       */
147      public void testOfferNull() {
148          try {
# Line 151 | Line 153 | public class LinkedBlockingQueueTest ext
153      }
154  
155      /**
156 <     *
156 >     * add(null) throws NPE
157 >     */
158 >    public void testAddNull() {
159 >        try {
160 >            LinkedBlockingQueue q = new LinkedBlockingQueue(1);
161 >            q.add(null);
162 >            shouldThrow();
163 >        } catch (NullPointerException success) { }  
164 >    }
165 >
166 >    /**
167 >     * Offer succeeds if not full; fails if full
168       */
169      public void testOffer() {
170          LinkedBlockingQueue q = new LinkedBlockingQueue(1);
# Line 160 | Line 173 | public class LinkedBlockingQueueTest ext
173      }
174  
175      /**
176 <     *
176 >     * add succeeds if not full; throws ISE if full
177       */
178      public void testAdd() {
179          try {
# Line 175 | Line 188 | public class LinkedBlockingQueueTest ext
188      }
189  
190      /**
191 <     *
191 >     * addAll(null) throws NPE
192       */
193      public void testAddAll1() {
194          try {
# Line 185 | Line 198 | public class LinkedBlockingQueueTest ext
198          }
199          catch (NullPointerException success) {}
200      }
201 +
202      /**
203 <     *
203 >     * addAll(this) throws IAE
204 >     */
205 >    public void testAddAllSelf() {
206 >        try {
207 >            LinkedBlockingQueue q = populatedQueue(SIZE);
208 >            q.addAll(q);
209 >            shouldThrow();
210 >        }
211 >        catch (IllegalArgumentException success) {}
212 >    }
213 >
214 >    /**
215 >     * addAll of a collection with null elements throws NPE
216       */
217      public void testAddAll2() {
218          try {
# Line 198 | Line 224 | public class LinkedBlockingQueueTest ext
224          catch (NullPointerException success) {}
225      }
226      /**
227 <     *
227 >     * addAll of a collection with any null elements throws NPE after
228 >     * possibly adding some elements
229       */
230      public void testAddAll3() {
231          try {
# Line 212 | Line 239 | public class LinkedBlockingQueueTest ext
239          catch (NullPointerException success) {}
240      }
241      /**
242 <     *
242 >     * addAll throws ISE if not enough room
243       */
244      public void testAddAll4() {
245          try {
# Line 226 | Line 253 | public class LinkedBlockingQueueTest ext
253          catch (IllegalStateException success) {}
254      }
255      /**
256 <     *
256 >     * Queue contains all elements, in traversal order, of successful addAll
257       */
258      public void testAddAll5() {
259          try {
# Line 244 | Line 271 | public class LinkedBlockingQueueTest ext
271      }
272  
273      /**
274 <     *
274 >     * put(null) throws NPE
275       */
276       public void testPutNull() {
277          try {
# Line 260 | Line 287 | public class LinkedBlockingQueueTest ext
287       }
288  
289      /**
290 <     *
290 >     * all elements successfully put are contained
291       */
292       public void testPut() {
293           try {
# Line 278 | Line 305 | public class LinkedBlockingQueueTest ext
305      }
306  
307      /**
308 <     *
308 >     * put blocks interruptibly if full
309       */
310      public void testBlockingPut() {
311          Thread t = new Thread(new Runnable() {
# Line 308 | Line 335 | public class LinkedBlockingQueueTest ext
335      }
336  
337      /**
338 <     *
338 >     * put blocks waiting for take when full
339       */
340      public void testPutWithTake() {
341          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 342 | Line 369 | public class LinkedBlockingQueueTest ext
369      }
370  
371      /**
372 <     *
372 >     * timed offer times out if full and elements not taken
373       */
374      public void testTimedOffer() {
375          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 369 | Line 396 | public class LinkedBlockingQueueTest ext
396      }
397  
398      /**
399 <     *
399 >     * take retrieves elements in FIFO order
400       */
401      public void testTake() {
402          try {
# Line 383 | Line 410 | public class LinkedBlockingQueueTest ext
410      }
411  
412      /**
413 <     *
413 >     * take blocks interruptibly when empty
414       */
415      public void testTakeFromEmpty() {
416          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 406 | Line 433 | public class LinkedBlockingQueueTest ext
433      }
434  
435      /**
436 <     *
436 >     * Take removes existing elements until empty, then blocks interruptibly
437       */
438      public void testBlockingTake() {
439          Thread t = new Thread(new Runnable() {
# Line 434 | Line 461 | public class LinkedBlockingQueueTest ext
461  
462  
463      /**
464 <     *
464 >     * poll succeeds unless empty
465       */
466      public void testPoll() {
467          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 445 | Line 472 | public class LinkedBlockingQueueTest ext
472      }
473  
474      /**
475 <     *
475 >     * timed pool with zero timeout succeeds when non-empty, else times out
476       */
477      public void testTimedPoll0() {
478          try {
# Line 460 | Line 487 | public class LinkedBlockingQueueTest ext
487      }
488  
489      /**
490 <     *
490 >     * timed pool with nonzero timeout succeeds when non-empty, else times out
491       */
492      public void testTimedPoll() {
493          try {
# Line 475 | Line 502 | public class LinkedBlockingQueueTest ext
502      }
503  
504      /**
505 <     *
505 >     * Interrupted timed poll throws InterruptedException instead of
506 >     * returning timeout status
507       */
508      public void testInterruptedTimedPoll() {
509          Thread t = new Thread(new Runnable() {
# Line 501 | Line 529 | public class LinkedBlockingQueueTest ext
529      }
530  
531      /**
532 <     *
532 >     *  timed poll before a delayed offer fails; after offer succeeds;
533 >     *  on interruption throws
534       */
535      public void testTimedPollWithOffer() {
536          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
# Line 526 | Line 555 | public class LinkedBlockingQueueTest ext
555          }
556      }  
557  
529
558      /**
559 <     *
559 >     * peek returns next element, or null if empty
560       */
561      public void testPeek() {
562          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 542 | Line 570 | public class LinkedBlockingQueueTest ext
570      }
571  
572      /**
573 <     *
573 >     * element returns next element, or throws NSEE if empty
574       */
575      public void testElement() {
576          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 558 | Line 586 | public class LinkedBlockingQueueTest ext
586      }
587  
588      /**
589 <     *
589 >     * remove removes next element, or throws NSEE if empty
590       */
591      public void testRemove() {
592          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 573 | Line 601 | public class LinkedBlockingQueueTest ext
601      }
602  
603      /**
604 <     *
604 >     * remove(x) removes x and returns true if present
605       */
606      public void testRemoveElement() {
607          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 588 | Line 616 | public class LinkedBlockingQueueTest ext
616      }
617          
618      /**
619 <     *
619 >     * contains(x) reports true when elements added but not yet removed
620       */
621      public void testContains() {
622          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 600 | Line 628 | public class LinkedBlockingQueueTest ext
628      }
629  
630      /**
631 <     *
631 >     * clear removes all elements
632       */
633      public void testClear() {
634          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 615 | Line 643 | public class LinkedBlockingQueueTest ext
643      }
644  
645      /**
646 <     *
646 >     * containsAll(c) is true when c contains a subset of elements
647       */
648      public void testContainsAll() {
649          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 629 | Line 657 | public class LinkedBlockingQueueTest ext
657      }
658  
659      /**
660 <     *
660 >     * retainAll(c) retains only those elements of c and reports true if changed
661       */
662      public void testRetainAll() {
663          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 648 | Line 676 | public class LinkedBlockingQueueTest ext
676      }
677  
678      /**
679 <     *
679 >     * removeAll(c) removes only those elements of c and reports true if changed
680       */
681      public void testRemoveAll() {
682          for (int i = 1; i < SIZE; ++i) {
# Line 663 | Line 691 | public class LinkedBlockingQueueTest ext
691          }
692      }
693  
666
694      /**
695 <     *
695 >     * toArray contains all elements
696       */
697      public void testToArray() {
698          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 679 | Line 706 | public class LinkedBlockingQueueTest ext
706      }
707  
708      /**
709 <     *
709 >     * toArray(a) contains all elements
710       */
711      public void testToArray2() {
712          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 692 | Line 719 | public class LinkedBlockingQueueTest ext
719              unexpectedException();
720          }    
721      }
722 +
723 +    /**
724 +     * toArray(null) throws NPE
725 +     */
726 +    public void testToArray_BadArg() {
727 +        try {
728 +            LinkedBlockingQueue q = populatedQueue(SIZE);
729 +            Object o[] = q.toArray(null);
730 +            shouldThrow();
731 +        } catch(NullPointerException success){}
732 +    }
733 +
734 +    /**
735 +     * toArray with incompatible array type throws CCE
736 +     */
737 +    public void testToArray1_BadArg() {
738 +        try {
739 +            LinkedBlockingQueue q = populatedQueue(SIZE);
740 +            Object o[] = q.toArray(new String[10] );
741 +            shouldThrow();
742 +        } catch(ArrayStoreException  success){}
743 +    }
744 +
745      
746      /**
747 <     *
747 >     * iterator iterates through all elements
748       */
749      public void testIterator() {
750          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 709 | Line 759 | public class LinkedBlockingQueueTest ext
759      }
760  
761      /**
762 <     *
762 >     * iterator.remove removes current element
763       */
764 <    public void testIteratorOrdering() {
715 <
764 >    public void testIteratorRemove () {
765          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
766 +        q.add(two);
767 +        q.add(one);
768 +        q.add(three);
769 +
770 +        Iterator it = q.iterator();
771 +        it.next();
772 +        it.remove();
773 +        
774 +        it = q.iterator();
775 +        assertEquals(it.next(), one);
776 +        assertEquals(it.next(), three);
777 +        assertFalse(it.hasNext());
778 +    }
779 +
780  
781 +    /**
782 +     * iterator ordering is FIFO
783 +     */
784 +    public void testIteratorOrdering() {
785 +        final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
786          q.add(one);
787          q.add(two);
788          q.add(three);
721
789          assertEquals(0, q.remainingCapacity());
723
790          int k = 0;
791          for (Iterator it = q.iterator(); it.hasNext();) {
792              int i = ((Integer)(it.next())).intValue();
793              assertEquals(++k, i);
794          }
729
795          assertEquals(3, k);
796      }
797  
798      /**
799 <     *
799 >     * Modifications do not cause iterators to fail
800       */
801      public void testWeaklyConsistentIteration () {
737
802          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
739
803          q.add(one);
804          q.add(two);
805          q.add(three);
743
806          try {
807              for (Iterator it = q.iterator(); it.hasNext();) {
808                  q.remove();
# Line 750 | Line 812 | public class LinkedBlockingQueueTest ext
812          catch (ConcurrentModificationException e) {
813              unexpectedException();
814          }
753
815          assertEquals(0, q.size());
816      }
817  
818  
819      /**
820 <     *
820 >     * toString contains toStrings of elements
821       */
822      public void testToString() {
823          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 768 | Line 829 | public class LinkedBlockingQueueTest ext
829  
830  
831      /**
832 <     *
832 >     * offer transfers elements across Executor tasks
833       */
834      public void testOfferInExecutor() {
774
835          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
776
836          q.add(one);
837          q.add(two);
779
838          ExecutorService executor = Executors.newFixedThreadPool(2);
781
839          executor.execute(new Runnable() {
840              public void run() {
841                  threadAssertFalse(q.offer(three));
# Line 805 | Line 862 | public class LinkedBlockingQueueTest ext
862          });
863          
864          joinPool(executor);
808
865      }
866  
867      /**
868 <     *
868 >     * poll retrieves elements across Executor threads
869       */
870      public void testPollInExecutor() {
815
871          final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
817
872          ExecutorService executor = Executors.newFixedThreadPool(2);
819
873          executor.execute(new Runnable() {
874              public void run() {
875                  threadAssertNull(q.poll());
# Line 846 | Line 899 | public class LinkedBlockingQueueTest ext
899      }
900  
901      /**
902 <     *
902 >     * A deserialized serialized queue has same elements in same order
903       */
904      public void testSerialization() {
905          LinkedBlockingQueue q = populatedQueue(SIZE);
# Line 868 | Line 921 | public class LinkedBlockingQueueTest ext
921          }
922      }
923  
924 +    /**
925 +     * drainTo(null) throws NPE
926 +     */
927 +    public void testDrainToNull() {
928 +        LinkedBlockingQueue q = populatedQueue(SIZE);
929 +        try {
930 +            q.drainTo(null);
931 +            shouldThrow();
932 +        } catch(NullPointerException success) {
933 +        }
934 +    }
935 +
936 +    /**
937 +     * drainTo(this) throws IAE
938 +     */
939 +    public void testDrainToSelf() {
940 +        LinkedBlockingQueue q = populatedQueue(SIZE);
941 +        try {
942 +            q.drainTo(q);
943 +            shouldThrow();
944 +        } catch(IllegalArgumentException success) {
945 +        }
946 +    }
947 +
948 +    /**
949 +     * drainTo(c) empties queue into another collection c
950 +     */
951 +    public void testDrainTo() {
952 +        LinkedBlockingQueue q = populatedQueue(SIZE);
953 +        ArrayList l = new ArrayList();
954 +        q.drainTo(l);
955 +        assertEquals(q.size(), 0);
956 +        assertEquals(l.size(), SIZE);
957 +        for (int i = 0; i < SIZE; ++i)
958 +            assertEquals(l.get(i), new Integer(i));
959 +    }
960 +
961 +    /**
962 +     * drainTo empties full queue, unblocking a waiting put.
963 +     */
964 +    public void testDrainToWithActivePut() {
965 +        final LinkedBlockingQueue q = populatedQueue(SIZE);
966 +        Thread t = new Thread(new Runnable() {
967 +                public void run() {
968 +                    try {
969 +                        q.put(new Integer(SIZE+1));
970 +                    } catch (InterruptedException ie){
971 +                        threadUnexpectedException();
972 +                    }
973 +                }
974 +            });
975 +        try {
976 +            t.start();
977 +            ArrayList l = new ArrayList();
978 +            q.drainTo(l);
979 +            assertTrue(l.size() >= SIZE);
980 +            for (int i = 0; i < SIZE; ++i)
981 +                assertEquals(l.get(i), new Integer(i));
982 +            t.join();
983 +            assertTrue(q.size() + l.size() >= SIZE);
984 +        } catch(Exception e){
985 +            unexpectedException();
986 +        }
987 +    }
988 +
989 +    /**
990 +     * drainTo(null, n) throws NPE
991 +     */
992 +    public void testDrainToNullN() {
993 +        LinkedBlockingQueue q = populatedQueue(SIZE);
994 +        try {
995 +            q.drainTo(null, 0);
996 +            shouldThrow();
997 +        } catch(NullPointerException success) {
998 +        }
999 +    }
1000 +
1001 +    /**
1002 +     * drainTo(this, n) throws IAE
1003 +     */
1004 +    public void testDrainToSelfN() {
1005 +        LinkedBlockingQueue q = populatedQueue(SIZE);
1006 +        try {
1007 +            q.drainTo(q, 0);
1008 +            shouldThrow();
1009 +        } catch(IllegalArgumentException success) {
1010 +        }
1011 +    }
1012 +
1013 +    /**
1014 +     * drainTo(c, n) empties first max {n, size} elements of queue into c
1015 +     */
1016 +    public void testDrainToN() {
1017 +        for (int i = 0; i < SIZE + 2; ++i) {
1018 +            LinkedBlockingQueue q = populatedQueue(SIZE);
1019 +            ArrayList l = new ArrayList();
1020 +            q.drainTo(l, i);
1021 +            int k = (i < SIZE)? i : SIZE;
1022 +            assertEquals(q.size(), SIZE-k);
1023 +            assertEquals(l.size(), k);
1024 +            for (int j = 0; j < k; ++j)
1025 +                assertEquals(l.get(j), new Integer(j));
1026 +        }
1027 +    }
1028 +
1029   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines