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.9 by jsr166, Sat Nov 21 08:37:39 2009 UTC vs.
Revision 1.36 by jsr166, Thu Apr 14 22:55:08 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   import junit.framework.*;
# 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();
186              shouldThrow();
187          } catch (NoSuchElementException success) {}
188 +        assertNull(q.peekFirst());
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 188 | 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();
217              shouldThrow();
218          } catch (NoSuchElementException success) {}
219 +        assertNull(q.peekFirst());
220 +    }
221 +
222 +    /**
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, q.removeLast());
229 +        }
230 +        try {
231 +            q.removeLast();
232 +            shouldThrow();
233 +        } catch (NoSuchElementException success) {}
234 +        assertNull(q.peekLast());
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 252 | 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 262 | 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 276 | Line 309 | public class LinkedBlockingDequeTest ext
309      }
310  
311      /**
312 <     * Constructor throws IAE if  capacity argument nonpositive
312 >     * Constructor throws IAE if capacity argument nonpositive
313       */
314      public void testConstructor2() {
315          try {
# Line 323 | Line 356 | public class LinkedBlockingDequeTest ext
356       * Deque contains all elements of collection used to initialize
357       */
358      public void testConstructor6() {
359 <        try {
360 <            Integer[] ints = new Integer[SIZE];
361 <            for (int i = 0; i < SIZE; ++i)
362 <                ints[i] = new Integer(i);
363 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
364 <            for (int i = 0; i < SIZE; ++i)
332 <                assertEquals(ints[i], q.poll());
333 <        }
334 <        finally {}
359 >        Integer[] ints = new Integer[SIZE];
360 >        for (int i = 0; i < SIZE; ++i)
361 >            ints[i] = new Integer(i);
362 >        LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
363 >        for (int i = 0; i < SIZE; ++i)
364 >            assertEquals(ints[i], q.poll());
365      }
366  
367      /**
# Line 423 | 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 499 | 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 513 | 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 526 | 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       */
565      public void testAddAll5() {
566 <        try {
567 <            Integer[] empty = new Integer[0];
568 <            Integer[] ints = new Integer[SIZE];
569 <            for (int i = 0; i < SIZE; ++i)
570 <                ints[i] = new Integer(i);
571 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
572 <            assertFalse(q.addAll(Arrays.asList(empty)));
573 <            assertTrue(q.addAll(Arrays.asList(ints)));
574 <            for (int i = 0; i < SIZE; ++i)
542 <                assertEquals(ints[i], q.poll());
543 <        }
544 <        finally {}
566 >        Integer[] empty = new Integer[0];
567 >        Integer[] ints = new Integer[SIZE];
568 >        for (int i = 0; i < SIZE; ++i)
569 >            ints[i] = new Integer(i);
570 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
571 >        assertFalse(q.addAll(Arrays.asList(empty)));
572 >        assertTrue(q.addAll(Arrays.asList(ints)));
573 >        for (int i = 0; i < SIZE; ++i)
574 >            assertEquals(ints[i], q.poll());
575      }
576  
577  
578      /**
579       * put(null) throws NPE
580       */
581 <     public void testPutNull() throws InterruptedException {
581 >    public void testPutNull() throws InterruptedException {
582          try {
583              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
584              q.put(null);
585              shouldThrow();
586          } catch (NullPointerException success) {}
587 <     }
587 >    }
588  
589      /**
590       * all elements successfully put are contained
591       */
592 <     public void testPut() throws InterruptedException {
593 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
594 <         for (int i = 0; i < SIZE; ++i) {
595 <             Integer I = new Integer(i);
596 <             q.put(I);
597 <             assertTrue(q.contains(I));
598 <         }
599 <         assertEquals(0, q.remainingCapacity());
592 >    public void testPut() throws InterruptedException {
593 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
594 >        for (int i = 0; i < SIZE; ++i) {
595 >            Integer I = new Integer(i);
596 >            q.put(I);
597 >            assertTrue(q.contains(I));
598 >        }
599 >        assertEquals(0, q.remainingCapacity());
600      }
601  
602      /**
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));
583 <                        ++added;
584 <                    }
585 <                    q.put(new Integer(SIZE));
586 <                    threadShouldThrow();
587 <                } catch (InterruptedException success) {
588 <                    threadAssertEquals(added, SIZE);
589 <                }
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);
632 <        Thread t = new Thread(new Runnable() {
633 <                public void run() {
634 <                    int added = 0;
635 <                    try {
636 <                        q.put(new Object());
637 <                        ++added;
638 <                        q.put(new Object());
639 <                        ++added;
640 <                        q.put(new Object());
641 <                        ++added;
613 <                        q.put(new Object());
614 <                        ++added;
615 <                        threadShouldThrow();
616 <                    } catch (InterruptedException success) {
617 <                        threadAssertTrue(added >= 2);
618 <                    }
619 <                }
620 <            });
631 >        final int capacity = 2;
632 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
633 >        Thread t = new Thread(new CheckedRunnable() {
634 >            public void realRun() throws InterruptedException {
635 >                for (int i = 0; i < capacity + 1; i++)
636 >                    q.put(i);
637 >                try {
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 631 | 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 651 | 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      /**
659     * take blocks interruptibly when empty
660     */
661    public void testTakeFromEmpty() throws InterruptedException {
662        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
663        Thread t = new ThreadShouldThrow(InterruptedException.class) {
664            public void realRun() throws InterruptedException {
665                q.take();
666            }};
667
668        t.start();
669        Thread.sleep(SHORT_DELAY_MS);
670        t.interrupt();
671        t.join();
672    }
673
674    /**
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 {
680                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 697 | 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 708 | 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 719 | 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 729 | 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 >        final BlockingQueue<Integer> q = populatedDeque(SIZE);
747 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
748 >        Thread t = newStartedThread(new CheckedRunnable() {
749              public void realRun() throws InterruptedException {
734                LinkedBlockingDeque q = populatedDeque(SIZE);
750                  for (int i = 0; i < SIZE; ++i) {
751 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
751 >                    long t0 = System.nanoTime();
752 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
753 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
754                  }
755 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
756 <            }};
755 >                long t0 = System.nanoTime();
756 >                aboutToWait.countDown();
757 >                try {
758 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
759 >                    shouldThrow();
760 >                } catch (InterruptedException success) {
761 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
762 >                }
763 >            }});
764  
765 <        t.start();
766 <        Thread.sleep(SHORT_DELAY_MS);
765 >        aboutToWait.await();
766 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
767          t.interrupt();
768 <        t.join();
768 >        awaitTermination(t, MEDIUM_DELAY_MS);
769 >        checkEmpty(q);
770      }
771  
772      /**
748     *  timed poll before a delayed offer fails; after offer succeeds;
749     *  on interruption throws
750     */
751    public void testTimedPollWithOffer() throws InterruptedException {
752        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
753        Thread t = new ThreadShouldThrow(InterruptedException.class) {
754            public void realRun() throws InterruptedException {
755                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
756                q.poll(LONG_DELAY_MS, MILLISECONDS);
757                q.poll(LONG_DELAY_MS, MILLISECONDS);
758            }};
759
760        t.start();
761        Thread.sleep(SMALL_DELAY_MS);
762        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
763        t.interrupt();
764        t.join();
765    }
766
767
768    /**
773       * putFirst(null) throws NPE
774       */
775 <     public void testPutFirstNull() throws InterruptedException {
775 >    public void testPutFirstNull() throws InterruptedException {
776          try {
777              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
778              q.putFirst(null);
779              shouldThrow();
780          } catch (NullPointerException success) {}
781 <     }
781 >    }
782  
783      /**
784       * all elements successfully putFirst are contained
785       */
786 <     public void testPutFirst() throws InterruptedException {
787 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
788 <         for (int i = 0; i < SIZE; ++i) {
789 <             Integer I = new Integer(i);
790 <             q.putFirst(I);
791 <             assertTrue(q.contains(I));
792 <         }
793 <         assertEquals(0, q.remainingCapacity());
786 >    public void testPutFirst() throws InterruptedException {
787 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
788 >        for (int i = 0; i < SIZE; ++i) {
789 >            Integer I = new Integer(i);
790 >            q.putFirst(I);
791 >            assertTrue(q.contains(I));
792 >        }
793 >        assertEquals(0, q.remainingCapacity());
794      }
795  
796      /**
797       * putFirst blocks interruptibly if full
798       */
799      public void testBlockingPutFirst() throws InterruptedException {
800 <        Thread t = new Thread(new Runnable() {
801 <                public void run() {
802 <                    int added = 0;
803 <                    try {
804 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
805 <                        for (int i = 0; i < SIZE; ++i) {
806 <                            q.putFirst(new Integer(i));
807 <                            ++added;
808 <                        }
809 <                        q.putFirst(new Integer(SIZE));
810 <                        threadShouldThrow();
811 <                    } catch (InterruptedException success) {
808 <                        threadAssertEquals(added, SIZE);
809 <                    }
810 <                }});
800 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
801 >        Thread t = new Thread(new CheckedRunnable() {
802 >            public void realRun() throws InterruptedException {
803 >                for (int i = 0; i < SIZE; ++i)
804 >                    q.putFirst(i);
805 >                assertEquals(SIZE, q.size());
806 >                assertEquals(0, q.remainingCapacity());
807 >                try {
808 >                    q.putFirst(99);
809 >                    shouldThrow();
810 >                } catch (InterruptedException success) {}
811 >            }});
812  
813          t.start();
814          Thread.sleep(SHORT_DELAY_MS);
815          t.interrupt();
816          t.join();
817 +        assertEquals(SIZE, q.size());
818 +        assertEquals(0, q.remainingCapacity());
819      }
820  
821      /**
822       * putFirst blocks waiting for take when full
823       */
824      public void testPutFirstWithTake() throws InterruptedException {
825 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
826 <        Thread t = new Thread(new Runnable() {
827 <                public void run() {
828 <                    int added = 0;
829 <                    try {
830 <                        q.putFirst(new Object());
831 <                        ++added;
832 <                        q.putFirst(new Object());
833 <                        ++added;
834 <                        q.putFirst(new Object());
835 <                        ++added;
833 <                        q.putFirst(new Object());
834 <                        ++added;
835 <                        threadShouldThrow();
836 <                    } catch (InterruptedException success) {
837 <                        threadAssertTrue(added >= 2);
838 <                    }
839 <                }
840 <            });
825 >        final int capacity = 2;
826 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
827 >        Thread t = new Thread(new CheckedRunnable() {
828 >            public void realRun() throws InterruptedException {
829 >                for (int i = 0; i < capacity + 1; i++)
830 >                    q.putFirst(i);
831 >                try {
832 >                    q.putFirst(99);
833 >                    shouldThrow();
834 >                } catch (InterruptedException success) {}
835 >            }});
836  
837          t.start();
838          Thread.sleep(SHORT_DELAY_MS);
839 <        q.take();
839 >        assertEquals(q.remainingCapacity(), 0);
840 >        assertEquals(capacity - 1, q.take());
841 >        Thread.sleep(SHORT_DELAY_MS);
842          t.interrupt();
843          t.join();
844 +        assertEquals(q.remainingCapacity(), 0);
845      }
846  
847      /**
# Line 851 | Line 849 | public class LinkedBlockingDequeTest ext
849       */
850      public void testTimedOfferFirst() throws InterruptedException {
851          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
852 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
852 >        Thread t = new Thread(new CheckedRunnable() {
853              public void realRun() throws InterruptedException {
854                  q.putFirst(new Object());
855                  q.putFirst(new Object());
856 <                threadAssertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
857 <                q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
858 <            }};
856 >                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
857 >                try {
858 >                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
859 >                    shouldThrow();
860 >                } catch (InterruptedException success) {}
861 >            }});
862  
863          t.start();
864          Thread.sleep(SMALL_DELAY_MS);
# Line 871 | Line 872 | public class LinkedBlockingDequeTest ext
872      public void testTakeFirst() throws InterruptedException {
873          LinkedBlockingDeque q = populatedDeque(SIZE);
874          for (int i = 0; i < SIZE; ++i) {
875 <            assertEquals(i, ((Integer)q.takeFirst()).intValue());
875 >            assertEquals(i, q.takeFirst());
876          }
877      }
878  
# Line 895 | Line 896 | public class LinkedBlockingDequeTest ext
896       * TakeFirst removes existing elements until empty, then blocks interruptibly
897       */
898      public void testBlockingTakeFirst() throws InterruptedException {
899 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
899 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
900 >        Thread t = new Thread(new CheckedRunnable() {
901              public void realRun() throws InterruptedException {
902 <                LinkedBlockingDeque q = populatedDeque(SIZE);
903 <                for (int i = 0; i < SIZE; ++i) {
904 <                    assertEquals(i, ((Integer)q.takeFirst()).intValue());
905 <                }
906 <                q.takeFirst();
907 <            }};
902 >                for (int i = 0; i < SIZE; ++i)
903 >                    assertEquals(i, q.takeFirst());
904 >                try {
905 >                    q.takeFirst();
906 >                    shouldThrow();
907 >                } catch (InterruptedException success) {}
908 >            }});
909  
910          t.start();
911          Thread.sleep(SHORT_DELAY_MS);
# Line 917 | Line 920 | public class LinkedBlockingDequeTest ext
920      public void testTimedPollFirst0() throws InterruptedException {
921          LinkedBlockingDeque q = populatedDeque(SIZE);
922          for (int i = 0; i < SIZE; ++i) {
923 <            assertEquals(i, ((Integer)q.pollFirst(0, MILLISECONDS)).intValue());
923 >            assertEquals(i, q.pollFirst(0, MILLISECONDS));
924          }
925          assertNull(q.pollFirst(0, MILLISECONDS));
926      }
# Line 928 | Line 931 | public class LinkedBlockingDequeTest ext
931      public void testTimedPollFirst() throws InterruptedException {
932          LinkedBlockingDeque q = populatedDeque(SIZE);
933          for (int i = 0; i < SIZE; ++i) {
934 <            assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
934 >            assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
935          }
936          assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
937      }
# Line 938 | Line 941 | public class LinkedBlockingDequeTest ext
941       * returning timeout status
942       */
943      public void testInterruptedTimedPollFirst() throws InterruptedException {
944 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
944 >        Thread t = new Thread(new CheckedRunnable() {
945              public void realRun() throws InterruptedException {
946                  LinkedBlockingDeque q = populatedDeque(SIZE);
947                  for (int i = 0; i < SIZE; ++i) {
948 <                    threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
948 >                    assertEquals(i, q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
949                  }
950 <                q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
951 <            }};
950 >                try {
951 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
952 >                    shouldThrow();
953 >                } catch (InterruptedException success) {}
954 >            }});
955  
956          t.start();
957          Thread.sleep(SHORT_DELAY_MS);
# Line 954 | Line 960 | public class LinkedBlockingDequeTest ext
960      }
961  
962      /**
963 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
964 <     *  on interruption throws
963 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
964 >     * on interruption throws
965       */
966      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
967          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
968 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
968 >        Thread t = new Thread(new CheckedRunnable() {
969              public void realRun() throws InterruptedException {
970 <                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
971 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
972 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
973 <            }};
970 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
971 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
972 >                try {
973 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
974 >                    shouldThrow();
975 >                } catch (InterruptedException success) {}
976 >            }});
977  
978          t.start();
979          Thread.sleep(SMALL_DELAY_MS);
# Line 976 | Line 985 | public class LinkedBlockingDequeTest ext
985      /**
986       * putLast(null) throws NPE
987       */
988 <     public void testPutLastNull() throws InterruptedException {
988 >    public void testPutLastNull() throws InterruptedException {
989          try {
990              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
991              q.putLast(null);
992              shouldThrow();
993          } catch (NullPointerException success) {}
994 <     }
994 >    }
995  
996      /**
997       * all elements successfully putLast are contained
998       */
999 <     public void testPutLast() throws InterruptedException {
1000 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1001 <         for (int i = 0; i < SIZE; ++i) {
1002 <             Integer I = new Integer(i);
1003 <             q.putLast(I);
1004 <             assertTrue(q.contains(I));
1005 <         }
1006 <         assertEquals(0, q.remainingCapacity());
999 >    public void testPutLast() throws InterruptedException {
1000 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1001 >        for (int i = 0; i < SIZE; ++i) {
1002 >            Integer I = new Integer(i);
1003 >            q.putLast(I);
1004 >            assertTrue(q.contains(I));
1005 >        }
1006 >        assertEquals(0, q.remainingCapacity());
1007      }
1008  
1009      /**
1010       * putLast blocks interruptibly if full
1011       */
1012      public void testBlockingPutLast() throws InterruptedException {
1013 <        Thread t = new Thread(new Runnable() {
1014 <                public void run() {
1015 <                    int added = 0;
1016 <                    try {
1017 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1018 <                        for (int i = 0; i < SIZE; ++i) {
1019 <                            q.putLast(new Integer(i));
1020 <                            ++added;
1021 <                        }
1022 <                        q.putLast(new Integer(SIZE));
1023 <                        threadShouldThrow();
1024 <                    } catch (InterruptedException success) {
1025 <                        threadAssertEquals(added, SIZE);
1017 <                    }
1018 <                }});
1013 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1014 >        Thread t = new Thread(new CheckedRunnable() {
1015 >            public void realRun() throws InterruptedException {
1016 >                for (int i = 0; i < SIZE; ++i)
1017 >                    q.putLast(i);
1018 >                assertEquals(SIZE, q.size());
1019 >                assertEquals(0, q.remainingCapacity());
1020 >                try {
1021 >                    q.putLast(99);
1022 >                    shouldThrow();
1023 >                } catch (InterruptedException success) {}
1024 >            }});
1025 >
1026          t.start();
1027          Thread.sleep(SHORT_DELAY_MS);
1028          t.interrupt();
1029          t.join();
1030 +        assertEquals(SIZE, q.size());
1031 +        assertEquals(0, q.remainingCapacity());
1032      }
1033  
1034      /**
1035       * putLast blocks waiting for take when full
1036       */
1037      public void testPutLastWithTake() throws InterruptedException {
1038 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1039 <        Thread t = new Thread(new Runnable() {
1040 <                public void run() {
1041 <                    int added = 0;
1042 <                    try {
1043 <                        q.putLast(new Object());
1044 <                        ++added;
1045 <                        q.putLast(new Object());
1046 <                        ++added;
1047 <                        q.putLast(new Object());
1048 <                        ++added;
1040 <                        q.putLast(new Object());
1041 <                        ++added;
1042 <                        threadShouldThrow();
1043 <                    } catch (InterruptedException success) {
1044 <                        threadAssertTrue(added >= 2);
1045 <                    }
1046 <                }
1047 <            });
1038 >        final int capacity = 2;
1039 >        final LinkedBlockingDeque q = new LinkedBlockingDeque(capacity);
1040 >        Thread t = new Thread(new CheckedRunnable() {
1041 >            public void realRun() throws InterruptedException {
1042 >                for (int i = 0; i < capacity + 1; i++)
1043 >                    q.putLast(i);
1044 >                try {
1045 >                    q.putLast(99);
1046 >                    shouldThrow();
1047 >                } catch (InterruptedException success) {}
1048 >            }});
1049  
1050          t.start();
1051          Thread.sleep(SHORT_DELAY_MS);
1052 <        q.take();
1052 >        assertEquals(q.remainingCapacity(), 0);
1053 >        assertEquals(0, q.take());
1054 >        Thread.sleep(SHORT_DELAY_MS);
1055          t.interrupt();
1056          t.join();
1057 +        assertEquals(q.remainingCapacity(), 0);
1058      }
1059  
1060      /**
# Line 1058 | Line 1062 | public class LinkedBlockingDequeTest ext
1062       */
1063      public void testTimedOfferLast() throws InterruptedException {
1064          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1065 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1065 >        Thread t = new Thread(new CheckedRunnable() {
1066              public void realRun() throws InterruptedException {
1067                  q.putLast(new Object());
1068                  q.putLast(new Object());
1069 <                threadAssertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1070 <                q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1071 <            }};
1069 >                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1070 >                try {
1071 >                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1072 >                    shouldThrow();
1073 >                } catch (InterruptedException success) {}
1074 >            }});
1075  
1076          t.start();
1077          Thread.sleep(SMALL_DELAY_MS);
# Line 1078 | Line 1085 | public class LinkedBlockingDequeTest ext
1085      public void testTakeLast() throws InterruptedException {
1086          LinkedBlockingDeque q = populatedDeque(SIZE);
1087          for (int i = 0; i < SIZE; ++i) {
1088 <            assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1088 >            assertEquals(SIZE-i-1, q.takeLast());
1089          }
1090      }
1091  
# Line 1102 | Line 1109 | public class LinkedBlockingDequeTest ext
1109       * TakeLast removes existing elements until empty, then blocks interruptibly
1110       */
1111      public void testBlockingTakeLast() throws InterruptedException {
1112 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1112 >        final LinkedBlockingDeque q = populatedDeque(SIZE);
1113 >        Thread t = new Thread(new CheckedRunnable() {
1114              public void realRun() throws InterruptedException {
1115 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1116 <                for (int i = 0; i < SIZE; ++i) {
1117 <                    assertEquals(SIZE-i-1, ((Integer)q.takeLast()).intValue());
1118 <                }
1119 <                q.takeLast();
1120 <            }};
1115 >                for (int i = 0; i < SIZE; ++i)
1116 >                    assertEquals(SIZE - 1 - i, q.takeLast());
1117 >                try {
1118 >                    q.takeLast();
1119 >                    shouldThrow();
1120 >                } catch (InterruptedException success) {}
1121 >            }});
1122  
1123          t.start();
1124          Thread.sleep(SHORT_DELAY_MS);
# Line 1117 | Line 1126 | public class LinkedBlockingDequeTest ext
1126          t.join();
1127      }
1128  
1120
1129      /**
1130       * timed pollLast with zero timeout succeeds when non-empty, else times out
1131       */
1132      public void testTimedPollLast0() throws InterruptedException {
1133          LinkedBlockingDeque q = populatedDeque(SIZE);
1134          for (int i = 0; i < SIZE; ++i) {
1135 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(0, MILLISECONDS)).intValue());
1135 >            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1136          }
1137          assertNull(q.pollLast(0, MILLISECONDS));
1138      }
# Line 1135 | Line 1143 | public class LinkedBlockingDequeTest ext
1143      public void testTimedPollLast() throws InterruptedException {
1144          LinkedBlockingDeque q = populatedDeque(SIZE);
1145          for (int i = 0; i < SIZE; ++i) {
1146 <            assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1146 >            assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1147          }
1148          assertNull(q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1149      }
# Line 1145 | Line 1153 | public class LinkedBlockingDequeTest ext
1153       * returning timeout status
1154       */
1155      public void testInterruptedTimedPollLast() throws InterruptedException {
1156 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1156 >        Thread t = new Thread(new CheckedRunnable() {
1157              public void realRun() throws InterruptedException {
1158                  LinkedBlockingDeque q = populatedDeque(SIZE);
1159                  for (int i = 0; i < SIZE; ++i) {
1160 <                    threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1160 >                    assertEquals(SIZE-i-1, q.pollLast(SHORT_DELAY_MS, MILLISECONDS));
1161                  }
1162 <                q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1163 <            }};
1162 >                try {
1163 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1164 >                    shouldThrow();
1165 >                } catch (InterruptedException success) {}
1166 >            }});
1167  
1168          t.start();
1169          Thread.sleep(SHORT_DELAY_MS);
# Line 1161 | Line 1172 | public class LinkedBlockingDequeTest ext
1172      }
1173  
1174      /**
1175 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1176 <     *  on interruption throws
1175 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1176 >     * on interruption throws
1177       */
1178      public void testTimedPollWithOfferLast() throws InterruptedException {
1179          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 1190 | Line 1201 | public class LinkedBlockingDequeTest ext
1201      public void testElement() {
1202          LinkedBlockingDeque q = populatedDeque(SIZE);
1203          for (int i = 0; i < SIZE; ++i) {
1204 <            assertEquals(i, ((Integer)q.element()).intValue());
1204 >            assertEquals(i, q.element());
1205              q.poll();
1206          }
1207          try {
# Line 1205 | Line 1216 | public class LinkedBlockingDequeTest ext
1216      public void testRemoveElement() {
1217          LinkedBlockingDeque q = populatedDeque(SIZE);
1218          for (int i = 1; i < SIZE; i+=2) {
1219 <            assertTrue(q.remove(new Integer(i)));
1219 >            assertTrue(q.contains(i));
1220 >            assertTrue(q.remove(i));
1221 >            assertFalse(q.contains(i));
1222 >            assertTrue(q.contains(i-1));
1223          }
1224          for (int i = 0; i < SIZE; i+=2) {
1225 <            assertTrue(q.remove(new Integer(i)));
1226 <            assertFalse(q.remove(new Integer(i+1)));
1225 >            assertTrue(q.contains(i));
1226 >            assertTrue(q.remove(i));
1227 >            assertFalse(q.contains(i));
1228 >            assertFalse(q.remove(i+1));
1229 >            assertFalse(q.contains(i+1));
1230          }
1231          assertTrue(q.isEmpty());
1232      }
# Line 1292 | Line 1309 | public class LinkedBlockingDequeTest ext
1309      }
1310  
1311      /**
1312 <     * toArray contains all elements
1312 >     * toArray contains all elements in FIFO order
1313       */
1314      public void testToArray() throws InterruptedException{
1315          LinkedBlockingDeque q = populatedDeque(SIZE);
1316          Object[] o = q.toArray();
1317          for (int i = 0; i < o.length; i++)
1318 <            assertEquals(o[i], q.take());
1318 >            assertSame(o[i], q.poll());
1319      }
1320  
1321      /**
1322 <     * toArray(a) contains all elements
1322 >     * toArray(a) contains all elements in FIFO order
1323       */
1324 <    public void testToArray2() throws InterruptedException {
1325 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1324 >    public void testToArray2() {
1325 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1326          Integer[] ints = new Integer[SIZE];
1327 <        ints = (Integer[])q.toArray(ints);
1327 >        Integer[] array = q.toArray(ints);
1328 >        assertSame(ints, array);
1329          for (int i = 0; i < ints.length; i++)
1330 <            assertEquals(ints[i], q.take());
1330 >            assertSame(ints[i], q.remove());
1331      }
1332  
1333      /**
1334 <     * toArray(null) throws NPE
1334 >     * toArray(null) throws NullPointerException
1335       */
1336 <    public void testToArray_BadArg() {
1336 >    public void testToArray_NullArg() {
1337 >        LinkedBlockingDeque q = populatedDeque(SIZE);
1338          try {
1339 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1321 <            Object o[] = q.toArray(null);
1339 >            q.toArray(null);
1340              shouldThrow();
1341          } catch (NullPointerException success) {}
1342      }
1343  
1344      /**
1345 <     * toArray with incompatible array type throws CCE
1345 >     * toArray(incompatible array type) throws ArrayStoreException
1346       */
1347      public void testToArray1_BadArg() {
1348 +        LinkedBlockingDeque q = populatedDeque(SIZE);
1349          try {
1350 <            LinkedBlockingDeque q = populatedDeque(SIZE);
1332 <            Object o[] = q.toArray(new String[10] );
1350 >            q.toArray(new String[10]);
1351              shouldThrow();
1352          } catch (ArrayStoreException success) {}
1353      }
# Line 1349 | Line 1367 | public class LinkedBlockingDequeTest ext
1367      /**
1368       * iterator.remove removes current element
1369       */
1370 <    public void testIteratorRemove () {
1370 >    public void testIteratorRemove() {
1371          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1372          q.add(two);
1373          q.add(one);
# Line 1360 | Line 1378 | public class LinkedBlockingDequeTest ext
1378          it.remove();
1379  
1380          it = q.iterator();
1381 <        assertEquals(it.next(), one);
1382 <        assertEquals(it.next(), three);
1381 >        assertSame(it.next(), one);
1382 >        assertSame(it.next(), three);
1383          assertFalse(it.hasNext());
1384      }
1385  
# Line 1377 | Line 1395 | public class LinkedBlockingDequeTest ext
1395          assertEquals(0, q.remainingCapacity());
1396          int k = 0;
1397          for (Iterator it = q.iterator(); it.hasNext();) {
1398 <            int i = ((Integer)(it.next())).intValue();
1381 <            assertEquals(++k, i);
1398 >            assertEquals(++k, it.next());
1399          }
1400          assertEquals(3, k);
1401      }
# Line 1386 | Line 1403 | public class LinkedBlockingDequeTest ext
1403      /**
1404       * Modifications do not cause iterators to fail
1405       */
1406 <    public void testWeaklyConsistentIteration () {
1406 >    public void testWeaklyConsistentIteration() {
1407          final LinkedBlockingDeque q = new LinkedBlockingDeque(3);
1408          q.add(one);
1409          q.add(two);
# Line 1400 | Line 1417 | public class LinkedBlockingDequeTest ext
1417  
1418  
1419      /**
1420 <     *  Descending iterator iterates through all elements
1420 >     * Descending iterator iterates through all elements
1421       */
1422      public void testDescendingIterator() {
1423          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 1419 | Line 1436 | public class LinkedBlockingDequeTest ext
1436      }
1437  
1438      /**
1439 <     *  Descending iterator ordering is reverse FIFO
1439 >     * Descending iterator ordering is reverse FIFO
1440       */
1441      public void testDescendingIteratorOrdering() {
1442          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1429 | Line 1446 | public class LinkedBlockingDequeTest ext
1446              q.add(new Integer(1));
1447              int k = 0;
1448              for (Iterator it = q.descendingIterator(); it.hasNext();) {
1449 <                int i = ((Integer)(it.next())).intValue();
1433 <                assertEquals(++k, i);
1449 >                assertEquals(++k, it.next());
1450              }
1451  
1452              assertEquals(3, k);
# Line 1443 | Line 1459 | public class LinkedBlockingDequeTest ext
1459      /**
1460       * descendingIterator.remove removes current element
1461       */
1462 <    public void testDescendingIteratorRemove () {
1462 >    public void testDescendingIteratorRemove() {
1463          final LinkedBlockingDeque q = new LinkedBlockingDeque();
1464          for (int iters = 0; iters < 100; ++iters) {
1465              q.add(new Integer(3));
# Line 1485 | Line 1501 | public class LinkedBlockingDequeTest ext
1501          ExecutorService executor = Executors.newFixedThreadPool(2);
1502          executor.execute(new CheckedRunnable() {
1503              public void realRun() throws InterruptedException {
1504 <                threadAssertFalse(q.offer(three));
1505 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1506 <                threadAssertEquals(0, q.remainingCapacity());
1504 >                assertFalse(q.offer(three));
1505 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
1506 >                assertEquals(0, q.remainingCapacity());
1507              }});
1508  
1509          executor.execute(new CheckedRunnable() {
1510              public void realRun() throws InterruptedException {
1511                  Thread.sleep(SMALL_DELAY_MS);
1512 <                threadAssertEquals(one, q.take());
1512 >                assertSame(one, q.take());
1513              }});
1514  
1515          joinPool(executor);
# Line 1507 | Line 1523 | public class LinkedBlockingDequeTest ext
1523          ExecutorService executor = Executors.newFixedThreadPool(2);
1524          executor.execute(new CheckedRunnable() {
1525              public void realRun() throws InterruptedException {
1526 <                threadAssertNull(q.poll());
1527 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1528 <                threadAssertTrue(q.isEmpty());
1526 >                assertNull(q.poll());
1527 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
1528 >                assertTrue(q.isEmpty());
1529              }});
1530  
1531          executor.execute(new CheckedRunnable() {
# Line 1559 | Line 1575 | public class LinkedBlockingDequeTest ext
1575          try {
1576              q.drainTo(q);
1577              shouldThrow();
1578 <        } catch (IllegalArgumentException success) {
1563 <        }
1578 >        } catch (IllegalArgumentException success) {}
1579      }
1580  
1581      /**
# Line 1630 | Line 1645 | public class LinkedBlockingDequeTest ext
1645      }
1646  
1647      /**
1648 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1648 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1649       */
1650      public void testDrainToN() {
1651          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1639 | Line 1654 | public class LinkedBlockingDequeTest ext
1654                  assertTrue(q.offer(new Integer(j)));
1655              ArrayList l = new ArrayList();
1656              q.drainTo(l, i);
1657 <            int k = (i < SIZE)? i : SIZE;
1657 >            int k = (i < SIZE) ? i : SIZE;
1658              assertEquals(l.size(), k);
1659              assertEquals(q.size(), SIZE-k);
1660              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines