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.5 by jsr166, Fri Nov 5 00:17:22 2010 UTC vs.
Revision 1.29 by jsr166, Fri Aug 4 03:30:21 2017 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  
20   public class ConcurrentLinkedDequeTest extends JSR166TestCase {
21  
22      public static void main(String[] args) {
23 <        junit.textui.TestRunner.run(suite());
23 >        main(suite(), args);
24      }
25  
26      public static Test suite() {
27 <        return new TestSuite(ConcurrentLinkedDequeTest.class);
27 >        class Implementation implements CollectionImplementation {
28 >            public Class<?> klazz() { return ConcurrentLinkedDeque.class; }
29 >            public Collection emptyCollection() { return new ConcurrentLinkedDeque(); }
30 >            public Object makeElement(int i) { return i; }
31 >            public boolean isConcurrent() { return true; }
32 >            public boolean permitsNulls() { return false; }
33 >        }
34 >        return newTestSuite(ConcurrentLinkedDequeTest.class,
35 >                            CollectionTest.testSuite(new Implementation()));
36      }
37  
38      /**
39 <     * Create a deque of given size containing consecutive
40 <     * Integers 0 ... n.
39 >     * Returns a new deque of given size containing consecutive
40 >     * Integers 0 ... n - 1.
41       */
42 <    private ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
43 <        ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>();
42 >    private static ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
43 >        ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>();
44          assertTrue(q.isEmpty());
45          for (int i = 0; i < n; ++i)
46              assertTrue(q.offer(new Integer(i)));
47          assertFalse(q.isEmpty());
48          assertEquals(n, q.size());
49 +        assertEquals((Integer) 0, q.peekFirst());
50 +        assertEquals((Integer) (n - 1), q.peekLast());
51          return q;
52      }
53  
# Line 48 | Line 64 | public class ConcurrentLinkedDequeTest e
64       */
65      public void testConstructor3() {
66          try {
67 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null);
67 >            new ConcurrentLinkedDeque((Collection)null);
68              shouldThrow();
69          } catch (NullPointerException success) {}
70      }
# Line 58 | Line 74 | public class ConcurrentLinkedDequeTest e
74       */
75      public void testConstructor4() {
76          try {
77 <            Integer[] ints = new Integer[SIZE];
62 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
77 >            new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE]));
78              shouldThrow();
79          } catch (NullPointerException success) {}
80      }
# Line 68 | Line 83 | public class ConcurrentLinkedDequeTest e
83       * Initializing from Collection with some null elements throws NPE
84       */
85      public void testConstructor5() {
86 +        Integer[] ints = new Integer[SIZE];
87 +        for (int i = 0; i < SIZE - 1; ++i)
88 +            ints[i] = new Integer(i);
89          try {
90 <            Integer[] ints = new Integer[SIZE];
73 <            for (int i = 0; i < SIZE-1; ++i)
74 <                ints[i] = new Integer(i);
75 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
90 >            new ConcurrentLinkedDeque(Arrays.asList(ints));
91              shouldThrow();
92          } catch (NullPointerException success) {}
93      }
# Line 109 | Line 124 | public class ConcurrentLinkedDequeTest e
124      public void testSize() {
125          ConcurrentLinkedDeque q = populatedDeque(SIZE);
126          for (int i = 0; i < SIZE; ++i) {
127 <            assertEquals(SIZE-i, q.size());
127 >            assertEquals(SIZE - i, q.size());
128              q.remove();
129          }
130          for (int i = 0; i < SIZE; ++i) {
# Line 122 | Line 137 | public class ConcurrentLinkedDequeTest e
137       * push(null) throws NPE
138       */
139      public void testPushNull() {
140 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
141          try {
126            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
142              q.push(null);
143              shouldThrow();
144          } catch (NullPointerException success) {}
# Line 157 | Line 172 | public class ConcurrentLinkedDequeTest e
172       * offer(null) throws NPE
173       */
174      public void testOfferNull() {
175 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
176          try {
161            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
177              q.offer(null);
178              shouldThrow();
179          } catch (NullPointerException success) {}
# Line 168 | Line 183 | public class ConcurrentLinkedDequeTest e
183       * offerFirst(null) throws NPE
184       */
185      public void testOfferFirstNull() {
186 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
187          try {
172            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
188              q.offerFirst(null);
189              shouldThrow();
190          } catch (NullPointerException success) {}
# Line 179 | Line 194 | public class ConcurrentLinkedDequeTest e
194       * offerLast(null) throws NPE
195       */
196      public void testOfferLastNull() {
197 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
198          try {
183            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
199              q.offerLast(null);
200              shouldThrow();
201          } catch (NullPointerException success) {}
# Line 223 | Line 238 | public class ConcurrentLinkedDequeTest e
238       * add(null) throws NPE
239       */
240      public void testAddNull() {
241 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
242          try {
227            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
243              q.add(null);
244              shouldThrow();
245          } catch (NullPointerException success) {}
# Line 234 | Line 249 | public class ConcurrentLinkedDequeTest e
249       * addFirst(null) throws NPE
250       */
251      public void testAddFirstNull() {
252 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
253          try {
238            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
254              q.addFirst(null);
255              shouldThrow();
256          } catch (NullPointerException success) {}
# Line 245 | Line 260 | public class ConcurrentLinkedDequeTest e
260       * addLast(null) throws NPE
261       */
262      public void testAddLastNull() {
263 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
264          try {
249            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
265              q.addLast(null);
266              shouldThrow();
267          } catch (NullPointerException success) {}
# Line 289 | Line 304 | public class ConcurrentLinkedDequeTest e
304       * addAll(null) throws NPE
305       */
306      public void testAddAll1() {
307 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
308          try {
293            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
309              q.addAll(null);
310              shouldThrow();
311          } catch (NullPointerException success) {}
312      }
313  
314      /**
315 <     * addAll(this) throws IAE
315 >     * addAll(this) throws IllegalArgumentException
316       */
317      public void testAddAllSelf() {
318 +        ConcurrentLinkedDeque q = populatedDeque(SIZE);
319          try {
304            ConcurrentLinkedDeque q = populatedDeque(SIZE);
320              q.addAll(q);
321              shouldThrow();
322          } catch (IllegalArgumentException success) {}
# Line 311 | Line 326 | public class ConcurrentLinkedDequeTest e
326       * addAll of a collection with null elements throws NPE
327       */
328      public void testAddAll2() {
329 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
330          try {
331 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
316 <            Integer[] ints = new Integer[SIZE];
317 <            q.addAll(Arrays.asList(ints));
331 >            q.addAll(Arrays.asList(new Integer[SIZE]));
332              shouldThrow();
333          } catch (NullPointerException success) {}
334      }
# Line 324 | Line 338 | public class ConcurrentLinkedDequeTest e
338       * possibly adding some elements
339       */
340      public void testAddAll3() {
341 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
342 +        Integer[] ints = new Integer[SIZE];
343 +        for (int i = 0; i < SIZE - 1; ++i)
344 +            ints[i] = new Integer(i);
345          try {
328            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
329            Integer[] ints = new Integer[SIZE];
330            for (int i = 0; i < SIZE-1; ++i)
331                ints[i] = new Integer(i);
346              q.addAll(Arrays.asList(ints));
347              shouldThrow();
348          } catch (NullPointerException success) {}
# Line 365 | Line 379 | public class ConcurrentLinkedDequeTest e
379       */
380      public void testPollLast() {
381          ConcurrentLinkedDeque q = populatedDeque(SIZE);
382 <        for (int i = SIZE-1; i >= 0; --i) {
382 >        for (int i = SIZE - 1; i >= 0; --i) {
383              assertEquals(i, q.pollLast());
384          }
385          assertNull(q.pollLast());
# Line 430 | Line 444 | public class ConcurrentLinkedDequeTest e
444       */
445      public void testRemoveElement() {
446          ConcurrentLinkedDeque q = populatedDeque(SIZE);
447 <        for (int i = 1; i < SIZE; i+=2) {
448 <            assertTrue(q.remove(new Integer(i)));
449 <        }
450 <        for (int i = 0; i < SIZE; i+=2) {
451 <            assertTrue(q.remove(new Integer(i)));
452 <            assertFalse(q.remove(new Integer(i+1)));
447 >        for (int i = 1; i < SIZE; i += 2) {
448 >            assertTrue(q.contains(i));
449 >            assertTrue(q.remove(i));
450 >            assertFalse(q.contains(i));
451 >            assertTrue(q.contains(i - 1));
452 >        }
453 >        for (int i = 0; i < SIZE; i += 2) {
454 >            assertTrue(q.contains(i));
455 >            assertTrue(q.remove(i));
456 >            assertFalse(q.contains(i));
457 >            assertFalse(q.remove(i + 1));
458 >            assertFalse(q.contains(i + 1));
459          }
460          assertTrue(q.isEmpty());
461      }
# Line 459 | Line 479 | public class ConcurrentLinkedDequeTest e
479       */
480      public void testPeekLast() {
481          ConcurrentLinkedDeque q = populatedDeque(SIZE);
482 <        for (int i = SIZE-1; i >= 0; --i) {
482 >        for (int i = SIZE - 1; i >= 0; --i) {
483              assertEquals(i, q.peekLast());
484              assertEquals(i, q.pollLast());
485              assertTrue(q.peekLast() == null ||
# Line 488 | Line 508 | public class ConcurrentLinkedDequeTest e
508       */
509      public void testLastElement() {
510          ConcurrentLinkedDeque q = populatedDeque(SIZE);
511 <        for (int i = SIZE-1; i >= 0; --i) {
511 >        for (int i = SIZE - 1; i >= 0; --i) {
512              assertEquals(i, q.getLast());
513              assertEquals(i, q.pollLast());
514          }
# Line 534 | Line 554 | public class ConcurrentLinkedDequeTest e
554       */
555      public void testRemoveFirstOccurrence() {
556          ConcurrentLinkedDeque q = populatedDeque(SIZE);
557 <        for (int i = 1; i < SIZE; i+=2) {
557 >        for (int i = 1; i < SIZE; i += 2) {
558              assertTrue(q.removeFirstOccurrence(new Integer(i)));
559          }
560 <        for (int i = 0; i < SIZE; i+=2) {
560 >        for (int i = 0; i < SIZE; i += 2) {
561              assertTrue(q.removeFirstOccurrence(new Integer(i)));
562 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
562 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
563          }
564          assertTrue(q.isEmpty());
565      }
# Line 549 | Line 569 | public class ConcurrentLinkedDequeTest e
569       */
570      public void testRemoveLastOccurrence() {
571          ConcurrentLinkedDeque q = populatedDeque(SIZE);
572 <        for (int i = 1; i < SIZE; i+=2) {
572 >        for (int i = 1; i < SIZE; i += 2) {
573              assertTrue(q.removeLastOccurrence(new Integer(i)));
574          }
575 <        for (int i = 0; i < SIZE; i+=2) {
575 >        for (int i = 0; i < SIZE; i += 2) {
576              assertTrue(q.removeLastOccurrence(new Integer(i)));
577 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
577 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
578          }
579          assertTrue(q.isEmpty());
580      }
# Line 613 | Line 633 | public class ConcurrentLinkedDequeTest e
633                  assertTrue(changed);
634  
635              assertTrue(q.containsAll(p));
636 <            assertEquals(SIZE-i, q.size());
636 >            assertEquals(SIZE - i, q.size());
637              p.remove();
638          }
639      }
# Line 626 | Line 646 | public class ConcurrentLinkedDequeTest e
646              ConcurrentLinkedDeque q = populatedDeque(SIZE);
647              ConcurrentLinkedDeque p = populatedDeque(i);
648              assertTrue(q.removeAll(p));
649 <            assertEquals(SIZE-i, q.size());
649 >            assertEquals(SIZE - i, q.size());
650              for (int j = 0; j < i; ++j) {
651 <                Integer I = (Integer)(p.remove());
652 <                assertFalse(q.contains(I));
651 >                Integer x = (Integer)(p.remove());
652 >                assertFalse(q.contains(x));
653              }
654          }
655      }
# Line 683 | Line 703 | public class ConcurrentLinkedDequeTest e
703       */
704      public void testIterator() {
705          ConcurrentLinkedDeque q = populatedDeque(SIZE);
686        int i = 0;
706          Iterator it = q.iterator();
707 <        while (it.hasNext()) {
707 >        int i;
708 >        for (i = 0; it.hasNext(); i++)
709              assertTrue(q.contains(it.next()));
690            ++i;
691        }
710          assertEquals(i, SIZE);
711 +        assertIteratorExhausted(it);
712 +    }
713 +
714 +    /**
715 +     * iterator of empty collection has no elements
716 +     */
717 +    public void testEmptyIterator() {
718 +        Deque c = new ConcurrentLinkedDeque();
719 +        assertIteratorExhausted(c.iterator());
720 +        assertIteratorExhausted(c.descendingIterator());
721      }
722  
723      /**
# Line 734 | Line 762 | public class ConcurrentLinkedDequeTest e
762          final Random rng = new Random();
763          for (int iters = 0; iters < 100; ++iters) {
764              int max = rng.nextInt(5) + 2;
765 <            int split = rng.nextInt(max-1) + 1;
765 >            int split = rng.nextInt(max - 1) + 1;
766              for (int j = 1; j <= max; ++j)
767                  q.add(new Integer(j));
768              Iterator it = q.iterator();
769              for (int j = 1; j <= split; ++j)
770                  assertEquals(it.next(), new Integer(j));
771              it.remove();
772 <            assertEquals(it.next(), new Integer(split+1));
772 >            assertEquals(it.next(), new Integer(split + 1));
773              for (int j = 1; j <= split; ++j)
774                  q.remove(new Integer(j));
775              it = q.iterator();
776 <            for (int j = split+1; j <= max; ++j) {
776 >            for (int j = split + 1; j <= max; ++j) {
777                  assertEquals(it.next(), new Integer(j));
778                  it.remove();
779              }
# Line 802 | Line 830 | public class ConcurrentLinkedDequeTest e
830          final Random rng = new Random();
831          for (int iters = 0; iters < 100; ++iters) {
832              int max = rng.nextInt(5) + 2;
833 <            int split = rng.nextInt(max-1) + 1;
833 >            int split = rng.nextInt(max - 1) + 1;
834              for (int j = max; j >= 1; --j)
835                  q.add(new Integer(j));
836              Iterator it = q.descendingIterator();
837              for (int j = 1; j <= split; ++j)
838                  assertEquals(it.next(), new Integer(j));
839              it.remove();
840 <            assertEquals(it.next(), new Integer(split+1));
840 >            assertEquals(it.next(), new Integer(split + 1));
841              for (int j = 1; j <= split; ++j)
842                  q.remove(new Integer(j));
843              it = q.descendingIterator();
844 <            for (int j = split+1; j <= max; ++j) {
844 >            for (int j = split + 1; j <= max; ++j) {
845                  assertEquals(it.next(), new Integer(j));
846                  it.remove();
847              }
# Line 829 | Line 857 | public class ConcurrentLinkedDequeTest e
857          ConcurrentLinkedDeque q = populatedDeque(SIZE);
858          String s = q.toString();
859          for (int i = 0; i < SIZE; ++i) {
860 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
860 >            assertTrue(s.contains(String.valueOf(i)));
861          }
862      }
863  
864      /**
865 <     * A deserialized serialized deque has same elements in same order
865 >     * A deserialized/reserialized deque has same elements in same order
866       */
867      public void testSerialization() throws Exception {
868 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
869 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
870 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
871 <        out.writeObject(q);
872 <        out.close();
873 <
874 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
875 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
876 <        ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject();
877 <        assertEquals(q.size(), r.size());
878 <        while (!q.isEmpty())
879 <            assertEquals(q.remove(), r.remove());
868 >        Queue x = populatedDeque(SIZE);
869 >        Queue y = serialClone(x);
870 >
871 >        assertNotSame(x, y);
872 >        assertEquals(x.size(), y.size());
873 >        assertEquals(x.toString(), y.toString());
874 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
875 >        while (!x.isEmpty()) {
876 >            assertFalse(y.isEmpty());
877 >            assertEquals(x.remove(), y.remove());
878 >        }
879 >        assertTrue(y.isEmpty());
880      }
881  
882 +    /**
883 +     * contains(null) always return false.
884 +     * remove(null) always throws NullPointerException.
885 +     */
886 +    public void testNeverContainsNull() {
887 +        Deque<?>[] qs = {
888 +            new ConcurrentLinkedDeque<Object>(),
889 +            populatedDeque(2),
890 +        };
891 +
892 +        for (Deque<?> q : qs) {
893 +            assertFalse(q.contains(null));
894 +            try {
895 +                assertFalse(q.remove(null));
896 +                shouldThrow();
897 +            } catch (NullPointerException success) {}
898 +            try {
899 +                assertFalse(q.removeFirstOccurrence(null));
900 +                shouldThrow();
901 +            } catch (NullPointerException success) {}
902 +            try {
903 +                assertFalse(q.removeLastOccurrence(null));
904 +                shouldThrow();
905 +            } catch (NullPointerException success) {}
906 +        }
907 +    }
908   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines