ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/LinkedBlockingQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/LinkedBlockingQueueTest.java (file contents):
Revision 1.22 by jsr166, Sun Nov 22 00:17:37 2009 UTC vs.
Revision 1.38 by jsr166, Thu Nov 18 20:21:53 2010 UTC

# Line 14 | Line 14 | import java.io.*;
14  
15   public class LinkedBlockingQueueTest extends JSR166TestCase {
16  
17 +    public static class Unbounded extends BlockingQueueTest {
18 +        protected BlockingQueue emptyCollection() {
19 +            return new LinkedBlockingQueue();
20 +        }
21 +    }
22 +
23 +    public static class Bounded extends BlockingQueueTest {
24 +        protected BlockingQueue emptyCollection() {
25 +            return new LinkedBlockingQueue(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(LinkedBlockingQueueTest.class);
34 >        return newTestSuite(LinkedBlockingQueueTest.class,
35 >                            new Unbounded().testSuite(),
36 >                            new Bounded().testSuite());
37      }
38  
39  
# Line 27 | Line 41 | public class LinkedBlockingQueueTest ext
41       * Create a queue of given size containing consecutive
42       * Integers 0 ... n.
43       */
44 <    private LinkedBlockingQueue populatedQueue(int n) {
45 <        LinkedBlockingQueue q = new LinkedBlockingQueue(n);
44 >    private LinkedBlockingQueue<Integer> populatedQueue(int n) {
45 >        LinkedBlockingQueue<Integer> q =
46 >            new LinkedBlockingQueue<Integer>(n);
47          assertTrue(q.isEmpty());
48          for (int i = 0; i < n; i++)
49              assertTrue(q.offer(new Integer(i)));
# Line 214 | Line 229 | public class LinkedBlockingQueueTest ext
229              shouldThrow();
230          } catch (NullPointerException success) {}
231      }
232 +
233      /**
234       * addAll of a collection with any null elements throws NPE after
235       * possibly adding some elements
# Line 228 | Line 244 | public class LinkedBlockingQueueTest ext
244              shouldThrow();
245          } catch (NullPointerException success) {}
246      }
247 +
248      /**
249       * addAll throws ISE if not enough room
250       */
# Line 241 | Line 258 | public class LinkedBlockingQueueTest ext
258              shouldThrow();
259          } catch (IllegalStateException success) {}
260      }
261 +
262      /**
263       * Queue contains all elements, in traversal order, of successful addAll
264       */
# Line 359 | Line 377 | public class LinkedBlockingQueueTest ext
377      public void testTake() throws InterruptedException {
378          LinkedBlockingQueue q = populatedQueue(SIZE);
379          for (int i = 0; i < SIZE; ++i) {
380 <            assertEquals(i, ((Integer)q.take()).intValue());
380 >            assertEquals(i, q.take());
381          }
382      }
383  
384      /**
367     * take blocks interruptibly when empty
368     */
369    public void testTakeFromEmpty() throws InterruptedException {
370        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
371        Thread t = new ThreadShouldThrow(InterruptedException.class) {
372            public void realRun() throws InterruptedException {
373                q.take();
374            }};
375
376        t.start();
377        Thread.sleep(SHORT_DELAY_MS);
378        t.interrupt();
379        t.join();
380    }
381
382    /**
385       * Take removes existing elements until empty, then blocks interruptibly
386       */
387      public void testBlockingTake() throws InterruptedException {
# Line 387 | Line 389 | public class LinkedBlockingQueueTest ext
389          Thread t = new Thread(new CheckedRunnable() {
390              public void realRun() throws InterruptedException {
391                  for (int i = 0; i < SIZE; ++i) {
392 <                    assertEquals(i, ((Integer)q.take()).intValue());
392 >                    assertEquals(i, q.take());
393                  }
394                  try {
395                      q.take();
# Line 407 | Line 409 | public class LinkedBlockingQueueTest ext
409      public void testPoll() {
410          LinkedBlockingQueue q = populatedQueue(SIZE);
411          for (int i = 0; i < SIZE; ++i) {
412 <            assertEquals(i, ((Integer)q.poll()).intValue());
412 >            assertEquals(i, q.poll());
413          }
414          assertNull(q.poll());
415      }
416  
417      /**
418 <     * timed pool with zero timeout succeeds when non-empty, else times out
418 >     * timed poll with zero timeout succeeds when non-empty, else times out
419       */
420      public void testTimedPoll0() throws InterruptedException {
421          LinkedBlockingQueue q = populatedQueue(SIZE);
422          for (int i = 0; i < SIZE; ++i) {
423 <            assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue());
423 >            assertEquals(i, q.poll(0, MILLISECONDS));
424          }
425          assertNull(q.poll(0, MILLISECONDS));
426      }
427  
428      /**
429 <     * timed pool with nonzero timeout succeeds when non-empty, else times out
429 >     * timed poll with nonzero timeout succeeds when non-empty, else times out
430       */
431      public void testTimedPoll() throws InterruptedException {
432          LinkedBlockingQueue q = populatedQueue(SIZE);
433          for (int i = 0; i < SIZE; ++i) {
434 <            assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
434 >            assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
435          }
436          assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
437      }
# Line 443 | Line 445 | public class LinkedBlockingQueueTest ext
445              public void realRun() throws InterruptedException {
446                  LinkedBlockingQueue q = populatedQueue(SIZE);
447                  for (int i = 0; i < SIZE; ++i) {
448 <                    assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue());
448 >                    assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS));
449                  }
450                  try {
451                      q.poll(SMALL_DELAY_MS, MILLISECONDS);
# Line 458 | Line 460 | public class LinkedBlockingQueueTest ext
460      }
461  
462      /**
461     *  timed poll before a delayed offer fails; after offer succeeds;
462     *  on interruption throws
463     */
464    public void testTimedPollWithOffer() throws InterruptedException {
465        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
466        Thread t = new Thread(new CheckedRunnable() {
467            public void realRun() throws InterruptedException {
468                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
469                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
470                try {
471                    q.poll(LONG_DELAY_MS, MILLISECONDS);
472                    shouldThrow();
473                } catch (InterruptedException success) {}
474            }});
475
476        t.start();
477        Thread.sleep(SMALL_DELAY_MS);
478        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
479        t.interrupt();
480        t.join();
481    }
482
483    /**
463       * peek returns next element, or null if empty
464       */
465      public void testPeek() {
466          LinkedBlockingQueue q = populatedQueue(SIZE);
467          for (int i = 0; i < SIZE; ++i) {
468 <            assertEquals(i, ((Integer)q.peek()).intValue());
469 <            q.poll();
468 >            assertEquals(i, q.peek());
469 >            assertEquals(i, q.poll());
470              assertTrue(q.peek() == null ||
471 <                       i != ((Integer)q.peek()).intValue());
471 >                       !q.peek().equals(i));
472          }
473          assertNull(q.peek());
474      }
# Line 500 | Line 479 | public class LinkedBlockingQueueTest ext
479      public void testElement() {
480          LinkedBlockingQueue q = populatedQueue(SIZE);
481          for (int i = 0; i < SIZE; ++i) {
482 <            assertEquals(i, ((Integer)q.element()).intValue());
483 <            q.poll();
482 >            assertEquals(i, q.element());
483 >            assertEquals(i, q.poll());
484          }
485          try {
486              q.element();
# Line 515 | Line 494 | public class LinkedBlockingQueueTest ext
494      public void testRemove() {
495          LinkedBlockingQueue q = populatedQueue(SIZE);
496          for (int i = 0; i < SIZE; ++i) {
497 <            assertEquals(i, ((Integer)q.remove()).intValue());
497 >            assertEquals(i, q.remove());
498          }
499          try {
500              q.remove();
# Line 529 | Line 508 | public class LinkedBlockingQueueTest ext
508      public void testRemoveElement() {
509          LinkedBlockingQueue q = populatedQueue(SIZE);
510          for (int i = 1; i < SIZE; i+=2) {
511 <            assertTrue(q.remove(new Integer(i)));
511 >            assertTrue(q.contains(i));
512 >            assertTrue(q.remove(i));
513 >            assertFalse(q.contains(i));
514 >            assertTrue(q.contains(i-1));
515          }
516          for (int i = 0; i < SIZE; i+=2) {
517 <            assertTrue(q.remove(new Integer(i)));
518 <            assertFalse(q.remove(new Integer(i+1)));
517 >            assertTrue(q.contains(i));
518 >            assertTrue(q.remove(i));
519 >            assertFalse(q.contains(i));
520 >            assertFalse(q.remove(i+1));
521 >            assertFalse(q.contains(i+1));
522          }
523          assertTrue(q.isEmpty());
524      }
# Line 629 | Line 614 | public class LinkedBlockingQueueTest ext
614      }
615  
616      /**
617 <     * toArray contains all elements
617 >     * toArray contains all elements in FIFO order
618       */
619 <    public void testToArray() throws InterruptedException {
619 >    public void testToArray() {
620          LinkedBlockingQueue q = populatedQueue(SIZE);
621          Object[] o = q.toArray();
622          for (int i = 0; i < o.length; i++)
623 <            assertEquals(o[i], q.take());
623 >            assertSame(o[i], q.poll());
624      }
625  
626      /**
627 <     * toArray(a) contains all elements
627 >     * toArray(a) contains all elements in FIFO order
628       */
629      public void testToArray2() throws InterruptedException {
630 <        LinkedBlockingQueue q = populatedQueue(SIZE);
630 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
631          Integer[] ints = new Integer[SIZE];
632 <        ints = (Integer[])q.toArray(ints);
632 >        Integer[] array = q.toArray(ints);
633 >        assertSame(ints, array);
634          for (int i = 0; i < ints.length; i++)
635 <            assertEquals(ints[i], q.take());
635 >            assertSame(ints[i], q.poll());
636      }
637  
638      /**
639 <     * toArray(null) throws NPE
639 >     * toArray(null) throws NullPointerException
640       */
641 <    public void testToArray_BadArg() {
641 >    public void testToArray_NullArg() {
642 >        LinkedBlockingQueue q = populatedQueue(SIZE);
643          try {
644 <            LinkedBlockingQueue q = populatedQueue(SIZE);
658 <            Object o[] = q.toArray(null);
644 >            q.toArray(null);
645              shouldThrow();
646          } catch (NullPointerException success) {}
647      }
648  
649      /**
650 <     * toArray with incompatible array type throws CCE
650 >     * toArray(incompatible array type) throws ArrayStoreException
651       */
652      public void testToArray1_BadArg() {
653 +        LinkedBlockingQueue q = populatedQueue(SIZE);
654          try {
655 <            LinkedBlockingQueue q = populatedQueue(SIZE);
669 <            Object o[] = q.toArray(new String[10] );
655 >            q.toArray(new String[10]);
656              shouldThrow();
657          } catch (ArrayStoreException success) {}
658      }
# Line 686 | Line 672 | public class LinkedBlockingQueueTest ext
672      /**
673       * iterator.remove removes current element
674       */
675 <    public void testIteratorRemove () {
675 >    public void testIteratorRemove() {
676          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
677          q.add(two);
678          q.add(one);
# Line 697 | Line 683 | public class LinkedBlockingQueueTest ext
683          it.remove();
684  
685          it = q.iterator();
686 <        assertEquals(it.next(), one);
687 <        assertEquals(it.next(), three);
686 >        assertSame(it.next(), one);
687 >        assertSame(it.next(), three);
688          assertFalse(it.hasNext());
689      }
690  
# Line 714 | Line 700 | public class LinkedBlockingQueueTest ext
700          assertEquals(0, q.remainingCapacity());
701          int k = 0;
702          for (Iterator it = q.iterator(); it.hasNext();) {
703 <            int i = ((Integer)(it.next())).intValue();
718 <            assertEquals(++k, i);
703 >            assertEquals(++k, it.next());
704          }
705          assertEquals(3, k);
706      }
# Line 723 | Line 708 | public class LinkedBlockingQueueTest ext
708      /**
709       * Modifications do not cause iterators to fail
710       */
711 <    public void testWeaklyConsistentIteration () {
711 >    public void testWeaklyConsistentIteration() {
712          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
713          q.add(one);
714          q.add(two);
# Line 758 | Line 743 | public class LinkedBlockingQueueTest ext
743          ExecutorService executor = Executors.newFixedThreadPool(2);
744          executor.execute(new CheckedRunnable() {
745              public void realRun() throws InterruptedException {
746 <                threadAssertFalse(q.offer(three));
747 <                threadAssertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
748 <                threadAssertEquals(0, q.remainingCapacity());
746 >                assertFalse(q.offer(three));
747 >                assertTrue(q.offer(three, MEDIUM_DELAY_MS, MILLISECONDS));
748 >                assertEquals(0, q.remainingCapacity());
749              }});
750  
751          executor.execute(new CheckedRunnable() {
752              public void realRun() throws InterruptedException {
753                  Thread.sleep(SMALL_DELAY_MS);
754 <                threadAssertEquals(one, q.take());
754 >                assertSame(one, q.take());
755              }});
756  
757          joinPool(executor);
# Line 780 | Line 765 | public class LinkedBlockingQueueTest ext
765          ExecutorService executor = Executors.newFixedThreadPool(2);
766          executor.execute(new CheckedRunnable() {
767              public void realRun() throws InterruptedException {
768 <                threadAssertNull(q.poll());
769 <                threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
770 <                threadAssertTrue(q.isEmpty());
768 >                assertNull(q.poll());
769 >                assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS));
770 >                assertTrue(q.isEmpty());
771              }});
772  
773          executor.execute(new CheckedRunnable() {
# Line 902 | Line 887 | public class LinkedBlockingQueueTest ext
887      }
888  
889      /**
890 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
890 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
891       */
892      public void testDrainToN() {
893          LinkedBlockingQueue q = new LinkedBlockingQueue();
# Line 911 | Line 896 | public class LinkedBlockingQueueTest ext
896                  assertTrue(q.offer(new Integer(j)));
897              ArrayList l = new ArrayList();
898              q.drainTo(l, i);
899 <            int k = (i < SIZE)? i : SIZE;
899 >            int k = (i < SIZE) ? i : SIZE;
900              assertEquals(l.size(), k);
901              assertEquals(q.size(), SIZE-k);
902              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines