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.40 by jsr166, Mon May 30 22:43:20 2011 UTC vs.
Revision 1.54 by jsr166, Wed Dec 31 20:17:39 2014 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
8 < import java.util.Arrays;
7 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
8 >
9   import java.util.ArrayList;
10 + import java.util.Arrays;
11   import java.util.Collection;
12 + import java.util.Deque;
13   import java.util.Iterator;
14   import java.util.NoSuchElementException;
15 + import java.util.Queue;
16   import java.util.concurrent.BlockingDeque;
17   import java.util.concurrent.BlockingQueue;
18   import java.util.concurrent.CountDownLatch;
19   import java.util.concurrent.Executors;
20   import java.util.concurrent.ExecutorService;
21   import java.util.concurrent.LinkedBlockingDeque;
22 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 < import java.io.*;
22 >
23 > import junit.framework.Test;
24  
25   public class LinkedBlockingDequeTest extends JSR166TestCase {
26  
# Line 29 | Line 32 | public class LinkedBlockingDequeTest ext
32  
33      public static class Bounded extends BlockingQueueTest {
34          protected BlockingQueue emptyCollection() {
35 <            return new LinkedBlockingDeque(20);
35 >            return new LinkedBlockingDeque(SIZE);
36          }
37      }
38  
# Line 44 | Line 47 | public class LinkedBlockingDequeTest ext
47      }
48  
49      /**
50 <     * Create a deque of given size containing consecutive
50 >     * Returns a new deque of given size containing consecutive
51       * Integers 0 ... n.
52       */
53      private LinkedBlockingDeque<Integer> populatedDeque(int n) {
# Line 273 | Line 276 | public class LinkedBlockingDequeTest ext
276       */
277      public void testRemoveFirstOccurrence() {
278          LinkedBlockingDeque q = populatedDeque(SIZE);
279 <        for (int i = 1; i < SIZE; i+=2) {
279 >        for (int i = 1; i < SIZE; i += 2) {
280              assertTrue(q.removeFirstOccurrence(new Integer(i)));
281          }
282 <        for (int i = 0; i < SIZE; i+=2) {
282 >        for (int i = 0; i < SIZE; i += 2) {
283              assertTrue(q.removeFirstOccurrence(new Integer(i)));
284              assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
285          }
# Line 288 | Line 291 | public class LinkedBlockingDequeTest ext
291       */
292      public void testRemoveLastOccurrence() {
293          LinkedBlockingDeque q = populatedDeque(SIZE);
294 <        for (int i = 1; i < SIZE; i+=2) {
294 >        for (int i = 1; i < SIZE; i += 2) {
295              assertTrue(q.removeLastOccurrence(new Integer(i)));
296          }
297 <        for (int i = 0; i < SIZE; i+=2) {
297 >        for (int i = 0; i < SIZE; i += 2) {
298              assertTrue(q.removeLastOccurrence(new Integer(i)));
299              assertFalse(q.removeLastOccurrence(new Integer(i+1)));
300          }
# Line 435 | Line 438 | public class LinkedBlockingDequeTest ext
438          try {
439              LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
440              for (int i = 0; i < SIZE; ++i) {
441 <                Integer I = new Integer(i);
442 <                q.push(I);
443 <                assertEquals(I, q.peek());
441 >                Integer x = new Integer(i);
442 >                q.push(x);
443 >                assertEquals(x, q.peek());
444              }
445              assertEquals(0, q.remainingCapacity());
446              q.push(new Integer(SIZE));
# Line 555 | Line 558 | public class LinkedBlockingDequeTest ext
558      public void testPut() throws InterruptedException {
559          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
560          for (int i = 0; i < SIZE; ++i) {
561 <            Integer I = new Integer(i);
562 <            q.put(I);
563 <            assertTrue(q.contains(I));
561 >            Integer x = new Integer(i);
562 >            q.put(x);
563 >            assertTrue(q.contains(x));
564          }
565          assertEquals(0, q.remainingCapacity());
566      }
# Line 622 | Line 625 | public class LinkedBlockingDequeTest ext
625              }});
626  
627          await(pleaseTake);
628 <        assertEquals(q.remainingCapacity(), 0);
628 >        assertEquals(0, q.remainingCapacity());
629          assertEquals(0, q.take());
630  
631          await(pleaseInterrupt);
632          assertThreadStaysAlive(t);
633          t.interrupt();
634          awaitTermination(t);
635 <        assertEquals(q.remainingCapacity(), 0);
635 >        assertEquals(0, q.remainingCapacity());
636      }
637  
638      /**
# Line 787 | Line 790 | public class LinkedBlockingDequeTest ext
790      public void testPutFirst() throws InterruptedException {
791          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
792          for (int i = 0; i < SIZE; ++i) {
793 <            Integer I = new Integer(i);
794 <            q.putFirst(I);
795 <            assertTrue(q.contains(I));
793 >            Integer x = new Integer(i);
794 >            q.putFirst(x);
795 >            assertTrue(q.contains(x));
796          }
797          assertEquals(0, q.remainingCapacity());
798      }
# Line 854 | Line 857 | public class LinkedBlockingDequeTest ext
857              }});
858  
859          await(pleaseTake);
860 <        assertEquals(q.remainingCapacity(), 0);
860 >        assertEquals(0, q.remainingCapacity());
861          assertEquals(capacity - 1, q.take());
862  
863          await(pleaseInterrupt);
864          assertThreadStaysAlive(t);
865          t.interrupt();
866          awaitTermination(t);
867 <        assertEquals(q.remainingCapacity(), 0);
867 >        assertEquals(0, q.remainingCapacity());
868      }
869  
870      /**
# Line 1064 | Line 1067 | public class LinkedBlockingDequeTest ext
1067  
1068                  pleaseInterrupt.countDown();
1069                  try {
1070 <                    q.pollFirst(SMALL_DELAY_MS, MILLISECONDS);
1070 >                    q.pollFirst(LONG_DELAY_MS, MILLISECONDS);
1071                      shouldThrow();
1072                  } catch (InterruptedException success) {}
1073                  assertFalse(Thread.interrupted());
# Line 1134 | Line 1137 | public class LinkedBlockingDequeTest ext
1137      public void testPutLast() throws InterruptedException {
1138          LinkedBlockingDeque q = new LinkedBlockingDeque(SIZE);
1139          for (int i = 0; i < SIZE; ++i) {
1140 <            Integer I = new Integer(i);
1141 <            q.putLast(I);
1142 <            assertTrue(q.contains(I));
1140 >            Integer x = new Integer(i);
1141 >            q.putLast(x);
1142 >            assertTrue(q.contains(x));
1143          }
1144          assertEquals(0, q.remainingCapacity());
1145      }
# Line 1201 | Line 1204 | public class LinkedBlockingDequeTest ext
1204              }});
1205  
1206          await(pleaseTake);
1207 <        assertEquals(q.remainingCapacity(), 0);
1207 >        assertEquals(0, q.remainingCapacity());
1208          assertEquals(0, q.take());
1209  
1210          await(pleaseInterrupt);
1211          assertThreadStaysAlive(t);
1212          t.interrupt();
1213          awaitTermination(t);
1214 <        assertEquals(q.remainingCapacity(), 0);
1214 >        assertEquals(0, q.remainingCapacity());
1215      }
1216  
1217      /**
# Line 1400 | Line 1403 | public class LinkedBlockingDequeTest ext
1403      }
1404  
1405      /**
1403     * remove(x) removes x and returns true if present
1404     */
1405    public void testRemoveElement() {
1406        LinkedBlockingDeque q = populatedDeque(SIZE);
1407        for (int i = 1; i < SIZE; i+=2) {
1408            assertTrue(q.contains(i));
1409            assertTrue(q.remove(i));
1410            assertFalse(q.contains(i));
1411            assertTrue(q.contains(i-1));
1412        }
1413        for (int i = 0; i < SIZE; i+=2) {
1414            assertTrue(q.contains(i));
1415            assertTrue(q.remove(i));
1416            assertFalse(q.contains(i));
1417            assertFalse(q.remove(i+1));
1418            assertFalse(q.contains(i+1));
1419        }
1420        assertTrue(q.isEmpty());
1421    }
1422
1423    /**
1406       * contains(x) reports true when elements added but not yet removed
1407       */
1408      public void testContains() {
# Line 1491 | Line 1473 | public class LinkedBlockingDequeTest ext
1473              assertTrue(q.removeAll(p));
1474              assertEquals(SIZE-i, q.size());
1475              for (int j = 0; j < i; ++j) {
1476 <                Integer I = (Integer)(p.remove());
1477 <                assertFalse(q.contains(I));
1476 >                Integer x = (Integer)(p.remove());
1477 >                assertFalse(q.contains(x));
1478              }
1479          }
1480      }
# Line 1500 | Line 1482 | public class LinkedBlockingDequeTest ext
1482      /**
1483       * toArray contains all elements in FIFO order
1484       */
1485 <    public void testToArray() throws InterruptedException{
1485 >    public void testToArray() throws InterruptedException {
1486          LinkedBlockingDeque q = populatedDeque(SIZE);
1487          Object[] o = q.toArray();
1488          for (int i = 0; i < o.length; i++)
# Line 1718 | Line 1700 | public class LinkedBlockingDequeTest ext
1700       * A deserialized serialized deque has same elements in same order
1701       */
1702      public void testSerialization() throws Exception {
1703 <        LinkedBlockingDeque q = populatedDeque(SIZE);
1703 >        Queue x = populatedDeque(SIZE);
1704 >        Queue y = serialClone(x);
1705  
1706 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1707 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1708 <        out.writeObject(q);
1709 <        out.close();
1710 <
1711 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1712 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1713 <        LinkedBlockingDeque r = (LinkedBlockingDeque)in.readObject();
1714 <        assertEquals(q.size(), r.size());
1732 <        while (!q.isEmpty())
1733 <            assertEquals(q.remove(), r.remove());
1706 >        assertNotSame(y, x);
1707 >        assertEquals(x.size(), y.size());
1708 >        assertEquals(x.toString(), y.toString());
1709 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
1710 >        while (!x.isEmpty()) {
1711 >            assertFalse(y.isEmpty());
1712 >            assertEquals(x.remove(), y.remove());
1713 >        }
1714 >        assertTrue(y.isEmpty());
1715      }
1716  
1717      /**
# Line 1740 | Line 1721 | public class LinkedBlockingDequeTest ext
1721          LinkedBlockingDeque q = populatedDeque(SIZE);
1722          ArrayList l = new ArrayList();
1723          q.drainTo(l);
1724 <        assertEquals(q.size(), 0);
1725 <        assertEquals(l.size(), SIZE);
1724 >        assertEquals(0, q.size());
1725 >        assertEquals(SIZE, l.size());
1726          for (int i = 0; i < SIZE; ++i)
1727              assertEquals(l.get(i), new Integer(i));
1728          q.add(zero);
# Line 1751 | Line 1732 | public class LinkedBlockingDequeTest ext
1732          assertTrue(q.contains(one));
1733          l.clear();
1734          q.drainTo(l);
1735 <        assertEquals(q.size(), 0);
1736 <        assertEquals(l.size(), 2);
1735 >        assertEquals(0, q.size());
1736 >        assertEquals(2, l.size());
1737          for (int i = 0; i < 2; ++i)
1738              assertEquals(l.get(i), new Integer(i));
1739      }
# Line 1788 | Line 1769 | public class LinkedBlockingDequeTest ext
1769              ArrayList l = new ArrayList();
1770              q.drainTo(l, i);
1771              int k = (i < SIZE) ? i : SIZE;
1772 <            assertEquals(l.size(), k);
1773 <            assertEquals(q.size(), SIZE-k);
1772 >            assertEquals(k, l.size());
1773 >            assertEquals(SIZE-k, q.size());
1774              for (int j = 0; j < k; ++j)
1775                  assertEquals(l.get(j), new Integer(j));
1776 <            while (q.poll() != null) ;
1776 >            do {} while (q.poll() != null);
1777 >        }
1778 >    }
1779 >
1780 >    /**
1781 >     * remove(null), contains(null) always return false
1782 >     */
1783 >    public void testNeverContainsNull() {
1784 >        Deque<?>[] qs = {
1785 >            new LinkedBlockingDeque<Object>(),
1786 >            populatedDeque(2),
1787 >        };
1788 >
1789 >        for (Deque<?> q : qs) {
1790 >            assertFalse(q.contains(null));
1791 >            assertFalse(q.remove(null));
1792 >            assertFalse(q.removeFirstOccurrence(null));
1793 >            assertFalse(q.removeLastOccurrence(null));
1794          }
1795      }
1796  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines