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.11 by jsr166, Tue Feb 21 02:04:17 2012 UTC vs.
Revision 1.27 by jsr166, Sat Mar 11 18:20:46 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
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       * Returns a new deque of given size containing consecutive
40 <     * Integers 0 ... n.
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 52 | 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 62 | Line 74 | public class ConcurrentLinkedDequeTest e
74       */
75      public void testConstructor4() {
76          try {
77 <            Integer[] ints = new Integer[SIZE];
66 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
77 >            new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE]));
78              shouldThrow();
79          } catch (NullPointerException success) {}
80      }
# Line 72 | 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];
77 <            for (int i = 0; i < SIZE-1; ++i)
78 <                ints[i] = new Integer(i);
79 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
90 >            new ConcurrentLinkedDeque(Arrays.asList(ints));
91              shouldThrow();
92          } catch (NullPointerException success) {}
93      }
# Line 113 | 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 126 | Line 137 | public class ConcurrentLinkedDequeTest e
137       * push(null) throws NPE
138       */
139      public void testPushNull() {
140 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
141          try {
130            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
142              q.push(null);
143              shouldThrow();
144          } catch (NullPointerException success) {}
# Line 161 | Line 172 | public class ConcurrentLinkedDequeTest e
172       * offer(null) throws NPE
173       */
174      public void testOfferNull() {
175 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
176          try {
165            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
177              q.offer(null);
178              shouldThrow();
179          } catch (NullPointerException success) {}
# Line 172 | Line 183 | public class ConcurrentLinkedDequeTest e
183       * offerFirst(null) throws NPE
184       */
185      public void testOfferFirstNull() {
186 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
187          try {
176            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
188              q.offerFirst(null);
189              shouldThrow();
190          } catch (NullPointerException success) {}
# Line 183 | Line 194 | public class ConcurrentLinkedDequeTest e
194       * offerLast(null) throws NPE
195       */
196      public void testOfferLastNull() {
197 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
198          try {
187            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
199              q.offerLast(null);
200              shouldThrow();
201          } catch (NullPointerException success) {}
# Line 227 | Line 238 | public class ConcurrentLinkedDequeTest e
238       * add(null) throws NPE
239       */
240      public void testAddNull() {
241 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
242          try {
231            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
243              q.add(null);
244              shouldThrow();
245          } catch (NullPointerException success) {}
# Line 238 | Line 249 | public class ConcurrentLinkedDequeTest e
249       * addFirst(null) throws NPE
250       */
251      public void testAddFirstNull() {
252 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
253          try {
242            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
254              q.addFirst(null);
255              shouldThrow();
256          } catch (NullPointerException success) {}
# Line 249 | Line 260 | public class ConcurrentLinkedDequeTest e
260       * addLast(null) throws NPE
261       */
262      public void testAddLastNull() {
263 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
264          try {
253            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
265              q.addLast(null);
266              shouldThrow();
267          } catch (NullPointerException success) {}
# Line 293 | Line 304 | public class ConcurrentLinkedDequeTest e
304       * addAll(null) throws NPE
305       */
306      public void testAddAll1() {
307 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
308          try {
297            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
309              q.addAll(null);
310              shouldThrow();
311          } catch (NullPointerException success) {}
# Line 304 | Line 315 | public class ConcurrentLinkedDequeTest e
315       * addAll(this) throws IAE
316       */
317      public void testAddAllSelf() {
318 +        ConcurrentLinkedDeque q = populatedDeque(SIZE);
319          try {
308            ConcurrentLinkedDeque q = populatedDeque(SIZE);
320              q.addAll(q);
321              shouldThrow();
322          } catch (IllegalArgumentException success) {}
# Line 315 | 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();
320 <            Integer[] ints = new Integer[SIZE];
321 <            q.addAll(Arrays.asList(ints));
331 >            q.addAll(Arrays.asList(new Integer[SIZE]));
332              shouldThrow();
333          } catch (NullPointerException success) {}
334      }
# Line 328 | 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 {
332            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
333            Integer[] ints = new Integer[SIZE];
334            for (int i = 0; i < SIZE-1; ++i)
335                ints[i] = new Integer(i);
346              q.addAll(Arrays.asList(ints));
347              shouldThrow();
348          } catch (NullPointerException success) {}
# Line 369 | 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 434 | 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) {
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));
451 >            assertTrue(q.contains(i - 1));
452          }
453 <        for (int i = 0; i < SIZE; i+=2) {
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));
457 >            assertFalse(q.remove(i + 1));
458 >            assertFalse(q.contains(i + 1));
459          }
460          assertTrue(q.isEmpty());
461      }
# Line 469 | 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 498 | 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 544 | 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 559 | 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 623 | 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 636 | 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 693 | Line 703 | public class ConcurrentLinkedDequeTest e
703       */
704      public void testIterator() {
705          ConcurrentLinkedDeque q = populatedDeque(SIZE);
696        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()));
700            ++i;
701        }
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 744 | 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 812 | 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 850 | Line 868 | public class ConcurrentLinkedDequeTest e
868          Queue x = populatedDeque(SIZE);
869          Queue y = serialClone(x);
870  
871 <        assertTrue(x != y);
871 >        assertNotSame(x, y);
872          assertEquals(x.size(), y.size());
873          assertEquals(x.toString(), y.toString());
874          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 861 | Line 879 | public class ConcurrentLinkedDequeTest e
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