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

Comparing jsr166/src/test/tck/ConcurrentLinkedDequeTest.java (file contents):
Revision 1.2 by jsr166, Wed Nov 3 07:54:52 2010 UTC vs.
Revision 1.14 by jsr166, Wed Dec 31 19:05:42 2014 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
9 > import java.util.Arrays;
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.Random;
16 > import java.util.concurrent.ConcurrentLinkedDeque;
17 >
18 > import junit.framework.Test;
19 > import junit.framework.TestSuite;
20  
21   public class ConcurrentLinkedDequeTest extends JSR166TestCase {
22  
# Line 22 | Line 29 | public class ConcurrentLinkedDequeTest e
29      }
30  
31      /**
32 <     * Create a deque of given size containing consecutive
32 >     * Returns a new deque of given size containing consecutive
33       * Integers 0 ... n.
34       */
35 <    private ConcurrentLinkedDeque populatedDeque(int n) {
36 <        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
35 >    private ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
36 >        ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>();
37          assertTrue(q.isEmpty());
38          for (int i = 0; i < n; ++i)
39              assertTrue(q.offer(new Integer(i)));
# Line 431 | Line 438 | public class ConcurrentLinkedDequeTest e
438      public void testRemoveElement() {
439          ConcurrentLinkedDeque q = populatedDeque(SIZE);
440          for (int i = 1; i < SIZE; i+=2) {
441 <            assertTrue(q.remove(new Integer(i)));
441 >            assertTrue(q.contains(i));
442 >            assertTrue(q.remove(i));
443 >            assertFalse(q.contains(i));
444 >            assertTrue(q.contains(i-1));
445          }
446          for (int i = 0; i < SIZE; i+=2) {
447 <            assertTrue(q.remove(new Integer(i)));
448 <            assertFalse(q.remove(new Integer(i+1)));
447 >            assertTrue(q.contains(i));
448 >            assertTrue(q.remove(i));
449 >            assertFalse(q.contains(i));
450 >            assertFalse(q.remove(i+1));
451 >            assertFalse(q.contains(i+1));
452          }
453          assertTrue(q.isEmpty());
454      }
# Line 635 | Line 648 | public class ConcurrentLinkedDequeTest e
648      }
649  
650      /**
651 <     * toArray() contains all elements
651 >     * toArray() contains all elements in FIFO order
652       */
653      public void testToArray() {
654          ConcurrentLinkedDeque q = populatedDeque(SIZE);
655          Object[] o = q.toArray();
643        Arrays.sort(o);
656          for (int i = 0; i < o.length; i++)
657 <            assertEquals(o[i], q.poll());
657 >            assertSame(o[i], q.poll());
658      }
659  
660      /**
661 <     * toArray(a) contains all elements
661 >     * toArray(a) contains all elements in FIFO order
662       */
663      public void testToArray2() {
664 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
664 >        ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE);
665          Integer[] ints = new Integer[SIZE];
666 <        ints = (Integer[])q.toArray(ints);
667 <        Arrays.sort(ints);
666 >        Integer[] array = q.toArray(ints);
667 >        assertSame(ints, array);
668          for (int i = 0; i < ints.length; i++)
669 <            assertEquals(ints[i], q.poll());
669 >            assertSame(ints[i], q.poll());
670      }
671  
672      /**
673 <     * toArray(null) throws NPE
673 >     * toArray(null) throws NullPointerException
674       */
675 <    public void testToArray_BadArg() {
675 >    public void testToArray_NullArg() {
676          ConcurrentLinkedDeque q = populatedDeque(SIZE);
677          try {
678 <            Object o[] = q.toArray(null);
678 >            q.toArray(null);
679              shouldThrow();
680          } catch (NullPointerException success) {}
681      }
# Line 830 | Line 842 | public class ConcurrentLinkedDequeTest e
842          ConcurrentLinkedDeque q = populatedDeque(SIZE);
843          String s = q.toString();
844          for (int i = 0; i < SIZE; ++i) {
845 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
845 >            assertTrue(s.contains(String.valueOf(i)));
846          }
847      }
848  
# Line 838 | Line 850 | public class ConcurrentLinkedDequeTest e
850       * A deserialized serialized deque has same elements in same order
851       */
852      public void testSerialization() throws Exception {
853 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
854 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
855 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
856 <        out.writeObject(q);
857 <        out.close();
858 <
859 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
860 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
861 <        ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject();
862 <        assertEquals(q.size(), r.size());
863 <        while (!q.isEmpty())
864 <            assertEquals(q.remove(), r.remove());
853 >        Queue x = populatedDeque(SIZE);
854 >        Queue y = serialClone(x);
855 >
856 >        assertNotSame(x, y);
857 >        assertEquals(x.size(), y.size());
858 >        assertEquals(x.toString(), y.toString());
859 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
860 >        while (!x.isEmpty()) {
861 >            assertFalse(y.isEmpty());
862 >            assertEquals(x.remove(), y.remove());
863 >        }
864 >        assertTrue(y.isEmpty());
865      }
866  
867 +    /**
868 +     * contains(null) always return false.
869 +     * remove(null) always throws NullPointerException.
870 +     */
871 +    public void testNeverContainsNull() {
872 +        Deque<?>[] qs = {
873 +            new ConcurrentLinkedDeque<Object>(),
874 +            populatedDeque(2),
875 +        };
876 +
877 +        for (Deque<?> q : qs) {
878 +            assertFalse(q.contains(null));
879 +            try {
880 +                assertFalse(q.remove(null));
881 +                shouldThrow();
882 +            } catch (NullPointerException success) {}
883 +            try {
884 +                assertFalse(q.removeFirstOccurrence(null));
885 +                shouldThrow();
886 +            } catch (NullPointerException success) {}
887 +            try {
888 +                assertFalse(q.removeLastOccurrence(null));
889 +                shouldThrow();
890 +            } catch (NullPointerException success) {}
891 +        }
892 +    }
893   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines