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.47 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.59 by jsr166, Wed Dec 31 19:21:20 2014 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 < import java.io.*;
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;
16 > import java.util.Queue;
17 > import java.util.concurrent.ArrayBlockingQueue;
18 > import java.util.concurrent.BlockingQueue;
19 > import java.util.concurrent.CountDownLatch;
20 > import java.util.concurrent.Executors;
21 > import java.util.concurrent.ExecutorService;
22 >
23 > import junit.framework.Test;
24  
25   public class ArrayBlockingQueueTest extends JSR166TestCase {
26  
27      public static class Fair extends BlockingQueueTest {
28          protected BlockingQueue emptyCollection() {
29 <            return new ArrayBlockingQueue(20, true);
29 >            return new ArrayBlockingQueue(SIZE, true);
30          }
31      }
32  
33      public static class NonFair extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new ArrayBlockingQueue(20, false);
35 >            return new ArrayBlockingQueue(SIZE, false);
36          }
37      }
38  
# Line 37 | Line 47 | public class ArrayBlockingQueueTest exte
47      }
48  
49      /**
50 <     * Create 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 63 | Line 73 | public class ArrayBlockingQueueTest exte
73       */
74      public void testConstructor2() {
75          try {
76 <            ArrayBlockingQueue q = new ArrayBlockingQueue(0);
76 >            new ArrayBlockingQueue(0);
77              shouldThrow();
78          } catch (IllegalArgumentException success) {}
79      }
# Line 73 | Line 83 | public class ArrayBlockingQueueTest exte
83       */
84      public void testConstructor3() {
85          try {
86 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, true, null);
86 >            new ArrayBlockingQueue(1, true, null);
87              shouldThrow();
88          } catch (NullPointerException success) {}
89      }
# Line 82 | Line 92 | public class ArrayBlockingQueueTest exte
92       * Initializing from Collection of null elements throws NPE
93       */
94      public void testConstructor4() {
95 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
96          try {
97 <            Integer[] ints = new Integer[SIZE];
87 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
97 >            new ArrayBlockingQueue(SIZE, false, elements);
98              shouldThrow();
99          } catch (NullPointerException success) {}
100      }
# Line 93 | Line 103 | public class ArrayBlockingQueueTest exte
103       * Initializing from Collection with some null elements throws NPE
104       */
105      public void testConstructor5() {
106 +        Integer[] ints = new Integer[SIZE];
107 +        for (int i = 0; i < SIZE-1; ++i)
108 +            ints[i] = i;
109 +        Collection<Integer> elements = Arrays.asList(ints);
110          try {
111 <            Integer[] ints = new Integer[SIZE];
98 <            for (int i = 0; i < SIZE-1; ++i)
99 <                ints[i] = new Integer(i);
100 <            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
111 >            new ArrayBlockingQueue(SIZE, false, Arrays.asList(ints));
112              shouldThrow();
113          } catch (NullPointerException success) {}
114      }
# Line 106 | Line 117 | public class ArrayBlockingQueueTest exte
117       * Initializing from too large collection throws IAE
118       */
119      public void testConstructor6() {
120 +        Integer[] ints = new Integer[SIZE];
121 +        for (int i = 0; i < SIZE; ++i)
122 +            ints[i] = i;
123 +        Collection<Integer> elements = Arrays.asList(ints);
124          try {
125 <            Integer[] ints = new Integer[SIZE];
111 <            for (int i = 0; i < SIZE; ++i)
112 <                ints[i] = new Integer(i);
113 <            ArrayBlockingQueue q = new ArrayBlockingQueue(1, false, Arrays.asList(ints));
125 >            new ArrayBlockingQueue(SIZE - 1, false, elements);
126              shouldThrow();
127          } catch (IllegalArgumentException success) {}
128      }
# Line 121 | Line 133 | public class ArrayBlockingQueueTest exte
133      public void testConstructor7() {
134          Integer[] ints = new Integer[SIZE];
135          for (int i = 0; i < SIZE; ++i)
136 <            ints[i] = new Integer(i);
137 <        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, Arrays.asList(ints));
136 >            ints[i] = i;
137 >        Collection<Integer> elements = Arrays.asList(ints);
138 >        ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE, true, elements);
139          for (int i = 0; i < SIZE; ++i)
140              assertEquals(ints[i], q.poll());
141      }
# Line 160 | Line 173 | public class ArrayBlockingQueueTest exte
173      }
174  
175      /**
163     * offer(null) throws NPE
164     */
165    public void testOfferNull() {
166        try {
167            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
168            q.offer(null);
169            shouldThrow();
170        } catch (NullPointerException success) {}
171    }
172
173    /**
174     * add(null) throws NPE
175     */
176    public void testAddNull() {
177        try {
178            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
179            q.add(null);
180            shouldThrow();
181        } catch (NullPointerException success) {}
182    }
183
184    /**
176       * Offer succeeds if not full; fails if full
177       */
178      public void testOffer() {
# Line 206 | Line 197 | public class ArrayBlockingQueueTest exte
197      }
198  
199      /**
209     * addAll(null) throws NPE
210     */
211    public void testAddAll1() {
212        try {
213            ArrayBlockingQueue q = new ArrayBlockingQueue(1);
214            q.addAll(null);
215            shouldThrow();
216        } catch (NullPointerException success) {}
217    }
218
219    /**
200       * addAll(this) throws IAE
201       */
202      public void testAddAllSelf() {
# Line 228 | Line 208 | public class ArrayBlockingQueueTest exte
208      }
209  
210      /**
231     * addAll of a collection with null elements throws NPE
232     */
233    public void testAddAll2() {
234        try {
235            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
236            Integer[] ints = new Integer[SIZE];
237            q.addAll(Arrays.asList(ints));
238            shouldThrow();
239        } catch (NullPointerException success) {}
240    }
241
242    /**
211       * addAll of a collection with any null elements throws NPE after
212       * possibly adding some elements
213       */
# Line 284 | Line 252 | public class ArrayBlockingQueueTest exte
252      }
253  
254      /**
287     * put(null) throws NPE
288     */
289    public void testPutNull() throws InterruptedException {
290        try {
291            ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
292            q.put(null);
293            shouldThrow();
294        } catch (NullPointerException success) {}
295    }
296
297    /**
255       * all elements successfully put are contained
256       */
257      public void testPut() throws InterruptedException {
# Line 367 | Line 324 | public class ArrayBlockingQueueTest exte
324              }});
325  
326          await(pleaseTake);
327 <        assertEquals(q.remainingCapacity(), 0);
327 >        assertEquals(0, q.remainingCapacity());
328          assertEquals(0, q.take());
329  
330          await(pleaseInterrupt);
331          assertThreadStaysAlive(t);
332          t.interrupt();
333          awaitTermination(t);
334 <        assertEquals(q.remainingCapacity(), 0);
334 >        assertEquals(0, q.remainingCapacity());
335      }
336  
337      /**
# Line 560 | Line 517 | public class ArrayBlockingQueueTest exte
517      }
518  
519      /**
563     * remove(x) removes x and returns true if present
564     */
565    public void testRemoveElement() {
566        ArrayBlockingQueue q = populatedQueue(SIZE);
567        for (int i = 1; i < SIZE; i+=2) {
568            assertTrue(q.remove(new Integer(i)));
569        }
570        for (int i = 0; i < SIZE; i+=2) {
571            assertTrue(q.remove(new Integer(i)));
572            assertFalse(q.remove(new Integer(i+1)));
573        }
574        assertTrue(q.isEmpty());
575    }
576
577    /**
520       * contains(x) reports true when elements added but not yet removed
521       */
522      public void testContains() {
# Line 651 | Line 593 | public class ArrayBlockingQueueTest exte
593          }
594      }
595  
596 <    /**
597 <     * toArray contains all elements in FIFO order
656 <     */
657 <    public void testToArray() {
658 <        ArrayBlockingQueue q = populatedQueue(SIZE);
596 >    void checkToArray(ArrayBlockingQueue q) {
597 >        int size = q.size();
598          Object[] o = q.toArray();
599 <        for (int i = 0; i < o.length; i++)
600 <            assertSame(o[i], q.poll());
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(a) contains all elements in FIFO order
609 >     * toArray() contains all elements in FIFO order
610       */
611 <    public void testToArray2() {
612 <        ArrayBlockingQueue<Integer> q = populatedQueue(SIZE);
613 <        Integer[] ints = new Integer[SIZE];
614 <        Integer[] array = q.toArray(ints);
615 <        assertSame(ints, array);
616 <        for (int i = 0; i < ints.length; i++)
617 <            assertSame(ints[i], q.poll());
611 >    public void testToArray() {
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(null) throws NullPointerException
663 >     * toArray(a) contains all elements in FIFO order
664       */
665 <    public void testToArray_NullArg() {
666 <        ArrayBlockingQueue q = populatedQueue(SIZE);
667 <        try {
668 <            q.toArray(null);
669 <            shouldThrow();
670 <        } catch (NullPointerException success) {}
665 >    public void testToArray2() {
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 824 | Line 821 | public class ArrayBlockingQueueTest exte
821       * A deserialized serialized queue has same elements in same order
822       */
823      public void testSerialization() throws Exception {
824 <        ArrayBlockingQueue q = populatedQueue(SIZE);
825 <
829 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
830 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
831 <        out.writeObject(q);
832 <        out.close();
833 <
834 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
835 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
836 <        ArrayBlockingQueue r = (ArrayBlockingQueue)in.readObject();
837 <        assertEquals(q.size(), r.size());
838 <        while (!q.isEmpty())
839 <            assertEquals(q.remove(), r.remove());
840 <    }
841 <
842 <    /**
843 <     * drainTo(null) throws NPE
844 <     */
845 <    public void testDrainToNull() {
846 <        ArrayBlockingQueue q = populatedQueue(SIZE);
847 <        try {
848 <            q.drainTo(null);
849 <            shouldThrow();
850 <        } catch (NullPointerException success) {}
851 <    }
824 >        Queue x = populatedQueue(SIZE);
825 >        Queue y = serialClone(x);
826  
827 <    /**
828 <     * drainTo(this) throws IAE
829 <     */
830 <    public void testDrainToSelf() {
831 <        ArrayBlockingQueue q = populatedQueue(SIZE);
832 <        try {
833 <            q.drainTo(q);
834 <            shouldThrow();
835 <        } catch (IllegalArgumentException success) {}
827 >        assertNotSame(x, y);
828 >        assertEquals(x.size(), y.size());
829 >        assertEquals(x.toString(), y.toString());
830 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
831 >        while (!x.isEmpty()) {
832 >            assertFalse(y.isEmpty());
833 >            assertEquals(x.remove(), y.remove());
834 >        }
835 >        assertTrue(y.isEmpty());
836      }
837  
838      /**
# Line 868 | Line 842 | public class ArrayBlockingQueueTest exte
842          ArrayBlockingQueue q = populatedQueue(SIZE);
843          ArrayList l = new ArrayList();
844          q.drainTo(l);
845 <        assertEquals(q.size(), 0);
846 <        assertEquals(l.size(), SIZE);
845 >        assertEquals(0, q.size());
846 >        assertEquals(SIZE, l.size());
847          for (int i = 0; i < SIZE; ++i)
848              assertEquals(l.get(i), new Integer(i));
849          q.add(zero);
# Line 879 | Line 853 | public class ArrayBlockingQueueTest exte
853          assertTrue(q.contains(one));
854          l.clear();
855          q.drainTo(l);
856 <        assertEquals(q.size(), 0);
857 <        assertEquals(l.size(), 2);
856 >        assertEquals(0, q.size());
857 >        assertEquals(2, l.size());
858          for (int i = 0; i < 2; ++i)
859              assertEquals(l.get(i), new Integer(i));
860      }
# Line 906 | Line 880 | public class ArrayBlockingQueueTest exte
880      }
881  
882      /**
909     * drainTo(null, n) throws NPE
910     */
911    public void testDrainToNullN() {
912        ArrayBlockingQueue q = populatedQueue(SIZE);
913        try {
914            q.drainTo(null, 0);
915            shouldThrow();
916        } catch (NullPointerException success) {}
917    }
918
919    /**
920     * drainTo(this, n) throws IAE
921     */
922    public void testDrainToSelfN() {
923        ArrayBlockingQueue q = populatedQueue(SIZE);
924        try {
925            q.drainTo(q, 0);
926            shouldThrow();
927        } catch (IllegalArgumentException success) {}
928    }
929
930    /**
883       * drainTo(c, n) empties first min(n, size) elements of queue into c
884       */
885      public void testDrainToN() {
# Line 938 | Line 890 | public class ArrayBlockingQueueTest exte
890              ArrayList l = new ArrayList();
891              q.drainTo(l, i);
892              int k = (i < SIZE) ? i : SIZE;
893 <            assertEquals(l.size(), k);
894 <            assertEquals(q.size(), SIZE-k);
893 >            assertEquals(k, l.size());
894 >            assertEquals(SIZE-k, q.size());
895              for (int j = 0; j < k; ++j)
896                  assertEquals(l.get(j), new Integer(j));
897 <            while (q.poll() != null) ;
897 >            do {} while (q.poll() != null);
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