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.23 by jsr166, Sun Oct 16 20:16:36 2016 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  
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run(suite());
24 >        main(suite(), args);
25      }
26  
27      public static Test suite() {
28 <        return new TestSuite(ConcurrentLinkedDequeTest.class);
28 >        class Implementation implements CollectionImplementation {
29 >            public Class<?> klazz() { return ConcurrentLinkedDeque.class; }
30 >            public Collection emptyCollection() { return new ConcurrentLinkedDeque(); }
31 >            public Object makeElement(int i) { return i; }
32 >            public boolean isConcurrent() { return true; }
33 >            public boolean permitsNulls() { return false; }
34 >        }
35 >        return newTestSuite(ConcurrentLinkedDequeTest.class,
36 >                            CollectionTest.testSuite(new Implementation()));
37      }
38  
39      /**
40 <     * Create a deque of given size containing consecutive
40 >     * Returns a new deque of given size containing consecutive
41       * Integers 0 ... n.
42       */
43 <    private ConcurrentLinkedDeque populatedDeque(int n) {
44 <        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
43 >    private ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
44 >        ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>();
45          assertTrue(q.isEmpty());
46          for (int i = 0; i < n; ++i)
47              assertTrue(q.offer(new Integer(i)));
# Line 48 | Line 63 | public class ConcurrentLinkedDequeTest e
63       */
64      public void testConstructor3() {
65          try {
66 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null);
66 >            new ConcurrentLinkedDeque((Collection)null);
67              shouldThrow();
68          } catch (NullPointerException success) {}
69      }
# Line 58 | Line 73 | public class ConcurrentLinkedDequeTest e
73       */
74      public void testConstructor4() {
75          try {
76 <            Integer[] ints = new Integer[SIZE];
62 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
76 >            new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE]));
77              shouldThrow();
78          } catch (NullPointerException success) {}
79      }
# Line 68 | Line 82 | public class ConcurrentLinkedDequeTest e
82       * Initializing from Collection with some null elements throws NPE
83       */
84      public void testConstructor5() {
85 +        Integer[] ints = new Integer[SIZE];
86 +        for (int i = 0; i < SIZE - 1; ++i)
87 +            ints[i] = new Integer(i);
88          try {
89 <            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));
89 >            new ConcurrentLinkedDeque(Arrays.asList(ints));
90              shouldThrow();
91          } catch (NullPointerException success) {}
92      }
# Line 109 | Line 123 | public class ConcurrentLinkedDequeTest e
123      public void testSize() {
124          ConcurrentLinkedDeque q = populatedDeque(SIZE);
125          for (int i = 0; i < SIZE; ++i) {
126 <            assertEquals(SIZE-i, q.size());
126 >            assertEquals(SIZE - i, q.size());
127              q.remove();
128          }
129          for (int i = 0; i < SIZE; ++i) {
# Line 122 | Line 136 | public class ConcurrentLinkedDequeTest e
136       * push(null) throws NPE
137       */
138      public void testPushNull() {
139 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
140          try {
126            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
141              q.push(null);
142              shouldThrow();
143          } catch (NullPointerException success) {}
# Line 157 | Line 171 | public class ConcurrentLinkedDequeTest e
171       * offer(null) throws NPE
172       */
173      public void testOfferNull() {
174 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
175          try {
161            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
176              q.offer(null);
177              shouldThrow();
178          } catch (NullPointerException success) {}
# Line 168 | Line 182 | public class ConcurrentLinkedDequeTest e
182       * offerFirst(null) throws NPE
183       */
184      public void testOfferFirstNull() {
185 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
186          try {
172            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
187              q.offerFirst(null);
188              shouldThrow();
189          } catch (NullPointerException success) {}
# Line 179 | Line 193 | public class ConcurrentLinkedDequeTest e
193       * offerLast(null) throws NPE
194       */
195      public void testOfferLastNull() {
196 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
197          try {
183            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
198              q.offerLast(null);
199              shouldThrow();
200          } catch (NullPointerException success) {}
# Line 223 | Line 237 | public class ConcurrentLinkedDequeTest e
237       * add(null) throws NPE
238       */
239      public void testAddNull() {
240 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
241          try {
227            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
242              q.add(null);
243              shouldThrow();
244          } catch (NullPointerException success) {}
# Line 234 | Line 248 | public class ConcurrentLinkedDequeTest e
248       * addFirst(null) throws NPE
249       */
250      public void testAddFirstNull() {
251 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
252          try {
238            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
253              q.addFirst(null);
254              shouldThrow();
255          } catch (NullPointerException success) {}
# Line 245 | Line 259 | public class ConcurrentLinkedDequeTest e
259       * addLast(null) throws NPE
260       */
261      public void testAddLastNull() {
262 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
263          try {
249            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
264              q.addLast(null);
265              shouldThrow();
266          } catch (NullPointerException success) {}
# Line 289 | Line 303 | public class ConcurrentLinkedDequeTest e
303       * addAll(null) throws NPE
304       */
305      public void testAddAll1() {
306 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
307          try {
293            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
308              q.addAll(null);
309              shouldThrow();
310          } catch (NullPointerException success) {}
# Line 300 | Line 314 | public class ConcurrentLinkedDequeTest e
314       * addAll(this) throws IAE
315       */
316      public void testAddAllSelf() {
317 +        ConcurrentLinkedDeque q = populatedDeque(SIZE);
318          try {
304            ConcurrentLinkedDeque q = populatedDeque(SIZE);
319              q.addAll(q);
320              shouldThrow();
321          } catch (IllegalArgumentException success) {}
# Line 311 | Line 325 | public class ConcurrentLinkedDequeTest e
325       * addAll of a collection with null elements throws NPE
326       */
327      public void testAddAll2() {
328 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
329          try {
330 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
316 <            Integer[] ints = new Integer[SIZE];
317 <            q.addAll(Arrays.asList(ints));
330 >            q.addAll(Arrays.asList(new Integer[SIZE]));
331              shouldThrow();
332          } catch (NullPointerException success) {}
333      }
# Line 324 | Line 337 | public class ConcurrentLinkedDequeTest e
337       * possibly adding some elements
338       */
339      public void testAddAll3() {
340 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
341 +        Integer[] ints = new Integer[SIZE];
342 +        for (int i = 0; i < SIZE - 1; ++i)
343 +            ints[i] = new Integer(i);
344          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);
345              q.addAll(Arrays.asList(ints));
346              shouldThrow();
347          } catch (NullPointerException success) {}
# Line 365 | Line 378 | public class ConcurrentLinkedDequeTest e
378       */
379      public void testPollLast() {
380          ConcurrentLinkedDeque q = populatedDeque(SIZE);
381 <        for (int i = SIZE-1; i >= 0; --i) {
381 >        for (int i = SIZE - 1; i >= 0; --i) {
382              assertEquals(i, q.pollLast());
383          }
384          assertNull(q.pollLast());
# Line 430 | Line 443 | public class ConcurrentLinkedDequeTest e
443       */
444      public void testRemoveElement() {
445          ConcurrentLinkedDeque q = populatedDeque(SIZE);
446 <        for (int i = 1; i < SIZE; i+=2) {
447 <            assertTrue(q.remove(new Integer(i)));
448 <        }
449 <        for (int i = 0; i < SIZE; i+=2) {
450 <            assertTrue(q.remove(new Integer(i)));
451 <            assertFalse(q.remove(new Integer(i+1)));
446 >        for (int i = 1; i < SIZE; i += 2) {
447 >            assertTrue(q.contains(i));
448 >            assertTrue(q.remove(i));
449 >            assertFalse(q.contains(i));
450 >            assertTrue(q.contains(i - 1));
451 >        }
452 >        for (int i = 0; i < SIZE; i += 2) {
453 >            assertTrue(q.contains(i));
454 >            assertTrue(q.remove(i));
455 >            assertFalse(q.contains(i));
456 >            assertFalse(q.remove(i + 1));
457 >            assertFalse(q.contains(i + 1));
458          }
459          assertTrue(q.isEmpty());
460      }
# Line 459 | Line 478 | public class ConcurrentLinkedDequeTest e
478       */
479      public void testPeekLast() {
480          ConcurrentLinkedDeque q = populatedDeque(SIZE);
481 <        for (int i = SIZE-1; i >= 0; --i) {
481 >        for (int i = SIZE - 1; i >= 0; --i) {
482              assertEquals(i, q.peekLast());
483              assertEquals(i, q.pollLast());
484              assertTrue(q.peekLast() == null ||
# Line 488 | Line 507 | public class ConcurrentLinkedDequeTest e
507       */
508      public void testLastElement() {
509          ConcurrentLinkedDeque q = populatedDeque(SIZE);
510 <        for (int i = SIZE-1; i >= 0; --i) {
510 >        for (int i = SIZE - 1; i >= 0; --i) {
511              assertEquals(i, q.getLast());
512              assertEquals(i, q.pollLast());
513          }
# Line 534 | Line 553 | public class ConcurrentLinkedDequeTest e
553       */
554      public void testRemoveFirstOccurrence() {
555          ConcurrentLinkedDeque q = populatedDeque(SIZE);
556 <        for (int i = 1; i < SIZE; i+=2) {
556 >        for (int i = 1; i < SIZE; i += 2) {
557              assertTrue(q.removeFirstOccurrence(new Integer(i)));
558          }
559 <        for (int i = 0; i < SIZE; i+=2) {
559 >        for (int i = 0; i < SIZE; i += 2) {
560              assertTrue(q.removeFirstOccurrence(new Integer(i)));
561 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
561 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
562          }
563          assertTrue(q.isEmpty());
564      }
# Line 549 | Line 568 | public class ConcurrentLinkedDequeTest e
568       */
569      public void testRemoveLastOccurrence() {
570          ConcurrentLinkedDeque q = populatedDeque(SIZE);
571 <        for (int i = 1; i < SIZE; i+=2) {
571 >        for (int i = 1; i < SIZE; i += 2) {
572              assertTrue(q.removeLastOccurrence(new Integer(i)));
573          }
574 <        for (int i = 0; i < SIZE; i+=2) {
574 >        for (int i = 0; i < SIZE; i += 2) {
575              assertTrue(q.removeLastOccurrence(new Integer(i)));
576 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
576 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
577          }
578          assertTrue(q.isEmpty());
579      }
# Line 613 | Line 632 | public class ConcurrentLinkedDequeTest e
632                  assertTrue(changed);
633  
634              assertTrue(q.containsAll(p));
635 <            assertEquals(SIZE-i, q.size());
635 >            assertEquals(SIZE - i, q.size());
636              p.remove();
637          }
638      }
# Line 626 | Line 645 | public class ConcurrentLinkedDequeTest e
645              ConcurrentLinkedDeque q = populatedDeque(SIZE);
646              ConcurrentLinkedDeque p = populatedDeque(i);
647              assertTrue(q.removeAll(p));
648 <            assertEquals(SIZE-i, q.size());
648 >            assertEquals(SIZE - i, q.size());
649              for (int j = 0; j < i; ++j) {
650 <                Integer I = (Integer)(p.remove());
651 <                assertFalse(q.contains(I));
650 >                Integer x = (Integer)(p.remove());
651 >                assertFalse(q.contains(x));
652              }
653          }
654      }
655  
656      /**
657 <     * toArray() contains all elements
657 >     * toArray() contains all elements in FIFO order
658       */
659      public void testToArray() {
660          ConcurrentLinkedDeque q = populatedDeque(SIZE);
661          Object[] o = q.toArray();
643        Arrays.sort(o);
662          for (int i = 0; i < o.length; i++)
663 <            assertEquals(o[i], q.poll());
663 >            assertSame(o[i], q.poll());
664      }
665  
666      /**
667 <     * toArray(a) contains all elements
667 >     * toArray(a) contains all elements in FIFO order
668       */
669      public void testToArray2() {
670 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
670 >        ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE);
671          Integer[] ints = new Integer[SIZE];
672 <        ints = (Integer[])q.toArray(ints);
673 <        Arrays.sort(ints);
672 >        Integer[] array = q.toArray(ints);
673 >        assertSame(ints, array);
674          for (int i = 0; i < ints.length; i++)
675 <            assertEquals(ints[i], q.poll());
675 >            assertSame(ints[i], q.poll());
676      }
677  
678      /**
679 <     * toArray(null) throws NPE
679 >     * toArray(null) throws NullPointerException
680       */
681 <    public void testToArray_BadArg() {
681 >    public void testToArray_NullArg() {
682          ConcurrentLinkedDeque q = populatedDeque(SIZE);
683          try {
684 <            Object o[] = q.toArray(null);
684 >            q.toArray(null);
685              shouldThrow();
686          } catch (NullPointerException success) {}
687      }
688  
689      /**
690 <     * toArray() with incompatible array type throws ArrayStoreException
690 >     * toArray(incompatible array type) throws ArrayStoreException
691       */
692      public void testToArray1_BadArg() {
693          ConcurrentLinkedDeque q = populatedDeque(SIZE);
694          try {
695 <            Object o[] = q.toArray(new String[10]);
695 >            q.toArray(new String[10]);
696              shouldThrow();
697          } catch (ArrayStoreException success) {}
698      }
# Line 684 | Line 702 | public class ConcurrentLinkedDequeTest e
702       */
703      public void testIterator() {
704          ConcurrentLinkedDeque q = populatedDeque(SIZE);
687        int i = 0;
705          Iterator it = q.iterator();
706 <        while (it.hasNext()) {
706 >        int i;
707 >        for (i = 0; it.hasNext(); i++)
708              assertTrue(q.contains(it.next()));
691            ++i;
692        }
709          assertEquals(i, SIZE);
710 +        assertIteratorExhausted(it);
711 +    }
712 +
713 +    /**
714 +     * iterator of empty collection has no elements
715 +     */
716 +    public void testEmptyIterator() {
717 +        Deque c = new ConcurrentLinkedDeque();
718 +        assertIteratorExhausted(c.iterator());
719 +        assertIteratorExhausted(c.descendingIterator());
720      }
721  
722      /**
# Line 735 | Line 761 | public class ConcurrentLinkedDequeTest e
761          final Random rng = new Random();
762          for (int iters = 0; iters < 100; ++iters) {
763              int max = rng.nextInt(5) + 2;
764 <            int split = rng.nextInt(max-1) + 1;
764 >            int split = rng.nextInt(max - 1) + 1;
765              for (int j = 1; j <= max; ++j)
766                  q.add(new Integer(j));
767              Iterator it = q.iterator();
768              for (int j = 1; j <= split; ++j)
769                  assertEquals(it.next(), new Integer(j));
770              it.remove();
771 <            assertEquals(it.next(), new Integer(split+1));
771 >            assertEquals(it.next(), new Integer(split + 1));
772              for (int j = 1; j <= split; ++j)
773                  q.remove(new Integer(j));
774              it = q.iterator();
775 <            for (int j = split+1; j <= max; ++j) {
775 >            for (int j = split + 1; j <= max; ++j) {
776                  assertEquals(it.next(), new Integer(j));
777                  it.remove();
778              }
# Line 803 | Line 829 | public class ConcurrentLinkedDequeTest e
829          final Random rng = new Random();
830          for (int iters = 0; iters < 100; ++iters) {
831              int max = rng.nextInt(5) + 2;
832 <            int split = rng.nextInt(max-1) + 1;
832 >            int split = rng.nextInt(max - 1) + 1;
833              for (int j = max; j >= 1; --j)
834                  q.add(new Integer(j));
835              Iterator it = q.descendingIterator();
836              for (int j = 1; j <= split; ++j)
837                  assertEquals(it.next(), new Integer(j));
838              it.remove();
839 <            assertEquals(it.next(), new Integer(split+1));
839 >            assertEquals(it.next(), new Integer(split + 1));
840              for (int j = 1; j <= split; ++j)
841                  q.remove(new Integer(j));
842              it = q.descendingIterator();
843 <            for (int j = split+1; j <= max; ++j) {
843 >            for (int j = split + 1; j <= max; ++j) {
844                  assertEquals(it.next(), new Integer(j));
845                  it.remove();
846              }
# Line 830 | Line 856 | public class ConcurrentLinkedDequeTest e
856          ConcurrentLinkedDeque q = populatedDeque(SIZE);
857          String s = q.toString();
858          for (int i = 0; i < SIZE; ++i) {
859 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
859 >            assertTrue(s.contains(String.valueOf(i)));
860          }
861      }
862  
# Line 838 | Line 864 | public class ConcurrentLinkedDequeTest e
864       * A deserialized serialized deque has same elements in same order
865       */
866      public void testSerialization() throws Exception {
867 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
868 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
869 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
870 <        out.writeObject(q);
871 <        out.close();
872 <
873 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
874 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
875 <        ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject();
876 <        assertEquals(q.size(), r.size());
877 <        while (!q.isEmpty())
878 <            assertEquals(q.remove(), r.remove());
867 >        Queue x = populatedDeque(SIZE);
868 >        Queue y = serialClone(x);
869 >
870 >        assertNotSame(x, y);
871 >        assertEquals(x.size(), y.size());
872 >        assertEquals(x.toString(), y.toString());
873 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
874 >        while (!x.isEmpty()) {
875 >            assertFalse(y.isEmpty());
876 >            assertEquals(x.remove(), y.remove());
877 >        }
878 >        assertTrue(y.isEmpty());
879      }
880  
881 +    /**
882 +     * contains(null) always return false.
883 +     * remove(null) always throws NullPointerException.
884 +     */
885 +    public void testNeverContainsNull() {
886 +        Deque<?>[] qs = {
887 +            new ConcurrentLinkedDeque<Object>(),
888 +            populatedDeque(2),
889 +        };
890 +
891 +        for (Deque<?> q : qs) {
892 +            assertFalse(q.contains(null));
893 +            try {
894 +                assertFalse(q.remove(null));
895 +                shouldThrow();
896 +            } catch (NullPointerException success) {}
897 +            try {
898 +                assertFalse(q.removeFirstOccurrence(null));
899 +                shouldThrow();
900 +            } catch (NullPointerException success) {}
901 +            try {
902 +                assertFalse(q.removeLastOccurrence(null));
903 +                shouldThrow();
904 +            } catch (NullPointerException success) {}
905 +        }
906 +    }
907   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines