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/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.LinkedList; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
|
15 |
> |
import junit.framework.Test; |
16 |
> |
import junit.framework.TestSuite; |
17 |
|
|
18 |
|
public class LinkedListTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run (suite()); |
20 |
> |
main(suite(), args); |
21 |
|
} |
22 |
|
|
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(LinkedListTest.class); |
24 |
> |
class Implementation implements CollectionImplementation { |
25 |
> |
public Class<?> klazz() { return LinkedList.class; } |
26 |
> |
public Collection emptyCollection() { return new LinkedList(); } |
27 |
> |
public Object makeElement(int i) { return i; } |
28 |
> |
public boolean isConcurrent() { return false; } |
29 |
> |
public boolean permitsNulls() { return true; } |
30 |
> |
} |
31 |
> |
return newTestSuite(LinkedListTest.class, |
32 |
> |
CollectionTest.testSuite(new Implementation())); |
33 |
|
} |
34 |
|
|
35 |
|
/** |
36 |
< |
* Create a queue of given size containing consecutive |
37 |
< |
* Integers 0 ... n. |
36 |
> |
* Returns a new queue of given size containing consecutive |
37 |
> |
* Integers 0 ... n - 1. |
38 |
|
*/ |
39 |
< |
private LinkedList populatedQueue(int n) { |
40 |
< |
LinkedList q = new LinkedList(); |
39 |
> |
private LinkedList<Integer> populatedQueue(int n) { |
40 |
> |
LinkedList<Integer> q = new LinkedList<Integer>(); |
41 |
|
assertTrue(q.isEmpty()); |
42 |
|
for (int i = 0; i < n; ++i) |
43 |
|
assertTrue(q.offer(new Integer(i))); |
44 |
|
assertFalse(q.isEmpty()); |
45 |
|
assertEquals(n, q.size()); |
46 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
47 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
48 |
|
return q; |
49 |
|
} |
50 |
|
|
60 |
|
*/ |
61 |
|
public void testConstructor3() { |
62 |
|
try { |
63 |
< |
LinkedList q = new LinkedList((Collection)null); |
63 |
> |
new LinkedList((Collection)null); |
64 |
|
shouldThrow(); |
65 |
|
} catch (NullPointerException success) {} |
66 |
|
} |
97 |
|
public void testSize() { |
98 |
|
LinkedList q = populatedQueue(SIZE); |
99 |
|
for (int i = 0; i < SIZE; ++i) { |
100 |
< |
assertEquals(SIZE-i, q.size()); |
100 |
> |
assertEquals(SIZE - i, q.size()); |
101 |
|
q.remove(); |
102 |
|
} |
103 |
|
for (int i = 0; i < SIZE; ++i) { |
112 |
|
public void testOfferNull() { |
113 |
|
LinkedList q = new LinkedList(); |
114 |
|
q.offer(null); |
115 |
+ |
assertNull(q.get(0)); |
116 |
+ |
assertTrue(q.contains(null)); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
140 |
|
* addAll(null) throws NPE |
141 |
|
*/ |
142 |
|
public void testAddAll1() { |
143 |
+ |
LinkedList q = new LinkedList(); |
144 |
|
try { |
127 |
– |
LinkedList q = new LinkedList(); |
145 |
|
q.addAll(null); |
146 |
|
shouldThrow(); |
147 |
|
} catch (NullPointerException success) {} |
191 |
|
} |
192 |
|
|
193 |
|
/** |
194 |
< |
* poll succeeds unless empty |
194 |
> |
* poll succeeds unless empty |
195 |
|
*/ |
196 |
|
public void testPoll() { |
197 |
|
LinkedList q = populatedQueue(SIZE); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* peek returns next element, or null if empty |
205 |
> |
* peek returns next element, or null if empty |
206 |
|
*/ |
207 |
|
public void testPeek() { |
208 |
|
LinkedList q = populatedQueue(SIZE); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
< |
* remove removes next element, or throws NSEE if empty |
234 |
> |
* remove removes next element, or throws NSEE if empty |
235 |
|
*/ |
236 |
|
public void testRemove() { |
237 |
|
LinkedList q = populatedQueue(SIZE); |
249 |
|
*/ |
250 |
|
public void testRemoveElement() { |
251 |
|
LinkedList q = populatedQueue(SIZE); |
252 |
< |
for (int i = 1; i < SIZE; i+=2) { |
253 |
< |
assertTrue(q.remove(new Integer(i))); |
254 |
< |
} |
255 |
< |
for (int i = 0; i < SIZE; i+=2) { |
256 |
< |
assertTrue(q.remove(new Integer(i))); |
257 |
< |
assertFalse(q.remove(new Integer(i+1))); |
252 |
> |
for (int i = 1; i < SIZE; i += 2) { |
253 |
> |
assertTrue(q.contains(i)); |
254 |
> |
assertTrue(q.remove((Integer)i)); |
255 |
> |
assertFalse(q.contains(i)); |
256 |
> |
assertTrue(q.contains(i - 1)); |
257 |
> |
} |
258 |
> |
for (int i = 0; i < SIZE; i += 2) { |
259 |
> |
assertTrue(q.contains(i)); |
260 |
> |
assertTrue(q.remove((Integer)i)); |
261 |
> |
assertFalse(q.contains(i)); |
262 |
> |
assertFalse(q.remove((Integer)(i + 1))); |
263 |
> |
assertFalse(q.contains(i + 1)); |
264 |
|
} |
265 |
|
assertTrue(q.isEmpty()); |
266 |
|
} |
319 |
|
assertTrue(changed); |
320 |
|
|
321 |
|
assertTrue(q.containsAll(p)); |
322 |
< |
assertEquals(SIZE-i, q.size()); |
322 |
> |
assertEquals(SIZE - i, q.size()); |
323 |
|
p.remove(); |
324 |
|
} |
325 |
|
} |
332 |
|
LinkedList q = populatedQueue(SIZE); |
333 |
|
LinkedList p = populatedQueue(i); |
334 |
|
assertTrue(q.removeAll(p)); |
335 |
< |
assertEquals(SIZE-i, q.size()); |
335 |
> |
assertEquals(SIZE - i, q.size()); |
336 |
|
for (int j = 0; j < i; ++j) { |
337 |
< |
Integer I = (Integer)(p.remove()); |
338 |
< |
assertFalse(q.contains(I)); |
337 |
> |
Integer x = (Integer)(p.remove()); |
338 |
> |
assertFalse(q.contains(x)); |
339 |
|
} |
340 |
|
} |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
< |
* toArray contains all elements |
344 |
> |
* toArray contains all elements in FIFO order |
345 |
|
*/ |
346 |
|
public void testToArray() { |
347 |
|
LinkedList q = populatedQueue(SIZE); |
348 |
|
Object[] o = q.toArray(); |
326 |
– |
Arrays.sort(o); |
349 |
|
for (int i = 0; i < o.length; i++) |
350 |
< |
assertEquals(o[i], q.poll()); |
350 |
> |
assertSame(o[i], q.poll()); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
354 |
< |
* toArray(a) contains all elements |
354 |
> |
* toArray(a) contains all elements in FIFO order |
355 |
|
*/ |
356 |
|
public void testToArray2() { |
357 |
< |
LinkedList q = populatedQueue(SIZE); |
357 |
> |
LinkedList<Integer> q = populatedQueue(SIZE); |
358 |
|
Integer[] ints = new Integer[SIZE]; |
359 |
< |
ints = (Integer[])q.toArray(ints); |
360 |
< |
Arrays.sort(ints); |
359 |
> |
Integer[] array = q.toArray(ints); |
360 |
> |
assertSame(ints, array); |
361 |
|
for (int i = 0; i < ints.length; i++) |
362 |
< |
assertEquals(ints[i], q.poll()); |
362 |
> |
assertSame(ints[i], q.poll()); |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
< |
* toArray(null) throws NPE |
366 |
> |
* toArray(null) throws NullPointerException |
367 |
|
*/ |
368 |
< |
public void testToArray_BadArg() { |
368 |
> |
public void testToArray_NullArg() { |
369 |
|
LinkedList l = new LinkedList(); |
370 |
|
l.add(new Object()); |
371 |
|
try { |
372 |
< |
Object o[] = l.toArray(null); |
372 |
> |
l.toArray(null); |
373 |
|
shouldThrow(); |
374 |
|
} catch (NullPointerException success) {} |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* toArray with incompatable aray type throws CCE |
378 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
379 |
|
*/ |
380 |
|
public void testToArray1_BadArg() { |
381 |
|
LinkedList l = new LinkedList(); |
382 |
|
l.add(new Integer(5)); |
383 |
|
try { |
384 |
< |
Object o[] = l.toArray(new String[10]); |
384 |
> |
l.toArray(new String[10]); |
385 |
|
shouldThrow(); |
386 |
|
} catch (ArrayStoreException success) {} |
387 |
|
} |
388 |
|
|
389 |
|
/** |
390 |
< |
* iterator iterates through all elements |
390 |
> |
* iterator iterates through all elements |
391 |
|
*/ |
392 |
|
public void testIterator() { |
393 |
|
LinkedList q = populatedQueue(SIZE); |
372 |
– |
int i = 0; |
394 |
|
Iterator it = q.iterator(); |
395 |
< |
while (it.hasNext()) { |
395 |
> |
int i; |
396 |
> |
for (i = 0; it.hasNext(); i++) |
397 |
|
assertTrue(q.contains(it.next())); |
376 |
– |
++i; |
377 |
– |
} |
398 |
|
assertEquals(i, SIZE); |
399 |
+ |
assertIteratorExhausted(it); |
400 |
+ |
} |
401 |
+ |
|
402 |
+ |
/** |
403 |
+ |
* iterator of empty collection has no elements |
404 |
+ |
*/ |
405 |
+ |
public void testEmptyIterator() { |
406 |
+ |
assertIteratorExhausted(new LinkedList().iterator()); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* iterator ordering is FIFO |
410 |
> |
* iterator ordering is FIFO |
411 |
|
*/ |
412 |
|
public void testIteratorOrdering() { |
413 |
|
final LinkedList q = new LinkedList(); |
425 |
|
/** |
426 |
|
* iterator.remove removes current element |
427 |
|
*/ |
428 |
< |
public void testIteratorRemove () { |
428 |
> |
public void testIteratorRemove() { |
429 |
|
final LinkedList q = new LinkedList(); |
430 |
|
q.add(new Integer(1)); |
431 |
|
q.add(new Integer(2)); |
432 |
|
q.add(new Integer(3)); |
433 |
|
Iterator it = q.iterator(); |
434 |
< |
assertEquals(it.next(), 1); |
434 |
> |
assertEquals(1, it.next()); |
435 |
|
it.remove(); |
436 |
|
it = q.iterator(); |
437 |
< |
assertEquals(it.next(), 2); |
438 |
< |
assertEquals(it.next(), 3); |
437 |
> |
assertEquals(2, it.next()); |
438 |
> |
assertEquals(3, it.next()); |
439 |
|
assertFalse(it.hasNext()); |
440 |
|
} |
441 |
|
|
442 |
|
/** |
443 |
< |
* Descending iterator iterates through all elements |
443 |
> |
* Descending iterator iterates through all elements |
444 |
|
*/ |
445 |
|
public void testDescendingIterator() { |
446 |
|
LinkedList q = populatedQueue(SIZE); |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
< |
* Descending iterator ordering is reverse FIFO |
462 |
> |
* Descending iterator ordering is reverse FIFO |
463 |
|
*/ |
464 |
|
public void testDescendingIteratorOrdering() { |
465 |
|
final LinkedList q = new LinkedList(); |
477 |
|
/** |
478 |
|
* descendingIterator.remove removes current element |
479 |
|
*/ |
480 |
< |
public void testDescendingIteratorRemove () { |
480 |
> |
public void testDescendingIteratorRemove() { |
481 |
|
final LinkedList q = new LinkedList(); |
482 |
< |
q.add(new Integer(3)); |
483 |
< |
q.add(new Integer(2)); |
484 |
< |
q.add(new Integer(1)); |
482 |
> |
q.add(three); |
483 |
> |
q.add(two); |
484 |
> |
q.add(one); |
485 |
|
Iterator it = q.descendingIterator(); |
486 |
|
it.next(); |
487 |
|
it.remove(); |
488 |
|
it = q.descendingIterator(); |
489 |
< |
assertEquals(it.next(), 2); |
490 |
< |
assertEquals(it.next(), 3); |
489 |
> |
assertSame(it.next(), two); |
490 |
> |
assertSame(it.next(), three); |
491 |
|
assertFalse(it.hasNext()); |
492 |
|
} |
493 |
|
|
466 |
– |
|
494 |
|
/** |
495 |
|
* toString contains toStrings of elements |
496 |
|
*/ |
498 |
|
LinkedList q = populatedQueue(SIZE); |
499 |
|
String s = q.toString(); |
500 |
|
for (int i = 0; i < SIZE; ++i) { |
501 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
501 |
> |
assertTrue(s.contains(String.valueOf(i))); |
502 |
|
} |
503 |
|
} |
504 |
|
|
508 |
|
public void testAddFirst() { |
509 |
|
LinkedList q = populatedQueue(3); |
510 |
|
q.addFirst(four); |
511 |
< |
assertEquals(four,q.peek()); |
511 |
> |
assertSame(four, q.peek()); |
512 |
|
} |
513 |
|
|
514 |
|
/** |
516 |
|
*/ |
517 |
|
public void testPush() { |
518 |
|
LinkedList q = populatedQueue(3); |
492 |
– |
q.pollLast(); |
519 |
|
q.push(four); |
520 |
< |
assertEquals(four,q.peekFirst()); |
520 |
> |
assertSame(four, q.peekFirst()); |
521 |
|
} |
522 |
|
|
523 |
|
/** |
524 |
< |
* pop removes next element, or throws NSEE if empty |
524 |
> |
* pop removes next element, or throws NSEE if empty |
525 |
|
*/ |
526 |
|
public void testPop() { |
527 |
|
LinkedList q = populatedQueue(SIZE); |
553 |
|
} |
554 |
|
|
555 |
|
/** |
556 |
< |
* pollLast succeeds unless empty |
556 |
> |
* pollLast succeeds unless empty |
557 |
|
*/ |
558 |
|
public void testPollLast() { |
559 |
|
LinkedList q = populatedQueue(SIZE); |
560 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
560 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
561 |
|
assertEquals(i, q.pollLast()); |
562 |
|
} |
563 |
|
assertNull(q.pollLast()); |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
< |
* peekFirst returns next element, or null if empty |
567 |
> |
* peekFirst returns next element, or null if empty |
568 |
|
*/ |
569 |
|
public void testPeekFirst() { |
570 |
|
LinkedList q = populatedQueue(SIZE); |
577 |
|
assertNull(q.peekFirst()); |
578 |
|
} |
579 |
|
|
554 |
– |
|
580 |
|
/** |
581 |
< |
* peekLast returns next element, or null if empty |
581 |
> |
* peekLast returns next element, or null if empty |
582 |
|
*/ |
583 |
|
public void testPeekLast() { |
584 |
|
LinkedList q = populatedQueue(SIZE); |
585 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
585 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
586 |
|
assertEquals(i, q.peekLast()); |
587 |
|
assertEquals(i, q.pollLast()); |
588 |
|
assertTrue(q.peekLast() == null || |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
< |
* getLast returns next element, or throws NSEE if empty |
607 |
> |
* getLast returns next element, or throws NSEE if empty |
608 |
|
*/ |
609 |
|
public void testLastElement() { |
610 |
|
LinkedList q = populatedQueue(SIZE); |
611 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
611 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
612 |
|
assertEquals(i, q.getLast()); |
613 |
|
assertEquals(i, q.pollLast()); |
614 |
|
} |
624 |
|
*/ |
625 |
|
public void testRemoveFirstOccurrence() { |
626 |
|
LinkedList q = populatedQueue(SIZE); |
627 |
< |
for (int i = 1; i < SIZE; i+=2) { |
627 |
> |
for (int i = 1; i < SIZE; i += 2) { |
628 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
629 |
|
} |
630 |
< |
for (int i = 0; i < SIZE; i+=2) { |
630 |
> |
for (int i = 0; i < SIZE; i += 2) { |
631 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
632 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
632 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
633 |
|
} |
634 |
|
assertTrue(q.isEmpty()); |
635 |
|
} |
639 |
|
*/ |
640 |
|
public void testRemoveLastOccurrence() { |
641 |
|
LinkedList q = populatedQueue(SIZE); |
642 |
< |
for (int i = 1; i < SIZE; i+=2) { |
642 |
> |
for (int i = 1; i < SIZE; i += 2) { |
643 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
644 |
|
} |
645 |
< |
for (int i = 0; i < SIZE; i+=2) { |
645 |
> |
for (int i = 0; i < SIZE; i += 2) { |
646 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
647 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
647 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
648 |
|
} |
649 |
|
assertTrue(q.isEmpty()); |
650 |
|
} |