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.8 by jsr166, Fri May 27 19:47:55 2011 UTC vs.
Revision 1.22 by jsr166, Sun May 24 01:42:14 2015 UTC

# Line 6 | Line 6
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() {
# 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<Integer> populatedDeque(int n) {
# Line 48 | Line 55 | public class ConcurrentLinkedDequeTest e
55       */
56      public void testConstructor3() {
57          try {
58 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null);
58 >            new ConcurrentLinkedDeque((Collection)null);
59              shouldThrow();
60          } catch (NullPointerException success) {}
61      }
# Line 58 | Line 65 | public class ConcurrentLinkedDequeTest e
65       */
66      public void testConstructor4() {
67          try {
68 <            Integer[] ints = new Integer[SIZE];
62 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
68 >            new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE]));
69              shouldThrow();
70          } catch (NullPointerException success) {}
71      }
# Line 68 | Line 74 | public class ConcurrentLinkedDequeTest e
74       * Initializing from Collection with some null elements throws NPE
75       */
76      public void testConstructor5() {
77 +        Integer[] ints = new Integer[SIZE];
78 +        for (int i = 0; i < SIZE - 1; ++i)
79 +            ints[i] = new Integer(i);
80          try {
81 <            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));
81 >            new ConcurrentLinkedDeque(Arrays.asList(ints));
82              shouldThrow();
83          } catch (NullPointerException success) {}
84      }
# Line 109 | Line 115 | public class ConcurrentLinkedDequeTest e
115      public void testSize() {
116          ConcurrentLinkedDeque q = populatedDeque(SIZE);
117          for (int i = 0; i < SIZE; ++i) {
118 <            assertEquals(SIZE-i, q.size());
118 >            assertEquals(SIZE - i, q.size());
119              q.remove();
120          }
121          for (int i = 0; i < SIZE; ++i) {
# Line 122 | Line 128 | public class ConcurrentLinkedDequeTest e
128       * push(null) throws NPE
129       */
130      public void testPushNull() {
131 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
132          try {
126            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
133              q.push(null);
134              shouldThrow();
135          } catch (NullPointerException success) {}
# Line 157 | Line 163 | public class ConcurrentLinkedDequeTest e
163       * offer(null) throws NPE
164       */
165      public void testOfferNull() {
166 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
167          try {
161            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
168              q.offer(null);
169              shouldThrow();
170          } catch (NullPointerException success) {}
# Line 168 | Line 174 | public class ConcurrentLinkedDequeTest e
174       * offerFirst(null) throws NPE
175       */
176      public void testOfferFirstNull() {
177 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
178          try {
172            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
179              q.offerFirst(null);
180              shouldThrow();
181          } catch (NullPointerException success) {}
# Line 179 | Line 185 | public class ConcurrentLinkedDequeTest e
185       * offerLast(null) throws NPE
186       */
187      public void testOfferLastNull() {
188 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
189          try {
183            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
190              q.offerLast(null);
191              shouldThrow();
192          } catch (NullPointerException success) {}
# Line 223 | Line 229 | public class ConcurrentLinkedDequeTest e
229       * add(null) throws NPE
230       */
231      public void testAddNull() {
232 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
233          try {
227            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
234              q.add(null);
235              shouldThrow();
236          } catch (NullPointerException success) {}
# Line 234 | Line 240 | public class ConcurrentLinkedDequeTest e
240       * addFirst(null) throws NPE
241       */
242      public void testAddFirstNull() {
243 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
244          try {
238            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
245              q.addFirst(null);
246              shouldThrow();
247          } catch (NullPointerException success) {}
# Line 245 | Line 251 | public class ConcurrentLinkedDequeTest e
251       * addLast(null) throws NPE
252       */
253      public void testAddLastNull() {
254 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
255          try {
249            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
256              q.addLast(null);
257              shouldThrow();
258          } catch (NullPointerException success) {}
# Line 289 | Line 295 | public class ConcurrentLinkedDequeTest e
295       * addAll(null) throws NPE
296       */
297      public void testAddAll1() {
298 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
299          try {
293            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
300              q.addAll(null);
301              shouldThrow();
302          } catch (NullPointerException success) {}
# Line 300 | Line 306 | public class ConcurrentLinkedDequeTest e
306       * addAll(this) throws IAE
307       */
308      public void testAddAllSelf() {
309 +        ConcurrentLinkedDeque q = populatedDeque(SIZE);
310          try {
304            ConcurrentLinkedDeque q = populatedDeque(SIZE);
311              q.addAll(q);
312              shouldThrow();
313          } catch (IllegalArgumentException success) {}
# Line 311 | Line 317 | public class ConcurrentLinkedDequeTest e
317       * addAll of a collection with null elements throws NPE
318       */
319      public void testAddAll2() {
320 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
321          try {
322 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
316 <            Integer[] ints = new Integer[SIZE];
317 <            q.addAll(Arrays.asList(ints));
322 >            q.addAll(Arrays.asList(new Integer[SIZE]));
323              shouldThrow();
324          } catch (NullPointerException success) {}
325      }
# Line 324 | Line 329 | public class ConcurrentLinkedDequeTest e
329       * possibly adding some elements
330       */
331      public void testAddAll3() {
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);
336          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);
337              q.addAll(Arrays.asList(ints));
338              shouldThrow();
339          } catch (NullPointerException success) {}
# Line 365 | Line 370 | public class ConcurrentLinkedDequeTest e
370       */
371      public void testPollLast() {
372          ConcurrentLinkedDeque q = populatedDeque(SIZE);
373 <        for (int i = SIZE-1; i >= 0; --i) {
373 >        for (int i = SIZE - 1; i >= 0; --i) {
374              assertEquals(i, q.pollLast());
375          }
376          assertNull(q.pollLast());
# Line 430 | Line 435 | public class ConcurrentLinkedDequeTest e
435       */
436      public void testRemoveElement() {
437          ConcurrentLinkedDeque q = populatedDeque(SIZE);
438 <        for (int i = 1; i < SIZE; i+=2) {
438 >        for (int i = 1; i < SIZE; i += 2) {
439              assertTrue(q.contains(i));
440              assertTrue(q.remove(i));
441              assertFalse(q.contains(i));
442 <            assertTrue(q.contains(i-1));
442 >            assertTrue(q.contains(i - 1));
443          }
444 <        for (int i = 0; i < SIZE; i+=2) {
444 >        for (int i = 0; i < SIZE; i += 2) {
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));
448 >            assertFalse(q.remove(i + 1));
449 >            assertFalse(q.contains(i + 1));
450          }
451          assertTrue(q.isEmpty());
452      }
# Line 465 | Line 470 | public class ConcurrentLinkedDequeTest e
470       */
471      public void testPeekLast() {
472          ConcurrentLinkedDeque q = populatedDeque(SIZE);
473 <        for (int i = SIZE-1; i >= 0; --i) {
473 >        for (int i = SIZE - 1; i >= 0; --i) {
474              assertEquals(i, q.peekLast());
475              assertEquals(i, q.pollLast());
476              assertTrue(q.peekLast() == null ||
# Line 494 | Line 499 | public class ConcurrentLinkedDequeTest e
499       */
500      public void testLastElement() {
501          ConcurrentLinkedDeque q = populatedDeque(SIZE);
502 <        for (int i = SIZE-1; i >= 0; --i) {
502 >        for (int i = SIZE - 1; i >= 0; --i) {
503              assertEquals(i, q.getLast());
504              assertEquals(i, q.pollLast());
505          }
# Line 540 | Line 545 | public class ConcurrentLinkedDequeTest e
545       */
546      public void testRemoveFirstOccurrence() {
547          ConcurrentLinkedDeque q = populatedDeque(SIZE);
548 <        for (int i = 1; i < SIZE; i+=2) {
548 >        for (int i = 1; i < SIZE; i += 2) {
549              assertTrue(q.removeFirstOccurrence(new Integer(i)));
550          }
551 <        for (int i = 0; i < SIZE; i+=2) {
551 >        for (int i = 0; i < SIZE; i += 2) {
552              assertTrue(q.removeFirstOccurrence(new Integer(i)));
553 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
553 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
554          }
555          assertTrue(q.isEmpty());
556      }
# Line 555 | Line 560 | public class ConcurrentLinkedDequeTest e
560       */
561      public void testRemoveLastOccurrence() {
562          ConcurrentLinkedDeque q = populatedDeque(SIZE);
563 <        for (int i = 1; i < SIZE; i+=2) {
563 >        for (int i = 1; i < SIZE; i += 2) {
564              assertTrue(q.removeLastOccurrence(new Integer(i)));
565          }
566 <        for (int i = 0; i < SIZE; i+=2) {
566 >        for (int i = 0; i < SIZE; i += 2) {
567              assertTrue(q.removeLastOccurrence(new Integer(i)));
568 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
568 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
569          }
570          assertTrue(q.isEmpty());
571      }
# Line 619 | Line 624 | public class ConcurrentLinkedDequeTest e
624                  assertTrue(changed);
625  
626              assertTrue(q.containsAll(p));
627 <            assertEquals(SIZE-i, q.size());
627 >            assertEquals(SIZE - i, q.size());
628              p.remove();
629          }
630      }
# Line 632 | Line 637 | public class ConcurrentLinkedDequeTest e
637              ConcurrentLinkedDeque q = populatedDeque(SIZE);
638              ConcurrentLinkedDeque p = populatedDeque(i);
639              assertTrue(q.removeAll(p));
640 <            assertEquals(SIZE-i, q.size());
640 >            assertEquals(SIZE - i, q.size());
641              for (int j = 0; j < i; ++j) {
642 <                Integer I = (Integer)(p.remove());
643 <                assertFalse(q.contains(I));
642 >                Integer x = (Integer)(p.remove());
643 >                assertFalse(q.contains(x));
644              }
645          }
646      }
# Line 689 | Line 694 | public class ConcurrentLinkedDequeTest e
694       */
695      public void testIterator() {
696          ConcurrentLinkedDeque q = populatedDeque(SIZE);
692        int i = 0;
697          Iterator it = q.iterator();
698 <        while (it.hasNext()) {
698 >        int i;
699 >        for (i = 0; it.hasNext(); i++)
700              assertTrue(q.contains(it.next()));
696            ++i;
697        }
701          assertEquals(i, SIZE);
702 +        assertIteratorExhausted(it);
703 +    }
704 +
705 +    /**
706 +     * iterator of empty collection has no elements
707 +     */
708 +    public void testEmptyIterator() {
709 +        Deque c = new ConcurrentLinkedDeque();
710 +        assertIteratorExhausted(c.iterator());
711 +        assertIteratorExhausted(c.descendingIterator());
712      }
713  
714      /**
# Line 740 | Line 753 | public class ConcurrentLinkedDequeTest e
753          final Random rng = new Random();
754          for (int iters = 0; iters < 100; ++iters) {
755              int max = rng.nextInt(5) + 2;
756 <            int split = rng.nextInt(max-1) + 1;
756 >            int split = rng.nextInt(max - 1) + 1;
757              for (int j = 1; j <= max; ++j)
758                  q.add(new Integer(j));
759              Iterator it = q.iterator();
760              for (int j = 1; j <= split; ++j)
761                  assertEquals(it.next(), new Integer(j));
762              it.remove();
763 <            assertEquals(it.next(), new Integer(split+1));
763 >            assertEquals(it.next(), new Integer(split + 1));
764              for (int j = 1; j <= split; ++j)
765                  q.remove(new Integer(j));
766              it = q.iterator();
767 <            for (int j = split+1; j <= max; ++j) {
767 >            for (int j = split + 1; j <= max; ++j) {
768                  assertEquals(it.next(), new Integer(j));
769                  it.remove();
770              }
# Line 808 | Line 821 | public class ConcurrentLinkedDequeTest e
821          final Random rng = new Random();
822          for (int iters = 0; iters < 100; ++iters) {
823              int max = rng.nextInt(5) + 2;
824 <            int split = rng.nextInt(max-1) + 1;
824 >            int split = rng.nextInt(max - 1) + 1;
825              for (int j = max; j >= 1; --j)
826                  q.add(new Integer(j));
827              Iterator it = q.descendingIterator();
828              for (int j = 1; j <= split; ++j)
829                  assertEquals(it.next(), new Integer(j));
830              it.remove();
831 <            assertEquals(it.next(), new Integer(split+1));
831 >            assertEquals(it.next(), new Integer(split + 1));
832              for (int j = 1; j <= split; ++j)
833                  q.remove(new Integer(j));
834              it = q.descendingIterator();
835 <            for (int j = split+1; j <= max; ++j) {
835 >            for (int j = split + 1; j <= max; ++j) {
836                  assertEquals(it.next(), new Integer(j));
837                  it.remove();
838              }
# Line 843 | Line 856 | public class ConcurrentLinkedDequeTest e
856       * A deserialized serialized deque has same elements in same order
857       */
858      public void testSerialization() throws Exception {
859 <        ConcurrentLinkedDeque q = populatedDeque(SIZE);
860 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
861 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
862 <        out.writeObject(q);
863 <        out.close();
864 <
865 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
866 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
867 <        ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject();
868 <        assertEquals(q.size(), r.size());
869 <        while (!q.isEmpty())
870 <            assertEquals(q.remove(), r.remove());
859 >        Queue x = populatedDeque(SIZE);
860 >        Queue y = serialClone(x);
861 >
862 >        assertNotSame(x, y);
863 >        assertEquals(x.size(), y.size());
864 >        assertEquals(x.toString(), y.toString());
865 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
866 >        while (!x.isEmpty()) {
867 >            assertFalse(y.isEmpty());
868 >            assertEquals(x.remove(), y.remove());
869 >        }
870 >        assertTrue(y.isEmpty());
871      }
872  
873 +    /**
874 +     * contains(null) always return false.
875 +     * remove(null) always throws NullPointerException.
876 +     */
877 +    public void testNeverContainsNull() {
878 +        Deque<?>[] qs = {
879 +            new ConcurrentLinkedDeque<Object>(),
880 +            populatedDeque(2),
881 +        };
882 +
883 +        for (Deque<?> q : qs) {
884 +            assertFalse(q.contains(null));
885 +            try {
886 +                assertFalse(q.remove(null));
887 +                shouldThrow();
888 +            } catch (NullPointerException success) {}
889 +            try {
890 +                assertFalse(q.removeFirstOccurrence(null));
891 +                shouldThrow();
892 +            } catch (NullPointerException success) {}
893 +            try {
894 +                assertFalse(q.removeLastOccurrence(null));
895 +                shouldThrow();
896 +            } catch (NullPointerException success) {}
897 +        }
898 +    }
899   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines