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 |
> |
class SubListImplementation extends Implementation { |
32 |
> |
public Collection emptyCollection() { |
33 |
> |
return new LinkedList().subList(0, 0); |
34 |
> |
} |
35 |
> |
} |
36 |
> |
return newTestSuite( |
37 |
> |
LinkedListTest.class, |
38 |
> |
CollectionTest.testSuite(new Implementation()), |
39 |
> |
CollectionTest.testSuite(new SubListImplementation())); |
40 |
|
} |
41 |
|
|
42 |
|
/** |
43 |
< |
* Create a queue of given size containing consecutive |
44 |
< |
* Integers 0 ... n. |
43 |
> |
* Returns a new queue of given size containing consecutive |
44 |
> |
* Integers 0 ... n - 1. |
45 |
|
*/ |
46 |
|
private LinkedList<Integer> populatedQueue(int n) { |
47 |
< |
LinkedList<Integer> q = new LinkedList<Integer>(); |
47 |
> |
LinkedList<Integer> q = new LinkedList<>(); |
48 |
|
assertTrue(q.isEmpty()); |
49 |
|
for (int i = 0; i < n; ++i) |
50 |
|
assertTrue(q.offer(new Integer(i))); |
51 |
|
assertFalse(q.isEmpty()); |
52 |
|
assertEquals(n, q.size()); |
53 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
54 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
55 |
|
return q; |
56 |
|
} |
57 |
|
|
67 |
|
*/ |
68 |
|
public void testConstructor3() { |
69 |
|
try { |
70 |
< |
LinkedList q = new LinkedList((Collection)null); |
70 |
> |
new LinkedList((Collection)null); |
71 |
|
shouldThrow(); |
72 |
|
} catch (NullPointerException success) {} |
73 |
|
} |
104 |
|
public void testSize() { |
105 |
|
LinkedList q = populatedQueue(SIZE); |
106 |
|
for (int i = 0; i < SIZE; ++i) { |
107 |
< |
assertEquals(SIZE-i, q.size()); |
107 |
> |
assertEquals(SIZE - i, q.size()); |
108 |
|
q.remove(); |
109 |
|
} |
110 |
|
for (int i = 0; i < SIZE; ++i) { |
119 |
|
public void testOfferNull() { |
120 |
|
LinkedList q = new LinkedList(); |
121 |
|
q.offer(null); |
122 |
+ |
assertNull(q.get(0)); |
123 |
+ |
assertTrue(q.contains(null)); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
147 |
|
* addAll(null) throws NPE |
148 |
|
*/ |
149 |
|
public void testAddAll1() { |
150 |
+ |
LinkedList q = new LinkedList(); |
151 |
|
try { |
127 |
– |
LinkedList q = new LinkedList(); |
152 |
|
q.addAll(null); |
153 |
|
shouldThrow(); |
154 |
|
} catch (NullPointerException success) {} |
256 |
|
*/ |
257 |
|
public void testRemoveElement() { |
258 |
|
LinkedList q = populatedQueue(SIZE); |
259 |
< |
for (int i = 1; i < SIZE; i+=2) { |
259 |
> |
for (int i = 1; i < SIZE; i += 2) { |
260 |
|
assertTrue(q.contains(i)); |
261 |
|
assertTrue(q.remove((Integer)i)); |
262 |
|
assertFalse(q.contains(i)); |
263 |
< |
assertTrue(q.contains(i-1)); |
263 |
> |
assertTrue(q.contains(i - 1)); |
264 |
|
} |
265 |
< |
for (int i = 0; i < SIZE; i+=2) { |
265 |
> |
for (int i = 0; i < SIZE; i += 2) { |
266 |
|
assertTrue(q.contains(i)); |
267 |
|
assertTrue(q.remove((Integer)i)); |
268 |
|
assertFalse(q.contains(i)); |
269 |
< |
assertFalse(q.remove((Integer)(i+1))); |
270 |
< |
assertFalse(q.contains(i+1)); |
269 |
> |
assertFalse(q.remove((Integer)(i + 1))); |
270 |
> |
assertFalse(q.contains(i + 1)); |
271 |
|
} |
272 |
|
assertTrue(q.isEmpty()); |
273 |
|
} |
326 |
|
assertTrue(changed); |
327 |
|
|
328 |
|
assertTrue(q.containsAll(p)); |
329 |
< |
assertEquals(SIZE-i, q.size()); |
329 |
> |
assertEquals(SIZE - i, q.size()); |
330 |
|
p.remove(); |
331 |
|
} |
332 |
|
} |
339 |
|
LinkedList q = populatedQueue(SIZE); |
340 |
|
LinkedList p = populatedQueue(i); |
341 |
|
assertTrue(q.removeAll(p)); |
342 |
< |
assertEquals(SIZE-i, q.size()); |
342 |
> |
assertEquals(SIZE - i, q.size()); |
343 |
|
for (int j = 0; j < i; ++j) { |
344 |
< |
Integer I = (Integer)(p.remove()); |
345 |
< |
assertFalse(q.contains(I)); |
344 |
> |
Integer x = (Integer)(p.remove()); |
345 |
> |
assertFalse(q.contains(x)); |
346 |
|
} |
347 |
|
} |
348 |
|
} |
398 |
|
*/ |
399 |
|
public void testIterator() { |
400 |
|
LinkedList q = populatedQueue(SIZE); |
377 |
– |
int i = 0; |
401 |
|
Iterator it = q.iterator(); |
402 |
< |
while (it.hasNext()) { |
402 |
> |
int i; |
403 |
> |
for (i = 0; it.hasNext(); i++) |
404 |
|
assertTrue(q.contains(it.next())); |
381 |
– |
++i; |
382 |
– |
} |
405 |
|
assertEquals(i, SIZE); |
406 |
+ |
assertIteratorExhausted(it); |
407 |
+ |
} |
408 |
+ |
|
409 |
+ |
/** |
410 |
+ |
* iterator of empty collection has no elements |
411 |
+ |
*/ |
412 |
+ |
public void testEmptyIterator() { |
413 |
+ |
assertIteratorExhausted(new LinkedList().iterator()); |
414 |
|
} |
415 |
|
|
416 |
|
/** |
438 |
|
q.add(new Integer(2)); |
439 |
|
q.add(new Integer(3)); |
440 |
|
Iterator it = q.iterator(); |
441 |
< |
assertEquals(it.next(), 1); |
441 |
> |
assertEquals(1, it.next()); |
442 |
|
it.remove(); |
443 |
|
it = q.iterator(); |
444 |
< |
assertEquals(it.next(), 2); |
445 |
< |
assertEquals(it.next(), 3); |
444 |
> |
assertEquals(2, it.next()); |
445 |
> |
assertEquals(3, it.next()); |
446 |
|
assertFalse(it.hasNext()); |
447 |
|
} |
448 |
|
|
498 |
|
assertFalse(it.hasNext()); |
499 |
|
} |
500 |
|
|
471 |
– |
|
501 |
|
/** |
502 |
|
* toString contains toStrings of elements |
503 |
|
*/ |
505 |
|
LinkedList q = populatedQueue(SIZE); |
506 |
|
String s = q.toString(); |
507 |
|
for (int i = 0; i < SIZE; ++i) { |
508 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
508 |
> |
assertTrue(s.contains(String.valueOf(i))); |
509 |
|
} |
510 |
|
} |
511 |
|
|
564 |
|
*/ |
565 |
|
public void testPollLast() { |
566 |
|
LinkedList q = populatedQueue(SIZE); |
567 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
567 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
568 |
|
assertEquals(i, q.pollLast()); |
569 |
|
} |
570 |
|
assertNull(q.pollLast()); |
584 |
|
assertNull(q.peekFirst()); |
585 |
|
} |
586 |
|
|
558 |
– |
|
587 |
|
/** |
588 |
|
* peekLast returns next element, or null if empty |
589 |
|
*/ |
590 |
|
public void testPeekLast() { |
591 |
|
LinkedList q = populatedQueue(SIZE); |
592 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
592 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
593 |
|
assertEquals(i, q.peekLast()); |
594 |
|
assertEquals(i, q.pollLast()); |
595 |
|
assertTrue(q.peekLast() == null || |
615 |
|
*/ |
616 |
|
public void testLastElement() { |
617 |
|
LinkedList q = populatedQueue(SIZE); |
618 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
618 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
619 |
|
assertEquals(i, q.getLast()); |
620 |
|
assertEquals(i, q.pollLast()); |
621 |
|
} |
631 |
|
*/ |
632 |
|
public void testRemoveFirstOccurrence() { |
633 |
|
LinkedList q = populatedQueue(SIZE); |
634 |
< |
for (int i = 1; i < SIZE; i+=2) { |
634 |
> |
for (int i = 1; i < SIZE; i += 2) { |
635 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
636 |
|
} |
637 |
< |
for (int i = 0; i < SIZE; i+=2) { |
637 |
> |
for (int i = 0; i < SIZE; i += 2) { |
638 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
639 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
639 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
640 |
|
} |
641 |
|
assertTrue(q.isEmpty()); |
642 |
|
} |
646 |
|
*/ |
647 |
|
public void testRemoveLastOccurrence() { |
648 |
|
LinkedList q = populatedQueue(SIZE); |
649 |
< |
for (int i = 1; i < SIZE; i+=2) { |
649 |
> |
for (int i = 1; i < SIZE; i += 2) { |
650 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
651 |
|
} |
652 |
< |
for (int i = 0; i < SIZE; i+=2) { |
652 |
> |
for (int i = 0; i < SIZE; i += 2) { |
653 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
654 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
654 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
655 |
|
} |
656 |
|
assertTrue(q.isEmpty()); |
657 |
|
} |