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.50 by jsr166, Sun Nov 23 22:27:06 2014 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.Deque;
12 > import java.util.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.Queue;
15 > import java.util.concurrent.BlockingDeque;
16 > import java.util.concurrent.BlockingQueue;
17 > import java.util.concurrent.CountDownLatch;
18 > import java.util.concurrent.Executors;
19 > import java.util.concurrent.ExecutorService;
20 > import java.util.concurrent.LinkedBlockingDeque;
21   import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 import java.io.*;
22  
23   public class LinkedBlockingDequeTest extends JSR166TestCase {
24  
# Line 20 | Line 30 | public class LinkedBlockingDequeTest ext
30  
31      public static class Bounded extends BlockingQueueTest {
32          protected BlockingQueue emptyCollection() {
33 <            return new LinkedBlockingDeque(20);
33 >            return new LinkedBlockingDeque(SIZE);
34          }
35      }
36  
# Line 35 | Line 45 | public class LinkedBlockingDequeTest ext
45      }
46  
47      /**
48 <     * Create a deque of given size containing consecutive
48 >     * Returns a new deque of given size containing consecutive
49       * Integers 0 ... n.
50       */
51      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
# Line 80 | Line 90 | public class LinkedBlockingDequeTest ext
90      }
91  
92      /**
93 <     * offer(null) throws NPE
93 >     * offerFirst(null) throws NullPointerException
94       */
95      public void testOfferFirstNull() {
96 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
97          try {
87            LinkedBlockingDeque q = new LinkedBlockingDeque();
98              q.offerFirst(null);
99              shouldThrow();
100          } catch (NullPointerException success) {}
101      }
102  
103      /**
104 +     * offerLast(null) throws NullPointerException
105 +     */
106 +    public void testOfferLastNull() {
107 +        LinkedBlockingDeque q = new LinkedBlockingDeque();
108 +        try {
109 +            q.offerLast(null);
110 +            shouldThrow();
111 +        } catch (NullPointerException success) {}
112 +    }
113 +
114 +    /**
115       * OfferFirst succeeds
116       */
117      public void testOfferFirst() {
# Line 308 | Line 329 | public class LinkedBlockingDequeTest ext
329      }
330  
331      /**
332 <     * Constructor throws IAE if capacity argument nonpositive
332 >     * Constructor throws IllegalArgumentException if capacity argument nonpositive
333       */
334      public void testConstructor2() {
335          try {
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(0);
336 >            new LinkedBlockingDeque(0);
337              shouldThrow();
338          } catch (IllegalArgumentException success) {}
339      }
340  
341      /**
342 <     * Initializing from null Collection throws NPE
342 >     * Initializing from null Collection throws NullPointerException
343       */
344      public void testConstructor3() {
345          try {
346 <            LinkedBlockingDeque q = new LinkedBlockingDeque(null);
346 >            new LinkedBlockingDeque(null);
347              shouldThrow();
348          } catch (NullPointerException success) {}
349      }
350  
351      /**
352 <     * Initializing from Collection of null elements throws NPE
352 >     * Initializing from Collection of null elements throws NullPointerException
353       */
354      public void testConstructor4() {
355 +        Collection<Integer> elements = Arrays.asList(new Integer[SIZE]);
356          try {
357 <            Integer[] ints = new Integer[SIZE];
336 <            LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
357 >            new LinkedBlockingDeque(elements);
358              shouldThrow();
359          } catch (NullPointerException success) {}
360      }
361  
362      /**
363 <     * Initializing from Collection with some null elements throws NPE
363 >     * Initializing from Collection with some null elements throws
364 >     * NullPointerException
365       */
366      public void testConstructor5() {
367 +        Integer[] ints = new Integer[SIZE];
368 +        for (int i = 0; i < SIZE-1; ++i)
369 +            ints[i] = i;
370 +        Collection<Integer> elements = Arrays.asList(ints);
371          try {
372 <            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));
372 >            new LinkedBlockingDeque(elements);
373              shouldThrow();
374          } catch (NullPointerException success) {}
375      }
# Line 357 | Line 380 | public class LinkedBlockingDequeTest ext
380      public void testConstructor6() {
381          Integer[] ints = new Integer[SIZE];
382          for (int i = 0; i < SIZE; ++i)
383 <            ints[i] = new Integer(i);
383 >            ints[i] = i;
384          LinkedBlockingDeque q = new LinkedBlockingDeque(Arrays.asList(ints));
385          for (int i = 0; i < SIZE; ++i)
386              assertEquals(ints[i], q.poll());
# Line 396 | Line 419 | public class LinkedBlockingDequeTest ext
419      }
420  
421      /**
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    /**
422       * push(null) throws NPE
423       */
424      public void testPushNull() {
# Line 482 | Line 483 | public class LinkedBlockingDequeTest ext
483       * add succeeds if not full; throws ISE if full
484       */
485      public void testAdd() {
486 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
487 +        for (int i = 0; i < SIZE; ++i)
488 +            assertTrue(q.add(new Integer(i)));
489 +        assertEquals(0, q.remainingCapacity());
490          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());
491              q.add(new Integer(SIZE));
492              shouldThrow();
493          } catch (IllegalStateException success) {}
494      }
495  
496      /**
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    /**
497       * addAll(this) throws IAE
498       */
499      public void testAddAllSelf() {
500 +        LinkedBlockingDeque q = populatedDeque(SIZE);
501          try {
512            LinkedBlockingDeque q = populatedDeque(SIZE);
502              q.addAll(q);
503              shouldThrow();
504          } catch (IllegalArgumentException success) {}
505      }
506  
507      /**
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    /**
508       * addAll of a collection with any null elements throws NPE after
509       * possibly adding some elements
510       */
511      public void testAddAll3() {
512 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
513 +        Integer[] ints = new Integer[SIZE];
514 +        for (int i = 0; i < SIZE-1; ++i)
515 +            ints[i] = new Integer(i);
516 +        Collection<Integer> elements = Arrays.asList(ints);
517          try {
518 <            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));
518 >            q.addAll(elements);
519              shouldThrow();
520          } catch (NullPointerException success) {}
521      }
522  
523      /**
524 <     * addAll throws ISE if not enough room
524 >     * addAll throws IllegalStateException if not enough room
525       */
526      public void testAddAll4() {
527 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE - 1);
528 +        Integer[] ints = new Integer[SIZE];
529 +        for (int i = 0; i < SIZE; ++i)
530 +            ints[i] = new Integer(i);
531 +        Collection<Integer> elements = Arrays.asList(ints);
532          try {
533 <            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));
533 >            q.addAll(elements);
534              shouldThrow();
535          } catch (IllegalStateException success) {}
536      }
# Line 572 | Line 551 | public class LinkedBlockingDequeTest ext
551      }
552  
553      /**
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    /**
554       * all elements successfully put are contained
555       */
556      public void testPut() throws InterruptedException {
# Line 655 | Line 623 | public class LinkedBlockingDequeTest ext
623              }});
624  
625          await(pleaseTake);
626 <        assertEquals(q.remainingCapacity(), 0);
626 >        assertEquals(0, q.remainingCapacity());
627          assertEquals(0, q.take());
628  
629          await(pleaseInterrupt);
630          assertThreadStaysAlive(t);
631          t.interrupt();
632          awaitTermination(t);
633 <        assertEquals(q.remainingCapacity(), 0);
633 >        assertEquals(0, q.remainingCapacity());
634      }
635  
636      /**
# Line 807 | Line 775 | public class LinkedBlockingDequeTest ext
775       * putFirst(null) throws NPE
776       */
777      public void testPutFirstNull() throws InterruptedException {
778 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
779          try {
811            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
780              q.putFirst(null);
781              shouldThrow();
782          } catch (NullPointerException success) {}
# Line 887 | Line 855 | public class LinkedBlockingDequeTest ext
855              }});
856  
857          await(pleaseTake);
858 <        assertEquals(q.remainingCapacity(), 0);
858 >        assertEquals(0, q.remainingCapacity());
859          assertEquals(capacity - 1, q.take());
860  
861          await(pleaseInterrupt);
862          assertThreadStaysAlive(t);
863          t.interrupt();
864          awaitTermination(t);
865 <        assertEquals(q.remainingCapacity(), 0);
865 >        assertEquals(0, q.remainingCapacity());
866      }
867  
868      /**
# Line 1097 | Line 1065 | public class LinkedBlockingDequeTest ext
1065  
1066                  pleaseInterrupt.countDown();
1067                  try {
1068 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1068 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1069                      shouldThrow();
1070                  } catch (InterruptedException success) {}
1071                  assertFalse(Thread.interrupted());
# Line 1154 | Line 1122 | public class LinkedBlockingDequeTest ext
1122       * putLast(null) throws NPE
1123       */
1124      public void testPutLastNull() throws InterruptedException {
1125 +        LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1126          try {
1158            LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1127              q.putLast(null);
1128              shouldThrow();
1129          } catch (NullPointerException success) {}
# Line 1234 | Line 1202 | public class LinkedBlockingDequeTest ext
1202              }});
1203  
1204          await(pleaseTake);
1205 <        assertEquals(q.remainingCapacity(), 0);
1205 >        assertEquals(0, q.remainingCapacity());
1206          assertEquals(0, q.take());
1207  
1208          await(pleaseInterrupt);
1209          assertThreadStaysAlive(t);
1210          t.interrupt();
1211          awaitTermination(t);
1212 <        assertEquals(q.remainingCapacity(), 0);
1212 >        assertEquals(0, q.remainingCapacity());
1213      }
1214  
1215      /**
# Line 1433 | Line 1401 | public class LinkedBlockingDequeTest ext
1401      }
1402  
1403      /**
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    /**
1404       * contains(x) reports true when elements added but not yet removed
1405       */
1406      public void testContains() {
# Line 1533 | Line 1480 | public class LinkedBlockingDequeTest ext
1480      /**
1481       * toArray contains all elements in FIFO order
1482       */
1483 <    public void testToArray() throws InterruptedException{
1483 >    public void testToArray() throws InterruptedException {
1484          LinkedBlockingDeque q = populatedDeque(SIZE);
1485          Object[] o = q.toArray();
1486          for (int i = 0; i < o.length; i++)
# Line 1553 | Line 1500 | public class LinkedBlockingDequeTest ext
1500      }
1501  
1502      /**
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    /**
1503       * toArray(incompatible array type) throws ArrayStoreException
1504       */
1505      public void testToArray1_BadArg() {
# Line 1762 | Line 1698 | public class LinkedBlockingDequeTest ext
1698       * A deserialized serialized deque has same elements in same order
1699       */
1700      public void testSerialization() throws Exception {
1701 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1702 <
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 <    }
1779 <
1780 <    /**
1781 <     * drainTo(null) throws NPE
1782 <     */
1783 <    public void testDrainToNull() {
1784 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1785 <        try {
1786 <            q.drainTo(null);
1787 <            shouldThrow();
1788 <        } catch (NullPointerException success) {}
1789 <    }
1701 >        Queue x = populatedDeque(SIZE);
1702 >        Queue y = serialClone(x);
1703  
1704 <    /**
1705 <     * drainTo(this) throws IAE
1706 <     */
1707 <    public void testDrainToSelf() {
1708 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1709 <        try {
1710 <            q.drainTo(q);
1711 <            shouldThrow();
1712 <        } catch (IllegalArgumentException success) {}
1704 >        assertNotSame(y, x);
1705 >        assertEquals(x.size(), y.size());
1706 >        assertEquals(x.toString(), y.toString());
1707 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1708 >        while (!x.isEmpty()) {
1709 >            assertFalse(y.isEmpty());
1710 >            assertEquals(x.remove(), y.remove());
1711 >        }
1712 >        assertTrue(y.isEmpty());
1713      }
1714  
1715      /**
# Line 1806 | Line 1719 | public class LinkedBlockingDequeTest ext
1719          LinkedBlockingDeque q = populatedDeque(SIZE);
1720          ArrayList l = new ArrayList();
1721          q.drainTo(l);
1722 <        assertEquals(q.size(), 0);
1723 <        assertEquals(l.size(), SIZE);
1722 >        assertEquals(0, q.size());
1723 >        assertEquals(SIZE, l.size());
1724          for (int i = 0; i < SIZE; ++i)
1725              assertEquals(l.get(i), new Integer(i));
1726          q.add(zero);
# Line 1817 | Line 1730 | public class LinkedBlockingDequeTest ext
1730          assertTrue(q.contains(one));
1731          l.clear();
1732          q.drainTo(l);
1733 <        assertEquals(q.size(), 0);
1734 <        assertEquals(l.size(), 2);
1733 >        assertEquals(0, q.size());
1734 >        assertEquals(2, l.size());
1735          for (int i = 0; i < 2; ++i)
1736              assertEquals(l.get(i), new Integer(i));
1737      }
# Line 1844 | Line 1757 | public class LinkedBlockingDequeTest ext
1757      }
1758  
1759      /**
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    /**
1760       * drainTo(c, n) empties first min(n, size) elements of queue into c
1761       */
1762      public void testDrainToN() {
# Line 1876 | Line 1767 | public class LinkedBlockingDequeTest ext
1767              ArrayList l = new ArrayList();
1768              q.drainTo(l, i);
1769              int k = (i < SIZE) ? i : SIZE;
1770 <            assertEquals(l.size(), k);
1771 <            assertEquals(q.size(), SIZE-k);
1770 >            assertEquals(k, l.size());
1771 >            assertEquals(SIZE-k, q.size());
1772              for (int j = 0; j < k; ++j)
1773                  assertEquals(l.get(j), new Integer(j));
1774              while (q.poll() != null) ;
1775          }
1776      }
1777  
1778 +    /**
1779 +     * remove(null), contains(null) always return false
1780 +     */
1781 +    public void testNeverContainsNull() {
1782 +        Deque<?>[] qs = {
1783 +            new LinkedBlockingDeque<Object>(),
1784 +            populatedDeque(2),
1785 +        };
1786 +
1787 +        for (Deque<?> q : qs) {
1788 +            assertFalse(q.contains(null));
1789 +            assertFalse(q.remove(null));
1790 +            assertFalse(q.removeFirstOccurrence(null));
1791 +            assertFalse(q.removeLastOccurrence(null));
1792 +        }
1793 +    }
1794 +
1795   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines