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.50 by jsr166, Sat Oct 22 05:45:56 2016 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
2 > * Written by Doug Lea and Martin Buchholz with assistance from
3 > * members of JCP JSR-166 Expert Group and released to the public
4 > * domain, as explained at
5 > * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
9 < import java.util.*;
10 < import java.util.concurrent.*;
8 > import java.util.ArrayDeque;
9 > import java.util.Arrays;
10 > import java.util.Collection;
11 > import java.util.Collections;
12 > import java.util.Deque;
13 > import java.util.Iterator;
14 > import java.util.NoSuchElementException;
15 > import java.util.Queue;
16 > import java.util.Random;
17 > import java.util.Spliterator;
18 > import java.util.concurrent.ThreadLocalRandom;
19 >
20 > import junit.framework.Test;
21 > import junit.framework.TestSuite;
22  
23   public class ArrayDequeTest extends JSR166TestCase {
24      public static void main(String[] args) {
25 <        junit.textui.TestRunner.run (suite());
25 >        main(suite(), args);
26      }
27  
28      public static Test suite() {
29 <        return new TestSuite(ArrayDequeTest.class);
29 >        class Implementation implements CollectionImplementation {
30 >            public Class<?> klazz() { return ArrayDeque.class; }
31 >            public Collection emptyCollection() { return populatedDeque(0); }
32 >            public Object makeElement(int i) { return i; }
33 >            public boolean isConcurrent() { return false; }
34 >            public boolean permitsNulls() { return false; }
35 >        }
36 >        return newTestSuite(ArrayDequeTest.class,
37 >                            CollectionTest.testSuite(new Implementation()));
38      }
39  
40      /**
41 <     * Create a queue of given size containing consecutive
42 <     * Integers 0 ... n.
43 <     */
44 <    private ArrayDeque populatedDeque(int n) {
45 <        ArrayDeque q = new ArrayDeque();
41 >     * Returns a new deque of given size containing consecutive
42 >     * Integers 0 ... n - 1.
43 >     */
44 >    private static ArrayDeque<Integer> populatedDeque(int n) {
45 >        // Randomize various aspects of memory layout, including
46 >        // filled-to-capacity and wraparound.
47 >        final ArrayDeque<Integer> q;
48 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
49 >        switch (rnd.nextInt(6)) {
50 >        case 0: q = new ArrayDeque<Integer>();      break;
51 >        case 1: q = new ArrayDeque<Integer>(0);     break;
52 >        case 2: q = new ArrayDeque<Integer>(1);     break;
53 >        case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break;
54 >        case 4: q = new ArrayDeque<Integer>(n);     break;
55 >        case 5: q = new ArrayDeque<Integer>(n + 1); break;
56 >        default: throw new AssertionError();
57 >        }
58 >        switch (rnd.nextInt(3)) {
59 >        case 0:
60 >            q.addFirst(42);
61 >            assertEquals((Integer) 42, q.removeLast());
62 >            break;
63 >        case 1:
64 >            q.addLast(42);
65 >            assertEquals((Integer) 42, q.removeFirst());
66 >            break;
67 >        case 2: /* do nothing */ break;
68 >        default: throw new AssertionError();
69 >        }
70          assertTrue(q.isEmpty());
71 <        for (int i = 0; i < n; ++i)
72 <            assertTrue(q.offerLast(new Integer(i)));
73 <        assertFalse(q.isEmpty());
71 >        if (rnd.nextBoolean())
72 >            for (int i = 0; i < n; i++)
73 >                assertTrue(q.offerLast((Integer) i));
74 >        else
75 >            for (int i = n; --i >= 0; )
76 >                q.addFirst((Integer) i);
77          assertEquals(n, q.size());
78 +        if (n > 0) {
79 +            assertFalse(q.isEmpty());
80 +            assertEquals((Integer) 0, q.peekFirst());
81 +            assertEquals((Integer) (n - 1), q.peekLast());
82 +        }
83          return q;
84      }
85  
86      /**
87 <     * new queue is empty
87 >     * new deque is empty
88       */
89      public void testConstructor1() {
90          assertEquals(0, new ArrayDeque().size());
# Line 43 | Line 95 | public class ArrayDequeTest extends JSR1
95       */
96      public void testConstructor3() {
97          try {
98 <            ArrayDeque q = new ArrayDeque((Collection)null);
98 >            new ArrayDeque((Collection)null);
99              shouldThrow();
100          } catch (NullPointerException success) {}
101      }
102  
103      /**
104 <     * Queue contains all elements of collection used to initialize
104 >     * Initializing from Collection of null elements throws NPE
105 >     */
106 >    public void testConstructor4() {
107 >        try {
108 >            new ArrayDeque(Arrays.asList(new Integer[SIZE]));
109 >            shouldThrow();
110 >        } catch (NullPointerException success) {}
111 >    }
112  
113 +    /**
114 +     * Initializing from Collection with some null elements throws NPE
115 +     */
116 +    public void testConstructor5() {
117 +        Integer[] ints = new Integer[SIZE];
118 +        for (int i = 0; i < SIZE - 1; ++i)
119 +            ints[i] = new Integer(i);
120 +        try {
121 +            new ArrayDeque(Arrays.asList(ints));
122 +            shouldThrow();
123 +        } catch (NullPointerException success) {}
124 +    }
125 +
126 +    /**
127 +     * Deque contains all elements of collection used to initialize
128       */
129      public void testConstructor6() {
130          Integer[] ints = new Integer[SIZE];
# Line 81 | Line 155 | public class ArrayDequeTest extends JSR1
155      public void testSize() {
156          ArrayDeque q = populatedDeque(SIZE);
157          for (int i = 0; i < SIZE; ++i) {
158 <            assertEquals(SIZE-i, q.size());
158 >            assertEquals(SIZE - i, q.size());
159              q.removeFirst();
160          }
161          for (int i = 0; i < SIZE; ++i) {
# Line 94 | Line 168 | public class ArrayDequeTest extends JSR1
168       * push(null) throws NPE
169       */
170      public void testPushNull() {
171 +        ArrayDeque q = new ArrayDeque(1);
172          try {
98            ArrayDeque q = new ArrayDeque(1);
173              q.push(null);
174              shouldThrow();
175          } catch (NullPointerException success) {}
176      }
177  
178      /**
179 <     * peekFirst returns element inserted with push
179 >     * peekFirst() returns element inserted with push
180       */
181      public void testPush() {
182          ArrayDeque q = populatedDeque(3);
# Line 112 | Line 186 | public class ArrayDequeTest extends JSR1
186      }
187  
188      /**
189 <     *  pop removes next element, or throws NSEE if empty
189 >     * pop() removes next element, or throws NSEE if empty
190       */
191      public void testPop() {
192          ArrayDeque q = populatedDeque(SIZE);
# Line 128 | Line 202 | public class ArrayDequeTest extends JSR1
202      /**
203       * offer(null) throws NPE
204       */
205 +    public void testOfferNull() {
206 +        ArrayDeque q = new ArrayDeque();
207 +        try {
208 +            q.offer(null);
209 +            shouldThrow();
210 +        } catch (NullPointerException success) {}
211 +    }
212 +
213 +    /**
214 +     * offerFirst(null) throws NPE
215 +     */
216      public void testOfferFirstNull() {
217 +        ArrayDeque q = new ArrayDeque();
218          try {
133            ArrayDeque q = new ArrayDeque();
219              q.offerFirst(null);
220              shouldThrow();
221          } catch (NullPointerException success) {}
222      }
223  
224      /**
225 <     * OfferFirst succeeds
225 >     * offerLast(null) throws NPE
226 >     */
227 >    public void testOfferLastNull() {
228 >        ArrayDeque q = new ArrayDeque();
229 >        try {
230 >            q.offerLast(null);
231 >            shouldThrow();
232 >        } catch (NullPointerException success) {}
233 >    }
234 >
235 >    /**
236 >     * offer(x) succeeds
237 >     */
238 >    public void testOffer() {
239 >        ArrayDeque q = new ArrayDeque();
240 >        assertTrue(q.offer(zero));
241 >        assertTrue(q.offer(one));
242 >        assertSame(zero, q.peekFirst());
243 >        assertSame(one, q.peekLast());
244 >    }
245 >
246 >    /**
247 >     * offerFirst(x) succeeds
248       */
249      public void testOfferFirst() {
250          ArrayDeque q = new ArrayDeque();
251 <        assertTrue(q.offerFirst(new Integer(0)));
252 <        assertTrue(q.offerFirst(new Integer(1)));
251 >        assertTrue(q.offerFirst(zero));
252 >        assertTrue(q.offerFirst(one));
253 >        assertSame(one, q.peekFirst());
254 >        assertSame(zero, q.peekLast());
255      }
256  
257      /**
258 <     * OfferLast succeeds
258 >     * offerLast(x) succeeds
259       */
260      public void testOfferLast() {
261          ArrayDeque q = new ArrayDeque();
262 <        assertTrue(q.offerLast(new Integer(0)));
263 <        assertTrue(q.offerLast(new Integer(1)));
262 >        assertTrue(q.offerLast(zero));
263 >        assertTrue(q.offerLast(one));
264 >        assertSame(zero, q.peekFirst());
265 >        assertSame(one, q.peekLast());
266 >    }
267 >
268 >    /**
269 >     * add(null) throws NPE
270 >     */
271 >    public void testAddNull() {
272 >        ArrayDeque q = new ArrayDeque();
273 >        try {
274 >            q.add(null);
275 >            shouldThrow();
276 >        } catch (NullPointerException success) {}
277 >    }
278 >
279 >    /**
280 >     * addFirst(null) throws NPE
281 >     */
282 >    public void testAddFirstNull() {
283 >        ArrayDeque q = new ArrayDeque();
284 >        try {
285 >            q.addFirst(null);
286 >            shouldThrow();
287 >        } catch (NullPointerException success) {}
288      }
289  
290      /**
291 <     * add succeeds
291 >     * addLast(null) throws NPE
292 >     */
293 >    public void testAddLastNull() {
294 >        ArrayDeque q = new ArrayDeque();
295 >        try {
296 >            q.addLast(null);
297 >            shouldThrow();
298 >        } catch (NullPointerException success) {}
299 >    }
300 >
301 >    /**
302 >     * add(x) succeeds
303       */
304      public void testAdd() {
305          ArrayDeque q = new ArrayDeque();
306 <        for (int i = 0; i < SIZE; ++i) {
307 <            assertEquals(i, q.size());
308 <            assertTrue(q.add(new Integer(i)));
309 <        }
306 >        assertTrue(q.add(zero));
307 >        assertTrue(q.add(one));
308 >        assertSame(zero, q.peekFirst());
309 >        assertSame(one, q.peekLast());
310 >    }
311 >
312 >    /**
313 >     * addFirst(x) succeeds
314 >     */
315 >    public void testAddFirst() {
316 >        ArrayDeque q = new ArrayDeque();
317 >        q.addFirst(zero);
318 >        q.addFirst(one);
319 >        assertSame(one, q.peekFirst());
320 >        assertSame(zero, q.peekLast());
321 >    }
322 >
323 >    /**
324 >     * addLast(x) succeeds
325 >     */
326 >    public void testAddLast() {
327 >        ArrayDeque q = new ArrayDeque();
328 >        q.addLast(zero);
329 >        q.addLast(one);
330 >        assertSame(zero, q.peekFirst());
331 >        assertSame(one, q.peekLast());
332      }
333  
334      /**
335       * addAll(null) throws NPE
336       */
337      public void testAddAll1() {
338 +        ArrayDeque q = new ArrayDeque();
339          try {
173            ArrayDeque q = new ArrayDeque();
340              q.addAll(null);
341              shouldThrow();
342          } catch (NullPointerException success) {}
343      }
344  
345      /**
346 <     * Queue contains all elements, in traversal order, of successful addAll
346 >     * addAll of a collection with null elements throws NPE
347 >     */
348 >    public void testAddAll2() {
349 >        ArrayDeque q = new ArrayDeque();
350 >        try {
351 >            q.addAll(Arrays.asList(new Integer[SIZE]));
352 >            shouldThrow();
353 >        } catch (NullPointerException success) {}
354 >    }
355 >
356 >    /**
357 >     * addAll of a collection with any null elements throws NPE after
358 >     * possibly adding some elements
359 >     */
360 >    public void testAddAll3() {
361 >        ArrayDeque q = new ArrayDeque();
362 >        Integer[] ints = new Integer[SIZE];
363 >        for (int i = 0; i < SIZE - 1; ++i)
364 >            ints[i] = new Integer(i);
365 >        try {
366 >            q.addAll(Arrays.asList(ints));
367 >            shouldThrow();
368 >        } catch (NullPointerException success) {}
369 >    }
370 >
371 >    /**
372 >     * Deque contains all elements, in traversal order, of successful addAll
373       */
374      public void testAddAll5() {
375          Integer[] empty = new Integer[0];
# Line 192 | Line 384 | public class ArrayDequeTest extends JSR1
384      }
385  
386      /**
387 <     *  pollFirst succeeds unless empty
387 >     * pollFirst() succeeds unless empty
388       */
389      public void testPollFirst() {
390          ArrayDeque q = populatedDeque(SIZE);
# Line 203 | Line 395 | public class ArrayDequeTest extends JSR1
395      }
396  
397      /**
398 <     *  pollLast succeeds unless empty
398 >     * pollLast() succeeds unless empty
399       */
400      public void testPollLast() {
401          ArrayDeque q = populatedDeque(SIZE);
402 <        for (int i = SIZE-1; i >= 0; --i) {
402 >        for (int i = SIZE - 1; i >= 0; --i) {
403              assertEquals(i, q.pollLast());
404          }
405          assertNull(q.pollLast());
406      }
407  
408      /**
409 <     *  poll succeeds unless empty
409 >     * poll() succeeds unless empty
410       */
411      public void testPoll() {
412          ArrayDeque q = populatedDeque(SIZE);
# Line 225 | Line 417 | public class ArrayDequeTest extends JSR1
417      }
418  
419      /**
420 <     *  remove removes next element, or throws NSEE if empty
420 >     * remove() removes next element, or throws NSEE if empty
421       */
422      public void testRemove() {
423          ArrayDeque q = populatedDeque(SIZE);
# Line 239 | Line 431 | public class ArrayDequeTest extends JSR1
431      }
432  
433      /**
434 <     *  peekFirst returns next element, or null if empty
434 >     * remove(x) removes x and returns true if present
435 >     */
436 >    public void testRemoveElement() {
437 >        ArrayDeque q = populatedDeque(SIZE);
438 >        for (int i = 1; i < SIZE; i += 2) {
439 >            assertTrue(q.contains(i));
440 >            assertTrue(q.remove(i));
441 >            assertFalse(q.contains(i));
442 >            assertTrue(q.contains(i - 1));
443 >        }
444 >        for (int i = 0; i < SIZE; i += 2) {
445 >            assertTrue(q.contains(i));
446 >            assertTrue(q.remove(i));
447 >            assertFalse(q.contains(i));
448 >            assertFalse(q.remove(i + 1));
449 >            assertFalse(q.contains(i + 1));
450 >        }
451 >        assertTrue(q.isEmpty());
452 >    }
453 >
454 >    /**
455 >     * peekFirst() returns next element, or null if empty
456       */
457      public void testPeekFirst() {
458          ArrayDeque q = populatedDeque(SIZE);
# Line 253 | Line 466 | public class ArrayDequeTest extends JSR1
466      }
467  
468      /**
469 <     *  peek returns next element, or null if empty
469 >     * peek() returns next element, or null if empty
470       */
471      public void testPeek() {
472          ArrayDeque q = populatedDeque(SIZE);
# Line 267 | Line 480 | public class ArrayDequeTest extends JSR1
480      }
481  
482      /**
483 <     *  peekLast returns next element, or null if empty
483 >     * peekLast() returns next element, or null if empty
484       */
485      public void testPeekLast() {
486          ArrayDeque q = populatedDeque(SIZE);
487 <        for (int i = SIZE-1; i >= 0; --i) {
487 >        for (int i = SIZE - 1; i >= 0; --i) {
488              assertEquals(i, q.peekLast());
489              assertEquals(i, q.pollLast());
490              assertTrue(q.peekLast() == null ||
# Line 281 | Line 494 | public class ArrayDequeTest extends JSR1
494      }
495  
496      /**
497 <     * getFirst returns next getFirst, or throws NSEE if empty
497 >     * element() returns first element, or throws NSEE if empty
498 >     */
499 >    public void testElement() {
500 >        ArrayDeque q = populatedDeque(SIZE);
501 >        for (int i = 0; i < SIZE; ++i) {
502 >            assertEquals(i, q.element());
503 >            assertEquals(i, q.poll());
504 >        }
505 >        try {
506 >            q.element();
507 >            shouldThrow();
508 >        } catch (NoSuchElementException success) {}
509 >    }
510 >
511 >    /**
512 >     * getFirst() returns first element, or throws NSEE if empty
513       */
514      public void testFirstElement() {
515          ArrayDeque q = populatedDeque(SIZE);
# Line 296 | Line 524 | public class ArrayDequeTest extends JSR1
524      }
525  
526      /**
527 <     *  getLast returns next element, or throws NSEE if empty
527 >     * getLast() returns last element, or throws NSEE if empty
528       */
529      public void testLastElement() {
530          ArrayDeque q = populatedDeque(SIZE);
531 <        for (int i = SIZE-1; i >= 0; --i) {
531 >        for (int i = SIZE - 1; i >= 0; --i) {
532              assertEquals(i, q.getLast());
533              assertEquals(i, q.pollLast());
534          }
# Line 311 | Line 539 | public class ArrayDequeTest extends JSR1
539          assertNull(q.peekLast());
540      }
541  
314
542      /**
543 <     *  removeFirst removes next element, or throws NSEE if empty
543 >     * removeFirst() removes first element, or throws NSEE if empty
544       */
545      public void testRemoveFirst() {
546          ArrayDeque q = populatedDeque(SIZE);
# Line 324 | Line 551 | public class ArrayDequeTest extends JSR1
551              q.removeFirst();
552              shouldThrow();
553          } catch (NoSuchElementException success) {}
554 +        assertNull(q.peekFirst());
555 +    }
556 +
557 +    /**
558 +     * removeLast() removes last element, or throws NSEE if empty
559 +     */
560 +    public void testRemoveLast() {
561 +        ArrayDeque q = populatedDeque(SIZE);
562 +        for (int i = SIZE - 1; i >= 0; --i) {
563 +            assertEquals(i, q.removeLast());
564 +        }
565 +        try {
566 +            q.removeLast();
567 +            shouldThrow();
568 +        } catch (NoSuchElementException success) {}
569 +        assertNull(q.peekLast());
570      }
571  
572      /**
# Line 331 | Line 574 | public class ArrayDequeTest extends JSR1
574       */
575      public void testRemoveFirstOccurrence() {
576          ArrayDeque q = populatedDeque(SIZE);
577 <        for (int i = 1; i < SIZE; i+=2) {
577 >        assertFalse(q.removeFirstOccurrence(null));
578 >        for (int i = 1; i < SIZE; i += 2) {
579              assertTrue(q.removeFirstOccurrence(new Integer(i)));
580          }
581 <        for (int i = 0; i < SIZE; i+=2) {
581 >        for (int i = 0; i < SIZE; i += 2) {
582              assertTrue(q.removeFirstOccurrence(new Integer(i)));
583 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
583 >            assertFalse(q.removeFirstOccurrence(new Integer(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      /**
# Line 346 | Line 595 | public class ArrayDequeTest extends JSR1
595       */
596      public void testRemoveLastOccurrence() {
597          ArrayDeque q = populatedDeque(SIZE);
598 <        for (int i = 1; i < SIZE; i+=2) {
598 >        assertFalse(q.removeLastOccurrence(null));
599 >        for (int i = 1; i < SIZE; i += 2) {
600              assertTrue(q.removeLastOccurrence(new Integer(i)));
601          }
602 <        for (int i = 0; i < SIZE; i+=2) {
602 >        for (int i = 0; i < SIZE; i += 2) {
603              assertTrue(q.removeLastOccurrence(new Integer(i)));
604 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
604 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
605          }
606          assertTrue(q.isEmpty());
607 +        assertFalse(q.removeLastOccurrence(null));
608 +        assertFalse(q.removeLastOccurrence(42));
609 +        q = new ArrayDeque();
610 +        assertFalse(q.removeLastOccurrence(null));
611 +        assertFalse(q.removeLastOccurrence(42));
612      }
613  
614      /**
# Line 406 | Line 661 | public class ArrayDequeTest extends JSR1
661              boolean changed = q.retainAll(p);
662              assertEquals(changed, (i > 0));
663              assertTrue(q.containsAll(p));
664 <            assertEquals(SIZE-i, q.size());
664 >            assertEquals(SIZE - i, q.size());
665              p.removeFirst();
666          }
667      }
# Line 419 | Line 674 | public class ArrayDequeTest extends JSR1
674              ArrayDeque q = populatedDeque(SIZE);
675              ArrayDeque p = populatedDeque(i);
676              assertTrue(q.removeAll(p));
677 <            assertEquals(SIZE-i, q.size());
677 >            assertEquals(SIZE - i, q.size());
678              for (int j = 0; j < i; ++j) {
679                  assertFalse(q.contains(p.removeFirst()));
680              }
681          }
682      }
683  
684 +    void checkToArray(ArrayDeque q) {
685 +        int size = q.size();
686 +        Object[] o = q.toArray();
687 +        assertEquals(size, o.length);
688 +        Iterator it = q.iterator();
689 +        for (int i = 0; i < size; i++) {
690 +            Integer x = (Integer) it.next();
691 +            assertEquals((Integer)o[0] + i, (int) x);
692 +            assertSame(o[i], x);
693 +        }
694 +    }
695 +
696      /**
697 <     *  toArray contains all elements
697 >     * toArray() contains all elements in FIFO order
698       */
699      public void testToArray() {
700 <        ArrayDeque q = populatedDeque(SIZE);
701 <        Object[] o = q.toArray();
702 <        Arrays.sort(o);
703 <        for (int i = 0; i < o.length; i++)
704 <            assertEquals(o[i], q.pollFirst());
700 >        ArrayDeque q = new ArrayDeque();
701 >        for (int i = 0; i < SIZE; i++) {
702 >            checkToArray(q);
703 >            q.addLast(i);
704 >        }
705 >        // Provoke wraparound
706 >        for (int i = 0; i < SIZE; i++) {
707 >            checkToArray(q);
708 >            assertEquals(i, q.poll());
709 >            q.addLast(SIZE + i);
710 >        }
711 >        for (int i = 0; i < SIZE; i++) {
712 >            checkToArray(q);
713 >            assertEquals(SIZE + i, q.poll());
714 >        }
715 >    }
716 >
717 >    void checkToArray2(ArrayDeque q) {
718 >        int size = q.size();
719 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
720 >        Integer[] a2 = new Integer[size];
721 >        Integer[] a3 = new Integer[size + 2];
722 >        if (size > 0) Arrays.fill(a1, 42);
723 >        Arrays.fill(a2, 42);
724 >        Arrays.fill(a3, 42);
725 >        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
726 >        Integer[] b2 = (Integer[]) q.toArray(a2);
727 >        Integer[] b3 = (Integer[]) q.toArray(a3);
728 >        assertSame(a2, b2);
729 >        assertSame(a3, b3);
730 >        Iterator it = q.iterator();
731 >        for (int i = 0; i < size; i++) {
732 >            Integer x = (Integer) it.next();
733 >            assertSame(b1[i], x);
734 >            assertEquals(b1[0] + i, (int) x);
735 >            assertSame(b2[i], x);
736 >            assertSame(b3[i], x);
737 >        }
738 >        assertNull(a3[size]);
739 >        assertEquals(42, (int) a3[size + 1]);
740 >        if (size > 0) {
741 >            assertNotSame(a1, b1);
742 >            assertEquals(size, b1.length);
743 >            for (int i = 0; i < a1.length; i++) {
744 >                assertEquals(42, (int) a1[i]);
745 >            }
746 >        }
747      }
748  
749      /**
750 <     *  toArray(a) contains all elements
750 >     * toArray(a) contains all elements in FIFO order
751       */
752      public void testToArray2() {
753 <        ArrayDeque q = populatedDeque(SIZE);
754 <        Integer[] ints = new Integer[SIZE];
755 <        ints = (Integer[])q.toArray(ints);
756 <        Arrays.sort(ints);
757 <        for (int i = 0; i < ints.length; i++)
758 <            assertEquals(ints[i], q.pollFirst());
753 >        ArrayDeque q = new ArrayDeque();
754 >        for (int i = 0; i < SIZE; i++) {
755 >            checkToArray2(q);
756 >            q.addLast(i);
757 >        }
758 >        // Provoke wraparound
759 >        for (int i = 0; i < SIZE; i++) {
760 >            checkToArray2(q);
761 >            assertEquals(i, q.poll());
762 >            q.addLast(SIZE + i);
763 >        }
764 >        for (int i = 0; i < SIZE; i++) {
765 >            checkToArray2(q);
766 >            assertEquals(SIZE + i, q.poll());
767 >        }
768      }
769  
770      /**
771 <     * toArray(null) throws NPE
771 >     * toArray(null) throws NullPointerException
772       */
773 <    public void testToArray_BadArg() {
773 >    public void testToArray_NullArg() {
774          ArrayDeque l = new ArrayDeque();
775          l.add(new Object());
776          try {
777 <            Object o[] = l.toArray(null);
777 >            l.toArray(null);
778              shouldThrow();
779          } catch (NullPointerException success) {}
780      }
781  
782      /**
783 <     * toArray with incompatable aray type throws CCE
783 >     * toArray(incompatible array type) throws ArrayStoreException
784       */
785      public void testToArray1_BadArg() {
786          ArrayDeque l = new ArrayDeque();
787          l.add(new Integer(5));
788          try {
789 <            Object o[] = l.toArray(new String[10]);
789 >            l.toArray(new String[10]);
790              shouldThrow();
791          } catch (ArrayStoreException success) {}
792      }
793  
794      /**
795 <     *  iterator iterates through all elements
795 >     * Iterator iterates through all elements
796       */
797      public void testIterator() {
798          ArrayDeque q = populatedDeque(SIZE);
481        int i = 0;
799          Iterator it = q.iterator();
800 <        while (it.hasNext()) {
800 >        int i;
801 >        for (i = 0; it.hasNext(); i++)
802              assertTrue(q.contains(it.next()));
485            ++i;
486        }
803          assertEquals(i, SIZE);
804 +        assertIteratorExhausted(it);
805 +    }
806 +
807 +    /**
808 +     * iterator of empty collection has no elements
809 +     */
810 +    public void testEmptyIterator() {
811 +        Deque c = new ArrayDeque();
812 +        assertIteratorExhausted(c.iterator());
813 +        assertIteratorExhausted(c.descendingIterator());
814      }
815  
816      /**
817 <     *  iterator ordering is FIFO
817 >     * Iterator ordering is FIFO
818       */
819      public void testIteratorOrdering() {
820          final ArrayDeque q = new ArrayDeque();
821 <        q.add(new Integer(1));
822 <        q.add(new Integer(2));
823 <        q.add(new Integer(3));
821 >        q.add(one);
822 >        q.add(two);
823 >        q.add(three);
824          int k = 0;
825          for (Iterator it = q.iterator(); it.hasNext();) {
826              assertEquals(++k, it.next());
# Line 504 | Line 830 | public class ArrayDequeTest extends JSR1
830      }
831  
832      /**
833 <     * iterator.remove removes current element
833 >     * iterator.remove() removes current element
834       */
835 <    public void testIteratorRemove () {
835 >    public void testIteratorRemove() {
836          final ArrayDeque q = new ArrayDeque();
837          final Random rng = new Random();
838          for (int iters = 0; iters < 100; ++iters) {
839              int max = rng.nextInt(5) + 2;
840 <            int split = rng.nextInt(max-1) + 1;
840 >            int split = rng.nextInt(max - 1) + 1;
841              for (int j = 1; j <= max; ++j)
842                  q.add(new Integer(j));
843              Iterator it = q.iterator();
844              for (int j = 1; j <= split; ++j)
845                  assertEquals(it.next(), new Integer(j));
846              it.remove();
847 <            assertEquals(it.next(), new Integer(split+1));
847 >            assertEquals(it.next(), new Integer(split + 1));
848              for (int j = 1; j <= split; ++j)
849                  q.remove(new Integer(j));
850              it = q.iterator();
851 <            for (int j = split+1; j <= max; ++j) {
851 >            for (int j = split + 1; j <= max; ++j) {
852                  assertEquals(it.next(), new Integer(j));
853                  it.remove();
854              }
# Line 532 | Line 858 | public class ArrayDequeTest extends JSR1
858      }
859  
860      /**
861 <     *  Descending iterator iterates through all elements
861 >     * Descending iterator iterates through all elements
862       */
863      public void testDescendingIterator() {
864          ArrayDeque q = populatedDeque(SIZE);
# Line 551 | Line 877 | public class ArrayDequeTest extends JSR1
877      }
878  
879      /**
880 <     *  Descending iterator ordering is reverse FIFO
880 >     * Descending iterator ordering is reverse FIFO
881       */
882      public void testDescendingIteratorOrdering() {
883          final ArrayDeque q = new ArrayDeque();
# Line 572 | Line 898 | public class ArrayDequeTest extends JSR1
898      }
899  
900      /**
901 <     * descendingIterator.remove removes current element
901 >     * descendingIterator.remove() removes current element
902       */
903 <    public void testDescendingIteratorRemove () {
903 >    public void testDescendingIteratorRemove() {
904          final ArrayDeque q = new ArrayDeque();
905          final Random rng = new Random();
906          for (int iters = 0; iters < 100; ++iters) {
907              int max = rng.nextInt(5) + 2;
908 <            int split = rng.nextInt(max-1) + 1;
908 >            int split = rng.nextInt(max - 1) + 1;
909              for (int j = max; j >= 1; --j)
910                  q.add(new Integer(j));
911              Iterator it = q.descendingIterator();
912              for (int j = 1; j <= split; ++j)
913                  assertEquals(it.next(), new Integer(j));
914              it.remove();
915 <            assertEquals(it.next(), new Integer(split+1));
915 >            assertEquals(it.next(), new Integer(split + 1));
916              for (int j = 1; j <= split; ++j)
917                  q.remove(new Integer(j));
918              it = q.descendingIterator();
919 <            for (int j = split+1; j <= max; ++j) {
919 >            for (int j = split + 1; j <= max; ++j) {
920                  assertEquals(it.next(), new Integer(j));
921                  it.remove();
922              }
# Line 599 | Line 925 | public class ArrayDequeTest extends JSR1
925          }
926      }
927  
602
928      /**
929 <     * toString contains toStrings of elements
929 >     * toString() contains toStrings of elements
930       */
931      public void testToString() {
932          ArrayDeque q = populatedDeque(SIZE);
933          String s = q.toString();
934          for (int i = 0; i < SIZE; ++i) {
935 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
935 >            assertTrue(s.contains(String.valueOf(i)));
936          }
937      }
938  
939      /**
940 <     * peekFirst returns element inserted with addFirst
940 >     * A deserialized serialized deque has same elements in same order
941       */
942 <    public void testAddFirst() {
943 <        ArrayDeque q = populatedDeque(3);
944 <        q.addFirst(four);
945 <        assertEquals(four,q.peekFirst());
942 >    public void testSerialization() throws Exception {
943 >        Queue x = populatedDeque(SIZE);
944 >        Queue y = serialClone(x);
945 >
946 >        assertNotSame(y, x);
947 >        assertEquals(x.size(), y.size());
948 >        assertEquals(x.toString(), y.toString());
949 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
950 >        while (!x.isEmpty()) {
951 >            assertFalse(y.isEmpty());
952 >            assertEquals(x.remove(), y.remove());
953 >        }
954 >        assertTrue(y.isEmpty());
955      }
956  
957      /**
958 <     * peekLast returns element inserted with addLast
958 >     * A cloned deque has same elements in same order
959       */
960 <    public void testAddLast() {
961 <        ArrayDeque q = populatedDeque(3);
962 <        q.addLast(four);
963 <        assertEquals(four,q.peekLast());
960 >    public void testClone() throws Exception {
961 >        ArrayDeque<Integer> x = populatedDeque(SIZE);
962 >        ArrayDeque<Integer> y = x.clone();
963 >
964 >        assertNotSame(y, x);
965 >        assertEquals(x.size(), y.size());
966 >        assertEquals(x.toString(), y.toString());
967 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
968 >        while (!x.isEmpty()) {
969 >            assertFalse(y.isEmpty());
970 >            assertEquals(x.remove(), y.remove());
971 >        }
972 >        assertTrue(y.isEmpty());
973 >    }
974 >
975 >    /**
976 >     * remove(null), contains(null) always return false
977 >     */
978 >    public void testNeverContainsNull() {
979 >        Deque<?>[] qs = {
980 >            new ArrayDeque<Object>(),
981 >            populatedDeque(2),
982 >        };
983 >
984 >        for (Deque<?> q : qs) {
985 >            assertFalse(q.contains(null));
986 >            assertFalse(q.remove(null));
987 >            assertFalse(q.removeFirstOccurrence(null));
988 >            assertFalse(q.removeLastOccurrence(null));
989 >        }
990 >    }
991 >
992 >    /**
993 >     * Spliterator characteristics are as advertised
994 >     */
995 >    public void testSpliterator_characteristics() {
996 >        ArrayDeque q = new ArrayDeque();
997 >        Spliterator s = q.spliterator();
998 >        int characteristics = s.characteristics();
999 >        int required = Spliterator.NONNULL
1000 >            | Spliterator.ORDERED
1001 >            | Spliterator.SIZED
1002 >            | Spliterator.SUBSIZED;
1003 >        assertEquals(required, characteristics & required);
1004 >        assertTrue(s.hasCharacteristics(required));
1005 >        assertEquals(0, characteristics
1006 >                     & (Spliterator.CONCURRENT
1007 >                        | Spliterator.DISTINCT
1008 >                        | Spliterator.IMMUTABLE
1009 >                        | Spliterator.SORTED));
1010 >    }
1011 >
1012 >    /**
1013 >     * Spliterator.getComparator always throws IllegalStateException
1014 >     */
1015 >    public void testSpliterator_getComparator() {
1016 >        assertThrows(IllegalStateException.class,
1017 >                     () -> new ArrayDeque().spliterator().getComparator());
1018 >    }
1019 >
1020 >    /**
1021 >     * Handle capacities near Integer.MAX_VALUE.
1022 >     * ant -Dvmoptions='-Xms28g -Xmx28g' -Djsr166.testImplementationDetails=true -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDequeTest -Djsr166.methodFilter=testHuge tck
1023 >     */
1024 >    public void testHuge() {
1025 >        if (! (testImplementationDetails
1026 >               && expensiveTests
1027 >               && Runtime.getRuntime().maxMemory() > 24L * (1 << 30)))
1028 >            return;
1029 >
1030 >        ArrayDeque q;
1031 >        Integer e = 42;
1032 >        final int maxSize = Integer.MAX_VALUE - 8;
1033 >
1034 >        assertThrows(OutOfMemoryError.class,
1035 >                     () -> new ArrayDeque<>(Integer.MAX_VALUE));
1036 >
1037 >        {
1038 >            q = new ArrayDeque<>(maxSize);
1039 >            assertEquals(0, q.size());
1040 >            assertTrue(q.isEmpty());
1041 >            q = null;
1042 >        }
1043 >
1044 >        {
1045 >            q = new ArrayDeque();
1046 >            assertTrue(q.addAll(Collections.nCopies(maxSize - 2, e)));
1047 >            assertEquals(e, q.peekFirst());
1048 >            assertEquals(e, q.peekLast());
1049 >            assertEquals(maxSize - 2, q.size());
1050 >            q.addFirst((Integer) 0);
1051 >            q.addLast((Integer) 1);
1052 >            assertEquals((Integer) 0, q.peekFirst());
1053 >            assertEquals((Integer) 1, q.peekLast());
1054 >            assertEquals(maxSize, q.size());
1055 >
1056 >            ArrayDeque qq = q;
1057 >            ArrayDeque smallish = new ArrayDeque(
1058 >                Collections.nCopies(Integer.MAX_VALUE - maxSize + 1, e));
1059 >            assertThrows(
1060 >                IllegalStateException.class,
1061 >                () -> qq.addAll(qq),
1062 >                () -> qq.addAll(smallish),
1063 >                () -> smallish.addAll(qq));
1064 >        }
1065      }
1066  
1067   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines