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

Comparing jsr166/src/test/tck/LinkedBlockingDequeTest.java (file contents):
Revision 1.15 by jsr166, Sat Nov 21 21:12:55 2009 UTC vs.
Revision 1.32 by jsr166, Fri Nov 5 00:17:22 2010 UTC

# Line 11 | Line 11 | import static java.util.concurrent.TimeU
11   import java.io.*;
12  
13   public class LinkedBlockingDequeTest extends JSR166TestCase {
14 +
15 +    public static class Unbounded extends BlockingQueueTest {
16 +        protected BlockingQueue emptyCollection() {
17 +            return new LinkedBlockingDeque();
18 +        }
19 +    }
20 +
21 +    public static class Bounded extends BlockingQueueTest {
22 +        protected BlockingQueue emptyCollection() {
23 +            return new LinkedBlockingDeque(20);
24 +        }
25 +    }
26 +
27      public static void main(String[] args) {
28 <        junit.textui.TestRunner.run (suite());
28 >        junit.textui.TestRunner.run(suite());
29      }
30  
31      public static Test suite() {
32 <        return new TestSuite(LinkedBlockingDequeTest.class);
32 >        return newTestSuite(LinkedBlockingDequeTest.class,
33 >                            new Unbounded().testSuite(),
34 >                            new Bounded().testSuite());
35      }
36  
37      /**
38       * Create a deque of given size containing consecutive
39       * Integers 0 ... n.
40       */
41 <    private LinkedBlockingDeque populatedDeque(int n) {
42 <        LinkedBlockingDeque q = new LinkedBlockingDeque(n);
41 >    private LinkedBlockingDeque<Integer> populatedDeque(int n) {
42 >        LinkedBlockingDeque<Integer> q =
43 >            new LinkedBlockingDeque<Integer>(n);
44          assertTrue(q.isEmpty());
45          for (int i = 0; i < n; i++)
46              assertTrue(q.offer(new Integer(i)));
# Line 93 | Line 109 | public class LinkedBlockingDequeTest ext
109      }
110  
111      /**
112 <     *  pollFirst succeeds unless empty
112 >     * pollFirst succeeds unless empty
113       */
114      public void testPollFirst() {
115          LinkedBlockingDeque q = populatedDeque(SIZE);
116          for (int i = 0; i < SIZE; ++i) {
117 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
117 >            assertEquals(i, q.pollFirst());
118          }
119          assertNull(q.pollFirst());
120      }
121  
122      /**
123 <     *  pollLast succeeds unless empty
123 >     * pollLast succeeds unless empty
124       */
125      public void testPollLast() {
126          LinkedBlockingDeque q = populatedDeque(SIZE);
127          for (int i = SIZE-1; i >= 0; --i) {
128 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
128 >            assertEquals(i, q.pollLast());
129          }
130          assertNull(q.pollLast());
131      }
132  
133      /**
134 <     *  peekFirst returns next element, or null if empty
134 >     * peekFirst returns next element, or null if empty
135       */
136      public void testPeekFirst() {
137          LinkedBlockingDeque q = populatedDeque(SIZE);
138          for (int i = 0; i < SIZE; ++i) {
139 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
140 <            q.pollFirst();
139 >            assertEquals(i, q.peekFirst());
140 >            assertEquals(i, q.pollFirst());
141              assertTrue(q.peekFirst() == null ||
142 <                       i != ((Integer)q.peekFirst()).intValue());
142 >                       !q.peekFirst().equals(i));
143          }
144          assertNull(q.peekFirst());
145      }
146  
147      /**
148 <     *  peek returns next element, or null if empty
148 >     * peek returns next element, or null if empty
149       */
150      public void testPeek() {
151          LinkedBlockingDeque q = populatedDeque(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153 <            assertEquals(i, ((Integer)q.peek()).intValue());
154 <            q.pollFirst();
153 >            assertEquals(i, q.peek());
154 >            assertEquals(i, q.pollFirst());
155              assertTrue(q.peek() == null ||
156 <                       i != ((Integer)q.peek()).intValue());
156 >                       !q.peek().equals(i));
157          }
158          assertNull(q.peek());
159      }
160  
161      /**
162 <     *  peekLast returns next element, or null if empty
162 >     * peekLast returns next element, or null if empty
163       */
164      public void testPeekLast() {
165          LinkedBlockingDeque q = populatedDeque(SIZE);
166          for (int i = SIZE-1; i >= 0; --i) {
167 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
168 <            q.pollLast();
167 >            assertEquals(i, q.peekLast());
168 >            assertEquals(i, q.pollLast());
169              assertTrue(q.peekLast() == null ||
170 <                       i != ((Integer)q.peekLast()).intValue());
170 >                       !q.peekLast().equals(i));
171          }
172          assertNull(q.peekLast());
173      }
174  
175      /**
176 <     * getFirst returns next getFirst, or throws NSEE if empty
176 >     * getFirst() returns first element, or throws NSEE if empty
177       */
178      public void testFirstElement() {
179          LinkedBlockingDeque q = populatedDeque(SIZE);
180          for (int i = 0; i < SIZE; ++i) {
181 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
182 <            q.pollFirst();
181 >            assertEquals(i, q.getFirst());
182 >            assertEquals(i, q.pollFirst());
183          }
184          try {
185              q.getFirst();
# Line 173 | Line 189 | public class LinkedBlockingDequeTest ext
189      }
190  
191      /**
192 <     *  getLast returns next element, or throws NSEE if empty
192 >     * getLast() returns last element, or throws NSEE if empty
193       */
194      public void testLastElement() {
195          LinkedBlockingDeque q = populatedDeque(SIZE);
196          for (int i = SIZE-1; i >= 0; --i) {
197 <            assertEquals(i, ((Integer)q.getLast()).intValue());
198 <            q.pollLast();
197 >            assertEquals(i, q.getLast());
198 >            assertEquals(i, q.pollLast());
199          }
200          try {
201              q.getLast();
# Line 189 | Line 205 | public class LinkedBlockingDequeTest ext
205      }
206  
207      /**
208 <     *  removeFirst removes next element, or throws NSEE if empty
208 >     * removeFirst() removes first element, or throws NSEE if empty
209       */
210      public void testRemoveFirst() {
211          LinkedBlockingDeque q = populatedDeque(SIZE);
212          for (int i = 0; i < SIZE; ++i) {
213 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
213 >            assertEquals(i, q.removeFirst());
214          }
215          try {
216              q.removeFirst();
# Line 204 | Line 220 | public class LinkedBlockingDequeTest ext
220      }
221  
222      /**
223 <     *  removeLast removes last element, or throws NSEE if empty
223 >     * removeLast() removes last element, or throws NSEE if empty
224       */
225      public void testRemoveLast() {
226          LinkedBlockingDeque q = populatedDeque(SIZE);
227          for (int i = SIZE - 1; i >= 0; --i) {
228 <            assertEquals(i, ((Integer)q.removeLast()).intValue());
228 >            assertEquals(i, q.removeLast());
229          }
230          try {
231              q.removeLast();
# Line 219 | Line 235 | public class LinkedBlockingDequeTest ext
235      }
236  
237      /**
238 <     *  remove removes next element, or throws NSEE if empty
238 >     * remove removes next element, or throws NSEE if empty
239       */
240      public void testRemove() {
241          LinkedBlockingDeque q = populatedDeque(SIZE);
242          for (int i = 0; i < SIZE; ++i) {
243 <            assertEquals(i, ((Integer)q.remove()).intValue());
243 >            assertEquals(i, q.remove());
244          }
245          try {
246              q.remove();
# Line 269 | Line 285 | public class LinkedBlockingDequeTest ext
285          LinkedBlockingDeque q = populatedDeque(3);
286          q.pollLast();
287          q.addFirst(four);
288 <        assertEquals(four,q.peekFirst());
288 >        assertSame(four, q.peekFirst());
289      }
290  
291      /**
# Line 279 | Line 295 | public class LinkedBlockingDequeTest ext
295          LinkedBlockingDeque q = populatedDeque(3);
296          q.pollLast();
297          q.addLast(four);
298 <        assertEquals(four,q.peekLast());
298 >        assertSame(four, q.peekLast());
299      }
300  
301  
# Line 437 | Line 453 | public class LinkedBlockingDequeTest ext
453          LinkedBlockingDeque q = populatedDeque(3);
454          q.pollLast();
455          q.push(four);
456 <        assertEquals(four,q.peekFirst());
456 >        assertSame(four, q.peekFirst());
457      }
458  
459  
460      /**
461 <     *  pop removes next element, or throws NSEE if empty
461 >     * pop removes next element, or throws NSEE if empty
462       */
463      public void testPop() {
464          LinkedBlockingDeque q = populatedDeque(SIZE);
465          for (int i = 0; i < SIZE; ++i) {
466 <            assertEquals(i, ((Integer)q.pop()).intValue());
466 >            assertEquals(i, q.pop());
467          }
468          try {
469              q.pop();
# Line 513 | Line 529 | public class LinkedBlockingDequeTest ext
529              shouldThrow();
530          } catch (NullPointerException success) {}
531      }
532 +
533      /**
534       * addAll of a collection with any null elements throws NPE after
535       * possibly adding some elements
# Line 527 | Line 544 | public class LinkedBlockingDequeTest ext
544              shouldThrow();
545          } catch (NullPointerException success) {}
546      }
547 +
548      /**
549       * addAll throws ISE if not enough room
550       */
# Line 540 | Line 558 | public class LinkedBlockingDequeTest ext
558              shouldThrow();
559          } catch (IllegalStateException success) {}
560      }
561 +
562      /**
563       * Deque contains all elements, in traversal order, of successful addAll
564       */
# Line 584 | Line 603 | public class LinkedBlockingDequeTest ext
603       * put blocks interruptibly if full
604       */
605      public void testBlockingPut() throws InterruptedException {
606 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
607          Thread t = new Thread(new CheckedRunnable() {
608 <            public void realRun() {
609 <                int added = 0;
608 >            public void realRun() throws InterruptedException {
609 >                for (int i = 0; i < SIZE; ++i)
610 >                    q.put(i);
611 >                assertEquals(SIZE, q.size());
612 >                assertEquals(0, q.remainingCapacity());
613                  try {
614 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
615 <                    for (int i = 0; i < SIZE; ++i) {
616 <                        q.put(new Integer(i));
594 <                        ++added;
595 <                    }
596 <                    q.put(new Integer(SIZE));
597 <                    threadShouldThrow();
598 <                } catch (InterruptedException success) {
599 <                    threadAssertEquals(added, SIZE);
600 <                }
614 >                    q.put(99);
615 >                    shouldThrow();
616 >                } catch (InterruptedException success) {}
617              }});
618  
619          t.start();
620          Thread.sleep(SHORT_DELAY_MS);
621          t.interrupt();
622          t.join();
623 +        assertEquals(SIZE, q.size());
624 +        assertEquals(0, q.remainingCapacity());
625      }
626  
627      /**
628       * put blocks waiting for take when full
629       */
630      public void testPutWithTake() throws InterruptedException {
631 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
631 >        final int capacity = 2;
632 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
633          Thread t = new Thread(new CheckedRunnable() {
634 <            public void realRun() {
635 <                int added = 0;
634 >            public void realRun() throws InterruptedException {
635 >                for (int i = 0; i < capacity + 1; i++)
636 >                    q.put(i);
637                  try {
638 <                    q.put(new Object());
639 <                    ++added;
640 <                    q.put(new Object());
621 <                    ++added;
622 <                    q.put(new Object());
623 <                    ++added;
624 <                    q.put(new Object());
625 <                    ++added;
626 <                    threadShouldThrow();
627 <                } catch (InterruptedException success) {
628 <                    threadAssertTrue(added >= 2);
629 <                }
638 >                    q.put(99);
639 >                    shouldThrow();
640 >                } catch (InterruptedException success) {}
641              }});
642  
643          t.start();
644          Thread.sleep(SHORT_DELAY_MS);
645 <        q.take();
645 >        assertEquals(q.remainingCapacity(), 0);
646 >        assertEquals(0, q.take());
647 >        Thread.sleep(SHORT_DELAY_MS);
648          t.interrupt();
649          t.join();
650 +        assertEquals(q.remainingCapacity(), 0);
651      }
652  
653      /**
# Line 641 | Line 655 | public class LinkedBlockingDequeTest ext
655       */
656      public void testTimedOffer() throws InterruptedException {
657          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
658 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
658 >        Thread t = new Thread(new CheckedRunnable() {
659              public void realRun() throws InterruptedException {
660                  q.put(new Object());
661                  q.put(new Object());
662 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
663 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
664 <            }};
662 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
663 >                try {
664 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
665 >                    shouldThrow();
666 >                } catch (InterruptedException success) {}
667 >            }});
668  
669          t.start();
670          Thread.sleep(SMALL_DELAY_MS);
# Line 661 | Line 678 | public class LinkedBlockingDequeTest ext
678      public void testTake() throws InterruptedException {
679          LinkedBlockingDeque q = populatedDeque(SIZE);
680          for (int i = 0; i < SIZE; ++i) {
681 <            assertEquals(i, ((Integer)q.take()).intValue());
681 >            assertEquals(i, q.take());
682          }
683      }
684  
685      /**
669     * take blocks interruptibly when empty
670     */
671    public void testTakeFromEmpty() throws InterruptedException {
672        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
673        Thread t = new ThreadShouldThrow(InterruptedException.class) {
674            public void realRun() throws InterruptedException {
675                q.take();
676            }};
677
678        t.start();
679        Thread.sleep(SHORT_DELAY_MS);
680        t.interrupt();
681        t.join();
682    }
683
684    /**
686       * Take removes existing elements until empty, then blocks interruptibly
687       */
688      public void testBlockingTake() throws InterruptedException {
689 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
689 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
690 >        Thread t = new Thread(new CheckedRunnable() {
691              public void realRun() throws InterruptedException {
690                LinkedBlockingDeque q = populatedDeque(SIZE);
692                  for (int i = 0; i < SIZE; ++i) {
693 <                    assertEquals(i, ((Integer)q.take()).intValue());
693 >                    assertEquals(i, q.take());
694                  }
695 <                q.take();
696 <            }};
695 >                try {
696 >                    q.take();
697 >                    shouldThrow();
698 >                } catch (InterruptedException success) {}
699 >            }});
700  
701          t.start();
702          Thread.sleep(SHORT_DELAY_MS);
# Line 707 | Line 711 | public class LinkedBlockingDequeTest ext
711      public void testPoll() {
712          LinkedBlockingDeque q = populatedDeque(SIZE);
713          for (int i = 0; i < SIZE; ++i) {
714 <            assertEquals(i, ((Integer)q.poll()).intValue());
714 >            assertEquals(i, q.poll());
715          }
716          assertNull(q.poll());
717      }
# Line 718 | Line 722 | public class LinkedBlockingDequeTest ext
722      public void testTimedPoll0() throws InterruptedException {
723          LinkedBlockingDeque q = populatedDeque(SIZE);
724          for (int i = 0; i < SIZE; ++i) {
725 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
725 >            assertEquals(i, q.poll(0, MILLISECONDS));
726          }
727          assertNull(q.poll(0, MILLISECONDS));
728      }
# Line 729 | Line 733 | public class LinkedBlockingDequeTest ext
733      public void testTimedPoll() throws InterruptedException {
734          LinkedBlockingDeque q = populatedDeque(SIZE);
735          for (int i = 0; i < SIZE; ++i) {
736 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
736 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
737          }
738          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
739      }
# Line 743 | Line 747 | public class LinkedBlockingDequeTest ext
747              public void realRun() throws InterruptedException {
748                  LinkedBlockingDeque q = populatedDeque(SIZE);
749                  for (int i = 0; i < SIZE; ++i) {
750 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
750 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
751                  }
752                  try {
753                      q.poll(SMALL_DELAY_MS, MILLISECONDS);
# Line 758 | Line 762 | public class LinkedBlockingDequeTest ext
762      }
763  
764      /**
761     *  timed poll before a delayed offer fails; after offer succeeds;
762     *  on interruption throws
763     */
764    public void testTimedPollWithOffer() throws InterruptedException {
765        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
766        Thread t = new Thread(new CheckedRunnable() {
767            public void realRun() throws InterruptedException {
768                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
769                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
770                try {
771                    q.poll(LONG_DELAY_MS, MILLISECONDS);
772                    shouldThrow();
773                } catch (InterruptedException success) {}
774            }});
775
776        t.start();
777        Thread.sleep(SMALL_DELAY_MS);
778        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
779        t.interrupt();
780        t.join();
781    }
782
783
784    /**
765       * putFirst(null) throws NPE
766       */
767       public void testPutFirstNull() throws InterruptedException {
# Line 809 | Line 789 | public class LinkedBlockingDequeTest ext
789       * putFirst blocks interruptibly if full
790       */
791      public void testBlockingPutFirst() throws InterruptedException {
792 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
793          Thread t = new Thread(new CheckedRunnable() {
794 <            public void realRun() {
795 <                int added = 0;
794 >            public void realRun() throws InterruptedException {
795 >                for (int i = 0; i < SIZE; ++i)
796 >                    q.putFirst(i);
797 >                assertEquals(SIZE, q.size());
798 >                assertEquals(0, q.remainingCapacity());
799                  try {
800 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
801 <                    for (int i = 0; i < SIZE; ++i) {
802 <                        q.putFirst(new Integer(i));
819 <                        ++added;
820 <                    }
821 <                    q.putFirst(new Integer(SIZE));
822 <                    threadShouldThrow();
823 <                } catch (InterruptedException success) {
824 <                    threadAssertEquals(added, SIZE);
825 <                }
800 >                    q.putFirst(99);
801 >                    shouldThrow();
802 >                } catch (InterruptedException success) {}
803              }});
804  
805          t.start();
806          Thread.sleep(SHORT_DELAY_MS);
807          t.interrupt();
808          t.join();
809 +        assertEquals(SIZE, q.size());
810 +        assertEquals(0, q.remainingCapacity());
811      }
812  
813      /**
814       * putFirst blocks waiting for take when full
815       */
816      public void testPutFirstWithTake() throws InterruptedException {
817 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
817 >        final int capacity = 2;
818 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
819          Thread t = new Thread(new CheckedRunnable() {
820 <            public void realRun() {
821 <                int added = 0;
820 >            public void realRun() throws InterruptedException {
821 >                for (int i = 0; i < capacity + 1; i++)
822 >                    q.putFirst(i);
823                  try {
824 <                    q.putFirst(new Object());
825 <                    ++added;
826 <                    q.putFirst(new Object());
846 <                    ++added;
847 <                    q.putFirst(new Object());
848 <                    ++added;
849 <                    q.putFirst(new Object());
850 <                    ++added;
851 <                    threadShouldThrow();
852 <                } catch (InterruptedException success) {
853 <                    threadAssertTrue(added >= 2);
854 <                }
824 >                    q.putFirst(99);
825 >                    shouldThrow();
826 >                } catch (InterruptedException success) {}
827              }});
828  
829          t.start();
830          Thread.sleep(SHORT_DELAY_MS);
831 <        q.take();
831 >        assertEquals(q.remainingCapacity(), 0);
832 >        assertEquals(capacity - 1, q.take());
833 >        Thread.sleep(SHORT_DELAY_MS);
834          t.interrupt();
835          t.join();
836 +        assertEquals(q.remainingCapacity(), 0);
837      }
838  
839      /**
# Line 866 | Line 841 | public class LinkedBlockingDequeTest ext
841       */
842      public void testTimedOfferFirst() throws InterruptedException {
843          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
844 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
844 >        Thread t = new Thread(new CheckedRunnable() {
845              public void realRun() throws InterruptedException {
846                  q.putFirst(new Object());
847                  q.putFirst(new Object());
848 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
849 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
850 <            }};
848 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
849 >                try {
850 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
851 >                    shouldThrow();
852 >                } catch (InterruptedException success) {}
853 >            }});
854  
855          t.start();
856          Thread.sleep(SMALL_DELAY_MS);
# Line 886 | Line 864 | public class LinkedBlockingDequeTest ext
864      public void testTakeFirst() throws InterruptedException {
865          LinkedBlockingDeque q = populatedDeque(SIZE);
866          for (int i = 0; i < SIZE; ++i) {
867 <            assertEquals(i, ((Integer)q.takeFirst()).intValue());
867 >            assertEquals(i, q.takeFirst());
868          }
869      }
870  
# Line 910 | Line 888 | public class LinkedBlockingDequeTest ext
888       * TakeFirst removes existing elements until empty, then blocks interruptibly
889       */
890      public void testBlockingTakeFirst() throws InterruptedException {
891 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
891 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
892 >        Thread t = new Thread(new CheckedRunnable() {
893              public void realRun() throws InterruptedException {
894 <                LinkedBlockingDeque q = populatedDeque(SIZE);
895 <                for (int i = 0; i < SIZE; ++i) {
896 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
897 <                }
898 <                q.takeFirst();
899 <            }};
894 >                for (int i = 0; i < SIZE; ++i)
895 >                    assertEquals(i, q.takeFirst());
896 >                try {
897 >                    q.takeFirst();
898 >                    shouldThrow();
899 >                } catch (InterruptedException success) {}
900 >            }});
901  
902          t.start();
903          Thread.sleep(SHORT_DELAY_MS);
# Line 932 | Line 912 | public class LinkedBlockingDequeTest ext
912      public void testTimedPollFirst0() throws InterruptedException {
913          LinkedBlockingDeque q = populatedDeque(SIZE);
914          for (int i = 0; i < SIZE; ++i) {
915 <            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
915 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
916          }
917          assertNull(q.pollFirst(0, MILLISECONDS));
918      }
# Line 943 | Line 923 | public class LinkedBlockingDequeTest ext
923      public void testTimedPollFirst() throws InterruptedException {
924          LinkedBlockingDeque q = populatedDeque(SIZE);
925          for (int i = 0; i < SIZE; ++i) {
926 <            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
926 >            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
927          }
928          assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
929      }
# Line 957 | Line 937 | public class LinkedBlockingDequeTest ext
937              public void realRun() throws InterruptedException {
938                  LinkedBlockingDeque q = populatedDeque(SIZE);
939                  for (int i = 0; i < SIZE; ++i) {
940 <                    assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
940 >                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
941                  }
942                  try {
943                      q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
# Line 972 | Line 952 | public class LinkedBlockingDequeTest ext
952      }
953  
954      /**
955 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
956 <     *  on interruption throws
955 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
956 >     * on interruption throws
957       */
958      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
959          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 1022 | Line 1002 | public class LinkedBlockingDequeTest ext
1002       * putLast blocks interruptibly if full
1003       */
1004      public void testBlockingPutLast() throws InterruptedException {
1005 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1006          Thread t = new Thread(new CheckedRunnable() {
1007 <            public void realRun() {
1008 <                int added = 0;
1007 >            public void realRun() throws InterruptedException {
1008 >                for (int i = 0; i < SIZE; ++i)
1009 >                    q.putLast(i);
1010 >                assertEquals(SIZE, q.size());
1011 >                assertEquals(0, q.remainingCapacity());
1012                  try {
1013 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1014 <                    for (int i = 0; i < SIZE; ++i) {
1015 <                        q.putLast(new Integer(i));
1032 <                        ++added;
1033 <                    }
1034 <                    q.putLast(new Integer(SIZE));
1035 <                    threadShouldThrow();
1036 <                } catch (InterruptedException success) {
1037 <                    threadAssertEquals(added, SIZE);
1038 <                }
1013 >                    q.putLast(99);
1014 >                    shouldThrow();
1015 >                } catch (InterruptedException success) {}
1016              }});
1017  
1018          t.start();
1019          Thread.sleep(SHORT_DELAY_MS);
1020          t.interrupt();
1021          t.join();
1022 +        assertEquals(SIZE, q.size());
1023 +        assertEquals(0, q.remainingCapacity());
1024      }
1025  
1026      /**
1027       * putLast blocks waiting for take when full
1028       */
1029      public void testPutLastWithTake() throws InterruptedException {
1030 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1030 >        final int capacity = 2;
1031 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1032          Thread t = new Thread(new CheckedRunnable() {
1033 <            public void realRun() {
1034 <                int added = 0;
1033 >            public void realRun() throws InterruptedException {
1034 >                for (int i = 0; i < capacity + 1; i++)
1035 >                    q.putLast(i);
1036                  try {
1037 <                    q.putLast(new Object());
1038 <                    ++added;
1039 <                    q.putLast(new Object());
1059 <                    ++added;
1060 <                    q.putLast(new Object());
1061 <                    ++added;
1062 <                    q.putLast(new Object());
1063 <                    ++added;
1064 <                    threadShouldThrow();
1065 <                } catch (InterruptedException success) {
1066 <                    threadAssertTrue(added >= 2);
1067 <                }
1037 >                    q.putLast(99);
1038 >                    shouldThrow();
1039 >                } catch (InterruptedException success) {}
1040              }});
1041  
1042          t.start();
1043          Thread.sleep(SHORT_DELAY_MS);
1044 <        q.take();
1044 >        assertEquals(q.remainingCapacity(), 0);
1045 >        assertEquals(0, q.take());
1046 >        Thread.sleep(SHORT_DELAY_MS);
1047          t.interrupt();
1048          t.join();
1049 +        assertEquals(q.remainingCapacity(), 0);
1050      }
1051  
1052      /**
# Line 1079 | Line 1054 | public class LinkedBlockingDequeTest ext
1054       */
1055      public void testTimedOfferLast() throws InterruptedException {
1056          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1057 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1057 >        Thread t = new Thread(new CheckedRunnable() {
1058              public void realRun() throws InterruptedException {
1059                  q.putLast(new Object());
1060                  q.putLast(new Object());
1061 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1062 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1063 <            }};
1061 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1062 >                try {
1063 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1064 >                    shouldThrow();
1065 >                } catch (InterruptedException success) {}
1066 >            }});
1067  
1068          t.start();
1069          Thread.sleep(SMALL_DELAY_MS);
# Line 1099 | Line 1077 | public class LinkedBlockingDequeTest ext
1077      public void testTakeLast() throws InterruptedException {
1078          LinkedBlockingDeque q = populatedDeque(SIZE);
1079          for (int i = 0; i < SIZE; ++i) {
1080 <            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1080 >            assertEquals(SIZE-i-1, q.takeLast());
1081          }
1082      }
1083  
# Line 1123 | Line 1101 | public class LinkedBlockingDequeTest ext
1101       * TakeLast removes existing elements until empty, then blocks interruptibly
1102       */
1103      public void testBlockingTakeLast() throws InterruptedException {
1104 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1104 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1105 >        Thread t = new Thread(new CheckedRunnable() {
1106              public void realRun() throws InterruptedException {
1107 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1108 <                for (int i = 0; i < SIZE; ++i) {
1109 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1110 <                }
1111 <                q.takeLast();
1112 <            }};
1107 >                for (int i = 0; i < SIZE; ++i)
1108 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1109 >                try {
1110 >                    q.takeLast();
1111 >                    shouldThrow();
1112 >                } catch (InterruptedException success) {}
1113 >            }});
1114  
1115          t.start();
1116          Thread.sleep(SHORT_DELAY_MS);
# Line 1138 | Line 1118 | public class LinkedBlockingDequeTest ext
1118          t.join();
1119      }
1120  
1141
1121      /**
1122       * timed pollLast with zero timeout succeeds when non-empty, else times out
1123       */
1124      public void testTimedPollLast0() throws InterruptedException {
1125          LinkedBlockingDeque q = populatedDeque(SIZE);
1126          for (int i = 0; i < SIZE; ++i) {
1127 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1127 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1128          }
1129          assertNull(q.pollLast(0, MILLISECONDS));
1130      }
# Line 1156 | Line 1135 | public class LinkedBlockingDequeTest ext
1135      public void testTimedPollLast() throws InterruptedException {
1136          LinkedBlockingDeque q = populatedDeque(SIZE);
1137          for (int i = 0; i < SIZE; ++i) {
1138 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1138 >            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1139          }
1140          assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1141      }
# Line 1170 | Line 1149 | public class LinkedBlockingDequeTest ext
1149              public void realRun() throws InterruptedException {
1150                  LinkedBlockingDeque q = populatedDeque(SIZE);
1151                  for (int i = 0; i < SIZE; ++i) {
1152 <                    assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1152 >                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1153                  }
1154                  try {
1155                      q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
# Line 1185 | Line 1164 | public class LinkedBlockingDequeTest ext
1164      }
1165  
1166      /**
1167 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1168 <     *  on interruption throws
1167 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1168 >     * on interruption throws
1169       */
1170      public void testTimedPollWithOfferLast() throws InterruptedException {
1171          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 1214 | Line 1193 | public class LinkedBlockingDequeTest ext
1193      public void testElement() {
1194          LinkedBlockingDeque q = populatedDeque(SIZE);
1195          for (int i = 0; i < SIZE; ++i) {
1196 <            assertEquals(i, ((Integer)q.element()).intValue());
1196 >            assertEquals(i, q.element());
1197              q.poll();
1198          }
1199          try {
# Line 1316 | Line 1295 | public class LinkedBlockingDequeTest ext
1295      }
1296  
1297      /**
1298 <     * toArray contains all elements
1298 >     * toArray contains all elements in FIFO order
1299       */
1300      public void testToArray() throws InterruptedException{
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          Object[] o = q.toArray();
1303          for (int i = 0; i < o.length; i++)
1304 <            assertEquals(o[i], q.take());
1304 >            assertSame(o[i], q.poll());
1305      }
1306  
1307      /**
1308 <     * toArray(a) contains all elements
1308 >     * toArray(a) contains all elements in FIFO order
1309       */
1310 <    public void testToArray2() throws InterruptedException {
1311 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1310 >    public void testToArray2() {
1311 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1312          Integer[] ints = new Integer[SIZE];
1313 <        ints = (Integer[])q.toArray(ints);
1313 >        Integer[] array = q.toArray(ints);
1314 >        assertSame(ints, array);
1315          for (int i = 0; i < ints.length; i++)
1316 <            assertEquals(ints[i], q.take());
1316 >            assertSame(ints[i], q.remove());
1317      }
1318  
1319      /**
1320 <     * toArray(null) throws NPE
1320 >     * toArray(null) throws NullPointerException
1321       */
1322 <    public void testToArray_BadArg() {
1322 >    public void testToArray_NullArg() {
1323 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1324          try {
1325 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1345 <            Object o[] = q.toArray(null);
1325 >            q.toArray(null);
1326              shouldThrow();
1327          } catch (NullPointerException success) {}
1328      }
1329  
1330      /**
1331 <     * toArray with incompatible array type throws CCE
1331 >     * toArray(incompatible array type) throws ArrayStoreException
1332       */
1333      public void testToArray1_BadArg() {
1334 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1335          try {
1336 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1356 <            Object o[] = q.toArray(new String[10] );
1336 >            q.toArray(new String[10]);
1337              shouldThrow();
1338          } catch (ArrayStoreException success) {}
1339      }
# Line 1373 | Line 1353 | public class LinkedBlockingDequeTest ext
1353      /**
1354       * iterator.remove removes current element
1355       */
1356 <    public void testIteratorRemove () {
1356 >    public void testIteratorRemove() {
1357          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1358          q.add(two);
1359          q.add(one);
# Line 1384 | Line 1364 | public class LinkedBlockingDequeTest ext
1364          it.remove();
1365  
1366          it = q.iterator();
1367 <        assertEquals(it.next(), one);
1368 <        assertEquals(it.next(), three);
1367 >        assertSame(it.next(), one);
1368 >        assertSame(it.next(), three);
1369          assertFalse(it.hasNext());
1370      }
1371  
# Line 1401 | Line 1381 | public class LinkedBlockingDequeTest ext
1381          assertEquals(0, q.remainingCapacity());
1382          int k = 0;
1383          for (Iterator it = q.iterator(); it.hasNext();) {
1384 <            int i = ((Integer)(it.next())).intValue();
1405 <            assertEquals(++k, i);
1384 >            assertEquals(++k, it.next());
1385          }
1386          assertEquals(3, k);
1387      }
# Line 1410 | Line 1389 | public class LinkedBlockingDequeTest ext
1389      /**
1390       * Modifications do not cause iterators to fail
1391       */
1392 <    public void testWeaklyConsistentIteration () {
1392 >    public void testWeaklyConsistentIteration() {
1393          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1394          q.add(one);
1395          q.add(two);
# Line 1424 | Line 1403 | public class LinkedBlockingDequeTest ext
1403  
1404  
1405      /**
1406 <     *  Descending iterator iterates through all elements
1406 >     * Descending iterator iterates through all elements
1407       */
1408      public void testDescendingIterator() {
1409          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 1443 | Line 1422 | public class LinkedBlockingDequeTest ext
1422      }
1423  
1424      /**
1425 <     *  Descending iterator ordering is reverse FIFO
1425 >     * Descending iterator ordering is reverse FIFO
1426       */
1427      public void testDescendingIteratorOrdering() {
1428          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1453 | Line 1432 | public class LinkedBlockingDequeTest ext
1432              q.add(new Integer(1));
1433              int k = 0;
1434              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1435 <                int i = ((Integer)(it.next())).intValue();
1457 <                assertEquals(++k, i);
1435 >                assertEquals(++k, it.next());
1436              }
1437  
1438              assertEquals(3, k);
# Line 1467 | Line 1445 | public class LinkedBlockingDequeTest ext
1445      /**
1446       * descendingIterator.remove removes current element
1447       */
1448 <    public void testDescendingIteratorRemove () {
1448 >    public void testDescendingIteratorRemove() {
1449          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1450          for (int iters = 0; iters < 100; ++iters) {
1451              q.add(new Integer(3));
# Line 1509 | Line 1487 | public class LinkedBlockingDequeTest ext
1487          ExecutorService executor = Executors.newFixedThreadPool(2);
1488          executor.execute(new CheckedRunnable() {
1489              public void realRun() throws InterruptedException {
1490 <                threadAssertFalse(q.offer(three));
1491 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1492 <                threadAssertEquals(0, q.remainingCapacity());
1490 >                assertFalse(q.offer(three));
1491 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1492 >                assertEquals(0, q.remainingCapacity());
1493              }});
1494  
1495          executor.execute(new CheckedRunnable() {
1496              public void realRun() throws InterruptedException {
1497                  Thread.sleep(SMALL_DELAY_MS);
1498 <                threadAssertEquals(one, q.take());
1498 >                assertSame(one, q.take());
1499              }});
1500  
1501          joinPool(executor);
# Line 1531 | Line 1509 | public class LinkedBlockingDequeTest ext
1509          ExecutorService executor = Executors.newFixedThreadPool(2);
1510          executor.execute(new CheckedRunnable() {
1511              public void realRun() throws InterruptedException {
1512 <                threadAssertNull(q.poll());
1513 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1514 <                threadAssertTrue(q.isEmpty());
1512 >                assertNull(q.poll());
1513 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1514 >                assertTrue(q.isEmpty());
1515              }});
1516  
1517          executor.execute(new CheckedRunnable() {
# Line 1653 | Line 1631 | public class LinkedBlockingDequeTest ext
1631      }
1632  
1633      /**
1634 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1634 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1635       */
1636      public void testDrainToN() {
1637          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1662 | Line 1640 | public class LinkedBlockingDequeTest ext
1640                  assertTrue(q.offer(new Integer(j)));
1641              ArrayList l = new ArrayList();
1642              q.drainTo(l, i);
1643 <            int k = (i < SIZE)? i : SIZE;
1643 >            int k = (i < SIZE) ? i : SIZE;
1644              assertEquals(l.size(), k);
1645              assertEquals(q.size(), SIZE-k);
1646              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines