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.19 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.35 by jsr166, Wed Nov 3 07:54:52 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 332 | Line 345 | public class PriorityBlockingQueueTest e
345              public void realRun() {
346                  q.put(new Integer(0));
347                  q.put(new Integer(0));
348 <                threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
349 <                threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
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();
# 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  
368      /**
356     * take blocks interruptibly when empty
357     */
358    public void testTakeFromEmpty() throws InterruptedException {
359        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
360        Thread t = new Thread(new CheckedInterruptedRunnable() {
361            public void realRun() throws InterruptedException {
362                q.take();
363            }});
364
365        t.start();
366        Thread.sleep(SHORT_DELAY_MS);
367        t.interrupt();
368        t.join();
369    }
370
371    /**
369       * Take removes existing elements until empty, then blocks interruptibly
370       */
371      public void testBlockingTake() throws InterruptedException {
372 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
372 >        final PriorityBlockingQueue q = populatedQueue(SIZE);
373 >        Thread t = new Thread(new CheckedRunnable() {
374              public void realRun() throws InterruptedException {
377                PriorityBlockingQueue q = populatedQueue(SIZE);
375                  for (int i = 0; i < SIZE; ++i) {
376 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
376 >                    assertEquals(i, q.take());
377                  }
378 <                q.take();
378 >                try {
379 >                    q.take();
380 >                    shouldThrow();
381 >                } catch (InterruptedException success) {}
382              }});
383  
384          t.start();
# Line 394 | Line 394 | public class PriorityBlockingQueueTest e
394      public void testPoll() {
395          PriorityBlockingQueue q = populatedQueue(SIZE);
396          for (int i = 0; i < SIZE; ++i) {
397 <            assertEquals(i, ((Integer)q.poll()).intValue());
397 >            assertEquals(i, q.poll());
398          }
399          assertNull(q.poll());
400      }
401  
402      /**
403 <     * timed pool with zero timeout succeeds when non-empty, else times out
403 >     * timed poll with zero timeout succeeds when non-empty, else times out
404       */
405      public void testTimedPoll0() throws InterruptedException {
406          PriorityBlockingQueue q = populatedQueue(SIZE);
407          for (int i = 0; i < SIZE; ++i) {
408 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
408 >            assertEquals(i, q.poll(0, MILLISECONDS));
409          }
410          assertNull(q.poll(0, MILLISECONDS));
411      }
412  
413      /**
414 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
414 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
415       */
416      public void testTimedPoll() throws InterruptedException {
417          PriorityBlockingQueue q = populatedQueue(SIZE);
418          for (int i = 0; i < SIZE; ++i) {
419 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
419 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
420          }
421          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
422      }
# Line 430 | Line 430 | public class PriorityBlockingQueueTest e
430              public void realRun() throws InterruptedException {
431                  PriorityBlockingQueue q = populatedQueue(SIZE);
432                  for (int i = 0; i < SIZE; ++i) {
433 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
433 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
434                  }
435                  try {
436 <                    q.poll(LONG_DELAY_MS, MILLISECONDS);
436 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
437                      shouldThrow();
438                  } catch (InterruptedException success) {}
439              }});
# Line 445 | Line 445 | public class PriorityBlockingQueueTest e
445      }
446  
447      /**
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                assertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
457                try {
458                    q.poll(LONG_DELAY_MS, MILLISECONDS);
459                    threadShouldThrow();
460                } catch (InterruptedException success) {}
461            }});
462
463        t.start();
464        Thread.sleep(SMALL_DELAY_MS);
465        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
466        t.interrupt();
467        t.join();
468    }
469
470
471    /**
448       * peek returns next element, or null if empty
449       */
450      public void testPeek() {
451          PriorityBlockingQueue q = populatedQueue(SIZE);
452          for (int i = 0; i < SIZE; ++i) {
453 <            assertEquals(i, ((Integer)q.peek()).intValue());
454 <            q.poll();
453 >            assertEquals(i, q.peek());
454 >            assertEquals(i, q.poll());
455              assertTrue(q.peek() == null ||
456 <                       i != ((Integer)q.peek()).intValue());
456 >                       !q.peek().equals(i));
457          }
458          assertNull(q.peek());
459      }
# Line 488 | Line 464 | public class PriorityBlockingQueueTest e
464      public void testElement() {
465          PriorityBlockingQueue q = populatedQueue(SIZE);
466          for (int i = 0; i < SIZE; ++i) {
467 <            assertEquals(i, ((Integer)q.element()).intValue());
468 <            q.poll();
467 >            assertEquals(i, q.element());
468 >            assertEquals(i, q.poll());
469          }
470          try {
471              q.element();
# Line 503 | Line 479 | public class PriorityBlockingQueueTest e
479      public void testRemove() {
480          PriorityBlockingQueue q = populatedQueue(SIZE);
481          for (int i = 0; i < SIZE; ++i) {
482 <            assertEquals(i, ((Integer)q.remove()).intValue());
482 >            assertEquals(i, q.remove());
483          }
484          try {
485              q.remove();
# Line 603 | Line 579 | public class PriorityBlockingQueueTest e
579      }
580  
581      /**
582 <     *  toArray contains all elements
582 >     * toArray contains all elements
583       */
584      public void testToArray() throws InterruptedException {
585          PriorityBlockingQueue q = populatedQueue(SIZE);
# Line 629 | Line 605 | public class PriorityBlockingQueueTest e
605       * toArray(null) throws NPE
606       */
607      public void testToArray_BadArg() {
608 +        PriorityBlockingQueue q = populatedQueue(SIZE);
609          try {
633            PriorityBlockingQueue q = populatedQueue(SIZE);
610              Object o[] = q.toArray(null);
611              shouldThrow();
612          } catch (NullPointerException success) {}
613      }
614  
615      /**
616 <     * toArray with incompatible array type throws CCE
616 >     * toArray(incompatible array type) throws ArrayStoreException
617       */
618      public void testToArray1_BadArg() {
619 +        PriorityBlockingQueue q = populatedQueue(SIZE);
620          try {
621 <            PriorityBlockingQueue q = populatedQueue(SIZE);
645 <            Object o[] = q.toArray(new String[10] );
621 >            q.toArray(new String[10]);
622              shouldThrow();
623          } catch (ArrayStoreException success) {}
624      }
# Line 664 | Line 640 | public class PriorityBlockingQueueTest e
640      /**
641       * iterator.remove removes current element
642       */
643 <    public void testIteratorRemove () {
643 >    public void testIteratorRemove() {
644          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
645          q.add(new Integer(2));
646          q.add(new Integer(1));
# Line 700 | Line 676 | public class PriorityBlockingQueueTest e
676          ExecutorService executor = Executors.newFixedThreadPool(2);
677          executor.execute(new CheckedRunnable() {
678              public void realRun() throws InterruptedException {
679 <                threadAssertNull(q.poll());
680 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
681 <                threadAssertTrue(q.isEmpty());
679 >                assertNull(q.poll());
680 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
681 >                assertTrue(q.isEmpty());
682              }});
683  
684          executor.execute(new CheckedRunnable() {
685              public void realRun() throws InterruptedException {
686                  Thread.sleep(SMALL_DELAY_MS);
687 <                q.put(new Integer(1));
687 >                q.put(one);
688              }});
689  
690          joinPool(executor);
# Line 821 | Line 797 | public class PriorityBlockingQueueTest e
797      }
798  
799      /**
800 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
800 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
801       */
802      public void testDrainToN() {
803          PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2);
# Line 830 | Line 806 | public class PriorityBlockingQueueTest e
806                  assertTrue(q.offer(new Integer(j)));
807              ArrayList l = new ArrayList();
808              q.drainTo(l, i);
809 <            int k = (i < SIZE)? i : SIZE;
809 >            int k = (i < SIZE) ? i : SIZE;
810              assertEquals(l.size(), k);
811              assertEquals(q.size(), SIZE-k);
812              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines