1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
12 |
|
|
13 |
|
public class LinkedListTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
17 |
|
|
18 |
|
public static Test suite() { |
19 |
< |
return new TestSuite(LinkedListTest.class); |
19 |
> |
return new TestSuite(LinkedListTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
/** |
26 |
|
private LinkedList populatedQueue(int n) { |
27 |
|
LinkedList q = new LinkedList(); |
28 |
|
assertTrue(q.isEmpty()); |
29 |
< |
for(int i = 0; i < n; ++i) |
30 |
< |
assertTrue(q.offer(new Integer(i))); |
29 |
> |
for (int i = 0; i < n; ++i) |
30 |
> |
assertTrue(q.offer(new Integer(i))); |
31 |
|
assertFalse(q.isEmpty()); |
32 |
< |
assertEquals(n, q.size()); |
32 |
> |
assertEquals(n, q.size()); |
33 |
|
return q; |
34 |
|
} |
35 |
< |
|
35 |
> |
|
36 |
|
/** |
37 |
|
* new queue is empty |
38 |
|
*/ |
47 |
|
try { |
48 |
|
LinkedList q = new LinkedList((Collection)null); |
49 |
|
shouldThrow(); |
50 |
< |
} |
50 |
< |
catch (NullPointerException success) {} |
50 |
> |
} catch (NullPointerException success) {} |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
|
* Queue contains all elements of collection used to initialize |
55 |
– |
|
55 |
|
*/ |
56 |
|
public void testConstructor6() { |
57 |
< |
try { |
58 |
< |
Integer[] ints = new Integer[SIZE]; |
59 |
< |
for (int i = 0; i < SIZE; ++i) |
60 |
< |
ints[i] = new Integer(i); |
61 |
< |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
62 |
< |
for (int i = 0; i < SIZE; ++i) |
64 |
< |
assertEquals(ints[i], q.poll()); |
65 |
< |
} |
66 |
< |
finally {} |
57 |
> |
Integer[] ints = new Integer[SIZE]; |
58 |
> |
for (int i = 0; i < SIZE; ++i) |
59 |
> |
ints[i] = new Integer(i); |
60 |
> |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
61 |
> |
for (int i = 0; i < SIZE; ++i) |
62 |
> |
assertEquals(ints[i], q.poll()); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
95 |
|
* offer(null) succeeds |
96 |
|
*/ |
97 |
|
public void testOfferNull() { |
98 |
< |
try { |
99 |
< |
LinkedList q = new LinkedList(); |
104 |
< |
q.offer(null); |
105 |
< |
} catch (NullPointerException ie) { |
106 |
< |
unexpectedException(); |
107 |
< |
} |
98 |
> |
LinkedList q = new LinkedList(); |
99 |
> |
q.offer(null); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* Offer succeeds |
103 |
> |
* Offer succeeds |
104 |
|
*/ |
105 |
|
public void testOffer() { |
106 |
|
LinkedList q = new LinkedList(); |
127 |
|
LinkedList q = new LinkedList(); |
128 |
|
q.addAll(null); |
129 |
|
shouldThrow(); |
130 |
< |
} |
139 |
< |
catch (NullPointerException success) {} |
130 |
> |
} catch (NullPointerException success) {} |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
|
* Queue contains all elements, in traversal order, of successful addAll |
135 |
|
*/ |
136 |
|
public void testAddAll5() { |
137 |
< |
try { |
138 |
< |
Integer[] empty = new Integer[0]; |
139 |
< |
Integer[] ints = new Integer[SIZE]; |
140 |
< |
for (int i = 0; i < SIZE; ++i) |
141 |
< |
ints[i] = new Integer(i); |
142 |
< |
LinkedList q = new LinkedList(); |
143 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
144 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
145 |
< |
for (int i = 0; i < SIZE; ++i) |
155 |
< |
assertEquals(ints[i], q.poll()); |
156 |
< |
} |
157 |
< |
finally {} |
137 |
> |
Integer[] empty = new Integer[0]; |
138 |
> |
Integer[] ints = new Integer[SIZE]; |
139 |
> |
for (int i = 0; i < SIZE; ++i) |
140 |
> |
ints[i] = new Integer(i); |
141 |
> |
LinkedList q = new LinkedList(); |
142 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
143 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
144 |
> |
for (int i = 0; i < SIZE; ++i) |
145 |
> |
assertEquals(ints[i], q.poll()); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
|
* addAll with too large an index throws IOOBE |
150 |
|
*/ |
151 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
152 |
< |
try { |
153 |
< |
LinkedList l = new LinkedList(); |
154 |
< |
l.add(new Object()); |
155 |
< |
LinkedList m = new LinkedList(); |
156 |
< |
m.add(new Object()); |
157 |
< |
l.addAll(4,m); |
158 |
< |
shouldThrow(); |
159 |
< |
} catch(IndexOutOfBoundsException success) {} |
152 |
> |
LinkedList l = new LinkedList(); |
153 |
> |
l.add(new Object()); |
154 |
> |
LinkedList m = new LinkedList(); |
155 |
> |
m.add(new Object()); |
156 |
> |
try { |
157 |
> |
l.addAll(4,m); |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (IndexOutOfBoundsException success) {} |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* addAll with negative index throws IOOBE |
164 |
|
*/ |
165 |
|
public void testAddAll4_BadIndex() { |
166 |
< |
try { |
167 |
< |
LinkedList l = new LinkedList(); |
168 |
< |
l.add(new Object()); |
169 |
< |
LinkedList m = new LinkedList(); |
170 |
< |
m.add(new Object()); |
171 |
< |
l.addAll(-1,m); |
172 |
< |
shouldThrow(); |
173 |
< |
} catch(IndexOutOfBoundsException success){} |
166 |
> |
LinkedList l = new LinkedList(); |
167 |
> |
l.add(new Object()); |
168 |
> |
LinkedList m = new LinkedList(); |
169 |
> |
m.add(new Object()); |
170 |
> |
try { |
171 |
> |
l.addAll(-1,m); |
172 |
> |
shouldThrow(); |
173 |
> |
} catch (IndexOutOfBoundsException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
181 |
|
for (int i = 0; i < SIZE; ++i) { |
182 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
183 |
|
} |
184 |
< |
assertNull(q.poll()); |
184 |
> |
assertNull(q.poll()); |
185 |
|
} |
186 |
|
|
187 |
|
/** |
195 |
|
assertTrue(q.peek() == null || |
196 |
|
i != ((Integer)q.peek()).intValue()); |
197 |
|
} |
198 |
< |
assertNull(q.peek()); |
198 |
> |
assertNull(q.peek()); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
< |
* |
202 |
> |
* element returns next element, or throws NSEE if empty |
203 |
|
*/ |
204 |
|
public void testElement() { |
205 |
|
LinkedList q = populatedQueue(SIZE); |
210 |
|
try { |
211 |
|
q.element(); |
212 |
|
shouldThrow(); |
213 |
< |
} |
226 |
< |
catch (NoSuchElementException success) {} |
213 |
> |
} catch (NoSuchElementException success) {} |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
< |
* element returns next element, or throws NSEE if empty |
217 |
> |
* remove removes next element, or throws NSEE if empty |
218 |
|
*/ |
219 |
|
public void testRemove() { |
220 |
|
LinkedList q = populatedQueue(SIZE); |
224 |
|
try { |
225 |
|
q.remove(); |
226 |
|
shouldThrow(); |
227 |
< |
} catch (NoSuchElementException success){ |
241 |
< |
} |
227 |
> |
} catch (NoSuchElementException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
241 |
|
} |
242 |
|
assertTrue(q.isEmpty()); |
243 |
|
} |
244 |
< |
|
244 |
> |
|
245 |
|
/** |
246 |
|
* contains(x) reports true when elements added but not yet removed |
247 |
|
*/ |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
< |
* removeAll(c) removes only those elements of c and reports true if changed |
305 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
306 |
|
*/ |
307 |
|
public void testRemoveAll() { |
308 |
|
for (int i = 1; i < SIZE; ++i) { |
322 |
|
*/ |
323 |
|
public void testToArray() { |
324 |
|
LinkedList q = populatedQueue(SIZE); |
325 |
< |
Object[] o = q.toArray(); |
325 |
> |
Object[] o = q.toArray(); |
326 |
|
Arrays.sort(o); |
327 |
< |
for(int i = 0; i < o.length; i++) |
328 |
< |
assertEquals(o[i], q.poll()); |
327 |
> |
for (int i = 0; i < o.length; i++) |
328 |
> |
assertEquals(o[i], q.poll()); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
333 |
|
*/ |
334 |
|
public void testToArray2() { |
335 |
|
LinkedList q = populatedQueue(SIZE); |
336 |
< |
Integer[] ints = new Integer[SIZE]; |
337 |
< |
ints = (Integer[])q.toArray(ints); |
336 |
> |
Integer[] ints = new Integer[SIZE]; |
337 |
> |
ints = (Integer[])q.toArray(ints); |
338 |
|
Arrays.sort(ints); |
339 |
< |
for(int i = 0; i < ints.length; i++) |
339 |
> |
for (int i = 0; i < ints.length; i++) |
340 |
|
assertEquals(ints[i], q.poll()); |
341 |
|
} |
342 |
|
|
344 |
|
* toArray(null) throws NPE |
345 |
|
*/ |
346 |
|
public void testToArray_BadArg() { |
347 |
< |
try { |
348 |
< |
LinkedList l = new LinkedList(); |
349 |
< |
l.add(new Object()); |
350 |
< |
Object o[] = l.toArray(null); |
351 |
< |
shouldThrow(); |
352 |
< |
} catch(NullPointerException success){} |
347 |
> |
try { |
348 |
> |
LinkedList l = new LinkedList(); |
349 |
> |
l.add(new Object()); |
350 |
> |
Object o[] = l.toArray(null); |
351 |
> |
shouldThrow(); |
352 |
> |
} catch (NullPointerException success) {} |
353 |
|
} |
354 |
|
|
355 |
|
/** |
356 |
|
* toArray with incompatable aray type throws CCE |
357 |
|
*/ |
358 |
|
public void testToArray1_BadArg() { |
359 |
< |
try { |
360 |
< |
LinkedList l = new LinkedList(); |
361 |
< |
l.add(new Integer(5)); |
362 |
< |
Object o[] = l.toArray(new String[10] ); |
363 |
< |
shouldThrow(); |
364 |
< |
} catch(ArrayStoreException success){} |
359 |
> |
try { |
360 |
> |
LinkedList l = new LinkedList(); |
361 |
> |
l.add(new Integer(5)); |
362 |
> |
Object o[] = l.toArray(new String[10] ); |
363 |
> |
shouldThrow(); |
364 |
> |
} catch (ArrayStoreException success) {} |
365 |
|
} |
366 |
< |
|
366 |
> |
|
367 |
|
/** |
368 |
|
* iterator iterates through all elements |
369 |
|
*/ |
370 |
|
public void testIterator() { |
371 |
|
LinkedList q = populatedQueue(SIZE); |
372 |
|
int i = 0; |
373 |
< |
Iterator it = q.iterator(); |
374 |
< |
while(it.hasNext()) { |
373 |
> |
Iterator it = q.iterator(); |
374 |
> |
while (it.hasNext()) { |
375 |
|
assertTrue(q.contains(it.next())); |
376 |
|
++i; |
377 |
|
} |
412 |
|
assertFalse(it.hasNext()); |
413 |
|
} |
414 |
|
|
415 |
+ |
/** |
416 |
+ |
* Descending iterator iterates through all elements |
417 |
+ |
*/ |
418 |
+ |
public void testDescendingIterator() { |
419 |
+ |
LinkedList q = populatedQueue(SIZE); |
420 |
+ |
int i = 0; |
421 |
+ |
Iterator it = q.descendingIterator(); |
422 |
+ |
while (it.hasNext()) { |
423 |
+ |
assertTrue(q.contains(it.next())); |
424 |
+ |
++i; |
425 |
+ |
} |
426 |
+ |
assertEquals(i, SIZE); |
427 |
+ |
assertFalse(it.hasNext()); |
428 |
+ |
try { |
429 |
+ |
it.next(); |
430 |
+ |
shouldThrow(); |
431 |
+ |
} catch (NoSuchElementException success) {} |
432 |
+ |
} |
433 |
+ |
|
434 |
+ |
/** |
435 |
+ |
* Descending iterator ordering is reverse FIFO |
436 |
+ |
*/ |
437 |
+ |
public void testDescendingIteratorOrdering() { |
438 |
+ |
final LinkedList q = new LinkedList(); |
439 |
+ |
q.add(new Integer(3)); |
440 |
+ |
q.add(new Integer(2)); |
441 |
+ |
q.add(new Integer(1)); |
442 |
+ |
int k = 0; |
443 |
+ |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
444 |
+ |
int i = ((Integer)(it.next())).intValue(); |
445 |
+ |
assertEquals(++k, i); |
446 |
+ |
} |
447 |
+ |
|
448 |
+ |
assertEquals(3, k); |
449 |
+ |
} |
450 |
+ |
|
451 |
+ |
/** |
452 |
+ |
* descendingIterator.remove removes current element |
453 |
+ |
*/ |
454 |
+ |
public void testDescendingIteratorRemove () { |
455 |
+ |
final LinkedList q = new LinkedList(); |
456 |
+ |
q.add(new Integer(3)); |
457 |
+ |
q.add(new Integer(2)); |
458 |
+ |
q.add(new Integer(1)); |
459 |
+ |
Iterator it = q.descendingIterator(); |
460 |
+ |
it.next(); |
461 |
+ |
it.remove(); |
462 |
+ |
it = q.descendingIterator(); |
463 |
+ |
assertEquals(it.next(), new Integer(2)); |
464 |
+ |
assertEquals(it.next(), new Integer(3)); |
465 |
+ |
assertFalse(it.hasNext()); |
466 |
+ |
} |
467 |
+ |
|
468 |
|
|
469 |
|
/** |
470 |
|
* toString contains toStrings of elements |
475 |
|
for (int i = 0; i < SIZE; ++i) { |
476 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
477 |
|
} |
478 |
< |
} |
478 |
> |
} |
479 |
|
|
480 |
|
/** |
481 |
|
* peek returns element inserted with addFirst |
482 |
|
*/ |
483 |
|
public void testAddFirst() { |
484 |
|
LinkedList q = populatedQueue(3); |
485 |
< |
q.addFirst(four); |
486 |
< |
assertEquals(four,q.peek()); |
487 |
< |
} |
485 |
> |
q.addFirst(four); |
486 |
> |
assertEquals(four,q.peek()); |
487 |
> |
} |
488 |
> |
|
489 |
> |
/** |
490 |
> |
* peekFirst returns element inserted with push |
491 |
> |
*/ |
492 |
> |
public void testPush() { |
493 |
> |
LinkedList q = populatedQueue(3); |
494 |
> |
q.pollLast(); |
495 |
> |
q.push(four); |
496 |
> |
assertEquals(four,q.peekFirst()); |
497 |
> |
} |
498 |
> |
|
499 |
> |
/** |
500 |
> |
* pop removes next element, or throws NSEE if empty |
501 |
> |
*/ |
502 |
> |
public void testPop() { |
503 |
> |
LinkedList q = populatedQueue(SIZE); |
504 |
> |
for (int i = 0; i < SIZE; ++i) { |
505 |
> |
assertEquals(i, ((Integer)q.pop()).intValue()); |
506 |
> |
} |
507 |
> |
try { |
508 |
> |
q.pop(); |
509 |
> |
shouldThrow(); |
510 |
> |
} catch (NoSuchElementException success) {} |
511 |
> |
} |
512 |
> |
|
513 |
> |
/** |
514 |
> |
* OfferFirst succeeds |
515 |
> |
*/ |
516 |
> |
public void testOfferFirst() { |
517 |
> |
LinkedList q = new LinkedList(); |
518 |
> |
assertTrue(q.offerFirst(new Integer(0))); |
519 |
> |
assertTrue(q.offerFirst(new Integer(1))); |
520 |
> |
} |
521 |
> |
|
522 |
> |
/** |
523 |
> |
* OfferLast succeeds |
524 |
> |
*/ |
525 |
> |
public void testOfferLast() { |
526 |
> |
LinkedList q = new LinkedList(); |
527 |
> |
assertTrue(q.offerLast(new Integer(0))); |
528 |
> |
assertTrue(q.offerLast(new Integer(1))); |
529 |
> |
} |
530 |
> |
|
531 |
> |
/** |
532 |
> |
* pollLast succeeds unless empty |
533 |
> |
*/ |
534 |
> |
public void testPollLast() { |
535 |
> |
LinkedList q = populatedQueue(SIZE); |
536 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
537 |
> |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
538 |
> |
} |
539 |
> |
assertNull(q.pollLast()); |
540 |
> |
} |
541 |
> |
|
542 |
> |
/** |
543 |
> |
* peekFirst returns next element, or null if empty |
544 |
> |
*/ |
545 |
> |
public void testPeekFirst() { |
546 |
> |
LinkedList q = populatedQueue(SIZE); |
547 |
> |
for (int i = 0; i < SIZE; ++i) { |
548 |
> |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
549 |
> |
q.pollFirst(); |
550 |
> |
assertTrue(q.peekFirst() == null || |
551 |
> |
i != ((Integer)q.peekFirst()).intValue()); |
552 |
> |
} |
553 |
> |
assertNull(q.peekFirst()); |
554 |
> |
} |
555 |
> |
|
556 |
> |
|
557 |
> |
/** |
558 |
> |
* peekLast returns next element, or null if empty |
559 |
> |
*/ |
560 |
> |
public void testPeekLast() { |
561 |
> |
LinkedList q = populatedQueue(SIZE); |
562 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
563 |
> |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
564 |
> |
q.pollLast(); |
565 |
> |
assertTrue(q.peekLast() == null || |
566 |
> |
i != ((Integer)q.peekLast()).intValue()); |
567 |
> |
} |
568 |
> |
assertNull(q.peekLast()); |
569 |
> |
} |
570 |
> |
|
571 |
> |
public void testFirstElement() { |
572 |
> |
LinkedList q = populatedQueue(SIZE); |
573 |
> |
for (int i = 0; i < SIZE; ++i) { |
574 |
> |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
575 |
> |
q.pollFirst(); |
576 |
> |
} |
577 |
> |
try { |
578 |
> |
q.getFirst(); |
579 |
> |
shouldThrow(); |
580 |
> |
} catch (NoSuchElementException success) {} |
581 |
> |
} |
582 |
> |
|
583 |
> |
/** |
584 |
> |
* getLast returns next element, or throws NSEE if empty |
585 |
> |
*/ |
586 |
> |
public void testLastElement() { |
587 |
> |
LinkedList q = populatedQueue(SIZE); |
588 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
589 |
> |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
590 |
> |
q.pollLast(); |
591 |
> |
} |
592 |
> |
try { |
593 |
> |
q.getLast(); |
594 |
> |
shouldThrow(); |
595 |
> |
} catch (NoSuchElementException success) {} |
596 |
> |
assertNull(q.peekLast()); |
597 |
> |
} |
598 |
> |
|
599 |
> |
/** |
600 |
> |
* removeFirstOccurrence(x) removes x and returns true if present |
601 |
> |
*/ |
602 |
> |
public void testRemoveFirstOccurrence() { |
603 |
> |
LinkedList q = populatedQueue(SIZE); |
604 |
> |
for (int i = 1; i < SIZE; i+=2) { |
605 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
606 |
> |
} |
607 |
> |
for (int i = 0; i < SIZE; i+=2) { |
608 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
609 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
610 |
> |
} |
611 |
> |
assertTrue(q.isEmpty()); |
612 |
> |
} |
613 |
> |
|
614 |
> |
/** |
615 |
> |
* removeLastOccurrence(x) removes x and returns true if present |
616 |
> |
*/ |
617 |
> |
public void testRemoveLastOccurrence() { |
618 |
> |
LinkedList q = populatedQueue(SIZE); |
619 |
> |
for (int i = 1; i < SIZE; i+=2) { |
620 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
621 |
> |
} |
622 |
> |
for (int i = 0; i < SIZE; i+=2) { |
623 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
624 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
625 |
> |
} |
626 |
> |
assertTrue(q.isEmpty()); |
627 |
> |
} |
628 |
|
|
629 |
|
} |