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.12 by jsr166, Sun Nov 22 18:57:16 2009 UTC vs.
Revision 1.34 by jsr166, Sat Apr 25 04:55:30 2015 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() {
# Line 18 | Line 26 | public class ArrayDequeTest extends JSR1
26      }
27  
28      /**
29 <     * Create a queue of given size containing consecutive
29 >     * Returns a new deque of given size containing consecutive
30       * Integers 0 ... n.
31       */
32 <    private ArrayDeque populatedDeque(int n) {
33 <        ArrayDeque q = new ArrayDeque();
32 >    private ArrayDeque<Integer> populatedDeque(int n) {
33 >        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
34          assertTrue(q.isEmpty());
35          for (int i = 0; i < n; ++i)
36              assertTrue(q.offerLast(new Integer(i)));
# Line 32 | Line 40 | public class ArrayDequeTest extends JSR1
40      }
41  
42      /**
43 <     * new queue is empty
43 >     * new deque is empty
44       */
45      public void testConstructor1() {
46          assertEquals(0, new ArrayDeque().size());
# Line 43 | Line 51 | public class ArrayDequeTest extends JSR1
51       */
52      public void testConstructor3() {
53          try {
54 <            ArrayDeque q = new ArrayDeque((Collection)null);
54 >            new ArrayDeque((Collection)null);
55              shouldThrow();
56          } catch (NullPointerException success) {}
57      }
58  
59      /**
60 <     * Queue contains all elements of collection used to initialize
60 >     * Initializing from Collection of null elements throws NPE
61 >     */
62 >    public void testConstructor4() {
63 >        try {
64 >            Integer[] ints = new Integer[SIZE];
65 >            new ArrayDeque(Arrays.asList(ints));
66 >            shouldThrow();
67 >        } catch (NullPointerException success) {}
68 >    }
69  
70 +    /**
71 +     * Initializing from Collection with some null elements throws NPE
72 +     */
73 +    public void testConstructor5() {
74 +        try {
75 +            Integer[] ints = new Integer[SIZE];
76 +            for (int i = 0; i < SIZE-1; ++i)
77 +                ints[i] = new Integer(i);
78 +            new ArrayDeque(Arrays.asList(ints));
79 +            shouldThrow();
80 +        } catch (NullPointerException success) {}
81 +    }
82 +
83 +    /**
84 +     * Deque contains all elements of collection used to initialize
85       */
86      public void testConstructor6() {
87          Integer[] ints = new Integer[SIZE];
# Line 102 | Line 133 | public class ArrayDequeTest extends JSR1
133      }
134  
135      /**
136 <     * peekFirst returns element inserted with push
136 >     * peekFirst() returns element inserted with push
137       */
138      public void testPush() {
139          ArrayDeque q = populatedDeque(3);
# Line 112 | Line 143 | public class ArrayDequeTest extends JSR1
143      }
144  
145      /**
146 <     *  pop removes next element, or throws NSEE if empty
146 >     * pop() removes next element, or throws NSEE if empty
147       */
148      public void testPop() {
149          ArrayDeque q = populatedDeque(SIZE);
# Line 128 | Line 159 | public class ArrayDequeTest extends JSR1
159      /**
160       * offer(null) throws NPE
161       */
162 +    public void testOfferNull() {
163 +        try {
164 +            ArrayDeque q = new ArrayDeque();
165 +            q.offer(null);
166 +            shouldThrow();
167 +        } catch (NullPointerException success) {}
168 +    }
169 +
170 +    /**
171 +     * offerFirst(null) throws NPE
172 +     */
173      public void testOfferFirstNull() {
174          try {
175              ArrayDeque q = new ArrayDeque();
# Line 137 | Line 179 | public class ArrayDequeTest extends JSR1
179      }
180  
181      /**
182 <     * OfferFirst succeeds
182 >     * offerLast(null) throws NPE
183 >     */
184 >    public void testOfferLastNull() {
185 >        try {
186 >            ArrayDeque q = new ArrayDeque();
187 >            q.offerLast(null);
188 >            shouldThrow();
189 >        } catch (NullPointerException success) {}
190 >    }
191 >
192 >    /**
193 >     * offer(x) succeeds
194 >     */
195 >    public void testOffer() {
196 >        ArrayDeque q = new ArrayDeque();
197 >        assertTrue(q.offer(zero));
198 >        assertTrue(q.offer(one));
199 >        assertSame(zero, q.peekFirst());
200 >        assertSame(one, q.peekLast());
201 >    }
202 >
203 >    /**
204 >     * offerFirst(x) succeeds
205       */
206      public void testOfferFirst() {
207          ArrayDeque q = new ArrayDeque();
208 <        assertTrue(q.offerFirst(new Integer(0)));
209 <        assertTrue(q.offerFirst(new Integer(1)));
208 >        assertTrue(q.offerFirst(zero));
209 >        assertTrue(q.offerFirst(one));
210 >        assertSame(one, q.peekFirst());
211 >        assertSame(zero, q.peekLast());
212      }
213  
214      /**
215 <     * OfferLast succeeds
215 >     * offerLast(x) succeeds
216       */
217      public void testOfferLast() {
218          ArrayDeque q = new ArrayDeque();
219 <        assertTrue(q.offerLast(new Integer(0)));
220 <        assertTrue(q.offerLast(new Integer(1)));
219 >        assertTrue(q.offerLast(zero));
220 >        assertTrue(q.offerLast(one));
221 >        assertSame(zero, q.peekFirst());
222 >        assertSame(one, q.peekLast());
223 >    }
224 >
225 >    /**
226 >     * add(null) throws NPE
227 >     */
228 >    public void testAddNull() {
229 >        try {
230 >            ArrayDeque q = new ArrayDeque();
231 >            q.add(null);
232 >            shouldThrow();
233 >        } catch (NullPointerException success) {}
234 >    }
235 >
236 >    /**
237 >     * addFirst(null) throws NPE
238 >     */
239 >    public void testAddFirstNull() {
240 >        try {
241 >            ArrayDeque q = new ArrayDeque();
242 >            q.addFirst(null);
243 >            shouldThrow();
244 >        } catch (NullPointerException success) {}
245      }
246  
247      /**
248 <     * add succeeds
248 >     * addLast(null) throws NPE
249 >     */
250 >    public void testAddLastNull() {
251 >        try {
252 >            ArrayDeque q = new ArrayDeque();
253 >            q.addLast(null);
254 >            shouldThrow();
255 >        } catch (NullPointerException success) {}
256 >    }
257 >
258 >    /**
259 >     * add(x) succeeds
260       */
261      public void testAdd() {
262          ArrayDeque q = new ArrayDeque();
263 <        for (int i = 0; i < SIZE; ++i) {
264 <            assertEquals(i, q.size());
265 <            assertTrue(q.add(new Integer(i)));
266 <        }
263 >        assertTrue(q.add(zero));
264 >        assertTrue(q.add(one));
265 >        assertSame(zero, q.peekFirst());
266 >        assertSame(one, q.peekLast());
267 >    }
268 >
269 >    /**
270 >     * addFirst(x) succeeds
271 >     */
272 >    public void testAddFirst() {
273 >        ArrayDeque q = new ArrayDeque();
274 >        q.addFirst(zero);
275 >        q.addFirst(one);
276 >        assertSame(one, q.peekFirst());
277 >        assertSame(zero, q.peekLast());
278 >    }
279 >
280 >    /**
281 >     * addLast(x) succeeds
282 >     */
283 >    public void testAddLast() {
284 >        ArrayDeque q = new ArrayDeque();
285 >        q.addLast(zero);
286 >        q.addLast(one);
287 >        assertSame(zero, q.peekFirst());
288 >        assertSame(one, q.peekLast());
289      }
290  
291      /**
# Line 177 | Line 300 | public class ArrayDequeTest extends JSR1
300      }
301  
302      /**
303 <     * Queue contains all elements, in traversal order, of successful addAll
303 >     * addAll of a collection with null elements throws NPE
304 >     */
305 >    public void testAddAll2() {
306 >        try {
307 >            ArrayDeque q = new ArrayDeque();
308 >            Integer[] ints = new Integer[SIZE];
309 >            q.addAll(Arrays.asList(ints));
310 >            shouldThrow();
311 >        } catch (NullPointerException success) {}
312 >    }
313 >
314 >    /**
315 >     * addAll of a collection with any null elements throws NPE after
316 >     * possibly adding some elements
317 >     */
318 >    public void testAddAll3() {
319 >        try {
320 >            ArrayDeque q = new ArrayDeque();
321 >            Integer[] ints = new Integer[SIZE];
322 >            for (int i = 0; i < SIZE-1; ++i)
323 >                ints[i] = new Integer(i);
324 >            q.addAll(Arrays.asList(ints));
325 >            shouldThrow();
326 >        } catch (NullPointerException success) {}
327 >    }
328 >
329 >    /**
330 >     * Deque contains all elements, in traversal order, of successful addAll
331       */
332      public void testAddAll5() {
333          Integer[] empty = new Integer[0];
# Line 192 | Line 342 | public class ArrayDequeTest extends JSR1
342      }
343  
344      /**
345 <     *  pollFirst succeeds unless empty
345 >     * pollFirst() succeeds unless empty
346       */
347      public void testPollFirst() {
348          ArrayDeque q = populatedDeque(SIZE);
# Line 203 | Line 353 | public class ArrayDequeTest extends JSR1
353      }
354  
355      /**
356 <     *  pollLast succeeds unless empty
356 >     * pollLast() succeeds unless empty
357       */
358      public void testPollLast() {
359          ArrayDeque q = populatedDeque(SIZE);
# Line 214 | Line 364 | public class ArrayDequeTest extends JSR1
364      }
365  
366      /**
367 <     *  poll succeeds unless empty
367 >     * poll() succeeds unless empty
368       */
369      public void testPoll() {
370          ArrayDeque q = populatedDeque(SIZE);
# Line 225 | Line 375 | public class ArrayDequeTest extends JSR1
375      }
376  
377      /**
378 <     *  remove removes next element, or throws NSEE if empty
378 >     * remove() removes next element, or throws NSEE if empty
379       */
380      public void testRemove() {
381          ArrayDeque q = populatedDeque(SIZE);
# Line 239 | Line 389 | public class ArrayDequeTest extends JSR1
389      }
390  
391      /**
392 <     *  peekFirst returns next element, or null if empty
392 >     * remove(x) removes x and returns true if present
393 >     */
394 >    public void testRemoveElement() {
395 >        ArrayDeque q = populatedDeque(SIZE);
396 >        for (int i = 1; i < SIZE; i += 2) {
397 >            assertTrue(q.contains(i));
398 >            assertTrue(q.remove(i));
399 >            assertFalse(q.contains(i));
400 >            assertTrue(q.contains(i-1));
401 >        }
402 >        for (int i = 0; i < SIZE; i += 2) {
403 >            assertTrue(q.contains(i));
404 >            assertTrue(q.remove(i));
405 >            assertFalse(q.contains(i));
406 >            assertFalse(q.remove(i+1));
407 >            assertFalse(q.contains(i+1));
408 >        }
409 >        assertTrue(q.isEmpty());
410 >    }
411 >
412 >    /**
413 >     * peekFirst() returns next element, or null if empty
414       */
415      public void testPeekFirst() {
416          ArrayDeque q = populatedDeque(SIZE);
# Line 253 | Line 424 | public class ArrayDequeTest extends JSR1
424      }
425  
426      /**
427 <     *  peek returns next element, or null if empty
427 >     * peek() returns next element, or null if empty
428       */
429      public void testPeek() {
430          ArrayDeque q = populatedDeque(SIZE);
# Line 267 | Line 438 | public class ArrayDequeTest extends JSR1
438      }
439  
440      /**
441 <     *  peekLast returns next element, or null if empty
441 >     * peekLast() returns next element, or null if empty
442       */
443      public void testPeekLast() {
444          ArrayDeque q = populatedDeque(SIZE);
# Line 281 | Line 452 | public class ArrayDequeTest extends JSR1
452      }
453  
454      /**
455 <     * getFirst returns next getFirst, or throws NSEE if empty
455 >     * element() returns first element, or throws NSEE if empty
456 >     */
457 >    public void testElement() {
458 >        ArrayDeque q = populatedDeque(SIZE);
459 >        for (int i = 0; i < SIZE; ++i) {
460 >            assertEquals(i, q.element());
461 >            assertEquals(i, q.poll());
462 >        }
463 >        try {
464 >            q.element();
465 >            shouldThrow();
466 >        } catch (NoSuchElementException success) {}
467 >    }
468 >
469 >    /**
470 >     * getFirst() returns first element, or throws NSEE if empty
471       */
472      public void testFirstElement() {
473          ArrayDeque q = populatedDeque(SIZE);
# Line 296 | Line 482 | public class ArrayDequeTest extends JSR1
482      }
483  
484      /**
485 <     *  getLast returns next element, or throws NSEE if empty
485 >     * getLast() returns last element, or throws NSEE if empty
486       */
487      public void testLastElement() {
488          ArrayDeque q = populatedDeque(SIZE);
# Line 311 | Line 497 | public class ArrayDequeTest extends JSR1
497          assertNull(q.peekLast());
498      }
499  
314
500      /**
501 <     *  removeFirst removes next element, or throws NSEE if empty
501 >     * removeFirst() removes first element, or throws NSEE if empty
502       */
503      public void testRemoveFirst() {
504          ArrayDeque q = populatedDeque(SIZE);
# Line 324 | Line 509 | public class ArrayDequeTest extends JSR1
509              q.removeFirst();
510              shouldThrow();
511          } catch (NoSuchElementException success) {}
512 +        assertNull(q.peekFirst());
513 +    }
514 +
515 +    /**
516 +     * removeLast() removes last element, or throws NSEE if empty
517 +     */
518 +    public void testRemoveLast() {
519 +        ArrayDeque q = populatedDeque(SIZE);
520 +        for (int i = SIZE - 1; i >= 0; --i) {
521 +            assertEquals(i, q.removeLast());
522 +        }
523 +        try {
524 +            q.removeLast();
525 +            shouldThrow();
526 +        } catch (NoSuchElementException success) {}
527 +        assertNull(q.peekLast());
528      }
529  
530      /**
# Line 331 | Line 532 | public class ArrayDequeTest extends JSR1
532       */
533      public void testRemoveFirstOccurrence() {
534          ArrayDeque q = populatedDeque(SIZE);
535 <        for (int i = 1; i < SIZE; i+=2) {
535 >        for (int i = 1; i < SIZE; i += 2) {
536              assertTrue(q.removeFirstOccurrence(new Integer(i)));
537          }
538 <        for (int i = 0; i < SIZE; i+=2) {
538 >        for (int i = 0; i < SIZE; i += 2) {
539              assertTrue(q.removeFirstOccurrence(new Integer(i)));
540              assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
541          }
# Line 346 | Line 547 | public class ArrayDequeTest extends JSR1
547       */
548      public void testRemoveLastOccurrence() {
549          ArrayDeque q = populatedDeque(SIZE);
550 <        for (int i = 1; i < SIZE; i+=2) {
550 >        for (int i = 1; i < SIZE; i += 2) {
551              assertTrue(q.removeLastOccurrence(new Integer(i)));
552          }
553 <        for (int i = 0; i < SIZE; i+=2) {
553 >        for (int i = 0; i < SIZE; i += 2) {
554              assertTrue(q.removeLastOccurrence(new Integer(i)));
555              assertFalse(q.removeLastOccurrence(new Integer(i+1)));
556          }
# Line 426 | Line 627 | public class ArrayDequeTest extends JSR1
627          }
628      }
629  
630 +    void checkToArray(ArrayDeque q) {
631 +        int size = q.size();
632 +        Object[] o = q.toArray();
633 +        assertEquals(size, o.length);
634 +        Iterator it = q.iterator();
635 +        for (int i = 0; i < size; i++) {
636 +            Integer x = (Integer) it.next();
637 +            assertEquals((Integer)o[0] + i, (int) x);
638 +            assertSame(o[i], x);
639 +        }
640 +    }
641 +
642      /**
643 <     *  toArray contains all elements
643 >     * toArray() contains all elements in FIFO order
644       */
645      public void testToArray() {
646 <        ArrayDeque q = populatedDeque(SIZE);
647 <        Object[] o = q.toArray();
648 <        Arrays.sort(o);
649 <        for (int i = 0; i < o.length; i++)
650 <            assertEquals(o[i], q.pollFirst());
646 >        ArrayDeque q = new ArrayDeque();
647 >        for (int i = 0; i < SIZE; i++) {
648 >            checkToArray(q);
649 >            q.addLast(i);
650 >        }
651 >        // Provoke wraparound
652 >        for (int i = 0; i < SIZE; i++) {
653 >            checkToArray(q);
654 >            assertEquals(i, q.poll());
655 >            q.addLast(SIZE+i);
656 >        }
657 >        for (int i = 0; i < SIZE; i++) {
658 >            checkToArray(q);
659 >            assertEquals(SIZE+i, q.poll());
660 >        }
661 >    }
662 >
663 >    void checkToArray2(ArrayDeque q) {
664 >        int size = q.size();
665 >        Integer[] a1 = size == 0 ? null : new Integer[size-1];
666 >        Integer[] a2 = new Integer[size];
667 >        Integer[] a3 = new Integer[size+2];
668 >        if (size > 0) Arrays.fill(a1, 42);
669 >        Arrays.fill(a2, 42);
670 >        Arrays.fill(a3, 42);
671 >        Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1);
672 >        Integer[] b2 = (Integer[]) q.toArray(a2);
673 >        Integer[] b3 = (Integer[]) q.toArray(a3);
674 >        assertSame(a2, b2);
675 >        assertSame(a3, b3);
676 >        Iterator it = q.iterator();
677 >        for (int i = 0; i < size; i++) {
678 >            Integer x = (Integer) it.next();
679 >            assertSame(b1[i], x);
680 >            assertEquals(b1[0] + i, (int) x);
681 >            assertSame(b2[i], x);
682 >            assertSame(b3[i], x);
683 >        }
684 >        assertNull(a3[size]);
685 >        assertEquals(42, (int) a3[size+1]);
686 >        if (size > 0) {
687 >            assertNotSame(a1, b1);
688 >            assertEquals(size, b1.length);
689 >            for (int i = 0; i < a1.length; i++) {
690 >                assertEquals(42, (int) a1[i]);
691 >            }
692 >        }
693      }
694  
695      /**
696 <     *  toArray(a) contains all elements
696 >     * toArray(a) contains all elements in FIFO order
697       */
698      public void testToArray2() {
699 <        ArrayDeque q = populatedDeque(SIZE);
700 <        Integer[] ints = new Integer[SIZE];
701 <        ints = (Integer[])q.toArray(ints);
702 <        Arrays.sort(ints);
703 <        for (int i = 0; i < ints.length; i++)
704 <            assertEquals(ints[i], q.pollFirst());
699 >        ArrayDeque q = new ArrayDeque();
700 >        for (int i = 0; i < SIZE; i++) {
701 >            checkToArray2(q);
702 >            q.addLast(i);
703 >        }
704 >        // Provoke wraparound
705 >        for (int i = 0; i < SIZE; i++) {
706 >            checkToArray2(q);
707 >            assertEquals(i, q.poll());
708 >            q.addLast(SIZE+i);
709 >        }
710 >        for (int i = 0; i < SIZE; i++) {
711 >            checkToArray2(q);
712 >            assertEquals(SIZE+i, q.poll());
713 >        }
714      }
715  
716      /**
717 <     * toArray(null) throws NPE
717 >     * toArray(null) throws NullPointerException
718       */
719 <    public void testToArray_BadArg() {
719 >    public void testToArray_NullArg() {
720          ArrayDeque l = new ArrayDeque();
721          l.add(new Object());
722          try {
723 <            Object o[] = l.toArray(null);
723 >            l.toArray(null);
724              shouldThrow();
725          } catch (NullPointerException success) {}
726      }
727  
728      /**
729 <     * toArray with incompatable aray type throws CCE
729 >     * toArray(incompatible array type) throws ArrayStoreException
730       */
731      public void testToArray1_BadArg() {
732          ArrayDeque l = new ArrayDeque();
733          l.add(new Integer(5));
734          try {
735 <            Object o[] = l.toArray(new String[10]);
735 >            l.toArray(new String[10]);
736              shouldThrow();
737          } catch (ArrayStoreException success) {}
738      }
739  
740      /**
741 <     *  iterator iterates through all elements
741 >     * Iterator iterates through all elements
742       */
743      public void testIterator() {
744          ArrayDeque q = populatedDeque(SIZE);
481        int i = 0;
745          Iterator it = q.iterator();
746 <        while (it.hasNext()) {
746 >        int i;
747 >        for (i = 0; it.hasNext(); i++)
748              assertTrue(q.contains(it.next()));
485            ++i;
486        }
749          assertEquals(i, SIZE);
750 +        assertIteratorExhausted(it);
751      }
752  
753      /**
754 <     *  iterator ordering is FIFO
754 >     * iterator of empty collection has no elements
755 >     */
756 >    public void testEmptyIterator() {
757 >        Deque c = new ArrayDeque();
758 >        assertIteratorExhausted(c.iterator());
759 >        assertIteratorExhausted(c.descendingIterator());
760 >    }
761 >
762 >    /**
763 >     * Iterator ordering is FIFO
764       */
765      public void testIteratorOrdering() {
766          final ArrayDeque q = new ArrayDeque();
767 <        q.add(new Integer(1));
768 <        q.add(new Integer(2));
769 <        q.add(new Integer(3));
767 >        q.add(one);
768 >        q.add(two);
769 >        q.add(three);
770          int k = 0;
771          for (Iterator it = q.iterator(); it.hasNext();) {
772              assertEquals(++k, it.next());
# Line 504 | Line 776 | public class ArrayDequeTest extends JSR1
776      }
777  
778      /**
779 <     * iterator.remove removes current element
779 >     * iterator.remove() removes current element
780       */
781 <    public void testIteratorRemove () {
781 >    public void testIteratorRemove() {
782          final ArrayDeque q = new ArrayDeque();
783          final Random rng = new Random();
784          for (int iters = 0; iters < 100; ++iters) {
# Line 532 | Line 804 | public class ArrayDequeTest extends JSR1
804      }
805  
806      /**
807 <     *  Descending iterator iterates through all elements
807 >     * Descending iterator iterates through all elements
808       */
809      public void testDescendingIterator() {
810          ArrayDeque q = populatedDeque(SIZE);
# Line 551 | Line 823 | public class ArrayDequeTest extends JSR1
823      }
824  
825      /**
826 <     *  Descending iterator ordering is reverse FIFO
826 >     * Descending iterator ordering is reverse FIFO
827       */
828      public void testDescendingIteratorOrdering() {
829          final ArrayDeque q = new ArrayDeque();
# Line 572 | Line 844 | public class ArrayDequeTest extends JSR1
844      }
845  
846      /**
847 <     * descendingIterator.remove removes current element
847 >     * descendingIterator.remove() removes current element
848       */
849 <    public void testDescendingIteratorRemove () {
849 >    public void testDescendingIteratorRemove() {
850          final ArrayDeque q = new ArrayDeque();
851          final Random rng = new Random();
852          for (int iters = 0; iters < 100; ++iters) {
# Line 599 | Line 871 | public class ArrayDequeTest extends JSR1
871          }
872      }
873  
602
874      /**
875 <     * toString contains toStrings of elements
875 >     * toString() contains toStrings of elements
876       */
877      public void testToString() {
878          ArrayDeque q = populatedDeque(SIZE);
879          String s = q.toString();
880          for (int i = 0; i < SIZE; ++i) {
881 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
881 >            assertTrue(s.contains(String.valueOf(i)));
882          }
883      }
884  
885      /**
886 <     * peekFirst returns element inserted with addFirst
886 >     * A deserialized serialized deque has same elements in same order
887       */
888 <    public void testAddFirst() {
889 <        ArrayDeque q = populatedDeque(3);
890 <        q.addFirst(four);
891 <        assertEquals(four,q.peekFirst());
888 >    public void testSerialization() throws Exception {
889 >        Queue x = populatedDeque(SIZE);
890 >        Queue y = serialClone(x);
891 >
892 >        assertNotSame(y, x);
893 >        assertEquals(x.size(), y.size());
894 >        assertEquals(x.toString(), y.toString());
895 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
896 >        while (!x.isEmpty()) {
897 >            assertFalse(y.isEmpty());
898 >            assertEquals(x.remove(), y.remove());
899 >        }
900 >        assertTrue(y.isEmpty());
901      }
902  
903      /**
904 <     * peekLast returns element inserted with addLast
904 >     * remove(null), contains(null) always return false
905       */
906 <    public void testAddLast() {
907 <        ArrayDeque q = populatedDeque(3);
908 <        q.addLast(four);
909 <        assertEquals(four,q.peekLast());
906 >    public void testNeverContainsNull() {
907 >        Deque<?>[] qs = {
908 >            new ArrayDeque<Object>(),
909 >            populatedDeque(2),
910 >        };
911 >
912 >        for (Deque<?> q : qs) {
913 >            assertFalse(q.contains(null));
914 >            assertFalse(q.remove(null));
915 >            assertFalse(q.removeFirstOccurrence(null));
916 >            assertFalse(q.removeLastOccurrence(null));
917 >        }
918      }
919  
920   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines