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.24 by jsr166, Tue Dec 1 06:03:49 2009 UTC vs.
Revision 1.37 by jsr166, Fri Nov 5 00:17:22 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 364 | Line 382 | public class LinkedBlockingQueueTest ext
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 413 | 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 424 | 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 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() {
# Line 629 | Line 608 | public class LinkedBlockingQueueTest ext
608      }
609  
610      /**
611 <     * toArray contains all elements
611 >     * toArray contains all elements in FIFO order
612       */
613 <    public void testToArray() throws InterruptedException {
613 >    public void testToArray() {
614          LinkedBlockingQueue q = populatedQueue(SIZE);
615          Object[] o = q.toArray();
616          for (int i = 0; i < o.length; i++)
617 <            assertEquals(o[i], q.take());
617 >            assertSame(o[i], q.poll());
618      }
619  
620      /**
621 <     * toArray(a) contains all elements
621 >     * toArray(a) contains all elements in FIFO order
622       */
623      public void testToArray2() throws InterruptedException {
624 <        LinkedBlockingQueue q = populatedQueue(SIZE);
624 >        LinkedBlockingQueue<Integer> q = populatedQueue(SIZE);
625          Integer[] ints = new Integer[SIZE];
626 <        ints = (Integer[])q.toArray(ints);
626 >        Integer[] array = q.toArray(ints);
627 >        assertSame(ints, array);
628          for (int i = 0; i < ints.length; i++)
629 <            assertEquals(ints[i], q.take());
629 >            assertSame(ints[i], q.poll());
630      }
631  
632      /**
633 <     * toArray(null) throws NPE
633 >     * toArray(null) throws NullPointerException
634       */
635 <    public void testToArray_BadArg() {
635 >    public void testToArray_NullArg() {
636          LinkedBlockingQueue q = populatedQueue(SIZE);
637          try {
638 <            Object o[] = q.toArray(null);
638 >            q.toArray(null);
639              shouldThrow();
640          } catch (NullPointerException success) {}
641      }
642  
643      /**
644 <     * toArray with incompatible array type throws CCE
644 >     * toArray(incompatible array type) throws ArrayStoreException
645       */
646      public void testToArray1_BadArg() {
647          LinkedBlockingQueue q = populatedQueue(SIZE);
648          try {
649 <            Object o[] = q.toArray(new String[10]);
649 >            q.toArray(new String[10]);
650              shouldThrow();
651          } catch (ArrayStoreException success) {}
652      }
# Line 686 | Line 666 | public class LinkedBlockingQueueTest ext
666      /**
667       * iterator.remove removes current element
668       */
669 <    public void testIteratorRemove () {
669 >    public void testIteratorRemove() {
670          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
671          q.add(two);
672          q.add(one);
# Line 697 | Line 677 | public class LinkedBlockingQueueTest ext
677          it.remove();
678  
679          it = q.iterator();
680 <        assertEquals(it.next(), one);
681 <        assertEquals(it.next(), three);
680 >        assertSame(it.next(), one);
681 >        assertSame(it.next(), three);
682          assertFalse(it.hasNext());
683      }
684  
# Line 722 | Line 702 | public class LinkedBlockingQueueTest ext
702      /**
703       * Modifications do not cause iterators to fail
704       */
705 <    public void testWeaklyConsistentIteration () {
705 >    public void testWeaklyConsistentIteration() {
706          final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
707          q.add(one);
708          q.add(two);
# Line 901 | Line 881 | public class LinkedBlockingQueueTest ext
881      }
882  
883      /**
884 <     * drainTo(c, n) empties first max {n, size} elements of queue into c
884 >     * drainTo(c, n) empties first min(n, size) elements of queue into c
885       */
886      public void testDrainToN() {
887          LinkedBlockingQueue q = new LinkedBlockingQueue();
# Line 910 | Line 890 | public class LinkedBlockingQueueTest ext
890                  assertTrue(q.offer(new Integer(j)));
891              ArrayList l = new ArrayList();
892              q.drainTo(l, i);
893 <            int k = (i < SIZE)? i : SIZE;
893 >            int k = (i < SIZE) ? i : SIZE;
894              assertEquals(l.size(), k);
895              assertEquals(q.size(), SIZE-k);
896              for (int j = 0; j < k; ++j)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines