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.4 by dl, Sat Sep 17 12:50:49 2005 UTC vs.
Revision 1.31 by jsr166, Wed Dec 31 20:09:08 2014 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() {
25 <        return new TestSuite(ArrayDequeTest.class);
25 >        return new TestSuite(ArrayDequeTest.class);
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)));
35 >        for (int i = 0; i < n; ++i)
36 >            assertTrue(q.offerLast(new Integer(i)));
37          assertFalse(q.isEmpty());
38 <        assertEquals(n, q.size());
38 >        assertEquals(n, q.size());
39          return q;
40      }
41 <
41 >
42      /**
43 <     * new queue is empty
43 >     * new deque is empty
44       */
45      public void testConstructor1() {
46          assertEquals(0, new ArrayDeque().size());
# Line 45 | Line 53 | public class ArrayDequeTest extends JSR1
53          try {
54              ArrayDeque q = 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 >            ArrayDeque q = 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 {}
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 98 | Line 125 | public class ArrayDequeTest extends JSR1
125       * push(null) throws NPE
126       */
127      public void testPushNull() {
128 <        try {
128 >        try {
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());
143 <    }  
141 >        q.push(four);
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 {
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 succeeds
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(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());
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());
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());
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){
389 <        }  
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());
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());
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());
451 >        assertNull(q.peekLast());
452 >    }
453 >
454 >    /**
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 next getFirst, or throws NSEE if empty
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 <        }
497 <        catch (NoSuchElementException success) {}
324 <        assertNull(q.peekLast());
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){
512 <        }  
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() {
720 <        try {
721 <            ArrayDeque l = new ArrayDeque();
722 <            l.add(new Object());
723 <            Object o[] = l.toArray(null);
724 <            shouldThrow();
725 <        } catch(NullPointerException success){}
719 >    public void testToArray_NullArg() {
720 >        ArrayDeque l = new ArrayDeque();
721 >        l.add(new Object());
722 >        try {
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 <        try {
733 <            ArrayDeque l = new ArrayDeque();
734 <            l.add(new Integer(5));
735 <            Object o[] = l.toArray(new String[10] );
736 <            shouldThrow();
737 <        } catch(ArrayStoreException  success){}
732 >        ArrayDeque l = new ArrayDeque();
733 >        l.add(new Integer(5));
734 >        try {
735 >            l.toArray(new String[10]);
736 >            shouldThrow();
737 >        } catch (ArrayStoreException success) {}
738      }
739 <    
739 >
740      /**
741 <     *  iterator iterates through all elements
741 >     * Iterator iterates through all elements
742       */
743      public void testIterator() {
744          ArrayDeque q = populatedDeque(SIZE);
745          int i = 0;
746 <        Iterator it = q.iterator();
747 <        while(it.hasNext()) {
746 >        Iterator it = q.iterator();
747 >        while (it.hasNext()) {
748              assertTrue(q.contains(it.next()));
749              ++i;
750          }
# Line 507 | Line 752 | public class ArrayDequeTest extends JSR1
752      }
753  
754      /**
755 <     *  iterator ordering is FIFO
755 >     * Iterator ordering is FIFO
756       */
757      public void testIteratorOrdering() {
758          final ArrayDeque q = new ArrayDeque();
759 <        q.add(new Integer(1));
760 <        q.add(new Integer(2));
761 <        q.add(new Integer(3));
759 >        q.add(one);
760 >        q.add(two);
761 >        q.add(three);
762          int k = 0;
763          for (Iterator it = q.iterator(); it.hasNext();) {
764 <            int i = ((Integer)(it.next())).intValue();
520 <            assertEquals(++k, i);
764 >            assertEquals(++k, it.next());
765          }
766  
767          assertEquals(3, k);
768      }
769  
770      /**
771 <     * iterator.remove removes current element
771 >     * iterator.remove() removes current element
772       */
773 <    public void testIteratorRemove () {
773 >    public void testIteratorRemove() {
774          final ArrayDeque q = new ArrayDeque();
775          final Random rng = new Random();
776          for (int iters = 0; iters < 100; ++iters) {
# Line 535 | Line 779 | public class ArrayDequeTest extends JSR1
779              for (int j = 1; j <= max; ++j)
780                  q.add(new Integer(j));
781              Iterator it = q.iterator();
782 <            for (int j = 1; j <= split; ++j)
782 >            for (int j = 1; j <= split; ++j)
783                  assertEquals(it.next(), new Integer(j));
784              it.remove();
785              assertEquals(it.next(), new Integer(split+1));
786 <            for (int j = 1; j <= split; ++j)
786 >            for (int j = 1; j <= split; ++j)
787                  q.remove(new Integer(j));
788              it = q.iterator();
789              for (int j = split+1; j <= max; ++j) {
# Line 552 | Line 796 | public class ArrayDequeTest extends JSR1
796      }
797  
798      /**
799 <     *  Descending iterator iterates through all elements
799 >     * Descending iterator iterates through all elements
800       */
801      public void testDescendingIterator() {
802          ArrayDeque q = populatedDeque(SIZE);
803          int i = 0;
804 <        Iterator it = q.descendingIterator();
805 <        while(it.hasNext()) {
804 >        Iterator it = q.descendingIterator();
805 >        while (it.hasNext()) {
806              assertTrue(q.contains(it.next()));
807              ++i;
808          }
# Line 566 | Line 810 | public class ArrayDequeTest extends JSR1
810          assertFalse(it.hasNext());
811          try {
812              it.next();
813 <        } catch(NoSuchElementException success) {
814 <        }
813 >            shouldThrow();
814 >        } catch (NoSuchElementException success) {}
815      }
816  
817      /**
818 <     *  Descending iterator ordering is reverse FIFO
818 >     * Descending iterator ordering is reverse FIFO
819       */
820      public void testDescendingIteratorOrdering() {
821          final ArrayDeque q = new ArrayDeque();
# Line 581 | Line 825 | public class ArrayDequeTest extends JSR1
825              q.add(new Integer(1));
826              int k = 0;
827              for (Iterator it = q.descendingIterator(); it.hasNext();) {
828 <                int i = ((Integer)(it.next())).intValue();
585 <                assertEquals(++k, i);
828 >                assertEquals(++k, it.next());
829              }
830 <            
830 >
831              assertEquals(3, k);
832              q.remove();
833              q.remove();
# Line 593 | Line 836 | public class ArrayDequeTest extends JSR1
836      }
837  
838      /**
839 <     * descendingIterator.remove removes current element
839 >     * descendingIterator.remove() removes current element
840       */
841 <    public void testDescendingIteratorRemove () {
841 >    public void testDescendingIteratorRemove() {
842          final ArrayDeque q = new ArrayDeque();
843          final Random rng = new Random();
844          for (int iters = 0; iters < 100; ++iters) {
# Line 604 | Line 847 | public class ArrayDequeTest extends JSR1
847              for (int j = max; j >= 1; --j)
848                  q.add(new Integer(j));
849              Iterator it = q.descendingIterator();
850 <            for (int j = 1; j <= split; ++j)
850 >            for (int j = 1; j <= split; ++j)
851                  assertEquals(it.next(), new Integer(j));
852              it.remove();
853              assertEquals(it.next(), new Integer(split+1));
854 <            for (int j = 1; j <= split; ++j)
854 >            for (int j = 1; j <= split; ++j)
855                  q.remove(new Integer(j));
856              it = q.descendingIterator();
857              for (int j = split+1; j <= max; ++j) {
# Line 620 | Line 863 | public class ArrayDequeTest extends JSR1
863          }
864      }
865  
623
866      /**
867 <     * toString contains toStrings of elements
867 >     * toString() contains toStrings of elements
868       */
869      public void testToString() {
870          ArrayDeque q = populatedDeque(SIZE);
871          String s = q.toString();
872          for (int i = 0; i < SIZE; ++i) {
873 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
873 >            assertTrue(s.contains(String.valueOf(i)));
874          }
875 <    }        
875 >    }
876  
877      /**
878 <     * peekFirst returns element inserted with addFirst
878 >     * A deserialized serialized deque has same elements in same order
879       */
880 <    public void testAddFirst() {
881 <        ArrayDeque q = populatedDeque(3);
882 <        q.addFirst(four);
883 <        assertEquals(four,q.peekFirst());
884 <    }  
880 >    public void testSerialization() throws Exception {
881 >        Queue x = populatedDeque(SIZE);
882 >        Queue y = serialClone(x);
883 >
884 >        assertNotSame(y, x);
885 >        assertEquals(x.size(), y.size());
886 >        assertEquals(x.toString(), y.toString());
887 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
888 >        while (!x.isEmpty()) {
889 >            assertFalse(y.isEmpty());
890 >            assertEquals(x.remove(), y.remove());
891 >        }
892 >        assertTrue(y.isEmpty());
893 >    }
894  
895      /**
896 <     * peekLast returns element inserted with addLast
896 >     * remove(null), contains(null) always return false
897       */
898 <    public void testAddLast() {
899 <        ArrayDeque q = populatedDeque(3);
900 <        q.addLast(four);
901 <        assertEquals(four,q.peekLast());
902 <    }  
898 >    public void testNeverContainsNull() {
899 >        Deque<?>[] qs = {
900 >            new ArrayDeque<Object>(),
901 >            populatedDeque(2),
902 >        };
903 >
904 >        for (Deque<?> q : qs) {
905 >            assertFalse(q.contains(null));
906 >            assertFalse(q.remove(null));
907 >            assertFalse(q.removeFirstOccurrence(null));
908 >            assertFalse(q.removeLastOccurrence(null));
909 >        }
910 >    }
911  
912   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines