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.71 by jsr166, Sat Aug 6 17:02:49 2016 UTC vs.
Revision 1.76 by jsr166, Sun Nov 6 02:40:38 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  
26   public class ArrayBlockingQueueTest extends JSR166TestCase {
27  
28 +    public static void main(String[] args) {
29 +        main(suite(), args);
30 +    }
31 +
32 +    public static Test suite() {
33 +        class Implementation implements CollectionImplementation {
34 +            public Class<?> klazz() { return ArrayBlockingQueue.class; }
35 +            public Collection emptyCollection() {
36 +                boolean fair = ThreadLocalRandom.current().nextBoolean();
37 +                return populatedQueue(0, SIZE, 2 * SIZE, fair);
38 +            }
39 +            public Object makeElement(int i) { return i; }
40 +            public boolean isConcurrent() { return true; }
41 +            public boolean permitsNulls() { return false; }
42 +        }
43 +
44 +        return newTestSuite(
45 +            ArrayBlockingQueueTest.class,
46 +            new Fair().testSuite(),
47 +            new NonFair().testSuite(),
48 +            CollectionTest.testSuite(new Implementation()));
49 +    }
50 +
51      public static class Fair extends BlockingQueueTest {
52          protected BlockingQueue emptyCollection() {
53 <            return new ArrayBlockingQueue(SIZE, true);
53 >            return populatedQueue(0, SIZE, 2 * SIZE, true);
54          }
55      }
56  
57      public static class NonFair extends BlockingQueueTest {
58          protected BlockingQueue emptyCollection() {
59 <            return new ArrayBlockingQueue(SIZE, false);
59 >            return populatedQueue(0, SIZE, 2 * SIZE, false);
60          }
61      }
62  
63 <    public static void main(String[] args) {
64 <        main(suite(), args);
65 <    }
66 <
67 <    public static Test suite() {
68 <        return newTestSuite(ArrayBlockingQueueTest.class,
45 <                            new Fair().testSuite(),
46 <                            new NonFair().testSuite());
63 >    /**
64 >     * Returns a new queue of given size containing consecutive
65 >     * Integers 0 ... n - 1.
66 >     */
67 >    static ArrayBlockingQueue<Integer> populatedQueue(int n) {
68 >        return populatedQueue(n, n, n, false);
69      }
70  
71      /**
72       * Returns a new queue of given size containing consecutive
73 <     * Integers 0 ... n.
73 >     * Integers 0 ... n - 1, with given capacity range and fairness.
74       */
75 <    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
76 <        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
75 >    static ArrayBlockingQueue<Integer> populatedQueue(
76 >        int size, int minCapacity, int maxCapacity, boolean fair) {
77 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
78 >        int capacity = rnd.nextInt(minCapacity, maxCapacity + 1);
79 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
80          assertTrue(q.isEmpty());
81 <        for (int i = 0; i < n; i++)
82 <            assertTrue(q.offer(new Integer(i)));
83 <        assertFalse(q.isEmpty());
84 <        assertEquals(0, q.remainingCapacity());
85 <        assertEquals(n, q.size());
81 >        // shuffle circular array elements so they wrap
82 >        {
83 >            int n = rnd.nextInt(capacity);
84 >            for (int i = 0; i < n; i++) q.add(42);
85 >            for (int i = 0; i < n; i++) q.remove();
86 >        }
87 >        for (int i = 0; i < size; i++)
88 >            assertTrue(q.offer((Integer) i));
89 >        assertEquals(size == 0, q.isEmpty());
90 >        assertEquals(capacity - size, q.remainingCapacity());
91 >        assertEquals(size, q.size());
92 >        if (size > 0)
93 >            assertEquals((Integer) 0, q.peek());
94          return q;
95      }
96  
# Line 591 | Line 624 | public class ArrayBlockingQueueTest exte
624          }
625      }
626  
627 <    void checkToArray(ArrayBlockingQueue q) {
627 >    void checkToArray(ArrayBlockingQueue<Integer> q) {
628          int size = q.size();
629 <        Object[] o = q.toArray();
630 <        assertEquals(size, o.length);
629 >        Object[] a1 = q.toArray();
630 >        assertEquals(size, a1.length);
631 >        Integer[] a2 = q.toArray(new Integer[0]);
632 >        assertEquals(size, a2.length);
633 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
634 >        assertEquals(size, a3.length);
635 >        Integer[] a4 = new Integer[size];
636 >        assertSame(a4, q.toArray(a4));
637 >        Integer[] a5 = new Integer[size + 1];
638 >        Arrays.fill(a5, 42);
639 >        assertSame(a5, q.toArray(a5));
640 >        Integer[] a6 = new Integer[size + 2];
641 >        Arrays.fill(a6, 42);
642 >        assertSame(a6, q.toArray(a6));
643 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
644 >        for (Object[] a : as) {
645 >            if (a.length > size) assertNull(a[size]);
646 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
647 >        }
648          Iterator it = q.iterator();
649 +        Integer s = q.peek();
650          for (int i = 0; i < size; i++) {
651              Integer x = (Integer) it.next();
652 <            assertEquals((Integer)o[0] + i, (int) x);
653 <            assertSame(o[i], x);
652 >            assertEquals(s + i, (int) x);
653 >            for (Object[] a : as)
654 >                assertSame(a1[i], x);
655          }
656      }
657  
658      /**
659 <     * toArray() contains all elements in FIFO order
659 >     * toArray() and toArray(a) contain all elements in FIFO order
660       */
661      public void testToArray() {
662 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
663 <        for (int i = 0; i < SIZE; i++) {
662 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
663 >        final int size = rnd.nextInt(6);
664 >        final int capacity = Math.max(1, size + rnd.nextInt(size + 1));
665 >        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
666 >        for (int i = 0; i < size; i++) {
667              checkToArray(q);
668              q.add(i);
669          }
670          // Provoke wraparound
671 <        for (int i = 0; i < SIZE; i++) {
672 <            checkToArray(q);
618 <            assertEquals(i, q.poll());
619 <            checkToArray(q);
620 <            q.add(SIZE + i);
621 <        }
622 <        for (int i = 0; i < SIZE; i++) {
671 >        int added = size * 2;
672 >        for (int i = 0; i < added; i++) {
673              checkToArray(q);
674 <            assertEquals(SIZE + i, q.poll());
674 >            assertEquals((Integer) i, q.poll());
675 >            q.add(size + i);
676          }
626    }
627
628    void checkToArray2(ArrayBlockingQueue q) {
629        int size = q.size();
630        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
631        Integer[] a2 = new Integer[size];
632        Integer[] a3 = new Integer[size + 2];
633        if (size > 0) Arrays.fill(a1, 42);
634        Arrays.fill(a2, 42);
635        Arrays.fill(a3, 42);
636        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
637        Integer[] b2 = (Integer[]) q.toArray(a2);
638        Integer[] b3 = (Integer[]) q.toArray(a3);
639        assertSame(a2, b2);
640        assertSame(a3, b3);
641        Iterator it = q.iterator();
677          for (int i = 0; i < size; i++) {
678 <            Integer x = (Integer) it.next();
679 <            assertSame(b1[i], x);
645 <            assertEquals(b1[0] + i, (int) x);
646 <            assertSame(b2[i], x);
647 <            assertSame(b3[i], x);
648 <        }
649 <        assertNull(a3[size]);
650 <        assertEquals(42, (int) a3[size + 1]);
651 <        if (size > 0) {
652 <            assertNotSame(a1, b1);
653 <            assertEquals(size, b1.length);
654 <            for (int i = 0; i < a1.length; i++) {
655 <                assertEquals(42, (int) a1[i]);
656 <            }
657 <        }
658 <    }
659 <
660 <    /**
661 <     * toArray(a) contains all elements in FIFO order
662 <     */
663 <    public void testToArray2() {
664 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
665 <        for (int i = 0; i < SIZE; i++) {
666 <            checkToArray2(q);
667 <            q.add(i);
668 <        }
669 <        // Provoke wraparound
670 <        for (int i = 0; i < SIZE; i++) {
671 <            checkToArray2(q);
672 <            assertEquals(i, q.poll());
673 <            checkToArray2(q);
674 <            q.add(SIZE + i);
675 <        }
676 <        for (int i = 0; i < SIZE; i++) {
677 <            checkToArray2(q);
678 <            assertEquals(SIZE + i, q.poll());
678 >            checkToArray(q);
679 >            assertEquals((Integer) (added + i), q.poll());
680          }
681      }
682  
683      /**
684       * toArray(incompatible array type) throws ArrayStoreException
685       */
686 <    public void testToArray1_BadArg() {
686 >    public void testToArray_incompatibleArrayType() {
687          ArrayBlockingQueue q = populatedQueue(SIZE);
688          try {
689              q.toArray(new String[10]);
690              shouldThrow();
691          } catch (ArrayStoreException success) {}
692 +        try {
693 +            q.toArray(new String[0]);
694 +            shouldThrow();
695 +        } catch (ArrayStoreException success) {}
696      }
697  
698      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines