ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/PriorityBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/PriorityBlockingQueueTest.java (file contents):
Revision 1.44 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.53 by jsr166, Sun Nov 23 22:27:06 2014 UTC

# Line 7 | Line 7
7   */
8  
9   import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
10 > import java.util.Arrays;
11 > import java.util.ArrayList;
12 > import java.util.Collection;
13 > import java.util.Comparator;
14 > import java.util.Iterator;
15 > import java.util.NoSuchElementException;
16 > import java.util.Queue;
17 > import java.util.concurrent.PriorityBlockingQueue;
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   import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 import java.io.*;
23  
24   public class PriorityBlockingQueueTest extends JSR166TestCase {
25  
# Line 22 | Line 31 | public class PriorityBlockingQueueTest e
31  
32      public static class InitialCapacity extends BlockingQueueTest {
33          protected BlockingQueue emptyCollection() {
34 <            return new PriorityBlockingQueue(20);
34 >            return new PriorityBlockingQueue(SIZE);
35          }
36      }
37  
# Line 46 | Line 55 | public class PriorityBlockingQueueTest e
55      }
56  
57      /**
58 <     * Create a queue of given size containing consecutive
58 >     * Returns a new queue of given size containing consecutive
59       * Integers 0 ... n.
60       */
61      private PriorityBlockingQueue<Integer> populatedQueue(int n) {
# Line 75 | Line 84 | public class PriorityBlockingQueueTest e
84       */
85      public void testConstructor2() {
86          try {
87 <            PriorityBlockingQueue q = new PriorityBlockingQueue(0);
87 >            new PriorityBlockingQueue(0);
88              shouldThrow();
89          } catch (IllegalArgumentException success) {}
90      }
# Line 85 | Line 94 | public class PriorityBlockingQueueTest e
94       */
95      public void testConstructor3() {
96          try {
97 <            PriorityBlockingQueue q = new PriorityBlockingQueue(null);
97 >            new PriorityBlockingQueue(null);
98              shouldThrow();
99          } catch (NullPointerException success) {}
100      }
# Line 94 | Line 103 | public class PriorityBlockingQueueTest e
103       * Initializing from Collection of null elements throws NPE
104       */
105      public void testConstructor4() {
106 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
107          try {
108 <            Integer[] ints = new Integer[SIZE];
99 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
108 >            new PriorityBlockingQueue(elements);
109              shouldThrow();
110          } catch (NullPointerException success) {}
111      }
# Line 105 | Line 114 | public class PriorityBlockingQueueTest e
114       * Initializing from Collection with some null elements throws NPE
115       */
116      public void testConstructor5() {
117 +        Integer[] ints = new Integer[SIZE];
118 +        for (int i = 0; i < SIZE-1; ++i)
119 +            ints[i] = i;
120 +        Collection<Integer> elements = Arrays.asList(ints);
121          try {
122 <            Integer[] ints = new Integer[SIZE];
110 <            for (int i = 0; i < SIZE-1; ++i)
111 <                ints[i] = new Integer(i);
112 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
122 >            new PriorityBlockingQueue(elements);
123              shouldThrow();
124          } catch (NullPointerException success) {}
125      }
# Line 120 | Line 130 | public class PriorityBlockingQueueTest e
130      public void testConstructor6() {
131          Integer[] ints = new Integer[SIZE];
132          for (int i = 0; i < SIZE; ++i)
133 <            ints[i] = new Integer(i);
133 >            ints[i] = i;
134          PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
135          for (int i = 0; i < SIZE; ++i)
136              assertEquals(ints[i], q.poll());
# Line 175 | Line 185 | public class PriorityBlockingQueueTest e
185      }
186  
187      /**
178     * offer(null) throws NPE
179     */
180    public void testOfferNull() {
181        try {
182            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
183            q.offer(null);
184            shouldThrow();
185        } catch (NullPointerException success) {}
186    }
187
188    /**
189     * add(null) throws NPE
190     */
191    public void testAddNull() {
192        try {
193            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
194            q.add(null);
195            shouldThrow();
196        } catch (NullPointerException success) {}
197    }
198
199    /**
188       * Offer of comparable element succeeds
189       */
190      public void testOffer() {
# Line 230 | Line 218 | public class PriorityBlockingQueueTest e
218      }
219  
220      /**
233     * addAll(null) throws NPE
234     */
235    public void testAddAll1() {
236        try {
237            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
238            q.addAll(null);
239            shouldThrow();
240        } catch (NullPointerException success) {}
241    }
242
243    /**
221       * addAll(this) throws IAE
222       */
223      public void testAddAllSelf() {
# Line 252 | Line 229 | public class PriorityBlockingQueueTest e
229      }
230  
231      /**
255     * addAll of a collection with null elements throws NPE
256     */
257    public void testAddAll2() {
258        try {
259            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
260            Integer[] ints = new Integer[SIZE];
261            q.addAll(Arrays.asList(ints));
262            shouldThrow();
263        } catch (NullPointerException success) {}
264    }
265
266    /**
232       * addAll of a collection with any null elements throws NPE after
233       * possibly adding some elements
234       */
# Line 294 | Line 259 | public class PriorityBlockingQueueTest e
259      }
260  
261      /**
297     * put(null) throws NPE
298     */
299    public void testPutNull() {
300        try {
301            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
302            q.put(null);
303            shouldThrow();
304        } catch (NullPointerException success) {}
305    }
306
307    /**
262       * all elements successfully put are contained
263       */
264      public void testPut() {
# Line 505 | Line 459 | public class PriorityBlockingQueueTest e
459      }
460  
461      /**
508     * remove(x) removes x and returns true if present
509     */
510    public void testRemoveElement() {
511        PriorityBlockingQueue q = populatedQueue(SIZE);
512        for (int i = 1; i < SIZE; i+=2) {
513            assertTrue(q.contains(i));
514            assertTrue(q.remove(i));
515            assertFalse(q.contains(i));
516            assertTrue(q.contains(i-1));
517        }
518        for (int i = 0; i < SIZE; i+=2) {
519            assertTrue(q.contains(i));
520            assertTrue(q.remove(i));
521            assertFalse(q.contains(i));
522            assertFalse(q.remove(i+1));
523            assertFalse(q.contains(i+1));
524        }
525        assertTrue(q.isEmpty());
526    }
527
528    /**
462       * contains(x) reports true when elements added but not yet removed
463       */
464      public void testContains() {
# Line 626 | Line 559 | public class PriorityBlockingQueueTest e
559      }
560  
561      /**
629     * toArray(null) throws NullPointerException
630     */
631    public void testToArray_NullArg() {
632        PriorityBlockingQueue q = populatedQueue(SIZE);
633        try {
634            q.toArray(null);
635            shouldThrow();
636        } catch (NullPointerException success) {}
637    }
638
639    /**
562       * toArray(incompatible array type) throws ArrayStoreException
563       */
564      public void testToArray1_BadArg() {
# Line 719 | Line 641 | public class PriorityBlockingQueueTest e
641       * A deserialized serialized queue has same elements
642       */
643      public void testSerialization() throws Exception {
644 <        PriorityBlockingQueue q = populatedQueue(SIZE);
645 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
724 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
725 <        out.writeObject(q);
726 <        out.close();
727 <
728 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
729 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
730 <        PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
731 <        assertEquals(q.size(), r.size());
732 <        while (!q.isEmpty())
733 <            assertEquals(q.remove(), r.remove());
734 <    }
644 >        Queue x = populatedQueue(SIZE);
645 >        Queue y = serialClone(x);
646  
647 <    /**
648 <     * drainTo(null) throws NPE
649 <     */
650 <    public void testDrainToNull() {
651 <        PriorityBlockingQueue q = populatedQueue(SIZE);
652 <        try {
653 <            q.drainTo(null);
743 <            shouldThrow();
744 <        } catch (NullPointerException success) {}
745 <    }
746 <
747 <    /**
748 <     * drainTo(this) throws IAE
749 <     */
750 <    public void testDrainToSelf() {
751 <        PriorityBlockingQueue q = populatedQueue(SIZE);
752 <        try {
753 <            q.drainTo(q);
754 <            shouldThrow();
755 <        } catch (IllegalArgumentException success) {}
647 >        assertNotSame(x, y);
648 >        assertEquals(x.size(), y.size());
649 >        while (!x.isEmpty()) {
650 >            assertFalse(y.isEmpty());
651 >            assertEquals(x.remove(), y.remove());
652 >        }
653 >        assertTrue(y.isEmpty());
654      }
655  
656      /**
# Line 762 | Line 660 | public class PriorityBlockingQueueTest e
660          PriorityBlockingQueue q = populatedQueue(SIZE);
661          ArrayList l = new ArrayList();
662          q.drainTo(l);
663 <        assertEquals(q.size(), 0);
664 <        assertEquals(l.size(), SIZE);
663 >        assertEquals(0, q.size());
664 >        assertEquals(SIZE, l.size());
665          for (int i = 0; i < SIZE; ++i)
666              assertEquals(l.get(i), new Integer(i));
667          q.add(zero);
# Line 773 | Line 671 | public class PriorityBlockingQueueTest e
671          assertTrue(q.contains(one));
672          l.clear();
673          q.drainTo(l);
674 <        assertEquals(q.size(), 0);
675 <        assertEquals(l.size(), 2);
674 >        assertEquals(0, q.size());
675 >        assertEquals(2, l.size());
676          for (int i = 0; i < 2; ++i)
677              assertEquals(l.get(i), new Integer(i));
678      }
# Line 800 | Line 698 | public class PriorityBlockingQueueTest e
698      }
699  
700      /**
803     * drainTo(null, n) throws NPE
804     */
805    public void testDrainToNullN() {
806        PriorityBlockingQueue q = populatedQueue(SIZE);
807        try {
808            q.drainTo(null, 0);
809            shouldThrow();
810        } catch (NullPointerException success) {}
811    }
812
813    /**
814     * drainTo(this, n) throws IAE
815     */
816    public void testDrainToSelfN() {
817        PriorityBlockingQueue q = populatedQueue(SIZE);
818        try {
819            q.drainTo(q, 0);
820            shouldThrow();
821        } catch (IllegalArgumentException success) {}
822    }
823
824    /**
701       * drainTo(c, n) empties first min(n, size) elements of queue into c
702       */
703      public void testDrainToN() {
# Line 832 | Line 708 | public class PriorityBlockingQueueTest e
708              ArrayList l = new ArrayList();
709              q.drainTo(l, i);
710              int k = (i < SIZE) ? i : SIZE;
711 <            assertEquals(l.size(), k);
712 <            assertEquals(q.size(), SIZE-k);
711 >            assertEquals(k, l.size());
712 >            assertEquals(SIZE-k, q.size());
713              for (int j = 0; j < k; ++j)
714                  assertEquals(l.get(j), new Integer(j));
715              while (q.poll() != null) ;
716          }
717      }
718  
719 +    /**
720 +     * remove(null), contains(null) always return false
721 +     */
722 +    public void testNeverContainsNull() {
723 +        Collection<?>[] qs = {
724 +            new PriorityBlockingQueue<Object>(),
725 +            populatedQueue(2),
726 +        };
727 +
728 +        for (Collection<?> q : qs) {
729 +            assertFalse(q.contains(null));
730 +            assertFalse(q.remove(null));
731 +        }
732 +    }
733 +
734   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines