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.15 by jsr166, Sat Nov 21 21:12:55 2009 UTC

# Line 169 | Line 169 | public class LinkedBlockingDequeTest ext
169              q.getFirst();
170              shouldThrow();
171          } catch (NoSuchElementException success) {}
172 +        assertNull(q.peekFirst());
173      }
174  
175      /**
# Line 199 | Line 200 | public class LinkedBlockingDequeTest ext
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, ((Integer)q.removeLast()).intValue());
213 +        }
214 +        try {
215 +            q.removeLast();
216 +            shouldThrow();
217 +        } catch (NoSuchElementException success) {}
218 +        assertNull(q.peekLast());
219      }
220  
221      /**
# 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 530 | Line 544 | public class LinkedBlockingDequeTest ext
544       * Deque contains all elements, in traversal order, of successful addAll
545       */
546      public void testAddAll5() {
547 <        try {
548 <            Integer[] empty = new Integer[0];
549 <            Integer[] ints = new Integer[SIZE];
550 <            for (int i = 0; i < SIZE; ++i)
551 <                ints[i] = new Integer(i);
552 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
553 <            assertFalse(q.addAll(Arrays.asList(empty)));
554 <            assertTrue(q.addAll(Arrays.asList(ints)));
555 <            for (int i = 0; i < SIZE; ++i)
542 <                assertEquals(ints[i], q.poll());
543 <        }
544 <        finally {}
547 >        Integer[] empty = new Integer[0];
548 >        Integer[] ints = new Integer[SIZE];
549 >        for (int i = 0; i < SIZE; ++i)
550 >            ints[i] = new Integer(i);
551 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
552 >        assertFalse(q.addAll(Arrays.asList(empty)));
553 >        assertTrue(q.addAll(Arrays.asList(ints)));
554 >        for (int i = 0; i < SIZE; ++i)
555 >            assertEquals(ints[i], q.poll());
556      }
557  
558  
559      /**
560       * put(null) throws NPE
561       */
562 <     public void testPutNull() throws InterruptedException {
562 >    public void testPutNull() throws InterruptedException {
563          try {
564              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
565              q.put(null);
566              shouldThrow();
567          } catch (NullPointerException success) {}
568 <     }
568 >    }
569  
570      /**
571       * all elements successfully put are contained
572       */
573 <     public void testPut() throws InterruptedException {
574 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
575 <         for (int i = 0; i < SIZE; ++i) {
576 <             Integer I = new Integer(i);
577 <             q.put(I);
578 <             assertTrue(q.contains(I));
579 <         }
580 <         assertEquals(0, q.remainingCapacity());
573 >    public void testPut() throws InterruptedException {
574 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
575 >        for (int i = 0; i < SIZE; ++i) {
576 >            Integer I = new Integer(i);
577 >            q.put(I);
578 >            assertTrue(q.contains(I));
579 >        }
580 >        assertEquals(0, q.remainingCapacity());
581      }
582  
583      /**
# Line 600 | Line 611 | public class LinkedBlockingDequeTest ext
611       */
612      public void testPutWithTake() throws InterruptedException {
613          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
614 <        Thread t = new Thread(new Runnable() {
615 <                public void run() {
616 <                    int added = 0;
617 <                    try {
618 <                        q.put(new Object());
619 <                        ++added;
620 <                        q.put(new Object());
621 <                        ++added;
622 <                        q.put(new Object());
623 <                        ++added;
624 <                        q.put(new Object());
625 <                        ++added;
626 <                        threadShouldThrow();
627 <                    } catch (InterruptedException success) {
628 <                        threadAssertTrue(added >= 2);
618 <                    }
614 >        Thread t = new Thread(new CheckedRunnable() {
615 >            public void realRun() {
616 >                int added = 0;
617 >                try {
618 >                    q.put(new Object());
619 >                    ++added;
620 >                    q.put(new Object());
621 >                    ++added;
622 >                    q.put(new Object());
623 >                    ++added;
624 >                    q.put(new Object());
625 >                    ++added;
626 >                    threadShouldThrow();
627 >                } catch (InterruptedException success) {
628 >                    threadAssertTrue(added >= 2);
629                  }
630 <            });
630 >            }});
631  
632          t.start();
633          Thread.sleep(SHORT_DELAY_MS);
# Line 729 | Line 739 | public class LinkedBlockingDequeTest ext
739       * returning timeout status
740       */
741      public void testInterruptedTimedPoll() throws InterruptedException {
742 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
742 >        Thread t = new Thread(new CheckedRunnable() {
743              public void realRun() throws InterruptedException {
744                  LinkedBlockingDeque q = populatedDeque(SIZE);
745                  for (int i = 0; i < SIZE; ++i) {
746 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
746 >                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
747                  }
748 <                q.poll(SMALL_DELAY_MS, MILLISECONDS);
749 <            }};
748 >                try {
749 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
750 >                    shouldThrow();
751 >                } catch (InterruptedException success) {}
752 >            }});
753  
754          t.start();
755          Thread.sleep(SHORT_DELAY_MS);
# Line 750 | Line 763 | public class LinkedBlockingDequeTest ext
763       */
764      public void testTimedPollWithOffer() throws InterruptedException {
765          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
766 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
766 >        Thread t = new Thread(new CheckedRunnable() {
767              public void realRun() throws InterruptedException {
768 <                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
769 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
770 <                q.poll(LONG_DELAY_MS, MILLISECONDS);
771 <            }};
768 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
769 >                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
770 >                try {
771 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
772 >                    shouldThrow();
773 >                } catch (InterruptedException success) {}
774 >            }});
775  
776          t.start();
777          Thread.sleep(SMALL_DELAY_MS);
# Line 793 | Line 809 | public class LinkedBlockingDequeTest ext
809       * putFirst blocks interruptibly if full
810       */
811      public void testBlockingPutFirst() throws InterruptedException {
812 <        Thread t = new Thread(new Runnable() {
813 <                public void run() {
814 <                    int added = 0;
815 <                    try {
816 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
817 <                        for (int i = 0; i < SIZE; ++i) {
818 <                            q.putFirst(new Integer(i));
819 <                            ++added;
804 <                        }
805 <                        q.putFirst(new Integer(SIZE));
806 <                        threadShouldThrow();
807 <                    } catch (InterruptedException success) {
808 <                        threadAssertEquals(added, SIZE);
812 >        Thread t = new Thread(new CheckedRunnable() {
813 >            public void realRun() {
814 >                int added = 0;
815 >                try {
816 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
817 >                    for (int i = 0; i < SIZE; ++i) {
818 >                        q.putFirst(new Integer(i));
819 >                        ++added;
820                      }
821 <                }});
821 >                    q.putFirst(new Integer(SIZE));
822 >                    threadShouldThrow();
823 >                } catch (InterruptedException success) {
824 >                    threadAssertEquals(added, SIZE);
825 >                }
826 >            }});
827  
828          t.start();
829          Thread.sleep(SHORT_DELAY_MS);
# Line 820 | Line 836 | public class LinkedBlockingDequeTest ext
836       */
837      public void testPutFirstWithTake() throws InterruptedException {
838          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
839 <        Thread t = new Thread(new Runnable() {
840 <                public void run() {
841 <                    int added = 0;
842 <                    try {
843 <                        q.putFirst(new Object());
844 <                        ++added;
845 <                        q.putFirst(new Object());
846 <                        ++added;
847 <                        q.putFirst(new Object());
848 <                        ++added;
849 <                        q.putFirst(new Object());
850 <                        ++added;
851 <                        threadShouldThrow();
852 <                    } catch (InterruptedException success) {
853 <                        threadAssertTrue(added >= 2);
838 <                    }
839 >        Thread t = new Thread(new CheckedRunnable() {
840 >            public void realRun() {
841 >                int added = 0;
842 >                try {
843 >                    q.putFirst(new Object());
844 >                    ++added;
845 >                    q.putFirst(new Object());
846 >                    ++added;
847 >                    q.putFirst(new Object());
848 >                    ++added;
849 >                    q.putFirst(new Object());
850 >                    ++added;
851 >                    threadShouldThrow();
852 >                } catch (InterruptedException success) {
853 >                    threadAssertTrue(added >= 2);
854                  }
855 <            });
855 >            }});
856  
857          t.start();
858          Thread.sleep(SHORT_DELAY_MS);
# Line 938 | Line 953 | public class LinkedBlockingDequeTest ext
953       * returning timeout status
954       */
955      public void testInterruptedTimedPollFirst() throws InterruptedException {
956 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
956 >        Thread t = new Thread(new CheckedRunnable() {
957              public void realRun() throws InterruptedException {
958                  LinkedBlockingDeque q = populatedDeque(SIZE);
959                  for (int i = 0; i < SIZE; ++i) {
960 <                    threadAssertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
960 >                    assertEquals(i, ((Integer)q.pollFirst(SHORT_DELAY_MS, MILLISECONDS)).intValue());
961                  }
962 <                q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
963 <            }};
962 >                try {
963 >                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
964 >                    shouldThrow();
965 >                } catch (InterruptedException success) {}
966 >            }});
967  
968          t.start();
969          Thread.sleep(SHORT_DELAY_MS);
# Line 959 | Line 977 | public class LinkedBlockingDequeTest ext
977       */
978      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
979          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
980 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
980 >        Thread t = new Thread(new CheckedRunnable() {
981              public void realRun() throws InterruptedException {
982 <                threadAssertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
983 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
984 <                q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
985 <            }};
982 >                assertNull(q.pollFirst(SHORT_DELAY_MS, MILLISECONDS));
983 >                assertSame(zero, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
984 >                try {
985 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
986 >                    shouldThrow();
987 >                } catch (InterruptedException success) {}
988 >            }});
989  
990          t.start();
991          Thread.sleep(SMALL_DELAY_MS);
# Line 1001 | Line 1022 | public class LinkedBlockingDequeTest ext
1022       * putLast blocks interruptibly if full
1023       */
1024      public void testBlockingPutLast() throws InterruptedException {
1025 <        Thread t = new Thread(new Runnable() {
1026 <                public void run() {
1027 <                    int added = 0;
1028 <                    try {
1029 <                        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1030 <                        for (int i = 0; i < SIZE; ++i) {
1031 <                            q.putLast(new Integer(i));
1032 <                            ++added;
1012 <                        }
1013 <                        q.putLast(new Integer(SIZE));
1014 <                        threadShouldThrow();
1015 <                    } catch (InterruptedException success) {
1016 <                        threadAssertEquals(added, SIZE);
1025 >        Thread t = new Thread(new CheckedRunnable() {
1026 >            public void realRun() {
1027 >                int added = 0;
1028 >                try {
1029 >                    LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1030 >                    for (int i = 0; i < SIZE; ++i) {
1031 >                        q.putLast(new Integer(i));
1032 >                        ++added;
1033                      }
1034 <                }});
1034 >                    q.putLast(new Integer(SIZE));
1035 >                    threadShouldThrow();
1036 >                } catch (InterruptedException success) {
1037 >                    threadAssertEquals(added, SIZE);
1038 >                }
1039 >            }});
1040 >
1041          t.start();
1042          Thread.sleep(SHORT_DELAY_MS);
1043          t.interrupt();
# Line 1027 | Line 1049 | public class LinkedBlockingDequeTest ext
1049       */
1050      public void testPutLastWithTake() throws InterruptedException {
1051          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1052 <        Thread t = new Thread(new Runnable() {
1053 <                public void run() {
1054 <                    int added = 0;
1055 <                    try {
1056 <                        q.putLast(new Object());
1057 <                        ++added;
1058 <                        q.putLast(new Object());
1059 <                        ++added;
1060 <                        q.putLast(new Object());
1061 <                        ++added;
1062 <                        q.putLast(new Object());
1063 <                        ++added;
1064 <                        threadShouldThrow();
1065 <                    } catch (InterruptedException success) {
1066 <                        threadAssertTrue(added >= 2);
1045 <                    }
1052 >        Thread t = new Thread(new CheckedRunnable() {
1053 >            public void realRun() {
1054 >                int added = 0;
1055 >                try {
1056 >                    q.putLast(new Object());
1057 >                    ++added;
1058 >                    q.putLast(new Object());
1059 >                    ++added;
1060 >                    q.putLast(new Object());
1061 >                    ++added;
1062 >                    q.putLast(new Object());
1063 >                    ++added;
1064 >                    threadShouldThrow();
1065 >                } catch (InterruptedException success) {
1066 >                    threadAssertTrue(added >= 2);
1067                  }
1068 <            });
1068 >            }});
1069  
1070          t.start();
1071          Thread.sleep(SHORT_DELAY_MS);
# Line 1145 | Line 1166 | public class LinkedBlockingDequeTest ext
1166       * returning timeout status
1167       */
1168      public void testInterruptedTimedPollLast() throws InterruptedException {
1169 <        Thread t = new ThreadShouldThrow(InterruptedException.class) {
1169 >        Thread t = new Thread(new CheckedRunnable() {
1170              public void realRun() throws InterruptedException {
1171                  LinkedBlockingDeque q = populatedDeque(SIZE);
1172                  for (int i = 0; i < SIZE; ++i) {
1173 <                    threadAssertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1173 >                    assertEquals(SIZE-i-1, ((Integer)q.pollLast(SHORT_DELAY_MS, MILLISECONDS)).intValue());
1174                  }
1175 <                q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1176 <            }};
1175 >                try {
1176 >                    q.pollLast(SMALL_DELAY_MS, MILLISECONDS);
1177 >                    shouldThrow();
1178 >                } catch (InterruptedException success) {}
1179 >            }});
1180  
1181          t.start();
1182          Thread.sleep(SHORT_DELAY_MS);
# Line 1559 | Line 1583 | public class LinkedBlockingDequeTest ext
1583          try {
1584              q.drainTo(q);
1585              shouldThrow();
1586 <        } catch (IllegalArgumentException success) {
1563 <        }
1586 >        } catch (IllegalArgumentException success) {}
1587      }
1588  
1589      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines