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.20 by jsr166, Tue Dec 1 09:56:28 2009 UTC vs.
Revision 1.38 by jsr166, Sat May 21 06:24:33 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);
# Line 104 | Line 120 | public class LinkedBlockingDequeTest ext
120      }
121  
122      /**
123 <     *  pollLast succeeds unless empty
123 >     * pollLast succeeds unless empty
124       */
125      public void testPollLast() {
126          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 115 | Line 131 | public class LinkedBlockingDequeTest ext
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);
# Line 129 | Line 145 | public class LinkedBlockingDequeTest ext
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);
# Line 143 | Line 159 | public class LinkedBlockingDequeTest ext
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);
# Line 157 | Line 173 | public class LinkedBlockingDequeTest ext
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);
# Line 173 | Line 189 | public class LinkedBlockingDequeTest ext
189      }
190  
191      /**
192 <     *  getLast returns next element, or throws NSEE if empty
192 >     * getLast() returns last element, or throws NSEE if empty
193       */
194      public void testLastElement() {
195          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 189 | Line 205 | public class LinkedBlockingDequeTest ext
205      }
206  
207      /**
208 <     *  removeFirst removes next element, or throws NSEE if empty
208 >     * removeFirst() removes first element, or throws NSEE if empty
209       */
210      public void testRemoveFirst() {
211          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 204 | Line 220 | public class LinkedBlockingDequeTest ext
220      }
221  
222      /**
223 <     *  removeLast removes last element, or throws NSEE if empty
223 >     * removeLast() removes last element, or throws NSEE if empty
224       */
225      public void testRemoveLast() {
226          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 219 | Line 235 | public class LinkedBlockingDequeTest ext
235      }
236  
237      /**
238 <     *  remove removes next element, or throws NSEE if empty
238 >     * remove removes next element, or throws NSEE if empty
239       */
240      public void testRemove() {
241          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 442 | Line 458 | public class LinkedBlockingDequeTest ext
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);
# Line 513 | Line 529 | public class LinkedBlockingDequeTest ext
529              shouldThrow();
530          } catch (NullPointerException success) {}
531      }
532 +
533      /**
534       * addAll of a collection with any null elements throws NPE after
535       * possibly adding some elements
# Line 527 | Line 544 | public class LinkedBlockingDequeTest ext
544              shouldThrow();
545          } catch (NullPointerException success) {}
546      }
547 +
548      /**
549       * addAll throws ISE if not enough room
550       */
# Line 599 | Line 617 | public class LinkedBlockingDequeTest ext
617              }});
618  
619          t.start();
620 <        Thread.sleep(SHORT_DELAY_MS);
620 >        delay(SHORT_DELAY_MS);
621          t.interrupt();
622          t.join();
623          assertEquals(SIZE, q.size());
# Line 623 | Line 641 | public class LinkedBlockingDequeTest ext
641              }});
642  
643          t.start();
644 <        Thread.sleep(SHORT_DELAY_MS);
644 >        delay(SHORT_DELAY_MS);
645          assertEquals(q.remainingCapacity(), 0);
646          assertEquals(0, q.take());
647 <        Thread.sleep(SHORT_DELAY_MS);
647 >        delay(SHORT_DELAY_MS);
648          t.interrupt();
649          t.join();
650          assertEquals(q.remainingCapacity(), 0);
# Line 637 | Line 655 | public class LinkedBlockingDequeTest ext
655       */
656      public void testTimedOffer() throws InterruptedException {
657          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
658 <        Thread t = new Thread(new CheckedRunnable() {
658 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
659 >        Thread t = newStartedThread(new CheckedRunnable() {
660              public void realRun() throws InterruptedException {
661                  q.put(new Object());
662                  q.put(new Object());
663 <                assertFalse(q.offer(new Object(), SHORT_DELAY_MS, MILLISECONDS));
663 >                long startTime = System.nanoTime();
664 >                assertFalse(q.offer(new Object(), timeoutMillis(), MILLISECONDS));
665 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
666 >                pleaseInterrupt.countDown();
667                  try {
668 <                    q.offer(new Object(), LONG_DELAY_MS, MILLISECONDS);
668 >                    q.offer(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
669                      shouldThrow();
670                  } catch (InterruptedException success) {}
671              }});
672  
673 <        t.start();
652 <        Thread.sleep(SMALL_DELAY_MS);
673 >        await(pleaseInterrupt);
674          t.interrupt();
675 <        t.join();
675 >        awaitTermination(t);
676      }
677  
678      /**
# Line 665 | Line 686 | public class LinkedBlockingDequeTest ext
686      }
687  
688      /**
668     * take blocks interruptibly when empty
669     */
670    public void testTakeFromEmpty() throws InterruptedException {
671        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
672        Thread t = new ThreadShouldThrow(InterruptedException.class) {
673            public void realRun() throws InterruptedException {
674                q.take();
675            }};
676
677        t.start();
678        Thread.sleep(SHORT_DELAY_MS);
679        t.interrupt();
680        t.join();
681    }
682
683    /**
689       * Take removes existing elements until empty, then blocks interruptibly
690       */
691      public void testBlockingTake() throws InterruptedException {
# Line 697 | Line 702 | public class LinkedBlockingDequeTest ext
702              }});
703  
704          t.start();
705 <        Thread.sleep(SHORT_DELAY_MS);
705 >        delay(SHORT_DELAY_MS);
706          t.interrupt();
707          t.join();
708      }
# Line 741 | Line 746 | public class LinkedBlockingDequeTest ext
746       * returning timeout status
747       */
748      public void testInterruptedTimedPoll() throws InterruptedException {
749 <        Thread t = new Thread(new CheckedRunnable() {
749 >        final BlockingQueue<Integer> q = populatedDeque(SIZE);
750 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
751 >        Thread t = newStartedThread(new CheckedRunnable() {
752              public void realRun() throws InterruptedException {
746                LinkedBlockingDeque q = populatedDeque(SIZE);
753                  for (int i = 0; i < SIZE; ++i) {
754 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
754 >                    long t0 = System.nanoTime();
755 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
756 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
757                  }
758 +                long t0 = System.nanoTime();
759 +                aboutToWait.countDown();
760                  try {
761 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
752 <                    shouldThrow();
753 <                } catch (InterruptedException success) {}
754 <            }});
755 <
756 <        t.start();
757 <        Thread.sleep(SHORT_DELAY_MS);
758 <        t.interrupt();
759 <        t.join();
760 <    }
761 <
762 <    /**
763 <     *  timed poll before a delayed offer fails; after offer succeeds;
764 <     *  on interruption throws
765 <     */
766 <    public void testTimedPollWithOffer() throws InterruptedException {
767 <        final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
768 <        Thread t = new Thread(new CheckedRunnable() {
769 <            public void realRun() throws InterruptedException {
770 <                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
771 <                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
772 <                try {
773 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
761 >                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
762                      shouldThrow();
763 <                } catch (InterruptedException success) {}
763 >                } catch (InterruptedException success) {
764 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
765 >                }
766              }});
767  
768 <        t.start();
769 <        Thread.sleep(SMALL_DELAY_MS);
780 <        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
768 >        aboutToWait.await();
769 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
770          t.interrupt();
771 <        t.join();
771 >        awaitTermination(t, MEDIUM_DELAY_MS);
772 >        checkEmpty(q);
773      }
774  
785
775      /**
776       * putFirst(null) throws NPE
777       */
778 <     public void testPutFirstNull() throws InterruptedException {
778 >    public void testPutFirstNull() throws InterruptedException {
779          try {
780              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
781              q.putFirst(null);
782              shouldThrow();
783          } catch (NullPointerException success) {}
784 <     }
784 >    }
785  
786      /**
787       * all elements successfully putFirst are contained
788       */
789 <     public void testPutFirst() throws InterruptedException {
790 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
791 <         for (int i = 0; i < SIZE; ++i) {
792 <             Integer I = new Integer(i);
793 <             q.putFirst(I);
794 <             assertTrue(q.contains(I));
795 <         }
796 <         assertEquals(0, q.remainingCapacity());
789 >    public void testPutFirst() throws InterruptedException {
790 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
791 >        for (int i = 0; i < SIZE; ++i) {
792 >            Integer I = new Integer(i);
793 >            q.putFirst(I);
794 >            assertTrue(q.contains(I));
795 >        }
796 >        assertEquals(0, q.remainingCapacity());
797      }
798  
799      /**
# Line 825 | Line 814 | public class LinkedBlockingDequeTest ext
814              }});
815  
816          t.start();
817 <        Thread.sleep(SHORT_DELAY_MS);
817 >        delay(SHORT_DELAY_MS);
818          t.interrupt();
819          t.join();
820          assertEquals(SIZE, q.size());
# Line 849 | Line 838 | public class LinkedBlockingDequeTest ext
838              }});
839  
840          t.start();
841 <        Thread.sleep(SHORT_DELAY_MS);
841 >        delay(SHORT_DELAY_MS);
842          assertEquals(q.remainingCapacity(), 0);
843          assertEquals(capacity - 1, q.take());
844 <        Thread.sleep(SHORT_DELAY_MS);
844 >        delay(SHORT_DELAY_MS);
845          t.interrupt();
846          t.join();
847          assertEquals(q.remainingCapacity(), 0);
# Line 863 | Line 852 | public class LinkedBlockingDequeTest ext
852       */
853      public void testTimedOfferFirst() throws InterruptedException {
854          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
855 <        Thread t = new Thread(new CheckedRunnable() {
855 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
856 >        Thread t = newStartedThread(new CheckedRunnable() {
857              public void realRun() throws InterruptedException {
858                  q.putFirst(new Object());
859                  q.putFirst(new Object());
860 <                assertFalse(q.offerFirst(new Object(), SHORT_DELAY_MS, MILLISECONDS));
860 >                long startTime = System.nanoTime();
861 >                assertFalse(q.offerFirst(new Object(), timeoutMillis(), MILLISECONDS));
862 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
863 >                pleaseInterrupt.countDown();
864                  try {
865 <                    q.offerFirst(new Object(), LONG_DELAY_MS, MILLISECONDS);
865 >                    q.offerFirst(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
866                      shouldThrow();
867                  } catch (InterruptedException success) {}
868              }});
869  
870 <        t.start();
878 <        Thread.sleep(SMALL_DELAY_MS);
870 >        await(pleaseInterrupt);
871          t.interrupt();
872 <        t.join();
872 >        awaitTermination(t);
873      }
874  
875      /**
# Line 901 | Line 893 | public class LinkedBlockingDequeTest ext
893              }};
894  
895          t.start();
896 <        Thread.sleep(SHORT_DELAY_MS);
896 >        delay(SHORT_DELAY_MS);
897          t.interrupt();
898          t.join();
899      }
# Line 922 | Line 914 | public class LinkedBlockingDequeTest ext
914              }});
915  
916          t.start();
917 <        Thread.sleep(SHORT_DELAY_MS);
917 >        delay(SHORT_DELAY_MS);
918          t.interrupt();
919          t.join();
920      }
# Line 968 | Line 960 | public class LinkedBlockingDequeTest ext
960              }});
961  
962          t.start();
963 <        Thread.sleep(SHORT_DELAY_MS);
963 >        delay(SHORT_DELAY_MS);
964          t.interrupt();
965          t.join();
966      }
967  
968      /**
969 <     *  timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
970 <     *  on interruption throws
969 >     * timed pollFirst before a delayed offerFirst fails; after offerFirst succeeds;
970 >     * on interruption throws
971       */
972      public void testTimedPollFirstWithOfferFirst() throws InterruptedException {
973          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 990 | Line 982 | public class LinkedBlockingDequeTest ext
982              }});
983  
984          t.start();
985 <        Thread.sleep(SMALL_DELAY_MS);
985 >        delay(SMALL_DELAY_MS);
986          assertTrue(q.offerFirst(zero, SHORT_DELAY_MS, MILLISECONDS));
987          t.interrupt();
988          t.join();
# Line 999 | Line 991 | public class LinkedBlockingDequeTest ext
991      /**
992       * putLast(null) throws NPE
993       */
994 <     public void testPutLastNull() throws InterruptedException {
994 >    public void testPutLastNull() throws InterruptedException {
995          try {
996              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
997              q.putLast(null);
998              shouldThrow();
999          } catch (NullPointerException success) {}
1000 <     }
1000 >    }
1001  
1002      /**
1003       * all elements successfully putLast are contained
1004       */
1005 <     public void testPutLast() throws InterruptedException {
1006 <         LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007 <         for (int i = 0; i < SIZE; ++i) {
1008 <             Integer I = new Integer(i);
1009 <             q.putLast(I);
1010 <             assertTrue(q.contains(I));
1011 <         }
1012 <         assertEquals(0, q.remainingCapacity());
1005 >    public void testPutLast() throws InterruptedException {
1006 >        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1007 >        for (int i = 0; i < SIZE; ++i) {
1008 >            Integer I = new Integer(i);
1009 >            q.putLast(I);
1010 >            assertTrue(q.contains(I));
1011 >        }
1012 >        assertEquals(0, q.remainingCapacity());
1013      }
1014  
1015      /**
# Line 1038 | Line 1030 | public class LinkedBlockingDequeTest ext
1030              }});
1031  
1032          t.start();
1033 <        Thread.sleep(SHORT_DELAY_MS);
1033 >        delay(SHORT_DELAY_MS);
1034          t.interrupt();
1035          t.join();
1036          assertEquals(SIZE, q.size());
# Line 1062 | Line 1054 | public class LinkedBlockingDequeTest ext
1054              }});
1055  
1056          t.start();
1057 <        Thread.sleep(SHORT_DELAY_MS);
1057 >        delay(SHORT_DELAY_MS);
1058          assertEquals(q.remainingCapacity(), 0);
1059          assertEquals(0, q.take());
1060 <        Thread.sleep(SHORT_DELAY_MS);
1060 >        delay(SHORT_DELAY_MS);
1061          t.interrupt();
1062          t.join();
1063          assertEquals(q.remainingCapacity(), 0);
# Line 1076 | Line 1068 | public class LinkedBlockingDequeTest ext
1068       */
1069      public void testTimedOfferLast() throws InterruptedException {
1070          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1071 <        Thread t = new Thread(new CheckedRunnable() {
1071 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1072 >        Thread t = newStartedThread(new CheckedRunnable() {
1073              public void realRun() throws InterruptedException {
1074                  q.putLast(new Object());
1075                  q.putLast(new Object());
1076 <                assertFalse(q.offerLast(new Object(), SHORT_DELAY_MS, MILLISECONDS));
1076 >                long startTime = System.nanoTime();
1077 >                assertFalse(q.offerLast(new Object(), timeoutMillis(), MILLISECONDS));
1078 >                assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
1079 >                pleaseInterrupt.countDown();
1080                  try {
1081 <                    q.offerLast(new Object(), LONG_DELAY_MS, MILLISECONDS);
1081 >                    q.offerLast(new Object(), 2 * LONG_DELAY_MS, MILLISECONDS);
1082                      shouldThrow();
1083                  } catch (InterruptedException success) {}
1084              }});
1085  
1086 <        t.start();
1091 <        Thread.sleep(SMALL_DELAY_MS);
1086 >        await(pleaseInterrupt);
1087          t.interrupt();
1088 <        t.join();
1088 >        awaitTermination(t);
1089      }
1090  
1091      /**
# Line 1114 | Line 1109 | public class LinkedBlockingDequeTest ext
1109              }};
1110  
1111          t.start();
1112 <        Thread.sleep(SHORT_DELAY_MS);
1112 >        delay(SHORT_DELAY_MS);
1113          t.interrupt();
1114          t.join();
1115      }
# Line 1135 | Line 1130 | public class LinkedBlockingDequeTest ext
1130              }});
1131  
1132          t.start();
1133 <        Thread.sleep(SHORT_DELAY_MS);
1133 >        delay(SHORT_DELAY_MS);
1134          t.interrupt();
1135          t.join();
1136      }
# Line 1180 | Line 1175 | public class LinkedBlockingDequeTest ext
1175              }});
1176  
1177          t.start();
1178 <        Thread.sleep(SHORT_DELAY_MS);
1178 >        delay(SHORT_DELAY_MS);
1179          t.interrupt();
1180          t.join();
1181      }
1182  
1183      /**
1184 <     *  timed poll before a delayed offerLast fails; after offerLast succeeds;
1185 <     *  on interruption throws
1184 >     * timed poll before a delayed offerLast fails; after offerLast succeeds;
1185 >     * on interruption throws
1186       */
1187      public void testTimedPollWithOfferLast() throws InterruptedException {
1188          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
# Line 1202 | Line 1197 | public class LinkedBlockingDequeTest ext
1197              }});
1198  
1199          t.start();
1200 <        Thread.sleep(SMALL_DELAY_MS);
1200 >        delay(SMALL_DELAY_MS);
1201          assertTrue(q.offerLast(zero, SHORT_DELAY_MS, MILLISECONDS));
1202          t.interrupt();
1203          t.join();
# Line 1230 | Line 1225 | public class LinkedBlockingDequeTest ext
1225      public void testRemoveElement() {
1226          LinkedBlockingDeque q = populatedDeque(SIZE);
1227          for (int i = 1; i < SIZE; i+=2) {
1228 <            assertTrue(q.remove(new Integer(i)));
1228 >            assertTrue(q.contains(i));
1229 >            assertTrue(q.remove(i));
1230 >            assertFalse(q.contains(i));
1231 >            assertTrue(q.contains(i-1));
1232          }
1233          for (int i = 0; i < SIZE; i+=2) {
1234 <            assertTrue(q.remove(new Integer(i)));
1235 <            assertFalse(q.remove(new Integer(i+1)));
1234 >            assertTrue(q.contains(i));
1235 >            assertTrue(q.remove(i));
1236 >            assertFalse(q.contains(i));
1237 >            assertFalse(q.remove(i+1));
1238 >            assertFalse(q.contains(i+1));
1239          }
1240          assertTrue(q.isEmpty());
1241      }
# Line 1317 | Line 1318 | public class LinkedBlockingDequeTest ext
1318      }
1319  
1320      /**
1321 <     * toArray contains all elements
1321 >     * toArray contains all elements in FIFO order
1322       */
1323      public void testToArray() throws InterruptedException{
1324          LinkedBlockingDeque q = populatedDeque(SIZE);
1325          Object[] o = q.toArray();
1326          for (int i = 0; i < o.length; i++)
1327 <            assertEquals(o[i], q.take());
1327 >            assertSame(o[i], q.poll());
1328      }
1329  
1330      /**
1331 <     * toArray(a) contains all elements
1331 >     * toArray(a) contains all elements in FIFO order
1332       */
1333 <    public void testToArray2() throws InterruptedException {
1334 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1333 >    public void testToArray2() {
1334 >        LinkedBlockingDeque<Integer> q = populatedDeque(SIZE);
1335          Integer[] ints = new Integer[SIZE];
1336 <        ints = (Integer[])q.toArray(ints);
1336 >        Integer[] array = q.toArray(ints);
1337 >        assertSame(ints, array);
1338          for (int i = 0; i < ints.length; i++)
1339 <            assertEquals(ints[i], q.take());
1339 >            assertSame(ints[i], q.remove());
1340      }
1341  
1342      /**
1343 <     * toArray(null) throws NPE
1343 >     * toArray(null) throws NullPointerException
1344       */
1345 <    public void testToArray_BadArg() {
1345 >    public void testToArray_NullArg() {
1346          LinkedBlockingDeque q = populatedDeque(SIZE);
1347          try {
1348 <            Object o[] = q.toArray(null);
1348 >            q.toArray(null);
1349              shouldThrow();
1350          } catch (NullPointerException success) {}
1351      }
1352  
1353      /**
1354 <     * toArray with incompatible array type throws CCE
1354 >     * toArray(incompatible array type) throws ArrayStoreException
1355       */
1356      public void testToArray1_BadArg() {
1357          LinkedBlockingDeque q = populatedDeque(SIZE);
1358          try {
1359 <            Object o[] = q.toArray(new String[10]);
1359 >            q.toArray(new String[10]);
1360              shouldThrow();
1361          } catch (ArrayStoreException success) {}
1362      }
# Line 1374 | 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 1410 | 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 1424 | Line 1426 | public class LinkedBlockingDequeTest ext
1426  
1427  
1428      /**
1429 <     *  Descending iterator iterates through all elements
1429 >     * Descending iterator iterates through all elements
1430       */
1431      public void testDescendingIterator() {
1432          LinkedBlockingDeque q = populatedDeque(SIZE);
# Line 1443 | Line 1445 | public class LinkedBlockingDequeTest ext
1445      }
1446  
1447      /**
1448 <     *  Descending iterator ordering is reverse FIFO
1448 >     * Descending iterator ordering is reverse FIFO
1449       */
1450      public void testDescendingIteratorOrdering() {
1451          final LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1466 | 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 1515 | Line 1517 | public class LinkedBlockingDequeTest ext
1517  
1518          executor.execute(new CheckedRunnable() {
1519              public void realRun() throws InterruptedException {
1520 <                Thread.sleep(SMALL_DELAY_MS);
1520 >                delay(SMALL_DELAY_MS);
1521                  assertSame(one, q.take());
1522              }});
1523  
# Line 1537 | Line 1539 | public class LinkedBlockingDequeTest ext
1539  
1540          executor.execute(new CheckedRunnable() {
1541              public void realRun() throws InterruptedException {
1542 <                Thread.sleep(SMALL_DELAY_MS);
1542 >                delay(SMALL_DELAY_MS);
1543                  q.put(one);
1544              }});
1545  
# Line 1652 | Line 1654 | public class LinkedBlockingDequeTest ext
1654      }
1655  
1656      /**
1657 <     * drainTo(c, n) empties first max {n, size} elements of deque into c
1657 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
1658       */
1659      public void testDrainToN() {
1660          LinkedBlockingDeque q = new LinkedBlockingDeque();
# Line 1661 | Line 1663 | public class LinkedBlockingDequeTest ext
1663                  assertTrue(q.offer(new Integer(j)));
1664              ArrayList l = new ArrayList();
1665              q.drainTo(l, i);
1666 <            int k = (i < SIZE)? i : SIZE;
1666 >            int k = (i < SIZE) ? i : SIZE;
1667              assertEquals(l.size(), k);
1668              assertEquals(q.size(), SIZE-k);
1669              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines