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.57 by jsr166, Sun Nov 23 22:27:06 2014 UTC vs.
Revision 1.68 by jsr166, Sun Oct 4 18:49:02 2015 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 >
23 > import junit.framework.Test;
24  
25   public class ArrayBlockingQueueTest extends JSR166TestCase {
26  
# Line 35 | Line 37 | public class ArrayBlockingQueueTest exte
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 102 | Line 104 | public class ArrayBlockingQueueTest exte
104       */
105      public void testConstructor5() {
106          Integer[] ints = new Integer[SIZE];
107 <        for (int i = 0; i < SIZE-1; ++i)
107 >        for (int i = 0; i < SIZE - 1; ++i)
108              ints[i] = i;
109          Collection<Integer> elements = Arrays.asList(ints);
110          try {
111 <            new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
111 >            new ArrayBlockingQueue(SIZE, false, elements);
112              shouldThrow();
113          } catch (NullPointerException success) {}
114      }
# Line 157 | Line 159 | public class ArrayBlockingQueueTest exte
159       * remainingCapacity decreases on add, increases on remove
160       */
161      public void testRemainingCapacity() {
162 <        ArrayBlockingQueue q = populatedQueue(SIZE);
162 >        BlockingQueue q = populatedQueue(SIZE);
163          for (int i = 0; i < SIZE; ++i) {
164              assertEquals(i, q.remainingCapacity());
165 <            assertEquals(SIZE-i, q.size());
166 <            q.remove();
165 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
166 >            assertEquals(i, q.remove());
167          }
168          for (int i = 0; i < SIZE; ++i) {
169 <            assertEquals(SIZE-i, q.remainingCapacity());
170 <            assertEquals(i, q.size());
171 <            q.add(new Integer(i));
169 >            assertEquals(SIZE - i, q.remainingCapacity());
170 >            assertEquals(SIZE, q.size() + q.remainingCapacity());
171 >            assertTrue(q.add(i));
172          }
173      }
174  
# Line 183 | Line 185 | public class ArrayBlockingQueueTest exte
185       * add succeeds if not full; throws ISE if full
186       */
187      public void testAdd() {
188 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
189 +        for (int i = 0; i < SIZE; ++i) {
190 +            assertTrue(q.add(new Integer(i)));
191 +        }
192 +        assertEquals(0, q.remainingCapacity());
193          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());
194              q.add(new Integer(SIZE));
195              shouldThrow();
196          } catch (IllegalStateException success) {}
# Line 198 | Line 200 | public class ArrayBlockingQueueTest exte
200       * addAll(this) throws IAE
201       */
202      public void testAddAllSelf() {
203 +        ArrayBlockingQueue q = populatedQueue(SIZE);
204          try {
202            ArrayBlockingQueue q = populatedQueue(SIZE);
205              q.addAll(q);
206              shouldThrow();
207          } catch (IllegalArgumentException success) {}
# Line 210 | Line 212 | public class ArrayBlockingQueueTest exte
212       * possibly adding some elements
213       */
214      public void testAddAll3() {
215 +        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
216 +        Integer[] ints = new Integer[SIZE];
217 +        for (int i = 0; i < SIZE - 1; ++i)
218 +            ints[i] = new Integer(i);
219          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);
220              q.addAll(Arrays.asList(ints));
221              shouldThrow();
222          } catch (NullPointerException success) {}
# Line 224 | Line 226 | public class ArrayBlockingQueueTest exte
226       * addAll throws ISE if not enough room
227       */
228      public void testAddAll4() {
229 +        ArrayBlockingQueue q = new ArrayBlockingQueue(1);
230 +        Integer[] ints = new Integer[SIZE];
231 +        for (int i = 0; i < SIZE; ++i)
232 +            ints[i] = new Integer(i);
233          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);
234              q.addAll(Arrays.asList(ints));
235              shouldThrow();
236          } catch (IllegalStateException success) {}
# Line 255 | Line 257 | public class ArrayBlockingQueueTest exte
257      public void testPut() throws InterruptedException {
258          ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
259          for (int i = 0; i < SIZE; ++i) {
260 <            Integer I = new Integer(i);
261 <            q.put(I);
262 <            assertTrue(q.contains(I));
260 >            Integer x = new Integer(i);
261 >            q.put(x);
262 >            assertTrue(q.contains(x));
263          }
264          assertEquals(0, q.remainingCapacity());
265      }
# Line 570 | Line 572 | public class ArrayBlockingQueueTest exte
572                  assertTrue(changed);
573  
574              assertTrue(q.containsAll(p));
575 <            assertEquals(SIZE-i, q.size());
575 >            assertEquals(SIZE - i, q.size());
576              p.remove();
577          }
578      }
# Line 583 | Line 585 | public class ArrayBlockingQueueTest exte
585              ArrayBlockingQueue q = populatedQueue(SIZE);
586              ArrayBlockingQueue p = populatedQueue(i);
587              assertTrue(q.removeAll(p));
588 <            assertEquals(SIZE-i, q.size());
588 >            assertEquals(SIZE - i, q.size());
589              for (int j = 0; j < i; ++j) {
590 <                Integer I = (Integer)(p.remove());
591 <                assertFalse(q.contains(I));
590 >                Integer x = (Integer)(p.remove());
591 >                assertFalse(q.contains(x));
592              }
593          }
594      }
# Line 617 | Line 619 | public class ArrayBlockingQueueTest exte
619              checkToArray(q);
620              assertEquals(i, q.poll());
621              checkToArray(q);
622 <            q.add(SIZE+i);
622 >            q.add(SIZE + i);
623          }
624          for (int i = 0; i < SIZE; i++) {
625              checkToArray(q);
626 <            assertEquals(SIZE+i, q.poll());
626 >            assertEquals(SIZE + i, q.poll());
627          }
628      }
629  
630      void checkToArray2(ArrayBlockingQueue q) {
631          int size = q.size();
632 <        Integer[] a1 = size == 0 ? null : new Integer[size-1];
632 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
633          Integer[] a2 = new Integer[size];
634 <        Integer[] a3 = new Integer[size+2];
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);
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);
# Line 647 | Line 649 | public class ArrayBlockingQueueTest exte
649              assertSame(b3[i], x);
650          }
651          assertNull(a3[size]);
652 <        assertEquals(42, (int) a3[size+1]);
652 >        assertEquals(42, (int) a3[size + 1]);
653          if (size > 0) {
654              assertNotSame(a1, b1);
655              assertEquals(size, b1.length);
# Line 671 | Line 673 | public class ArrayBlockingQueueTest exte
673              checkToArray2(q);
674              assertEquals(i, q.poll());
675              checkToArray2(q);
676 <            q.add(SIZE+i);
676 >            q.add(SIZE + i);
677          }
678          for (int i = 0; i < SIZE; i++) {
679              checkToArray2(q);
680 <            assertEquals(SIZE+i, q.poll());
680 >            assertEquals(SIZE + i, q.poll());
681          }
682      }
683  
# Line 696 | Line 698 | public class ArrayBlockingQueueTest exte
698      public void testIterator() throws InterruptedException {
699          ArrayBlockingQueue q = populatedQueue(SIZE);
700          Iterator it = q.iterator();
701 <        while (it.hasNext()) {
701 >        int i;
702 >        for (i = 0; it.hasNext(); i++)
703 >            assertTrue(q.contains(it.next()));
704 >        assertEquals(i, SIZE);
705 >        assertIteratorExhausted(it);
706 >
707 >        it = q.iterator();
708 >        for (i = 0; it.hasNext(); i++)
709              assertEquals(it.next(), q.take());
710 <        }
710 >        assertEquals(i, SIZE);
711 >        assertIteratorExhausted(it);
712 >    }
713 >
714 >    /**
715 >     * iterator of empty collection has no elements
716 >     */
717 >    public void testEmptyIterator() {
718 >        assertIteratorExhausted(new ArrayBlockingQueue(SIZE).iterator());
719      }
720  
721      /**
# Line 771 | Line 788 | public class ArrayBlockingQueueTest exte
788          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
789          q.add(one);
790          q.add(two);
774        ExecutorService executor = Executors.newFixedThreadPool(2);
791          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
792 <        executor.execute(new CheckedRunnable() {
793 <            public void realRun() throws InterruptedException {
794 <                assertFalse(q.offer(three));
795 <                threadsStarted.await();
796 <                assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
797 <                assertEquals(0, q.remainingCapacity());
798 <            }});
799 <
800 <        executor.execute(new CheckedRunnable() {
801 <            public void realRun() throws InterruptedException {
802 <                threadsStarted.await();
803 <                assertEquals(0, q.remainingCapacity());
804 <                assertSame(one, q.take());
805 <            }});
806 <
807 <        joinPool(executor);
792 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
793 >        try (PoolCleaner cleaner = cleaner(executor)) {
794 >            executor.execute(new CheckedRunnable() {
795 >                public void realRun() throws InterruptedException {
796 >                    assertFalse(q.offer(three));
797 >                    threadsStarted.await();
798 >                    assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
799 >                    assertEquals(0, q.remainingCapacity());
800 >                }});
801 >
802 >            executor.execute(new CheckedRunnable() {
803 >                public void realRun() throws InterruptedException {
804 >                    threadsStarted.await();
805 >                    assertEquals(0, q.remainingCapacity());
806 >                    assertSame(one, q.take());
807 >                }});
808 >        }
809      }
810  
811      /**
# Line 797 | Line 814 | public class ArrayBlockingQueueTest exte
814      public void testPollInExecutor() {
815          final ArrayBlockingQueue q = new ArrayBlockingQueue(2);
816          final CheckedBarrier threadsStarted = new CheckedBarrier(2);
817 <        ExecutorService executor = Executors.newFixedThreadPool(2);
818 <        executor.execute(new CheckedRunnable() {
819 <            public void realRun() throws InterruptedException {
820 <                assertNull(q.poll());
821 <                threadsStarted.await();
822 <                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
823 <                checkEmpty(q);
824 <            }});
825 <
826 <        executor.execute(new CheckedRunnable() {
827 <            public void realRun() throws InterruptedException {
828 <                threadsStarted.await();
829 <                q.put(one);
830 <            }});
831 <
832 <        joinPool(executor);
817 >        final ExecutorService executor = Executors.newFixedThreadPool(2);
818 >        try (PoolCleaner cleaner = cleaner(executor)) {
819 >            executor.execute(new CheckedRunnable() {
820 >                public void realRun() throws InterruptedException {
821 >                    assertNull(q.poll());
822 >                    threadsStarted.await();
823 >                    assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
824 >                    checkEmpty(q);
825 >                }});
826 >
827 >            executor.execute(new CheckedRunnable() {
828 >                public void realRun() throws InterruptedException {
829 >                    threadsStarted.await();
830 >                    q.put(one);
831 >                }});
832 >        }
833      }
834  
835      /**
# Line 864 | Line 881 | public class ArrayBlockingQueueTest exte
881          final ArrayBlockingQueue q = populatedQueue(SIZE);
882          Thread t = new Thread(new CheckedRunnable() {
883              public void realRun() throws InterruptedException {
884 <                q.put(new Integer(SIZE+1));
884 >                q.put(new Integer(SIZE + 1));
885              }});
886  
887          t.start();
# Line 881 | Line 898 | public class ArrayBlockingQueueTest exte
898       * drainTo(c, n) empties first min(n, size) elements of queue into c
899       */
900      public void testDrainToN() {
901 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE*2);
901 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
902          for (int i = 0; i < SIZE + 2; ++i) {
903              for (int j = 0; j < SIZE; j++)
904                  assertTrue(q.offer(new Integer(j)));
# Line 889 | Line 906 | public class ArrayBlockingQueueTest exte
906              q.drainTo(l, i);
907              int k = (i < SIZE) ? i : SIZE;
908              assertEquals(k, l.size());
909 <            assertEquals(SIZE-k, q.size());
909 >            assertEquals(SIZE - k, q.size());
910              for (int j = 0; j < k; ++j)
911                  assertEquals(l.get(j), new Integer(j));
912 <            while (q.poll() != null) ;
912 >            do {} while (q.poll() != null);
913          }
914      }
915  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines