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.10 by jsr166, Sat Nov 21 09:28:16 2009 UTC vs.
Revision 1.22 by jsr166, Wed Aug 25 01:44:48 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      }
158  
159      /**
160 <     * getFirst returns next getFirst, or throws NSEE if empty
160 >     * getFirst() returns first element, or throws NSEE if empty
161       */
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      /**
176 <     *  getLast returns next element, or throws NSEE if empty
176 >     *  getLast() returns last element, or throws NSEE if empty
177       */
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 188 | Line 189 | public class LinkedBlockingDequeTest ext
189      }
190  
191      /**
192 <     *  removeFirst removes next element, or throws NSEE if empty
192 >     * removeFirst() removes first element, or throws NSEE if empty
193       */
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 276 | Line 293 | public class LinkedBlockingDequeTest ext
293      }
294  
295      /**
296 <     * Constructor throws IAE if  capacity argument nonpositive
296 >     * Constructor throws IAE if capacity argument nonpositive
297       */
298      public void testConstructor2() {
299          try {
# 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 499 | Line 513 | public class LinkedBlockingDequeTest ext
513              shouldThrow();
514          } catch (NullPointerException success) {}
515      }
516 +
517      /**
518       * addAll of a collection with any null elements throws NPE after
519       * possibly adding some elements
# Line 513 | Line 528 | public class LinkedBlockingDequeTest ext
528              shouldThrow();
529          } catch (NullPointerException success) {}
530      }
531 +
532      /**
533       * addAll throws ISE if not enough room
534       */
# Line 526 | Line 542 | public class LinkedBlockingDequeTest ext
542              shouldThrow();
543          } catch (IllegalStateException success) {}
544      }
545 +
546      /**
547       * Deque contains all elements, in traversal order, of successful addAll
548       */
549      public void testAddAll5() {
550 <        try {
551 <            Integer[] empty = new Integer[0];
552 <            Integer[] ints = new Integer[SIZE];
553 <            for (int i = 0; i < SIZE; ++i)
554 <                ints[i] = new Integer(i);
555 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
556 <            assertFalse(q.addAll(Arrays.asList(empty)));
557 <            assertTrue(q.addAll(Arrays.asList(ints)));
558 <            for (int i = 0; i < SIZE; ++i)
542 <                assertEquals(ints[i], q.poll());
543 <        }
544 <        finally {}
550 >        Integer[] empty = new Integer[0];
551 >        Integer[] ints = new Integer[SIZE];
552 >        for (int i = 0; i < SIZE; ++i)
553 >            ints[i] = new Integer(i);
554 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
555 >        assertFalse(q.addAll(Arrays.asList(empty)));
556 >        assertTrue(q.addAll(Arrays.asList(ints)));
557 >        for (int i = 0; i < SIZE; ++i)
558 >            assertEquals(ints[i], q.poll());
559      }
560  
561  
# Line 573 | Line 587 | public class LinkedBlockingDequeTest ext
587       * put blocks interruptibly if full
588       */
589      public void testBlockingPut() throws InterruptedException {
590 +        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
591          Thread t = new Thread(new CheckedRunnable() {
592 <            public void realRun() {
593 <                int added = 0;
592 >            public void realRun() throws InterruptedException {
593 >                for (int i = 0; i < SIZE; ++i)
594 >                    q.put(i);
595 >                assertEquals(SIZE, q.size());
596 >                assertEquals(0, q.remainingCapacity());
597                  try {
598 <                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
599 <                    for (int i = 0; i < SIZE; ++i) {
600 <                        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 <                }
598 >                    q.put(99);
599 >                    shouldThrow();
600 >                } catch (InterruptedException success) {}
601              }});
602  
603          t.start();
604          Thread.sleep(SHORT_DELAY_MS);
605          t.interrupt();
606          t.join();
607 +        assertEquals(SIZE, q.size());
608 +        assertEquals(0, q.remainingCapacity());
609      }
610  
611      /**
612       * put blocks waiting for take when full
613       */
614      public void testPutWithTake() throws InterruptedException {
615 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
615 >        final int capacity = 2;
616 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
617          Thread t = new Thread(new CheckedRunnable() {
618 <            public void realRun() {
619 <                int added = 0;
618 >            public void realRun() throws InterruptedException {
619 >                for (int i = 0; i < capacity + 1; i++)
620 >                    q.put(i);
621                  try {
622 <                    q.put(new Object());
623 <                    ++added;
624 <                    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 <                }
622 >                    q.put(99);
623 >                    shouldThrow();
624 >                } catch (InterruptedException success) {}
625              }});
626  
627          t.start();
628          Thread.sleep(SHORT_DELAY_MS);
629 <        q.take();
629 >        assertEquals(q.remainingCapacity(), 0);
630 >        assertEquals(0, q.take());
631 >        Thread.sleep(SHORT_DELAY_MS);
632          t.interrupt();
633          t.join();
634 +        assertEquals(q.remainingCapacity(), 0);
635      }
636  
637      /**
# Line 630 | Line 639 | public class LinkedBlockingDequeTest ext
639       */
640      public void testTimedOffer() throws InterruptedException {
641          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
642 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
642 >        Thread t = new Thread(new CheckedRunnable() {
643              public void realRun() throws InterruptedException {
644                  q.put(new Object());
645                  q.put(new Object());
646 <                threadAssertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
647 <                q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
648 <            }};
646 >                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
647 >                try {
648 >                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
649 >                    shouldThrow();
650 >                } catch (InterruptedException success) {}
651 >            }});
652  
653          t.start();
654          Thread.sleep(SMALL_DELAY_MS);
# Line 650 | Line 662 | public class LinkedBlockingDequeTest ext
662      public void testTake() throws InterruptedException {
663          LinkedBlockingDeque q = populatedDeque(SIZE);
664          for (int i = 0; i < SIZE; ++i) {
665 <            assertEquals(i, ((Integer)q.take()).intValue());
665 >            assertEquals(i, q.take());
666          }
667      }
668  
# Line 674 | Line 686 | public class LinkedBlockingDequeTest ext
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 {
679                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 696 | 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 707 | 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 718 | 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 728 | Line 743 | public class LinkedBlockingDequeTest ext
743       * returning timeout status
744       */
745      public void testInterruptedTimedPoll() throws InterruptedException {
746 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
746 >        Thread t = new Thread(new CheckedRunnable() {
747              public void realRun() throws InterruptedException {
748                  LinkedBlockingDeque q = populatedDeque(SIZE);
749                  for (int i = 0; i < SIZE; ++i) {
750 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
750 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
751                  }
752 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
753 <            }};
752 >                try {
753 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
754 >                    shouldThrow();
755 >                } catch (InterruptedException success) {}
756 >            }});
757  
758          t.start();
759          Thread.sleep(SHORT_DELAY_MS);
# Line 749 | Line 767 | public class LinkedBlockingDequeTest ext
767       */
768      public void testTimedPollWithOffer() throws InterruptedException {
769          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
770 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
770 >        Thread t = new Thread(new CheckedRunnable() {
771              public void realRun() throws InterruptedException {
772 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
773 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
774 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
775 <            }};
772 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
773 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
774 >                try {
775 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
776 >                    shouldThrow();
777 >                } catch (InterruptedException success) {}
778 >            }});
779  
780          t.start();
781          Thread.sleep(SMALL_DELAY_MS);
# Line 792 | Line 813 | public class LinkedBlockingDequeTest ext
813       * putFirst blocks interruptibly if full
814       */
815      public void testBlockingPutFirst() throws InterruptedException {
816 <        Thread t = new Thread(new Runnable() {
817 <                public void run() {
818 <                    int added = 0;
819 <                    try {
820 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
821 <                        for (int i = 0; i < SIZE; ++i) {
822 <                            q.putFirst(new Integer(i));
823 <                            ++added;
824 <                        }
825 <                        q.putFirst(new Integer(SIZE));
826 <                        threadShouldThrow();
827 <                    } catch (InterruptedException success) {
807 <                        threadAssertEquals(added, SIZE);
808 <                    }
809 <                }});
816 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
817 >        Thread t = new Thread(new CheckedRunnable() {
818 >            public void realRun() throws InterruptedException {
819 >                for (int i = 0; i < SIZE; ++i)
820 >                    q.putFirst(i);
821 >                assertEquals(SIZE, q.size());
822 >                assertEquals(0, q.remainingCapacity());
823 >                try {
824 >                    q.putFirst(99);
825 >                    shouldThrow();
826 >                } catch (InterruptedException success) {}
827 >            }});
828  
829          t.start();
830          Thread.sleep(SHORT_DELAY_MS);
831          t.interrupt();
832          t.join();
833 +        assertEquals(SIZE, q.size());
834 +        assertEquals(0, q.remainingCapacity());
835      }
836  
837      /**
838       * putFirst blocks waiting for take when full
839       */
840      public void testPutFirstWithTake() throws InterruptedException {
841 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
842 <        Thread t = new Thread(new Runnable() {
843 <                public void run() {
844 <                    int added = 0;
845 <                    try {
846 <                        q.putFirst(new Object());
847 <                        ++added;
848 <                        q.putFirst(new Object());
849 <                        ++added;
850 <                        q.putFirst(new Object());
851 <                        ++added;
832 <                        q.putFirst(new Object());
833 <                        ++added;
834 <                        threadShouldThrow();
835 <                    } catch (InterruptedException success) {
836 <                        threadAssertTrue(added >= 2);
837 <                    }
838 <                }
839 <            });
841 >        final int capacity = 2;
842 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
843 >        Thread t = new Thread(new CheckedRunnable() {
844 >            public void realRun() throws InterruptedException {
845 >                for (int i = 0; i < capacity + 1; i++)
846 >                    q.putFirst(i);
847 >                try {
848 >                    q.putFirst(99);
849 >                    shouldThrow();
850 >                } catch (InterruptedException success) {}
851 >            }});
852  
853          t.start();
854          Thread.sleep(SHORT_DELAY_MS);
855 <        q.take();
855 >        assertEquals(q.remainingCapacity(), 0);
856 >        assertEquals(capacity - 1, q.take());
857 >        Thread.sleep(SHORT_DELAY_MS);
858          t.interrupt();
859          t.join();
860 +        assertEquals(q.remainingCapacity(), 0);
861      }
862  
863      /**
# Line 850 | Line 865 | public class LinkedBlockingDequeTest ext
865       */
866      public void testTimedOfferFirst() throws InterruptedException {
867          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
868 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
868 >        Thread t = new Thread(new CheckedRunnable() {
869              public void realRun() throws InterruptedException {
870                  q.putFirst(new Object());
871                  q.putFirst(new Object());
872 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
873 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
874 <            }};
872 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
873 >                try {
874 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
875 >                    shouldThrow();
876 >                } catch (InterruptedException success) {}
877 >            }});
878  
879          t.start();
880          Thread.sleep(SMALL_DELAY_MS);
# Line 870 | Line 888 | public class LinkedBlockingDequeTest ext
888      public void testTakeFirst() throws InterruptedException {
889          LinkedBlockingDeque q = populatedDeque(SIZE);
890          for (int i = 0; i < SIZE; ++i) {
891 <            assertEquals(i, ((Integer)q.takeFirst()).intValue());
891 >            assertEquals(i, q.takeFirst());
892          }
893      }
894  
# Line 894 | Line 912 | public class LinkedBlockingDequeTest ext
912       * TakeFirst removes existing elements until empty, then blocks interruptibly
913       */
914      public void testBlockingTakeFirst() throws InterruptedException {
915 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
915 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
916 >        Thread t = new Thread(new CheckedRunnable() {
917              public void realRun() throws InterruptedException {
918 <                LinkedBlockingDeque q = populatedDeque(SIZE);
919 <                for (int i = 0; i < SIZE; ++i) {
920 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
921 <                }
922 <                q.takeFirst();
923 <            }};
918 >                for (int i = 0; i < SIZE; ++i)
919 >                    assertEquals(i, q.takeFirst());
920 >                try {
921 >                    q.takeFirst();
922 >                    shouldThrow();
923 >                } catch (InterruptedException success) {}
924 >            }});
925  
926          t.start();
927          Thread.sleep(SHORT_DELAY_MS);
# Line 916 | Line 936 | public class LinkedBlockingDequeTest ext
936      public void testTimedPollFirst0() throws InterruptedException {
937          LinkedBlockingDeque q = populatedDeque(SIZE);
938          for (int i = 0; i < SIZE; ++i) {
939 <            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
939 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
940          }
941          assertNull(q.pollFirst(0, MILLISECONDS));
942      }
# Line 927 | Line 947 | public class LinkedBlockingDequeTest ext
947      public void testTimedPollFirst() throws InterruptedException {
948          LinkedBlockingDeque q = populatedDeque(SIZE);
949          for (int i = 0; i < SIZE; ++i) {
950 <            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
950 >            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
951          }
952          assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
953      }
# Line 937 | Line 957 | public class LinkedBlockingDequeTest ext
957       * returning timeout status
958       */
959      public void testInterruptedTimedPollFirst() throws InterruptedException {
960 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
960 >        Thread t = new Thread(new CheckedRunnable() {
961              public void realRun() throws InterruptedException {
962                  LinkedBlockingDeque q = populatedDeque(SIZE);
963                  for (int i = 0; i < SIZE; ++i) {
964 <                    threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
964 >                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
965                  }
966 <                q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
967 <            }};
966 >                try {
967 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
968 >                    shouldThrow();
969 >                } catch (InterruptedException success) {}
970 >            }});
971  
972          t.start();
973          Thread.sleep(SHORT_DELAY_MS);
# Line 958 | Line 981 | public class LinkedBlockingDequeTest ext
981       */
982      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
983          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
984 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
984 >        Thread t = new Thread(new CheckedRunnable() {
985              public void realRun() throws InterruptedException {
986 <                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
987 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
988 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
989 <            }};
986 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
987 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
988 >                try {
989 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
990 >                    shouldThrow();
991 >                } catch (InterruptedException success) {}
992 >            }});
993  
994          t.start();
995          Thread.sleep(SMALL_DELAY_MS);
# Line 1000 | Line 1026 | public class LinkedBlockingDequeTest ext
1026       * putLast blocks interruptibly if full
1027       */
1028      public void testBlockingPutLast() throws InterruptedException {
1029 <        Thread t = new Thread(new Runnable() {
1030 <                public void run() {
1031 <                    int added = 0;
1032 <                    try {
1033 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1034 <                        for (int i = 0; i < SIZE; ++i) {
1035 <                            q.putLast(new Integer(i));
1036 <                            ++added;
1037 <                        }
1038 <                        q.putLast(new Integer(SIZE));
1039 <                        threadShouldThrow();
1040 <                    } catch (InterruptedException success) {
1041 <                        threadAssertEquals(added, SIZE);
1016 <                    }
1017 <                }});
1029 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1030 >        Thread t = new Thread(new CheckedRunnable() {
1031 >            public void realRun() throws InterruptedException {
1032 >                for (int i = 0; i < SIZE; ++i)
1033 >                    q.putLast(i);
1034 >                assertEquals(SIZE, q.size());
1035 >                assertEquals(0, q.remainingCapacity());
1036 >                try {
1037 >                    q.putLast(99);
1038 >                    shouldThrow();
1039 >                } catch (InterruptedException success) {}
1040 >            }});
1041 >
1042          t.start();
1043          Thread.sleep(SHORT_DELAY_MS);
1044          t.interrupt();
1045          t.join();
1046 +        assertEquals(SIZE, q.size());
1047 +        assertEquals(0, q.remainingCapacity());
1048      }
1049  
1050      /**
1051       * putLast blocks waiting for take when full
1052       */
1053      public void testPutLastWithTake() throws InterruptedException {
1054 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1055 <        Thread t = new Thread(new Runnable() {
1056 <                public void run() {
1057 <                    int added = 0;
1058 <                    try {
1059 <                        q.putLast(new Object());
1060 <                        ++added;
1061 <                        q.putLast(new Object());
1062 <                        ++added;
1063 <                        q.putLast(new Object());
1064 <                        ++added;
1039 <                        q.putLast(new Object());
1040 <                        ++added;
1041 <                        threadShouldThrow();
1042 <                    } catch (InterruptedException success) {
1043 <                        threadAssertTrue(added >= 2);
1044 <                    }
1045 <                }
1046 <            });
1054 >        final int capacity = 2;
1055 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1056 >        Thread t = new Thread(new CheckedRunnable() {
1057 >            public void realRun() throws InterruptedException {
1058 >                for (int i = 0; i < capacity + 1; i++)
1059 >                    q.putLast(i);
1060 >                try {
1061 >                    q.putLast(99);
1062 >                    shouldThrow();
1063 >                } catch (InterruptedException success) {}
1064 >            }});
1065  
1066          t.start();
1067          Thread.sleep(SHORT_DELAY_MS);
1068 <        q.take();
1068 >        assertEquals(q.remainingCapacity(), 0);
1069 >        assertEquals(0, q.take());
1070 >        Thread.sleep(SHORT_DELAY_MS);
1071          t.interrupt();
1072          t.join();
1073 +        assertEquals(q.remainingCapacity(), 0);
1074      }
1075  
1076      /**
# Line 1057 | Line 1078 | public class LinkedBlockingDequeTest ext
1078       */
1079      public void testTimedOfferLast() throws InterruptedException {
1080          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1081 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1081 >        Thread t = new Thread(new CheckedRunnable() {
1082              public void realRun() throws InterruptedException {
1083                  q.putLast(new Object());
1084                  q.putLast(new Object());
1085 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1086 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1087 <            }};
1085 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1086 >                try {
1087 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1088 >                    shouldThrow();
1089 >                } catch (InterruptedException success) {}
1090 >            }});
1091  
1092          t.start();
1093          Thread.sleep(SMALL_DELAY_MS);
# Line 1077 | Line 1101 | public class LinkedBlockingDequeTest ext
1101      public void testTakeLast() throws InterruptedException {
1102          LinkedBlockingDeque q = populatedDeque(SIZE);
1103          for (int i = 0; i < SIZE; ++i) {
1104 <            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1104 >            assertEquals(SIZE-i-1, q.takeLast());
1105          }
1106      }
1107  
# Line 1101 | Line 1125 | public class LinkedBlockingDequeTest ext
1125       * TakeLast removes existing elements until empty, then blocks interruptibly
1126       */
1127      public void testBlockingTakeLast() throws InterruptedException {
1128 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1128 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1129 >        Thread t = new Thread(new CheckedRunnable() {
1130              public void realRun() throws InterruptedException {
1131 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1132 <                for (int i = 0; i < SIZE; ++i) {
1133 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1134 <                }
1135 <                q.takeLast();
1136 <            }};
1131 >                for (int i = 0; i < SIZE; ++i)
1132 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1133 >                try {
1134 >                    q.takeLast();
1135 >                    shouldThrow();
1136 >                } catch (InterruptedException success) {}
1137 >            }});
1138  
1139          t.start();
1140          Thread.sleep(SHORT_DELAY_MS);
# Line 1116 | Line 1142 | public class LinkedBlockingDequeTest ext
1142          t.join();
1143      }
1144  
1119
1145      /**
1146       * timed pollLast with zero timeout succeeds when non-empty, else times out
1147       */
1148      public void testTimedPollLast0() throws InterruptedException {
1149          LinkedBlockingDeque q = populatedDeque(SIZE);
1150          for (int i = 0; i < SIZE; ++i) {
1151 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1151 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1152          }
1153          assertNull(q.pollLast(0, MILLISECONDS));
1154      }
# Line 1134 | Line 1159 | public class LinkedBlockingDequeTest ext
1159      public void testTimedPollLast() throws InterruptedException {
1160          LinkedBlockingDeque q = populatedDeque(SIZE);
1161          for (int i = 0; i < SIZE; ++i) {
1162 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1162 >            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1163          }
1164          assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1165      }
# Line 1144 | Line 1169 | public class LinkedBlockingDequeTest ext
1169       * returning timeout status
1170       */
1171      public void testInterruptedTimedPollLast() throws InterruptedException {
1172 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1172 >        Thread t = new Thread(new CheckedRunnable() {
1173              public void realRun() throws InterruptedException {
1174                  LinkedBlockingDeque q = populatedDeque(SIZE);
1175                  for (int i = 0; i < SIZE; ++i) {
1176 <                    threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1176 >                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1177                  }
1178 <                q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1179 <            }};
1178 >                try {
1179 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1180 >                    shouldThrow();
1181 >                } catch (InterruptedException success) {}
1182 >            }});
1183  
1184          t.start();
1185          Thread.sleep(SHORT_DELAY_MS);
# Line 1189 | Line 1217 | public class LinkedBlockingDequeTest ext
1217      public void testElement() {
1218          LinkedBlockingDeque q = populatedDeque(SIZE);
1219          for (int i = 0; i < SIZE; ++i) {
1220 <            assertEquals(i, ((Integer)q.element()).intValue());
1220 >            assertEquals(i, q.element());
1221              q.poll();
1222          }
1223          try {
# Line 1315 | Line 1343 | public class LinkedBlockingDequeTest ext
1343       * toArray(null) throws NPE
1344       */
1345      public void testToArray_BadArg() {
1346 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1347          try {
1319            LinkedBlockingDeque q = populatedDeque(SIZE);
1348              Object o[] = q.toArray(null);
1349              shouldThrow();
1350          } catch (NullPointerException success) {}
# Line 1326 | Line 1354 | public class LinkedBlockingDequeTest ext
1354       * toArray with incompatible array type throws CCE
1355       */
1356      public void testToArray1_BadArg() {
1357 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1358          try {
1359 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1331 <            Object o[] = q.toArray(new String[10] );
1359 >            Object o[] = q.toArray(new String[10]);
1360              shouldThrow();
1361          } catch (ArrayStoreException success) {}
1362      }
# Line 1348 | Line 1376 | public class LinkedBlockingDequeTest ext
1376      /**
1377       * iterator.remove removes current element
1378       */
1379 <    public void testIteratorRemove () {
1379 >    public void testIteratorRemove() {
1380          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1381          q.add(two);
1382          q.add(one);
# Line 1359 | Line 1387 | public class LinkedBlockingDequeTest ext
1387          it.remove();
1388  
1389          it = q.iterator();
1390 <        assertEquals(it.next(), one);
1391 <        assertEquals(it.next(), three);
1390 >        assertSame(it.next(), one);
1391 >        assertSame(it.next(), three);
1392          assertFalse(it.hasNext());
1393      }
1394  
# Line 1376 | Line 1404 | public class LinkedBlockingDequeTest ext
1404          assertEquals(0, q.remainingCapacity());
1405          int k = 0;
1406          for (Iterator it = q.iterator(); it.hasNext();) {
1407 <            int i = ((Integer)(it.next())).intValue();
1380 <            assertEquals(++k, i);
1407 >            assertEquals(++k, it.next());
1408          }
1409          assertEquals(3, k);
1410      }
# Line 1385 | Line 1412 | public class LinkedBlockingDequeTest ext
1412      /**
1413       * Modifications do not cause iterators to fail
1414       */
1415 <    public void testWeaklyConsistentIteration () {
1415 >    public void testWeaklyConsistentIteration() {
1416          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1417          q.add(one);
1418          q.add(two);
# Line 1428 | Line 1455 | public class LinkedBlockingDequeTest ext
1455              q.add(new Integer(1));
1456              int k = 0;
1457              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1458 <                int i = ((Integer)(it.next())).intValue();
1432 <                assertEquals(++k, i);
1458 >                assertEquals(++k, it.next());
1459              }
1460  
1461              assertEquals(3, k);
# Line 1442 | Line 1468 | public class LinkedBlockingDequeTest ext
1468      /**
1469       * descendingIterator.remove removes current element
1470       */
1471 <    public void testDescendingIteratorRemove () {
1471 >    public void testDescendingIteratorRemove() {
1472          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1473          for (int iters = 0; iters < 100; ++iters) {
1474              q.add(new Integer(3));
# Line 1484 | Line 1510 | public class LinkedBlockingDequeTest ext
1510          ExecutorService executor = Executors.newFixedThreadPool(2);
1511          executor.execute(new CheckedRunnable() {
1512              public void realRun() throws InterruptedException {
1513 <                threadAssertFalse(q.offer(three));
1514 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1515 <                threadAssertEquals(0, q.remainingCapacity());
1513 >                assertFalse(q.offer(three));
1514 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1515 >                assertEquals(0, q.remainingCapacity());
1516              }});
1517  
1518          executor.execute(new CheckedRunnable() {
1519              public void realRun() throws InterruptedException {
1520                  Thread.sleep(SMALL_DELAY_MS);
1521 <                threadAssertEquals(one, q.take());
1521 >                assertSame(one, q.take());
1522              }});
1523  
1524          joinPool(executor);
# Line 1506 | Line 1532 | public class LinkedBlockingDequeTest ext
1532          ExecutorService executor = Executors.newFixedThreadPool(2);
1533          executor.execute(new CheckedRunnable() {
1534              public void realRun() throws InterruptedException {
1535 <                threadAssertNull(q.poll());
1536 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1537 <                threadAssertTrue(q.isEmpty());
1535 >                assertNull(q.poll());
1536 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1537 >                assertTrue(q.isEmpty());
1538              }});
1539  
1540          executor.execute(new CheckedRunnable() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines