ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
Revision: 1.53
Committed: Thu Nov 3 15:45:56 2016 UTC (7 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.52: +15 -8 lines
Log Message:
more assertions

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