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.15 by jsr166, Sat Nov 21 00:04:40 2009 UTC vs.
Revision 1.32 by jsr166, Tue Oct 19 00:43:49 2010 UTC

# Line 9 | Line 9
9   import junit.framework.*;
10   import java.util.*;
11   import java.util.concurrent.*;
12 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
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 24 | 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();
28 <            int j = ((Integer)y).intValue();
29 <            if (i < j) return 1;
30 <            if (i > j) return -1;
31 <            return 0;
44 >            return ((Comparable)y).compareTo(x);
45          }
46      }
47  
# Line 39 | Line 52 | public class PriorityBlockingQueueTest e
52      private PriorityBlockingQueue populatedQueue(int n) {
53          PriorityBlockingQueue q = new PriorityBlockingQueue(n);
54          assertTrue(q.isEmpty());
55 <        for (int i = n-1; i >= 0; i-=2)
56 <            assertTrue(q.offer(new Integer(i)));
57 <        for (int i = (n & 1); i < n; i+=2)
58 <            assertTrue(q.offer(new Integer(i)));
55 >        for (int i = n-1; i >= 0; i-=2)
56 >            assertTrue(q.offer(new Integer(i)));
57 >        for (int i = (n & 1); i < n; i+=2)
58 >            assertTrue(q.offer(new Integer(i)));
59          assertFalse(q.isEmpty());
60          assertEquals(NOCAP, q.remainingCapacity());
61 <        assertEquals(n, q.size());
61 >        assertEquals(n, q.size());
62          return q;
63      }
64  
# Line 57 | 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 164 | Line 177 | public class PriorityBlockingQueueTest e
177       * offer(null) throws NPE
178       */
179      public void testOfferNull() {
180 <        try {
180 >        try {
181              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
182              q.offer(null);
183              shouldThrow();
# Line 175 | Line 188 | public class PriorityBlockingQueueTest e
188       * add(null) throws NPE
189       */
190      public void testAddNull() {
191 <        try {
191 >        try {
192              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
193              q.add(null);
194              shouldThrow();
# Line 248 | 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 282 | Line 296 | public class PriorityBlockingQueueTest e
296       * put(null) throws NPE
297       */
298       public void testPutNull() {
299 <        try {
299 >        try {
300              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
301              q.put(null);
302              shouldThrow();
# Line 327 | 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, TimeUnit.MILLISECONDS));
350 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
337 <                    } finally { }
338 <                }
339 <            });
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 350 | 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 374 | 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 {
379                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 396 | 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());
415 >        assertNull(q.poll());
416      }
417  
418      /**
# Line 407 | 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, TimeUnit.MILLISECONDS)).intValue());
424 >            assertEquals(i, q.poll(0, MILLISECONDS));
425          }
426 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
426 >        assertNull(q.poll(0, MILLISECONDS));
427      }
428  
429      /**
# Line 418 | 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, TimeUnit.MILLISECONDS)).intValue());
435 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
436          }
437 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
437 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
438      }
439  
440      /**
# Line 432 | 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, TimeUnit.MILLISECONDS)).intValue());
449 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
450                  }
451                  try {
452 <                    q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
453 <                    threadShouldThrow();
452 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
453 >                    shouldThrow();
454                  } catch (InterruptedException success) {}
455              }});
456  
# Line 447 | Line 461 | public class PriorityBlockingQueueTest e
461      }
462  
463      /**
450     *  timed poll before a delayed offer fails; after offer succeeds;
451     *  on interruption throws
452     */
453    public void testTimedPollWithOffer() throws InterruptedException {
454        final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
455        Thread t = new Thread(new CheckedRunnable() {
456            public void realRun() throws InterruptedException {
457                threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
458                threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
459                try {
460                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
461                    threadShouldThrow();
462                } catch (InterruptedException success) {}
463            }});
464
465        t.start();
466        Thread.sleep(SMALL_DELAY_MS);
467        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
468        t.interrupt();
469        t.join();
470    }
471
472
473    /**
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());
474 >        assertNull(q.peek());
475      }
476  
477      /**
# Line 490 | 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 505 | 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 605 | 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);
602 <        Object[] o = q.toArray();
602 >        Object[] o = q.toArray();
603          Arrays.sort(o);
604 <        for (int i = 0; i < o.length; i++)
605 <            assertEquals(o[i], q.take());
604 >        for (int i = 0; i < o.length; i++)
605 >            assertEquals(o[i], q.take());
606      }
607  
608      /**
# Line 620 | Line 610 | public class PriorityBlockingQueueTest e
610       */
611      public void testToArray2() throws InterruptedException {
612          PriorityBlockingQueue q = populatedQueue(SIZE);
613 <        Integer[] ints = new Integer[SIZE];
614 <        ints = (Integer[])q.toArray(ints);
613 >        Integer[] ints = new Integer[SIZE];
614 >        ints = (Integer[])q.toArray(ints);
615          Arrays.sort(ints);
616          for (int i = 0; i < ints.length; i++)
617              assertEquals(ints[i], q.take());
# Line 631 | Line 621 | public class PriorityBlockingQueueTest e
621       * toArray(null) throws NPE
622       */
623      public void testToArray_BadArg() {
624 <        try {
625 <            PriorityBlockingQueue q = populatedQueue(SIZE);
626 <            Object o[] = q.toArray(null);
627 <            shouldThrow();
628 <        } catch (NullPointerException success) {}
624 >        PriorityBlockingQueue q = populatedQueue(SIZE);
625 >        try {
626 >            Object o[] = q.toArray(null);
627 >            shouldThrow();
628 >        } catch (NullPointerException success) {}
629      }
630  
631      /**
632       * toArray with incompatible array type throws CCE
633       */
634      public void testToArray1_BadArg() {
635 <        try {
636 <            PriorityBlockingQueue q = populatedQueue(SIZE);
637 <            Object o[] = q.toArray(new String[10] );
638 <            shouldThrow();
639 <        } catch (ArrayStoreException  success) {}
635 >        PriorityBlockingQueue q = populatedQueue(SIZE);
636 >        try {
637 >            Object o[] = q.toArray(new String[10]);
638 >            shouldThrow();
639 >        } catch (ArrayStoreException success) {}
640      }
641  
642      /**
# Line 655 | Line 645 | public class PriorityBlockingQueueTest e
645      public void testIterator() {
646          PriorityBlockingQueue q = populatedQueue(SIZE);
647          int i = 0;
648 <        Iterator it = q.iterator();
648 >        Iterator it = q.iterator();
649          while (it.hasNext()) {
650              assertTrue(q.contains(it.next()));
651              ++i;
# Line 666 | 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 702 | 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, TimeUnit.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 823 | 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 832 | 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