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.13 by jsr166, Tue Dec 1 09:56:28 2009 UTC vs.
Revision 1.42 by jsr166, Sun Oct 16 22:13:15 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   */
6  
7 < import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
7 > import java.util.ArrayDeque;
8 > import java.util.Arrays;
9 > import java.util.Collection;
10 > import java.util.Deque;
11 > import java.util.Iterator;
12 > import java.util.NoSuchElementException;
13 > import java.util.Queue;
14 > import java.util.Random;
15 > import java.util.concurrent.ThreadLocalRandom;
16 >
17 > import junit.framework.Test;
18 > import junit.framework.TestSuite;
19  
20   public class ArrayDequeTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());
22 >        main(suite(), args);
23      }
24  
25      public static Test suite() {
26 <        return new TestSuite(ArrayDequeTest.class);
26 >        class Implementation implements CollectionImplementation {
27 >            public Class<?> klazz() { return ArrayDeque.class; }
28 >            public Collection emptyCollection() { return new ArrayDeque(); }
29 >            public Object makeElement(int i) { return i; }
30 >            public boolean isConcurrent() { return false; }
31 >            public boolean permitsNulls() { return false; }
32 >        }
33 >        return newTestSuite(ArrayDequeTest.class,
34 >                            CollectionTest.testSuite(new Implementation()));
35      }
36  
37      /**
38 <     * Create a queue of given size containing consecutive
39 <     * Integers 0 ... n.
40 <     */
41 <    private ArrayDeque populatedDeque(int n) {
42 <        ArrayDeque q = new ArrayDeque();
38 >     * Returns a new deque of given size containing consecutive
39 >     * Integers 0 ... n - 1.
40 >     */
41 >    private ArrayDeque<Integer> populatedDeque(int n) {
42 >        // Randomize various aspects of memory layout, including
43 >        // filled-to-capacity and wraparound.
44 >        final ArrayDeque<Integer> q;
45 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
46 >        switch (rnd.nextInt(6)) {
47 >        case 0: q = new ArrayDeque<Integer>(); break;
48 >        case 1: q = new ArrayDeque<Integer>(0); break;
49 >        case 2: q = new ArrayDeque<Integer>(1); break;
50 >        case 3: q = new ArrayDeque<Integer>(n - 1); break;
51 >        case 4: q = new ArrayDeque<Integer>(n); break;
52 >        case 5: q = new ArrayDeque<Integer>(n + 1); break;
53 >        default: throw new AssertionError();
54 >        }
55 >        switch (rnd.nextInt(3)) {
56 >        case 0:
57 >            q.addFirst(42);
58 >            assertEquals((Integer) 42, q.removeLast());
59 >            break;
60 >        case 1:
61 >            q.addLast(42);
62 >            assertEquals((Integer) 42, q.removeLast());
63 >            break;
64 >        case 2: /* do nothing */ break;
65 >        default: throw new AssertionError();
66 >        }
67          assertTrue(q.isEmpty());
68 <        for (int i = 0; i < n; ++i)
69 <            assertTrue(q.offerLast(new Integer(i)));
68 >        if (rnd.nextBoolean())
69 >            for (int i = 0; i < n; i++)
70 >                assertTrue(q.offerLast((Integer) i));
71 >        else
72 >            for (int i = n; --i >= 0; )
73 >                q.addFirst((Integer) i);
74          assertFalse(q.isEmpty());
75          assertEquals(n, q.size());
76 +        assertEquals((Integer) 0, q.peekFirst());
77 +        assertEquals((Integer) (n - 1), q.peekLast());
78          return q;
79      }
80  
81      /**
82 <     * new queue is empty
82 >     * new deque is empty
83       */
84      public void testConstructor1() {
85          assertEquals(0, new ArrayDeque().size());
# Line 43 | Line 90 | public class ArrayDequeTest extends JSR1
90       */
91      public void testConstructor3() {
92          try {
93 <            ArrayDeque q = new ArrayDeque((Collection)null);
93 >            new ArrayDeque((Collection)null);
94 >            shouldThrow();
95 >        } catch (NullPointerException success) {}
96 >    }
97 >
98 >    /**
99 >     * Initializing from Collection of null elements throws NPE
100 >     */
101 >    public void testConstructor4() {
102 >        try {
103 >            new ArrayDeque(Arrays.asList(new Integer[SIZE]));
104              shouldThrow();
105          } catch (NullPointerException success) {}
106      }
107  
108      /**
109 <     * Queue contains all elements of collection used to initialize
109 >     * Initializing from Collection with some null elements throws NPE
110 >     */
111 >    public void testConstructor5() {
112 >        Integer[] ints = new Integer[SIZE];
113 >        for (int i = 0; i < SIZE - 1; ++i)
114 >            ints[i] = new Integer(i);
115 >        try {
116 >            new ArrayDeque(Arrays.asList(ints));
117 >            shouldThrow();
118 >        } catch (NullPointerException success) {}
119 >    }
120  
121 +    /**
122 +     * Deque contains all elements of collection used to initialize
123       */
124      public void testConstructor6() {
125          Integer[] ints = new Integer[SIZE];
# Line 81 | Line 150 | public class ArrayDequeTest extends JSR1
150      public void testSize() {
151          ArrayDeque q = populatedDeque(SIZE);
152          for (int i = 0; i < SIZE; ++i) {
153 <            assertEquals(SIZE-i, q.size());
153 >            assertEquals(SIZE - i, q.size());
154              q.removeFirst();
155          }
156          for (int i = 0; i < SIZE; ++i) {
# Line 94 | Line 163 | public class ArrayDequeTest extends JSR1
163       * push(null) throws NPE
164       */
165      public void testPushNull() {
166 +        ArrayDeque q = new ArrayDeque(1);
167          try {
98            ArrayDeque q = new ArrayDeque(1);
168              q.push(null);
169              shouldThrow();
170          } catch (NullPointerException success) {}
171      }
172  
173      /**
174 <     * peekFirst returns element inserted with push
174 >     * peekFirst() returns element inserted with push
175       */
176      public void testPush() {
177          ArrayDeque q = populatedDeque(3);
# Line 112 | Line 181 | public class ArrayDequeTest extends JSR1
181      }
182  
183      /**
184 <     *  pop removes next element, or throws NSEE if empty
184 >     * pop() removes next element, or throws NSEE if empty
185       */
186      public void testPop() {
187          ArrayDeque q = populatedDeque(SIZE);
# Line 128 | Line 197 | public class ArrayDequeTest extends JSR1
197      /**
198       * offer(null) throws NPE
199       */
200 +    public void testOfferNull() {
201 +        ArrayDeque q = new ArrayDeque();
202 +        try {
203 +            q.offer(null);
204 +            shouldThrow();
205 +        } catch (NullPointerException success) {}
206 +    }
207 +
208 +    /**
209 +     * offerFirst(null) throws NPE
210 +     */
211      public void testOfferFirstNull() {
212 +        ArrayDeque q = new ArrayDeque();
213          try {
133            ArrayDeque q = new ArrayDeque();
214              q.offerFirst(null);
215              shouldThrow();
216          } catch (NullPointerException success) {}
217      }
218  
219      /**
220 <     * OfferFirst succeeds
220 >     * offerLast(null) throws NPE
221 >     */
222 >    public void testOfferLastNull() {
223 >        ArrayDeque q = new ArrayDeque();
224 >        try {
225 >            q.offerLast(null);
226 >            shouldThrow();
227 >        } catch (NullPointerException success) {}
228 >    }
229 >
230 >    /**
231 >     * offer(x) succeeds
232 >     */
233 >    public void testOffer() {
234 >        ArrayDeque q = new ArrayDeque();
235 >        assertTrue(q.offer(zero));
236 >        assertTrue(q.offer(one));
237 >        assertSame(zero, q.peekFirst());
238 >        assertSame(one, q.peekLast());
239 >    }
240 >
241 >    /**
242 >     * offerFirst(x) succeeds
243       */
244      public void testOfferFirst() {
245          ArrayDeque q = new ArrayDeque();
246 <        assertTrue(q.offerFirst(new Integer(0)));
247 <        assertTrue(q.offerFirst(new Integer(1)));
246 >        assertTrue(q.offerFirst(zero));
247 >        assertTrue(q.offerFirst(one));
248 >        assertSame(one, q.peekFirst());
249 >        assertSame(zero, q.peekLast());
250      }
251  
252      /**
253 <     * OfferLast succeeds
253 >     * offerLast(x) succeeds
254       */
255      public void testOfferLast() {
256          ArrayDeque q = new ArrayDeque();
257 <        assertTrue(q.offerLast(new Integer(0)));
258 <        assertTrue(q.offerLast(new Integer(1)));
257 >        assertTrue(q.offerLast(zero));
258 >        assertTrue(q.offerLast(one));
259 >        assertSame(zero, q.peekFirst());
260 >        assertSame(one, q.peekLast());
261 >    }
262 >
263 >    /**
264 >     * add(null) throws NPE
265 >     */
266 >    public void testAddNull() {
267 >        ArrayDeque q = new ArrayDeque();
268 >        try {
269 >            q.add(null);
270 >            shouldThrow();
271 >        } catch (NullPointerException success) {}
272 >    }
273 >
274 >    /**
275 >     * addFirst(null) throws NPE
276 >     */
277 >    public void testAddFirstNull() {
278 >        ArrayDeque q = new ArrayDeque();
279 >        try {
280 >            q.addFirst(null);
281 >            shouldThrow();
282 >        } catch (NullPointerException success) {}
283 >    }
284 >
285 >    /**
286 >     * addLast(null) throws NPE
287 >     */
288 >    public void testAddLastNull() {
289 >        ArrayDeque q = new ArrayDeque();
290 >        try {
291 >            q.addLast(null);
292 >            shouldThrow();
293 >        } catch (NullPointerException success) {}
294      }
295  
296      /**
297 <     * add succeeds
297 >     * add(x) succeeds
298       */
299      public void testAdd() {
300          ArrayDeque q = new ArrayDeque();
301 <        for (int i = 0; i < SIZE; ++i) {
302 <            assertEquals(i, q.size());
303 <            assertTrue(q.add(new Integer(i)));
304 <        }
301 >        assertTrue(q.add(zero));
302 >        assertTrue(q.add(one));
303 >        assertSame(zero, q.peekFirst());
304 >        assertSame(one, q.peekLast());
305 >    }
306 >
307 >    /**
308 >     * addFirst(x) succeeds
309 >     */
310 >    public void testAddFirst() {
311 >        ArrayDeque q = new ArrayDeque();
312 >        q.addFirst(zero);
313 >        q.addFirst(one);
314 >        assertSame(one, q.peekFirst());
315 >        assertSame(zero, q.peekLast());
316 >    }
317 >
318 >    /**
319 >     * addLast(x) succeeds
320 >     */
321 >    public void testAddLast() {
322 >        ArrayDeque q = new ArrayDeque();
323 >        q.addLast(zero);
324 >        q.addLast(one);
325 >        assertSame(zero, q.peekFirst());
326 >        assertSame(one, q.peekLast());
327      }
328  
329      /**
330       * addAll(null) throws NPE
331       */
332      public void testAddAll1() {
333 +        ArrayDeque q = new ArrayDeque();
334          try {
173            ArrayDeque q = new ArrayDeque();
335              q.addAll(null);
336              shouldThrow();
337          } catch (NullPointerException success) {}
338      }
339  
340      /**
341 <     * Queue contains all elements, in traversal order, of successful addAll
341 >     * addAll of a collection with null elements throws NPE
342 >     */
343 >    public void testAddAll2() {
344 >        ArrayDeque q = new ArrayDeque();
345 >        try {
346 >            q.addAll(Arrays.asList(new Integer[SIZE]));
347 >            shouldThrow();
348 >        } catch (NullPointerException success) {}
349 >    }
350 >
351 >    /**
352 >     * addAll of a collection with any null elements throws NPE after
353 >     * possibly adding some elements
354 >     */
355 >    public void testAddAll3() {
356 >        ArrayDeque q = new ArrayDeque();
357 >        Integer[] ints = new Integer[SIZE];
358 >        for (int i = 0; i < SIZE - 1; ++i)
359 >            ints[i] = new Integer(i);
360 >        try {
361 >            q.addAll(Arrays.asList(ints));
362 >            shouldThrow();
363 >        } catch (NullPointerException success) {}
364 >    }
365 >
366 >    /**
367 >     * Deque contains all elements, in traversal order, of successful addAll
368       */
369      public void testAddAll5() {
370          Integer[] empty = new Integer[0];
# Line 192 | Line 379 | public class ArrayDequeTest extends JSR1
379      }
380  
381      /**
382 <     *  pollFirst succeeds unless empty
382 >     * pollFirst() succeeds unless empty
383       */
384      public void testPollFirst() {
385          ArrayDeque q = populatedDeque(SIZE);
# Line 203 | Line 390 | public class ArrayDequeTest extends JSR1
390      }
391  
392      /**
393 <     *  pollLast succeeds unless empty
393 >     * pollLast() succeeds unless empty
394       */
395      public void testPollLast() {
396          ArrayDeque q = populatedDeque(SIZE);
397 <        for (int i = SIZE-1; i >= 0; --i) {
397 >        for (int i = SIZE - 1; i >= 0; --i) {
398              assertEquals(i, q.pollLast());
399          }
400          assertNull(q.pollLast());
401      }
402  
403      /**
404 <     *  poll succeeds unless empty
404 >     * poll() succeeds unless empty
405       */
406      public void testPoll() {
407          ArrayDeque q = populatedDeque(SIZE);
# Line 225 | Line 412 | public class ArrayDequeTest extends JSR1
412      }
413  
414      /**
415 <     *  remove removes next element, or throws NSEE if empty
415 >     * remove() removes next element, or throws NSEE if empty
416       */
417      public void testRemove() {
418          ArrayDeque q = populatedDeque(SIZE);
# Line 239 | Line 426 | public class ArrayDequeTest extends JSR1
426      }
427  
428      /**
429 <     *  peekFirst returns next element, or null if empty
429 >     * remove(x) removes x and returns true if present
430 >     */
431 >    public void testRemoveElement() {
432 >        ArrayDeque q = populatedDeque(SIZE);
433 >        for (int i = 1; i < SIZE; i += 2) {
434 >            assertTrue(q.contains(i));
435 >            assertTrue(q.remove(i));
436 >            assertFalse(q.contains(i));
437 >            assertTrue(q.contains(i - 1));
438 >        }
439 >        for (int i = 0; i < SIZE; i += 2) {
440 >            assertTrue(q.contains(i));
441 >            assertTrue(q.remove(i));
442 >            assertFalse(q.contains(i));
443 >            assertFalse(q.remove(i + 1));
444 >            assertFalse(q.contains(i + 1));
445 >        }
446 >        assertTrue(q.isEmpty());
447 >    }
448 >
449 >    /**
450 >     * peekFirst() returns next element, or null if empty
451       */
452      public void testPeekFirst() {
453          ArrayDeque q = populatedDeque(SIZE);
# Line 253 | Line 461 | public class ArrayDequeTest extends JSR1
461      }
462  
463      /**
464 <     *  peek returns next element, or null if empty
464 >     * peek() returns next element, or null if empty
465       */
466      public void testPeek() {
467          ArrayDeque q = populatedDeque(SIZE);
# Line 267 | Line 475 | public class ArrayDequeTest extends JSR1
475      }
476  
477      /**
478 <     *  peekLast returns next element, or null if empty
478 >     * peekLast() returns next element, or null if empty
479       */
480      public void testPeekLast() {
481          ArrayDeque 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 281 | Line 489 | public class ArrayDequeTest extends JSR1
489      }
490  
491      /**
492 <     * getFirst returns next getFirst, or throws NSEE if empty
492 >     * element() returns first element, or throws NSEE if empty
493 >     */
494 >    public void testElement() {
495 >        ArrayDeque q = populatedDeque(SIZE);
496 >        for (int i = 0; i < SIZE; ++i) {
497 >            assertEquals(i, q.element());
498 >            assertEquals(i, q.poll());
499 >        }
500 >        try {
501 >            q.element();
502 >            shouldThrow();
503 >        } catch (NoSuchElementException success) {}
504 >    }
505 >
506 >    /**
507 >     * getFirst() returns first element, or throws NSEE if empty
508       */
509      public void testFirstElement() {
510          ArrayDeque q = populatedDeque(SIZE);
# Line 296 | Line 519 | public class ArrayDequeTest extends JSR1
519      }
520  
521      /**
522 <     *  getLast returns next element, or throws NSEE if empty
522 >     * getLast() returns last element, or throws NSEE if empty
523       */
524      public void testLastElement() {
525          ArrayDeque q = populatedDeque(SIZE);
526 <        for (int i = SIZE-1; i >= 0; --i) {
526 >        for (int i = SIZE - 1; i >= 0; --i) {
527              assertEquals(i, q.getLast());
528              assertEquals(i, q.pollLast());
529          }
# Line 311 | Line 534 | public class ArrayDequeTest extends JSR1
534          assertNull(q.peekLast());
535      }
536  
314
537      /**
538 <     *  removeFirst removes next element, or throws NSEE if empty
538 >     * removeFirst() removes first element, or throws NSEE if empty
539       */
540      public void testRemoveFirst() {
541          ArrayDeque q = populatedDeque(SIZE);
# Line 324 | Line 546 | public class ArrayDequeTest extends JSR1
546              q.removeFirst();
547              shouldThrow();
548          } catch (NoSuchElementException success) {}
549 +        assertNull(q.peekFirst());
550 +    }
551 +
552 +    /**
553 +     * removeLast() removes last element, or throws NSEE if empty
554 +     */
555 +    public void testRemoveLast() {
556 +        ArrayDeque q = populatedDeque(SIZE);
557 +        for (int i = SIZE - 1; i >= 0; --i) {
558 +            assertEquals(i, q.removeLast());
559 +        }
560 +        try {
561 +            q.removeLast();
562 +            shouldThrow();
563 +        } catch (NoSuchElementException success) {}
564 +        assertNull(q.peekLast());
565      }
566  
567      /**
# Line 331 | Line 569 | public class ArrayDequeTest extends JSR1
569       */
570      public void testRemoveFirstOccurrence() {
571          ArrayDeque q = populatedDeque(SIZE);
572 <        for (int i = 1; i < SIZE; i+=2) {
572 >        assertFalse(q.removeFirstOccurrence(null));
573 >        for (int i = 1; i < SIZE; i += 2) {
574              assertTrue(q.removeFirstOccurrence(new Integer(i)));
575          }
576 <        for (int i = 0; i < SIZE; i+=2) {
576 >        for (int i = 0; i < SIZE; i += 2) {
577              assertTrue(q.removeFirstOccurrence(new Integer(i)));
578 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
578 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
579          }
580          assertTrue(q.isEmpty());
581 +        assertFalse(q.removeFirstOccurrence(null));
582 +        assertFalse(q.removeFirstOccurrence(42));
583 +        q = new ArrayDeque();
584 +        assertFalse(q.removeFirstOccurrence(null));
585 +        assertFalse(q.removeFirstOccurrence(42));
586      }
587  
588      /**
# Line 346 | Line 590 | public class ArrayDequeTest extends JSR1
590       */
591      public void testRemoveLastOccurrence() {
592          ArrayDeque q = populatedDeque(SIZE);
593 <        for (int i = 1; i < SIZE; i+=2) {
593 >        assertFalse(q.removeLastOccurrence(null));
594 >        for (int i = 1; i < SIZE; i += 2) {
595              assertTrue(q.removeLastOccurrence(new Integer(i)));
596          }
597 <        for (int i = 0; i < SIZE; i+=2) {
597 >        for (int i = 0; i < SIZE; i += 2) {
598              assertTrue(q.removeLastOccurrence(new Integer(i)));
599 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
599 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
600          }
601          assertTrue(q.isEmpty());
602 +        assertFalse(q.removeLastOccurrence(null));
603 +        assertFalse(q.removeLastOccurrence(42));
604 +        q = new ArrayDeque();
605 +        assertFalse(q.removeLastOccurrence(null));
606 +        assertFalse(q.removeLastOccurrence(42));
607      }
608  
609      /**
# Line 406 | Line 656 | public class ArrayDequeTest extends JSR1
656              boolean changed = q.retainAll(p);
657              assertEquals(changed, (i > 0));
658              assertTrue(q.containsAll(p));
659 <            assertEquals(SIZE-i, q.size());
659 >            assertEquals(SIZE - i, q.size());
660              p.removeFirst();
661          }
662      }
# Line 419 | Line 669 | public class ArrayDequeTest extends JSR1
669              ArrayDeque q = populatedDeque(SIZE);
670              ArrayDeque p = populatedDeque(i);
671              assertTrue(q.removeAll(p));
672 <            assertEquals(SIZE-i, q.size());
672 >            assertEquals(SIZE - i, q.size());
673              for (int j = 0; j < i; ++j) {
674                  assertFalse(q.contains(p.removeFirst()));
675              }
676          }
677      }
678  
679 +    void checkToArray(ArrayDeque q) {
680 +        int size = q.size();
681 +        Object[] o = q.toArray();
682 +        assertEquals(size, o.length);
683 +        Iterator it = q.iterator();
684 +        for (int i = 0; i < size; i++) {
685 +            Integer x = (Integer) it.next();
686 +            assertEquals((Integer)o[0] + i, (int) x);
687 +            assertSame(o[i], x);
688 +        }
689 +    }
690 +
691      /**
692 <     *  toArray contains all elements
692 >     * toArray() contains all elements in FIFO order
693       */
694      public void testToArray() {
695 <        ArrayDeque q = populatedDeque(SIZE);
696 <        Object[] o = q.toArray();
697 <        Arrays.sort(o);
698 <        for (int i = 0; i < o.length; i++)
699 <            assertEquals(o[i], q.pollFirst());
695 >        ArrayDeque q = new ArrayDeque();
696 >        for (int i = 0; i < SIZE; i++) {
697 >            checkToArray(q);
698 >            q.addLast(i);
699 >        }
700 >        // Provoke wraparound
701 >        for (int i = 0; i < SIZE; i++) {
702 >            checkToArray(q);
703 >            assertEquals(i, q.poll());
704 >            q.addLast(SIZE + i);
705 >        }
706 >        for (int i = 0; i < SIZE; i++) {
707 >            checkToArray(q);
708 >            assertEquals(SIZE + i, q.poll());
709 >        }
710 >    }
711 >
712 >    void checkToArray2(ArrayDeque q) {
713 >        int size = q.size();
714 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
715 >        Integer[] a2 = new Integer[size];
716 >        Integer[] a3 = new Integer[size + 2];
717 >        if (size > 0) Arrays.fill(a1, 42);
718 >        Arrays.fill(a2, 42);
719 >        Arrays.fill(a3, 42);
720 >        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
721 >        Integer[] b2 = (Integer[]) q.toArray(a2);
722 >        Integer[] b3 = (Integer[]) q.toArray(a3);
723 >        assertSame(a2, b2);
724 >        assertSame(a3, b3);
725 >        Iterator it = q.iterator();
726 >        for (int i = 0; i < size; i++) {
727 >            Integer x = (Integer) it.next();
728 >            assertSame(b1[i], x);
729 >            assertEquals(b1[0] + i, (int) x);
730 >            assertSame(b2[i], x);
731 >            assertSame(b3[i], x);
732 >        }
733 >        assertNull(a3[size]);
734 >        assertEquals(42, (int) a3[size + 1]);
735 >        if (size > 0) {
736 >            assertNotSame(a1, b1);
737 >            assertEquals(size, b1.length);
738 >            for (int i = 0; i < a1.length; i++) {
739 >                assertEquals(42, (int) a1[i]);
740 >            }
741 >        }
742      }
743  
744      /**
745 <     *  toArray(a) contains all elements
745 >     * toArray(a) contains all elements in FIFO order
746       */
747      public void testToArray2() {
748 <        ArrayDeque q = populatedDeque(SIZE);
749 <        Integer[] ints = new Integer[SIZE];
750 <        ints = (Integer[])q.toArray(ints);
751 <        Arrays.sort(ints);
752 <        for (int i = 0; i < ints.length; i++)
753 <            assertEquals(ints[i], q.pollFirst());
748 >        ArrayDeque q = new ArrayDeque();
749 >        for (int i = 0; i < SIZE; i++) {
750 >            checkToArray2(q);
751 >            q.addLast(i);
752 >        }
753 >        // Provoke wraparound
754 >        for (int i = 0; i < SIZE; i++) {
755 >            checkToArray2(q);
756 >            assertEquals(i, q.poll());
757 >            q.addLast(SIZE + i);
758 >        }
759 >        for (int i = 0; i < SIZE; i++) {
760 >            checkToArray2(q);
761 >            assertEquals(SIZE + i, q.poll());
762 >        }
763      }
764  
765      /**
766 <     * toArray(null) throws NPE
766 >     * toArray(null) throws NullPointerException
767       */
768 <    public void testToArray_BadArg() {
768 >    public void testToArray_NullArg() {
769          ArrayDeque l = new ArrayDeque();
770          l.add(new Object());
771          try {
772 <            Object o[] = l.toArray(null);
772 >            l.toArray(null);
773              shouldThrow();
774          } catch (NullPointerException success) {}
775      }
776  
777      /**
778 <     * toArray with incompatable aray type throws CCE
778 >     * toArray(incompatible array type) throws ArrayStoreException
779       */
780      public void testToArray1_BadArg() {
781          ArrayDeque l = new ArrayDeque();
782          l.add(new Integer(5));
783          try {
784 <            Object o[] = l.toArray(new String[10]);
784 >            l.toArray(new String[10]);
785              shouldThrow();
786          } catch (ArrayStoreException success) {}
787      }
788  
789      /**
790 <     *  iterator iterates through all elements
790 >     * Iterator iterates through all elements
791       */
792      public void testIterator() {
793          ArrayDeque q = populatedDeque(SIZE);
481        int i = 0;
794          Iterator it = q.iterator();
795 <        while (it.hasNext()) {
795 >        int i;
796 >        for (i = 0; it.hasNext(); i++)
797              assertTrue(q.contains(it.next()));
485            ++i;
486        }
798          assertEquals(i, SIZE);
799 +        assertIteratorExhausted(it);
800      }
801  
802      /**
803 <     *  iterator ordering is FIFO
803 >     * iterator of empty collection has no elements
804 >     */
805 >    public void testEmptyIterator() {
806 >        Deque c = new ArrayDeque();
807 >        assertIteratorExhausted(c.iterator());
808 >        assertIteratorExhausted(c.descendingIterator());
809 >    }
810 >
811 >    /**
812 >     * Iterator ordering is FIFO
813       */
814      public void testIteratorOrdering() {
815          final ArrayDeque q = new ArrayDeque();
816 <        q.add(new Integer(1));
817 <        q.add(new Integer(2));
818 <        q.add(new Integer(3));
816 >        q.add(one);
817 >        q.add(two);
818 >        q.add(three);
819          int k = 0;
820          for (Iterator it = q.iterator(); it.hasNext();) {
821              assertEquals(++k, it.next());
# Line 504 | Line 825 | public class ArrayDequeTest extends JSR1
825      }
826  
827      /**
828 <     * iterator.remove removes current element
828 >     * iterator.remove() removes current element
829       */
830 <    public void testIteratorRemove () {
830 >    public void testIteratorRemove() {
831          final ArrayDeque q = new ArrayDeque();
832          final Random rng = new Random();
833          for (int iters = 0; iters < 100; ++iters) {
834              int max = rng.nextInt(5) + 2;
835 <            int split = rng.nextInt(max-1) + 1;
835 >            int split = rng.nextInt(max - 1) + 1;
836              for (int j = 1; j <= max; ++j)
837                  q.add(new Integer(j));
838              Iterator it = q.iterator();
839              for (int j = 1; j <= split; ++j)
840                  assertEquals(it.next(), new Integer(j));
841              it.remove();
842 <            assertEquals(it.next(), new Integer(split+1));
842 >            assertEquals(it.next(), new Integer(split + 1));
843              for (int j = 1; j <= split; ++j)
844                  q.remove(new Integer(j));
845              it = q.iterator();
846 <            for (int j = split+1; j <= max; ++j) {
846 >            for (int j = split + 1; j <= max; ++j) {
847                  assertEquals(it.next(), new Integer(j));
848                  it.remove();
849              }
# Line 532 | Line 853 | public class ArrayDequeTest extends JSR1
853      }
854  
855      /**
856 <     *  Descending iterator iterates through all elements
856 >     * Descending iterator iterates through all elements
857       */
858      public void testDescendingIterator() {
859          ArrayDeque q = populatedDeque(SIZE);
# Line 551 | Line 872 | public class ArrayDequeTest extends JSR1
872      }
873  
874      /**
875 <     *  Descending iterator ordering is reverse FIFO
875 >     * Descending iterator ordering is reverse FIFO
876       */
877      public void testDescendingIteratorOrdering() {
878          final ArrayDeque q = new ArrayDeque();
# Line 572 | Line 893 | public class ArrayDequeTest extends JSR1
893      }
894  
895      /**
896 <     * descendingIterator.remove removes current element
896 >     * descendingIterator.remove() removes current element
897       */
898 <    public void testDescendingIteratorRemove () {
898 >    public void testDescendingIteratorRemove() {
899          final ArrayDeque q = new ArrayDeque();
900          final Random rng = new Random();
901          for (int iters = 0; iters < 100; ++iters) {
902              int max = rng.nextInt(5) + 2;
903 <            int split = rng.nextInt(max-1) + 1;
903 >            int split = rng.nextInt(max - 1) + 1;
904              for (int j = max; j >= 1; --j)
905                  q.add(new Integer(j));
906              Iterator it = q.descendingIterator();
907              for (int j = 1; j <= split; ++j)
908                  assertEquals(it.next(), new Integer(j));
909              it.remove();
910 <            assertEquals(it.next(), new Integer(split+1));
910 >            assertEquals(it.next(), new Integer(split + 1));
911              for (int j = 1; j <= split; ++j)
912                  q.remove(new Integer(j));
913              it = q.descendingIterator();
914 <            for (int j = split+1; j <= max; ++j) {
914 >            for (int j = split + 1; j <= max; ++j) {
915                  assertEquals(it.next(), new Integer(j));
916                  it.remove();
917              }
# Line 599 | Line 920 | public class ArrayDequeTest extends JSR1
920          }
921      }
922  
602
923      /**
924 <     * toString contains toStrings of elements
924 >     * toString() contains toStrings of elements
925       */
926      public void testToString() {
927          ArrayDeque q = populatedDeque(SIZE);
928          String s = q.toString();
929          for (int i = 0; i < SIZE; ++i) {
930 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
930 >            assertTrue(s.contains(String.valueOf(i)));
931          }
932      }
933  
934      /**
935 <     * peekFirst returns element inserted with addFirst
935 >     * A deserialized serialized deque has same elements in same order
936       */
937 <    public void testAddFirst() {
938 <        ArrayDeque q = populatedDeque(3);
939 <        q.addFirst(four);
940 <        assertSame(four, q.peekFirst());
937 >    public void testSerialization() throws Exception {
938 >        Queue x = populatedDeque(SIZE);
939 >        Queue y = serialClone(x);
940 >
941 >        assertNotSame(y, x);
942 >        assertEquals(x.size(), y.size());
943 >        assertEquals(x.toString(), y.toString());
944 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
945 >        while (!x.isEmpty()) {
946 >            assertFalse(y.isEmpty());
947 >            assertEquals(x.remove(), y.remove());
948 >        }
949 >        assertTrue(y.isEmpty());
950      }
951  
952      /**
953 <     * peekLast returns element inserted with addLast
953 >     * remove(null), contains(null) always return false
954       */
955 <    public void testAddLast() {
956 <        ArrayDeque q = populatedDeque(3);
957 <        q.addLast(four);
958 <        assertSame(four, q.peekLast());
955 >    public void testNeverContainsNull() {
956 >        Deque<?>[] qs = {
957 >            new ArrayDeque<Object>(),
958 >            populatedDeque(2),
959 >        };
960 >
961 >        for (Deque<?> q : qs) {
962 >            assertFalse(q.contains(null));
963 >            assertFalse(q.remove(null));
964 >            assertFalse(q.removeFirstOccurrence(null));
965 >            assertFalse(q.removeLastOccurrence(null));
966 >        }
967      }
968  
969   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines