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.41 by jsr166, Sun Oct 16 20:44:18 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 >
16 > import junit.framework.Test;
17 > import junit.framework.TestSuite;
18  
19   public class ArrayDequeTest extends JSR166TestCase {
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run (suite());
21 >        main(suite(), args);
22      }
23  
24      public static Test suite() {
25 <        return new TestSuite(ArrayDequeTest.class);
25 >        class Implementation implements CollectionImplementation {
26 >            public Class<?> klazz() { return ArrayDeque.class; }
27 >            public Collection emptyCollection() { return new ArrayDeque(); }
28 >            public Object makeElement(int i) { return i; }
29 >            public boolean isConcurrent() { return false; }
30 >            public boolean permitsNulls() { return false; }
31 >        }
32 >        return newTestSuite(ArrayDequeTest.class,
33 >                            CollectionTest.testSuite(new Implementation()));
34      }
35  
36      /**
37 <     * Create a queue of given size containing consecutive
38 <     * Integers 0 ... n.
37 >     * Returns a new deque of given size containing consecutive
38 >     * Integers 0 ... n - 1.
39       */
40 <    private ArrayDeque populatedDeque(int n) {
41 <        ArrayDeque q = new ArrayDeque();
40 >    private ArrayDeque<Integer> populatedDeque(int n) {
41 >        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
42          assertTrue(q.isEmpty());
43          for (int i = 0; i < n; ++i)
44              assertTrue(q.offerLast(new Integer(i)));
45          assertFalse(q.isEmpty());
46          assertEquals(n, q.size());
47 +        assertEquals((Integer) 0, q.peekFirst());
48 +        assertEquals((Integer) (n - 1), q.peekLast());
49          return q;
50      }
51  
52      /**
53 <     * new queue is empty
53 >     * new deque is empty
54       */
55      public void testConstructor1() {
56          assertEquals(0, new ArrayDeque().size());
# Line 43 | Line 61 | public class ArrayDequeTest extends JSR1
61       */
62      public void testConstructor3() {
63          try {
64 <            ArrayDeque q = new ArrayDeque((Collection)null);
64 >            new ArrayDeque((Collection)null);
65              shouldThrow();
66          } catch (NullPointerException success) {}
67      }
68  
69      /**
70 <     * Queue contains all elements of collection used to initialize
70 >     * Initializing from Collection of null elements throws NPE
71 >     */
72 >    public void testConstructor4() {
73 >        try {
74 >            new ArrayDeque(Arrays.asList(new Integer[SIZE]));
75 >            shouldThrow();
76 >        } catch (NullPointerException success) {}
77 >    }
78 >
79 >    /**
80 >     * Initializing from Collection with some null elements throws NPE
81 >     */
82 >    public void testConstructor5() {
83 >        Integer[] ints = new Integer[SIZE];
84 >        for (int i = 0; i < SIZE - 1; ++i)
85 >            ints[i] = new Integer(i);
86 >        try {
87 >            new ArrayDeque(Arrays.asList(ints));
88 >            shouldThrow();
89 >        } catch (NullPointerException success) {}
90 >    }
91  
92 +    /**
93 +     * Deque contains all elements of collection used to initialize
94       */
95      public void testConstructor6() {
96          Integer[] ints = new Integer[SIZE];
# Line 81 | Line 121 | public class ArrayDequeTest extends JSR1
121      public void testSize() {
122          ArrayDeque q = populatedDeque(SIZE);
123          for (int i = 0; i < SIZE; ++i) {
124 <            assertEquals(SIZE-i, q.size());
124 >            assertEquals(SIZE - i, q.size());
125              q.removeFirst();
126          }
127          for (int i = 0; i < SIZE; ++i) {
# Line 94 | Line 134 | public class ArrayDequeTest extends JSR1
134       * push(null) throws NPE
135       */
136      public void testPushNull() {
137 +        ArrayDeque q = new ArrayDeque(1);
138          try {
98            ArrayDeque q = new ArrayDeque(1);
139              q.push(null);
140              shouldThrow();
141          } catch (NullPointerException success) {}
142      }
143  
144      /**
145 <     * peekFirst returns element inserted with push
145 >     * peekFirst() returns element inserted with push
146       */
147      public void testPush() {
148          ArrayDeque q = populatedDeque(3);
# Line 112 | Line 152 | public class ArrayDequeTest extends JSR1
152      }
153  
154      /**
155 <     *  pop removes next element, or throws NSEE if empty
155 >     * pop() removes next element, or throws NSEE if empty
156       */
157      public void testPop() {
158          ArrayDeque q = populatedDeque(SIZE);
# Line 128 | Line 168 | public class ArrayDequeTest extends JSR1
168      /**
169       * offer(null) throws NPE
170       */
171 +    public void testOfferNull() {
172 +        ArrayDeque q = new ArrayDeque();
173 +        try {
174 +            q.offer(null);
175 +            shouldThrow();
176 +        } catch (NullPointerException success) {}
177 +    }
178 +
179 +    /**
180 +     * offerFirst(null) throws NPE
181 +     */
182      public void testOfferFirstNull() {
183 +        ArrayDeque q = new ArrayDeque();
184          try {
133            ArrayDeque q = new ArrayDeque();
185              q.offerFirst(null);
186              shouldThrow();
187          } catch (NullPointerException success) {}
188      }
189  
190      /**
191 <     * OfferFirst succeeds
191 >     * offerLast(null) throws NPE
192 >     */
193 >    public void testOfferLastNull() {
194 >        ArrayDeque q = new ArrayDeque();
195 >        try {
196 >            q.offerLast(null);
197 >            shouldThrow();
198 >        } catch (NullPointerException success) {}
199 >    }
200 >
201 >    /**
202 >     * offer(x) succeeds
203 >     */
204 >    public void testOffer() {
205 >        ArrayDeque q = new ArrayDeque();
206 >        assertTrue(q.offer(zero));
207 >        assertTrue(q.offer(one));
208 >        assertSame(zero, q.peekFirst());
209 >        assertSame(one, q.peekLast());
210 >    }
211 >
212 >    /**
213 >     * offerFirst(x) succeeds
214       */
215      public void testOfferFirst() {
216          ArrayDeque q = new ArrayDeque();
217 <        assertTrue(q.offerFirst(new Integer(0)));
218 <        assertTrue(q.offerFirst(new Integer(1)));
217 >        assertTrue(q.offerFirst(zero));
218 >        assertTrue(q.offerFirst(one));
219 >        assertSame(one, q.peekFirst());
220 >        assertSame(zero, q.peekLast());
221      }
222  
223      /**
224 <     * OfferLast succeeds
224 >     * offerLast(x) succeeds
225       */
226      public void testOfferLast() {
227          ArrayDeque q = new ArrayDeque();
228 <        assertTrue(q.offerLast(new Integer(0)));
229 <        assertTrue(q.offerLast(new Integer(1)));
228 >        assertTrue(q.offerLast(zero));
229 >        assertTrue(q.offerLast(one));
230 >        assertSame(zero, q.peekFirst());
231 >        assertSame(one, q.peekLast());
232 >    }
233 >
234 >    /**
235 >     * add(null) throws NPE
236 >     */
237 >    public void testAddNull() {
238 >        ArrayDeque q = new ArrayDeque();
239 >        try {
240 >            q.add(null);
241 >            shouldThrow();
242 >        } catch (NullPointerException success) {}
243 >    }
244 >
245 >    /**
246 >     * addFirst(null) throws NPE
247 >     */
248 >    public void testAddFirstNull() {
249 >        ArrayDeque q = new ArrayDeque();
250 >        try {
251 >            q.addFirst(null);
252 >            shouldThrow();
253 >        } catch (NullPointerException success) {}
254 >    }
255 >
256 >    /**
257 >     * addLast(null) throws NPE
258 >     */
259 >    public void testAddLastNull() {
260 >        ArrayDeque q = new ArrayDeque();
261 >        try {
262 >            q.addLast(null);
263 >            shouldThrow();
264 >        } catch (NullPointerException success) {}
265      }
266  
267      /**
268 <     * add succeeds
268 >     * add(x) succeeds
269       */
270      public void testAdd() {
271          ArrayDeque q = new ArrayDeque();
272 <        for (int i = 0; i < SIZE; ++i) {
273 <            assertEquals(i, q.size());
274 <            assertTrue(q.add(new Integer(i)));
275 <        }
272 >        assertTrue(q.add(zero));
273 >        assertTrue(q.add(one));
274 >        assertSame(zero, q.peekFirst());
275 >        assertSame(one, q.peekLast());
276 >    }
277 >
278 >    /**
279 >     * addFirst(x) succeeds
280 >     */
281 >    public void testAddFirst() {
282 >        ArrayDeque q = new ArrayDeque();
283 >        q.addFirst(zero);
284 >        q.addFirst(one);
285 >        assertSame(one, q.peekFirst());
286 >        assertSame(zero, q.peekLast());
287 >    }
288 >
289 >    /**
290 >     * addLast(x) succeeds
291 >     */
292 >    public void testAddLast() {
293 >        ArrayDeque q = new ArrayDeque();
294 >        q.addLast(zero);
295 >        q.addLast(one);
296 >        assertSame(zero, q.peekFirst());
297 >        assertSame(one, q.peekLast());
298      }
299  
300      /**
301       * addAll(null) throws NPE
302       */
303      public void testAddAll1() {
304 +        ArrayDeque q = new ArrayDeque();
305          try {
173            ArrayDeque q = new ArrayDeque();
306              q.addAll(null);
307              shouldThrow();
308          } catch (NullPointerException success) {}
309      }
310  
311      /**
312 <     * Queue contains all elements, in traversal order, of successful addAll
312 >     * addAll of a collection with null elements throws NPE
313 >     */
314 >    public void testAddAll2() {
315 >        ArrayDeque q = new ArrayDeque();
316 >        try {
317 >            q.addAll(Arrays.asList(new Integer[SIZE]));
318 >            shouldThrow();
319 >        } catch (NullPointerException success) {}
320 >    }
321 >
322 >    /**
323 >     * addAll of a collection with any null elements throws NPE after
324 >     * possibly adding some elements
325 >     */
326 >    public void testAddAll3() {
327 >        ArrayDeque q = new ArrayDeque();
328 >        Integer[] ints = new Integer[SIZE];
329 >        for (int i = 0; i < SIZE - 1; ++i)
330 >            ints[i] = new Integer(i);
331 >        try {
332 >            q.addAll(Arrays.asList(ints));
333 >            shouldThrow();
334 >        } catch (NullPointerException success) {}
335 >    }
336 >
337 >    /**
338 >     * Deque contains all elements, in traversal order, of successful addAll
339       */
340      public void testAddAll5() {
341          Integer[] empty = new Integer[0];
# Line 192 | Line 350 | public class ArrayDequeTest extends JSR1
350      }
351  
352      /**
353 <     *  pollFirst succeeds unless empty
353 >     * pollFirst() succeeds unless empty
354       */
355      public void testPollFirst() {
356          ArrayDeque q = populatedDeque(SIZE);
# Line 203 | Line 361 | public class ArrayDequeTest extends JSR1
361      }
362  
363      /**
364 <     *  pollLast succeeds unless empty
364 >     * pollLast() succeeds unless empty
365       */
366      public void testPollLast() {
367          ArrayDeque q = populatedDeque(SIZE);
368 <        for (int i = SIZE-1; i >= 0; --i) {
368 >        for (int i = SIZE - 1; i >= 0; --i) {
369              assertEquals(i, q.pollLast());
370          }
371          assertNull(q.pollLast());
372      }
373  
374      /**
375 <     *  poll succeeds unless empty
375 >     * poll() succeeds unless empty
376       */
377      public void testPoll() {
378          ArrayDeque q = populatedDeque(SIZE);
# Line 225 | Line 383 | public class ArrayDequeTest extends JSR1
383      }
384  
385      /**
386 <     *  remove removes next element, or throws NSEE if empty
386 >     * remove() removes next element, or throws NSEE if empty
387       */
388      public void testRemove() {
389          ArrayDeque q = populatedDeque(SIZE);
# Line 239 | Line 397 | public class ArrayDequeTest extends JSR1
397      }
398  
399      /**
400 <     *  peekFirst returns next element, or null if empty
400 >     * remove(x) removes x and returns true if present
401 >     */
402 >    public void testRemoveElement() {
403 >        ArrayDeque q = populatedDeque(SIZE);
404 >        for (int i = 1; i < SIZE; i += 2) {
405 >            assertTrue(q.contains(i));
406 >            assertTrue(q.remove(i));
407 >            assertFalse(q.contains(i));
408 >            assertTrue(q.contains(i - 1));
409 >        }
410 >        for (int i = 0; i < SIZE; i += 2) {
411 >            assertTrue(q.contains(i));
412 >            assertTrue(q.remove(i));
413 >            assertFalse(q.contains(i));
414 >            assertFalse(q.remove(i + 1));
415 >            assertFalse(q.contains(i + 1));
416 >        }
417 >        assertTrue(q.isEmpty());
418 >    }
419 >
420 >    /**
421 >     * peekFirst() returns next element, or null if empty
422       */
423      public void testPeekFirst() {
424          ArrayDeque q = populatedDeque(SIZE);
# Line 253 | Line 432 | public class ArrayDequeTest extends JSR1
432      }
433  
434      /**
435 <     *  peek returns next element, or null if empty
435 >     * peek() returns next element, or null if empty
436       */
437      public void testPeek() {
438          ArrayDeque q = populatedDeque(SIZE);
# Line 267 | Line 446 | public class ArrayDequeTest extends JSR1
446      }
447  
448      /**
449 <     *  peekLast returns next element, or null if empty
449 >     * peekLast() returns next element, or null if empty
450       */
451      public void testPeekLast() {
452          ArrayDeque q = populatedDeque(SIZE);
453 <        for (int i = SIZE-1; i >= 0; --i) {
453 >        for (int i = SIZE - 1; i >= 0; --i) {
454              assertEquals(i, q.peekLast());
455              assertEquals(i, q.pollLast());
456              assertTrue(q.peekLast() == null ||
# Line 281 | Line 460 | public class ArrayDequeTest extends JSR1
460      }
461  
462      /**
463 <     * getFirst returns next getFirst, or throws NSEE if empty
463 >     * element() returns first element, or throws NSEE if empty
464 >     */
465 >    public void testElement() {
466 >        ArrayDeque q = populatedDeque(SIZE);
467 >        for (int i = 0; i < SIZE; ++i) {
468 >            assertEquals(i, q.element());
469 >            assertEquals(i, q.poll());
470 >        }
471 >        try {
472 >            q.element();
473 >            shouldThrow();
474 >        } catch (NoSuchElementException success) {}
475 >    }
476 >
477 >    /**
478 >     * getFirst() returns first element, or throws NSEE if empty
479       */
480      public void testFirstElement() {
481          ArrayDeque q = populatedDeque(SIZE);
# Line 296 | Line 490 | public class ArrayDequeTest extends JSR1
490      }
491  
492      /**
493 <     *  getLast returns next element, or throws NSEE if empty
493 >     * getLast() returns last element, or throws NSEE if empty
494       */
495      public void testLastElement() {
496          ArrayDeque q = populatedDeque(SIZE);
497 <        for (int i = SIZE-1; i >= 0; --i) {
497 >        for (int i = SIZE - 1; i >= 0; --i) {
498              assertEquals(i, q.getLast());
499              assertEquals(i, q.pollLast());
500          }
# Line 311 | Line 505 | public class ArrayDequeTest extends JSR1
505          assertNull(q.peekLast());
506      }
507  
314
508      /**
509 <     *  removeFirst removes next element, or throws NSEE if empty
509 >     * removeFirst() removes first element, or throws NSEE if empty
510       */
511      public void testRemoveFirst() {
512          ArrayDeque q = populatedDeque(SIZE);
# Line 324 | Line 517 | public class ArrayDequeTest extends JSR1
517              q.removeFirst();
518              shouldThrow();
519          } catch (NoSuchElementException success) {}
520 +        assertNull(q.peekFirst());
521 +    }
522 +
523 +    /**
524 +     * removeLast() removes last element, or throws NSEE if empty
525 +     */
526 +    public void testRemoveLast() {
527 +        ArrayDeque q = populatedDeque(SIZE);
528 +        for (int i = SIZE - 1; i >= 0; --i) {
529 +            assertEquals(i, q.removeLast());
530 +        }
531 +        try {
532 +            q.removeLast();
533 +            shouldThrow();
534 +        } catch (NoSuchElementException success) {}
535 +        assertNull(q.peekLast());
536      }
537  
538      /**
# Line 331 | Line 540 | public class ArrayDequeTest extends JSR1
540       */
541      public void testRemoveFirstOccurrence() {
542          ArrayDeque q = populatedDeque(SIZE);
543 <        for (int i = 1; i < SIZE; i+=2) {
543 >        assertFalse(q.removeFirstOccurrence(null));
544 >        for (int i = 1; i < SIZE; i += 2) {
545              assertTrue(q.removeFirstOccurrence(new Integer(i)));
546          }
547 <        for (int i = 0; i < SIZE; i+=2) {
547 >        for (int i = 0; i < SIZE; i += 2) {
548              assertTrue(q.removeFirstOccurrence(new Integer(i)));
549 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
549 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
550          }
551          assertTrue(q.isEmpty());
552 +        assertFalse(q.removeFirstOccurrence(null));
553 +        assertFalse(q.removeFirstOccurrence(42));
554 +        q = new ArrayDeque();
555 +        assertFalse(q.removeFirstOccurrence(null));
556 +        assertFalse(q.removeFirstOccurrence(42));
557      }
558  
559      /**
# Line 346 | Line 561 | public class ArrayDequeTest extends JSR1
561       */
562      public void testRemoveLastOccurrence() {
563          ArrayDeque q = populatedDeque(SIZE);
564 <        for (int i = 1; i < SIZE; i+=2) {
564 >        assertFalse(q.removeLastOccurrence(null));
565 >        for (int i = 1; i < SIZE; i += 2) {
566              assertTrue(q.removeLastOccurrence(new Integer(i)));
567          }
568 <        for (int i = 0; i < SIZE; i+=2) {
568 >        for (int i = 0; i < SIZE; i += 2) {
569              assertTrue(q.removeLastOccurrence(new Integer(i)));
570 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
570 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
571          }
572          assertTrue(q.isEmpty());
573 +        assertFalse(q.removeLastOccurrence(null));
574 +        assertFalse(q.removeLastOccurrence(42));
575 +        q = new ArrayDeque();
576 +        assertFalse(q.removeLastOccurrence(null));
577 +        assertFalse(q.removeLastOccurrence(42));
578      }
579  
580      /**
# Line 406 | Line 627 | public class ArrayDequeTest extends JSR1
627              boolean changed = q.retainAll(p);
628              assertEquals(changed, (i > 0));
629              assertTrue(q.containsAll(p));
630 <            assertEquals(SIZE-i, q.size());
630 >            assertEquals(SIZE - i, q.size());
631              p.removeFirst();
632          }
633      }
# Line 419 | Line 640 | public class ArrayDequeTest extends JSR1
640              ArrayDeque q = populatedDeque(SIZE);
641              ArrayDeque p = populatedDeque(i);
642              assertTrue(q.removeAll(p));
643 <            assertEquals(SIZE-i, q.size());
643 >            assertEquals(SIZE - i, q.size());
644              for (int j = 0; j < i; ++j) {
645                  assertFalse(q.contains(p.removeFirst()));
646              }
647          }
648      }
649  
650 +    void checkToArray(ArrayDeque q) {
651 +        int size = q.size();
652 +        Object[] o = q.toArray();
653 +        assertEquals(size, o.length);
654 +        Iterator it = q.iterator();
655 +        for (int i = 0; i < size; i++) {
656 +            Integer x = (Integer) it.next();
657 +            assertEquals((Integer)o[0] + i, (int) x);
658 +            assertSame(o[i], x);
659 +        }
660 +    }
661 +
662      /**
663 <     *  toArray contains all elements
663 >     * toArray() contains all elements in FIFO order
664       */
665      public void testToArray() {
666 <        ArrayDeque q = populatedDeque(SIZE);
667 <        Object[] o = q.toArray();
668 <        Arrays.sort(o);
669 <        for (int i = 0; i < o.length; i++)
670 <            assertEquals(o[i], q.pollFirst());
666 >        ArrayDeque q = new ArrayDeque();
667 >        for (int i = 0; i < SIZE; i++) {
668 >            checkToArray(q);
669 >            q.addLast(i);
670 >        }
671 >        // Provoke wraparound
672 >        for (int i = 0; i < SIZE; i++) {
673 >            checkToArray(q);
674 >            assertEquals(i, q.poll());
675 >            q.addLast(SIZE + i);
676 >        }
677 >        for (int i = 0; i < SIZE; i++) {
678 >            checkToArray(q);
679 >            assertEquals(SIZE + i, q.poll());
680 >        }
681 >    }
682 >
683 >    void checkToArray2(ArrayDeque q) {
684 >        int size = q.size();
685 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
686 >        Integer[] a2 = new Integer[size];
687 >        Integer[] a3 = new Integer[size + 2];
688 >        if (size > 0) Arrays.fill(a1, 42);
689 >        Arrays.fill(a2, 42);
690 >        Arrays.fill(a3, 42);
691 >        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
692 >        Integer[] b2 = (Integer[]) q.toArray(a2);
693 >        Integer[] b3 = (Integer[]) q.toArray(a3);
694 >        assertSame(a2, b2);
695 >        assertSame(a3, b3);
696 >        Iterator it = q.iterator();
697 >        for (int i = 0; i < size; i++) {
698 >            Integer x = (Integer) it.next();
699 >            assertSame(b1[i], x);
700 >            assertEquals(b1[0] + i, (int) x);
701 >            assertSame(b2[i], x);
702 >            assertSame(b3[i], x);
703 >        }
704 >        assertNull(a3[size]);
705 >        assertEquals(42, (int) a3[size + 1]);
706 >        if (size > 0) {
707 >            assertNotSame(a1, b1);
708 >            assertEquals(size, b1.length);
709 >            for (int i = 0; i < a1.length; i++) {
710 >                assertEquals(42, (int) a1[i]);
711 >            }
712 >        }
713      }
714  
715      /**
716 <     *  toArray(a) contains all elements
716 >     * toArray(a) contains all elements in FIFO order
717       */
718      public void testToArray2() {
719 <        ArrayDeque q = populatedDeque(SIZE);
720 <        Integer[] ints = new Integer[SIZE];
721 <        ints = (Integer[])q.toArray(ints);
722 <        Arrays.sort(ints);
723 <        for (int i = 0; i < ints.length; i++)
724 <            assertEquals(ints[i], q.pollFirst());
719 >        ArrayDeque q = new ArrayDeque();
720 >        for (int i = 0; i < SIZE; i++) {
721 >            checkToArray2(q);
722 >            q.addLast(i);
723 >        }
724 >        // Provoke wraparound
725 >        for (int i = 0; i < SIZE; i++) {
726 >            checkToArray2(q);
727 >            assertEquals(i, q.poll());
728 >            q.addLast(SIZE + i);
729 >        }
730 >        for (int i = 0; i < SIZE; i++) {
731 >            checkToArray2(q);
732 >            assertEquals(SIZE + i, q.poll());
733 >        }
734      }
735  
736      /**
737 <     * toArray(null) throws NPE
737 >     * toArray(null) throws NullPointerException
738       */
739 <    public void testToArray_BadArg() {
739 >    public void testToArray_NullArg() {
740          ArrayDeque l = new ArrayDeque();
741          l.add(new Object());
742          try {
743 <            Object o[] = l.toArray(null);
743 >            l.toArray(null);
744              shouldThrow();
745          } catch (NullPointerException success) {}
746      }
747  
748      /**
749 <     * toArray with incompatible aray type throws CCE
749 >     * toArray(incompatible array type) throws ArrayStoreException
750       */
751      public void testToArray1_BadArg() {
752          ArrayDeque l = new ArrayDeque();
753          l.add(new Integer(5));
754          try {
755 <            Object o[] = l.toArray(new String[10]);
755 >            l.toArray(new String[10]);
756              shouldThrow();
757          } catch (ArrayStoreException success) {}
758      }
759  
760      /**
761 <     *  iterator iterates through all elements
761 >     * Iterator iterates through all elements
762       */
763      public void testIterator() {
764          ArrayDeque q = populatedDeque(SIZE);
481        int i = 0;
765          Iterator it = q.iterator();
766 <        while (it.hasNext()) {
766 >        int i;
767 >        for (i = 0; it.hasNext(); i++)
768              assertTrue(q.contains(it.next()));
485            ++i;
486        }
769          assertEquals(i, SIZE);
770 +        assertIteratorExhausted(it);
771      }
772  
773      /**
774 <     *  iterator ordering is FIFO
774 >     * iterator of empty collection has no elements
775 >     */
776 >    public void testEmptyIterator() {
777 >        Deque c = new ArrayDeque();
778 >        assertIteratorExhausted(c.iterator());
779 >        assertIteratorExhausted(c.descendingIterator());
780 >    }
781 >
782 >    /**
783 >     * Iterator ordering is FIFO
784       */
785      public void testIteratorOrdering() {
786          final ArrayDeque q = new ArrayDeque();
787 <        q.add(new Integer(1));
788 <        q.add(new Integer(2));
789 <        q.add(new Integer(3));
787 >        q.add(one);
788 >        q.add(two);
789 >        q.add(three);
790          int k = 0;
791          for (Iterator it = q.iterator(); it.hasNext();) {
792              assertEquals(++k, it.next());
# Line 504 | Line 796 | public class ArrayDequeTest extends JSR1
796      }
797  
798      /**
799 <     * iterator.remove removes current element
799 >     * iterator.remove() removes current element
800       */
801 <    public void testIteratorRemove () {
801 >    public void testIteratorRemove() {
802          final ArrayDeque q = new ArrayDeque();
803          final Random rng = new Random();
804          for (int iters = 0; iters < 100; ++iters) {
805              int max = rng.nextInt(5) + 2;
806 <            int split = rng.nextInt(max-1) + 1;
806 >            int split = rng.nextInt(max - 1) + 1;
807              for (int j = 1; j <= max; ++j)
808                  q.add(new Integer(j));
809              Iterator it = q.iterator();
810              for (int j = 1; j <= split; ++j)
811                  assertEquals(it.next(), new Integer(j));
812              it.remove();
813 <            assertEquals(it.next(), new Integer(split+1));
813 >            assertEquals(it.next(), new Integer(split + 1));
814              for (int j = 1; j <= split; ++j)
815                  q.remove(new Integer(j));
816              it = q.iterator();
817 <            for (int j = split+1; j <= max; ++j) {
817 >            for (int j = split + 1; j <= max; ++j) {
818                  assertEquals(it.next(), new Integer(j));
819                  it.remove();
820              }
# Line 532 | Line 824 | public class ArrayDequeTest extends JSR1
824      }
825  
826      /**
827 <     *  Descending iterator iterates through all elements
827 >     * Descending iterator iterates through all elements
828       */
829      public void testDescendingIterator() {
830          ArrayDeque q = populatedDeque(SIZE);
# Line 551 | Line 843 | public class ArrayDequeTest extends JSR1
843      }
844  
845      /**
846 <     *  Descending iterator ordering is reverse FIFO
846 >     * Descending iterator ordering is reverse FIFO
847       */
848      public void testDescendingIteratorOrdering() {
849          final ArrayDeque q = new ArrayDeque();
# Line 572 | Line 864 | public class ArrayDequeTest extends JSR1
864      }
865  
866      /**
867 <     * descendingIterator.remove removes current element
867 >     * descendingIterator.remove() removes current element
868       */
869 <    public void testDescendingIteratorRemove () {
869 >    public void testDescendingIteratorRemove() {
870          final ArrayDeque q = new ArrayDeque();
871          final Random rng = new Random();
872          for (int iters = 0; iters < 100; ++iters) {
873              int max = rng.nextInt(5) + 2;
874 <            int split = rng.nextInt(max-1) + 1;
874 >            int split = rng.nextInt(max - 1) + 1;
875              for (int j = max; j >= 1; --j)
876                  q.add(new Integer(j));
877              Iterator it = q.descendingIterator();
878              for (int j = 1; j <= split; ++j)
879                  assertEquals(it.next(), new Integer(j));
880              it.remove();
881 <            assertEquals(it.next(), new Integer(split+1));
881 >            assertEquals(it.next(), new Integer(split + 1));
882              for (int j = 1; j <= split; ++j)
883                  q.remove(new Integer(j));
884              it = q.descendingIterator();
885 <            for (int j = split+1; j <= max; ++j) {
885 >            for (int j = split + 1; j <= max; ++j) {
886                  assertEquals(it.next(), new Integer(j));
887                  it.remove();
888              }
# Line 599 | Line 891 | public class ArrayDequeTest extends JSR1
891          }
892      }
893  
602
894      /**
895 <     * toString contains toStrings of elements
895 >     * toString() contains toStrings of elements
896       */
897      public void testToString() {
898          ArrayDeque q = populatedDeque(SIZE);
899          String s = q.toString();
900          for (int i = 0; i < SIZE; ++i) {
901 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
901 >            assertTrue(s.contains(String.valueOf(i)));
902          }
903      }
904  
905      /**
906 <     * peekFirst returns element inserted with addFirst
906 >     * A deserialized serialized deque has same elements in same order
907       */
908 <    public void testAddFirst() {
909 <        ArrayDeque q = populatedDeque(3);
910 <        q.addFirst(four);
911 <        assertSame(four, q.peekFirst());
908 >    public void testSerialization() throws Exception {
909 >        Queue x = populatedDeque(SIZE);
910 >        Queue y = serialClone(x);
911 >
912 >        assertNotSame(y, x);
913 >        assertEquals(x.size(), y.size());
914 >        assertEquals(x.toString(), y.toString());
915 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
916 >        while (!x.isEmpty()) {
917 >            assertFalse(y.isEmpty());
918 >            assertEquals(x.remove(), y.remove());
919 >        }
920 >        assertTrue(y.isEmpty());
921      }
922  
923      /**
924 <     * peekLast returns element inserted with addLast
924 >     * remove(null), contains(null) always return false
925       */
926 <    public void testAddLast() {
927 <        ArrayDeque q = populatedDeque(3);
928 <        q.addLast(four);
929 <        assertSame(four, q.peekLast());
926 >    public void testNeverContainsNull() {
927 >        Deque<?>[] qs = {
928 >            new ArrayDeque<Object>(),
929 >            populatedDeque(2),
930 >        };
931 >
932 >        for (Deque<?> q : qs) {
933 >            assertFalse(q.contains(null));
934 >            assertFalse(q.remove(null));
935 >            assertFalse(q.removeFirstOccurrence(null));
936 >            assertFalse(q.removeLastOccurrence(null));
937 >        }
938      }
939  
940   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines