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.36 by jsr166, Wed Nov 3 16:46:34 2010 UTC vs.
Revision 1.59 by jsr166, Sat Feb 28 19:59:23 2015 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
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.Comparator;
15 > import java.util.Iterator;
16 > import java.util.NoSuchElementException;
17 > import java.util.Queue;
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 java.util.concurrent.PriorityBlockingQueue;
23 >
24 > import junit.framework.Test;
25  
26   public class PriorityBlockingQueueTest extends JSR166TestCase {
27  
# Line 22 | Line 33 | public class PriorityBlockingQueueTest e
33  
34      public static class InitialCapacity extends BlockingQueueTest {
35          protected BlockingQueue emptyCollection() {
36 <            return new PriorityBlockingQueue(20);
36 >            return new PriorityBlockingQueue(SIZE);
37          }
38      }
39  
# Line 36 | Line 47 | public class PriorityBlockingQueueTest e
47                              new InitialCapacity().testSuite());
48      }
49  
39    private static final int NOCAP = Integer.MAX_VALUE;
40
50      /** Sample Comparator */
51      static class MyReverseComparator implements Comparator {
52          public int compare(Object x, Object y) {
# 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 populatedQueue(int n) {
62 <        PriorityBlockingQueue q = new PriorityBlockingQueue(n);
61 >    private PriorityBlockingQueue<Integer> populatedQueue(int n) {
62 >        PriorityBlockingQueue<Integer> q =
63 >            new PriorityBlockingQueue<Integer>(n);
64          assertTrue(q.isEmpty());
65 <        for (int i = n-1; i >= 0; i-=2)
65 >        for (int i = n-1; i >= 0; i -= 2)
66              assertTrue(q.offer(new Integer(i)));
67 <        for (int i = (n & 1); i < n; i+=2)
67 >        for (int i = (n & 1); i < n; i += 2)
68              assertTrue(q.offer(new Integer(i)));
69          assertFalse(q.isEmpty());
70 <        assertEquals(NOCAP, q.remainingCapacity());
70 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
71          assertEquals(n, q.size());
72          return q;
73      }
# Line 66 | Line 76 | public class PriorityBlockingQueueTest e
76       * A new queue has unbounded capacity
77       */
78      public void testConstructor1() {
79 <        assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity());
79 >        assertEquals(Integer.MAX_VALUE,
80 >                     new PriorityBlockingQueue(SIZE).remainingCapacity());
81      }
82  
83      /**
# Line 74 | Line 85 | public class PriorityBlockingQueueTest e
85       */
86      public void testConstructor2() {
87          try {
88 <            PriorityBlockingQueue q = new PriorityBlockingQueue(0);
88 >            new PriorityBlockingQueue(0);
89              shouldThrow();
90          } catch (IllegalArgumentException success) {}
91      }
# Line 84 | Line 95 | public class PriorityBlockingQueueTest e
95       */
96      public void testConstructor3() {
97          try {
98 <            PriorityBlockingQueue q = new PriorityBlockingQueue(null);
98 >            new PriorityBlockingQueue(null);
99              shouldThrow();
100          } catch (NullPointerException success) {}
101      }
# Line 93 | Line 104 | public class PriorityBlockingQueueTest e
104       * Initializing from Collection of null elements throws NPE
105       */
106      public void testConstructor4() {
107 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
108          try {
109 <            Integer[] ints = new Integer[SIZE];
98 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
109 >            new PriorityBlockingQueue(elements);
110              shouldThrow();
111          } catch (NullPointerException success) {}
112      }
# Line 104 | Line 115 | public class PriorityBlockingQueueTest e
115       * Initializing from Collection with some null elements throws NPE
116       */
117      public void testConstructor5() {
118 +        Integer[] ints = new Integer[SIZE];
119 +        for (int i = 0; i < SIZE-1; ++i)
120 +            ints[i] = i;
121 +        Collection<Integer> elements = Arrays.asList(ints);
122          try {
123 <            Integer[] ints = new Integer[SIZE];
109 <            for (int i = 0; i < SIZE-1; ++i)
110 <                ints[i] = new Integer(i);
111 <            PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
123 >            new PriorityBlockingQueue(elements);
124              shouldThrow();
125          } catch (NullPointerException success) {}
126      }
# Line 119 | Line 131 | public class PriorityBlockingQueueTest e
131      public void testConstructor6() {
132          Integer[] ints = new Integer[SIZE];
133          for (int i = 0; i < SIZE; ++i)
134 <            ints[i] = new Integer(i);
134 >            ints[i] = i;
135          PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints));
136          for (int i = 0; i < SIZE; ++i)
137              assertEquals(ints[i], q.poll());
# Line 146 | Line 158 | public class PriorityBlockingQueueTest e
158      public void testEmpty() {
159          PriorityBlockingQueue q = new PriorityBlockingQueue(2);
160          assertTrue(q.isEmpty());
161 <        assertEquals(NOCAP, q.remainingCapacity());
161 >        assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
162          q.add(one);
163          assertFalse(q.isEmpty());
164          q.add(two);
# Line 156 | Line 168 | public class PriorityBlockingQueueTest e
168      }
169  
170      /**
171 <     * remainingCapacity does not change when elements added or removed,
160 <     * but size does
171 >     * remainingCapacity() always returns Integer.MAX_VALUE
172       */
173      public void testRemainingCapacity() {
174 <        PriorityBlockingQueue q = populatedQueue(SIZE);
174 >        BlockingQueue q = populatedQueue(SIZE);
175          for (int i = 0; i < SIZE; ++i) {
176 <            assertEquals(NOCAP, q.remainingCapacity());
177 <            assertEquals(SIZE-i, q.size());
178 <            q.remove();
176 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
177 >            assertEquals(SIZE - i, q.size());
178 >            assertEquals(i, q.remove());
179          }
180          for (int i = 0; i < SIZE; ++i) {
181 <            assertEquals(NOCAP, q.remainingCapacity());
181 >            assertEquals(Integer.MAX_VALUE, q.remainingCapacity());
182              assertEquals(i, q.size());
183 <            q.add(new Integer(i));
183 >            assertTrue(q.add(i));
184          }
185      }
186  
187      /**
177     * offer(null) throws NPE
178     */
179    public void testOfferNull() {
180        try {
181            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
182            q.offer(null);
183            shouldThrow();
184        } catch (NullPointerException success) {}
185    }
186
187    /**
188     * add(null) throws NPE
189     */
190    public void testAddNull() {
191        try {
192            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
193            q.add(null);
194            shouldThrow();
195        } catch (NullPointerException success) {}
196    }
197
198    /**
188       * Offer of comparable element succeeds
189       */
190      public void testOffer() {
# Line 229 | Line 218 | public class PriorityBlockingQueueTest e
218      }
219  
220      /**
232     * addAll(null) throws NPE
233     */
234    public void testAddAll1() {
235        try {
236            PriorityBlockingQueue q = new PriorityBlockingQueue(1);
237            q.addAll(null);
238            shouldThrow();
239        } catch (NullPointerException success) {}
240    }
241
242    /**
221       * addAll(this) throws IAE
222       */
223      public void testAddAllSelf() {
# Line 251 | Line 229 | public class PriorityBlockingQueueTest e
229      }
230  
231      /**
254     * addAll of a collection with null elements throws NPE
255     */
256    public void testAddAll2() {
257        try {
258            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
259            Integer[] ints = new Integer[SIZE];
260            q.addAll(Arrays.asList(ints));
261            shouldThrow();
262        } catch (NullPointerException success) {}
263    }
264
265    /**
232       * addAll of a collection with any null elements throws NPE after
233       * possibly adding some elements
234       */
# Line 293 | Line 259 | public class PriorityBlockingQueueTest e
259      }
260  
261      /**
296     * put(null) throws NPE
297     */
298     public void testPutNull() {
299        try {
300            PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
301            q.put(null);
302            shouldThrow();
303        } catch (NullPointerException success) {}
304     }
305
306    /**
262       * all elements successfully put are contained
263       */
264 <     public void testPut() {
265 <         PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
266 <         for (int i = 0; i < SIZE; ++i) {
267 <             Integer I = new Integer(i);
268 <             q.put(I);
269 <             assertTrue(q.contains(I));
270 <         }
271 <         assertEquals(SIZE, q.size());
264 >    public void testPut() {
265 >        PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
266 >        for (int i = 0; i < SIZE; ++i) {
267 >            Integer x = new Integer(i);
268 >            q.put(x);
269 >            assertTrue(q.contains(x));
270 >        }
271 >        assertEquals(SIZE, q.size());
272      }
273  
274      /**
# Line 322 | Line 277 | public class PriorityBlockingQueueTest e
277      public void testPutWithTake() throws InterruptedException {
278          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
279          final int size = 4;
280 <        Thread t = new Thread(new CheckedRunnable() {
280 >        Thread t = newStartedThread(new CheckedRunnable() {
281              public void realRun() {
282                  for (int i = 0; i < size; i++)
283                      q.put(new Integer(0));
284              }});
285  
286 <        t.start();
287 <        Thread.sleep(SHORT_DELAY_MS);
333 <        assertEquals(q.size(), size);
286 >        awaitTermination(t);
287 >        assertEquals(size, q.size());
288          q.take();
335        t.interrupt();
336        t.join();
289      }
290  
291      /**
# Line 341 | Line 293 | public class PriorityBlockingQueueTest e
293       */
294      public void testTimedOffer() throws InterruptedException {
295          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
296 <        Thread t = new Thread(new CheckedRunnable() {
296 >        Thread t = newStartedThread(new CheckedRunnable() {
297              public void realRun() {
298                  q.put(new Integer(0));
299                  q.put(new Integer(0));
# Line 349 | Line 301 | public class PriorityBlockingQueueTest e
301                  assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
302              }});
303  
304 <        t.start();
353 <        Thread.sleep(SMALL_DELAY_MS);
354 <        t.interrupt();
355 <        t.join();
304 >        awaitTermination(t);
305      }
306  
307      /**
# Line 370 | Line 319 | public class PriorityBlockingQueueTest e
319       */
320      public void testBlockingTake() throws InterruptedException {
321          final PriorityBlockingQueue q = populatedQueue(SIZE);
322 <        Thread t = new Thread(new CheckedRunnable() {
322 >        final CountDownLatch pleaseInterrupt = new CountDownLatch(1);
323 >        Thread t = newStartedThread(new CheckedRunnable() {
324              public void realRun() throws InterruptedException {
325                  for (int i = 0; i < SIZE; ++i) {
326                      assertEquals(i, q.take());
327                  }
328 +
329 +                Thread.currentThread().interrupt();
330                  try {
331                      q.take();
332                      shouldThrow();
333                  } catch (InterruptedException success) {}
334 +                assertFalse(Thread.interrupted());
335 +
336 +                pleaseInterrupt.countDown();
337 +                try {
338 +                    q.take();
339 +                    shouldThrow();
340 +                } catch (InterruptedException success) {}
341 +                assertFalse(Thread.interrupted());
342              }});
343  
344 <        t.start();
345 <        Thread.sleep(SHORT_DELAY_MS);
344 >        await(pleaseInterrupt);
345 >        assertThreadStaysAlive(t);
346          t.interrupt();
347 <        t.join();
347 >        awaitTermination(t);
348      }
349  
390
350      /**
351       * poll succeeds unless empty
352       */
# Line 414 | Line 373 | public class PriorityBlockingQueueTest e
373       * timed poll with nonzero timeout succeeds when non-empty, else times out
374       */
375      public void testTimedPoll() throws InterruptedException {
376 <        PriorityBlockingQueue q = populatedQueue(SIZE);
376 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
377          for (int i = 0; i < SIZE; ++i) {
378 <            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
379 <        }
380 <        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
378 >            long startTime = System.nanoTime();
379 >            assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
380 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
381 >        }
382 >        long startTime = System.nanoTime();
383 >        assertNull(q.poll(timeoutMillis(), MILLISECONDS));
384 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis());
385 >        checkEmpty(q);
386      }
387  
388      /**
# Line 426 | Line 390 | public class PriorityBlockingQueueTest e
390       * returning timeout status
391       */
392      public void testInterruptedTimedPoll() throws InterruptedException {
393 <        Thread t = new Thread(new CheckedRunnable() {
393 >        final BlockingQueue<Integer> q = populatedQueue(SIZE);
394 >        final CountDownLatch aboutToWait = new CountDownLatch(1);
395 >        Thread t = newStartedThread(new CheckedRunnable() {
396              public void realRun() throws InterruptedException {
431                PriorityBlockingQueue q = populatedQueue(SIZE);
397                  for (int i = 0; i < SIZE; ++i) {
398 <                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
398 >                    long t0 = System.nanoTime();
399 >                    assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS));
400 >                    assertTrue(millisElapsedSince(t0) < SMALL_DELAY_MS);
401                  }
402 +                long t0 = System.nanoTime();
403 +                aboutToWait.countDown();
404                  try {
405 <                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
405 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
406                      shouldThrow();
407 <                } catch (InterruptedException success) {}
407 >                } catch (InterruptedException success) {
408 >                    assertTrue(millisElapsedSince(t0) < MEDIUM_DELAY_MS);
409 >                }
410              }});
411  
412 <        t.start();
413 <        Thread.sleep(SHORT_DELAY_MS);
412 >        aboutToWait.await();
413 >        waitForThreadToEnterWaitState(t, SMALL_DELAY_MS);
414          t.interrupt();
415 <        t.join();
415 >        awaitTermination(t, MEDIUM_DELAY_MS);
416      }
417  
418      /**
# Line 488 | Line 459 | public class PriorityBlockingQueueTest e
459      }
460  
461      /**
491     * remove(x) removes x and returns true if present
492     */
493    public void testRemoveElement() {
494        PriorityBlockingQueue q = populatedQueue(SIZE);
495        for (int i = 1; i < SIZE; i+=2) {
496            assertTrue(q.remove(new Integer(i)));
497        }
498        for (int i = 0; i < SIZE; i+=2) {
499            assertTrue(q.remove(new Integer(i)));
500            assertFalse(q.remove(new Integer(i+1)));
501        }
502        assertTrue(q.isEmpty());
503    }
504
505    /**
462       * contains(x) reports true when elements added but not yet removed
463       */
464      public void testContains() {
# Line 572 | Line 528 | public class PriorityBlockingQueueTest e
528              assertTrue(q.removeAll(p));
529              assertEquals(SIZE-i, q.size());
530              for (int j = 0; j < i; ++j) {
531 <                Integer I = (Integer)(p.remove());
532 <                assertFalse(q.contains(I));
531 >                Integer x = (Integer)(p.remove());
532 >                assertFalse(q.contains(x));
533              }
534          }
535      }
# Line 586 | Line 542 | public class PriorityBlockingQueueTest e
542          Object[] o = q.toArray();
543          Arrays.sort(o);
544          for (int i = 0; i < o.length; i++)
545 <            assertEquals(o[i], q.take());
545 >            assertSame(o[i], q.take());
546      }
547  
548      /**
549       * toArray(a) contains all elements
550       */
551      public void testToArray2() throws InterruptedException {
552 <        PriorityBlockingQueue q = populatedQueue(SIZE);
552 >        PriorityBlockingQueue<Integer> q = populatedQueue(SIZE);
553          Integer[] ints = new Integer[SIZE];
554 <        ints = (Integer[])q.toArray(ints);
554 >        Integer[] array = q.toArray(ints);
555 >        assertSame(ints, array);
556          Arrays.sort(ints);
557          for (int i = 0; i < ints.length; i++)
558 <            assertEquals(ints[i], q.take());
602 <    }
603 <
604 <    /**
605 <     * toArray(null) throws NullPointerException
606 <     */
607 <    public void testToArray_NullArg() {
608 <        PriorityBlockingQueue q = populatedQueue(SIZE);
609 <        try {
610 <            q.toArray(null);
611 <            shouldThrow();
612 <        } catch (NullPointerException success) {}
558 >            assertSame(ints[i], q.take());
559      }
560  
561      /**
# Line 628 | Line 574 | public class PriorityBlockingQueueTest e
574       */
575      public void testIterator() {
576          PriorityBlockingQueue q = populatedQueue(SIZE);
631        int i = 0;
577          Iterator it = q.iterator();
578 <        while (it.hasNext()) {
578 >        int i;
579 >        for (i = 0; it.hasNext(); i++)
580              assertTrue(q.contains(it.next()));
635            ++i;
636        }
581          assertEquals(i, SIZE);
582 +        assertIteratorExhausted(it);
583 +    }
584 +
585 +    /**
586 +     * iterator of empty collection has no elements
587 +     */
588 +    public void testEmptyIterator() {
589 +        assertIteratorExhausted(new PriorityBlockingQueue().iterator());
590      }
591  
592      /**
# Line 656 | Line 608 | public class PriorityBlockingQueueTest e
608          assertFalse(it.hasNext());
609      }
610  
659
611      /**
612       * toString contains toStrings of elements
613       */
# Line 664 | Line 615 | public class PriorityBlockingQueueTest e
615          PriorityBlockingQueue q = populatedQueue(SIZE);
616          String s = q.toString();
617          for (int i = 0; i < SIZE; ++i) {
618 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
618 >            assertTrue(s.contains(String.valueOf(i)));
619          }
620      }
621  
622      /**
623 <     * offer transfers elements across Executor tasks
623 >     * timed poll transfers elements across Executor tasks
624       */
625      public void testPollInExecutor() {
626          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
627 +        final CheckedBarrier threadsStarted = new CheckedBarrier(2);
628          ExecutorService executor = Executors.newFixedThreadPool(2);
629          executor.execute(new CheckedRunnable() {
630              public void realRun() throws InterruptedException {
631                  assertNull(q.poll());
632 <                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
633 <                assertTrue(q.isEmpty());
632 >                threadsStarted.await();
633 >                assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS));
634 >                checkEmpty(q);
635              }});
636  
637          executor.execute(new CheckedRunnable() {
638              public void realRun() throws InterruptedException {
639 <                Thread.sleep(SMALL_DELAY_MS);
639 >                threadsStarted.await();
640                  q.put(one);
641              }});
642  
# Line 694 | Line 647 | public class PriorityBlockingQueueTest e
647       * A deserialized serialized queue has same elements
648       */
649      public void testSerialization() throws Exception {
650 <        PriorityBlockingQueue q = populatedQueue(SIZE);
651 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
699 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
700 <        out.writeObject(q);
701 <        out.close();
650 >        Queue x = populatedQueue(SIZE);
651 >        Queue y = serialClone(x);
652  
653 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
654 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
655 <        PriorityBlockingQueue r = (PriorityBlockingQueue)in.readObject();
656 <        assertEquals(q.size(), r.size());
657 <        while (!q.isEmpty())
658 <            assertEquals(q.remove(), r.remove());
659 <    }
710 <
711 <    /**
712 <     * drainTo(null) throws NPE
713 <     */
714 <    public void testDrainToNull() {
715 <        PriorityBlockingQueue q = populatedQueue(SIZE);
716 <        try {
717 <            q.drainTo(null);
718 <            shouldThrow();
719 <        } catch (NullPointerException success) {}
720 <    }
721 <
722 <    /**
723 <     * drainTo(this) throws IAE
724 <     */
725 <    public void testDrainToSelf() {
726 <        PriorityBlockingQueue q = populatedQueue(SIZE);
727 <        try {
728 <            q.drainTo(q);
729 <            shouldThrow();
730 <        } catch (IllegalArgumentException success) {}
653 >        assertNotSame(x, y);
654 >        assertEquals(x.size(), y.size());
655 >        while (!x.isEmpty()) {
656 >            assertFalse(y.isEmpty());
657 >            assertEquals(x.remove(), y.remove());
658 >        }
659 >        assertTrue(y.isEmpty());
660      }
661  
662      /**
# Line 737 | Line 666 | public class PriorityBlockingQueueTest e
666          PriorityBlockingQueue q = populatedQueue(SIZE);
667          ArrayList l = new ArrayList();
668          q.drainTo(l);
669 <        assertEquals(q.size(), 0);
670 <        assertEquals(l.size(), SIZE);
669 >        assertEquals(0, q.size());
670 >        assertEquals(SIZE, l.size());
671          for (int i = 0; i < SIZE; ++i)
672              assertEquals(l.get(i), new Integer(i));
673          q.add(zero);
# Line 748 | Line 677 | public class PriorityBlockingQueueTest e
677          assertTrue(q.contains(one));
678          l.clear();
679          q.drainTo(l);
680 <        assertEquals(q.size(), 0);
681 <        assertEquals(l.size(), 2);
680 >        assertEquals(0, q.size());
681 >        assertEquals(2, l.size());
682          for (int i = 0; i < 2; ++i)
683              assertEquals(l.get(i), new Integer(i));
684      }
# Line 775 | Line 704 | public class PriorityBlockingQueueTest e
704      }
705  
706      /**
778     * drainTo(null, n) throws NPE
779     */
780    public void testDrainToNullN() {
781        PriorityBlockingQueue q = populatedQueue(SIZE);
782        try {
783            q.drainTo(null, 0);
784            shouldThrow();
785        } catch (NullPointerException success) {}
786    }
787
788    /**
789     * drainTo(this, n) throws IAE
790     */
791    public void testDrainToSelfN() {
792        PriorityBlockingQueue q = populatedQueue(SIZE);
793        try {
794            q.drainTo(q, 0);
795            shouldThrow();
796        } catch (IllegalArgumentException success) {}
797    }
798
799    /**
707       * drainTo(c, n) empties first min(n, size) elements of queue into c
708       */
709      public void testDrainToN() {
# Line 807 | Line 714 | public class PriorityBlockingQueueTest e
714              ArrayList l = new ArrayList();
715              q.drainTo(l, i);
716              int k = (i < SIZE) ? i : SIZE;
717 <            assertEquals(l.size(), k);
718 <            assertEquals(q.size(), SIZE-k);
717 >            assertEquals(k, l.size());
718 >            assertEquals(SIZE-k, q.size());
719              for (int j = 0; j < k; ++j)
720                  assertEquals(l.get(j), new Integer(j));
721 <            while (q.poll() != null) ;
721 >            do {} while (q.poll() != null);
722 >        }
723 >    }
724 >
725 >    /**
726 >     * remove(null), contains(null) always return false
727 >     */
728 >    public void testNeverContainsNull() {
729 >        Collection<?>[] qs = {
730 >            new PriorityBlockingQueue<Object>(),
731 >            populatedQueue(2),
732 >        };
733 >
734 >        for (Collection<?> q : qs) {
735 >            assertFalse(q.contains(null));
736 >            assertFalse(q.remove(null));
737          }
738      }
739  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines