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.22 by jsr166, Sat Nov 21 22:00:46 2009 UTC vs.
Revision 1.31 by jsr166, Tue Oct 19 00:41:14 2010 UTC

# Line 13 | Line 13 | import static java.util.concurrent.TimeU
13   import java.io.*;
14  
15   public class PriorityBlockingQueueTest extends JSR166TestCase {
16 +
17 +    public static class Generic extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new PriorityBlockingQueue();
20 +        }
21 +    }
22 +
23 +    public static class InitialCapacity extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new PriorityBlockingQueue(20);
26 +        }
27 +    }
28 +
29      public static void main(String[] args) {
30 <        junit.textui.TestRunner.run (suite());
30 >        junit.textui.TestRunner.run(suite());
31      }
32 +
33      public static Test suite() {
34 <        return new TestSuite(PriorityBlockingQueueTest.class);
34 >        return newTestSuite(PriorityBlockingQueueTest.class,
35 >                            new Generic().testSuite(),
36 >                            new InitialCapacity().testSuite());
37      }
38  
39      private static final int NOCAP = Integer.MAX_VALUE;
# Line 25 | Line 41 | public class PriorityBlockingQueueTest e
41      /** Sample Comparator */
42      static class MyReverseComparator implements Comparator {
43          public int compare(Object x, Object y) {
44 <            int i = ((Integer)x).intValue();
29 <            int j = ((Integer)y).intValue();
30 <            if (i < j) return 1;
31 <            if (i > j) return -1;
32 <            return 0;
44 >            return ((Comparable)y).compareTo(x);
45          }
46      }
47  
# Line 249 | Line 261 | public class PriorityBlockingQueueTest e
261              shouldThrow();
262          } catch (NullPointerException success) {}
263      }
264 +
265      /**
266       * addAll of a collection with any null elements throws NPE after
267       * possibly adding some elements
# Line 348 | Line 361 | public class PriorityBlockingQueueTest e
361      public void testTake() throws InterruptedException {
362          PriorityBlockingQueue q = populatedQueue(SIZE);
363          for (int i = 0; i < SIZE; ++i) {
364 <            assertEquals(i, ((Integer)q.take()).intValue());
364 >            assertEquals(i, q.take());
365          }
366      }
367  
# Line 372 | Line 385 | public class PriorityBlockingQueueTest e
385       * Take removes existing elements until empty, then blocks interruptibly
386       */
387      public void testBlockingTake() throws InterruptedException {
388 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
388 >        final PriorityBlockingQueue q = populatedQueue(SIZE);
389 >        Thread t = new Thread(new CheckedRunnable() {
390              public void realRun() throws InterruptedException {
377                PriorityBlockingQueue q = populatedQueue(SIZE);
391                  for (int i = 0; i < SIZE; ++i) {
392 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
392 >                    assertEquals(i, q.take());
393                  }
394 <                q.take();
394 >                try {
395 >                    q.take();
396 >                    shouldThrow();
397 >                } catch (InterruptedException success) {}
398              }});
399  
400          t.start();
# Line 394 | Line 410 | public class PriorityBlockingQueueTest e
410      public void testPoll() {
411          PriorityBlockingQueue q = populatedQueue(SIZE);
412          for (int i = 0; i < SIZE; ++i) {
413 <            assertEquals(i, ((Integer)q.poll()).intValue());
413 >            assertEquals(i, q.poll());
414          }
415          assertNull(q.poll());
416      }
# Line 405 | Line 421 | public class PriorityBlockingQueueTest e
421      public void testTimedPoll0() throws InterruptedException {
422          PriorityBlockingQueue q = populatedQueue(SIZE);
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
424 >            assertEquals(i, q.poll(0, MILLISECONDS));
425          }
426          assertNull(q.poll(0, MILLISECONDS));
427      }
# Line 416 | Line 432 | public class PriorityBlockingQueueTest e
432      public void testTimedPoll() throws InterruptedException {
433          PriorityBlockingQueue q = populatedQueue(SIZE);
434          for (int i = 0; i < SIZE; ++i) {
435 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
435 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
436          }
437          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
438      }
# Line 430 | Line 446 | public class PriorityBlockingQueueTest e
446              public void realRun() throws InterruptedException {
447                  PriorityBlockingQueue q = populatedQueue(SIZE);
448                  for (int i = 0; i < SIZE; ++i) {
449 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
449 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450                  }
451                  try {
452                      q.poll(SMALL_DELAY_MS, MILLISECONDS);
# Line 445 | Line 461 | public class PriorityBlockingQueueTest e
461      }
462  
463      /**
448     *  timed poll before a delayed offer fails; after offer succeeds;
449     *  on interruption throws
450     */
451    public void testTimedPollWithOffer() throws InterruptedException {
452        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
453        Thread t = new Thread(new CheckedRunnable() {
454            public void realRun() throws InterruptedException {
455                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
456                assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
457                try {
458                    q.poll(LONG_DELAY_MS, MILLISECONDS);
459                    shouldThrow();
460                } catch (InterruptedException success) {}
461            }});
462
463        t.start();
464        Thread.sleep(SMALL_DELAY_MS);
465        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
466        t.interrupt();
467        t.join();
468    }
469
470
471    /**
464       * peek returns next element, or null if empty
465       */
466      public void testPeek() {
467          PriorityBlockingQueue q = populatedQueue(SIZE);
468          for (int i = 0; i < SIZE; ++i) {
469 <            assertEquals(i, ((Integer)q.peek()).intValue());
470 <            q.poll();
469 >            assertEquals(i, q.peek());
470 >            assertEquals(i, q.poll());
471              assertTrue(q.peek() == null ||
472 <                       i != ((Integer)q.peek()).intValue());
472 >                       !q.peek().equals(i));
473          }
474          assertNull(q.peek());
475      }
# Line 488 | Line 480 | public class PriorityBlockingQueueTest e
480      public void testElement() {
481          PriorityBlockingQueue q = populatedQueue(SIZE);
482          for (int i = 0; i < SIZE; ++i) {
483 <            assertEquals(i, ((Integer)q.element()).intValue());
484 <            q.poll();
483 >            assertEquals(i, q.element());
484 >            assertEquals(i, q.poll());
485          }
486          try {
487              q.element();
# Line 503 | Line 495 | public class PriorityBlockingQueueTest e
495      public void testRemove() {
496          PriorityBlockingQueue q = populatedQueue(SIZE);
497          for (int i = 0; i < SIZE; ++i) {
498 <            assertEquals(i, ((Integer)q.remove()).intValue());
498 >            assertEquals(i, q.remove());
499          }
500          try {
501              q.remove();
# Line 603 | Line 595 | public class PriorityBlockingQueueTest e
595      }
596  
597      /**
598 <     *  toArray contains all elements
598 >     * toArray contains all elements
599       */
600      public void testToArray() throws InterruptedException {
601          PriorityBlockingQueue q = populatedQueue(SIZE);
# Line 629 | Line 621 | public class PriorityBlockingQueueTest e
621       * toArray(null) throws NPE
622       */
623      public void testToArray_BadArg() {
624 +        PriorityBlockingQueue q = populatedQueue(SIZE);
625          try {
633            PriorityBlockingQueue q = populatedQueue(SIZE);
626              Object o[] = q.toArray(null);
627              shouldThrow();
628          } catch (NullPointerException success) {}
# Line 640 | Line 632 | public class PriorityBlockingQueueTest e
632       * toArray with incompatible array type throws CCE
633       */
634      public void testToArray1_BadArg() {
635 +        PriorityBlockingQueue q = populatedQueue(SIZE);
636          try {
637 <            PriorityBlockingQueue q = populatedQueue(SIZE);
645 <            Object o[] = q.toArray(new String[10] );
637 >            Object o[] = q.toArray(new String[10]);
638              shouldThrow();
639          } catch (ArrayStoreException success) {}
640      }
# Line 664 | Line 656 | public class PriorityBlockingQueueTest e
656      /**
657       * iterator.remove removes current element
658       */
659 <    public void testIteratorRemove () {
659 >    public void testIteratorRemove() {
660          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
661          q.add(new Integer(2));
662          q.add(new Integer(1));
# Line 700 | Line 692 | public class PriorityBlockingQueueTest e
692          ExecutorService executor = Executors.newFixedThreadPool(2);
693          executor.execute(new CheckedRunnable() {
694              public void realRun() throws InterruptedException {
695 <                threadAssertNull(q.poll());
696 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
697 <                threadAssertTrue(q.isEmpty());
695 >                assertNull(q.poll());
696 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
697 >                assertTrue(q.isEmpty());
698              }});
699  
700          executor.execute(new CheckedRunnable() {
701              public void realRun() throws InterruptedException {
702                  Thread.sleep(SMALL_DELAY_MS);
703 <                q.put(new Integer(1));
703 >                q.put(one);
704              }});
705  
706          joinPool(executor);
# Line 821 | Line 813 | public class PriorityBlockingQueueTest e
813      }
814  
815      /**
816 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
816 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
817       */
818      public void testDrainToN() {
819          PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines