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.10 by jsr166, Sat Nov 21 10:29:49 2009 UTC vs.
Revision 1.56 by jsr166, Fri Aug 4 03:30:21 2017 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.Deque;
12 > import java.util.Iterator;
13 > import java.util.NoSuchElementException;
14 > import java.util.Queue;
15 > import java.util.Random;
16 > import java.util.concurrent.ThreadLocalRandom;
17 >
18 > import junit.framework.Test;
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 populatedDeque(0); }
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 static ArrayDeque<Integer> populatedDeque(int n) {
42 >        // Randomize various aspects of memory layout, including
43 >        // capacity slop 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>(Math.max(0, 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.removeFirst());
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)));
70 <        assertFalse(q.isEmpty());
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          assertEquals(n, q.size());
75 +        if (n > 0) {
76 +            assertFalse(q.isEmpty());
77 +            assertEquals((Integer) 0, q.peekFirst());
78 +            assertEquals((Integer) (n - 1), q.peekLast());
79 +        }
80          return q;
81      }
82  
83      /**
84 <     * new queue is empty
84 >     * new deque is empty
85       */
86      public void testConstructor1() {
87          assertEquals(0, new ArrayDeque().size());
# Line 43 | Line 92 | public class ArrayDequeTest extends JSR1
92       */
93      public void testConstructor3() {
94          try {
95 <            ArrayDeque q = new ArrayDeque((Collection)null);
95 >            new ArrayDeque((Collection)null);
96              shouldThrow();
97          } catch (NullPointerException success) {}
98      }
99  
100      /**
101 <     * Queue contains all elements of collection used to initialize
101 >     * Initializing from Collection of null elements throws NPE
102 >     */
103 >    public void testConstructor4() {
104 >        try {
105 >            new ArrayDeque(Arrays.asList(new Integer[SIZE]));
106 >            shouldThrow();
107 >        } catch (NullPointerException success) {}
108 >    }
109  
110 +    /**
111 +     * Initializing from Collection with some null elements throws NPE
112 +     */
113 +    public void testConstructor5() {
114 +        Integer[] ints = new Integer[SIZE];
115 +        for (int i = 0; i < SIZE - 1; ++i)
116 +            ints[i] = new Integer(i);
117 +        try {
118 +            new ArrayDeque(Arrays.asList(ints));
119 +            shouldThrow();
120 +        } catch (NullPointerException success) {}
121 +    }
122 +
123 +    /**
124 +     * Deque contains all elements of collection used to initialize
125       */
126      public void testConstructor6() {
127          Integer[] ints = new Integer[SIZE];
# Line 81 | Line 152 | public class ArrayDequeTest extends JSR1
152      public void testSize() {
153          ArrayDeque q = populatedDeque(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
155 <            assertEquals(SIZE-i, q.size());
155 >            assertEquals(SIZE - i, q.size());
156              q.removeFirst();
157          }
158          for (int i = 0; i < SIZE; ++i) {
# Line 94 | Line 165 | public class ArrayDequeTest extends JSR1
165       * push(null) throws NPE
166       */
167      public void testPushNull() {
168 +        ArrayDeque q = new ArrayDeque(1);
169          try {
98            ArrayDeque q = new ArrayDeque(1);
170              q.push(null);
171              shouldThrow();
172          } catch (NullPointerException success) {}
173      }
174  
175      /**
176 <     * peekFirst returns element inserted with push
176 >     * peekFirst() returns element inserted with push
177       */
178      public void testPush() {
179          ArrayDeque q = populatedDeque(3);
180          q.pollLast();
181          q.push(four);
182 <        assertEquals(four,q.peekFirst());
182 >        assertSame(four, q.peekFirst());
183      }
184  
185      /**
186 <     *  pop removes next element, or throws NSEE if empty
186 >     * pop() removes next element, or throws NSEE if empty
187       */
188      public void testPop() {
189          ArrayDeque q = populatedDeque(SIZE);
190          for (int i = 0; i < SIZE; ++i) {
191 <            assertEquals(i, ((Integer)q.pop()).intValue());
191 >            assertEquals(i, q.pop());
192          }
193          try {
194              q.pop();
# Line 128 | Line 199 | public class ArrayDequeTest extends JSR1
199      /**
200       * offer(null) throws NPE
201       */
202 +    public void testOfferNull() {
203 +        ArrayDeque q = new ArrayDeque();
204 +        try {
205 +            q.offer(null);
206 +            shouldThrow();
207 +        } catch (NullPointerException success) {}
208 +    }
209 +
210 +    /**
211 +     * offerFirst(null) throws NPE
212 +     */
213      public void testOfferFirstNull() {
214 +        ArrayDeque q = new ArrayDeque();
215          try {
133            ArrayDeque q = new ArrayDeque();
216              q.offerFirst(null);
217              shouldThrow();
218          } catch (NullPointerException success) {}
219      }
220  
221      /**
222 <     * OfferFirst succeeds
222 >     * offerLast(null) throws NPE
223 >     */
224 >    public void testOfferLastNull() {
225 >        ArrayDeque q = new ArrayDeque();
226 >        try {
227 >            q.offerLast(null);
228 >            shouldThrow();
229 >        } catch (NullPointerException success) {}
230 >    }
231 >
232 >    /**
233 >     * offer(x) succeeds
234 >     */
235 >    public void testOffer() {
236 >        ArrayDeque q = new ArrayDeque();
237 >        assertTrue(q.offer(zero));
238 >        assertTrue(q.offer(one));
239 >        assertSame(zero, q.peekFirst());
240 >        assertSame(one, q.peekLast());
241 >    }
242 >
243 >    /**
244 >     * offerFirst(x) succeeds
245       */
246      public void testOfferFirst() {
247          ArrayDeque q = new ArrayDeque();
248 <        assertTrue(q.offerFirst(new Integer(0)));
249 <        assertTrue(q.offerFirst(new Integer(1)));
248 >        assertTrue(q.offerFirst(zero));
249 >        assertTrue(q.offerFirst(one));
250 >        assertSame(one, q.peekFirst());
251 >        assertSame(zero, q.peekLast());
252      }
253  
254      /**
255 <     * OfferLast succeeds
255 >     * offerLast(x) succeeds
256       */
257      public void testOfferLast() {
258          ArrayDeque q = new ArrayDeque();
259 <        assertTrue(q.offerLast(new Integer(0)));
260 <        assertTrue(q.offerLast(new Integer(1)));
259 >        assertTrue(q.offerLast(zero));
260 >        assertTrue(q.offerLast(one));
261 >        assertSame(zero, q.peekFirst());
262 >        assertSame(one, q.peekLast());
263 >    }
264 >
265 >    /**
266 >     * add(null) throws NPE
267 >     */
268 >    public void testAddNull() {
269 >        ArrayDeque q = new ArrayDeque();
270 >        try {
271 >            q.add(null);
272 >            shouldThrow();
273 >        } catch (NullPointerException success) {}
274      }
275  
276      /**
277 <     * add succeeds
277 >     * addFirst(null) throws NPE
278 >     */
279 >    public void testAddFirstNull() {
280 >        ArrayDeque q = new ArrayDeque();
281 >        try {
282 >            q.addFirst(null);
283 >            shouldThrow();
284 >        } catch (NullPointerException success) {}
285 >    }
286 >
287 >    /**
288 >     * addLast(null) throws NPE
289 >     */
290 >    public void testAddLastNull() {
291 >        ArrayDeque q = new ArrayDeque();
292 >        try {
293 >            q.addLast(null);
294 >            shouldThrow();
295 >        } catch (NullPointerException success) {}
296 >    }
297 >
298 >    /**
299 >     * add(x) succeeds
300       */
301      public void testAdd() {
302          ArrayDeque q = new ArrayDeque();
303 <        for (int i = 0; i < SIZE; ++i) {
304 <            assertEquals(i, q.size());
305 <            assertTrue(q.add(new Integer(i)));
306 <        }
303 >        assertTrue(q.add(zero));
304 >        assertTrue(q.add(one));
305 >        assertSame(zero, q.peekFirst());
306 >        assertSame(one, q.peekLast());
307 >    }
308 >
309 >    /**
310 >     * addFirst(x) succeeds
311 >     */
312 >    public void testAddFirst() {
313 >        ArrayDeque q = new ArrayDeque();
314 >        q.addFirst(zero);
315 >        q.addFirst(one);
316 >        assertSame(one, q.peekFirst());
317 >        assertSame(zero, q.peekLast());
318 >    }
319 >
320 >    /**
321 >     * addLast(x) succeeds
322 >     */
323 >    public void testAddLast() {
324 >        ArrayDeque q = new ArrayDeque();
325 >        q.addLast(zero);
326 >        q.addLast(one);
327 >        assertSame(zero, q.peekFirst());
328 >        assertSame(one, q.peekLast());
329      }
330  
331      /**
332       * addAll(null) throws NPE
333       */
334      public void testAddAll1() {
335 +        ArrayDeque q = new ArrayDeque();
336          try {
173            ArrayDeque q = new ArrayDeque();
337              q.addAll(null);
338              shouldThrow();
339          } catch (NullPointerException success) {}
340      }
341  
342      /**
343 <     * Queue contains all elements, in traversal order, of successful addAll
343 >     * addAll of a collection with null elements throws NPE
344 >     */
345 >    public void testAddAll2() {
346 >        ArrayDeque q = new ArrayDeque();
347 >        try {
348 >            q.addAll(Arrays.asList(new Integer[SIZE]));
349 >            shouldThrow();
350 >        } catch (NullPointerException success) {}
351 >    }
352 >
353 >    /**
354 >     * addAll of a collection with any null elements throws NPE after
355 >     * possibly adding some elements
356 >     */
357 >    public void testAddAll3() {
358 >        ArrayDeque q = new ArrayDeque();
359 >        Integer[] ints = new Integer[SIZE];
360 >        for (int i = 0; i < SIZE - 1; ++i)
361 >            ints[i] = new Integer(i);
362 >        try {
363 >            q.addAll(Arrays.asList(ints));
364 >            shouldThrow();
365 >        } catch (NullPointerException success) {}
366 >    }
367 >
368 >    /**
369 >     * Deque contains all elements, in traversal order, of successful addAll
370       */
371      public void testAddAll5() {
372          Integer[] empty = new Integer[0];
# Line 192 | Line 381 | public class ArrayDequeTest extends JSR1
381      }
382  
383      /**
384 <     *  pollFirst succeeds unless empty
384 >     * pollFirst() succeeds unless empty
385       */
386      public void testPollFirst() {
387          ArrayDeque q = populatedDeque(SIZE);
388          for (int i = 0; i < SIZE; ++i) {
389 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
389 >            assertEquals(i, q.pollFirst());
390          }
391          assertNull(q.pollFirst());
392      }
393  
394      /**
395 <     *  pollLast succeeds unless empty
395 >     * pollLast() succeeds unless empty
396       */
397      public void testPollLast() {
398          ArrayDeque q = populatedDeque(SIZE);
399 <        for (int i = SIZE-1; i >= 0; --i) {
400 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
399 >        for (int i = SIZE - 1; i >= 0; --i) {
400 >            assertEquals(i, q.pollLast());
401          }
402          assertNull(q.pollLast());
403      }
404  
405      /**
406 <     *  poll succeeds unless empty
406 >     * poll() succeeds unless empty
407       */
408      public void testPoll() {
409          ArrayDeque q = populatedDeque(SIZE);
410          for (int i = 0; i < SIZE; ++i) {
411 <            assertEquals(i, ((Integer)q.poll()).intValue());
411 >            assertEquals(i, q.poll());
412          }
413          assertNull(q.poll());
414      }
415  
416      /**
417 <     *  remove removes next element, or throws NSEE if empty
417 >     * remove() removes next element, or throws NSEE if empty
418       */
419      public void testRemove() {
420          ArrayDeque q = populatedDeque(SIZE);
421          for (int i = 0; i < SIZE; ++i) {
422 <            assertEquals(i, ((Integer)q.remove()).intValue());
422 >            assertEquals(i, q.remove());
423          }
424          try {
425              q.remove();
# Line 239 | Line 428 | public class ArrayDequeTest extends JSR1
428      }
429  
430      /**
431 <     *  peekFirst returns next element, or null if empty
431 >     * remove(x) removes x and returns true if present
432 >     */
433 >    public void testRemoveElement() {
434 >        ArrayDeque q = populatedDeque(SIZE);
435 >        for (int i = 1; i < SIZE; i += 2) {
436 >            assertTrue(q.contains(i));
437 >            assertTrue(q.remove(i));
438 >            assertFalse(q.contains(i));
439 >            assertTrue(q.contains(i - 1));
440 >        }
441 >        for (int i = 0; i < SIZE; i += 2) {
442 >            assertTrue(q.contains(i));
443 >            assertTrue(q.remove(i));
444 >            assertFalse(q.contains(i));
445 >            assertFalse(q.remove(i + 1));
446 >            assertFalse(q.contains(i + 1));
447 >        }
448 >        assertTrue(q.isEmpty());
449 >    }
450 >
451 >    /**
452 >     * peekFirst() returns next element, or null if empty
453       */
454      public void testPeekFirst() {
455          ArrayDeque q = populatedDeque(SIZE);
456          for (int i = 0; i < SIZE; ++i) {
457 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
458 <            q.pollFirst();
457 >            assertEquals(i, q.peekFirst());
458 >            assertEquals(i, q.pollFirst());
459              assertTrue(q.peekFirst() == null ||
460 <                       i != ((Integer)q.peekFirst()).intValue());
460 >                       !q.peekFirst().equals(i));
461          }
462          assertNull(q.peekFirst());
463      }
464  
465      /**
466 <     *  peek returns next element, or null if empty
466 >     * peek() returns next element, or null if empty
467       */
468      public void testPeek() {
469          ArrayDeque q = populatedDeque(SIZE);
470          for (int i = 0; i < SIZE; ++i) {
471 <            assertEquals(i, ((Integer)q.peek()).intValue());
472 <            q.poll();
471 >            assertEquals(i, q.peek());
472 >            assertEquals(i, q.poll());
473              assertTrue(q.peek() == null ||
474 <                       i != ((Integer)q.peek()).intValue());
474 >                       !q.peek().equals(i));
475          }
476          assertNull(q.peek());
477      }
478  
479      /**
480 <     *  peekLast returns next element, or null if empty
480 >     * peekLast() returns next element, or null if empty
481       */
482      public void testPeekLast() {
483          ArrayDeque q = populatedDeque(SIZE);
484 <        for (int i = SIZE-1; i >= 0; --i) {
485 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
486 <            q.pollLast();
484 >        for (int i = SIZE - 1; i >= 0; --i) {
485 >            assertEquals(i, q.peekLast());
486 >            assertEquals(i, q.pollLast());
487              assertTrue(q.peekLast() == null ||
488 <                       i != ((Integer)q.peekLast()).intValue());
488 >                       !q.peekLast().equals(i));
489          }
490          assertNull(q.peekLast());
491      }
492  
493      /**
494 <     * getFirst returns next getFirst, or throws NSEE if empty
494 >     * element() returns first element, or throws NSEE if empty
495 >     */
496 >    public void testElement() {
497 >        ArrayDeque q = populatedDeque(SIZE);
498 >        for (int i = 0; i < SIZE; ++i) {
499 >            assertEquals(i, q.element());
500 >            assertEquals(i, q.poll());
501 >        }
502 >        try {
503 >            q.element();
504 >            shouldThrow();
505 >        } catch (NoSuchElementException success) {}
506 >    }
507 >
508 >    /**
509 >     * getFirst() returns first element, or throws NSEE if empty
510       */
511      public void testFirstElement() {
512          ArrayDeque q = populatedDeque(SIZE);
513          for (int i = 0; i < SIZE; ++i) {
514 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
515 <            q.pollFirst();
514 >            assertEquals(i, q.getFirst());
515 >            assertEquals(i, q.pollFirst());
516          }
517          try {
518              q.getFirst();
# Line 296 | Line 521 | public class ArrayDequeTest extends JSR1
521      }
522  
523      /**
524 <     *  getLast returns next element, or throws NSEE if empty
524 >     * getLast() returns last element, or throws NSEE if empty
525       */
526      public void testLastElement() {
527          ArrayDeque q = populatedDeque(SIZE);
528 <        for (int i = SIZE-1; i >= 0; --i) {
529 <            assertEquals(i, ((Integer)q.getLast()).intValue());
530 <            q.pollLast();
528 >        for (int i = SIZE - 1; i >= 0; --i) {
529 >            assertEquals(i, q.getLast());
530 >            assertEquals(i, q.pollLast());
531          }
532          try {
533              q.getLast();
# Line 311 | Line 536 | public class ArrayDequeTest extends JSR1
536          assertNull(q.peekLast());
537      }
538  
314
539      /**
540 <     *  removeFirst removes next element, or throws NSEE if empty
540 >     * removeFirst() removes first element, or throws NSEE if empty
541       */
542      public void testRemoveFirst() {
543          ArrayDeque q = populatedDeque(SIZE);
544          for (int i = 0; i < SIZE; ++i) {
545 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
545 >            assertEquals(i, q.removeFirst());
546          }
547          try {
548              q.removeFirst();
549              shouldThrow();
550          } catch (NoSuchElementException success) {}
551 +        assertNull(q.peekFirst());
552      }
553  
554      /**
555 <     * removeFirstOccurrence(x) removes x and returns true if present
555 >     * removeLast() removes last element, or throws NSEE if empty
556       */
557 <    public void testRemoveFirstOccurrence() {
557 >    public void testRemoveLast() {
558          ArrayDeque q = populatedDeque(SIZE);
559 <        for (int i = 1; i < SIZE; i+=2) {
560 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
559 >        for (int i = SIZE - 1; i >= 0; --i) {
560 >            assertEquals(i, q.removeLast());
561          }
562 <        for (int i = 0; i < SIZE; i+=2) {
563 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
564 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
562 >        try {
563 >            q.removeLast();
564 >            shouldThrow();
565 >        } catch (NoSuchElementException success) {}
566 >        assertNull(q.peekLast());
567 >    }
568 >
569 >    /**
570 >     * removeFirstOccurrence(x) removes x and returns true if present
571 >     */
572 >    public void testRemoveFirstOccurrence() {
573 >        Deque<Integer> q = populatedDeque(SIZE);
574 >        assertFalse(q.removeFirstOccurrence(null));
575 >        for (int i = 1; i < SIZE; i += 2) {
576 >            assertTrue(q.removeFirstOccurrence(i));
577 >            assertFalse(q.contains(i));
578 >        }
579 >        for (int i = 0; i < SIZE; i += 2) {
580 >            assertTrue(q.removeFirstOccurrence(i));
581 >            assertFalse(q.removeFirstOccurrence(i + 1));
582 >            assertFalse(q.contains(i));
583 >            assertFalse(q.contains(i + 1));
584          }
585          assertTrue(q.isEmpty());
586 +        assertFalse(q.removeFirstOccurrence(null));
587 +        assertFalse(q.removeFirstOccurrence(42));
588 +        q = new ArrayDeque();
589 +        assertFalse(q.removeFirstOccurrence(null));
590 +        assertFalse(q.removeFirstOccurrence(42));
591      }
592  
593      /**
594       * removeLastOccurrence(x) removes x and returns true if present
595       */
596      public void testRemoveLastOccurrence() {
597 <        ArrayDeque q = populatedDeque(SIZE);
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) {
602 <            assertTrue(q.removeLastOccurrence(new Integer(i)));
603 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
597 >        Deque<Integer> q = populatedDeque(SIZE);
598 >        assertFalse(q.removeLastOccurrence(null));
599 >        for (int i = 1; i < SIZE; i += 2) {
600 >            assertTrue(q.removeLastOccurrence(i));
601 >            assertFalse(q.contains(i));
602 >        }
603 >        for (int i = 0; i < SIZE; i += 2) {
604 >            assertTrue(q.removeLastOccurrence(i));
605 >            assertFalse(q.removeLastOccurrence(i + 1));
606 >            assertFalse(q.contains(i));
607 >            assertFalse(q.contains(i + 1));
608          }
609          assertTrue(q.isEmpty());
610 +        assertFalse(q.removeLastOccurrence(null));
611 +        assertFalse(q.removeLastOccurrence(42));
612 +        q = new ArrayDeque();
613 +        assertFalse(q.removeLastOccurrence(null));
614 +        assertFalse(q.removeLastOccurrence(42));
615      }
616  
617      /**
# Line 363 | Line 621 | public class ArrayDequeTest extends JSR1
621          ArrayDeque q = populatedDeque(SIZE);
622          for (int i = 0; i < SIZE; ++i) {
623              assertTrue(q.contains(new Integer(i)));
624 <            q.pollFirst();
624 >            assertEquals(i, q.pollFirst());
625              assertFalse(q.contains(new Integer(i)));
626          }
627      }
# Line 376 | Line 634 | public class ArrayDequeTest extends JSR1
634          q.clear();
635          assertTrue(q.isEmpty());
636          assertEquals(0, q.size());
637 <        q.add(new Integer(1));
637 >        assertTrue(q.add(new Integer(1)));
638          assertFalse(q.isEmpty());
639          q.clear();
640          assertTrue(q.isEmpty());
# Line 391 | Line 649 | public class ArrayDequeTest extends JSR1
649          for (int i = 0; i < SIZE; ++i) {
650              assertTrue(q.containsAll(p));
651              assertFalse(p.containsAll(q));
652 <            p.add(new Integer(i));
652 >            assertTrue(p.add(new Integer(i)));
653          }
654          assertTrue(p.containsAll(q));
655      }
# Line 404 | Line 662 | public class ArrayDequeTest extends JSR1
662          ArrayDeque p = populatedDeque(SIZE);
663          for (int i = 0; i < SIZE; ++i) {
664              boolean changed = q.retainAll(p);
665 <            if (i == 0)
408 <                assertFalse(changed);
409 <            else
410 <                assertTrue(changed);
411 <
665 >            assertEquals(changed, (i > 0));
666              assertTrue(q.containsAll(p));
667 <            assertEquals(SIZE-i, q.size());
667 >            assertEquals(SIZE - i, q.size());
668              p.removeFirst();
669          }
670      }
# Line 423 | Line 677 | public class ArrayDequeTest extends JSR1
677              ArrayDeque q = populatedDeque(SIZE);
678              ArrayDeque p = populatedDeque(i);
679              assertTrue(q.removeAll(p));
680 <            assertEquals(SIZE-i, q.size());
680 >            assertEquals(SIZE - i, q.size());
681              for (int j = 0; j < i; ++j) {
682 <                Integer I = (Integer)(p.removeFirst());
429 <                assertFalse(q.contains(I));
682 >                assertFalse(q.contains(p.removeFirst()));
683              }
684          }
685      }
686  
687 <    /**
688 <     *  toArray contains all elements
689 <     */
690 <    public void testToArray() {
691 <        ArrayDeque q = populatedDeque(SIZE);
692 <        Object[] o = q.toArray();
693 <        Arrays.sort(o);
694 <        for (int i = 0; i < o.length; i++)
695 <            assertEquals(o[i], q.pollFirst());
687 >    void checkToArray(ArrayDeque<Integer> q) {
688 >        int size = q.size();
689 >        Object[] a1 = q.toArray();
690 >        assertEquals(size, a1.length);
691 >        Integer[] a2 = q.toArray(new Integer[0]);
692 >        assertEquals(size, a2.length);
693 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
694 >        assertEquals(size, a3.length);
695 >        Integer[] a4 = new Integer[size];
696 >        assertSame(a4, q.toArray(a4));
697 >        Integer[] a5 = new Integer[size + 1];
698 >        Arrays.fill(a5, 42);
699 >        assertSame(a5, q.toArray(a5));
700 >        Integer[] a6 = new Integer[size + 2];
701 >        Arrays.fill(a6, 42);
702 >        assertSame(a6, q.toArray(a6));
703 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
704 >        for (Object[] a : as) {
705 >            if (a.length > size) assertNull(a[size]);
706 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
707 >        }
708 >        Iterator it = q.iterator();
709 >        Integer s = q.peekFirst();
710 >        for (int i = 0; i < size; i++) {
711 >            Integer x = (Integer) it.next();
712 >            assertEquals(s + i, (int) x);
713 >            for (Object[] a : as)
714 >                assertSame(a1[i], x);
715 >        }
716      }
717  
718      /**
719 <     *  toArray(a) contains all elements
719 >     * toArray() and toArray(a) contain all elements in FIFO order
720       */
721 <    public void testToArray2() {
722 <        ArrayDeque q = populatedDeque(SIZE);
723 <        Integer[] ints = new Integer[SIZE];
724 <        ints = (Integer[])q.toArray(ints);
725 <        Arrays.sort(ints);
726 <        for (int i = 0; i < ints.length; i++)
727 <            assertEquals(ints[i], q.pollFirst());
721 >    public void testToArray() {
722 >        final int size = ThreadLocalRandom.current().nextInt(10);
723 >        ArrayDeque<Integer> q = new ArrayDeque<>(size);
724 >        for (int i = 0; i < size; i++) {
725 >            checkToArray(q);
726 >            q.addLast(i);
727 >        }
728 >        // Provoke wraparound
729 >        int added = size * 2;
730 >        for (int i = 0; i < added; i++) {
731 >            checkToArray(q);
732 >            assertEquals((Integer) i, q.poll());
733 >            q.addLast(size + i);
734 >        }
735 >        for (int i = 0; i < size; i++) {
736 >            checkToArray(q);
737 >            assertEquals((Integer) (added + i), q.poll());
738 >        }
739      }
740  
741      /**
742 <     * toArray(null) throws NPE
742 >     * toArray(null) throws NullPointerException
743       */
744 <    public void testToArray_BadArg() {
744 >    public void testToArray_NullArg() {
745 >        ArrayDeque l = new ArrayDeque();
746 >        l.add(new Object());
747          try {
748 <            ArrayDeque l = new ArrayDeque();
463 <            l.add(new Object());
464 <            Object o[] = l.toArray(null);
748 >            l.toArray(null);
749              shouldThrow();
750          } catch (NullPointerException success) {}
751      }
752  
753      /**
754 <     * toArray with incompatable aray type throws CCE
754 >     * toArray(incompatible array type) throws ArrayStoreException
755       */
756 <    public void testToArray1_BadArg() {
756 >    public void testToArray_incompatibleArrayType() {
757 >        ArrayDeque l = new ArrayDeque();
758 >        l.add(new Integer(5));
759          try {
760 <            ArrayDeque l = new ArrayDeque();
761 <            l.add(new Integer(5));
762 <            Object o[] = l.toArray(new String[10] );
760 >            l.toArray(new String[10]);
761 >            shouldThrow();
762 >        } catch (ArrayStoreException success) {}
763 >        try {
764 >            l.toArray(new String[0]);
765              shouldThrow();
766          } catch (ArrayStoreException success) {}
767      }
768  
769      /**
770 <     *  iterator iterates through all elements
770 >     * Iterator iterates through all elements
771       */
772      public void testIterator() {
773          ArrayDeque q = populatedDeque(SIZE);
486        int i = 0;
774          Iterator it = q.iterator();
775 <        while (it.hasNext()) {
775 >        int i;
776 >        for (i = 0; it.hasNext(); i++)
777              assertTrue(q.contains(it.next()));
490            ++i;
491        }
778          assertEquals(i, SIZE);
779 +        assertIteratorExhausted(it);
780      }
781  
782      /**
783 <     *  iterator ordering is FIFO
783 >     * iterator of empty collection has no elements
784 >     */
785 >    public void testEmptyIterator() {
786 >        Deque c = new ArrayDeque();
787 >        assertIteratorExhausted(c.iterator());
788 >        assertIteratorExhausted(c.descendingIterator());
789 >    }
790 >
791 >    /**
792 >     * Iterator ordering is FIFO
793       */
794      public void testIteratorOrdering() {
795          final ArrayDeque q = new ArrayDeque();
796 <        q.add(new Integer(1));
797 <        q.add(new Integer(2));
798 <        q.add(new Integer(3));
796 >        q.add(one);
797 >        q.add(two);
798 >        q.add(three);
799          int k = 0;
800          for (Iterator it = q.iterator(); it.hasNext();) {
801 <            int i = ((Integer)(it.next())).intValue();
506 <            assertEquals(++k, i);
801 >            assertEquals(++k, it.next());
802          }
803  
804          assertEquals(3, k);
805      }
806  
807      /**
808 <     * iterator.remove removes current element
808 >     * iterator.remove() removes current element
809       */
810 <    public void testIteratorRemove () {
810 >    public void testIteratorRemove() {
811          final ArrayDeque q = new ArrayDeque();
812          final Random rng = new Random();
813          for (int iters = 0; iters < 100; ++iters) {
814              int max = rng.nextInt(5) + 2;
815 <            int split = rng.nextInt(max-1) + 1;
815 >            int split = rng.nextInt(max - 1) + 1;
816              for (int j = 1; j <= max; ++j)
817                  q.add(new Integer(j));
818              Iterator it = q.iterator();
819              for (int j = 1; j <= split; ++j)
820                  assertEquals(it.next(), new Integer(j));
821              it.remove();
822 <            assertEquals(it.next(), new Integer(split+1));
822 >            assertEquals(it.next(), new Integer(split + 1));
823              for (int j = 1; j <= split; ++j)
824                  q.remove(new Integer(j));
825              it = q.iterator();
826 <            for (int j = split+1; j <= max; ++j) {
826 >            for (int j = split + 1; j <= max; ++j) {
827                  assertEquals(it.next(), new Integer(j));
828                  it.remove();
829              }
# Line 538 | Line 833 | public class ArrayDequeTest extends JSR1
833      }
834  
835      /**
836 <     *  Descending iterator iterates through all elements
836 >     * Descending iterator iterates through all elements
837       */
838      public void testDescendingIterator() {
839          ArrayDeque q = populatedDeque(SIZE);
# Line 552 | Line 847 | public class ArrayDequeTest extends JSR1
847          assertFalse(it.hasNext());
848          try {
849              it.next();
850 +            shouldThrow();
851          } catch (NoSuchElementException success) {}
852      }
853  
854      /**
855 <     *  Descending iterator ordering is reverse FIFO
855 >     * Descending iterator ordering is reverse FIFO
856       */
857      public void testDescendingIteratorOrdering() {
858          final ArrayDeque q = new ArrayDeque();
# Line 566 | Line 862 | public class ArrayDequeTest extends JSR1
862              q.add(new Integer(1));
863              int k = 0;
864              for (Iterator it = q.descendingIterator(); it.hasNext();) {
865 <                int i = ((Integer)(it.next())).intValue();
570 <                assertEquals(++k, i);
865 >                assertEquals(++k, it.next());
866              }
867  
868              assertEquals(3, k);
# Line 578 | Line 873 | public class ArrayDequeTest extends JSR1
873      }
874  
875      /**
876 <     * descendingIterator.remove removes current element
876 >     * descendingIterator.remove() removes current element
877       */
878 <    public void testDescendingIteratorRemove () {
878 >    public void testDescendingIteratorRemove() {
879          final ArrayDeque q = new ArrayDeque();
880          final Random rng = new Random();
881          for (int iters = 0; iters < 100; ++iters) {
882              int max = rng.nextInt(5) + 2;
883 <            int split = rng.nextInt(max-1) + 1;
883 >            int split = rng.nextInt(max - 1) + 1;
884              for (int j = max; j >= 1; --j)
885                  q.add(new Integer(j));
886              Iterator it = q.descendingIterator();
887              for (int j = 1; j <= split; ++j)
888                  assertEquals(it.next(), new Integer(j));
889              it.remove();
890 <            assertEquals(it.next(), new Integer(split+1));
890 >            assertEquals(it.next(), new Integer(split + 1));
891              for (int j = 1; j <= split; ++j)
892                  q.remove(new Integer(j));
893              it = q.descendingIterator();
894 <            for (int j = split+1; j <= max; ++j) {
894 >            for (int j = split + 1; j <= max; ++j) {
895                  assertEquals(it.next(), new Integer(j));
896                  it.remove();
897              }
# Line 605 | Line 900 | public class ArrayDequeTest extends JSR1
900          }
901      }
902  
608
903      /**
904 <     * toString contains toStrings of elements
904 >     * toString() contains toStrings of elements
905       */
906      public void testToString() {
907          ArrayDeque q = populatedDeque(SIZE);
908          String s = q.toString();
909          for (int i = 0; i < SIZE; ++i) {
910 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
910 >            assertTrue(s.contains(String.valueOf(i)));
911          }
912      }
913  
914      /**
915 <     * peekFirst returns element inserted with addFirst
915 >     * A deserialized/reserialized deque has same elements in same order
916       */
917 <    public void testAddFirst() {
918 <        ArrayDeque q = populatedDeque(3);
919 <        q.addFirst(four);
920 <        assertEquals(four,q.peekFirst());
917 >    public void testSerialization() throws Exception {
918 >        Queue x = populatedDeque(SIZE);
919 >        Queue y = serialClone(x);
920 >
921 >        assertNotSame(y, x);
922 >        assertEquals(x.size(), y.size());
923 >        assertEquals(x.toString(), y.toString());
924 >        assertEquals(Arrays.toString(x.toArray()), Arrays.toString(y.toArray()));
925 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
926 >        while (!x.isEmpty()) {
927 >            assertFalse(y.isEmpty());
928 >            assertEquals(x.remove(), y.remove());
929 >        }
930 >        assertTrue(y.isEmpty());
931      }
932  
933      /**
934 <     * peekLast returns element inserted with addLast
934 >     * A cloned deque has same elements in same order
935       */
936 <    public void testAddLast() {
937 <        ArrayDeque q = populatedDeque(3);
938 <        q.addLast(four);
939 <        assertEquals(four,q.peekLast());
936 >    public void testClone() throws Exception {
937 >        ArrayDeque<Integer> x = populatedDeque(SIZE);
938 >        ArrayDeque<Integer> y = x.clone();
939 >
940 >        assertNotSame(y, x);
941 >        assertEquals(x.size(), y.size());
942 >        assertEquals(x.toString(), y.toString());
943 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
944 >        while (!x.isEmpty()) {
945 >            assertFalse(y.isEmpty());
946 >            assertEquals(x.remove(), y.remove());
947 >        }
948 >        assertTrue(y.isEmpty());
949 >    }
950 >
951 >    /**
952 >     * remove(null), contains(null) always return false
953 >     */
954 >    public void testNeverContainsNull() {
955 >        Deque<?>[] qs = {
956 >            new ArrayDeque<Object>(),
957 >            populatedDeque(2),
958 >        };
959 >
960 >        for (Deque<?> q : qs) {
961 >            assertFalse(q.contains(null));
962 >            assertFalse(q.remove(null));
963 >            assertFalse(q.removeFirstOccurrence(null));
964 >            assertFalse(q.removeLastOccurrence(null));
965 >        }
966      }
967  
968   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines