22 |
|
} |
23 |
|
|
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(ArrayDequeTest.class); |
25 |
> |
class Implementation implements CollectionImplementation { |
26 |
> |
public Class<?> klazz() { return ArrayDeque.class; } |
27 |
> |
public Collection emptyCollection() { return new ArrayDeque(); } |
28 |
> |
public Object makeElement(int i) { return i; } |
29 |
> |
public boolean isConcurrent() { return false; } |
30 |
> |
public boolean permitsNulls() { return false; } |
31 |
> |
} |
32 |
> |
return newTestSuite(ArrayDequeTest.class, |
33 |
> |
CollectionTest.testSuite(new Implementation())); |
34 |
|
} |
35 |
|
|
36 |
|
/** |
69 |
|
*/ |
70 |
|
public void testConstructor4() { |
71 |
|
try { |
72 |
< |
Integer[] ints = new Integer[SIZE]; |
65 |
< |
new ArrayDeque(Arrays.asList(ints)); |
72 |
> |
new ArrayDeque(Arrays.asList(new Integer[SIZE])); |
73 |
|
shouldThrow(); |
74 |
|
} catch (NullPointerException success) {} |
75 |
|
} |
78 |
|
* Initializing from Collection with some null elements throws NPE |
79 |
|
*/ |
80 |
|
public void testConstructor5() { |
81 |
+ |
Integer[] ints = new Integer[SIZE]; |
82 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
83 |
+ |
ints[i] = new Integer(i); |
84 |
|
try { |
75 |
– |
Integer[] ints = new Integer[SIZE]; |
76 |
– |
for (int i = 0; i < SIZE-1; ++i) |
77 |
– |
ints[i] = new Integer(i); |
85 |
|
new ArrayDeque(Arrays.asList(ints)); |
86 |
|
shouldThrow(); |
87 |
|
} catch (NullPointerException success) {} |
119 |
|
public void testSize() { |
120 |
|
ArrayDeque q = populatedDeque(SIZE); |
121 |
|
for (int i = 0; i < SIZE; ++i) { |
122 |
< |
assertEquals(SIZE-i, q.size()); |
122 |
> |
assertEquals(SIZE - i, q.size()); |
123 |
|
q.removeFirst(); |
124 |
|
} |
125 |
|
for (int i = 0; i < SIZE; ++i) { |
132 |
|
* push(null) throws NPE |
133 |
|
*/ |
134 |
|
public void testPushNull() { |
135 |
+ |
ArrayDeque q = new ArrayDeque(1); |
136 |
|
try { |
129 |
– |
ArrayDeque q = new ArrayDeque(1); |
137 |
|
q.push(null); |
138 |
|
shouldThrow(); |
139 |
|
} catch (NullPointerException success) {} |
167 |
|
* offer(null) throws NPE |
168 |
|
*/ |
169 |
|
public void testOfferNull() { |
170 |
+ |
ArrayDeque q = new ArrayDeque(); |
171 |
|
try { |
164 |
– |
ArrayDeque q = new ArrayDeque(); |
172 |
|
q.offer(null); |
173 |
|
shouldThrow(); |
174 |
|
} catch (NullPointerException success) {} |
178 |
|
* offerFirst(null) throws NPE |
179 |
|
*/ |
180 |
|
public void testOfferFirstNull() { |
181 |
+ |
ArrayDeque q = new ArrayDeque(); |
182 |
|
try { |
175 |
– |
ArrayDeque q = new ArrayDeque(); |
183 |
|
q.offerFirst(null); |
184 |
|
shouldThrow(); |
185 |
|
} catch (NullPointerException success) {} |
189 |
|
* offerLast(null) throws NPE |
190 |
|
*/ |
191 |
|
public void testOfferLastNull() { |
192 |
+ |
ArrayDeque q = new ArrayDeque(); |
193 |
|
try { |
186 |
– |
ArrayDeque q = new ArrayDeque(); |
194 |
|
q.offerLast(null); |
195 |
|
shouldThrow(); |
196 |
|
} catch (NullPointerException success) {} |
233 |
|
* add(null) throws NPE |
234 |
|
*/ |
235 |
|
public void testAddNull() { |
236 |
+ |
ArrayDeque q = new ArrayDeque(); |
237 |
|
try { |
230 |
– |
ArrayDeque q = new ArrayDeque(); |
238 |
|
q.add(null); |
239 |
|
shouldThrow(); |
240 |
|
} catch (NullPointerException success) {} |
244 |
|
* addFirst(null) throws NPE |
245 |
|
*/ |
246 |
|
public void testAddFirstNull() { |
247 |
+ |
ArrayDeque q = new ArrayDeque(); |
248 |
|
try { |
241 |
– |
ArrayDeque q = new ArrayDeque(); |
249 |
|
q.addFirst(null); |
250 |
|
shouldThrow(); |
251 |
|
} catch (NullPointerException success) {} |
255 |
|
* addLast(null) throws NPE |
256 |
|
*/ |
257 |
|
public void testAddLastNull() { |
258 |
+ |
ArrayDeque q = new ArrayDeque(); |
259 |
|
try { |
252 |
– |
ArrayDeque q = new ArrayDeque(); |
260 |
|
q.addLast(null); |
261 |
|
shouldThrow(); |
262 |
|
} catch (NullPointerException success) {} |
299 |
|
* addAll(null) throws NPE |
300 |
|
*/ |
301 |
|
public void testAddAll1() { |
302 |
+ |
ArrayDeque q = new ArrayDeque(); |
303 |
|
try { |
296 |
– |
ArrayDeque q = new ArrayDeque(); |
304 |
|
q.addAll(null); |
305 |
|
shouldThrow(); |
306 |
|
} catch (NullPointerException success) {} |
310 |
|
* addAll of a collection with null elements throws NPE |
311 |
|
*/ |
312 |
|
public void testAddAll2() { |
313 |
+ |
ArrayDeque q = new ArrayDeque(); |
314 |
|
try { |
315 |
< |
ArrayDeque q = new ArrayDeque(); |
308 |
< |
Integer[] ints = new Integer[SIZE]; |
309 |
< |
q.addAll(Arrays.asList(ints)); |
315 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
316 |
|
shouldThrow(); |
317 |
|
} catch (NullPointerException success) {} |
318 |
|
} |
322 |
|
* possibly adding some elements |
323 |
|
*/ |
324 |
|
public void testAddAll3() { |
325 |
+ |
ArrayDeque q = new ArrayDeque(); |
326 |
+ |
Integer[] ints = new Integer[SIZE]; |
327 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
328 |
+ |
ints[i] = new Integer(i); |
329 |
|
try { |
320 |
– |
ArrayDeque q = new ArrayDeque(); |
321 |
– |
Integer[] ints = new Integer[SIZE]; |
322 |
– |
for (int i = 0; i < SIZE-1; ++i) |
323 |
– |
ints[i] = new Integer(i); |
330 |
|
q.addAll(Arrays.asList(ints)); |
331 |
|
shouldThrow(); |
332 |
|
} catch (NullPointerException success) {} |
363 |
|
*/ |
364 |
|
public void testPollLast() { |
365 |
|
ArrayDeque q = populatedDeque(SIZE); |
366 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
366 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
367 |
|
assertEquals(i, q.pollLast()); |
368 |
|
} |
369 |
|
assertNull(q.pollLast()); |
403 |
|
assertTrue(q.contains(i)); |
404 |
|
assertTrue(q.remove(i)); |
405 |
|
assertFalse(q.contains(i)); |
406 |
< |
assertTrue(q.contains(i-1)); |
406 |
> |
assertTrue(q.contains(i - 1)); |
407 |
|
} |
408 |
|
for (int i = 0; i < SIZE; i += 2) { |
409 |
|
assertTrue(q.contains(i)); |
410 |
|
assertTrue(q.remove(i)); |
411 |
|
assertFalse(q.contains(i)); |
412 |
< |
assertFalse(q.remove(i+1)); |
413 |
< |
assertFalse(q.contains(i+1)); |
412 |
> |
assertFalse(q.remove(i + 1)); |
413 |
> |
assertFalse(q.contains(i + 1)); |
414 |
|
} |
415 |
|
assertTrue(q.isEmpty()); |
416 |
|
} |
448 |
|
*/ |
449 |
|
public void testPeekLast() { |
450 |
|
ArrayDeque q = populatedDeque(SIZE); |
451 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
451 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
452 |
|
assertEquals(i, q.peekLast()); |
453 |
|
assertEquals(i, q.pollLast()); |
454 |
|
assertTrue(q.peekLast() == null || |
492 |
|
*/ |
493 |
|
public void testLastElement() { |
494 |
|
ArrayDeque q = populatedDeque(SIZE); |
495 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
495 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
496 |
|
assertEquals(i, q.getLast()); |
497 |
|
assertEquals(i, q.pollLast()); |
498 |
|
} |
538 |
|
*/ |
539 |
|
public void testRemoveFirstOccurrence() { |
540 |
|
ArrayDeque q = populatedDeque(SIZE); |
541 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
542 |
|
for (int i = 1; i < SIZE; i += 2) { |
543 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
544 |
|
} |
545 |
|
for (int i = 0; i < SIZE; i += 2) { |
546 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
547 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
547 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
548 |
|
} |
549 |
|
assertTrue(q.isEmpty()); |
550 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
551 |
+ |
assertFalse(q.removeFirstOccurrence(42)); |
552 |
+ |
q = new ArrayDeque(); |
553 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
554 |
+ |
assertFalse(q.removeFirstOccurrence(42)); |
555 |
|
} |
556 |
|
|
557 |
|
/** |
559 |
|
*/ |
560 |
|
public void testRemoveLastOccurrence() { |
561 |
|
ArrayDeque q = populatedDeque(SIZE); |
562 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
563 |
|
for (int i = 1; i < SIZE; i += 2) { |
564 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
565 |
|
} |
566 |
|
for (int i = 0; i < SIZE; i += 2) { |
567 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
568 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
568 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
569 |
|
} |
570 |
|
assertTrue(q.isEmpty()); |
571 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
572 |
+ |
assertFalse(q.removeLastOccurrence(42)); |
573 |
+ |
q = new ArrayDeque(); |
574 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
575 |
+ |
assertFalse(q.removeLastOccurrence(42)); |
576 |
|
} |
577 |
|
|
578 |
|
/** |
625 |
|
boolean changed = q.retainAll(p); |
626 |
|
assertEquals(changed, (i > 0)); |
627 |
|
assertTrue(q.containsAll(p)); |
628 |
< |
assertEquals(SIZE-i, q.size()); |
628 |
> |
assertEquals(SIZE - i, q.size()); |
629 |
|
p.removeFirst(); |
630 |
|
} |
631 |
|
} |
638 |
|
ArrayDeque q = populatedDeque(SIZE); |
639 |
|
ArrayDeque p = populatedDeque(i); |
640 |
|
assertTrue(q.removeAll(p)); |
641 |
< |
assertEquals(SIZE-i, q.size()); |
641 |
> |
assertEquals(SIZE - i, q.size()); |
642 |
|
for (int j = 0; j < i; ++j) { |
643 |
|
assertFalse(q.contains(p.removeFirst())); |
644 |
|
} |
670 |
|
for (int i = 0; i < SIZE; i++) { |
671 |
|
checkToArray(q); |
672 |
|
assertEquals(i, q.poll()); |
673 |
< |
q.addLast(SIZE+i); |
673 |
> |
q.addLast(SIZE + i); |
674 |
|
} |
675 |
|
for (int i = 0; i < SIZE; i++) { |
676 |
|
checkToArray(q); |
677 |
< |
assertEquals(SIZE+i, q.poll()); |
677 |
> |
assertEquals(SIZE + i, q.poll()); |
678 |
|
} |
679 |
|
} |
680 |
|
|
681 |
|
void checkToArray2(ArrayDeque q) { |
682 |
|
int size = q.size(); |
683 |
< |
Integer[] a1 = size == 0 ? null : new Integer[size-1]; |
683 |
> |
Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; |
684 |
|
Integer[] a2 = new Integer[size]; |
685 |
< |
Integer[] a3 = new Integer[size+2]; |
685 |
> |
Integer[] a3 = new Integer[size + 2]; |
686 |
|
if (size > 0) Arrays.fill(a1, 42); |
687 |
|
Arrays.fill(a2, 42); |
688 |
|
Arrays.fill(a3, 42); |
689 |
< |
Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); |
689 |
> |
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); |
690 |
|
Integer[] b2 = (Integer[]) q.toArray(a2); |
691 |
|
Integer[] b3 = (Integer[]) q.toArray(a3); |
692 |
|
assertSame(a2, b2); |
700 |
|
assertSame(b3[i], x); |
701 |
|
} |
702 |
|
assertNull(a3[size]); |
703 |
< |
assertEquals(42, (int) a3[size+1]); |
703 |
> |
assertEquals(42, (int) a3[size + 1]); |
704 |
|
if (size > 0) { |
705 |
|
assertNotSame(a1, b1); |
706 |
|
assertEquals(size, b1.length); |
723 |
|
for (int i = 0; i < SIZE; i++) { |
724 |
|
checkToArray2(q); |
725 |
|
assertEquals(i, q.poll()); |
726 |
< |
q.addLast(SIZE+i); |
726 |
> |
q.addLast(SIZE + i); |
727 |
|
} |
728 |
|
for (int i = 0; i < SIZE; i++) { |
729 |
|
checkToArray2(q); |
730 |
< |
assertEquals(SIZE+i, q.poll()); |
730 |
> |
assertEquals(SIZE + i, q.poll()); |
731 |
|
} |
732 |
|
} |
733 |
|
|
801 |
|
final Random rng = new Random(); |
802 |
|
for (int iters = 0; iters < 100; ++iters) { |
803 |
|
int max = rng.nextInt(5) + 2; |
804 |
< |
int split = rng.nextInt(max-1) + 1; |
804 |
> |
int split = rng.nextInt(max - 1) + 1; |
805 |
|
for (int j = 1; j <= max; ++j) |
806 |
|
q.add(new Integer(j)); |
807 |
|
Iterator it = q.iterator(); |
808 |
|
for (int j = 1; j <= split; ++j) |
809 |
|
assertEquals(it.next(), new Integer(j)); |
810 |
|
it.remove(); |
811 |
< |
assertEquals(it.next(), new Integer(split+1)); |
811 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
812 |
|
for (int j = 1; j <= split; ++j) |
813 |
|
q.remove(new Integer(j)); |
814 |
|
it = q.iterator(); |
815 |
< |
for (int j = split+1; j <= max; ++j) { |
815 |
> |
for (int j = split + 1; j <= max; ++j) { |
816 |
|
assertEquals(it.next(), new Integer(j)); |
817 |
|
it.remove(); |
818 |
|
} |
869 |
|
final Random rng = new Random(); |
870 |
|
for (int iters = 0; iters < 100; ++iters) { |
871 |
|
int max = rng.nextInt(5) + 2; |
872 |
< |
int split = rng.nextInt(max-1) + 1; |
872 |
> |
int split = rng.nextInt(max - 1) + 1; |
873 |
|
for (int j = max; j >= 1; --j) |
874 |
|
q.add(new Integer(j)); |
875 |
|
Iterator it = q.descendingIterator(); |
876 |
|
for (int j = 1; j <= split; ++j) |
877 |
|
assertEquals(it.next(), new Integer(j)); |
878 |
|
it.remove(); |
879 |
< |
assertEquals(it.next(), new Integer(split+1)); |
879 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
880 |
|
for (int j = 1; j <= split; ++j) |
881 |
|
q.remove(new Integer(j)); |
882 |
|
it = q.descendingIterator(); |
883 |
< |
for (int j = split+1; j <= max; ++j) { |
883 |
> |
for (int j = split + 1; j <= max; ++j) { |
884 |
|
assertEquals(it.next(), new Integer(j)); |
885 |
|
it.remove(); |
886 |
|
} |