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.25 by jsr166, Tue Dec 1 06:03:49 2009 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 282 | Line 279 | public class PriorityBlockingQueueTest e
279       * put(null) throws NPE
280       */
281       public void testPutNull() {
282 <        try {
282 >        try {
283              PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE);
284              q.put(null);
285              shouldThrow();
# Line 327 | Line 324 | public class PriorityBlockingQueueTest e
324       */
325      public void testTimedOffer() throws InterruptedException {
326          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
327 <        Thread t = new Thread(new Runnable() {
328 <                public void run() {
329 <                    try {
330 <                        q.put(new Integer(0));
331 <                        q.put(new Integer(0));
332 <                        threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
333 <                        threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS));
337 <                    } finally { }
338 <                }
339 <            });
327 >        Thread t = new Thread(new CheckedRunnable() {
328 >            public void realRun() {
329 >                q.put(new Integer(0));
330 >                q.put(new Integer(0));
331 >                assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS));
332 >                assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS));
333 >            }});
334  
335          t.start();
336          Thread.sleep(SMALL_DELAY_MS);
# Line 350 | Line 344 | public class PriorityBlockingQueueTest e
344      public void testTake() throws InterruptedException {
345          PriorityBlockingQueue q = populatedQueue(SIZE);
346          for (int i = 0; i < SIZE; ++i) {
347 <            assertEquals(i, ((Integer)q.take()).intValue());
347 >            assertEquals(i, q.take());
348          }
349      }
350  
# Line 374 | Line 368 | public class PriorityBlockingQueueTest e
368       * Take removes existing elements until empty, then blocks interruptibly
369       */
370      public void testBlockingTake() throws InterruptedException {
371 <        Thread t = new Thread(new CheckedInterruptedRunnable() {
371 >        final PriorityBlockingQueue q = populatedQueue(SIZE);
372 >        Thread t = new Thread(new CheckedRunnable() {
373              public void realRun() throws InterruptedException {
379                PriorityBlockingQueue q = populatedQueue(SIZE);
374                  for (int i = 0; i < SIZE; ++i) {
375 <                    threadAssertEquals(i, ((Integer)q.take()).intValue());
375 >                    assertEquals(i, q.take());
376                  }
377 <                q.take();
377 >                try {
378 >                    q.take();
379 >                    shouldThrow();
380 >                } catch (InterruptedException success) {}
381              }});
382  
383          t.start();
# Line 396 | Line 393 | public class PriorityBlockingQueueTest e
393      public void testPoll() {
394          PriorityBlockingQueue q = populatedQueue(SIZE);
395          for (int i = 0; i < SIZE; ++i) {
396 <            assertEquals(i, ((Integer)q.poll()).intValue());
396 >            assertEquals(i, q.poll());
397          }
398 <        assertNull(q.poll());
398 >        assertNull(q.poll());
399      }
400  
401      /**
# Line 407 | Line 404 | public class PriorityBlockingQueueTest e
404      public void testTimedPoll0() throws InterruptedException {
405          PriorityBlockingQueue q = populatedQueue(SIZE);
406          for (int i = 0; i < SIZE; ++i) {
407 <            assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue());
407 >            assertEquals(i, q.poll(0, MILLISECONDS));
408          }
409 <        assertNull(q.poll(0, TimeUnit.MILLISECONDS));
409 >        assertNull(q.poll(0, MILLISECONDS));
410      }
411  
412      /**
# Line 418 | Line 415 | public class PriorityBlockingQueueTest e
415      public void testTimedPoll() throws InterruptedException {
416          PriorityBlockingQueue q = populatedQueue(SIZE);
417          for (int i = 0; i < SIZE; ++i) {
418 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
418 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
419          }
420 <        assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
420 >        assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
421      }
422  
423      /**
# Line 432 | Line 429 | public class PriorityBlockingQueueTest e
429              public void realRun() throws InterruptedException {
430                  PriorityBlockingQueue q = populatedQueue(SIZE);
431                  for (int i = 0; i < SIZE; ++i) {
432 <                    threadAssertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue());
432 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
433                  }
434                  try {
435 <                    q.poll(SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
436 <                    threadShouldThrow();
435 >                    q.poll(SMALL_DELAY_MS, MILLISECONDS);
436 >                    shouldThrow();
437                  } catch (InterruptedException success) {}
438              }});
439  
# Line 454 | Line 451 | public class PriorityBlockingQueueTest e
451          final PriorityBlockingQueue q = new PriorityBlockingQueue(2);
452          Thread t = new Thread(new CheckedRunnable() {
453              public void realRun() throws InterruptedException {
454 <                threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
455 <                threadAssertEquals(0, q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
454 >                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
455 >                assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
456                  try {
457 <                    q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS);
458 <                    threadShouldThrow();
457 >                    q.poll(LONG_DELAY_MS, MILLISECONDS);
458 >                    shouldThrow();
459                  } catch (InterruptedException success) {}
460              }});
461  
462          t.start();
463          Thread.sleep(SMALL_DELAY_MS);
464 <        assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS));
464 >        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
465          t.interrupt();
466          t.join();
467      }
# Line 476 | Line 473 | public class PriorityBlockingQueueTest e
473      public void testPeek() {
474          PriorityBlockingQueue q = populatedQueue(SIZE);
475          for (int i = 0; i < SIZE; ++i) {
476 <            assertEquals(i, ((Integer)q.peek()).intValue());
477 <            q.poll();
476 >            assertEquals(i, q.peek());
477 >            assertEquals(i, q.poll());
478              assertTrue(q.peek() == null ||
479 <                       i != ((Integer)q.peek()).intValue());
479 >                       !q.peek().equals(i));
480          }
481 <        assertNull(q.peek());
481 >        assertNull(q.peek());
482      }
483  
484      /**
# Line 490 | Line 487 | public class PriorityBlockingQueueTest e
487      public void testElement() {
488          PriorityBlockingQueue q = populatedQueue(SIZE);
489          for (int i = 0; i < SIZE; ++i) {
490 <            assertEquals(i, ((Integer)q.element()).intValue());
491 <            q.poll();
490 >            assertEquals(i, q.element());
491 >            assertEquals(i, q.poll());
492          }
493          try {
494              q.element();
# Line 505 | Line 502 | public class PriorityBlockingQueueTest e
502      public void testRemove() {
503          PriorityBlockingQueue q = populatedQueue(SIZE);
504          for (int i = 0; i < SIZE; ++i) {
505 <            assertEquals(i, ((Integer)q.remove()).intValue());
505 >            assertEquals(i, q.remove());
506          }
507          try {
508              q.remove();
# Line 609 | Line 606 | public class PriorityBlockingQueueTest e
606       */
607      public void testToArray() throws InterruptedException {
608          PriorityBlockingQueue q = populatedQueue(SIZE);
609 <        Object[] o = q.toArray();
609 >        Object[] o = q.toArray();
610          Arrays.sort(o);
611 <        for (int i = 0; i < o.length; i++)
612 <            assertEquals(o[i], q.take());
611 >        for (int i = 0; i < o.length; i++)
612 >            assertEquals(o[i], q.take());
613      }
614  
615      /**
# Line 620 | Line 617 | public class PriorityBlockingQueueTest e
617       */
618      public void testToArray2() throws InterruptedException {
619          PriorityBlockingQueue q = populatedQueue(SIZE);
620 <        Integer[] ints = new Integer[SIZE];
621 <        ints = (Integer[])q.toArray(ints);
620 >        Integer[] ints = new Integer[SIZE];
621 >        ints = (Integer[])q.toArray(ints);
622          Arrays.sort(ints);
623          for (int i = 0; i < ints.length; i++)
624              assertEquals(ints[i], q.take());
# Line 631 | Line 628 | public class PriorityBlockingQueueTest e
628       * toArray(null) throws NPE
629       */
630      public void testToArray_BadArg() {
631 <        try {
632 <            PriorityBlockingQueue q = populatedQueue(SIZE);
633 <            Object o[] = q.toArray(null);
634 <            shouldThrow();
635 <        } catch (NullPointerException success) {}
631 >        PriorityBlockingQueue q = populatedQueue(SIZE);
632 >        try {
633 >            Object o[] = q.toArray(null);
634 >            shouldThrow();
635 >        } catch (NullPointerException success) {}
636      }
637  
638      /**
639       * toArray with incompatible array type throws CCE
640       */
641      public void testToArray1_BadArg() {
642 <        try {
643 <            PriorityBlockingQueue q = populatedQueue(SIZE);
644 <            Object o[] = q.toArray(new String[10] );
645 <            shouldThrow();
646 <        } catch (ArrayStoreException  success) {}
642 >        PriorityBlockingQueue q = populatedQueue(SIZE);
643 >        try {
644 >            Object o[] = q.toArray(new String[10]);
645 >            shouldThrow();
646 >        } catch (ArrayStoreException success) {}
647      }
648  
649      /**
# Line 655 | Line 652 | public class PriorityBlockingQueueTest e
652      public void testIterator() {
653          PriorityBlockingQueue q = populatedQueue(SIZE);
654          int i = 0;
655 <        Iterator it = q.iterator();
655 >        Iterator it = q.iterator();
656          while (it.hasNext()) {
657              assertTrue(q.contains(it.next()));
658              ++i;
# Line 702 | Line 699 | public class PriorityBlockingQueueTest e
699          ExecutorService executor = Executors.newFixedThreadPool(2);
700          executor.execute(new CheckedRunnable() {
701              public void realRun() throws InterruptedException {
702 <                threadAssertNull(q.poll());
703 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS));
704 <                threadAssertTrue(q.isEmpty());
702 >                assertNull(q.poll());
703 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
704 >                assertTrue(q.isEmpty());
705              }});
706  
707          executor.execute(new CheckedRunnable() {
708              public void realRun() throws InterruptedException {
709                  Thread.sleep(SMALL_DELAY_MS);
710 <                q.put(new Integer(1));
710 >                q.put(one);
711              }});
712  
713          joinPool(executor);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines