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