ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ArrayDequeTest.java (file contents):
Revision 1.14 by jsr166, Wed Dec 23 00:47:16 2009 UTC vs.
Revision 1.52 by jsr166, Sun Oct 30 21:07:27 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
2 > * Written by Doug Lea and Martin Buchholz with assistance from
3 > * members of JCP JSR-166 Expert Group and released to the public
4 > * domain, as explained at
5 > * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
9 < import java.util.*;
10 < import java.util.concurrent.*;
8 > import java.util.ArrayDeque;
9 > import java.util.Arrays;
10 > import java.util.Collection;
11 > import java.util.Collections;
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.ThreadLocalRandom;
18 >
19 > import junit.framework.Test;
20 > import junit.framework.TestSuite;
21  
22   public class ArrayDequeTest extends JSR166TestCase {
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(ArrayDequeTest.class);
28 >        class Implementation implements CollectionImplementation {
29 >            public Class<?> klazz() { return ArrayDeque.class; }
30 >            public Collection emptyCollection() { return populatedDeque(0); }
31 >            public Object makeElement(int i) { return i; }
32 >            public boolean isConcurrent() { return false; }
33 >            public boolean permitsNulls() { return false; }
34 >        }
35 >        return newTestSuite(ArrayDequeTest.class,
36 >                            CollectionTest.testSuite(new Implementation()));
37      }
38  
39      /**
40 <     * Create a queue of given size containing consecutive
41 <     * Integers 0 ... n.
42 <     */
43 <    private ArrayDeque populatedDeque(int n) {
44 <        ArrayDeque q = new ArrayDeque();
40 >     * Returns a new deque of given size containing consecutive
41 >     * Integers 0 ... n - 1.
42 >     */
43 >    private static ArrayDeque<Integer> populatedDeque(int n) {
44 >        // Randomize various aspects of memory layout, including
45 >        // filled-to-capacity and wraparound.
46 >        final ArrayDeque<Integer> q;
47 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
48 >        switch (rnd.nextInt(6)) {
49 >        case 0: q = new ArrayDeque<Integer>();      break;
50 >        case 1: q = new ArrayDeque<Integer>(0);     break;
51 >        case 2: q = new ArrayDeque<Integer>(1);     break;
52 >        case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break;
53 >        case 4: q = new ArrayDeque<Integer>(n);     break;
54 >        case 5: q = new ArrayDeque<Integer>(n + 1); break;
55 >        default: throw new AssertionError();
56 >        }
57 >        switch (rnd.nextInt(3)) {
58 >        case 0:
59 >            q.addFirst(42);
60 >            assertEquals((Integer) 42, q.removeLast());
61 >            break;
62 >        case 1:
63 >            q.addLast(42);
64 >            assertEquals((Integer) 42, q.removeFirst());
65 >            break;
66 >        case 2: /* do nothing */ break;
67 >        default: throw new AssertionError();
68 >        }
69          assertTrue(q.isEmpty());
70 <        for (int i = 0; i < n; ++i)
71 <            assertTrue(q.offerLast(new Integer(i)));
72 <        assertFalse(q.isEmpty());
70 >        if (rnd.nextBoolean())
71 >            for (int i = 0; i < n; i++)
72 >                assertTrue(q.offerLast((Integer) i));
73 >        else
74 >            for (int i = n; --i >= 0; )
75 >                q.addFirst((Integer) i);
76          assertEquals(n, q.size());
77 +        if (n > 0) {
78 +            assertFalse(q.isEmpty());
79 +            assertEquals((Integer) 0, q.peekFirst());
80 +            assertEquals((Integer) (n - 1), q.peekLast());
81 +        }
82          return q;
83      }
84  
85      /**
86 <     * new queue is empty
86 >     * new deque is empty
87       */
88      public void testConstructor1() {
89          assertEquals(0, new ArrayDeque().size());
# Line 43 | Line 94 | public class ArrayDequeTest extends JSR1
94       */
95      public void testConstructor3() {
96          try {
97 <            ArrayDeque q = new ArrayDeque((Collection)null);
97 >            new ArrayDeque((Collection)null);
98 >            shouldThrow();
99 >        } catch (NullPointerException success) {}
100 >    }
101 >
102 >    /**
103 >     * Initializing from Collection of null elements throws NPE
104 >     */
105 >    public void testConstructor4() {
106 >        try {
107 >            new ArrayDeque(Arrays.asList(new Integer[SIZE]));
108              shouldThrow();
109          } catch (NullPointerException success) {}
110      }
111  
112      /**
113 <     * Queue contains all elements of collection used to initialize
113 >     * Initializing from Collection with some null elements throws NPE
114 >     */
115 >    public void testConstructor5() {
116 >        Integer[] ints = new Integer[SIZE];
117 >        for (int i = 0; i < SIZE - 1; ++i)
118 >            ints[i] = new Integer(i);
119 >        try {
120 >            new ArrayDeque(Arrays.asList(ints));
121 >            shouldThrow();
122 >        } catch (NullPointerException success) {}
123 >    }
124  
125 +    /**
126 +     * Deque contains all elements of collection used to initialize
127       */
128      public void testConstructor6() {
129          Integer[] ints = new Integer[SIZE];
# Line 81 | Line 154 | public class ArrayDequeTest extends JSR1
154      public void testSize() {
155          ArrayDeque q = populatedDeque(SIZE);
156          for (int i = 0; i < SIZE; ++i) {
157 <            assertEquals(SIZE-i, q.size());
157 >            assertEquals(SIZE - i, q.size());
158              q.removeFirst();
159          }
160          for (int i = 0; i < SIZE; ++i) {
# Line 94 | Line 167 | public class ArrayDequeTest extends JSR1
167       * push(null) throws NPE
168       */
169      public void testPushNull() {
170 +        ArrayDeque q = new ArrayDeque(1);
171          try {
98            ArrayDeque q = new ArrayDeque(1);
172              q.push(null);
173              shouldThrow();
174          } catch (NullPointerException success) {}
175      }
176  
177      /**
178 <     * peekFirst returns element inserted with push
178 >     * peekFirst() returns element inserted with push
179       */
180      public void testPush() {
181          ArrayDeque q = populatedDeque(3);
# Line 112 | Line 185 | public class ArrayDequeTest extends JSR1
185      }
186  
187      /**
188 <     *  pop removes next element, or throws NSEE if empty
188 >     * pop() removes next element, or throws NSEE if empty
189       */
190      public void testPop() {
191          ArrayDeque q = populatedDeque(SIZE);
# Line 128 | Line 201 | public class ArrayDequeTest extends JSR1
201      /**
202       * offer(null) throws NPE
203       */
204 +    public void testOfferNull() {
205 +        ArrayDeque q = new ArrayDeque();
206 +        try {
207 +            q.offer(null);
208 +            shouldThrow();
209 +        } catch (NullPointerException success) {}
210 +    }
211 +
212 +    /**
213 +     * offerFirst(null) throws NPE
214 +     */
215      public void testOfferFirstNull() {
216 +        ArrayDeque q = new ArrayDeque();
217          try {
133            ArrayDeque q = new ArrayDeque();
218              q.offerFirst(null);
219              shouldThrow();
220          } catch (NullPointerException success) {}
221      }
222  
223      /**
224 <     * OfferFirst succeeds
224 >     * offerLast(null) throws NPE
225 >     */
226 >    public void testOfferLastNull() {
227 >        ArrayDeque q = new ArrayDeque();
228 >        try {
229 >            q.offerLast(null);
230 >            shouldThrow();
231 >        } catch (NullPointerException success) {}
232 >    }
233 >
234 >    /**
235 >     * offer(x) succeeds
236 >     */
237 >    public void testOffer() {
238 >        ArrayDeque q = new ArrayDeque();
239 >        assertTrue(q.offer(zero));
240 >        assertTrue(q.offer(one));
241 >        assertSame(zero, q.peekFirst());
242 >        assertSame(one, q.peekLast());
243 >    }
244 >
245 >    /**
246 >     * offerFirst(x) succeeds
247       */
248      public void testOfferFirst() {
249          ArrayDeque q = new ArrayDeque();
250 <        assertTrue(q.offerFirst(new Integer(0)));
251 <        assertTrue(q.offerFirst(new Integer(1)));
250 >        assertTrue(q.offerFirst(zero));
251 >        assertTrue(q.offerFirst(one));
252 >        assertSame(one, q.peekFirst());
253 >        assertSame(zero, q.peekLast());
254      }
255  
256      /**
257 <     * OfferLast succeeds
257 >     * offerLast(x) succeeds
258       */
259      public void testOfferLast() {
260          ArrayDeque q = new ArrayDeque();
261 <        assertTrue(q.offerLast(new Integer(0)));
262 <        assertTrue(q.offerLast(new Integer(1)));
261 >        assertTrue(q.offerLast(zero));
262 >        assertTrue(q.offerLast(one));
263 >        assertSame(zero, q.peekFirst());
264 >        assertSame(one, q.peekLast());
265      }
266  
267      /**
268 <     * add succeeds
268 >     * add(null) throws NPE
269 >     */
270 >    public void testAddNull() {
271 >        ArrayDeque q = new ArrayDeque();
272 >        try {
273 >            q.add(null);
274 >            shouldThrow();
275 >        } catch (NullPointerException success) {}
276 >    }
277 >
278 >    /**
279 >     * addFirst(null) throws NPE
280 >     */
281 >    public void testAddFirstNull() {
282 >        ArrayDeque q = new ArrayDeque();
283 >        try {
284 >            q.addFirst(null);
285 >            shouldThrow();
286 >        } catch (NullPointerException success) {}
287 >    }
288 >
289 >    /**
290 >     * addLast(null) throws NPE
291 >     */
292 >    public void testAddLastNull() {
293 >        ArrayDeque q = new ArrayDeque();
294 >        try {
295 >            q.addLast(null);
296 >            shouldThrow();
297 >        } catch (NullPointerException success) {}
298 >    }
299 >
300 >    /**
301 >     * add(x) succeeds
302       */
303      public void testAdd() {
304          ArrayDeque q = new ArrayDeque();
305 <        for (int i = 0; i < SIZE; ++i) {
306 <            assertEquals(i, q.size());
307 <            assertTrue(q.add(new Integer(i)));
308 <        }
305 >        assertTrue(q.add(zero));
306 >        assertTrue(q.add(one));
307 >        assertSame(zero, q.peekFirst());
308 >        assertSame(one, q.peekLast());
309 >    }
310 >
311 >    /**
312 >     * addFirst(x) succeeds
313 >     */
314 >    public void testAddFirst() {
315 >        ArrayDeque q = new ArrayDeque();
316 >        q.addFirst(zero);
317 >        q.addFirst(one);
318 >        assertSame(one, q.peekFirst());
319 >        assertSame(zero, q.peekLast());
320 >    }
321 >
322 >    /**
323 >     * addLast(x) succeeds
324 >     */
325 >    public void testAddLast() {
326 >        ArrayDeque q = new ArrayDeque();
327 >        q.addLast(zero);
328 >        q.addLast(one);
329 >        assertSame(zero, q.peekFirst());
330 >        assertSame(one, q.peekLast());
331      }
332  
333      /**
334       * addAll(null) throws NPE
335       */
336      public void testAddAll1() {
337 +        ArrayDeque q = new ArrayDeque();
338          try {
173            ArrayDeque q = new ArrayDeque();
339              q.addAll(null);
340              shouldThrow();
341          } catch (NullPointerException success) {}
342      }
343  
344      /**
345 <     * Queue contains all elements, in traversal order, of successful addAll
345 >     * addAll of a collection with null elements throws NPE
346 >     */
347 >    public void testAddAll2() {
348 >        ArrayDeque q = new ArrayDeque();
349 >        try {
350 >            q.addAll(Arrays.asList(new Integer[SIZE]));
351 >            shouldThrow();
352 >        } catch (NullPointerException success) {}
353 >    }
354 >
355 >    /**
356 >     * addAll of a collection with any null elements throws NPE after
357 >     * possibly adding some elements
358 >     */
359 >    public void testAddAll3() {
360 >        ArrayDeque q = new ArrayDeque();
361 >        Integer[] ints = new Integer[SIZE];
362 >        for (int i = 0; i < SIZE - 1; ++i)
363 >            ints[i] = new Integer(i);
364 >        try {
365 >            q.addAll(Arrays.asList(ints));
366 >            shouldThrow();
367 >        } catch (NullPointerException success) {}
368 >    }
369 >
370 >    /**
371 >     * Deque contains all elements, in traversal order, of successful addAll
372       */
373      public void testAddAll5() {
374          Integer[] empty = new Integer[0];
# Line 192 | Line 383 | public class ArrayDequeTest extends JSR1
383      }
384  
385      /**
386 <     *  pollFirst succeeds unless empty
386 >     * pollFirst() succeeds unless empty
387       */
388      public void testPollFirst() {
389          ArrayDeque q = populatedDeque(SIZE);
# Line 203 | Line 394 | public class ArrayDequeTest extends JSR1
394      }
395  
396      /**
397 <     *  pollLast succeeds unless empty
397 >     * pollLast() succeeds unless empty
398       */
399      public void testPollLast() {
400          ArrayDeque q = populatedDeque(SIZE);
401 <        for (int i = SIZE-1; i >= 0; --i) {
401 >        for (int i = SIZE - 1; i >= 0; --i) {
402              assertEquals(i, q.pollLast());
403          }
404          assertNull(q.pollLast());
405      }
406  
407      /**
408 <     *  poll succeeds unless empty
408 >     * poll() succeeds unless empty
409       */
410      public void testPoll() {
411          ArrayDeque q = populatedDeque(SIZE);
# Line 225 | Line 416 | public class ArrayDequeTest extends JSR1
416      }
417  
418      /**
419 <     *  remove removes next element, or throws NSEE if empty
419 >     * remove() removes next element, or throws NSEE if empty
420       */
421      public void testRemove() {
422          ArrayDeque q = populatedDeque(SIZE);
# Line 239 | Line 430 | public class ArrayDequeTest extends JSR1
430      }
431  
432      /**
433 <     *  peekFirst returns next element, or null if empty
433 >     * remove(x) removes x and returns true if present
434 >     */
435 >    public void testRemoveElement() {
436 >        ArrayDeque q = populatedDeque(SIZE);
437 >        for (int i = 1; i < SIZE; i += 2) {
438 >            assertTrue(q.contains(i));
439 >            assertTrue(q.remove(i));
440 >            assertFalse(q.contains(i));
441 >            assertTrue(q.contains(i - 1));
442 >        }
443 >        for (int i = 0; i < SIZE; i += 2) {
444 >            assertTrue(q.contains(i));
445 >            assertTrue(q.remove(i));
446 >            assertFalse(q.contains(i));
447 >            assertFalse(q.remove(i + 1));
448 >            assertFalse(q.contains(i + 1));
449 >        }
450 >        assertTrue(q.isEmpty());
451 >    }
452 >
453 >    /**
454 >     * peekFirst() returns next element, or null if empty
455       */
456      public void testPeekFirst() {
457          ArrayDeque q = populatedDeque(SIZE);
# Line 253 | Line 465 | public class ArrayDequeTest extends JSR1
465      }
466  
467      /**
468 <     *  peek returns next element, or null if empty
468 >     * peek() returns next element, or null if empty
469       */
470      public void testPeek() {
471          ArrayDeque q = populatedDeque(SIZE);
# Line 267 | Line 479 | public class ArrayDequeTest extends JSR1
479      }
480  
481      /**
482 <     *  peekLast returns next element, or null if empty
482 >     * peekLast() returns next element, or null if empty
483       */
484      public void testPeekLast() {
485          ArrayDeque q = populatedDeque(SIZE);
486 <        for (int i = SIZE-1; i >= 0; --i) {
486 >        for (int i = SIZE - 1; i >= 0; --i) {
487              assertEquals(i, q.peekLast());
488              assertEquals(i, q.pollLast());
489              assertTrue(q.peekLast() == null ||
# Line 281 | Line 493 | public class ArrayDequeTest extends JSR1
493      }
494  
495      /**
496 <     * getFirst returns next getFirst, or throws NSEE if empty
496 >     * element() returns first element, or throws NSEE if empty
497 >     */
498 >    public void testElement() {
499 >        ArrayDeque q = populatedDeque(SIZE);
500 >        for (int i = 0; i < SIZE; ++i) {
501 >            assertEquals(i, q.element());
502 >            assertEquals(i, q.poll());
503 >        }
504 >        try {
505 >            q.element();
506 >            shouldThrow();
507 >        } catch (NoSuchElementException success) {}
508 >    }
509 >
510 >    /**
511 >     * getFirst() returns first element, or throws NSEE if empty
512       */
513      public void testFirstElement() {
514          ArrayDeque q = populatedDeque(SIZE);
# Line 296 | Line 523 | public class ArrayDequeTest extends JSR1
523      }
524  
525      /**
526 <     *  getLast returns next element, or throws NSEE if empty
526 >     * getLast() returns last element, or throws NSEE if empty
527       */
528      public void testLastElement() {
529          ArrayDeque q = populatedDeque(SIZE);
530 <        for (int i = SIZE-1; i >= 0; --i) {
530 >        for (int i = SIZE - 1; i >= 0; --i) {
531              assertEquals(i, q.getLast());
532              assertEquals(i, q.pollLast());
533          }
# Line 311 | Line 538 | public class ArrayDequeTest extends JSR1
538          assertNull(q.peekLast());
539      }
540  
314
541      /**
542 <     *  removeFirst removes next element, or throws NSEE if empty
542 >     * removeFirst() removes first element, or throws NSEE if empty
543       */
544      public void testRemoveFirst() {
545          ArrayDeque q = populatedDeque(SIZE);
# Line 324 | Line 550 | public class ArrayDequeTest extends JSR1
550              q.removeFirst();
551              shouldThrow();
552          } catch (NoSuchElementException success) {}
553 +        assertNull(q.peekFirst());
554 +    }
555 +
556 +    /**
557 +     * removeLast() removes last element, or throws NSEE if empty
558 +     */
559 +    public void testRemoveLast() {
560 +        ArrayDeque q = populatedDeque(SIZE);
561 +        for (int i = SIZE - 1; i >= 0; --i) {
562 +            assertEquals(i, q.removeLast());
563 +        }
564 +        try {
565 +            q.removeLast();
566 +            shouldThrow();
567 +        } catch (NoSuchElementException success) {}
568 +        assertNull(q.peekLast());
569      }
570  
571      /**
# Line 331 | Line 573 | public class ArrayDequeTest extends JSR1
573       */
574      public void testRemoveFirstOccurrence() {
575          ArrayDeque q = populatedDeque(SIZE);
576 <        for (int i = 1; i < SIZE; i+=2) {
576 >        assertFalse(q.removeFirstOccurrence(null));
577 >        for (int i = 1; i < SIZE; i += 2) {
578              assertTrue(q.removeFirstOccurrence(new Integer(i)));
579          }
580 <        for (int i = 0; i < SIZE; i+=2) {
580 >        for (int i = 0; i < SIZE; i += 2) {
581              assertTrue(q.removeFirstOccurrence(new Integer(i)));
582 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
582 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
583          }
584          assertTrue(q.isEmpty());
585 +        assertFalse(q.removeFirstOccurrence(null));
586 +        assertFalse(q.removeFirstOccurrence(42));
587 +        q = new ArrayDeque();
588 +        assertFalse(q.removeFirstOccurrence(null));
589 +        assertFalse(q.removeFirstOccurrence(42));
590      }
591  
592      /**
# Line 346 | Line 594 | public class ArrayDequeTest extends JSR1
594       */
595      public void testRemoveLastOccurrence() {
596          ArrayDeque q = populatedDeque(SIZE);
597 <        for (int i = 1; i < SIZE; i+=2) {
597 >        assertFalse(q.removeLastOccurrence(null));
598 >        for (int i = 1; i < SIZE; i += 2) {
599              assertTrue(q.removeLastOccurrence(new Integer(i)));
600          }
601 <        for (int i = 0; i < SIZE; i+=2) {
601 >        for (int i = 0; i < SIZE; i += 2) {
602              assertTrue(q.removeLastOccurrence(new Integer(i)));
603 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
603 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
604          }
605          assertTrue(q.isEmpty());
606 +        assertFalse(q.removeLastOccurrence(null));
607 +        assertFalse(q.removeLastOccurrence(42));
608 +        q = new ArrayDeque();
609 +        assertFalse(q.removeLastOccurrence(null));
610 +        assertFalse(q.removeLastOccurrence(42));
611      }
612  
613      /**
# Line 406 | Line 660 | public class ArrayDequeTest extends JSR1
660              boolean changed = q.retainAll(p);
661              assertEquals(changed, (i > 0));
662              assertTrue(q.containsAll(p));
663 <            assertEquals(SIZE-i, q.size());
663 >            assertEquals(SIZE - i, q.size());
664              p.removeFirst();
665          }
666      }
# Line 419 | Line 673 | public class ArrayDequeTest extends JSR1
673              ArrayDeque q = populatedDeque(SIZE);
674              ArrayDeque p = populatedDeque(i);
675              assertTrue(q.removeAll(p));
676 <            assertEquals(SIZE-i, q.size());
676 >            assertEquals(SIZE - i, q.size());
677              for (int j = 0; j < i; ++j) {
678                  assertFalse(q.contains(p.removeFirst()));
679              }
680          }
681      }
682  
683 <    /**
684 <     *  toArray contains all elements
685 <     */
686 <    public void testToArray() {
687 <        ArrayDeque q = populatedDeque(SIZE);
688 <        Object[] o = q.toArray();
689 <        Arrays.sort(o);
690 <        for (int i = 0; i < o.length; i++)
691 <            assertEquals(o[i], q.pollFirst());
683 >    void checkToArray(ArrayDeque<Integer> q) {
684 >        int size = q.size();
685 >        Object[] a1 = q.toArray();
686 >        assertEquals(size, a1.length);
687 >        Integer[] a2 = q.toArray(new Integer[0]);
688 >        assertEquals(size, a2.length);
689 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
690 >        assertEquals(size, a3.length);
691 >        Integer[] a4 = new Integer[size];
692 >        assertSame(a4, q.toArray(a4));
693 >        Integer[] a5 = new Integer[size + 1];
694 >        Arrays.fill(a5, 42);
695 >        assertSame(a5, q.toArray(a5));
696 >        Integer[] a6 = new Integer[size + 2];
697 >        Arrays.fill(a6, 42);
698 >        assertSame(a6, q.toArray(a6));
699 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
700 >        for (Object[] a : as) {
701 >            if (a.length > size) assertNull(a[size]);
702 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
703 >        }
704 >        Iterator it = q.iterator();
705 >        Integer s = q.peekFirst();
706 >        for (int i = 0; i < size; i++) {
707 >            Integer x = (Integer) it.next();
708 >            assertEquals(s + i, (int) x);
709 >            for (Object[] a : as)
710 >                assertSame(a1[i], x);
711 >        }
712      }
713  
714      /**
715 <     *  toArray(a) contains all elements
715 >     * toArray() and toArray(a) contain all elements in FIFO order
716       */
717 <    public void testToArray2() {
718 <        ArrayDeque q = populatedDeque(SIZE);
719 <        Integer[] ints = new Integer[SIZE];
720 <        ints = (Integer[])q.toArray(ints);
721 <        Arrays.sort(ints);
722 <        for (int i = 0; i < ints.length; i++)
723 <            assertEquals(ints[i], q.pollFirst());
717 >    public void testToArray() {
718 >        final int size = ThreadLocalRandom.current().nextInt(10);
719 >        ArrayDeque<Integer> q = new ArrayDeque<>(size);
720 >        for (int i = 0; i < size; i++) {
721 >            checkToArray(q);
722 >            q.addLast(i);
723 >        }
724 >        // Provoke wraparound
725 >        int added = size * 2;
726 >        for (int i = 0; i < added; i++) {
727 >            checkToArray(q);
728 >            assertEquals((Integer) i, q.poll());
729 >            q.addLast(size + i);
730 >        }
731 >        for (int i = 0; i < size; i++) {
732 >            checkToArray(q);
733 >            assertEquals((Integer) (added + i), q.poll());
734 >        }
735      }
736  
737      /**
738 <     * toArray(null) throws NPE
738 >     * toArray(null) throws NullPointerException
739       */
740 <    public void testToArray_BadArg() {
740 >    public void testToArray_NullArg() {
741          ArrayDeque l = new ArrayDeque();
742          l.add(new Object());
743          try {
744 <            Object o[] = l.toArray(null);
744 >            l.toArray(null);
745              shouldThrow();
746          } catch (NullPointerException success) {}
747      }
748  
749      /**
750 <     * toArray with incompatible aray type throws CCE
750 >     * toArray(incompatible array type) throws ArrayStoreException
751       */
752 <    public void testToArray1_BadArg() {
752 >    public void testToArray_incompatibleArrayType() {
753          ArrayDeque l = new ArrayDeque();
754          l.add(new Integer(5));
755          try {
756 <            Object o[] = l.toArray(new String[10]);
756 >            l.toArray(new String[10]);
757 >            shouldThrow();
758 >        } catch (ArrayStoreException success) {}
759 >        try {
760 >            l.toArray(new String[0]);
761              shouldThrow();
762          } catch (ArrayStoreException success) {}
763      }
764  
765      /**
766 <     *  iterator iterates through all elements
766 >     * Iterator iterates through all elements
767       */
768      public void testIterator() {
769          ArrayDeque q = populatedDeque(SIZE);
481        int i = 0;
770          Iterator it = q.iterator();
771 <        while (it.hasNext()) {
771 >        int i;
772 >        for (i = 0; it.hasNext(); i++)
773              assertTrue(q.contains(it.next()));
485            ++i;
486        }
774          assertEquals(i, SIZE);
775 +        assertIteratorExhausted(it);
776      }
777  
778      /**
779 <     *  iterator ordering is FIFO
779 >     * iterator of empty collection has no elements
780 >     */
781 >    public void testEmptyIterator() {
782 >        Deque c = new ArrayDeque();
783 >        assertIteratorExhausted(c.iterator());
784 >        assertIteratorExhausted(c.descendingIterator());
785 >    }
786 >
787 >    /**
788 >     * Iterator ordering is FIFO
789       */
790      public void testIteratorOrdering() {
791          final ArrayDeque q = new ArrayDeque();
792 <        q.add(new Integer(1));
793 <        q.add(new Integer(2));
794 <        q.add(new Integer(3));
792 >        q.add(one);
793 >        q.add(two);
794 >        q.add(three);
795          int k = 0;
796          for (Iterator it = q.iterator(); it.hasNext();) {
797              assertEquals(++k, it.next());
# Line 504 | Line 801 | public class ArrayDequeTest extends JSR1
801      }
802  
803      /**
804 <     * iterator.remove removes current element
804 >     * iterator.remove() removes current element
805       */
806 <    public void testIteratorRemove () {
806 >    public void testIteratorRemove() {
807          final ArrayDeque q = new ArrayDeque();
808          final Random rng = new Random();
809          for (int iters = 0; iters < 100; ++iters) {
810              int max = rng.nextInt(5) + 2;
811 <            int split = rng.nextInt(max-1) + 1;
811 >            int split = rng.nextInt(max - 1) + 1;
812              for (int j = 1; j <= max; ++j)
813                  q.add(new Integer(j));
814              Iterator it = q.iterator();
815              for (int j = 1; j <= split; ++j)
816                  assertEquals(it.next(), new Integer(j));
817              it.remove();
818 <            assertEquals(it.next(), new Integer(split+1));
818 >            assertEquals(it.next(), new Integer(split + 1));
819              for (int j = 1; j <= split; ++j)
820                  q.remove(new Integer(j));
821              it = q.iterator();
822 <            for (int j = split+1; j <= max; ++j) {
822 >            for (int j = split + 1; j <= max; ++j) {
823                  assertEquals(it.next(), new Integer(j));
824                  it.remove();
825              }
# Line 532 | Line 829 | public class ArrayDequeTest extends JSR1
829      }
830  
831      /**
832 <     *  Descending iterator iterates through all elements
832 >     * Descending iterator iterates through all elements
833       */
834      public void testDescendingIterator() {
835          ArrayDeque q = populatedDeque(SIZE);
# Line 551 | Line 848 | public class ArrayDequeTest extends JSR1
848      }
849  
850      /**
851 <     *  Descending iterator ordering is reverse FIFO
851 >     * Descending iterator ordering is reverse FIFO
852       */
853      public void testDescendingIteratorOrdering() {
854          final ArrayDeque q = new ArrayDeque();
# Line 572 | Line 869 | public class ArrayDequeTest extends JSR1
869      }
870  
871      /**
872 <     * descendingIterator.remove removes current element
872 >     * descendingIterator.remove() removes current element
873       */
874 <    public void testDescendingIteratorRemove () {
874 >    public void testDescendingIteratorRemove() {
875          final ArrayDeque q = new ArrayDeque();
876          final Random rng = new Random();
877          for (int iters = 0; iters < 100; ++iters) {
878              int max = rng.nextInt(5) + 2;
879 <            int split = rng.nextInt(max-1) + 1;
879 >            int split = rng.nextInt(max - 1) + 1;
880              for (int j = max; j >= 1; --j)
881                  q.add(new Integer(j));
882              Iterator it = q.descendingIterator();
883              for (int j = 1; j <= split; ++j)
884                  assertEquals(it.next(), new Integer(j));
885              it.remove();
886 <            assertEquals(it.next(), new Integer(split+1));
886 >            assertEquals(it.next(), new Integer(split + 1));
887              for (int j = 1; j <= split; ++j)
888                  q.remove(new Integer(j));
889              it = q.descendingIterator();
890 <            for (int j = split+1; j <= max; ++j) {
890 >            for (int j = split + 1; j <= max; ++j) {
891                  assertEquals(it.next(), new Integer(j));
892                  it.remove();
893              }
# Line 599 | Line 896 | public class ArrayDequeTest extends JSR1
896          }
897      }
898  
602
899      /**
900 <     * toString contains toStrings of elements
900 >     * toString() contains toStrings of elements
901       */
902      public void testToString() {
903          ArrayDeque q = populatedDeque(SIZE);
904          String s = q.toString();
905          for (int i = 0; i < SIZE; ++i) {
906 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
906 >            assertTrue(s.contains(String.valueOf(i)));
907          }
908      }
909  
910      /**
911 <     * peekFirst returns element inserted with addFirst
911 >     * A deserialized serialized deque has same elements in same order
912       */
913 <    public void testAddFirst() {
914 <        ArrayDeque q = populatedDeque(3);
915 <        q.addFirst(four);
916 <        assertSame(four, q.peekFirst());
913 >    public void testSerialization() throws Exception {
914 >        Queue x = populatedDeque(SIZE);
915 >        Queue y = serialClone(x);
916 >
917 >        assertNotSame(y, x);
918 >        assertEquals(x.size(), y.size());
919 >        assertEquals(x.toString(), y.toString());
920 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
921 >        while (!x.isEmpty()) {
922 >            assertFalse(y.isEmpty());
923 >            assertEquals(x.remove(), y.remove());
924 >        }
925 >        assertTrue(y.isEmpty());
926      }
927  
928      /**
929 <     * peekLast returns element inserted with addLast
929 >     * A cloned deque has same elements in same order
930       */
931 <    public void testAddLast() {
932 <        ArrayDeque q = populatedDeque(3);
933 <        q.addLast(four);
934 <        assertSame(four, q.peekLast());
931 >    public void testClone() throws Exception {
932 >        ArrayDeque<Integer> x = populatedDeque(SIZE);
933 >        ArrayDeque<Integer> y = x.clone();
934 >
935 >        assertNotSame(y, x);
936 >        assertEquals(x.size(), y.size());
937 >        assertEquals(x.toString(), y.toString());
938 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
939 >        while (!x.isEmpty()) {
940 >            assertFalse(y.isEmpty());
941 >            assertEquals(x.remove(), y.remove());
942 >        }
943 >        assertTrue(y.isEmpty());
944 >    }
945 >
946 >    /**
947 >     * remove(null), contains(null) always return false
948 >     */
949 >    public void testNeverContainsNull() {
950 >        Deque<?>[] qs = {
951 >            new ArrayDeque<Object>(),
952 >            populatedDeque(2),
953 >        };
954 >
955 >        for (Deque<?> q : qs) {
956 >            assertFalse(q.contains(null));
957 >            assertFalse(q.remove(null));
958 >            assertFalse(q.removeFirstOccurrence(null));
959 >            assertFalse(q.removeLastOccurrence(null));
960 >        }
961      }
962  
963   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines