ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
Revision: 1.56
Committed: Fri Aug 4 03:30:21 2017 UTC (6 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.55: +1 -1 lines
Log Message:
improve javadoc wording for testSerialization methods

File Contents

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