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.53 by jsr166, Tue Feb 21 01:54:03 2012 UTC vs.
Revision 1.58 by jsr166, Wed Dec 31 19:05:42 2014 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 45 | Line 47 | public class ArrayBlockingQueueTest exte
47      }
48  
49      /**
50 <     * Creates a queue of given size containing consecutive
50 >     * Returns a new queue of given size containing consecutive
51       * Integers 0 ... n.
52       */
53      private ArrayBlockingQueue<Integer> populatedQueue(int n) {
# Line 591 | Line 593 | public class ArrayBlockingQueueTest exte
593          }
594      }
595  
596 +    void checkToArray(ArrayBlockingQueue q) {
597 +        int size = q.size();
598 +        Object[] o = q.toArray();
599 +        assertEquals(size, o.length);
600 +        Iterator it = q.iterator();
601 +        for (int i = 0; i < size; i++) {
602 +            Integer x = (Integer) it.next();
603 +            assertEquals((Integer)o[0] + i, (int) x);
604 +            assertSame(o[i], x);
605 +        }
606 +    }
607 +
608      /**
609 <     * toArray contains all elements in FIFO order
609 >     * toArray() contains all elements in FIFO order
610       */
611      public void testToArray() {
612 <        ArrayBlockingQueue q = populatedQueue(SIZE);
613 <        Object[] o = q.toArray();
614 <        for (int i = 0; i < o.length; i++)
615 <            assertSame(o[i], q.poll());
612 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
613 >        for (int i = 0; i < SIZE; i++) {
614 >            checkToArray(q);
615 >            q.add(i);
616 >        }
617 >        // Provoke wraparound
618 >        for (int i = 0; i < SIZE; i++) {
619 >            checkToArray(q);
620 >            assertEquals(i, q.poll());
621 >            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());
627 >        }
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();
644 >        for (int i = 0; i < size; i++) {
645 >            Integer x = (Integer) it.next();
646 >            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<Integer> q = populatedQueue(SIZE);
667 <        Integer[] ints = new Integer[SIZE];
668 <        Integer[] array = q.toArray(ints);
669 <        assertSame(ints, array);
670 <        for (int i = 0; i < ints.length; i++)
671 <            assertSame(ints[i], q.poll());
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());
681 >        }
682      }
683  
684      /**
# Line 756 | Line 824 | public class ArrayBlockingQueueTest exte
824          Queue x = populatedQueue(SIZE);
825          Queue y = serialClone(x);
826  
827 <        assertTrue(x != y);
827 >        assertNotSame(x, y);
828          assertEquals(x.size(), y.size());
829          assertEquals(x.toString(), y.toString());
830          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 830 | Line 898 | public class ArrayBlockingQueueTest exte
898          }
899      }
900  
901 +    /**
902 +     * remove(null), contains(null) always return false
903 +     */
904 +    public void testNeverContainsNull() {
905 +        Collection<?>[] qs = {
906 +            new ArrayBlockingQueue<Object>(10),
907 +            populatedQueue(2),
908 +        };
909 +
910 +        for (Collection<?> q : qs) {
911 +            assertFalse(q.contains(null));
912 +            assertFalse(q.remove(null));
913 +        }
914 +    }
915   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines