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.73 by jsr166, Mon Oct 17 01:52:04 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  
27    public static class Fair extends BlockingQueueTest {
28        protected BlockingQueue emptyCollection() {
29            return new ArrayBlockingQueue(SIZE, true);
30        }
31    }
32
33    public static class NonFair extends BlockingQueueTest {
34        protected BlockingQueue emptyCollection() {
35            return new ArrayBlockingQueue(SIZE, false);
36        }
37    }
38
28      public static void main(String[] args) {
29          main(suite(), args);
30      }
# Line 43 | Line 32 | public class ArrayBlockingQueueTest exte
32      public static Test suite() {
33          class Implementation implements CollectionImplementation {
34              public Class<?> klazz() { return ArrayBlockingQueue.class; }
35 <            public Collection emptyCollection() { return new ArrayBlockingQueue(SIZE, false); }
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 <        return newTestSuite(ArrayBlockingQueueTest.class,
44 <                            new Fair().testSuite(),
45 <                            new NonFair().testSuite(),
46 <                            CollectionTest.testSuite(new Implementation()));
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 populatedQueue(0, SIZE, 2 * SIZE, true);
54 >        }
55 >    }
56 >
57 >    public static class NonFair extends BlockingQueueTest {
58 >        protected BlockingQueue emptyCollection() {
59 >            return populatedQueue(0, SIZE, 2 * SIZE, false);
60 >        }
61      }
62  
63      /**
64       * Returns a new queue of given size containing consecutive
65       * Integers 0 ... n - 1.
66       */
67 <    private ArrayBlockingQueue<Integer> populatedQueue(int n) {
68 <        ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(n);
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 - 1, with given capacity range and fairness.
74 >     */
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());
86 <        assertEquals((Integer) 0, q.peek());
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 600 | 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);
627 <            assertEquals(i, q.poll());
628 <            checkToArray(q);
629 <            q.add(SIZE + i);
630 <        }
631 <        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          }
635    }
636
637    void checkToArray2(ArrayBlockingQueue q) {
638        int size = q.size();
639        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
640        Integer[] a2 = new Integer[size];
641        Integer[] a3 = new Integer[size + 2];
642        if (size > 0) Arrays.fill(a1, 42);
643        Arrays.fill(a2, 42);
644        Arrays.fill(a3, 42);
645        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
646        Integer[] b2 = (Integer[]) q.toArray(a2);
647        Integer[] b3 = (Integer[]) q.toArray(a3);
648        assertSame(a2, b2);
649        assertSame(a3, b3);
650        Iterator it = q.iterator();
677          for (int i = 0; i < size; i++) {
678 <            Integer x = (Integer) it.next();
679 <            assertSame(b1[i], x);
654 <            assertEquals(b1[0] + i, (int) x);
655 <            assertSame(b2[i], x);
656 <            assertSame(b3[i], x);
657 <        }
658 <        assertNull(a3[size]);
659 <        assertEquals(42, (int) a3[size + 1]);
660 <        if (size > 0) {
661 <            assertNotSame(a1, b1);
662 <            assertEquals(size, b1.length);
663 <            for (int i = 0; i < a1.length; i++) {
664 <                assertEquals(42, (int) a1[i]);
665 <            }
666 <        }
667 <    }
668 <
669 <    /**
670 <     * toArray(a) contains all elements in FIFO order
671 <     */
672 <    public void testToArray2() {
673 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
674 <        for (int i = 0; i < SIZE; i++) {
675 <            checkToArray2(q);
676 <            q.add(i);
677 <        }
678 <        // Provoke wraparound
679 <        for (int i = 0; i < SIZE; i++) {
680 <            checkToArray2(q);
681 <            assertEquals(i, q.poll());
682 <            checkToArray2(q);
683 <            q.add(SIZE + i);
684 <        }
685 <        for (int i = 0; i < SIZE; i++) {
686 <            checkToArray2(q);
687 <            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