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.27 by jsr166, Wed Aug 25 01:44:48 2010 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());
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 367 | Line 382 | public class LinkedBlockingQueueTest ext
382      }
383  
384      /**
370     * take blocks interruptibly when empty
371     */
372    public void testTakeFromEmpty() throws InterruptedException {
373        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
374        Thread t = new ThreadShouldThrow(InterruptedException.class) {
375            public void realRun() throws InterruptedException {
376                q.take();
377            }};
378
379        t.start();
380        Thread.sleep(SHORT_DELAY_MS);
381        t.interrupt();
382        t.join();
383    }
384
385    /**
385       * Take removes existing elements until empty, then blocks interruptibly
386       */
387      public void testBlockingTake() throws InterruptedException {
# Line 416 | Line 415 | public class LinkedBlockingQueueTest ext
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);
# Line 427 | Line 426 | public class LinkedBlockingQueueTest ext
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);
# Line 461 | Line 460 | public class LinkedBlockingQueueTest ext
460      }
461  
462      /**
464     *  timed poll before a delayed offer fails; after offer succeeds;
465     *  on interruption throws
466     */
467    public void testTimedPollWithOffer() throws InterruptedException {
468        final LinkedBlockingQueue q = new LinkedBlockingQueue(2);
469        Thread t = new Thread(new CheckedRunnable() {
470            public void realRun() throws InterruptedException {
471                assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS));
472                assertSame(zero, q.poll(LONG_DELAY_MS, MILLISECONDS));
473                try {
474                    q.poll(LONG_DELAY_MS, MILLISECONDS);
475                    shouldThrow();
476                } catch (InterruptedException success) {}
477            }});
478
479        t.start();
480        Thread.sleep(SMALL_DELAY_MS);
481        assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS));
482        t.interrupt();
483        t.join();
484    }
485
486    /**
463       * peek returns next element, or null if empty
464       */
465      public void testPeek() {
# Line 532 | 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 632 | 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 <            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 <            Object o[] = q.toArray(new String[10]);
655 >            q.toArray(new String[10]);
656              shouldThrow();
657          } catch (ArrayStoreException success) {}
658      }
# Line 904 | 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 913 | 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