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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines