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.List; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.concurrent.ThreadLocalRandom; |
16 |
> |
|
17 |
> |
import junit.framework.Test; |
18 |
|
|
19 |
|
public class LinkedListTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
21 |
< |
junit.textui.TestRunner.run (suite()); |
21 |
> |
main(suite(), args); |
22 |
|
} |
23 |
|
|
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(LinkedListTest.class); |
25 |
> |
class Implementation implements CollectionImplementation { |
26 |
> |
public Class<?> klazz() { return LinkedList.class; } |
27 |
> |
public List emptyCollection() { return new LinkedList(); } |
28 |
> |
public Object makeElement(int i) { return i; } |
29 |
> |
public boolean isConcurrent() { return false; } |
30 |
> |
public boolean permitsNulls() { return true; } |
31 |
> |
} |
32 |
> |
class SubListImplementation extends Implementation { |
33 |
> |
public List emptyCollection() { |
34 |
> |
List list = super.emptyCollection(); |
35 |
> |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
36 |
> |
if (rnd.nextBoolean()) |
37 |
> |
list.add(makeElement(rnd.nextInt())); |
38 |
> |
int i = rnd.nextInt(list.size() + 1); |
39 |
> |
return list.subList(i, i); |
40 |
> |
} |
41 |
> |
} |
42 |
> |
return newTestSuite( |
43 |
> |
LinkedListTest.class, |
44 |
> |
CollectionTest.testSuite(new Implementation()), |
45 |
> |
CollectionTest.testSuite(new SubListImplementation())); |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
< |
* Create a queue of given size containing consecutive |
50 |
< |
* Integers 0 ... n. |
49 |
> |
* Returns a new queue of given size containing consecutive |
50 |
> |
* Integers 0 ... n - 1. |
51 |
|
*/ |
52 |
< |
private LinkedList populatedQueue(int n) { |
53 |
< |
LinkedList q = new LinkedList(); |
52 |
> |
private static LinkedList<Integer> populatedQueue(int n) { |
53 |
> |
LinkedList<Integer> q = new LinkedList<>(); |
54 |
|
assertTrue(q.isEmpty()); |
55 |
|
for (int i = 0; i < n; ++i) |
56 |
|
assertTrue(q.offer(new Integer(i))); |
57 |
|
assertFalse(q.isEmpty()); |
58 |
|
assertEquals(n, q.size()); |
59 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
60 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
61 |
|
return q; |
62 |
|
} |
63 |
|
|
73 |
|
*/ |
74 |
|
public void testConstructor3() { |
75 |
|
try { |
76 |
< |
LinkedList q = new LinkedList((Collection)null); |
76 |
> |
new LinkedList((Collection)null); |
77 |
|
shouldThrow(); |
78 |
|
} catch (NullPointerException success) {} |
79 |
|
} |
110 |
|
public void testSize() { |
111 |
|
LinkedList q = populatedQueue(SIZE); |
112 |
|
for (int i = 0; i < SIZE; ++i) { |
113 |
< |
assertEquals(SIZE-i, q.size()); |
113 |
> |
assertEquals(SIZE - i, q.size()); |
114 |
|
q.remove(); |
115 |
|
} |
116 |
|
for (int i = 0; i < SIZE; ++i) { |
125 |
|
public void testOfferNull() { |
126 |
|
LinkedList q = new LinkedList(); |
127 |
|
q.offer(null); |
128 |
+ |
assertNull(q.get(0)); |
129 |
+ |
assertTrue(q.contains(null)); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
153 |
|
* addAll(null) throws NPE |
154 |
|
*/ |
155 |
|
public void testAddAll1() { |
156 |
+ |
LinkedList q = new LinkedList(); |
157 |
|
try { |
127 |
– |
LinkedList q = new LinkedList(); |
158 |
|
q.addAll(null); |
159 |
|
shouldThrow(); |
160 |
|
} catch (NullPointerException success) {} |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* poll succeeds unless empty |
207 |
> |
* poll succeeds unless empty |
208 |
|
*/ |
209 |
|
public void testPoll() { |
210 |
|
LinkedList q = populatedQueue(SIZE); |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
< |
* peek returns next element, or null if empty |
218 |
> |
* peek returns next element, or null if empty |
219 |
|
*/ |
220 |
|
public void testPeek() { |
221 |
|
LinkedList q = populatedQueue(SIZE); |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* remove removes next element, or throws NSEE if empty |
247 |
> |
* remove removes next element, or throws NSEE if empty |
248 |
|
*/ |
249 |
|
public void testRemove() { |
250 |
|
LinkedList q = populatedQueue(SIZE); |
262 |
|
*/ |
263 |
|
public void testRemoveElement() { |
264 |
|
LinkedList q = populatedQueue(SIZE); |
265 |
< |
for (int i = 1; i < SIZE; i+=2) { |
266 |
< |
assertTrue(q.remove(new Integer(i))); |
267 |
< |
} |
268 |
< |
for (int i = 0; i < SIZE; i+=2) { |
269 |
< |
assertTrue(q.remove(new Integer(i))); |
270 |
< |
assertFalse(q.remove(new Integer(i+1))); |
265 |
> |
for (int i = 1; i < SIZE; i += 2) { |
266 |
> |
assertTrue(q.contains(i)); |
267 |
> |
assertTrue(q.remove((Integer)i)); |
268 |
> |
assertFalse(q.contains(i)); |
269 |
> |
assertTrue(q.contains(i - 1)); |
270 |
> |
} |
271 |
> |
for (int i = 0; i < SIZE; i += 2) { |
272 |
> |
assertTrue(q.contains(i)); |
273 |
> |
assertTrue(q.remove((Integer)i)); |
274 |
> |
assertFalse(q.contains(i)); |
275 |
> |
assertFalse(q.remove((Integer)(i + 1))); |
276 |
> |
assertFalse(q.contains(i + 1)); |
277 |
|
} |
278 |
|
assertTrue(q.isEmpty()); |
279 |
|
} |
332 |
|
assertTrue(changed); |
333 |
|
|
334 |
|
assertTrue(q.containsAll(p)); |
335 |
< |
assertEquals(SIZE-i, q.size()); |
335 |
> |
assertEquals(SIZE - i, q.size()); |
336 |
|
p.remove(); |
337 |
|
} |
338 |
|
} |
345 |
|
LinkedList q = populatedQueue(SIZE); |
346 |
|
LinkedList p = populatedQueue(i); |
347 |
|
assertTrue(q.removeAll(p)); |
348 |
< |
assertEquals(SIZE-i, q.size()); |
348 |
> |
assertEquals(SIZE - i, q.size()); |
349 |
|
for (int j = 0; j < i; ++j) { |
350 |
< |
Integer I = (Integer)(p.remove()); |
351 |
< |
assertFalse(q.contains(I)); |
350 |
> |
Integer x = (Integer)(p.remove()); |
351 |
> |
assertFalse(q.contains(x)); |
352 |
|
} |
353 |
|
} |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
< |
* toArray contains all elements |
357 |
> |
* toArray contains all elements in FIFO order |
358 |
|
*/ |
359 |
|
public void testToArray() { |
360 |
|
LinkedList q = populatedQueue(SIZE); |
361 |
< |
Object[] o = q.toArray(); |
362 |
< |
Arrays.sort(o); |
363 |
< |
for (int i = 0; i < o.length; i++) |
364 |
< |
assertEquals(o[i], q.poll()); |
361 |
> |
Object[] a = q.toArray(); |
362 |
> |
assertSame(Object[].class, a.getClass()); |
363 |
> |
for (Object o : a) |
364 |
> |
assertSame(o, q.poll()); |
365 |
> |
assertTrue(q.isEmpty()); |
366 |
|
} |
367 |
|
|
368 |
|
/** |
369 |
< |
* toArray(a) contains all elements |
369 |
> |
* toArray(a) contains all elements in FIFO order |
370 |
|
*/ |
371 |
|
public void testToArray2() { |
372 |
< |
LinkedList q = populatedQueue(SIZE); |
372 |
> |
LinkedList<Integer> q = populatedQueue(SIZE); |
373 |
|
Integer[] ints = new Integer[SIZE]; |
374 |
< |
ints = (Integer[])q.toArray(ints); |
375 |
< |
Arrays.sort(ints); |
376 |
< |
for (int i = 0; i < ints.length; i++) |
377 |
< |
assertEquals(ints[i], q.poll()); |
374 |
> |
Integer[] array = q.toArray(ints); |
375 |
> |
assertSame(ints, array); |
376 |
> |
for (Integer o : ints) |
377 |
> |
assertSame(o, q.poll()); |
378 |
> |
assertTrue(q.isEmpty()); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* toArray(null) throws NPE |
382 |
> |
* toArray(null) throws NullPointerException |
383 |
|
*/ |
384 |
< |
public void testToArray_BadArg() { |
384 |
> |
public void testToArray_NullArg() { |
385 |
|
LinkedList l = new LinkedList(); |
386 |
|
l.add(new Object()); |
387 |
|
try { |
388 |
< |
Object o[] = l.toArray(null); |
388 |
> |
l.toArray(null); |
389 |
|
shouldThrow(); |
390 |
|
} catch (NullPointerException success) {} |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
< |
* toArray with incompatible array type throws CCE |
394 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
395 |
|
*/ |
396 |
|
public void testToArray1_BadArg() { |
397 |
|
LinkedList l = new LinkedList(); |
398 |
|
l.add(new Integer(5)); |
399 |
|
try { |
400 |
< |
Object o[] = l.toArray(new String[10]); |
400 |
> |
l.toArray(new String[10]); |
401 |
|
shouldThrow(); |
402 |
|
} catch (ArrayStoreException success) {} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* iterator iterates through all elements |
406 |
> |
* iterator iterates through all elements |
407 |
|
*/ |
408 |
|
public void testIterator() { |
409 |
|
LinkedList q = populatedQueue(SIZE); |
372 |
– |
int i = 0; |
410 |
|
Iterator it = q.iterator(); |
411 |
< |
while (it.hasNext()) { |
411 |
> |
int i; |
412 |
> |
for (i = 0; it.hasNext(); i++) |
413 |
|
assertTrue(q.contains(it.next())); |
376 |
– |
++i; |
377 |
– |
} |
414 |
|
assertEquals(i, SIZE); |
415 |
+ |
assertIteratorExhausted(it); |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
/** |
419 |
+ |
* iterator of empty collection has no elements |
420 |
+ |
*/ |
421 |
+ |
public void testEmptyIterator() { |
422 |
+ |
assertIteratorExhausted(new LinkedList().iterator()); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
< |
* iterator ordering is FIFO |
426 |
> |
* iterator ordering is FIFO |
427 |
|
*/ |
428 |
|
public void testIteratorOrdering() { |
429 |
|
final LinkedList q = new LinkedList(); |
441 |
|
/** |
442 |
|
* iterator.remove removes current element |
443 |
|
*/ |
444 |
< |
public void testIteratorRemove () { |
444 |
> |
public void testIteratorRemove() { |
445 |
|
final LinkedList q = new LinkedList(); |
446 |
|
q.add(new Integer(1)); |
447 |
|
q.add(new Integer(2)); |
448 |
|
q.add(new Integer(3)); |
449 |
|
Iterator it = q.iterator(); |
450 |
< |
assertEquals(it.next(), 1); |
450 |
> |
assertEquals(1, it.next()); |
451 |
|
it.remove(); |
452 |
|
it = q.iterator(); |
453 |
< |
assertEquals(it.next(), 2); |
454 |
< |
assertEquals(it.next(), 3); |
453 |
> |
assertEquals(2, it.next()); |
454 |
> |
assertEquals(3, it.next()); |
455 |
|
assertFalse(it.hasNext()); |
456 |
|
} |
457 |
|
|
458 |
|
/** |
459 |
< |
* Descending iterator iterates through all elements |
459 |
> |
* Descending iterator iterates through all elements |
460 |
|
*/ |
461 |
|
public void testDescendingIterator() { |
462 |
|
LinkedList q = populatedQueue(SIZE); |
475 |
|
} |
476 |
|
|
477 |
|
/** |
478 |
< |
* Descending iterator ordering is reverse FIFO |
478 |
> |
* Descending iterator ordering is reverse FIFO |
479 |
|
*/ |
480 |
|
public void testDescendingIteratorOrdering() { |
481 |
|
final LinkedList q = new LinkedList(); |
493 |
|
/** |
494 |
|
* descendingIterator.remove removes current element |
495 |
|
*/ |
496 |
< |
public void testDescendingIteratorRemove () { |
496 |
> |
public void testDescendingIteratorRemove() { |
497 |
|
final LinkedList q = new LinkedList(); |
498 |
|
q.add(three); |
499 |
|
q.add(two); |
507 |
|
assertFalse(it.hasNext()); |
508 |
|
} |
509 |
|
|
466 |
– |
|
510 |
|
/** |
511 |
|
* toString contains toStrings of elements |
512 |
|
*/ |
514 |
|
LinkedList q = populatedQueue(SIZE); |
515 |
|
String s = q.toString(); |
516 |
|
for (int i = 0; i < SIZE; ++i) { |
517 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
517 |
> |
assertTrue(s.contains(String.valueOf(i))); |
518 |
|
} |
519 |
|
} |
520 |
|
|
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
< |
* pop removes next element, or throws NSEE if empty |
540 |
> |
* pop removes next element, or throws NSEE if empty |
541 |
|
*/ |
542 |
|
public void testPop() { |
543 |
|
LinkedList q = populatedQueue(SIZE); |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
< |
* pollLast succeeds unless empty |
572 |
> |
* pollLast succeeds unless empty |
573 |
|
*/ |
574 |
|
public void testPollLast() { |
575 |
|
LinkedList q = populatedQueue(SIZE); |
576 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
576 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
577 |
|
assertEquals(i, q.pollLast()); |
578 |
|
} |
579 |
|
assertNull(q.pollLast()); |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
< |
* peekFirst returns next element, or null if empty |
583 |
> |
* peekFirst returns next element, or null if empty |
584 |
|
*/ |
585 |
|
public void testPeekFirst() { |
586 |
|
LinkedList q = populatedQueue(SIZE); |
593 |
|
assertNull(q.peekFirst()); |
594 |
|
} |
595 |
|
|
553 |
– |
|
596 |
|
/** |
597 |
< |
* peekLast returns next element, or null if empty |
597 |
> |
* peekLast returns next element, or null if empty |
598 |
|
*/ |
599 |
|
public void testPeekLast() { |
600 |
|
LinkedList q = populatedQueue(SIZE); |
601 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
601 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
602 |
|
assertEquals(i, q.peekLast()); |
603 |
|
assertEquals(i, q.pollLast()); |
604 |
|
assertTrue(q.peekLast() == null || |
620 |
|
} |
621 |
|
|
622 |
|
/** |
623 |
< |
* getLast returns next element, or throws NSEE if empty |
623 |
> |
* getLast returns next element, or throws NSEE if empty |
624 |
|
*/ |
625 |
|
public void testLastElement() { |
626 |
|
LinkedList q = populatedQueue(SIZE); |
627 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
627 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
628 |
|
assertEquals(i, q.getLast()); |
629 |
|
assertEquals(i, q.pollLast()); |
630 |
|
} |
640 |
|
*/ |
641 |
|
public void testRemoveFirstOccurrence() { |
642 |
|
LinkedList q = populatedQueue(SIZE); |
643 |
< |
for (int i = 1; i < SIZE; i+=2) { |
643 |
> |
for (int i = 1; i < SIZE; i += 2) { |
644 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
645 |
|
} |
646 |
< |
for (int i = 0; i < SIZE; i+=2) { |
646 |
> |
for (int i = 0; i < SIZE; i += 2) { |
647 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
648 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
648 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
649 |
|
} |
650 |
|
assertTrue(q.isEmpty()); |
651 |
|
} |
655 |
|
*/ |
656 |
|
public void testRemoveLastOccurrence() { |
657 |
|
LinkedList q = populatedQueue(SIZE); |
658 |
< |
for (int i = 1; i < SIZE; i+=2) { |
658 |
> |
for (int i = 1; i < SIZE; i += 2) { |
659 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
660 |
|
} |
661 |
< |
for (int i = 0; i < SIZE; i+=2) { |
661 |
> |
for (int i = 0; i < SIZE; i += 2) { |
662 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
663 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
663 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
664 |
|
} |
665 |
|
assertTrue(q.isEmpty()); |
666 |
|
} |