10 |
|
|
11 |
|
public class ArrayDequeTest extends JSR166TestCase { |
12 |
|
public static void main(String[] args) { |
13 |
< |
junit.textui.TestRunner.run (suite()); |
13 |
> |
junit.textui.TestRunner.run (suite()); |
14 |
|
} |
15 |
|
|
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(ArrayDequeTest.class); |
17 |
> |
return new TestSuite(ArrayDequeTest.class); |
18 |
|
} |
19 |
|
|
20 |
|
/** |
24 |
|
private ArrayDeque populatedDeque(int n) { |
25 |
|
ArrayDeque q = new ArrayDeque(); |
26 |
|
assertTrue(q.isEmpty()); |
27 |
< |
for(int i = 0; i < n; ++i) |
28 |
< |
assertTrue(q.offerLast(new Integer(i))); |
27 |
> |
for (int i = 0; i < n; ++i) |
28 |
> |
assertTrue(q.offerLast(new Integer(i))); |
29 |
|
assertFalse(q.isEmpty()); |
30 |
< |
assertEquals(n, q.size()); |
30 |
> |
assertEquals(n, q.size()); |
31 |
|
return q; |
32 |
|
} |
33 |
< |
|
33 |
> |
|
34 |
|
/** |
35 |
|
* new queue is empty |
36 |
|
*/ |
45 |
|
try { |
46 |
|
ArrayDeque q = new ArrayDeque((Collection)null); |
47 |
|
shouldThrow(); |
48 |
< |
} |
49 |
< |
catch (NullPointerException success) {} |
48 |
> |
} catch (NullPointerException success) {} |
49 |
|
} |
50 |
|
|
51 |
|
/** |
53 |
|
|
54 |
|
*/ |
55 |
|
public void testConstructor6() { |
56 |
< |
try { |
57 |
< |
Integer[] ints = new Integer[SIZE]; |
58 |
< |
for (int i = 0; i < SIZE; ++i) |
59 |
< |
ints[i] = new Integer(i); |
60 |
< |
ArrayDeque q = new ArrayDeque(Arrays.asList(ints)); |
61 |
< |
for (int i = 0; i < SIZE; ++i) |
63 |
< |
assertEquals(ints[i], q.pollFirst()); |
64 |
< |
} |
65 |
< |
finally {} |
56 |
> |
Integer[] ints = new Integer[SIZE]; |
57 |
> |
for (int i = 0; i < SIZE; ++i) |
58 |
> |
ints[i] = new Integer(i); |
59 |
> |
ArrayDeque q = new ArrayDeque(Arrays.asList(ints)); |
60 |
> |
for (int i = 0; i < SIZE; ++i) |
61 |
> |
assertEquals(ints[i], q.pollFirst()); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
94 |
|
* push(null) throws NPE |
95 |
|
*/ |
96 |
|
public void testPushNull() { |
97 |
< |
try { |
97 |
> |
try { |
98 |
|
ArrayDeque q = new ArrayDeque(1); |
99 |
|
q.push(null); |
100 |
|
shouldThrow(); |
101 |
< |
} catch (NullPointerException success) { } |
101 |
> |
} catch (NullPointerException success) {} |
102 |
|
} |
103 |
|
|
104 |
|
/** |
107 |
|
public void testPush() { |
108 |
|
ArrayDeque q = populatedDeque(3); |
109 |
|
q.pollLast(); |
110 |
< |
q.push(four); |
111 |
< |
assertEquals(four,q.peekFirst()); |
112 |
< |
} |
110 |
> |
q.push(four); |
111 |
> |
assertEquals(four,q.peekFirst()); |
112 |
> |
} |
113 |
|
|
114 |
|
/** |
115 |
|
* pop removes next element, or throws NSEE if empty |
122 |
|
try { |
123 |
|
q.pop(); |
124 |
|
shouldThrow(); |
125 |
< |
} catch (NoSuchElementException success){ |
130 |
< |
} |
125 |
> |
} catch (NoSuchElementException success) {} |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
|
* offer(null) throws NPE |
130 |
|
*/ |
131 |
|
public void testOfferFirstNull() { |
132 |
< |
try { |
132 |
> |
try { |
133 |
|
ArrayDeque q = new ArrayDeque(); |
134 |
|
q.offerFirst(null); |
135 |
|
shouldThrow(); |
136 |
< |
} catch (NullPointerException success) { |
142 |
< |
} |
136 |
> |
} catch (NullPointerException success) {} |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* OfferFirst succeeds |
140 |
> |
* OfferFirst succeeds |
141 |
|
*/ |
142 |
|
public void testOfferFirst() { |
143 |
|
ArrayDeque q = new ArrayDeque(); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
< |
* OfferLast succeeds |
149 |
> |
* OfferLast succeeds |
150 |
|
*/ |
151 |
|
public void testOfferLast() { |
152 |
|
ArrayDeque q = new ArrayDeque(); |
173 |
|
ArrayDeque q = new ArrayDeque(); |
174 |
|
q.addAll(null); |
175 |
|
shouldThrow(); |
176 |
< |
} |
183 |
< |
catch (NullPointerException success) {} |
176 |
> |
} catch (NullPointerException success) {} |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
|
* Queue contains all elements, in traversal order, of successful addAll |
181 |
|
*/ |
182 |
|
public void testAddAll5() { |
183 |
< |
try { |
184 |
< |
Integer[] empty = new Integer[0]; |
185 |
< |
Integer[] ints = new Integer[SIZE]; |
186 |
< |
for (int i = 0; i < SIZE; ++i) |
187 |
< |
ints[i] = new Integer(i); |
188 |
< |
ArrayDeque q = new ArrayDeque(); |
189 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
190 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
191 |
< |
for (int i = 0; i < SIZE; ++i) |
199 |
< |
assertEquals(ints[i], q.pollFirst()); |
200 |
< |
} |
201 |
< |
finally {} |
183 |
> |
Integer[] empty = new Integer[0]; |
184 |
> |
Integer[] ints = new Integer[SIZE]; |
185 |
> |
for (int i = 0; i < SIZE; ++i) |
186 |
> |
ints[i] = new Integer(i); |
187 |
> |
ArrayDeque q = new ArrayDeque(); |
188 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
189 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
190 |
> |
for (int i = 0; i < SIZE; ++i) |
191 |
> |
assertEquals(ints[i], q.pollFirst()); |
192 |
|
} |
193 |
|
|
194 |
|
/** |
199 |
|
for (int i = 0; i < SIZE; ++i) { |
200 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
201 |
|
} |
202 |
< |
assertNull(q.pollFirst()); |
202 |
> |
assertNull(q.pollFirst()); |
203 |
|
} |
204 |
|
|
205 |
|
/** |
210 |
|
for (int i = SIZE-1; i >= 0; --i) { |
211 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
212 |
|
} |
213 |
< |
assertNull(q.pollLast()); |
213 |
> |
assertNull(q.pollLast()); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
221 |
|
for (int i = 0; i < SIZE; ++i) { |
222 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
223 |
|
} |
224 |
< |
assertNull(q.poll()); |
224 |
> |
assertNull(q.poll()); |
225 |
|
} |
226 |
|
|
227 |
|
/** |
235 |
|
try { |
236 |
|
q.remove(); |
237 |
|
shouldThrow(); |
238 |
< |
} catch (NoSuchElementException success){ |
249 |
< |
} |
238 |
> |
} catch (NoSuchElementException success) {} |
239 |
|
} |
240 |
|
|
241 |
|
/** |
249 |
|
assertTrue(q.peekFirst() == null || |
250 |
|
i != ((Integer)q.peekFirst()).intValue()); |
251 |
|
} |
252 |
< |
assertNull(q.peekFirst()); |
252 |
> |
assertNull(q.peekFirst()); |
253 |
|
} |
254 |
|
|
255 |
|
/** |
263 |
|
assertTrue(q.peek() == null || |
264 |
|
i != ((Integer)q.peek()).intValue()); |
265 |
|
} |
266 |
< |
assertNull(q.peek()); |
266 |
> |
assertNull(q.peek()); |
267 |
|
} |
268 |
|
|
269 |
|
/** |
277 |
|
assertTrue(q.peekLast() == null || |
278 |
|
i != ((Integer)q.peekLast()).intValue()); |
279 |
|
} |
280 |
< |
assertNull(q.peekLast()); |
280 |
> |
assertNull(q.peekLast()); |
281 |
|
} |
282 |
|
|
283 |
|
/** |
292 |
|
try { |
293 |
|
q.getFirst(); |
294 |
|
shouldThrow(); |
295 |
< |
} |
307 |
< |
catch (NoSuchElementException success) {} |
295 |
> |
} catch (NoSuchElementException success) {} |
296 |
|
} |
297 |
|
|
298 |
|
/** |
307 |
|
try { |
308 |
|
q.getLast(); |
309 |
|
shouldThrow(); |
310 |
< |
} |
311 |
< |
catch (NoSuchElementException success) {} |
324 |
< |
assertNull(q.peekLast()); |
310 |
> |
} catch (NoSuchElementException success) {} |
311 |
> |
assertNull(q.peekLast()); |
312 |
|
} |
313 |
|
|
314 |
|
|
323 |
|
try { |
324 |
|
q.removeFirst(); |
325 |
|
shouldThrow(); |
326 |
< |
} catch (NoSuchElementException success){ |
340 |
< |
} |
326 |
> |
} catch (NoSuchElementException success) {} |
327 |
|
} |
328 |
|
|
329 |
|
/** |
436 |
|
*/ |
437 |
|
public void testToArray() { |
438 |
|
ArrayDeque q = populatedDeque(SIZE); |
439 |
< |
Object[] o = q.toArray(); |
439 |
> |
Object[] o = q.toArray(); |
440 |
|
Arrays.sort(o); |
441 |
< |
for(int i = 0; i < o.length; i++) |
442 |
< |
assertEquals(o[i], q.pollFirst()); |
441 |
> |
for (int i = 0; i < o.length; i++) |
442 |
> |
assertEquals(o[i], q.pollFirst()); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
447 |
|
*/ |
448 |
|
public void testToArray2() { |
449 |
|
ArrayDeque q = populatedDeque(SIZE); |
450 |
< |
Integer[] ints = new Integer[SIZE]; |
451 |
< |
ints = (Integer[])q.toArray(ints); |
450 |
> |
Integer[] ints = new Integer[SIZE]; |
451 |
> |
ints = (Integer[])q.toArray(ints); |
452 |
|
Arrays.sort(ints); |
453 |
< |
for(int i = 0; i < ints.length; i++) |
453 |
> |
for (int i = 0; i < ints.length; i++) |
454 |
|
assertEquals(ints[i], q.pollFirst()); |
455 |
|
} |
456 |
|
|
458 |
|
* toArray(null) throws NPE |
459 |
|
*/ |
460 |
|
public void testToArray_BadArg() { |
461 |
< |
try { |
462 |
< |
ArrayDeque l = new ArrayDeque(); |
463 |
< |
l.add(new Object()); |
464 |
< |
Object o[] = l.toArray(null); |
465 |
< |
shouldThrow(); |
466 |
< |
} catch(NullPointerException success){} |
461 |
> |
try { |
462 |
> |
ArrayDeque l = new ArrayDeque(); |
463 |
> |
l.add(new Object()); |
464 |
> |
Object o[] = l.toArray(null); |
465 |
> |
shouldThrow(); |
466 |
> |
} catch (NullPointerException success) {} |
467 |
|
} |
468 |
|
|
469 |
|
/** |
470 |
|
* toArray with incompatable aray type throws CCE |
471 |
|
*/ |
472 |
|
public void testToArray1_BadArg() { |
473 |
< |
try { |
474 |
< |
ArrayDeque l = new ArrayDeque(); |
475 |
< |
l.add(new Integer(5)); |
476 |
< |
Object o[] = l.toArray(new String[10] ); |
477 |
< |
shouldThrow(); |
478 |
< |
} catch(ArrayStoreException success){} |
473 |
> |
try { |
474 |
> |
ArrayDeque l = new ArrayDeque(); |
475 |
> |
l.add(new Integer(5)); |
476 |
> |
Object o[] = l.toArray(new String[10] ); |
477 |
> |
shouldThrow(); |
478 |
> |
} catch (ArrayStoreException success) {} |
479 |
|
} |
480 |
< |
|
480 |
> |
|
481 |
|
/** |
482 |
|
* iterator iterates through all elements |
483 |
|
*/ |
484 |
|
public void testIterator() { |
485 |
|
ArrayDeque q = populatedDeque(SIZE); |
486 |
|
int i = 0; |
487 |
< |
Iterator it = q.iterator(); |
488 |
< |
while(it.hasNext()) { |
487 |
> |
Iterator it = q.iterator(); |
488 |
> |
while (it.hasNext()) { |
489 |
|
assertTrue(q.contains(it.next())); |
490 |
|
++i; |
491 |
|
} |
514 |
|
*/ |
515 |
|
public void testIteratorRemove () { |
516 |
|
final ArrayDeque q = new ArrayDeque(); |
517 |
+ |
final Random rng = new Random(); |
518 |
|
for (int iters = 0; iters < 100; ++iters) { |
519 |
< |
q.add(new Integer(1)); |
520 |
< |
q.add(new Integer(2)); |
521 |
< |
q.add(new Integer(3)); |
519 |
> |
int max = rng.nextInt(5) + 2; |
520 |
> |
int split = rng.nextInt(max-1) + 1; |
521 |
> |
for (int j = 1; j <= max; ++j) |
522 |
> |
q.add(new Integer(j)); |
523 |
|
Iterator it = q.iterator(); |
524 |
< |
assertEquals(it.next(), new Integer(1)); |
524 |
> |
for (int j = 1; j <= split; ++j) |
525 |
> |
assertEquals(it.next(), new Integer(j)); |
526 |
|
it.remove(); |
527 |
< |
assertEquals(it.next(), new Integer(2)); |
527 |
> |
assertEquals(it.next(), new Integer(split+1)); |
528 |
> |
for (int j = 1; j <= split; ++j) |
529 |
> |
q.remove(new Integer(j)); |
530 |
|
it = q.iterator(); |
531 |
< |
assertEquals(it.next(), new Integer(2)); |
532 |
< |
assertEquals(it.next(), new Integer(3)); |
533 |
< |
it.remove(); |
531 |
> |
for (int j = split+1; j <= max; ++j) { |
532 |
> |
assertEquals(it.next(), new Integer(j)); |
533 |
> |
it.remove(); |
534 |
> |
} |
535 |
|
assertFalse(it.hasNext()); |
536 |
< |
q.remove(); |
536 |
> |
assertTrue(q.isEmpty()); |
537 |
|
} |
538 |
|
} |
539 |
|
|
543 |
|
public void testDescendingIterator() { |
544 |
|
ArrayDeque q = populatedDeque(SIZE); |
545 |
|
int i = 0; |
546 |
< |
Iterator it = q.descendingIterator(); |
547 |
< |
while(it.hasNext()) { |
546 |
> |
Iterator it = q.descendingIterator(); |
547 |
> |
while (it.hasNext()) { |
548 |
|
assertTrue(q.contains(it.next())); |
549 |
|
++i; |
550 |
|
} |
552 |
|
assertFalse(it.hasNext()); |
553 |
|
try { |
554 |
|
it.next(); |
555 |
< |
} catch(NoSuchElementException success) { |
556 |
< |
} |
555 |
> |
shouldThrow(); |
556 |
> |
} catch (NoSuchElementException success) {} |
557 |
|
} |
558 |
|
|
559 |
|
/** |
570 |
|
int i = ((Integer)(it.next())).intValue(); |
571 |
|
assertEquals(++k, i); |
572 |
|
} |
573 |
< |
|
573 |
> |
|
574 |
|
assertEquals(3, k); |
575 |
|
q.remove(); |
576 |
|
q.remove(); |
583 |
|
*/ |
584 |
|
public void testDescendingIteratorRemove () { |
585 |
|
final ArrayDeque q = new ArrayDeque(); |
586 |
+ |
final Random rng = new Random(); |
587 |
|
for (int iters = 0; iters < 100; ++iters) { |
588 |
< |
q.add(new Integer(3)); |
589 |
< |
q.add(new Integer(2)); |
590 |
< |
q.add(new Integer(1)); |
588 |
> |
int max = rng.nextInt(5) + 2; |
589 |
> |
int split = rng.nextInt(max-1) + 1; |
590 |
> |
for (int j = max; j >= 1; --j) |
591 |
> |
q.add(new Integer(j)); |
592 |
|
Iterator it = q.descendingIterator(); |
593 |
< |
assertEquals(it.next(), new Integer(1)); |
593 |
> |
for (int j = 1; j <= split; ++j) |
594 |
> |
assertEquals(it.next(), new Integer(j)); |
595 |
|
it.remove(); |
596 |
< |
assertEquals(it.next(), new Integer(2)); |
596 |
> |
assertEquals(it.next(), new Integer(split+1)); |
597 |
> |
for (int j = 1; j <= split; ++j) |
598 |
> |
q.remove(new Integer(j)); |
599 |
|
it = q.descendingIterator(); |
600 |
< |
assertEquals(it.next(), new Integer(2)); |
601 |
< |
assertEquals(it.next(), new Integer(3)); |
602 |
< |
it.remove(); |
600 |
> |
for (int j = split+1; j <= max; ++j) { |
601 |
> |
assertEquals(it.next(), new Integer(j)); |
602 |
> |
it.remove(); |
603 |
> |
} |
604 |
|
assertFalse(it.hasNext()); |
605 |
< |
q.remove(); |
605 |
> |
assertTrue(q.isEmpty()); |
606 |
|
} |
607 |
|
} |
608 |
|
|
616 |
|
for (int i = 0; i < SIZE; ++i) { |
617 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
618 |
|
} |
619 |
< |
} |
619 |
> |
} |
620 |
|
|
621 |
|
/** |
622 |
|
* peekFirst returns element inserted with addFirst |
623 |
|
*/ |
624 |
|
public void testAddFirst() { |
625 |
|
ArrayDeque q = populatedDeque(3); |
626 |
< |
q.addFirst(four); |
627 |
< |
assertEquals(four,q.peekFirst()); |
628 |
< |
} |
626 |
> |
q.addFirst(four); |
627 |
> |
assertEquals(four,q.peekFirst()); |
628 |
> |
} |
629 |
|
|
630 |
|
/** |
631 |
|
* peekLast returns element inserted with addLast |
632 |
|
*/ |
633 |
|
public void testAddLast() { |
634 |
|
ArrayDeque q = populatedDeque(3); |
635 |
< |
q.addLast(four); |
636 |
< |
assertEquals(four,q.peekLast()); |
637 |
< |
} |
635 |
> |
q.addLast(four); |
636 |
> |
assertEquals(four,q.peekLast()); |
637 |
> |
} |
638 |
|
|
639 |
|
} |