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.54 by jsr166, Tue Feb 21 02:04:17 2012 UTC vs.
Revision 1.74 by jsr166, Sun Oct 30 21:07:27 2016 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.Arrays;
9 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 >
11   import java.util.ArrayList;
12 + import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Iterator;
15   import java.util.NoSuchElementException;
# Line 18 | 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 static java.util.concurrent.TimeUnit.MILLISECONDS;
22 > import java.util.concurrent.ThreadLocalRandom;
23 >
24 > import junit.framework.Test;
25  
26   public class ArrayBlockingQueueTest extends JSR166TestCase {
27  
# Line 35 | Line 38 | public class ArrayBlockingQueueTest exte
38      }
39  
40      public static void main(String[] args) {
41 <        junit.textui.TestRunner.run(suite());
41 >        main(suite(), args);
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 56 | 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 102 | Line 114 | public class ArrayBlockingQueueTest exte
114       */
115      public void testConstructor5() {
116          Integer[] ints = new Integer[SIZE];
117 <        for (int i = 0; i < SIZE-1; ++i)
117 >        for (int i = 0; i < SIZE - 1; ++i)
118              ints[i] = i;
119          Collection<Integer> elements = Arrays.asList(ints);
120          try {
121 <            new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
121 >            new ArrayBlockingQueue(SIZE, false, elements);
122              shouldThrow();
123          } catch (NullPointerException success) {}
124      }
# Line 157 | Line 169 | public class ArrayBlockingQueueTest exte
169       * remainingCapacity decreases on add, increases on remove
170       */
171      public void testRemainingCapacity() {
172 <        ArrayBlockingQueue q = populatedQueue(SIZE);
172 >        BlockingQueue q = populatedQueue(SIZE);
173          for (int i = 0; i < SIZE; ++i) {
174              assertEquals(i, q.remainingCapacity());
175 <            assertEquals(SIZE-i, q.size());
176 <            q.remove();
175 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
176 >            assertEquals(i, q.remove());
177          }
178          for (int i = 0; i < SIZE; ++i) {
179 <            assertEquals(SIZE-i, q.remainingCapacity());
180 <            assertEquals(i, q.size());
181 <            q.add(new Integer(i));
179 >            assertEquals(SIZE - i, q.remainingCapacity());
180 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
181 >            assertTrue(q.add(i));
182          }
183      }
184  
# Line 183 | Line 195 | public class ArrayBlockingQueueTest exte
195       * add succeeds if not full; throws ISE if full
196       */
197      public void testAdd() {
198 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
199 +        for (int i = 0; i < SIZE; ++i) {
200 +            assertTrue(q.add(new Integer(i)));
201 +        }
202 +        assertEquals(0, q.remainingCapacity());
203          try {
187            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
188            for (int i = 0; i < SIZE; ++i) {
189                assertTrue(q.add(new Integer(i)));
190            }
191            assertEquals(0, q.remainingCapacity());
204              q.add(new Integer(SIZE));
205              shouldThrow();
206          } catch (IllegalStateException success) {}
# Line 198 | Line 210 | public class ArrayBlockingQueueTest exte
210       * addAll(this) throws IAE
211       */
212      public void testAddAllSelf() {
213 +        ArrayBlockingQueue q = populatedQueue(SIZE);
214          try {
202            ArrayBlockingQueue q = populatedQueue(SIZE);
215              q.addAll(q);
216              shouldThrow();
217          } catch (IllegalArgumentException success) {}
# Line 210 | Line 222 | public class ArrayBlockingQueueTest exte
222       * possibly adding some elements
223       */
224      public void testAddAll3() {
225 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
226 +        Integer[] ints = new Integer[SIZE];
227 +        for (int i = 0; i < SIZE - 1; ++i)
228 +            ints[i] = new Integer(i);
229          try {
214            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
215            Integer[] ints = new Integer[SIZE];
216            for (int i = 0; i < SIZE-1; ++i)
217                ints[i] = new Integer(i);
230              q.addAll(Arrays.asList(ints));
231              shouldThrow();
232          } catch (NullPointerException success) {}
# Line 224 | Line 236 | public class ArrayBlockingQueueTest exte
236       * addAll throws ISE if not enough room
237       */
238      public void testAddAll4() {
239 +        ArrayBlockingQueue q = new ArrayBlockingQueue(1);
240 +        Integer[] ints = new Integer[SIZE];
241 +        for (int i = 0; i < SIZE; ++i)
242 +            ints[i] = new Integer(i);
243          try {
228            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
229            Integer[] ints = new Integer[SIZE];
230            for (int i = 0; i < SIZE; ++i)
231                ints[i] = new Integer(i);
244              q.addAll(Arrays.asList(ints));
245              shouldThrow();
246          } catch (IllegalStateException success) {}
# Line 255 | Line 267 | public class ArrayBlockingQueueTest exte
267      public void testPut() throws InterruptedException {
268          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
269          for (int i = 0; i < SIZE; ++i) {
270 <            Integer I = new Integer(i);
271 <            q.put(I);
272 <            assertTrue(q.contains(I));
270 >            Integer x = new Integer(i);
271 >            q.put(x);
272 >            assertTrue(q.contains(x));
273          }
274          assertEquals(0, q.remainingCapacity());
275      }
# Line 449 | 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) {
453                    long t0 = System.nanoTime();
466                      assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
455                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
467                  }
457                long t0 = System.nanoTime();
468                  aboutToWait.countDown();
469                  try {
470 <                    q.poll(MEDIUM_DELAY_MS, MILLISECONDS);
470 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
471                      shouldThrow();
472                  } catch (InterruptedException success) {
473 <                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
473 >                    assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
474                  }
475              }});
476  
477 <        aboutToWait.await();
478 <        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
477 >        await(aboutToWait);
478 >        waitForThreadToEnterWaitState(t);
479          t.interrupt();
480 <        awaitTermination(t, MEDIUM_DELAY_MS);
480 >        awaitTermination(t);
481          checkEmpty(q);
482      }
483  
# Line 570 | Line 580 | public class ArrayBlockingQueueTest exte
580                  assertTrue(changed);
581  
582              assertTrue(q.containsAll(p));
583 <            assertEquals(SIZE-i, q.size());
583 >            assertEquals(SIZE - i, q.size());
584              p.remove();
585          }
586      }
# Line 583 | Line 593 | public class ArrayBlockingQueueTest exte
593              ArrayBlockingQueue q = populatedQueue(SIZE);
594              ArrayBlockingQueue p = populatedQueue(i);
595              assertTrue(q.removeAll(p));
596 <            assertEquals(SIZE-i, q.size());
596 >            assertEquals(SIZE - i, q.size());
597              for (int j = 0; j < i; ++j) {
598 <                Integer I = (Integer)(p.remove());
599 <                assertFalse(q.contains(I));
598 >                Integer x = (Integer)(p.remove());
599 >                assertFalse(q.contains(x));
600              }
601          }
602      }
603  
604 <    /**
605 <     * toArray contains all elements in FIFO order
606 <     */
607 <    public void testToArray() {
608 <        ArrayBlockingQueue q = populatedQueue(SIZE);
609 <        Object[] o = q.toArray();
610 <        for (int i = 0; i < o.length; i++)
611 <            assertSame(o[i], q.poll());
604 >    void checkToArray(ArrayBlockingQueue<Integer> q) {
605 >        int size = q.size();
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(s + i, (int) x);
630 >            for (Object[] a : as)
631 >                assertSame(a1[i], x);
632 >        }
633      }
634  
635      /**
636 <     * toArray(a) contains all elements in FIFO order
636 >     * toArray() and toArray(a) contain all elements in FIFO order
637       */
638 <    public void testToArray2() {
639 <        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
640 <        Integer[] ints = new Integer[SIZE];
641 <        Integer[] array = q.toArray(ints);
642 <        assertSame(ints, array);
643 <        for (int i = 0; i < ints.length; i++)
644 <            assertSame(ints[i], q.poll());
638 >    public void testToArray() {
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 >        int added = size * 2;
649 >        for (int i = 0; i < added; i++) {
650 >            checkToArray(q);
651 >            assertEquals((Integer) i, q.poll());
652 >            q.add(size + i);
653 >        }
654 >        for (int i = 0; i < size; i++) {
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      /**
# Line 630 | Line 678 | public class ArrayBlockingQueueTest exte
678      public void testIterator() throws InterruptedException {
679          ArrayBlockingQueue q = populatedQueue(SIZE);
680          Iterator it = q.iterator();
681 <        while (it.hasNext()) {
681 >        int i;
682 >        for (i = 0; it.hasNext(); i++)
683 >            assertTrue(q.contains(it.next()));
684 >        assertEquals(i, SIZE);
685 >        assertIteratorExhausted(it);
686 >
687 >        it = q.iterator();
688 >        for (i = 0; it.hasNext(); i++)
689              assertEquals(it.next(), q.take());
690 <        }
690 >        assertEquals(i, SIZE);
691 >        assertIteratorExhausted(it);
692 >    }
693 >
694 >    /**
695 >     * iterator of empty collection has no elements
696 >     */
697 >    public void testEmptyIterator() {
698 >        assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator());
699      }
700  
701      /**
# Line 705 | Line 768 | public class ArrayBlockingQueueTest exte
768          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
769          q.add(one);
770          q.add(two);
708        ExecutorService executor = Executors.newFixedThreadPool(2);
771          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
772 <        executor.execute(new CheckedRunnable() {
773 <            public void realRun() throws InterruptedException {
774 <                assertFalse(q.offer(three));
775 <                threadsStarted.await();
776 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
777 <                assertEquals(0, q.remainingCapacity());
778 <            }});
779 <
780 <        executor.execute(new CheckedRunnable() {
781 <            public void realRun() throws InterruptedException {
782 <                threadsStarted.await();
783 <                assertEquals(0, q.remainingCapacity());
784 <                assertSame(one, q.take());
785 <            }});
786 <
787 <        joinPool(executor);
772 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
773 >        try (PoolCleaner cleaner = cleaner(executor)) {
774 >            executor.execute(new CheckedRunnable() {
775 >                public void realRun() throws InterruptedException {
776 >                    assertFalse(q.offer(three));
777 >                    threadsStarted.await();
778 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
779 >                    assertEquals(0, q.remainingCapacity());
780 >                }});
781 >
782 >            executor.execute(new CheckedRunnable() {
783 >                public void realRun() throws InterruptedException {
784 >                    threadsStarted.await();
785 >                    assertEquals(0, q.remainingCapacity());
786 >                    assertSame(one, q.take());
787 >                }});
788 >        }
789      }
790  
791      /**
# Line 731 | Line 794 | public class ArrayBlockingQueueTest exte
794      public void testPollInExecutor() {
795          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
796          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
797 <        ExecutorService executor = Executors.newFixedThreadPool(2);
798 <        executor.execute(new CheckedRunnable() {
799 <            public void realRun() throws InterruptedException {
800 <                assertNull(q.poll());
801 <                threadsStarted.await();
802 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
803 <                checkEmpty(q);
804 <            }});
805 <
806 <        executor.execute(new CheckedRunnable() {
807 <            public void realRun() throws InterruptedException {
808 <                threadsStarted.await();
809 <                q.put(one);
810 <            }});
811 <
812 <        joinPool(executor);
797 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
798 >        try (PoolCleaner cleaner = cleaner(executor)) {
799 >            executor.execute(new CheckedRunnable() {
800 >                public void realRun() throws InterruptedException {
801 >                    assertNull(q.poll());
802 >                    threadsStarted.await();
803 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
804 >                    checkEmpty(q);
805 >                }});
806 >
807 >            executor.execute(new CheckedRunnable() {
808 >                public void realRun() throws InterruptedException {
809 >                    threadsStarted.await();
810 >                    q.put(one);
811 >                }});
812 >        }
813      }
814  
815      /**
# Line 756 | Line 819 | public class ArrayBlockingQueueTest exte
819          Queue x = populatedQueue(SIZE);
820          Queue y = serialClone(x);
821  
822 <        assertTrue(x != y);
822 >        assertNotSame(x, y);
823          assertEquals(x.size(), y.size());
824          assertEquals(x.toString(), y.toString());
825          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 798 | Line 861 | public class ArrayBlockingQueueTest exte
861          final ArrayBlockingQueue q = populatedQueue(SIZE);
862          Thread t = new Thread(new CheckedRunnable() {
863              public void realRun() throws InterruptedException {
864 <                q.put(new Integer(SIZE+1));
864 >                q.put(new Integer(SIZE + 1));
865              }});
866  
867          t.start();
# Line 815 | Line 878 | public class ArrayBlockingQueueTest exte
878       * drainTo(c, n) empties first min(n, size) elements of queue into c
879       */
880      public void testDrainToN() {
881 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
881 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
882          for (int i = 0; i < SIZE + 2; ++i) {
883              for (int j = 0; j < SIZE; j++)
884                  assertTrue(q.offer(new Integer(j)));
# Line 823 | Line 886 | public class ArrayBlockingQueueTest exte
886              q.drainTo(l, i);
887              int k = (i < SIZE) ? i : SIZE;
888              assertEquals(k, l.size());
889 <            assertEquals(SIZE-k, q.size());
889 >            assertEquals(SIZE - k, q.size());
890              for (int j = 0; j < k; ++j)
891                  assertEquals(l.get(j), new Integer(j));
892 <            while (q.poll() != null) ;
892 >            do {} while (q.poll() != null);
893          }
894      }
895  
896 +    /**
897 +     * remove(null), contains(null) always return false
898 +     */
899 +    public void testNeverContainsNull() {
900 +        Collection<?>[] qs = {
901 +            new ArrayBlockingQueue<Object>(10),
902 +            populatedQueue(2),
903 +        };
904 +
905 +        for (Collection<?> q : qs) {
906 +            assertFalse(q.contains(null));
907 +            assertFalse(q.remove(null));
908 +        }
909 +    }
910   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines