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.11 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.21 by jsr166, Wed Aug 25 00:07:03 2010 UTC

# Line 12 | Line 12 | import java.io.*;
12  
13   public class LinkedBlockingDequeTest extends JSR166TestCase {
14      public static void main(String[] args) {
15 <        junit.textui.TestRunner.run (suite());
15 >        junit.textui.TestRunner.run(suite());
16      }
17  
18      public static Test suite() {
# Line 98 | Line 98 | public class LinkedBlockingDequeTest ext
98      public void testPollFirst() {
99          LinkedBlockingDeque q = populatedDeque(SIZE);
100          for (int i = 0; i < SIZE; ++i) {
101 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
101 >            assertEquals(i, q.pollFirst());
102          }
103          assertNull(q.pollFirst());
104      }
# Line 109 | Line 109 | public class LinkedBlockingDequeTest ext
109      public void testPollLast() {
110          LinkedBlockingDeque q = populatedDeque(SIZE);
111          for (int i = SIZE-1; i >= 0; --i) {
112 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
112 >            assertEquals(i, q.pollLast());
113          }
114          assertNull(q.pollLast());
115      }
# Line 120 | Line 120 | public class LinkedBlockingDequeTest ext
120      public void testPeekFirst() {
121          LinkedBlockingDeque q = populatedDeque(SIZE);
122          for (int i = 0; i < SIZE; ++i) {
123 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
124 <            q.pollFirst();
123 >            assertEquals(i, q.peekFirst());
124 >            assertEquals(i, q.pollFirst());
125              assertTrue(q.peekFirst() == null ||
126 <                       i != ((Integer)q.peekFirst()).intValue());
126 >                       !q.peekFirst().equals(i));
127          }
128          assertNull(q.peekFirst());
129      }
# Line 134 | Line 134 | public class LinkedBlockingDequeTest ext
134      public void testPeek() {
135          LinkedBlockingDeque q = populatedDeque(SIZE);
136          for (int i = 0; i < SIZE; ++i) {
137 <            assertEquals(i, ((Integer)q.peek()).intValue());
138 <            q.pollFirst();
137 >            assertEquals(i, q.peek());
138 >            assertEquals(i, q.pollFirst());
139              assertTrue(q.peek() == null ||
140 <                       i != ((Integer)q.peek()).intValue());
140 >                       !q.peek().equals(i));
141          }
142          assertNull(q.peek());
143      }
# Line 148 | Line 148 | public class LinkedBlockingDequeTest ext
148      public void testPeekLast() {
149          LinkedBlockingDeque q = populatedDeque(SIZE);
150          for (int i = SIZE-1; i >= 0; --i) {
151 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
152 <            q.pollLast();
151 >            assertEquals(i, q.peekLast());
152 >            assertEquals(i, q.pollLast());
153              assertTrue(q.peekLast() == null ||
154 <                       i != ((Integer)q.peekLast()).intValue());
154 >                       !q.peekLast().equals(i));
155          }
156          assertNull(q.peekLast());
157      }
# Line 162 | Line 162 | public class LinkedBlockingDequeTest ext
162      public void testFirstElement() {
163          LinkedBlockingDeque q = populatedDeque(SIZE);
164          for (int i = 0; i < SIZE; ++i) {
165 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
166 <            q.pollFirst();
165 >            assertEquals(i, q.getFirst());
166 >            assertEquals(i, q.pollFirst());
167          }
168          try {
169              q.getFirst();
170              shouldThrow();
171          } catch (NoSuchElementException success) {}
172 +        assertNull(q.peekFirst());
173      }
174  
175      /**
# Line 177 | Line 178 | public class LinkedBlockingDequeTest ext
178      public void testLastElement() {
179          LinkedBlockingDeque q = populatedDeque(SIZE);
180          for (int i = SIZE-1; i >= 0; --i) {
181 <            assertEquals(i, ((Integer)q.getLast()).intValue());
182 <            q.pollLast();
181 >            assertEquals(i, q.getLast());
182 >            assertEquals(i, q.pollLast());
183          }
184          try {
185              q.getLast();
# Line 193 | Line 194 | public class LinkedBlockingDequeTest ext
194      public void testRemoveFirst() {
195          LinkedBlockingDeque q = populatedDeque(SIZE);
196          for (int i = 0; i < SIZE; ++i) {
197 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
197 >            assertEquals(i, q.removeFirst());
198          }
199          try {
200              q.removeFirst();
201              shouldThrow();
202          } catch (NoSuchElementException success) {}
203 +        assertNull(q.peekFirst());
204 +    }
205 +
206 +    /**
207 +     *  removeLast removes last element, or throws NSEE if empty
208 +     */
209 +    public void testRemoveLast() {
210 +        LinkedBlockingDeque q = populatedDeque(SIZE);
211 +        for (int i = SIZE - 1; i >= 0; --i) {
212 +            assertEquals(i, q.removeLast());
213 +        }
214 +        try {
215 +            q.removeLast();
216 +            shouldThrow();
217 +        } catch (NoSuchElementException success) {}
218 +        assertNull(q.peekLast());
219      }
220  
221      /**
# Line 207 | Line 224 | public class LinkedBlockingDequeTest ext
224      public void testRemove() {
225          LinkedBlockingDeque q = populatedDeque(SIZE);
226          for (int i = 0; i < SIZE; ++i) {
227 <            assertEquals(i, ((Integer)q.remove()).intValue());
227 >            assertEquals(i, q.remove());
228          }
229          try {
230              q.remove();
# Line 252 | Line 269 | public class LinkedBlockingDequeTest ext
269          LinkedBlockingDeque q = populatedDeque(3);
270          q.pollLast();
271          q.addFirst(four);
272 <        assertEquals(four,q.peekFirst());
272 >        assertSame(four, q.peekFirst());
273      }
274  
275      /**
# Line 262 | Line 279 | public class LinkedBlockingDequeTest ext
279          LinkedBlockingDeque q = populatedDeque(3);
280          q.pollLast();
281          q.addLast(four);
282 <        assertEquals(four,q.peekLast());
282 >        assertSame(four, q.peekLast());
283      }
284  
285  
# Line 323 | Line 340 | public class LinkedBlockingDequeTest ext
340       * Deque contains all elements of collection used to initialize
341       */
342      public void testConstructor6() {
343 <        try {
344 <            Integer[] ints = new Integer[SIZE];
345 <            for (int i = 0; i < SIZE; ++i)
346 <                ints[i] = new Integer(i);
347 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
348 <            for (int i = 0; i < SIZE; ++i)
332 <                assertEquals(ints[i], q.poll());
333 <        }
334 <        finally {}
343 >        Integer[] ints = new Integer[SIZE];
344 >        for (int i = 0; i < SIZE; ++i)
345 >            ints[i] = new Integer(i);
346 >        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
347 >        for (int i = 0; i < SIZE; ++i)
348 >            assertEquals(ints[i], q.poll());
349      }
350  
351      /**
# Line 423 | Line 437 | public class LinkedBlockingDequeTest ext
437          LinkedBlockingDeque q = populatedDeque(3);
438          q.pollLast();
439          q.push(four);
440 <        assertEquals(four,q.peekFirst());
440 >        assertSame(four, q.peekFirst());
441      }
442  
443  
# Line 433 | Line 447 | public class LinkedBlockingDequeTest ext
447      public void testPop() {
448          LinkedBlockingDeque q = populatedDeque(SIZE);
449          for (int i = 0; i < SIZE; ++i) {
450 <            assertEquals(i, ((Integer)q.pop()).intValue());
450 >            assertEquals(i, q.pop());
451          }
452          try {
453              q.pop();
# Line 526 | Line 540 | public class LinkedBlockingDequeTest ext
540              shouldThrow();
541          } catch (IllegalStateException success) {}
542      }
543 +
544      /**
545       * Deque contains all elements, in traversal order, of successful addAll
546       */
547      public void testAddAll5() {
548 <        try {
549 <            Integer[] empty = new Integer[0];
550 <            Integer[] ints = new Integer[SIZE];
551 <            for (int i = 0; i < SIZE; ++i)
552 <                ints[i] = new Integer(i);
553 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
554 <            assertFalse(q.addAll(Arrays.asList(empty)));
555 <            assertTrue(q.addAll(Arrays.asList(ints)));
556 <            for (int i = 0; i < SIZE; ++i)
542 <                assertEquals(ints[i], q.poll());
543 <        }
544 <        finally {}
548 >        Integer[] empty = new Integer[0];
549 >        Integer[] ints = new Integer[SIZE];
550 >        for (int i = 0; i < SIZE; ++i)
551 >            ints[i] = new Integer(i);
552 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
553 >        assertFalse(q.addAll(Arrays.asList(empty)));
554 >        assertTrue(q.addAll(Arrays.asList(ints)));
555 >        for (int i = 0; i < SIZE; ++i)
556 >            assertEquals(ints[i], q.poll());
557      }
558  
559  
# Line 573 | Line 585 | public class LinkedBlockingDequeTest ext
585       * put blocks interruptibly if full
586       */
587      public void testBlockingPut() throws InterruptedException {
588 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
589          Thread t = new Thread(new CheckedRunnable() {
590 <            public void realRun() {
591 <                int added = 0;
590 >            public void realRun() throws InterruptedException {
591 >                for (int i = 0; i < SIZE; ++i)
592 >                    q.put(i);
593 >                assertEquals(SIZE, q.size());
594 >                assertEquals(0, q.remainingCapacity());
595                  try {
596 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
597 <                    for (int i = 0; i < SIZE; ++i) {
598 <                        q.put(new Integer(i));
583 <                        ++added;
584 <                    }
585 <                    q.put(new Integer(SIZE));
586 <                    threadShouldThrow();
587 <                } catch (InterruptedException success) {
588 <                    threadAssertEquals(added, SIZE);
589 <                }
596 >                    q.put(99);
597 >                    shouldThrow();
598 >                } catch (InterruptedException success) {}
599              }});
600  
601          t.start();
602          Thread.sleep(SHORT_DELAY_MS);
603          t.interrupt();
604          t.join();
605 +        assertEquals(SIZE, q.size());
606 +        assertEquals(0, q.remainingCapacity());
607      }
608  
609      /**
610       * put blocks waiting for take when full
611       */
612      public void testPutWithTake() throws InterruptedException {
613 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
613 >        final int capacity = 2;
614 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
615          Thread t = new Thread(new CheckedRunnable() {
616 <            public void realRun() {
617 <                int added = 0;
616 >            public void realRun() throws InterruptedException {
617 >                for (int i = 0; i < capacity + 1; i++)
618 >                    q.put(i);
619                  try {
620 <                    q.put(new Object());
621 <                    ++added;
622 <                    q.put(new Object());
610 <                    ++added;
611 <                    q.put(new Object());
612 <                    ++added;
613 <                    q.put(new Object());
614 <                    ++added;
615 <                    threadShouldThrow();
616 <                } catch (InterruptedException success) {
617 <                    threadAssertTrue(added >= 2);
618 <                }
620 >                    q.put(99);
621 >                    shouldThrow();
622 >                } catch (InterruptedException success) {}
623              }});
624  
625          t.start();
626          Thread.sleep(SHORT_DELAY_MS);
627 <        q.take();
627 >        assertEquals(q.remainingCapacity(), 0);
628 >        assertEquals(0, q.take());
629 >        Thread.sleep(SHORT_DELAY_MS);
630          t.interrupt();
631          t.join();
632 +        assertEquals(q.remainingCapacity(), 0);
633      }
634  
635      /**
# Line 630 | Line 637 | public class LinkedBlockingDequeTest ext
637       */
638      public void testTimedOffer() throws InterruptedException {
639          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
640 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
640 >        Thread t = new Thread(new CheckedRunnable() {
641              public void realRun() throws InterruptedException {
642                  q.put(new Object());
643                  q.put(new Object());
644 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
645 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
646 <            }};
644 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
645 >                try {
646 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
647 >                    shouldThrow();
648 >                } catch (InterruptedException success) {}
649 >            }});
650  
651          t.start();
652          Thread.sleep(SMALL_DELAY_MS);
# Line 650 | Line 660 | public class LinkedBlockingDequeTest ext
660      public void testTake() throws InterruptedException {
661          LinkedBlockingDeque q = populatedDeque(SIZE);
662          for (int i = 0; i < SIZE; ++i) {
663 <            assertEquals(i, ((Integer)q.take()).intValue());
663 >            assertEquals(i, q.take());
664          }
665      }
666  
# Line 674 | Line 684 | public class LinkedBlockingDequeTest ext
684       * Take removes existing elements until empty, then blocks interruptibly
685       */
686      public void testBlockingTake() throws InterruptedException {
687 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
687 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
688 >        Thread t = new Thread(new CheckedRunnable() {
689              public void realRun() throws InterruptedException {
679                LinkedBlockingDeque q = populatedDeque(SIZE);
690                  for (int i = 0; i < SIZE; ++i) {
691 <                    assertEquals(i, ((Integer)q.take()).intValue());
691 >                    assertEquals(i, q.take());
692                  }
693 <                q.take();
694 <            }};
693 >                try {
694 >                    q.take();
695 >                    shouldThrow();
696 >                } catch (InterruptedException success) {}
697 >            }});
698  
699          t.start();
700          Thread.sleep(SHORT_DELAY_MS);
# Line 696 | Line 709 | public class LinkedBlockingDequeTest ext
709      public void testPoll() {
710          LinkedBlockingDeque q = populatedDeque(SIZE);
711          for (int i = 0; i < SIZE; ++i) {
712 <            assertEquals(i, ((Integer)q.poll()).intValue());
712 >            assertEquals(i, q.poll());
713          }
714          assertNull(q.poll());
715      }
# Line 707 | Line 720 | public class LinkedBlockingDequeTest ext
720      public void testTimedPoll0() throws InterruptedException {
721          LinkedBlockingDeque q = populatedDeque(SIZE);
722          for (int i = 0; i < SIZE; ++i) {
723 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
723 >            assertEquals(i, q.poll(0, MILLISECONDS));
724          }
725          assertNull(q.poll(0, MILLISECONDS));
726      }
# Line 718 | Line 731 | public class LinkedBlockingDequeTest ext
731      public void testTimedPoll() throws InterruptedException {
732          LinkedBlockingDeque q = populatedDeque(SIZE);
733          for (int i = 0; i < SIZE; ++i) {
734 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
734 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
735          }
736          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
737      }
# Line 728 | Line 741 | public class LinkedBlockingDequeTest ext
741       * returning timeout status
742       */
743      public void testInterruptedTimedPoll() throws InterruptedException {
744 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
744 >        Thread t = new Thread(new CheckedRunnable() {
745              public void realRun() throws InterruptedException {
746                  LinkedBlockingDeque q = populatedDeque(SIZE);
747                  for (int i = 0; i < SIZE; ++i) {
748 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
748 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
749                  }
750 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
751 <            }};
750 >                try {
751 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
752 >                    shouldThrow();
753 >                } catch (InterruptedException success) {}
754 >            }});
755  
756          t.start();
757          Thread.sleep(SHORT_DELAY_MS);
# Line 749 | Line 765 | public class LinkedBlockingDequeTest ext
765       */
766      public void testTimedPollWithOffer() throws InterruptedException {
767          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
768 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
768 >        Thread t = new Thread(new CheckedRunnable() {
769              public void realRun() throws InterruptedException {
770 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
772 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
773 <            }};
770 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
772 >                try {
773 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
774 >                    shouldThrow();
775 >                } catch (InterruptedException success) {}
776 >            }});
777  
778          t.start();
779          Thread.sleep(SMALL_DELAY_MS);
# Line 792 | Line 811 | public class LinkedBlockingDequeTest ext
811       * putFirst blocks interruptibly if full
812       */
813      public void testBlockingPutFirst() throws InterruptedException {
814 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
815          Thread t = new Thread(new CheckedRunnable() {
816 <            public void realRun() {
817 <                int added = 0;
816 >            public void realRun() throws InterruptedException {
817 >                for (int i = 0; i < SIZE; ++i)
818 >                    q.putFirst(i);
819 >                assertEquals(SIZE, q.size());
820 >                assertEquals(0, q.remainingCapacity());
821                  try {
822 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
823 <                    for (int i = 0; i < SIZE; ++i) {
824 <                        q.putFirst(new Integer(i));
802 <                        ++added;
803 <                    }
804 <                    q.putFirst(new Integer(SIZE));
805 <                    threadShouldThrow();
806 <                } catch (InterruptedException success) {
807 <                    threadAssertEquals(added, SIZE);
808 <                }
822 >                    q.putFirst(99);
823 >                    shouldThrow();
824 >                } catch (InterruptedException success) {}
825              }});
826  
827          t.start();
828          Thread.sleep(SHORT_DELAY_MS);
829          t.interrupt();
830          t.join();
831 +        assertEquals(SIZE, q.size());
832 +        assertEquals(0, q.remainingCapacity());
833      }
834  
835      /**
836       * putFirst blocks waiting for take when full
837       */
838      public void testPutFirstWithTake() throws InterruptedException {
839 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
839 >        final int capacity = 2;
840 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
841          Thread t = new Thread(new CheckedRunnable() {
842 <            public void realRun() {
843 <                int added = 0;
842 >            public void realRun() throws InterruptedException {
843 >                for (int i = 0; i < capacity + 1; i++)
844 >                    q.putFirst(i);
845                  try {
846 <                    q.putFirst(new Object());
847 <                    ++added;
848 <                    q.putFirst(new Object());
829 <                    ++added;
830 <                    q.putFirst(new Object());
831 <                    ++added;
832 <                    q.putFirst(new Object());
833 <                    ++added;
834 <                    threadShouldThrow();
835 <                } catch (InterruptedException success) {
836 <                    threadAssertTrue(added >= 2);
837 <                }
846 >                    q.putFirst(99);
847 >                    shouldThrow();
848 >                } catch (InterruptedException success) {}
849              }});
850  
851          t.start();
852          Thread.sleep(SHORT_DELAY_MS);
853 <        q.take();
853 >        assertEquals(q.remainingCapacity(), 0);
854 >        assertEquals(capacity - 1, q.take());
855 >        Thread.sleep(SHORT_DELAY_MS);
856          t.interrupt();
857          t.join();
858 +        assertEquals(q.remainingCapacity(), 0);
859      }
860  
861      /**
# Line 849 | Line 863 | public class LinkedBlockingDequeTest ext
863       */
864      public void testTimedOfferFirst() throws InterruptedException {
865          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
866 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
866 >        Thread t = new Thread(new CheckedRunnable() {
867              public void realRun() throws InterruptedException {
868                  q.putFirst(new Object());
869                  q.putFirst(new Object());
870 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
871 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
872 <            }};
870 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
871 >                try {
872 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
873 >                    shouldThrow();
874 >                } catch (InterruptedException success) {}
875 >            }});
876  
877          t.start();
878          Thread.sleep(SMALL_DELAY_MS);
# Line 869 | Line 886 | public class LinkedBlockingDequeTest ext
886      public void testTakeFirst() throws InterruptedException {
887          LinkedBlockingDeque q = populatedDeque(SIZE);
888          for (int i = 0; i < SIZE; ++i) {
889 <            assertEquals(i, ((Integer)q.takeFirst()).intValue());
889 >            assertEquals(i, q.takeFirst());
890          }
891      }
892  
# Line 893 | Line 910 | public class LinkedBlockingDequeTest ext
910       * TakeFirst removes existing elements until empty, then blocks interruptibly
911       */
912      public void testBlockingTakeFirst() throws InterruptedException {
913 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
913 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
914 >        Thread t = new Thread(new CheckedRunnable() {
915              public void realRun() throws InterruptedException {
916 <                LinkedBlockingDeque q = populatedDeque(SIZE);
917 <                for (int i = 0; i < SIZE; ++i) {
918 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
919 <                }
920 <                q.takeFirst();
921 <            }};
916 >                for (int i = 0; i < SIZE; ++i)
917 >                    assertEquals(i, q.takeFirst());
918 >                try {
919 >                    q.takeFirst();
920 >                    shouldThrow();
921 >                } catch (InterruptedException success) {}
922 >            }});
923  
924          t.start();
925          Thread.sleep(SHORT_DELAY_MS);
# Line 915 | Line 934 | public class LinkedBlockingDequeTest ext
934      public void testTimedPollFirst0() throws InterruptedException {
935          LinkedBlockingDeque q = populatedDeque(SIZE);
936          for (int i = 0; i < SIZE; ++i) {
937 <            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
937 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
938          }
939          assertNull(q.pollFirst(0, MILLISECONDS));
940      }
# Line 926 | Line 945 | public class LinkedBlockingDequeTest ext
945      public void testTimedPollFirst() throws InterruptedException {
946          LinkedBlockingDeque q = populatedDeque(SIZE);
947          for (int i = 0; i < SIZE; ++i) {
948 <            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
948 >            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
949          }
950          assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
951      }
# Line 936 | Line 955 | public class LinkedBlockingDequeTest ext
955       * returning timeout status
956       */
957      public void testInterruptedTimedPollFirst() throws InterruptedException {
958 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
958 >        Thread t = new Thread(new CheckedRunnable() {
959              public void realRun() throws InterruptedException {
960                  LinkedBlockingDeque q = populatedDeque(SIZE);
961                  for (int i = 0; i < SIZE; ++i) {
962 <                    threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
962 >                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
963                  }
964 <                q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
965 <            }};
964 >                try {
965 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
966 >                    shouldThrow();
967 >                } catch (InterruptedException success) {}
968 >            }});
969  
970          t.start();
971          Thread.sleep(SHORT_DELAY_MS);
# Line 957 | Line 979 | public class LinkedBlockingDequeTest ext
979       */
980      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
981          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
982 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
982 >        Thread t = new Thread(new CheckedRunnable() {
983              public void realRun() throws InterruptedException {
984 <                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
985 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
986 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
987 <            }};
984 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
985 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
986 >                try {
987 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
988 >                    shouldThrow();
989 >                } catch (InterruptedException success) {}
990 >            }});
991  
992          t.start();
993          Thread.sleep(SMALL_DELAY_MS);
# Line 999 | Line 1024 | public class LinkedBlockingDequeTest ext
1024       * putLast blocks interruptibly if full
1025       */
1026      public void testBlockingPutLast() throws InterruptedException {
1027 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1028          Thread t = new Thread(new CheckedRunnable() {
1029 <            public void realRun() {
1030 <                int added = 0;
1029 >            public void realRun() throws InterruptedException {
1030 >                for (int i = 0; i < SIZE; ++i)
1031 >                    q.putLast(i);
1032 >                assertEquals(SIZE, q.size());
1033 >                assertEquals(0, q.remainingCapacity());
1034                  try {
1035 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1036 <                    for (int i = 0; i < SIZE; ++i) {
1037 <                        q.putLast(new Integer(i));
1009 <                        ++added;
1010 <                    }
1011 <                    q.putLast(new Integer(SIZE));
1012 <                    threadShouldThrow();
1013 <                } catch (InterruptedException success) {
1014 <                    threadAssertEquals(added, SIZE);
1015 <                }
1035 >                    q.putLast(99);
1036 >                    shouldThrow();
1037 >                } catch (InterruptedException success) {}
1038              }});
1039  
1040          t.start();
1041          Thread.sleep(SHORT_DELAY_MS);
1042          t.interrupt();
1043          t.join();
1044 +        assertEquals(SIZE, q.size());
1045 +        assertEquals(0, q.remainingCapacity());
1046      }
1047  
1048      /**
1049       * putLast blocks waiting for take when full
1050       */
1051      public void testPutLastWithTake() throws InterruptedException {
1052 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1052 >        final int capacity = 2;
1053 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1054          Thread t = new Thread(new CheckedRunnable() {
1055 <            public void realRun() {
1056 <                int added = 0;
1055 >            public void realRun() throws InterruptedException {
1056 >                for (int i = 0; i < capacity + 1; i++)
1057 >                    q.putLast(i);
1058                  try {
1059 <                    q.putLast(new Object());
1060 <                    ++added;
1061 <                    q.putLast(new Object());
1036 <                    ++added;
1037 <                    q.putLast(new Object());
1038 <                    ++added;
1039 <                    q.putLast(new Object());
1040 <                    ++added;
1041 <                    threadShouldThrow();
1042 <                } catch (InterruptedException success) {
1043 <                    threadAssertTrue(added >= 2);
1044 <                }
1059 >                    q.putLast(99);
1060 >                    shouldThrow();
1061 >                } catch (InterruptedException success) {}
1062              }});
1063  
1064          t.start();
1065          Thread.sleep(SHORT_DELAY_MS);
1066 <        q.take();
1066 >        assertEquals(q.remainingCapacity(), 0);
1067 >        assertEquals(0, q.take());
1068 >        Thread.sleep(SHORT_DELAY_MS);
1069          t.interrupt();
1070          t.join();
1071 +        assertEquals(q.remainingCapacity(), 0);
1072      }
1073  
1074      /**
# Line 1056 | Line 1076 | public class LinkedBlockingDequeTest ext
1076       */
1077      public void testTimedOfferLast() throws InterruptedException {
1078          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1079 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1079 >        Thread t = new Thread(new CheckedRunnable() {
1080              public void realRun() throws InterruptedException {
1081                  q.putLast(new Object());
1082                  q.putLast(new Object());
1083 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1084 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1085 <            }};
1083 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1084 >                try {
1085 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1086 >                    shouldThrow();
1087 >                } catch (InterruptedException success) {}
1088 >            }});
1089  
1090          t.start();
1091          Thread.sleep(SMALL_DELAY_MS);
# Line 1076 | Line 1099 | public class LinkedBlockingDequeTest ext
1099      public void testTakeLast() throws InterruptedException {
1100          LinkedBlockingDeque q = populatedDeque(SIZE);
1101          for (int i = 0; i < SIZE; ++i) {
1102 <            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1102 >            assertEquals(SIZE-i-1, q.takeLast());
1103          }
1104      }
1105  
# Line 1100 | Line 1123 | public class LinkedBlockingDequeTest ext
1123       * TakeLast removes existing elements until empty, then blocks interruptibly
1124       */
1125      public void testBlockingTakeLast() throws InterruptedException {
1126 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1126 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1127 >        Thread t = new Thread(new CheckedRunnable() {
1128              public void realRun() throws InterruptedException {
1129 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1130 <                for (int i = 0; i < SIZE; ++i) {
1131 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1132 <                }
1133 <                q.takeLast();
1134 <            }};
1129 >                for (int i = 0; i < SIZE; ++i)
1130 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1131 >                try {
1132 >                    q.takeLast();
1133 >                    shouldThrow();
1134 >                } catch (InterruptedException success) {}
1135 >            }});
1136  
1137          t.start();
1138          Thread.sleep(SHORT_DELAY_MS);
# Line 1115 | Line 1140 | public class LinkedBlockingDequeTest ext
1140          t.join();
1141      }
1142  
1118
1143      /**
1144       * timed pollLast with zero timeout succeeds when non-empty, else times out
1145       */
1146      public void testTimedPollLast0() throws InterruptedException {
1147          LinkedBlockingDeque q = populatedDeque(SIZE);
1148          for (int i = 0; i < SIZE; ++i) {
1149 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1149 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1150          }
1151          assertNull(q.pollLast(0, MILLISECONDS));
1152      }
# Line 1133 | Line 1157 | public class LinkedBlockingDequeTest ext
1157      public void testTimedPollLast() throws InterruptedException {
1158          LinkedBlockingDeque q = populatedDeque(SIZE);
1159          for (int i = 0; i < SIZE; ++i) {
1160 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1160 >            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1161          }
1162          assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1163      }
# Line 1143 | Line 1167 | public class LinkedBlockingDequeTest ext
1167       * returning timeout status
1168       */
1169      public void testInterruptedTimedPollLast() throws InterruptedException {
1170 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1170 >        Thread t = new Thread(new CheckedRunnable() {
1171              public void realRun() throws InterruptedException {
1172                  LinkedBlockingDeque q = populatedDeque(SIZE);
1173                  for (int i = 0; i < SIZE; ++i) {
1174 <                    threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1174 >                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1175                  }
1176 <                q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1177 <            }};
1176 >                try {
1177 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1178 >                    shouldThrow();
1179 >                } catch (InterruptedException success) {}
1180 >            }});
1181  
1182          t.start();
1183          Thread.sleep(SHORT_DELAY_MS);
# Line 1188 | Line 1215 | public class LinkedBlockingDequeTest ext
1215      public void testElement() {
1216          LinkedBlockingDeque q = populatedDeque(SIZE);
1217          for (int i = 0; i < SIZE; ++i) {
1218 <            assertEquals(i, ((Integer)q.element()).intValue());
1218 >            assertEquals(i, q.element());
1219              q.poll();
1220          }
1221          try {
# Line 1314 | Line 1341 | public class LinkedBlockingDequeTest ext
1341       * toArray(null) throws NPE
1342       */
1343      public void testToArray_BadArg() {
1344 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1345          try {
1318            LinkedBlockingDeque q = populatedDeque(SIZE);
1346              Object o[] = q.toArray(null);
1347              shouldThrow();
1348          } catch (NullPointerException success) {}
# Line 1325 | Line 1352 | public class LinkedBlockingDequeTest ext
1352       * toArray with incompatible array type throws CCE
1353       */
1354      public void testToArray1_BadArg() {
1355 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1356          try {
1357 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1330 <            Object o[] = q.toArray(new String[10] );
1357 >            Object o[] = q.toArray(new String[10]);
1358              shouldThrow();
1359          } catch (ArrayStoreException success) {}
1360      }
# Line 1347 | Line 1374 | public class LinkedBlockingDequeTest ext
1374      /**
1375       * iterator.remove removes current element
1376       */
1377 <    public void testIteratorRemove () {
1377 >    public void testIteratorRemove() {
1378          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1379          q.add(two);
1380          q.add(one);
# Line 1358 | Line 1385 | public class LinkedBlockingDequeTest ext
1385          it.remove();
1386  
1387          it = q.iterator();
1388 <        assertEquals(it.next(), one);
1389 <        assertEquals(it.next(), three);
1388 >        assertSame(it.next(), one);
1389 >        assertSame(it.next(), three);
1390          assertFalse(it.hasNext());
1391      }
1392  
# Line 1375 | Line 1402 | public class LinkedBlockingDequeTest ext
1402          assertEquals(0, q.remainingCapacity());
1403          int k = 0;
1404          for (Iterator it = q.iterator(); it.hasNext();) {
1405 <            int i = ((Integer)(it.next())).intValue();
1379 <            assertEquals(++k, i);
1405 >            assertEquals(++k, it.next());
1406          }
1407          assertEquals(3, k);
1408      }
# Line 1384 | Line 1410 | public class LinkedBlockingDequeTest ext
1410      /**
1411       * Modifications do not cause iterators to fail
1412       */
1413 <    public void testWeaklyConsistentIteration () {
1413 >    public void testWeaklyConsistentIteration() {
1414          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1415          q.add(one);
1416          q.add(two);
# Line 1427 | Line 1453 | public class LinkedBlockingDequeTest ext
1453              q.add(new Integer(1));
1454              int k = 0;
1455              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1456 <                int i = ((Integer)(it.next())).intValue();
1431 <                assertEquals(++k, i);
1456 >                assertEquals(++k, it.next());
1457              }
1458  
1459              assertEquals(3, k);
# Line 1441 | Line 1466 | public class LinkedBlockingDequeTest ext
1466      /**
1467       * descendingIterator.remove removes current element
1468       */
1469 <    public void testDescendingIteratorRemove () {
1469 >    public void testDescendingIteratorRemove() {
1470          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1471          for (int iters = 0; iters < 100; ++iters) {
1472              q.add(new Integer(3));
# Line 1483 | Line 1508 | public class LinkedBlockingDequeTest ext
1508          ExecutorService executor = Executors.newFixedThreadPool(2);
1509          executor.execute(new CheckedRunnable() {
1510              public void realRun() throws InterruptedException {
1511 <                threadAssertFalse(q.offer(three));
1512 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1513 <                threadAssertEquals(0, q.remainingCapacity());
1511 >                assertFalse(q.offer(three));
1512 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1513 >                assertEquals(0, q.remainingCapacity());
1514              }});
1515  
1516          executor.execute(new CheckedRunnable() {
1517              public void realRun() throws InterruptedException {
1518                  Thread.sleep(SMALL_DELAY_MS);
1519 <                threadAssertEquals(one, q.take());
1519 >                assertSame(one, q.take());
1520              }});
1521  
1522          joinPool(executor);
# Line 1505 | Line 1530 | public class LinkedBlockingDequeTest ext
1530          ExecutorService executor = Executors.newFixedThreadPool(2);
1531          executor.execute(new CheckedRunnable() {
1532              public void realRun() throws InterruptedException {
1533 <                threadAssertNull(q.poll());
1534 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1535 <                threadAssertTrue(q.isEmpty());
1533 >                assertNull(q.poll());
1534 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1535 >                assertTrue(q.isEmpty());
1536              }});
1537  
1538          executor.execute(new CheckedRunnable() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines