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.6 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.40 by jsr166, Sun Oct 16 20:16:36 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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 < import junit.framework.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
7 > import java.util.ArrayDeque;
8 > import java.util.Arrays;
9 > import java.util.Collection;
10 > import java.util.Deque;
11 > import java.util.Iterator;
12 > import java.util.NoSuchElementException;
13 > import java.util.Queue;
14 > import java.util.Random;
15 >
16 > import junit.framework.Test;
17 > import junit.framework.TestSuite;
18  
19   public class ArrayDequeTest extends JSR166TestCase {
20      public static void main(String[] args) {
21 <        junit.textui.TestRunner.run (suite());
21 >        main(suite(), args);
22      }
23  
24      public static Test suite() {
25 <        return new TestSuite(ArrayDequeTest.class);
25 >        class Implementation implements CollectionImplementation {
26 >            public Class<?> klazz() { return ArrayDeque.class; }
27 >            public Collection emptyCollection() { return new ArrayDeque(); }
28 >            public Object makeElement(int i) { return i; }
29 >            public boolean isConcurrent() { return false; }
30 >            public boolean permitsNulls() { return false; }
31 >        }
32 >        return newTestSuite(ArrayDequeTest.class,
33 >                            CollectionTest.testSuite(new Implementation()));
34      }
35  
36      /**
37 <     * Create a queue of given size containing consecutive
37 >     * Returns a new deque of given size containing consecutive
38       * Integers 0 ... n.
39       */
40 <    private ArrayDeque populatedDeque(int n) {
41 <        ArrayDeque q = new ArrayDeque();
40 >    private ArrayDeque<Integer> populatedDeque(int n) {
41 >        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
42          assertTrue(q.isEmpty());
43 <        for (int i = 0; i < n; ++i)
44 <            assertTrue(q.offerLast(new Integer(i)));
43 >        for (int i = 0; i < n; ++i)
44 >            assertTrue(q.offerLast(new Integer(i)));
45          assertFalse(q.isEmpty());
46 <        assertEquals(n, q.size());
46 >        assertEquals(n, q.size());
47          return q;
48      }
49  
50      /**
51 <     * new queue is empty
51 >     * new deque is empty
52       */
53      public void testConstructor1() {
54          assertEquals(0, new ArrayDeque().size());
# Line 43 | Line 59 | public class ArrayDequeTest extends JSR1
59       */
60      public void testConstructor3() {
61          try {
62 <            ArrayDeque q = new ArrayDeque((Collection)null);
62 >            new ArrayDeque((Collection)null);
63              shouldThrow();
64 <        }
49 <        catch (NullPointerException success) {}
64 >        } catch (NullPointerException success) {}
65      }
66  
67      /**
68 <     * Queue contains all elements of collection used to initialize
68 >     * Initializing from Collection of null elements throws NPE
69 >     */
70 >    public void testConstructor4() {
71 >        try {
72 >            new ArrayDeque(Arrays.asList(new Integer[SIZE]));
73 >            shouldThrow();
74 >        } catch (NullPointerException success) {}
75 >    }
76  
77 +    /**
78 +     * Initializing from Collection with some null elements throws NPE
79       */
80 <    public void testConstructor6() {
80 >    public void testConstructor5() {
81 >        Integer[] ints = new Integer[SIZE];
82 >        for (int i = 0; i < SIZE - 1; ++i)
83 >            ints[i] = new Integer(i);
84          try {
85 <            Integer[] ints = new Integer[SIZE];
86 <            for (int i = 0; i < SIZE; ++i)
87 <                ints[i] = new Integer(i);
88 <            ArrayDeque q = new ArrayDeque(Arrays.asList(ints));
89 <            for (int i = 0; i < SIZE; ++i)
90 <                assertEquals(ints[i], q.pollFirst());
91 <        }
92 <        finally {}
85 >            new ArrayDeque(Arrays.asList(ints));
86 >            shouldThrow();
87 >        } catch (NullPointerException success) {}
88 >    }
89 >
90 >    /**
91 >     * Deque contains all elements of collection used to initialize
92 >     */
93 >    public void testConstructor6() {
94 >        Integer[] ints = new Integer[SIZE];
95 >        for (int i = 0; i < SIZE; ++i)
96 >            ints[i] = new Integer(i);
97 >        ArrayDeque q = new ArrayDeque(Arrays.asList(ints));
98 >        for (int i = 0; i < SIZE; ++i)
99 >            assertEquals(ints[i], q.pollFirst());
100      }
101  
102      /**
# Line 85 | Line 119 | public class ArrayDequeTest extends JSR1
119      public void testSize() {
120          ArrayDeque q = populatedDeque(SIZE);
121          for (int i = 0; i < SIZE; ++i) {
122 <            assertEquals(SIZE-i, q.size());
122 >            assertEquals(SIZE - i, q.size());
123              q.removeFirst();
124          }
125          for (int i = 0; i < SIZE; ++i) {
# Line 98 | Line 132 | public class ArrayDequeTest extends JSR1
132       * push(null) throws NPE
133       */
134      public void testPushNull() {
135 <        try {
136 <            ArrayDeque q = new ArrayDeque(1);
135 >        ArrayDeque q = new ArrayDeque(1);
136 >        try {
137              q.push(null);
138              shouldThrow();
139 <        } catch (NullPointerException success) { }
139 >        } catch (NullPointerException success) {}
140      }
141  
142      /**
143 <     * peekFirst returns element inserted with push
143 >     * peekFirst() returns element inserted with push
144       */
145      public void testPush() {
146          ArrayDeque q = populatedDeque(3);
147          q.pollLast();
148 <        q.push(four);
149 <        assertEquals(four,q.peekFirst());
148 >        q.push(four);
149 >        assertSame(four, q.peekFirst());
150      }
151  
152      /**
153 <     *  pop removes next element, or throws NSEE if empty
153 >     * pop() removes next element, or throws NSEE if empty
154       */
155      public void testPop() {
156          ArrayDeque q = populatedDeque(SIZE);
157          for (int i = 0; i < SIZE; ++i) {
158 <            assertEquals(i, ((Integer)q.pop()).intValue());
158 >            assertEquals(i, q.pop());
159          }
160          try {
161              q.pop();
162              shouldThrow();
163 <        } catch (NoSuchElementException success){
130 <        }
163 >        } catch (NoSuchElementException success) {}
164      }
165  
166      /**
167       * offer(null) throws NPE
168       */
169 +    public void testOfferNull() {
170 +        ArrayDeque q = new ArrayDeque();
171 +        try {
172 +            q.offer(null);
173 +            shouldThrow();
174 +        } catch (NullPointerException success) {}
175 +    }
176 +
177 +    /**
178 +     * offerFirst(null) throws NPE
179 +     */
180      public void testOfferFirstNull() {
181 <        try {
182 <            ArrayDeque q = new ArrayDeque();
181 >        ArrayDeque q = new ArrayDeque();
182 >        try {
183              q.offerFirst(null);
184              shouldThrow();
185 <        } catch (NullPointerException success) {
142 <        }
185 >        } catch (NullPointerException success) {}
186      }
187  
188      /**
189 <     * OfferFirst succeeds
189 >     * offerLast(null) throws NPE
190 >     */
191 >    public void testOfferLastNull() {
192 >        ArrayDeque q = new ArrayDeque();
193 >        try {
194 >            q.offerLast(null);
195 >            shouldThrow();
196 >        } catch (NullPointerException success) {}
197 >    }
198 >
199 >    /**
200 >     * offer(x) succeeds
201 >     */
202 >    public void testOffer() {
203 >        ArrayDeque q = new ArrayDeque();
204 >        assertTrue(q.offer(zero));
205 >        assertTrue(q.offer(one));
206 >        assertSame(zero, q.peekFirst());
207 >        assertSame(one, q.peekLast());
208 >    }
209 >
210 >    /**
211 >     * offerFirst(x) succeeds
212       */
213      public void testOfferFirst() {
214          ArrayDeque q = new ArrayDeque();
215 <        assertTrue(q.offerFirst(new Integer(0)));
216 <        assertTrue(q.offerFirst(new Integer(1)));
215 >        assertTrue(q.offerFirst(zero));
216 >        assertTrue(q.offerFirst(one));
217 >        assertSame(one, q.peekFirst());
218 >        assertSame(zero, q.peekLast());
219      }
220  
221      /**
222 <     * OfferLast succeeds
222 >     * offerLast(x) succeeds
223       */
224      public void testOfferLast() {
225          ArrayDeque q = new ArrayDeque();
226 <        assertTrue(q.offerLast(new Integer(0)));
227 <        assertTrue(q.offerLast(new Integer(1)));
226 >        assertTrue(q.offerLast(zero));
227 >        assertTrue(q.offerLast(one));
228 >        assertSame(zero, q.peekFirst());
229 >        assertSame(one, q.peekLast());
230 >    }
231 >
232 >    /**
233 >     * add(null) throws NPE
234 >     */
235 >    public void testAddNull() {
236 >        ArrayDeque q = new ArrayDeque();
237 >        try {
238 >            q.add(null);
239 >            shouldThrow();
240 >        } catch (NullPointerException success) {}
241      }
242  
243      /**
244 <     * add succeeds
244 >     * addFirst(null) throws NPE
245 >     */
246 >    public void testAddFirstNull() {
247 >        ArrayDeque q = new ArrayDeque();
248 >        try {
249 >            q.addFirst(null);
250 >            shouldThrow();
251 >        } catch (NullPointerException success) {}
252 >    }
253 >
254 >    /**
255 >     * addLast(null) throws NPE
256 >     */
257 >    public void testAddLastNull() {
258 >        ArrayDeque q = new ArrayDeque();
259 >        try {
260 >            q.addLast(null);
261 >            shouldThrow();
262 >        } catch (NullPointerException success) {}
263 >    }
264 >
265 >    /**
266 >     * add(x) succeeds
267       */
268      public void testAdd() {
269          ArrayDeque q = new ArrayDeque();
270 <        for (int i = 0; i < SIZE; ++i) {
271 <            assertEquals(i, q.size());
272 <            assertTrue(q.add(new Integer(i)));
273 <        }
270 >        assertTrue(q.add(zero));
271 >        assertTrue(q.add(one));
272 >        assertSame(zero, q.peekFirst());
273 >        assertSame(one, q.peekLast());
274 >    }
275 >
276 >    /**
277 >     * addFirst(x) succeeds
278 >     */
279 >    public void testAddFirst() {
280 >        ArrayDeque q = new ArrayDeque();
281 >        q.addFirst(zero);
282 >        q.addFirst(one);
283 >        assertSame(one, q.peekFirst());
284 >        assertSame(zero, q.peekLast());
285 >    }
286 >
287 >    /**
288 >     * addLast(x) succeeds
289 >     */
290 >    public void testAddLast() {
291 >        ArrayDeque q = new ArrayDeque();
292 >        q.addLast(zero);
293 >        q.addLast(one);
294 >        assertSame(zero, q.peekFirst());
295 >        assertSame(one, q.peekLast());
296      }
297  
298      /**
299       * addAll(null) throws NPE
300       */
301      public void testAddAll1() {
302 +        ArrayDeque q = new ArrayDeque();
303          try {
179            ArrayDeque q = new ArrayDeque();
304              q.addAll(null);
305              shouldThrow();
306 <        }
183 <        catch (NullPointerException success) {}
306 >        } catch (NullPointerException success) {}
307      }
308  
309      /**
310 <     * Queue contains all elements, in traversal order, of successful addAll
310 >     * addAll of a collection with null elements throws NPE
311       */
312 <    public void testAddAll5() {
312 >    public void testAddAll2() {
313 >        ArrayDeque q = new ArrayDeque();
314          try {
315 <            Integer[] empty = new Integer[0];
316 <            Integer[] ints = new Integer[SIZE];
317 <            for (int i = 0; i < SIZE; ++i)
318 <                ints[i] = new Integer(i);
319 <            ArrayDeque q = new ArrayDeque();
320 <            assertFalse(q.addAll(Arrays.asList(empty)));
321 <            assertTrue(q.addAll(Arrays.asList(ints)));
322 <            for (int i = 0; i < SIZE; ++i)
323 <                assertEquals(ints[i], q.pollFirst());
324 <        }
325 <        finally {}
315 >            q.addAll(Arrays.asList(new Integer[SIZE]));
316 >            shouldThrow();
317 >        } catch (NullPointerException success) {}
318 >    }
319 >
320 >    /**
321 >     * addAll of a collection with any null elements throws NPE after
322 >     * possibly adding some elements
323 >     */
324 >    public void testAddAll3() {
325 >        ArrayDeque q = new ArrayDeque();
326 >        Integer[] ints = new Integer[SIZE];
327 >        for (int i = 0; i < SIZE - 1; ++i)
328 >            ints[i] = new Integer(i);
329 >        try {
330 >            q.addAll(Arrays.asList(ints));
331 >            shouldThrow();
332 >        } catch (NullPointerException success) {}
333      }
334  
335      /**
336 <     *  pollFirst succeeds unless empty
336 >     * Deque contains all elements, in traversal order, of successful addAll
337 >     */
338 >    public void testAddAll5() {
339 >        Integer[] empty = new Integer[0];
340 >        Integer[] ints = new Integer[SIZE];
341 >        for (int i = 0; i < SIZE; ++i)
342 >            ints[i] = new Integer(i);
343 >        ArrayDeque q = new ArrayDeque();
344 >        assertFalse(q.addAll(Arrays.asList(empty)));
345 >        assertTrue(q.addAll(Arrays.asList(ints)));
346 >        for (int i = 0; i < SIZE; ++i)
347 >            assertEquals(ints[i], q.pollFirst());
348 >    }
349 >
350 >    /**
351 >     * pollFirst() succeeds unless empty
352       */
353      public void testPollFirst() {
354          ArrayDeque q = populatedDeque(SIZE);
355          for (int i = 0; i < SIZE; ++i) {
356 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
356 >            assertEquals(i, q.pollFirst());
357          }
358 <        assertNull(q.pollFirst());
358 >        assertNull(q.pollFirst());
359      }
360  
361      /**
362 <     *  pollLast succeeds unless empty
362 >     * pollLast() succeeds unless empty
363       */
364      public void testPollLast() {
365          ArrayDeque q = populatedDeque(SIZE);
366 <        for (int i = SIZE-1; i >= 0; --i) {
367 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
366 >        for (int i = SIZE - 1; i >= 0; --i) {
367 >            assertEquals(i, q.pollLast());
368          }
369 <        assertNull(q.pollLast());
369 >        assertNull(q.pollLast());
370      }
371  
372      /**
373 <     *  poll succeeds unless empty
373 >     * poll() succeeds unless empty
374       */
375      public void testPoll() {
376          ArrayDeque q = populatedDeque(SIZE);
377          for (int i = 0; i < SIZE; ++i) {
378 <            assertEquals(i, ((Integer)q.poll()).intValue());
378 >            assertEquals(i, q.poll());
379          }
380 <        assertNull(q.poll());
380 >        assertNull(q.poll());
381      }
382  
383      /**
384 <     *  remove removes next element, or throws NSEE if empty
384 >     * remove() removes next element, or throws NSEE if empty
385       */
386      public void testRemove() {
387          ArrayDeque q = populatedDeque(SIZE);
388          for (int i = 0; i < SIZE; ++i) {
389 <            assertEquals(i, ((Integer)q.remove()).intValue());
389 >            assertEquals(i, q.remove());
390          }
391          try {
392              q.remove();
393              shouldThrow();
394 <        } catch (NoSuchElementException success){
395 <        }
394 >        } catch (NoSuchElementException success) {}
395 >    }
396 >
397 >    /**
398 >     * remove(x) removes x and returns true if present
399 >     */
400 >    public void testRemoveElement() {
401 >        ArrayDeque q = populatedDeque(SIZE);
402 >        for (int i = 1; i < SIZE; i += 2) {
403 >            assertTrue(q.contains(i));
404 >            assertTrue(q.remove(i));
405 >            assertFalse(q.contains(i));
406 >            assertTrue(q.contains(i - 1));
407 >        }
408 >        for (int i = 0; i < SIZE; i += 2) {
409 >            assertTrue(q.contains(i));
410 >            assertTrue(q.remove(i));
411 >            assertFalse(q.contains(i));
412 >            assertFalse(q.remove(i + 1));
413 >            assertFalse(q.contains(i + 1));
414 >        }
415 >        assertTrue(q.isEmpty());
416      }
417  
418      /**
419 <     *  peekFirst returns next element, or null if empty
419 >     * peekFirst() returns next element, or null if empty
420       */
421      public void testPeekFirst() {
422          ArrayDeque q = populatedDeque(SIZE);
423          for (int i = 0; i < SIZE; ++i) {
424 <            assertEquals(i, ((Integer)q.peekFirst()).intValue());
425 <            q.pollFirst();
424 >            assertEquals(i, q.peekFirst());
425 >            assertEquals(i, q.pollFirst());
426              assertTrue(q.peekFirst() == null ||
427 <                       i != ((Integer)q.peekFirst()).intValue());
427 >                       !q.peekFirst().equals(i));
428          }
429 <        assertNull(q.peekFirst());
429 >        assertNull(q.peekFirst());
430      }
431  
432      /**
433 <     *  peek returns next element, or null if empty
433 >     * peek() returns next element, or null if empty
434       */
435      public void testPeek() {
436          ArrayDeque q = populatedDeque(SIZE);
437          for (int i = 0; i < SIZE; ++i) {
438 <            assertEquals(i, ((Integer)q.peek()).intValue());
439 <            q.poll();
438 >            assertEquals(i, q.peek());
439 >            assertEquals(i, q.poll());
440              assertTrue(q.peek() == null ||
441 <                       i != ((Integer)q.peek()).intValue());
441 >                       !q.peek().equals(i));
442          }
443 <        assertNull(q.peek());
443 >        assertNull(q.peek());
444      }
445  
446      /**
447 <     *  peekLast returns next element, or null if empty
447 >     * peekLast() returns next element, or null if empty
448       */
449      public void testPeekLast() {
450          ArrayDeque q = populatedDeque(SIZE);
451 <        for (int i = SIZE-1; i >= 0; --i) {
452 <            assertEquals(i, ((Integer)q.peekLast()).intValue());
453 <            q.pollLast();
451 >        for (int i = SIZE - 1; i >= 0; --i) {
452 >            assertEquals(i, q.peekLast());
453 >            assertEquals(i, q.pollLast());
454              assertTrue(q.peekLast() == null ||
455 <                       i != ((Integer)q.peekLast()).intValue());
455 >                       !q.peekLast().equals(i));
456          }
457 <        assertNull(q.peekLast());
457 >        assertNull(q.peekLast());
458      }
459  
460      /**
461 <     * getFirst returns next getFirst, or throws NSEE if empty
461 >     * element() returns first element, or throws NSEE if empty
462 >     */
463 >    public void testElement() {
464 >        ArrayDeque q = populatedDeque(SIZE);
465 >        for (int i = 0; i < SIZE; ++i) {
466 >            assertEquals(i, q.element());
467 >            assertEquals(i, q.poll());
468 >        }
469 >        try {
470 >            q.element();
471 >            shouldThrow();
472 >        } catch (NoSuchElementException success) {}
473 >    }
474 >
475 >    /**
476 >     * getFirst() returns first element, or throws NSEE if empty
477       */
478      public void testFirstElement() {
479          ArrayDeque q = populatedDeque(SIZE);
480          for (int i = 0; i < SIZE; ++i) {
481 <            assertEquals(i, ((Integer)q.getFirst()).intValue());
482 <            q.pollFirst();
481 >            assertEquals(i, q.getFirst());
482 >            assertEquals(i, q.pollFirst());
483          }
484          try {
485              q.getFirst();
486              shouldThrow();
487 <        }
307 <        catch (NoSuchElementException success) {}
487 >        } catch (NoSuchElementException success) {}
488      }
489  
490      /**
491 <     *  getLast returns next element, or throws NSEE if empty
491 >     * getLast() returns last element, or throws NSEE if empty
492       */
493      public void testLastElement() {
494          ArrayDeque q = populatedDeque(SIZE);
495 <        for (int i = SIZE-1; i >= 0; --i) {
496 <            assertEquals(i, ((Integer)q.getLast()).intValue());
497 <            q.pollLast();
495 >        for (int i = SIZE - 1; i >= 0; --i) {
496 >            assertEquals(i, q.getLast());
497 >            assertEquals(i, q.pollLast());
498          }
499          try {
500              q.getLast();
501              shouldThrow();
502 <        }
503 <        catch (NoSuchElementException success) {}
324 <        assertNull(q.peekLast());
502 >        } catch (NoSuchElementException success) {}
503 >        assertNull(q.peekLast());
504      }
505  
327
506      /**
507 <     *  removeFirst removes next element, or throws NSEE if empty
507 >     * removeFirst() removes first element, or throws NSEE if empty
508       */
509      public void testRemoveFirst() {
510          ArrayDeque q = populatedDeque(SIZE);
511          for (int i = 0; i < SIZE; ++i) {
512 <            assertEquals(i, ((Integer)q.removeFirst()).intValue());
512 >            assertEquals(i, q.removeFirst());
513          }
514          try {
515              q.removeFirst();
516              shouldThrow();
517 <        } catch (NoSuchElementException success){
518 <        }
517 >        } catch (NoSuchElementException success) {}
518 >        assertNull(q.peekFirst());
519 >    }
520 >
521 >    /**
522 >     * removeLast() removes last element, or throws NSEE if empty
523 >     */
524 >    public void testRemoveLast() {
525 >        ArrayDeque q = populatedDeque(SIZE);
526 >        for (int i = SIZE - 1; i >= 0; --i) {
527 >            assertEquals(i, q.removeLast());
528 >        }
529 >        try {
530 >            q.removeLast();
531 >            shouldThrow();
532 >        } catch (NoSuchElementException success) {}
533 >        assertNull(q.peekLast());
534      }
535  
536      /**
# Line 345 | Line 538 | public class ArrayDequeTest extends JSR1
538       */
539      public void testRemoveFirstOccurrence() {
540          ArrayDeque q = populatedDeque(SIZE);
541 <        for (int i = 1; i < SIZE; i+=2) {
541 >        assertFalse(q.removeFirstOccurrence(null));
542 >        for (int i = 1; i < SIZE; i += 2) {
543              assertTrue(q.removeFirstOccurrence(new Integer(i)));
544          }
545 <        for (int i = 0; i < SIZE; i+=2) {
545 >        for (int i = 0; i < SIZE; i += 2) {
546              assertTrue(q.removeFirstOccurrence(new Integer(i)));
547 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
547 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
548          }
549          assertTrue(q.isEmpty());
550 +        assertFalse(q.removeFirstOccurrence(null));
551 +        assertFalse(q.removeFirstOccurrence(42));
552 +        q = new ArrayDeque();
553 +        assertFalse(q.removeFirstOccurrence(null));
554 +        assertFalse(q.removeFirstOccurrence(42));
555      }
556  
557      /**
# Line 360 | Line 559 | public class ArrayDequeTest extends JSR1
559       */
560      public void testRemoveLastOccurrence() {
561          ArrayDeque q = populatedDeque(SIZE);
562 <        for (int i = 1; i < SIZE; i+=2) {
562 >        assertFalse(q.removeLastOccurrence(null));
563 >        for (int i = 1; i < SIZE; i += 2) {
564              assertTrue(q.removeLastOccurrence(new Integer(i)));
565          }
566 <        for (int i = 0; i < SIZE; i+=2) {
566 >        for (int i = 0; i < SIZE; i += 2) {
567              assertTrue(q.removeLastOccurrence(new Integer(i)));
568 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
568 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
569          }
570          assertTrue(q.isEmpty());
571 +        assertFalse(q.removeLastOccurrence(null));
572 +        assertFalse(q.removeLastOccurrence(42));
573 +        q = new ArrayDeque();
574 +        assertFalse(q.removeLastOccurrence(null));
575 +        assertFalse(q.removeLastOccurrence(42));
576      }
577  
578      /**
# Line 377 | Line 582 | public class ArrayDequeTest extends JSR1
582          ArrayDeque q = populatedDeque(SIZE);
583          for (int i = 0; i < SIZE; ++i) {
584              assertTrue(q.contains(new Integer(i)));
585 <            q.pollFirst();
585 >            assertEquals(i, q.pollFirst());
586              assertFalse(q.contains(new Integer(i)));
587          }
588      }
# Line 390 | Line 595 | public class ArrayDequeTest extends JSR1
595          q.clear();
596          assertTrue(q.isEmpty());
597          assertEquals(0, q.size());
598 <        q.add(new Integer(1));
598 >        assertTrue(q.add(new Integer(1)));
599          assertFalse(q.isEmpty());
600          q.clear();
601          assertTrue(q.isEmpty());
# Line 405 | Line 610 | public class ArrayDequeTest extends JSR1
610          for (int i = 0; i < SIZE; ++i) {
611              assertTrue(q.containsAll(p));
612              assertFalse(p.containsAll(q));
613 <            p.add(new Integer(i));
613 >            assertTrue(p.add(new Integer(i)));
614          }
615          assertTrue(p.containsAll(q));
616      }
# Line 418 | Line 623 | public class ArrayDequeTest extends JSR1
623          ArrayDeque p = populatedDeque(SIZE);
624          for (int i = 0; i < SIZE; ++i) {
625              boolean changed = q.retainAll(p);
626 <            if (i == 0)
422 <                assertFalse(changed);
423 <            else
424 <                assertTrue(changed);
425 <
626 >            assertEquals(changed, (i > 0));
627              assertTrue(q.containsAll(p));
628 <            assertEquals(SIZE-i, q.size());
628 >            assertEquals(SIZE - i, q.size());
629              p.removeFirst();
630          }
631      }
# Line 437 | Line 638 | public class ArrayDequeTest extends JSR1
638              ArrayDeque q = populatedDeque(SIZE);
639              ArrayDeque p = populatedDeque(i);
640              assertTrue(q.removeAll(p));
641 <            assertEquals(SIZE-i, q.size());
641 >            assertEquals(SIZE - i, q.size());
642              for (int j = 0; j < i; ++j) {
643 <                Integer I = (Integer)(p.removeFirst());
443 <                assertFalse(q.contains(I));
643 >                assertFalse(q.contains(p.removeFirst()));
644              }
645          }
646      }
647  
648 +    void checkToArray(ArrayDeque q) {
649 +        int size = q.size();
650 +        Object[] o = q.toArray();
651 +        assertEquals(size, o.length);
652 +        Iterator it = q.iterator();
653 +        for (int i = 0; i < size; i++) {
654 +            Integer x = (Integer) it.next();
655 +            assertEquals((Integer)o[0] + i, (int) x);
656 +            assertSame(o[i], x);
657 +        }
658 +    }
659 +
660      /**
661 <     *  toArray contains all elements
661 >     * toArray() contains all elements in FIFO order
662       */
663      public void testToArray() {
664 <        ArrayDeque q = populatedDeque(SIZE);
665 <        Object[] o = q.toArray();
666 <        Arrays.sort(o);
667 <        for (int i = 0; i < o.length; i++)
668 <            assertEquals(o[i], q.pollFirst());
664 >        ArrayDeque q = new ArrayDeque();
665 >        for (int i = 0; i < SIZE; i++) {
666 >            checkToArray(q);
667 >            q.addLast(i);
668 >        }
669 >        // Provoke wraparound
670 >        for (int i = 0; i < SIZE; i++) {
671 >            checkToArray(q);
672 >            assertEquals(i, q.poll());
673 >            q.addLast(SIZE + i);
674 >        }
675 >        for (int i = 0; i < SIZE; i++) {
676 >            checkToArray(q);
677 >            assertEquals(SIZE + i, q.poll());
678 >        }
679 >    }
680 >
681 >    void checkToArray2(ArrayDeque q) {
682 >        int size = q.size();
683 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
684 >        Integer[] a2 = new Integer[size];
685 >        Integer[] a3 = new Integer[size + 2];
686 >        if (size > 0) Arrays.fill(a1, 42);
687 >        Arrays.fill(a2, 42);
688 >        Arrays.fill(a3, 42);
689 >        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
690 >        Integer[] b2 = (Integer[]) q.toArray(a2);
691 >        Integer[] b3 = (Integer[]) q.toArray(a3);
692 >        assertSame(a2, b2);
693 >        assertSame(a3, b3);
694 >        Iterator it = q.iterator();
695 >        for (int i = 0; i < size; i++) {
696 >            Integer x = (Integer) it.next();
697 >            assertSame(b1[i], x);
698 >            assertEquals(b1[0] + i, (int) x);
699 >            assertSame(b2[i], x);
700 >            assertSame(b3[i], x);
701 >        }
702 >        assertNull(a3[size]);
703 >        assertEquals(42, (int) a3[size + 1]);
704 >        if (size > 0) {
705 >            assertNotSame(a1, b1);
706 >            assertEquals(size, b1.length);
707 >            for (int i = 0; i < a1.length; i++) {
708 >                assertEquals(42, (int) a1[i]);
709 >            }
710 >        }
711      }
712  
713      /**
714 <     *  toArray(a) contains all elements
714 >     * toArray(a) contains all elements in FIFO order
715       */
716      public void testToArray2() {
717 <        ArrayDeque q = populatedDeque(SIZE);
718 <        Integer[] ints = new Integer[SIZE];
719 <        ints = (Integer[])q.toArray(ints);
720 <        Arrays.sort(ints);
721 <        for (int i = 0; i < ints.length; i++)
722 <            assertEquals(ints[i], q.pollFirst());
717 >        ArrayDeque q = new ArrayDeque();
718 >        for (int i = 0; i < SIZE; i++) {
719 >            checkToArray2(q);
720 >            q.addLast(i);
721 >        }
722 >        // Provoke wraparound
723 >        for (int i = 0; i < SIZE; i++) {
724 >            checkToArray2(q);
725 >            assertEquals(i, q.poll());
726 >            q.addLast(SIZE + i);
727 >        }
728 >        for (int i = 0; i < SIZE; i++) {
729 >            checkToArray2(q);
730 >            assertEquals(SIZE + i, q.poll());
731 >        }
732      }
733  
734      /**
735 <     * toArray(null) throws NPE
735 >     * toArray(null) throws NullPointerException
736       */
737 <    public void testToArray_BadArg() {
738 <        try {
739 <            ArrayDeque l = new ArrayDeque();
740 <            l.add(new Object());
741 <            Object o[] = l.toArray(null);
742 <            shouldThrow();
743 <        } catch (NullPointerException success){}
737 >    public void testToArray_NullArg() {
738 >        ArrayDeque l = new ArrayDeque();
739 >        l.add(new Object());
740 >        try {
741 >            l.toArray(null);
742 >            shouldThrow();
743 >        } catch (NullPointerException success) {}
744      }
745  
746      /**
747 <     * toArray with incompatable aray type throws CCE
747 >     * toArray(incompatible array type) throws ArrayStoreException
748       */
749      public void testToArray1_BadArg() {
750 <        try {
751 <            ArrayDeque l = new ArrayDeque();
752 <            l.add(new Integer(5));
753 <            Object o[] = l.toArray(new String[10] );
754 <            shouldThrow();
755 <        } catch (ArrayStoreException  success){}
750 >        ArrayDeque l = new ArrayDeque();
751 >        l.add(new Integer(5));
752 >        try {
753 >            l.toArray(new String[10]);
754 >            shouldThrow();
755 >        } catch (ArrayStoreException success) {}
756      }
757  
758      /**
759 <     *  iterator iterates through all elements
759 >     * Iterator iterates through all elements
760       */
761      public void testIterator() {
762          ArrayDeque q = populatedDeque(SIZE);
763 <        int i = 0;
764 <        Iterator it = q.iterator();
765 <        while (it.hasNext()) {
763 >        Iterator it = q.iterator();
764 >        int i;
765 >        for (i = 0; it.hasNext(); i++)
766              assertTrue(q.contains(it.next()));
504            ++i;
505        }
767          assertEquals(i, SIZE);
768 +        assertIteratorExhausted(it);
769      }
770  
771      /**
772 <     *  iterator ordering is FIFO
772 >     * iterator of empty collection has no elements
773 >     */
774 >    public void testEmptyIterator() {
775 >        Deque c = new ArrayDeque();
776 >        assertIteratorExhausted(c.iterator());
777 >        assertIteratorExhausted(c.descendingIterator());
778 >    }
779 >
780 >    /**
781 >     * Iterator ordering is FIFO
782       */
783      public void testIteratorOrdering() {
784          final ArrayDeque q = new ArrayDeque();
785 <        q.add(new Integer(1));
786 <        q.add(new Integer(2));
787 <        q.add(new Integer(3));
785 >        q.add(one);
786 >        q.add(two);
787 >        q.add(three);
788          int k = 0;
789          for (Iterator it = q.iterator(); it.hasNext();) {
790 <            int i = ((Integer)(it.next())).intValue();
520 <            assertEquals(++k, i);
790 >            assertEquals(++k, it.next());
791          }
792  
793          assertEquals(3, k);
794      }
795  
796      /**
797 <     * iterator.remove removes current element
797 >     * iterator.remove() removes current element
798       */
799 <    public void testIteratorRemove () {
799 >    public void testIteratorRemove() {
800          final ArrayDeque q = new ArrayDeque();
801          final Random rng = new Random();
802          for (int iters = 0; iters < 100; ++iters) {
803              int max = rng.nextInt(5) + 2;
804 <            int split = rng.nextInt(max-1) + 1;
804 >            int split = rng.nextInt(max - 1) + 1;
805              for (int j = 1; j <= max; ++j)
806                  q.add(new Integer(j));
807              Iterator it = q.iterator();
808              for (int j = 1; j <= split; ++j)
809                  assertEquals(it.next(), new Integer(j));
810              it.remove();
811 <            assertEquals(it.next(), new Integer(split+1));
811 >            assertEquals(it.next(), new Integer(split + 1));
812              for (int j = 1; j <= split; ++j)
813                  q.remove(new Integer(j));
814              it = q.iterator();
815 <            for (int j = split+1; j <= max; ++j) {
815 >            for (int j = split + 1; j <= max; ++j) {
816                  assertEquals(it.next(), new Integer(j));
817                  it.remove();
818              }
# Line 552 | Line 822 | public class ArrayDequeTest extends JSR1
822      }
823  
824      /**
825 <     *  Descending iterator iterates through all elements
825 >     * Descending iterator iterates through all elements
826       */
827      public void testDescendingIterator() {
828          ArrayDeque q = populatedDeque(SIZE);
829          int i = 0;
830 <        Iterator it = q.descendingIterator();
830 >        Iterator it = q.descendingIterator();
831          while (it.hasNext()) {
832              assertTrue(q.contains(it.next()));
833              ++i;
# Line 566 | Line 836 | public class ArrayDequeTest extends JSR1
836          assertFalse(it.hasNext());
837          try {
838              it.next();
839 <        } catch (NoSuchElementException success) {
840 <        }
839 >            shouldThrow();
840 >        } catch (NoSuchElementException success) {}
841      }
842  
843      /**
844 <     *  Descending iterator ordering is reverse FIFO
844 >     * Descending iterator ordering is reverse FIFO
845       */
846      public void testDescendingIteratorOrdering() {
847          final ArrayDeque q = new ArrayDeque();
# Line 581 | Line 851 | public class ArrayDequeTest extends JSR1
851              q.add(new Integer(1));
852              int k = 0;
853              for (Iterator it = q.descendingIterator(); it.hasNext();) {
854 <                int i = ((Integer)(it.next())).intValue();
585 <                assertEquals(++k, i);
854 >                assertEquals(++k, it.next());
855              }
856  
857              assertEquals(3, k);
# Line 593 | Line 862 | public class ArrayDequeTest extends JSR1
862      }
863  
864      /**
865 <     * descendingIterator.remove removes current element
865 >     * descendingIterator.remove() removes current element
866       */
867 <    public void testDescendingIteratorRemove () {
867 >    public void testDescendingIteratorRemove() {
868          final ArrayDeque q = new ArrayDeque();
869          final Random rng = new Random();
870          for (int iters = 0; iters < 100; ++iters) {
871              int max = rng.nextInt(5) + 2;
872 <            int split = rng.nextInt(max-1) + 1;
872 >            int split = rng.nextInt(max - 1) + 1;
873              for (int j = max; j >= 1; --j)
874                  q.add(new Integer(j));
875              Iterator it = q.descendingIterator();
876              for (int j = 1; j <= split; ++j)
877                  assertEquals(it.next(), new Integer(j));
878              it.remove();
879 <            assertEquals(it.next(), new Integer(split+1));
879 >            assertEquals(it.next(), new Integer(split + 1));
880              for (int j = 1; j <= split; ++j)
881                  q.remove(new Integer(j));
882              it = q.descendingIterator();
883 <            for (int j = split+1; j <= max; ++j) {
883 >            for (int j = split + 1; j <= max; ++j) {
884                  assertEquals(it.next(), new Integer(j));
885                  it.remove();
886              }
# Line 620 | Line 889 | public class ArrayDequeTest extends JSR1
889          }
890      }
891  
623
892      /**
893 <     * toString contains toStrings of elements
893 >     * toString() contains toStrings of elements
894       */
895      public void testToString() {
896          ArrayDeque q = populatedDeque(SIZE);
897          String s = q.toString();
898          for (int i = 0; i < SIZE; ++i) {
899 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
899 >            assertTrue(s.contains(String.valueOf(i)));
900          }
901      }
902  
903      /**
904 <     * peekFirst returns element inserted with addFirst
904 >     * A deserialized serialized deque has same elements in same order
905       */
906 <    public void testAddFirst() {
907 <        ArrayDeque q = populatedDeque(3);
908 <        q.addFirst(four);
909 <        assertEquals(four,q.peekFirst());
906 >    public void testSerialization() throws Exception {
907 >        Queue x = populatedDeque(SIZE);
908 >        Queue y = serialClone(x);
909 >
910 >        assertNotSame(y, x);
911 >        assertEquals(x.size(), y.size());
912 >        assertEquals(x.toString(), y.toString());
913 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
914 >        while (!x.isEmpty()) {
915 >            assertFalse(y.isEmpty());
916 >            assertEquals(x.remove(), y.remove());
917 >        }
918 >        assertTrue(y.isEmpty());
919      }
920  
921      /**
922 <     * peekLast returns element inserted with addLast
922 >     * remove(null), contains(null) always return false
923       */
924 <    public void testAddLast() {
925 <        ArrayDeque q = populatedDeque(3);
926 <        q.addLast(four);
927 <        assertEquals(four,q.peekLast());
924 >    public void testNeverContainsNull() {
925 >        Deque<?>[] qs = {
926 >            new ArrayDeque<Object>(),
927 >            populatedDeque(2),
928 >        };
929 >
930 >        for (Deque<?> q : qs) {
931 >            assertFalse(q.contains(null));
932 >            assertFalse(q.remove(null));
933 >            assertFalse(q.removeFirstOccurrence(null));
934 >            assertFalse(q.removeLastOccurrence(null));
935 >        }
936      }
937  
938   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines