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

Comparing jsr166/src/test/tck/LinkedBlockingDequeTest.java (file contents):
Revision 1.39 by jsr166, Fri May 27 20:07:24 2011 UTC vs.
Revision 1.49 by jsr166, Thu May 30 03:28:55 2013 UTC

# Line 5 | Line 5
5   */
6  
7   import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
8 > import java.util.Arrays;
9 > import java.util.ArrayList;
10 > import java.util.Collection;
11 > import java.util.Iterator;
12 > import java.util.NoSuchElementException;
13 > import java.util.Queue;
14 > import java.util.concurrent.BlockingDeque;
15 > import java.util.concurrent.BlockingQueue;
16 > import java.util.concurrent.CountDownLatch;
17 > import java.util.concurrent.Executors;
18 > import java.util.concurrent.ExecutorService;
19 > import java.util.concurrent.LinkedBlockingDeque;
20   import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import java.io.*;
21  
22   public class LinkedBlockingDequeTest extends JSR166TestCase {
23  
# Line 20 | Line 29 | public class LinkedBlockingDequeTest ext
29  
30      public static class Bounded extends BlockingQueueTest {
31          protected BlockingQueue emptyCollection() {
32 <            return new LinkedBlockingDeque(20);
32 >            return new LinkedBlockingDeque(SIZE);
33          }
34      }
35  
# Line 35 | Line 44 | public class LinkedBlockingDequeTest ext
44      }
45  
46      /**
47 <     * Create a deque of given size containing consecutive
47 >     * Returns a new deque of given size containing consecutive
48       * Integers 0 ... n.
49       */
50      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
# Line 80 | Line 89 | public class LinkedBlockingDequeTest ext
89      }
90  
91      /**
92 <     * offer(null) throws NPE
92 >     * offerFirst(null) throws NullPointerException
93       */
94      public void testOfferFirstNull() {
95 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
96          try {
87            LinkedBlockingDeque q = new LinkedBlockingDeque();
97              q.offerFirst(null);
98              shouldThrow();
99          } catch (NullPointerException success) {}
100      }
101  
102      /**
103 +     * offerLast(null) throws NullPointerException
104 +     */
105 +    public void testOfferLastNull() {
106 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
107 +        try {
108 +            q.offerLast(null);
109 +            shouldThrow();
110 +        } catch (NullPointerException success) {}
111 +    }
112 +
113 +    /**
114       * OfferFirst succeeds
115       */
116      public void testOfferFirst() {
# Line 308 | Line 328 | public class LinkedBlockingDequeTest ext
328      }
329  
330      /**
331 <     * Constructor throws IAE if capacity argument nonpositive
331 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
332       */
333      public void testConstructor2() {
334          try {
335 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
335 >            new LinkedBlockingDeque(0);
336              shouldThrow();
337          } catch (IllegalArgumentException success) {}
338      }
339  
340      /**
341 <     * Initializing from null Collection throws NPE
341 >     * Initializing from null Collection throws NullPointerException
342       */
343      public void testConstructor3() {
344          try {
345 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
345 >            new LinkedBlockingDeque(null);
346              shouldThrow();
347          } catch (NullPointerException success) {}
348      }
349  
350      /**
351 <     * Initializing from Collection of null elements throws NPE
351 >     * Initializing from Collection of null elements throws NullPointerException
352       */
353      public void testConstructor4() {
354 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
355          try {
356 <            Integer[] ints = new Integer[SIZE];
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
356 >            new LinkedBlockingDeque(elements);
357              shouldThrow();
358          } catch (NullPointerException success) {}
359      }
360  
361      /**
362 <     * Initializing from Collection with some null elements throws NPE
362 >     * Initializing from Collection with some null elements throws
363 >     * NullPointerException
364       */
365      public void testConstructor5() {
366 +        Integer[] ints = new Integer[SIZE];
367 +        for (int i = 0; i < SIZE-1; ++i)
368 +            ints[i] = i;
369 +        Collection<Integer> elements = Arrays.asList(ints);
370          try {
371 <            Integer[] ints = new Integer[SIZE];
347 <            for (int i = 0; i < SIZE-1; ++i)
348 <                ints[i] = new Integer(i);
349 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
371 >            new LinkedBlockingDeque(elements);
372              shouldThrow();
373          } catch (NullPointerException success) {}
374      }
# Line 357 | Line 379 | public class LinkedBlockingDequeTest ext
379      public void testConstructor6() {
380          Integer[] ints = new Integer[SIZE];
381          for (int i = 0; i < SIZE; ++i)
382 <            ints[i] = new Integer(i);
382 >            ints[i] = i;
383          LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
384          for (int i = 0; i < SIZE; ++i)
385              assertEquals(ints[i], q.poll());
# Line 396 | Line 418 | public class LinkedBlockingDequeTest ext
418      }
419  
420      /**
399     * offer(null) throws NPE
400     */
401    public void testOfferNull() {
402        try {
403            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
404            q.offer(null);
405            shouldThrow();
406        } catch (NullPointerException success) {}
407    }
408
409    /**
410     * add(null) throws NPE
411     */
412    public void testAddNull() {
413        try {
414            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
415            q.add(null);
416            shouldThrow();
417        } catch (NullPointerException success) {}
418    }
419
420    /**
421       * push(null) throws NPE
422       */
423      public void testPushNull() {
# Line 482 | Line 482 | public class LinkedBlockingDequeTest ext
482       * add succeeds if not full; throws ISE if full
483       */
484      public void testAdd() {
485 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
486 +        for (int i = 0; i < SIZE; ++i)
487 +            assertTrue(q.add(new Integer(i)));
488 +        assertEquals(0, q.remainingCapacity());
489          try {
486            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487            for (int i = 0; i < SIZE; ++i) {
488                assertTrue(q.add(new Integer(i)));
489            }
490            assertEquals(0, q.remainingCapacity());
490              q.add(new Integer(SIZE));
491              shouldThrow();
492          } catch (IllegalStateException success) {}
493      }
494  
495      /**
497     * addAll(null) throws NPE
498     */
499    public void testAddAll1() {
500        try {
501            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
502            q.addAll(null);
503            shouldThrow();
504        } catch (NullPointerException success) {}
505    }
506
507    /**
496       * addAll(this) throws IAE
497       */
498      public void testAddAllSelf() {
499 +        LinkedBlockingDeque q = populatedDeque(SIZE);
500          try {
512            LinkedBlockingDeque q = populatedDeque(SIZE);
501              q.addAll(q);
502              shouldThrow();
503          } catch (IllegalArgumentException success) {}
504      }
505  
506      /**
519     * addAll of a collection with null elements throws NPE
520     */
521    public void testAddAll2() {
522        try {
523            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
524            Integer[] ints = new Integer[SIZE];
525            q.addAll(Arrays.asList(ints));
526            shouldThrow();
527        } catch (NullPointerException success) {}
528    }
529
530    /**
507       * addAll of a collection with any null elements throws NPE after
508       * possibly adding some elements
509       */
510      public void testAddAll3() {
511 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
512 +        Integer[] ints = new Integer[SIZE];
513 +        for (int i = 0; i < SIZE-1; ++i)
514 +            ints[i] = new Integer(i);
515 +        Collection<Integer> elements = Arrays.asList(ints);
516          try {
517 <            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
537 <            Integer[] ints = new Integer[SIZE];
538 <            for (int i = 0; i < SIZE-1; ++i)
539 <                ints[i] = new Integer(i);
540 <            q.addAll(Arrays.asList(ints));
517 >            q.addAll(elements);
518              shouldThrow();
519          } catch (NullPointerException success) {}
520      }
521  
522      /**
523 <     * addAll throws ISE if not enough room
523 >     * addAll throws IllegalStateException if not enough room
524       */
525      public void testAddAll4() {
526 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
527 +        Integer[] ints = new Integer[SIZE];
528 +        for (int i = 0; i < SIZE; ++i)
529 +            ints[i] = new Integer(i);
530 +        Collection<Integer> elements = Arrays.asList(ints);
531          try {
532 <            LinkedBlockingDeque q = new LinkedBlockingDeque(1);
551 <            Integer[] ints = new Integer[SIZE];
552 <            for (int i = 0; i < SIZE; ++i)
553 <                ints[i] = new Integer(i);
554 <            q.addAll(Arrays.asList(ints));
532 >            q.addAll(elements);
533              shouldThrow();
534          } catch (IllegalStateException success) {}
535      }
# Line 572 | Line 550 | public class LinkedBlockingDequeTest ext
550      }
551  
552      /**
575     * put(null) throws NPE
576     */
577    public void testPutNull() throws InterruptedException {
578        try {
579            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
580            q.put(null);
581            shouldThrow();
582        } catch (NullPointerException success) {}
583    }
584
585    /**
553       * all elements successfully put are contained
554       */
555      public void testPut() throws InterruptedException {
# Line 655 | Line 622 | public class LinkedBlockingDequeTest ext
622              }});
623  
624          await(pleaseTake);
625 <        assertEquals(q.remainingCapacity(), 0);
625 >        assertEquals(0, q.remainingCapacity());
626          assertEquals(0, q.take());
627  
628          await(pleaseInterrupt);
629          assertThreadStaysAlive(t);
630          t.interrupt();
631          awaitTermination(t);
632 <        assertEquals(q.remainingCapacity(), 0);
632 >        assertEquals(0, q.remainingCapacity());
633      }
634  
635      /**
# Line 807 | Line 774 | public class LinkedBlockingDequeTest ext
774       * putFirst(null) throws NPE
775       */
776      public void testPutFirstNull() throws InterruptedException {
777 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
778          try {
811            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
779              q.putFirst(null);
780              shouldThrow();
781          } catch (NullPointerException success) {}
# Line 887 | Line 854 | public class LinkedBlockingDequeTest ext
854              }});
855  
856          await(pleaseTake);
857 <        assertEquals(q.remainingCapacity(), 0);
857 >        assertEquals(0, q.remainingCapacity());
858          assertEquals(capacity - 1, q.take());
859  
860          await(pleaseInterrupt);
861          assertThreadStaysAlive(t);
862          t.interrupt();
863          awaitTermination(t);
864 <        assertEquals(q.remainingCapacity(), 0);
864 >        assertEquals(0, q.remainingCapacity());
865      }
866  
867      /**
# Line 1097 | Line 1064 | public class LinkedBlockingDequeTest ext
1064  
1065                  pleaseInterrupt.countDown();
1066                  try {
1067 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1067 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1068                      shouldThrow();
1069                  } catch (InterruptedException success) {}
1070                  assertFalse(Thread.interrupted());
# Line 1154 | Line 1121 | public class LinkedBlockingDequeTest ext
1121       * putLast(null) throws NPE
1122       */
1123      public void testPutLastNull() throws InterruptedException {
1124 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1125          try {
1158            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1126              q.putLast(null);
1127              shouldThrow();
1128          } catch (NullPointerException success) {}
# Line 1234 | Line 1201 | public class LinkedBlockingDequeTest ext
1201              }});
1202  
1203          await(pleaseTake);
1204 <        assertEquals(q.remainingCapacity(), 0);
1204 >        assertEquals(0, q.remainingCapacity());
1205          assertEquals(0, q.take());
1206  
1207          await(pleaseInterrupt);
1208          assertThreadStaysAlive(t);
1209          t.interrupt();
1210          awaitTermination(t);
1211 <        assertEquals(q.remainingCapacity(), 0);
1211 >        assertEquals(0, q.remainingCapacity());
1212      }
1213  
1214      /**
# Line 1433 | Line 1400 | public class LinkedBlockingDequeTest ext
1400      }
1401  
1402      /**
1436     * remove(x) removes x and returns true if present
1437     */
1438    public void testRemoveElement() {
1439        LinkedBlockingDeque q = populatedDeque(SIZE);
1440        for (int i = 1; i < SIZE; i+=2) {
1441            assertTrue(q.contains(i));
1442            assertTrue(q.remove(i));
1443            assertFalse(q.contains(i));
1444            assertTrue(q.contains(i-1));
1445        }
1446        for (int i = 0; i < SIZE; i+=2) {
1447            assertTrue(q.contains(i));
1448            assertTrue(q.remove(i));
1449            assertFalse(q.contains(i));
1450            assertFalse(q.remove(i+1));
1451            assertFalse(q.contains(i+1));
1452        }
1453        assertTrue(q.isEmpty());
1454    }
1455
1456    /**
1403       * contains(x) reports true when elements added but not yet removed
1404       */
1405      public void testContains() {
# Line 1533 | Line 1479 | public class LinkedBlockingDequeTest ext
1479      /**
1480       * toArray contains all elements in FIFO order
1481       */
1482 <    public void testToArray() throws InterruptedException{
1482 >    public void testToArray() throws InterruptedException {
1483          LinkedBlockingDeque q = populatedDeque(SIZE);
1484          Object[] o = q.toArray();
1485          for (int i = 0; i < o.length; i++)
# Line 1553 | Line 1499 | public class LinkedBlockingDequeTest ext
1499      }
1500  
1501      /**
1556     * toArray(null) throws NullPointerException
1557     */
1558    public void testToArray_NullArg() {
1559        LinkedBlockingDeque q = populatedDeque(SIZE);
1560        try {
1561            q.toArray(null);
1562            shouldThrow();
1563        } catch (NullPointerException success) {}
1564    }
1565
1566    /**
1502       * toArray(incompatible array type) throws ArrayStoreException
1503       */
1504      public void testToArray1_BadArg() {
# Line 1762 | Line 1697 | public class LinkedBlockingDequeTest ext
1697       * A deserialized serialized deque has same elements in same order
1698       */
1699      public void testSerialization() throws Exception {
1700 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1701 <
1767 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1768 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1769 <        out.writeObject(q);
1770 <        out.close();
1771 <
1772 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1773 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1774 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1775 <        assertEquals(q.size(), r.size());
1776 <        while (!q.isEmpty())
1777 <            assertEquals(q.remove(), r.remove());
1778 <    }
1700 >        Queue x = populatedDeque(SIZE);
1701 >        Queue y = serialClone(x);
1702  
1703 <    /**
1704 <     * drainTo(null) throws NPE
1705 <     */
1706 <    public void testDrainToNull() {
1707 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1708 <        try {
1709 <            q.drainTo(null);
1710 <            shouldThrow();
1711 <        } catch (NullPointerException success) {}
1789 <    }
1790 <
1791 <    /**
1792 <     * drainTo(this) throws IAE
1793 <     */
1794 <    public void testDrainToSelf() {
1795 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1796 <        try {
1797 <            q.drainTo(q);
1798 <            shouldThrow();
1799 <        } catch (IllegalArgumentException success) {}
1703 >        assertNotSame(y, x);
1704 >        assertEquals(x.size(), y.size());
1705 >        assertEquals(x.toString(), y.toString());
1706 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1707 >        while (!x.isEmpty()) {
1708 >            assertFalse(y.isEmpty());
1709 >            assertEquals(x.remove(), y.remove());
1710 >        }
1711 >        assertTrue(y.isEmpty());
1712      }
1713  
1714      /**
# Line 1806 | Line 1718 | public class LinkedBlockingDequeTest ext
1718          LinkedBlockingDeque q = populatedDeque(SIZE);
1719          ArrayList l = new ArrayList();
1720          q.drainTo(l);
1721 <        assertEquals(q.size(), 0);
1722 <        assertEquals(l.size(), SIZE);
1721 >        assertEquals(0, q.size());
1722 >        assertEquals(SIZE, l.size());
1723          for (int i = 0; i < SIZE; ++i)
1724              assertEquals(l.get(i), new Integer(i));
1725          q.add(zero);
# Line 1817 | Line 1729 | public class LinkedBlockingDequeTest ext
1729          assertTrue(q.contains(one));
1730          l.clear();
1731          q.drainTo(l);
1732 <        assertEquals(q.size(), 0);
1733 <        assertEquals(l.size(), 2);
1732 >        assertEquals(0, q.size());
1733 >        assertEquals(2, l.size());
1734          for (int i = 0; i < 2; ++i)
1735              assertEquals(l.get(i), new Integer(i));
1736      }
# Line 1844 | Line 1756 | public class LinkedBlockingDequeTest ext
1756      }
1757  
1758      /**
1847     * drainTo(null, n) throws NPE
1848     */
1849    public void testDrainToNullN() {
1850        LinkedBlockingDeque q = populatedDeque(SIZE);
1851        try {
1852            q.drainTo(null, 0);
1853            shouldThrow();
1854        } catch (NullPointerException success) {}
1855    }
1856
1857    /**
1858     * drainTo(this, n) throws IAE
1859     */
1860    public void testDrainToSelfN() {
1861        LinkedBlockingDeque q = populatedDeque(SIZE);
1862        try {
1863            q.drainTo(q, 0);
1864            shouldThrow();
1865        } catch (IllegalArgumentException success) {}
1866    }
1867
1868    /**
1759       * drainTo(c, n) empties first min(n, size) elements of queue into c
1760       */
1761      public void testDrainToN() {
# Line 1876 | Line 1766 | public class LinkedBlockingDequeTest ext
1766              ArrayList l = new ArrayList();
1767              q.drainTo(l, i);
1768              int k = (i < SIZE) ? i : SIZE;
1769 <            assertEquals(l.size(), k);
1770 <            assertEquals(q.size(), SIZE-k);
1769 >            assertEquals(k, l.size());
1770 >            assertEquals(SIZE-k, q.size());
1771              for (int j = 0; j < k; ++j)
1772                  assertEquals(l.get(j), new Integer(j));
1773              while (q.poll() != null) ;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines