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.28 by dl, Wed Sep 29 12:33:48 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      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
20 <        return new TestSuite(PriorityBlockingQueueTest.class);
20 >        return new TestSuite(PriorityBlockingQueueTest.class);
21      }
22  
23      private static final int NOCAP = Integer.MAX_VALUE;
# Line 24 | Line 25 | public class PriorityBlockingQueueTest e
25      /** Sample Comparator */
26      static class MyReverseComparator implements Comparator {
27          public int compare(Object x, Object y) {
28 <            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;
28 >            return ((Comparable)y).compareTo(x);
29          }
30      }
31  
# Line 39 | Line 36 | public class PriorityBlockingQueueTest e
36      private PriorityBlockingQueue populatedQueue(int n) {
37          PriorityBlockingQueue q = new PriorityBlockingQueue(n);
38          assertTrue(q.isEmpty());
39 <        for (int i = n-1; i >= 0; i-=2)
40 <            assertTrue(q.offer(new Integer(i)));
41 <        for (int i = (n & 1); i < n; i+=2)
42 <            assertTrue(q.offer(new Integer(i)));
39 >        for (int i = n-1; i >= 0; i-=2)
40 >            assertTrue(q.offer(new Integer(i)));
41 >        for (int i = (n & 1); i < n; i+=2)
42 >            assertTrue(q.offer(new Integer(i)));
43          assertFalse(q.isEmpty());
44          assertEquals(NOCAP, q.remainingCapacity());
45 <        assertEquals(n, q.size());
45 >        assertEquals(n, q.size());
46          return q;
47      }
48  
# Line 57 | Line 54 | public class PriorityBlockingQueueTest e
54      }
55  
56      /**
57 <     * Constructor throws IAE if  capacity argument nonpositive
57 >     * Constructor throws IAE if capacity argument nonpositive
58       */
59      public void testConstructor2() {
60          try {
# Line 164 | Line 161 | public class PriorityBlockingQueueTest e
161       * offer(null) throws NPE
162       */
163      public void testOfferNull() {
164 <        try {
164 >        try {
165              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
166              q.offer(null);
167              shouldThrow();
# Line 175 | Line 172 | public class PriorityBlockingQueueTest e
172       * add(null) throws NPE
173       */
174      public void testAddNull() {
175 <        try {
175 >        try {
176              PriorityBlockingQueue q = new PriorityBlockingQueue(1);
177              q.add(null);
178              shouldThrow();
# Line 248 | Line 245 | public class PriorityBlockingQueueTest e
245              shouldThrow();
246          } catch (NullPointerException success) {}
247      }
248 +
249      /**
250       * addAll of a collection with any null elements throws NPE after
251       * possibly adding some elements
# Line 282 | Line 280 | public class PriorityBlockingQueueTest e
280       * put(null) throws NPE
281       */
282       public void testPutNull() {
283 <        try {
283 >        try {
284              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
285              q.put(null);
286              shouldThrow();
# Line 327 | Line 325 | public class PriorityBlockingQueueTest e
325       */
326      public void testTimedOffer() throws InterruptedException {
327          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
328 <        Thread t = new Thread(new Runnable() {
329 <                public void run() {
330 <                    try {
331 <                        q.put(new Integer(0));
332 <                        q.put(new Integer(0));
333 <                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
334 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
337 <                    } finally { }
338 <                }
339 <            });
328 >        Thread t = new Thread(new CheckedRunnable() {
329 >            public void realRun() {
330 >                q.put(new Integer(0));
331 >                q.put(new Integer(0));
332 >                assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
333 >                assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
334 >            }});
335  
336          t.start();
337          Thread.sleep(SMALL_DELAY_MS);
# Line 350 | Line 345 | public class PriorityBlockingQueueTest e
345      public void testTake() throws InterruptedException {
346          PriorityBlockingQueue q = populatedQueue(SIZE);
347          for (int i = 0; i < SIZE; ++i) {
348 <            assertEquals(i, ((Integer)q.take()).intValue());
348 >            assertEquals(i, q.take());
349          }
350      }
351  
# Line 374 | Line 369 | public class PriorityBlockingQueueTest e
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 {
379                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 396 | 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());
399 >        assertNull(q.poll());
400      }
401  
402      /**
# Line 407 | Line 405 | public class PriorityBlockingQueueTest e
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, TimeUnit.MILLISECONDS)).intValue());
408 >            assertEquals(i, q.poll(0, MILLISECONDS));
409          }
410 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
410 >        assertNull(q.poll(0, MILLISECONDS));
411      }
412  
413      /**
# Line 418 | Line 416 | public class PriorityBlockingQueueTest e
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, TimeUnit.MILLISECONDS)).intValue());
419 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
420          }
421 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
421 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
422      }
423  
424      /**
# Line 432 | 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 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
433 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
434                  }
435                  try {
436 <                    q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
437 <                    threadShouldThrow();
436 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
437 >                    shouldThrow();
438                  } catch (InterruptedException success) {}
439              }});
440  
# Line 454 | Line 452 | public class PriorityBlockingQueueTest e
452          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
453          Thread t = new Thread(new CheckedRunnable() {
454              public void realRun() throws InterruptedException {
457                threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
458                threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
455                  try {
456 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
457 <                    threadShouldThrow();
456 >                    assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
457 >                    assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
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(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
465 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
466          t.interrupt();
467          t.join();
468      }
# Line 476 | Line 474 | public class PriorityBlockingQueueTest e
474      public void testPeek() {
475          PriorityBlockingQueue q = populatedQueue(SIZE);
476          for (int i = 0; i < SIZE; ++i) {
477 <            assertEquals(i, ((Integer)q.peek()).intValue());
478 <            q.poll();
477 >            assertEquals(i, q.peek());
478 >            assertEquals(i, q.poll());
479              assertTrue(q.peek() == null ||
480 <                       i != ((Integer)q.peek()).intValue());
480 >                       !q.peek().equals(i));
481          }
482 <        assertNull(q.peek());
482 >        assertNull(q.peek());
483      }
484  
485      /**
# Line 490 | Line 488 | public class PriorityBlockingQueueTest e
488      public void testElement() {
489          PriorityBlockingQueue q = populatedQueue(SIZE);
490          for (int i = 0; i < SIZE; ++i) {
491 <            assertEquals(i, ((Integer)q.element()).intValue());
492 <            q.poll();
491 >            assertEquals(i, q.element());
492 >            assertEquals(i, q.poll());
493          }
494          try {
495              q.element();
# Line 505 | Line 503 | public class PriorityBlockingQueueTest e
503      public void testRemove() {
504          PriorityBlockingQueue q = populatedQueue(SIZE);
505          for (int i = 0; i < SIZE; ++i) {
506 <            assertEquals(i, ((Integer)q.remove()).intValue());
506 >            assertEquals(i, q.remove());
507          }
508          try {
509              q.remove();
# Line 609 | Line 607 | public class PriorityBlockingQueueTest e
607       */
608      public void testToArray() throws InterruptedException {
609          PriorityBlockingQueue q = populatedQueue(SIZE);
610 <        Object[] o = q.toArray();
610 >        Object[] o = q.toArray();
611          Arrays.sort(o);
612 <        for (int i = 0; i < o.length; i++)
613 <            assertEquals(o[i], q.take());
612 >        for (int i = 0; i < o.length; i++)
613 >            assertEquals(o[i], q.take());
614      }
615  
616      /**
# Line 620 | Line 618 | public class PriorityBlockingQueueTest e
618       */
619      public void testToArray2() throws InterruptedException {
620          PriorityBlockingQueue q = populatedQueue(SIZE);
621 <        Integer[] ints = new Integer[SIZE];
622 <        ints = (Integer[])q.toArray(ints);
621 >        Integer[] ints = new Integer[SIZE];
622 >        ints = (Integer[])q.toArray(ints);
623          Arrays.sort(ints);
624          for (int i = 0; i < ints.length; i++)
625              assertEquals(ints[i], q.take());
# Line 631 | Line 629 | public class PriorityBlockingQueueTest e
629       * toArray(null) throws NPE
630       */
631      public void testToArray_BadArg() {
632 <        try {
633 <            PriorityBlockingQueue q = populatedQueue(SIZE);
634 <            Object o[] = q.toArray(null);
635 <            shouldThrow();
636 <        } catch (NullPointerException success) {}
632 >        PriorityBlockingQueue q = populatedQueue(SIZE);
633 >        try {
634 >            Object o[] = q.toArray(null);
635 >            shouldThrow();
636 >        } catch (NullPointerException success) {}
637      }
638  
639      /**
640       * toArray with incompatible array type throws CCE
641       */
642      public void testToArray1_BadArg() {
643 <        try {
644 <            PriorityBlockingQueue q = populatedQueue(SIZE);
645 <            Object o[] = q.toArray(new String[10] );
646 <            shouldThrow();
647 <        } catch (ArrayStoreException  success) {}
643 >        PriorityBlockingQueue q = populatedQueue(SIZE);
644 >        try {
645 >            Object o[] = q.toArray(new String[10]);
646 >            shouldThrow();
647 >        } catch (ArrayStoreException success) {}
648      }
649  
650      /**
# Line 655 | Line 653 | public class PriorityBlockingQueueTest e
653      public void testIterator() {
654          PriorityBlockingQueue q = populatedQueue(SIZE);
655          int i = 0;
656 <        Iterator it = q.iterator();
656 >        Iterator it = q.iterator();
657          while (it.hasNext()) {
658              assertTrue(q.contains(it.next()));
659              ++i;
# Line 666 | Line 664 | public class PriorityBlockingQueueTest e
664      /**
665       * iterator.remove removes current element
666       */
667 <    public void testIteratorRemove () {
667 >    public void testIteratorRemove() {
668          final PriorityBlockingQueue q = new PriorityBlockingQueue(3);
669          q.add(new Integer(2));
670          q.add(new Integer(1));
# Line 702 | Line 700 | public class PriorityBlockingQueueTest e
700          ExecutorService executor = Executors.newFixedThreadPool(2);
701          executor.execute(new CheckedRunnable() {
702              public void realRun() throws InterruptedException {
703 <                threadAssertNull(q.poll());
704 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
705 <                threadAssertTrue(q.isEmpty());
703 >                assertNull(q.poll());
704 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
705 >                assertTrue(q.isEmpty());
706              }});
707  
708          executor.execute(new CheckedRunnable() {
709              public void realRun() throws InterruptedException {
710                  Thread.sleep(SMALL_DELAY_MS);
711 <                q.put(new Integer(1));
711 >                q.put(one);
712              }});
713  
714          joinPool(executor);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines