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 |
|
*/ |
98 |
|
* push(null) throws NPE |
99 |
|
*/ |
100 |
|
public void testPushNull() { |
101 |
< |
try { |
101 |
> |
try { |
102 |
|
ArrayDeque q = new ArrayDeque(1); |
103 |
|
q.push(null); |
104 |
|
shouldThrow(); |
105 |
< |
} catch (NullPointerException success) { } |
105 |
> |
} catch (NullPointerException success) { } |
106 |
|
} |
107 |
|
|
108 |
|
/** |
111 |
|
public void testPush() { |
112 |
|
ArrayDeque q = populatedDeque(3); |
113 |
|
q.pollLast(); |
114 |
< |
q.push(four); |
115 |
< |
assertEquals(four,q.peekFirst()); |
116 |
< |
} |
114 |
> |
q.push(four); |
115 |
> |
assertEquals(four,q.peekFirst()); |
116 |
> |
} |
117 |
|
|
118 |
|
/** |
119 |
|
* pop removes next element, or throws NSEE if empty |
126 |
|
try { |
127 |
|
q.pop(); |
128 |
|
shouldThrow(); |
129 |
< |
} catch (NoSuchElementException success){ |
130 |
< |
} |
129 |
> |
} catch (NoSuchElementException success) { |
130 |
> |
} |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
|
* offer(null) throws NPE |
135 |
|
*/ |
136 |
|
public void testOfferFirstNull() { |
137 |
< |
try { |
137 |
> |
try { |
138 |
|
ArrayDeque q = new ArrayDeque(); |
139 |
|
q.offerFirst(null); |
140 |
|
shouldThrow(); |
141 |
< |
} catch (NullPointerException success) { |
142 |
< |
} |
141 |
> |
} catch (NullPointerException success) { |
142 |
> |
} |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
< |
* OfferFirst succeeds |
146 |
> |
* OfferFirst succeeds |
147 |
|
*/ |
148 |
|
public void testOfferFirst() { |
149 |
|
ArrayDeque q = new ArrayDeque(); |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
< |
* OfferLast succeeds |
155 |
> |
* OfferLast succeeds |
156 |
|
*/ |
157 |
|
public void testOfferLast() { |
158 |
|
ArrayDeque q = new ArrayDeque(); |
209 |
|
for (int i = 0; i < SIZE; ++i) { |
210 |
|
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
211 |
|
} |
212 |
< |
assertNull(q.pollFirst()); |
212 |
> |
assertNull(q.pollFirst()); |
213 |
|
} |
214 |
|
|
215 |
|
/** |
220 |
|
for (int i = SIZE-1; i >= 0; --i) { |
221 |
|
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
222 |
|
} |
223 |
< |
assertNull(q.pollLast()); |
223 |
> |
assertNull(q.pollLast()); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
231 |
|
for (int i = 0; i < SIZE; ++i) { |
232 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
233 |
|
} |
234 |
< |
assertNull(q.poll()); |
234 |
> |
assertNull(q.poll()); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
245 |
|
try { |
246 |
|
q.remove(); |
247 |
|
shouldThrow(); |
248 |
< |
} catch (NoSuchElementException success){ |
249 |
< |
} |
248 |
> |
} catch (NoSuchElementException success) { |
249 |
> |
} |
250 |
|
} |
251 |
|
|
252 |
|
/** |
260 |
|
assertTrue(q.peekFirst() == null || |
261 |
|
i != ((Integer)q.peekFirst()).intValue()); |
262 |
|
} |
263 |
< |
assertNull(q.peekFirst()); |
263 |
> |
assertNull(q.peekFirst()); |
264 |
|
} |
265 |
|
|
266 |
|
/** |
274 |
|
assertTrue(q.peek() == null || |
275 |
|
i != ((Integer)q.peek()).intValue()); |
276 |
|
} |
277 |
< |
assertNull(q.peek()); |
277 |
> |
assertNull(q.peek()); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
288 |
|
assertTrue(q.peekLast() == null || |
289 |
|
i != ((Integer)q.peekLast()).intValue()); |
290 |
|
} |
291 |
< |
assertNull(q.peekLast()); |
291 |
> |
assertNull(q.peekLast()); |
292 |
|
} |
293 |
|
|
294 |
|
/** |
321 |
|
shouldThrow(); |
322 |
|
} |
323 |
|
catch (NoSuchElementException success) {} |
324 |
< |
assertNull(q.peekLast()); |
324 |
> |
assertNull(q.peekLast()); |
325 |
|
} |
326 |
|
|
327 |
|
|
336 |
|
try { |
337 |
|
q.removeFirst(); |
338 |
|
shouldThrow(); |
339 |
< |
} catch (NoSuchElementException success){ |
340 |
< |
} |
339 |
> |
} catch (NoSuchElementException success) { |
340 |
> |
} |
341 |
|
} |
342 |
|
|
343 |
|
/** |
450 |
|
*/ |
451 |
|
public void testToArray() { |
452 |
|
ArrayDeque q = populatedDeque(SIZE); |
453 |
< |
Object[] o = q.toArray(); |
453 |
> |
Object[] o = q.toArray(); |
454 |
|
Arrays.sort(o); |
455 |
< |
for(int i = 0; i < o.length; i++) |
456 |
< |
assertEquals(o[i], q.pollFirst()); |
455 |
> |
for (int i = 0; i < o.length; i++) |
456 |
> |
assertEquals(o[i], q.pollFirst()); |
457 |
|
} |
458 |
|
|
459 |
|
/** |
461 |
|
*/ |
462 |
|
public void testToArray2() { |
463 |
|
ArrayDeque q = populatedDeque(SIZE); |
464 |
< |
Integer[] ints = new Integer[SIZE]; |
465 |
< |
ints = (Integer[])q.toArray(ints); |
464 |
> |
Integer[] ints = new Integer[SIZE]; |
465 |
> |
ints = (Integer[])q.toArray(ints); |
466 |
|
Arrays.sort(ints); |
467 |
< |
for(int i = 0; i < ints.length; i++) |
467 |
> |
for (int i = 0; i < ints.length; i++) |
468 |
|
assertEquals(ints[i], q.pollFirst()); |
469 |
|
} |
470 |
|
|
472 |
|
* toArray(null) throws NPE |
473 |
|
*/ |
474 |
|
public void testToArray_BadArg() { |
475 |
< |
try { |
476 |
< |
ArrayDeque l = new ArrayDeque(); |
477 |
< |
l.add(new Object()); |
478 |
< |
Object o[] = l.toArray(null); |
479 |
< |
shouldThrow(); |
480 |
< |
} catch(NullPointerException success){} |
475 |
> |
try { |
476 |
> |
ArrayDeque l = new ArrayDeque(); |
477 |
> |
l.add(new Object()); |
478 |
> |
Object o[] = l.toArray(null); |
479 |
> |
shouldThrow(); |
480 |
> |
} catch (NullPointerException success) {} |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
|
* toArray with incompatable aray type throws CCE |
485 |
|
*/ |
486 |
|
public void testToArray1_BadArg() { |
487 |
< |
try { |
488 |
< |
ArrayDeque l = new ArrayDeque(); |
489 |
< |
l.add(new Integer(5)); |
490 |
< |
Object o[] = l.toArray(new String[10] ); |
491 |
< |
shouldThrow(); |
492 |
< |
} catch(ArrayStoreException success){} |
487 |
> |
try { |
488 |
> |
ArrayDeque l = new ArrayDeque(); |
489 |
> |
l.add(new Integer(5)); |
490 |
> |
Object o[] = l.toArray(new String[10] ); |
491 |
> |
shouldThrow(); |
492 |
> |
} catch (ArrayStoreException success) {} |
493 |
|
} |
494 |
< |
|
494 |
> |
|
495 |
|
/** |
496 |
|
* iterator iterates through all elements |
497 |
|
*/ |
498 |
|
public void testIterator() { |
499 |
|
ArrayDeque q = populatedDeque(SIZE); |
500 |
|
int i = 0; |
501 |
< |
Iterator it = q.iterator(); |
502 |
< |
while(it.hasNext()) { |
501 |
> |
Iterator it = q.iterator(); |
502 |
> |
while (it.hasNext()) { |
503 |
|
assertTrue(q.contains(it.next())); |
504 |
|
++i; |
505 |
|
} |
535 |
|
for (int j = 1; j <= max; ++j) |
536 |
|
q.add(new Integer(j)); |
537 |
|
Iterator it = q.iterator(); |
538 |
< |
for (int j = 1; j <= split; ++j) |
538 |
> |
for (int j = 1; j <= split; ++j) |
539 |
|
assertEquals(it.next(), new Integer(j)); |
540 |
|
it.remove(); |
541 |
|
assertEquals(it.next(), new Integer(split+1)); |
542 |
< |
for (int j = 1; j <= split; ++j) |
542 |
> |
for (int j = 1; j <= split; ++j) |
543 |
|
q.remove(new Integer(j)); |
544 |
|
it = q.iterator(); |
545 |
|
for (int j = split+1; j <= max; ++j) { |
557 |
|
public void testDescendingIterator() { |
558 |
|
ArrayDeque q = populatedDeque(SIZE); |
559 |
|
int i = 0; |
560 |
< |
Iterator it = q.descendingIterator(); |
561 |
< |
while(it.hasNext()) { |
560 |
> |
Iterator it = q.descendingIterator(); |
561 |
> |
while (it.hasNext()) { |
562 |
|
assertTrue(q.contains(it.next())); |
563 |
|
++i; |
564 |
|
} |
566 |
|
assertFalse(it.hasNext()); |
567 |
|
try { |
568 |
|
it.next(); |
569 |
< |
} catch(NoSuchElementException success) { |
569 |
> |
} catch (NoSuchElementException success) { |
570 |
|
} |
571 |
|
} |
572 |
|
|
584 |
|
int i = ((Integer)(it.next())).intValue(); |
585 |
|
assertEquals(++k, i); |
586 |
|
} |
587 |
< |
|
587 |
> |
|
588 |
|
assertEquals(3, k); |
589 |
|
q.remove(); |
590 |
|
q.remove(); |
604 |
|
for (int j = max; j >= 1; --j) |
605 |
|
q.add(new Integer(j)); |
606 |
|
Iterator it = q.descendingIterator(); |
607 |
< |
for (int j = 1; j <= split; ++j) |
607 |
> |
for (int j = 1; j <= split; ++j) |
608 |
|
assertEquals(it.next(), new Integer(j)); |
609 |
|
it.remove(); |
610 |
|
assertEquals(it.next(), new Integer(split+1)); |
611 |
< |
for (int j = 1; j <= split; ++j) |
611 |
> |
for (int j = 1; j <= split; ++j) |
612 |
|
q.remove(new Integer(j)); |
613 |
|
it = q.descendingIterator(); |
614 |
|
for (int j = split+1; j <= max; ++j) { |
630 |
|
for (int i = 0; i < SIZE; ++i) { |
631 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
632 |
|
} |
633 |
< |
} |
633 |
> |
} |
634 |
|
|
635 |
|
/** |
636 |
|
* peekFirst returns element inserted with addFirst |
637 |
|
*/ |
638 |
|
public void testAddFirst() { |
639 |
|
ArrayDeque q = populatedDeque(3); |
640 |
< |
q.addFirst(four); |
641 |
< |
assertEquals(four,q.peekFirst()); |
642 |
< |
} |
640 |
> |
q.addFirst(four); |
641 |
> |
assertEquals(four,q.peekFirst()); |
642 |
> |
} |
643 |
|
|
644 |
|
/** |
645 |
|
* peekLast returns element inserted with addLast |
646 |
|
*/ |
647 |
|
public void testAddLast() { |
648 |
|
ArrayDeque q = populatedDeque(3); |
649 |
< |
q.addLast(four); |
650 |
< |
assertEquals(four,q.peekLast()); |
651 |
< |
} |
649 |
> |
q.addLast(four); |
650 |
> |
assertEquals(four,q.peekLast()); |
651 |
> |
} |
652 |
|
|
653 |
|
} |