ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ArrayBlockingQueueTest.java (file contents):
Revision 1.69 by jsr166, Mon Oct 5 23:06:12 2015 UTC vs.
Revision 1.74 by jsr166, Sun Oct 30 21:07:27 2016 UTC

# Line 19 | Line 19 | import java.util.concurrent.BlockingQueu
19   import java.util.concurrent.CountDownLatch;
20   import java.util.concurrent.Executors;
21   import java.util.concurrent.ExecutorService;
22 + import java.util.concurrent.ThreadLocalRandom;
23  
24   import junit.framework.Test;
25  
# Line 41 | Line 42 | public class ArrayBlockingQueueTest exte
42      }
43  
44      public static Test suite() {
45 +        class Implementation implements CollectionImplementation {
46 +            public Class<?> klazz() { return ArrayBlockingQueue.class; }
47 +            public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); }
48 +            public Object makeElement(int i) { return i; }
49 +            public boolean isConcurrent() { return true; }
50 +            public boolean permitsNulls() { return false; }
51 +        }
52          return newTestSuite(ArrayBlockingQueueTest.class,
53                              new Fair().testSuite(),
54 <                            new NonFair().testSuite());
54 >                            new NonFair().testSuite(),
55 >                            CollectionTest.testSuite(new Implementation()));
56      }
57  
58      /**
59       * Returns a new queue of given size containing consecutive
60 <     * Integers 0 ... n.
60 >     * Integers 0 ... n - 1.
61       */
62      private ArrayBlockingQueue<Integer> populatedQueue(int n) {
63          ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
# Line 58 | Line 67 | public class ArrayBlockingQueueTest exte
67          assertFalse(q.isEmpty());
68          assertEquals(0, q.remainingCapacity());
69          assertEquals(n, q.size());
70 +        assertEquals((Integer) 0, q.peek());
71          return q;
72      }
73  
# Line 451 | Line 461 | public class ArrayBlockingQueueTest exte
461          final CountDownLatch aboutToWait = new CountDownLatch(1);
462          Thread t = newStartedThread(new CheckedRunnable() {
463              public void realRun() throws InterruptedException {
464 +                long startTime = System.nanoTime();
465                  for (int i = 0; i < SIZE; ++i) {
466 <                    long startTime = System.nanoTime();
456 <                    assertEquals(i, (int) q.poll(2*LONG_DELAY_MS, MILLISECONDS));
457 <                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
466 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
467                  }
459                long startTime = System.nanoTime();
468                  aboutToWait.countDown();
469                  try {
470 <                    q.poll(2*LONG_DELAY_MS, MILLISECONDS);
470 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
471                      shouldThrow();
472                  } catch (InterruptedException success) {
473                      assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
# Line 467 | Line 475 | public class ArrayBlockingQueueTest exte
475              }});
476  
477          await(aboutToWait);
478 <        waitForThreadToEnterWaitState(t, LONG_DELAY_MS);
478 >        waitForThreadToEnterWaitState(t);
479          t.interrupt();
480 <        awaitTermination(t, LONG_DELAY_MS);
480 >        awaitTermination(t);
481          checkEmpty(q);
482      }
483  
# Line 593 | Line 601 | public class ArrayBlockingQueueTest exte
601          }
602      }
603  
604 <    void checkToArray(ArrayBlockingQueue q) {
604 >    void checkToArray(ArrayBlockingQueue<Integer> q) {
605          int size = q.size();
606 <        Object[] o = q.toArray();
607 <        assertEquals(size, o.length);
606 >        Object[] a1 = q.toArray();
607 >        assertEquals(size, a1.length);
608 >        Integer[] a2 = q.toArray(new Integer[0]);
609 >        assertEquals(size, a2.length);
610 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
611 >        assertEquals(size, a3.length);
612 >        Integer[] a4 = new Integer[size];
613 >        assertSame(a4, q.toArray(a4));
614 >        Integer[] a5 = new Integer[size + 1];
615 >        Arrays.fill(a5, 42);
616 >        assertSame(a5, q.toArray(a5));
617 >        Integer[] a6 = new Integer[size + 2];
618 >        Arrays.fill(a6, 42);
619 >        assertSame(a6, q.toArray(a6));
620 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
621 >        for (Object[] a : as) {
622 >            if (a.length > size) assertNull(a[size]);
623 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
624 >        }
625          Iterator it = q.iterator();
626 +        Integer s = q.peek();
627          for (int i = 0; i < size; i++) {
628              Integer x = (Integer) it.next();
629 <            assertEquals((Integer)o[0] + i, (int) x);
630 <            assertSame(o[i], x);
629 >            assertEquals(s + i, (int) x);
630 >            for (Object[] a : as)
631 >                assertSame(a1[i], x);
632          }
633      }
634  
635      /**
636 <     * toArray() contains all elements in FIFO order
636 >     * toArray() and toArray(a) contain all elements in FIFO order
637       */
638      public void testToArray() {
639 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
640 <        for (int i = 0; i < SIZE; i++) {
639 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
640 >        final int size = rnd.nextInt(6);
641 >        final int capacity = Math.max(1, size + rnd.nextInt(size + 1));
642 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
643 >        for (int i = 0; i < size; i++) {
644              checkToArray(q);
645              q.add(i);
646          }
647          // Provoke wraparound
648 <        for (int i = 0; i < SIZE; i++) {
648 >        int added = size * 2;
649 >        for (int i = 0; i < added; i++) {
650              checkToArray(q);
651 <            assertEquals(i, q.poll());
652 <            checkToArray(q);
622 <            q.add(SIZE + i);
623 <        }
624 <        for (int i = 0; i < SIZE; i++) {
625 <            checkToArray(q);
626 <            assertEquals(SIZE + i, q.poll());
651 >            assertEquals((Integer) i, q.poll());
652 >            q.add(size + i);
653          }
628    }
629
630    void checkToArray2(ArrayBlockingQueue q) {
631        int size = q.size();
632        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
633        Integer[] a2 = new Integer[size];
634        Integer[] a3 = new Integer[size + 2];
635        if (size > 0) Arrays.fill(a1, 42);
636        Arrays.fill(a2, 42);
637        Arrays.fill(a3, 42);
638        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
639        Integer[] b2 = (Integer[]) q.toArray(a2);
640        Integer[] b3 = (Integer[]) q.toArray(a3);
641        assertSame(a2, b2);
642        assertSame(a3, b3);
643        Iterator it = q.iterator();
654          for (int i = 0; i < size; i++) {
655 <            Integer x = (Integer) it.next();
656 <            assertSame(b1[i], x);
647 <            assertEquals(b1[0] + i, (int) x);
648 <            assertSame(b2[i], x);
649 <            assertSame(b3[i], x);
650 <        }
651 <        assertNull(a3[size]);
652 <        assertEquals(42, (int) a3[size + 1]);
653 <        if (size > 0) {
654 <            assertNotSame(a1, b1);
655 <            assertEquals(size, b1.length);
656 <            for (int i = 0; i < a1.length; i++) {
657 <                assertEquals(42, (int) a1[i]);
658 <            }
659 <        }
660 <    }
661 <
662 <    /**
663 <     * toArray(a) contains all elements in FIFO order
664 <     */
665 <    public void testToArray2() {
666 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
667 <        for (int i = 0; i < SIZE; i++) {
668 <            checkToArray2(q);
669 <            q.add(i);
670 <        }
671 <        // Provoke wraparound
672 <        for (int i = 0; i < SIZE; i++) {
673 <            checkToArray2(q);
674 <            assertEquals(i, q.poll());
675 <            checkToArray2(q);
676 <            q.add(SIZE + i);
677 <        }
678 <        for (int i = 0; i < SIZE; i++) {
679 <            checkToArray2(q);
680 <            assertEquals(SIZE + i, q.poll());
655 >            checkToArray(q);
656 >            assertEquals((Integer) (added + i), q.poll());
657          }
658      }
659  
660      /**
661       * toArray(incompatible array type) throws ArrayStoreException
662       */
663 <    public void testToArray1_BadArg() {
663 >    public void testToArray_incompatibleArrayType() {
664          ArrayBlockingQueue q = populatedQueue(SIZE);
665          try {
666              q.toArray(new String[10]);
667              shouldThrow();
668          } catch (ArrayStoreException success) {}
669 +        try {
670 +            q.toArray(new String[0]);
671 +            shouldThrow();
672 +        } catch (ArrayStoreException success) {}
673      }
674  
675      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines