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.1 by jsr166, Wed Aug 25 21:40:03 2010 UTC vs.
Revision 1.13 by jsr166, Sun Nov 23 22:27:06 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.*;
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.Random;
17 > import java.util.concurrent.ConcurrentLinkedDeque;
18  
19   public class ConcurrentLinkedDequeTest extends JSR166TestCase {
20  
# Line 22 | Line 27 | public class ConcurrentLinkedDequeTest e
27      }
28  
29      /**
30 <     * Create a deque of given size containing consecutive
30 >     * Returns a new deque of given size containing consecutive
31       * Integers 0 ... n.
32       */
33 <    private ConcurrentLinkedDeque populatedDeque(int n) {
34 <        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
33 >    private ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
34 >        ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>();
35          assertTrue(q.isEmpty());
36          for (int i = 0; i < n; ++i)
37              assertTrue(q.offer(new Integer(i)));
# Line 431 | Line 436 | public class ConcurrentLinkedDequeTest e
436      public void testRemoveElement() {
437          ConcurrentLinkedDeque q = populatedDeque(SIZE);
438          for (int i = 1; i < SIZE; i+=2) {
439 <            assertTrue(q.remove(new Integer(i)));
439 >            assertTrue(q.contains(i));
440 >            assertTrue(q.remove(i));
441 >            assertFalse(q.contains(i));
442 >            assertTrue(q.contains(i-1));
443          }
444          for (int i = 0; i < SIZE; i+=2) {
445 <            assertTrue(q.remove(new Integer(i)));
446 <            assertFalse(q.remove(new Integer(i+1)));
445 >            assertTrue(q.contains(i));
446 >            assertTrue(q.remove(i));
447 >            assertFalse(q.contains(i));
448 >            assertFalse(q.remove(i+1));
449 >            assertFalse(q.contains(i+1));
450          }
451          assertTrue(q.isEmpty());
452      }
# Line 635 | Line 646 | public class ConcurrentLinkedDequeTest e
646      }
647  
648      /**
649 <     * toArray() contains all elements
649 >     * toArray() contains all elements in FIFO order
650       */
651      public void testToArray() {
652          ConcurrentLinkedDeque q = populatedDeque(SIZE);
653          Object[] o = q.toArray();
643        Arrays.sort(o);
654          for (int i = 0; i < o.length; i++)
655 <            assertEquals(o[i], q.poll());
655 >            assertSame(o[i], q.poll());
656      }
657  
658      /**
659 <     * toArray(a) contains all elements
659 >     * toArray(a) contains all elements in FIFO order
660       */
661      public void testToArray2() {
662 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
662 >        ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE);
663          Integer[] ints = new Integer[SIZE];
664 <        ints = (Integer[])q.toArray(ints);
665 <        Arrays.sort(ints);
664 >        Integer[] array = q.toArray(ints);
665 >        assertSame(ints, array);
666          for (int i = 0; i < ints.length; i++)
667 <            assertEquals(ints[i], q.poll());
667 >            assertSame(ints[i], q.poll());
668      }
669  
670      /**
671 <     * toArray(null) throws NPE
671 >     * toArray(null) throws NullPointerException
672       */
673 <    public void testToArray_BadArg() {
673 >    public void testToArray_NullArg() {
674          ConcurrentLinkedDeque q = populatedDeque(SIZE);
675          try {
676 <            Object o[] = q.toArray(null);
676 >            q.toArray(null);
677              shouldThrow();
678          } catch (NullPointerException success) {}
679      }
680  
681      /**
682 <     * toArray() with incompatible array type throws ArrayStoreException
682 >     * toArray(incompatible array type) throws ArrayStoreException
683       */
684      public void testToArray1_BadArg() {
685          ConcurrentLinkedDeque q = populatedDeque(SIZE);
686          try {
687 <            Object o[] = q.toArray(new String[10]);
687 >            q.toArray(new String[10]);
688              shouldThrow();
689          } catch (ArrayStoreException success) {}
690      }
# Line 830 | Line 840 | public class ConcurrentLinkedDequeTest e
840          ConcurrentLinkedDeque q = populatedDeque(SIZE);
841          String s = q.toString();
842          for (int i = 0; i < SIZE; ++i) {
843 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
843 >            assertTrue(s.contains(String.valueOf(i)));
844          }
845      }
846  
# Line 838 | Line 848 | public class ConcurrentLinkedDequeTest e
848       * A deserialized serialized deque has same elements in same order
849       */
850      public void testSerialization() throws Exception {
851 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
852 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
853 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
854 <        out.writeObject(q);
855 <        out.close();
856 <
857 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
858 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
859 <        ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject();
860 <        assertEquals(q.size(), r.size());
861 <        while (!q.isEmpty())
862 <            assertEquals(q.remove(), r.remove());
851 >        Queue x = populatedDeque(SIZE);
852 >        Queue y = serialClone(x);
853 >
854 >        assertNotSame(x, y);
855 >        assertEquals(x.size(), y.size());
856 >        assertEquals(x.toString(), y.toString());
857 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
858 >        while (!x.isEmpty()) {
859 >            assertFalse(y.isEmpty());
860 >            assertEquals(x.remove(), y.remove());
861 >        }
862 >        assertTrue(y.isEmpty());
863      }
864  
865 +    /**
866 +     * contains(null) always return false.
867 +     * remove(null) always throws NullPointerException.
868 +     */
869 +    public void testNeverContainsNull() {
870 +        Deque<?>[] qs = {
871 +            new ConcurrentLinkedDeque<Object>(),
872 +            populatedDeque(2),
873 +        };
874 +
875 +        for (Deque<?> q : qs) {
876 +            assertFalse(q.contains(null));
877 +            try {
878 +                assertFalse(q.remove(null));
879 +                shouldThrow();
880 +            } catch (NullPointerException success) {}
881 +            try {
882 +                assertFalse(q.removeFirstOccurrence(null));
883 +                shouldThrow();
884 +            } catch (NullPointerException success) {}
885 +            try {
886 +                assertFalse(q.removeLastOccurrence(null));
887 +                shouldThrow();
888 +            } catch (NullPointerException success) {}
889 +        }
890 +    }
891   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines