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.17 by jsr166, Sat Nov 21 02:33:20 2009 UTC vs.
Revision 1.32 by jsr166, Tue Oct 19 00:43:49 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 58 | Line 70 | public class PriorityBlockingQueueTest e
70      }
71  
72      /**
73 <     * Constructor throws IAE if  capacity argument nonpositive
73 >     * Constructor throws IAE if capacity argument nonpositive
74       */
75      public void testConstructor2() {
76          try {
# 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 328 | Line 341 | public class PriorityBlockingQueueTest e
341       */
342      public void testTimedOffer() throws InterruptedException {
343          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
344 <        Thread t = new Thread(new Runnable() {
345 <                public void run() {
346 <                    try {
347 <                        q.put(new Integer(0));
348 <                        q.put(new Integer(0));
349 <                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
350 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
338 <                    } finally { }
339 <                }
340 <            });
344 >        Thread t = new Thread(new CheckedRunnable() {
345 >            public void realRun() {
346 >                q.put(new Integer(0));
347 >                q.put(new Integer(0));
348 >                assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
349 >                assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
350 >            }});
351  
352          t.start();
353          Thread.sleep(SMALL_DELAY_MS);
# Line 351 | 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 375 | 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 {
380                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 397 | 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 408 | 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 419 | 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 433 | 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 <                    threadAssertEquals(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);
453 <                    threadShouldThrow();
453 >                    shouldThrow();
454                  } catch (InterruptedException success) {}
455              }});
456  
# Line 448 | Line 461 | public class PriorityBlockingQueueTest e
461      }
462  
463      /**
451     *  timed poll before a delayed offer fails; after offer succeeds;
452     *  on interruption throws
453     */
454    public void testTimedPollWithOffer() throws InterruptedException {
455        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
456        Thread t = new Thread(new CheckedRunnable() {
457            public void realRun() throws InterruptedException {
458                threadAssertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
459                threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
460                try {
461                    q.poll(LONG_DELAY_MS, MILLISECONDS);
462                    threadShouldThrow();
463                } catch (InterruptedException success) {}
464            }});
465
466        t.start();
467        Thread.sleep(SMALL_DELAY_MS);
468        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
469        t.interrupt();
470        t.join();
471    }
472
473
474    /**
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 491 | 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 506 | 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 606 | 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 632 | Line 621 | public class PriorityBlockingQueueTest e
621       * toArray(null) throws NPE
622       */
623      public void testToArray_BadArg() {
624 +        PriorityBlockingQueue q = populatedQueue(SIZE);
625          try {
636            PriorityBlockingQueue q = populatedQueue(SIZE);
626              Object o[] = q.toArray(null);
627              shouldThrow();
628          } catch (NullPointerException success) {}
# Line 643 | 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);
648 <            Object o[] = q.toArray(new String[10] );
637 >            Object o[] = q.toArray(new String[10]);
638              shouldThrow();
639 <        } catch (ArrayStoreException  success) {}
639 >        } catch (ArrayStoreException success) {}
640      }
641  
642      /**
# Line 667 | 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 703 | 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 824 | 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);
# Line 833 | Line 822 | public class PriorityBlockingQueueTest e
822                  assertTrue(q.offer(new Integer(j)));
823              ArrayList l = new ArrayList();
824              q.drainTo(l, i);
825 <            int k = (i < SIZE)? i : SIZE;
825 >            int k = (i < SIZE) ? i : SIZE;
826              assertEquals(l.size(), k);
827              assertEquals(q.size(), SIZE-k);
828              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines