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.56 by jsr166, Sat Feb 28 19:59:23 2015 UTC vs.
Revision 1.62 by jsr166, Tue Oct 6 00:03:55 2015 UTC

# Line 37 | Line 37 | public class LinkedBlockingDequeTest ext
37      }
38  
39      public static void main(String[] args) {
40 <        junit.textui.TestRunner.run(suite());
40 >        main(suite(), args);
41      }
42  
43      public static Test suite() {
# Line 82 | Line 82 | public class LinkedBlockingDequeTest ext
82      public void testSize() {
83          LinkedBlockingDeque q = populatedDeque(SIZE);
84          for (int i = 0; i < SIZE; ++i) {
85 <            assertEquals(SIZE-i, q.size());
85 >            assertEquals(SIZE - i, q.size());
86              q.removeFirst();
87          }
88          for (int i = 0; i < SIZE; ++i) {
# Line 147 | Line 147 | public class LinkedBlockingDequeTest ext
147       */
148      public void testPollLast() {
149          LinkedBlockingDeque q = populatedDeque(SIZE);
150 <        for (int i = SIZE-1; i >= 0; --i) {
150 >        for (int i = SIZE - 1; i >= 0; --i) {
151              assertEquals(i, q.pollLast());
152          }
153          assertNull(q.pollLast());
# Line 186 | Line 186 | public class LinkedBlockingDequeTest ext
186       */
187      public void testPeekLast() {
188          LinkedBlockingDeque q = populatedDeque(SIZE);
189 <        for (int i = SIZE-1; i >= 0; --i) {
189 >        for (int i = SIZE - 1; i >= 0; --i) {
190              assertEquals(i, q.peekLast());
191              assertEquals(i, q.pollLast());
192              assertTrue(q.peekLast() == null ||
# Line 216 | Line 216 | public class LinkedBlockingDequeTest ext
216       */
217      public void testLastElement() {
218          LinkedBlockingDeque q = populatedDeque(SIZE);
219 <        for (int i = SIZE-1; i >= 0; --i) {
219 >        for (int i = SIZE - 1; i >= 0; --i) {
220              assertEquals(i, q.getLast());
221              assertEquals(i, q.pollLast());
222          }
# Line 281 | Line 281 | public class LinkedBlockingDequeTest ext
281          }
282          for (int i = 0; i < SIZE; i += 2) {
283              assertTrue(q.removeFirstOccurrence(new Integer(i)));
284 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
284 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
285          }
286          assertTrue(q.isEmpty());
287      }
# Line 296 | Line 296 | public class LinkedBlockingDequeTest ext
296          }
297          for (int i = 0; i < SIZE; i += 2) {
298              assertTrue(q.removeLastOccurrence(new Integer(i)));
299 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
299 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
300          }
301          assertTrue(q.isEmpty());
302      }
# Line 367 | Line 367 | public class LinkedBlockingDequeTest ext
367       */
368      public void testConstructor5() {
369          Integer[] ints = new Integer[SIZE];
370 <        for (int i = 0; i < SIZE-1; ++i)
370 >        for (int i = 0; i < SIZE - 1; ++i)
371              ints[i] = i;
372          Collection<Integer> elements = Arrays.asList(ints);
373          try {
# Line 414 | Line 414 | public class LinkedBlockingDequeTest ext
414              assertEquals(i, q.remove());
415          }
416          for (int i = 0; i < SIZE; ++i) {
417 <            assertEquals(SIZE-i, q.remainingCapacity());
417 >            assertEquals(SIZE - i, q.remainingCapacity());
418              assertEquals(SIZE, q.size() + q.remainingCapacity());
419              assertTrue(q.add(i));
420          }
# Line 424 | Line 424 | public class LinkedBlockingDequeTest ext
424       * push(null) throws NPE
425       */
426      public void testPushNull() {
427 +        LinkedBlockingDeque q = new LinkedBlockingDeque(1);
428          try {
428            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
429              q.push(null);
430              shouldThrow();
431          } catch (NullPointerException success) {}
# Line 435 | Line 435 | public class LinkedBlockingDequeTest ext
435       * push succeeds if not full; throws ISE if full
436       */
437      public void testPush() {
438 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
439 +        for (int i = 0; i < SIZE; ++i) {
440 +            Integer x = new Integer(i);
441 +            q.push(x);
442 +            assertEquals(x, q.peek());
443 +        }
444 +        assertEquals(0, q.remainingCapacity());
445          try {
439            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
440            for (int i = 0; i < SIZE; ++i) {
441                Integer x = new Integer(i);
442                q.push(x);
443                assertEquals(x, q.peek());
444            }
445            assertEquals(0, q.remainingCapacity());
446              q.push(new Integer(SIZE));
447              shouldThrow();
448          } catch (IllegalStateException success) {}
# Line 513 | Line 513 | public class LinkedBlockingDequeTest ext
513      public void testAddAll3() {
514          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
515          Integer[] ints = new Integer[SIZE];
516 <        for (int i = 0; i < SIZE-1; ++i)
516 >        for (int i = 0; i < SIZE - 1; ++i)
517              ints[i] = new Integer(i);
518          Collection<Integer> elements = Arrays.asList(ints);
519          try {
# Line 751 | Line 751 | public class LinkedBlockingDequeTest ext
751          final CountDownLatch aboutToWait = new CountDownLatch(1);
752          Thread t = newStartedThread(new CheckedRunnable() {
753              public void realRun() throws InterruptedException {
754 +                long startTime = System.nanoTime();
755                  for (int i = 0; i < SIZE; ++i) {
755                    long t0 = System.nanoTime();
756                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
757                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
757                  }
759                long t0 = System.nanoTime();
758                  aboutToWait.countDown();
759                  try {
760 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
760 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
761                      shouldThrow();
762                  } catch (InterruptedException success) {
763 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
763 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
764                  }
765              }});
766  
767          aboutToWait.await();
768 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
768 >        waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
769          t.interrupt();
770 <        awaitTermination(t, MEDIUM_DELAY_MS);
770 >        awaitTermination(t);
771          checkEmpty(q);
772      }
773  
# Line 1050 | Line 1048 | public class LinkedBlockingDequeTest ext
1048       * returning timeout status
1049       */
1050      public void testInterruptedTimedPollFirst() throws InterruptedException {
1051 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1052          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1053          Thread t = newStartedThread(new CheckedRunnable() {
1054              public void realRun() throws InterruptedException {
1055 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1055 >                long startTime = System.nanoTime();
1056                  for (int i = 0; i < SIZE; ++i) {
1057                      assertEquals(i, q.pollFirst(LONG_DELAY_MS, MILLISECONDS));
1058                  }
1059  
1060                  Thread.currentThread().interrupt();
1061                  try {
1062 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1062 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1063                      shouldThrow();
1064                  } catch (InterruptedException success) {}
1065                  assertFalse(Thread.interrupted());
# Line 1071 | Line 1070 | public class LinkedBlockingDequeTest ext
1070                      shouldThrow();
1071                  } catch (InterruptedException success) {}
1072                  assertFalse(Thread.interrupted());
1073 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1074              }});
1075  
1076          await(pleaseInterrupt);
# Line 1246 | Line 1246 | public class LinkedBlockingDequeTest ext
1246      public void testTakeLast() throws InterruptedException {
1247          LinkedBlockingDeque q = populatedDeque(SIZE);
1248          for (int i = 0; i < SIZE; ++i) {
1249 <            assertEquals(SIZE-i-1, q.takeLast());
1249 >            assertEquals(SIZE - i - 1, q.takeLast());
1250          }
1251      }
1252  
# Line 1259 | Line 1259 | public class LinkedBlockingDequeTest ext
1259          Thread t = newStartedThread(new CheckedRunnable() {
1260              public void realRun() throws InterruptedException {
1261                  for (int i = 0; i < SIZE; ++i) {
1262 <                    assertEquals(SIZE-i-1, q.takeLast());
1262 >                    assertEquals(SIZE - i - 1, q.takeLast());
1263                  }
1264  
1265                  Thread.currentThread().interrupt();
# Line 1289 | Line 1289 | public class LinkedBlockingDequeTest ext
1289      public void testTimedPollLast0() throws InterruptedException {
1290          LinkedBlockingDeque q = populatedDeque(SIZE);
1291          for (int i = 0; i < SIZE; ++i) {
1292 <            assertEquals(SIZE-i-1, q.pollLast(0, MILLISECONDS));
1292 >            assertEquals(SIZE - i - 1, q.pollLast(0, MILLISECONDS));
1293          }
1294          assertNull(q.pollLast(0, MILLISECONDS));
1295      }
# Line 1301 | Line 1301 | public class LinkedBlockingDequeTest ext
1301          LinkedBlockingDeque q = populatedDeque(SIZE);
1302          for (int i = 0; i < SIZE; ++i) {
1303              long startTime = System.nanoTime();
1304 <            assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1304 >            assertEquals(SIZE - i - 1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1305              assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1306          }
1307          long startTime = System.nanoTime();
# Line 1315 | Line 1315 | public class LinkedBlockingDequeTest ext
1315       * returning timeout status
1316       */
1317      public void testInterruptedTimedPollLast() throws InterruptedException {
1318 +        final LinkedBlockingDeque q = populatedDeque(SIZE);
1319          final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
1320          Thread t = newStartedThread(new CheckedRunnable() {
1321              public void realRun() throws InterruptedException {
1322 <                LinkedBlockingDeque q = populatedDeque(SIZE);
1322 >                long startTime = System.nanoTime();
1323                  for (int i = 0; i < SIZE; ++i) {
1324 <                    assertEquals(SIZE-i-1, q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1324 >                    assertEquals(SIZE - i - 1,
1325 >                                 q.pollLast(LONG_DELAY_MS, MILLISECONDS));
1326                  }
1327  
1328                  Thread.currentThread().interrupt();
# Line 1336 | Line 1338 | public class LinkedBlockingDequeTest ext
1338                      shouldThrow();
1339                  } catch (InterruptedException success) {}
1340                  assertFalse(Thread.interrupted());
1341 +
1342 +                assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1343              }});
1344  
1345          await(pleaseInterrupt);
1346          assertThreadStaysAlive(t);
1347          t.interrupt();
1348          awaitTermination(t);
1349 +        checkEmpty(q);
1350      }
1351  
1352      /**
# Line 1458 | Line 1463 | public class LinkedBlockingDequeTest ext
1463                  assertTrue(changed);
1464  
1465              assertTrue(q.containsAll(p));
1466 <            assertEquals(SIZE-i, q.size());
1466 >            assertEquals(SIZE - i, q.size());
1467              p.remove();
1468          }
1469      }
# Line 1471 | Line 1476 | public class LinkedBlockingDequeTest ext
1476              LinkedBlockingDeque q = populatedDeque(SIZE);
1477              LinkedBlockingDeque p = populatedDeque(i);
1478              assertTrue(q.removeAll(p));
1479 <            assertEquals(SIZE-i, q.size());
1479 >            assertEquals(SIZE - i, q.size());
1480              for (int j = 0; j < i; ++j) {
1481                  Integer x = (Integer)(p.remove());
1482                  assertFalse(q.contains(x));
# Line 1670 | Line 1675 | public class LinkedBlockingDequeTest ext
1675          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1676          q.add(one);
1677          q.add(two);
1673        ExecutorService executor = Executors.newFixedThreadPool(2);
1678          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1679 <        executor.execute(new CheckedRunnable() {
1680 <            public void realRun() throws InterruptedException {
1681 <                assertFalse(q.offer(three));
1682 <                threadsStarted.await();
1683 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1684 <                assertEquals(0, q.remainingCapacity());
1685 <            }});
1686 <
1687 <        executor.execute(new CheckedRunnable() {
1688 <            public void realRun() throws InterruptedException {
1689 <                threadsStarted.await();
1690 <                assertSame(one, q.take());
1691 <            }});
1692 <
1693 <        joinPool(executor);
1679 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1680 >        try (PoolCleaner cleaner = cleaner(executor)) {
1681 >            executor.execute(new CheckedRunnable() {
1682 >                public void realRun() throws InterruptedException {
1683 >                    assertFalse(q.offer(three));
1684 >                    threadsStarted.await();
1685 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
1686 >                    assertEquals(0, q.remainingCapacity());
1687 >                }});
1688 >
1689 >            executor.execute(new CheckedRunnable() {
1690 >                public void realRun() throws InterruptedException {
1691 >                    threadsStarted.await();
1692 >                    assertSame(one, q.take());
1693 >                }});
1694 >        }
1695      }
1696  
1697      /**
# Line 1695 | Line 1700 | public class LinkedBlockingDequeTest ext
1700      public void testPollInExecutor() {
1701          final LinkedBlockingDeque q = new LinkedBlockingDeque(2);
1702          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
1703 <        ExecutorService executor = Executors.newFixedThreadPool(2);
1704 <        executor.execute(new CheckedRunnable() {
1705 <            public void realRun() throws InterruptedException {
1706 <                assertNull(q.poll());
1707 <                threadsStarted.await();
1708 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1709 <                checkEmpty(q);
1710 <            }});
1711 <
1712 <        executor.execute(new CheckedRunnable() {
1713 <            public void realRun() throws InterruptedException {
1714 <                threadsStarted.await();
1715 <                q.put(one);
1716 <            }});
1717 <
1718 <        joinPool(executor);
1703 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
1704 >        try (PoolCleaner cleaner = cleaner(executor)) {
1705 >            executor.execute(new CheckedRunnable() {
1706 >                public void realRun() throws InterruptedException {
1707 >                    assertNull(q.poll());
1708 >                    threadsStarted.await();
1709 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
1710 >                    checkEmpty(q);
1711 >                }});
1712 >
1713 >            executor.execute(new CheckedRunnable() {
1714 >                public void realRun() throws InterruptedException {
1715 >                    threadsStarted.await();
1716 >                    q.put(one);
1717 >                }});
1718 >        }
1719      }
1720  
1721      /**
# Line 1762 | Line 1767 | public class LinkedBlockingDequeTest ext
1767          final LinkedBlockingDeque q = populatedDeque(SIZE);
1768          Thread t = new Thread(new CheckedRunnable() {
1769              public void realRun() throws InterruptedException {
1770 <                q.put(new Integer(SIZE+1));
1770 >                q.put(new Integer(SIZE + 1));
1771              }});
1772  
1773          t.start();
# Line 1787 | Line 1792 | public class LinkedBlockingDequeTest ext
1792              q.drainTo(l, i);
1793              int k = (i < SIZE) ? i : SIZE;
1794              assertEquals(k, l.size());
1795 <            assertEquals(SIZE-k, q.size());
1795 >            assertEquals(SIZE - k, q.size());
1796              for (int j = 0; j < k; ++j)
1797                  assertEquals(l.get(j), new Integer(j));
1798              do {} while (q.poll() != null);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines