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.8 by jsr166, Sat Nov 21 02:07:26 2009 UTC vs.
Revision 1.33 by jsr166, Sun Feb 22 04:34:44 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 >        junit.textui.TestRunner.run(suite());
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 <        }
49 <        catch (NullPointerException success) {}
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 testConstructor6() {
73 >    public void testConstructor5() {
74          try {
75              Integer[] ints = new Integer[SIZE];
76 <            for (int i = 0; i < SIZE; ++i)
76 >            for (int i = 0; i < SIZE-1; ++i)
77                  ints[i] = new Integer(i);
78 <            ArrayDeque q = new ArrayDeque(Arrays.asList(ints));
79 <            for (int i = 0; i < SIZE; ++i)
80 <                assertEquals(ints[i], q.pollFirst());
81 <        }
82 <        finally {}
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];
88 >        for (int i = 0; i < SIZE; ++i)
89 >            ints[i] = new Integer(i);
90 >        ArrayDeque q = new ArrayDeque(Arrays.asList(ints));
91 >        for (int i = 0; i < SIZE; ++i)
92 >            assertEquals(ints[i], q.pollFirst());
93      }
94  
95      /**
# Line 102 | Line 129 | public class ArrayDequeTest extends JSR1
129              ArrayDeque q = new ArrayDeque(1);
130              q.push(null);
131              shouldThrow();
132 <        } catch (NullPointerException success) { }
132 >        } catch (NullPointerException success) {}
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);
140          q.pollLast();
141          q.push(four);
142 <        assertEquals(four,q.peekFirst());
142 >        assertSame(four, q.peekFirst());
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);
150          for (int i = 0; i < SIZE; ++i) {
151 <            assertEquals(i, ((Integer)q.pop()).intValue());
151 >            assertEquals(i, q.pop());
152          }
153          try {
154              q.pop();
155              shouldThrow();
156 <        } catch (NoSuchElementException success) {
130 <        }
156 >        } catch (NoSuchElementException success) {}
157      }
158  
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();
176              q.offerFirst(null);
177              shouldThrow();
178 <        } catch (NullPointerException success) {
179 <        }
178 >        } catch (NullPointerException success) {}
179 >    }
180 >
181 >    /**
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 succeeds
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 >     * 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 succeeds
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 179 | Line 296 | public class ArrayDequeTest extends JSR1
296              ArrayDeque q = new ArrayDeque();
297              q.addAll(null);
298              shouldThrow();
299 <        }
183 <        catch (NullPointerException success) {}
299 >        } catch (NullPointerException success) {}
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 testAddAll5() {
305 >    public void testAddAll2() {
306          try {
307 <            Integer[] empty = new Integer[0];
307 >            ArrayDeque q = new ArrayDeque();
308              Integer[] ints = new Integer[SIZE];
309 <            for (int i = 0; i < SIZE; ++i)
310 <                ints[i] = new Integer(i);
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 <            assertFalse(q.addAll(Arrays.asList(empty)));
322 <            assertTrue(q.addAll(Arrays.asList(ints)));
323 <            for (int i = 0; i < SIZE; ++i)
324 <                assertEquals(ints[i], q.pollFirst());
325 <        }
326 <        finally {}
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 <     *  pollFirst succeeds unless empty
330 >     * Deque contains all elements, in traversal order, of successful addAll
331 >     */
332 >    public void testAddAll5() {
333 >        Integer[] empty = new Integer[0];
334 >        Integer[] ints = new Integer[SIZE];
335 >        for (int i = 0; i < SIZE; ++i)
336 >            ints[i] = new Integer(i);
337 >        ArrayDeque q = new ArrayDeque();
338 >        assertFalse(q.addAll(Arrays.asList(empty)));
339 >        assertTrue(q.addAll(Arrays.asList(ints)));
340 >        for (int i = 0; i < SIZE; ++i)
341 >            assertEquals(ints[i], q.pollFirst());
342 >    }
343 >
344 >    /**
345 >     * pollFirst() succeeds unless empty
346       */
347      public void testPollFirst() {
348          ArrayDeque q = populatedDeque(SIZE);
349          for (int i = 0; i < SIZE; ++i) {
350 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
350 >            assertEquals(i, q.pollFirst());
351          }
352          assertNull(q.pollFirst());
353      }
354  
355      /**
356 <     *  pollLast succeeds unless empty
356 >     * pollLast() succeeds unless empty
357       */
358      public void testPollLast() {
359          ArrayDeque q = populatedDeque(SIZE);
360          for (int i = SIZE-1; i >= 0; --i) {
361 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
361 >            assertEquals(i, q.pollLast());
362          }
363          assertNull(q.pollLast());
364      }
365  
366      /**
367 <     *  poll succeeds unless empty
367 >     * poll() succeeds unless empty
368       */
369      public void testPoll() {
370          ArrayDeque q = populatedDeque(SIZE);
371          for (int i = 0; i < SIZE; ++i) {
372 <            assertEquals(i, ((Integer)q.poll()).intValue());
372 >            assertEquals(i, q.poll());
373          }
374          assertNull(q.poll());
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);
382          for (int i = 0; i < SIZE; ++i) {
383 <            assertEquals(i, ((Integer)q.remove()).intValue());
383 >            assertEquals(i, q.remove());
384          }
385          try {
386              q.remove();
387              shouldThrow();
388 <        } catch (NoSuchElementException success) {
388 >        } catch (NoSuchElementException success) {}
389 >    }
390 >
391 >    /**
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
413 >     * peekFirst() returns next element, or null if empty
414       */
415      public void testPeekFirst() {
416          ArrayDeque q = populatedDeque(SIZE);
417          for (int i = 0; i < SIZE; ++i) {
418 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
419 <            q.pollFirst();
418 >            assertEquals(i, q.peekFirst());
419 >            assertEquals(i, q.pollFirst());
420              assertTrue(q.peekFirst() == null ||
421 <                       i != ((Integer)q.peekFirst()).intValue());
421 >                       !q.peekFirst().equals(i));
422          }
423          assertNull(q.peekFirst());
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);
431          for (int i = 0; i < SIZE; ++i) {
432 <            assertEquals(i, ((Integer)q.peek()).intValue());
433 <            q.poll();
432 >            assertEquals(i, q.peek());
433 >            assertEquals(i, q.poll());
434              assertTrue(q.peek() == null ||
435 <                       i != ((Integer)q.peek()).intValue());
435 >                       !q.peek().equals(i));
436          }
437          assertNull(q.peek());
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);
445          for (int i = SIZE-1; i >= 0; --i) {
446 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
447 <            q.pollLast();
446 >            assertEquals(i, q.peekLast());
447 >            assertEquals(i, q.pollLast());
448              assertTrue(q.peekLast() == null ||
449 <                       i != ((Integer)q.peekLast()).intValue());
449 >                       !q.peekLast().equals(i));
450          }
451          assertNull(q.peekLast());
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);
474          for (int i = 0; i < SIZE; ++i) {
475 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
476 <            q.pollFirst();
475 >            assertEquals(i, q.getFirst());
476 >            assertEquals(i, q.pollFirst());
477          }
478          try {
479              q.getFirst();
480              shouldThrow();
481 <        }
307 <        catch (NoSuchElementException success) {}
481 >        } catch (NoSuchElementException success) {}
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);
489          for (int i = SIZE-1; i >= 0; --i) {
490 <            assertEquals(i, ((Integer)q.getLast()).intValue());
491 <            q.pollLast();
490 >            assertEquals(i, q.getLast());
491 >            assertEquals(i, q.pollLast());
492          }
493          try {
494              q.getLast();
495              shouldThrow();
496 <        }
323 <        catch (NoSuchElementException success) {}
496 >        } catch (NoSuchElementException success) {}
497          assertNull(q.peekLast());
498      }
499  
327
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);
505          for (int i = 0; i < SIZE; ++i) {
506 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
506 >            assertEquals(i, q.removeFirst());
507          }
508          try {
509              q.removeFirst();
510              shouldThrow();
511 <        } catch (NoSuchElementException success) {
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 345 | 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 360 | 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 377 | Line 564 | public class ArrayDequeTest extends JSR1
564          ArrayDeque q = populatedDeque(SIZE);
565          for (int i = 0; i < SIZE; ++i) {
566              assertTrue(q.contains(new Integer(i)));
567 <            q.pollFirst();
567 >            assertEquals(i, q.pollFirst());
568              assertFalse(q.contains(new Integer(i)));
569          }
570      }
# Line 390 | Line 577 | public class ArrayDequeTest extends JSR1
577          q.clear();
578          assertTrue(q.isEmpty());
579          assertEquals(0, q.size());
580 <        q.add(new Integer(1));
580 >        assertTrue(q.add(new Integer(1)));
581          assertFalse(q.isEmpty());
582          q.clear();
583          assertTrue(q.isEmpty());
# Line 405 | Line 592 | public class ArrayDequeTest extends JSR1
592          for (int i = 0; i < SIZE; ++i) {
593              assertTrue(q.containsAll(p));
594              assertFalse(p.containsAll(q));
595 <            p.add(new Integer(i));
595 >            assertTrue(p.add(new Integer(i)));
596          }
597          assertTrue(p.containsAll(q));
598      }
# Line 418 | Line 605 | public class ArrayDequeTest extends JSR1
605          ArrayDeque p = populatedDeque(SIZE);
606          for (int i = 0; i < SIZE; ++i) {
607              boolean changed = q.retainAll(p);
608 <            if (i == 0)
422 <                assertFalse(changed);
423 <            else
424 <                assertTrue(changed);
425 <
608 >            assertEquals(changed, (i > 0));
609              assertTrue(q.containsAll(p));
610              assertEquals(SIZE-i, q.size());
611              p.removeFirst();
# Line 439 | Line 622 | public class ArrayDequeTest extends JSR1
622              assertTrue(q.removeAll(p));
623              assertEquals(SIZE-i, q.size());
624              for (int j = 0; j < i; ++j) {
625 <                Integer I = (Integer)(p.removeFirst());
443 <                assertFalse(q.contains(I));
625 >                assertFalse(q.contains(p.removeFirst()));
626              }
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 <            ArrayDeque l = new ArrayDeque();
477 <            l.add(new Object());
478 <            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 <            ArrayDeque l = new ArrayDeque();
489 <            l.add(new Integer(5));
490 <            Object o[] = l.toArray(new String[10] );
735 >            l.toArray(new String[10]);
736              shouldThrow();
737 <        } catch (ArrayStoreException  success) {}
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);
500        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()));
504            ++i;
505        }
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 <            int i = ((Integer)(it.next())).intValue();
520 <            assertEquals(++k, i);
772 >            assertEquals(++k, it.next());
773          }
774  
775          assertEquals(3, k);
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 552 | 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 566 | Line 818 | public class ArrayDequeTest extends JSR1
818          assertFalse(it.hasNext());
819          try {
820              it.next();
821 <        } catch (NoSuchElementException success) {
822 <        }
821 >            shouldThrow();
822 >        } catch (NoSuchElementException success) {}
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 581 | Line 833 | public class ArrayDequeTest extends JSR1
833              q.add(new Integer(1));
834              int k = 0;
835              for (Iterator it = q.descendingIterator(); it.hasNext();) {
836 <                int i = ((Integer)(it.next())).intValue();
585 <                assertEquals(++k, i);
836 >                assertEquals(++k, it.next());
837              }
838  
839              assertEquals(3, k);
# Line 593 | 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 620 | Line 871 | public class ArrayDequeTest extends JSR1
871          }
872      }
873  
623
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