6 |
* Pat Fisher, Mike Judd. |
* Pat Fisher, Mike Judd. |
7 |
*/ |
*/ |
8 |
|
|
|
import junit.framework.*; |
|
9 |
import java.util.Arrays; |
import java.util.Arrays; |
10 |
import java.util.Collection; |
import java.util.Collection; |
11 |
import java.util.Iterator; |
import java.util.Iterator; |
12 |
import java.util.LinkedList; |
import java.util.LinkedList; |
13 |
import java.util.NoSuchElementException; |
import java.util.NoSuchElementException; |
14 |
|
|
15 |
|
import junit.framework.Test; |
16 |
|
|
17 |
public class LinkedListTest extends JSR166TestCase { |
public class LinkedListTest extends JSR166TestCase { |
18 |
public static void main(String[] args) { |
public static void main(String[] args) { |
19 |
junit.textui.TestRunner.run(suite()); |
main(suite(), args); |
20 |
} |
} |
21 |
|
|
22 |
public static Test suite() { |
public static Test suite() { |
23 |
return new TestSuite(LinkedListTest.class); |
class Implementation implements CollectionImplementation { |
24 |
|
public Class<?> klazz() { return LinkedList.class; } |
25 |
|
public Collection emptyCollection() { return new LinkedList(); } |
26 |
|
public Object makeElement(int i) { return i; } |
27 |
|
public boolean isConcurrent() { return false; } |
28 |
|
public boolean permitsNulls() { return true; } |
29 |
|
} |
30 |
|
class SubListImplementation extends Implementation { |
31 |
|
public Collection emptyCollection() { |
32 |
|
return new LinkedList().subList(0, 0); |
33 |
|
} |
34 |
|
} |
35 |
|
return newTestSuite( |
36 |
|
LinkedListTest.class, |
37 |
|
CollectionTest.testSuite(new Implementation()), |
38 |
|
CollectionTest.testSuite(new SubListImplementation())); |
39 |
} |
} |
40 |
|
|
41 |
/** |
/** |
42 |
* Returns a new queue of given size containing consecutive |
* Returns a new queue of given size containing consecutive |
43 |
* Integers 0 ... n. |
* Integers 0 ... n - 1. |
44 |
*/ |
*/ |
45 |
private LinkedList<Integer> populatedQueue(int n) { |
private static LinkedList<Integer> populatedQueue(int n) { |
46 |
LinkedList<Integer> q = new LinkedList<Integer>(); |
LinkedList<Integer> q = new LinkedList<>(); |
47 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
48 |
for (int i = 0; i < n; ++i) |
for (int i = 0; i < n; ++i) |
49 |
assertTrue(q.offer(new Integer(i))); |
assertTrue(q.offer(new Integer(i))); |
50 |
assertFalse(q.isEmpty()); |
assertFalse(q.isEmpty()); |
51 |
assertEquals(n, q.size()); |
assertEquals(n, q.size()); |
52 |
|
assertEquals((Integer) 0, q.peekFirst()); |
53 |
|
assertEquals((Integer) (n - 1), q.peekLast()); |
54 |
return q; |
return q; |
55 |
} |
} |
56 |
|
|
66 |
*/ |
*/ |
67 |
public void testConstructor3() { |
public void testConstructor3() { |
68 |
try { |
try { |
69 |
LinkedList q = new LinkedList((Collection)null); |
new LinkedList((Collection)null); |
70 |
shouldThrow(); |
shouldThrow(); |
71 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
72 |
} |
} |
118 |
public void testOfferNull() { |
public void testOfferNull() { |
119 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
120 |
q.offer(null); |
q.offer(null); |
121 |
|
assertNull(q.get(0)); |
122 |
|
assertTrue(q.contains(null)); |
123 |
} |
} |
124 |
|
|
125 |
/** |
/** |
146 |
* addAll(null) throws NPE |
* addAll(null) throws NPE |
147 |
*/ |
*/ |
148 |
public void testAddAll1() { |
public void testAddAll1() { |
|
try { |
|
149 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
150 |
|
try { |
151 |
q.addAll(null); |
q.addAll(null); |
152 |
shouldThrow(); |
shouldThrow(); |
153 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
340 |
assertTrue(q.removeAll(p)); |
assertTrue(q.removeAll(p)); |
341 |
assertEquals(SIZE-i, q.size()); |
assertEquals(SIZE-i, q.size()); |
342 |
for (int j = 0; j < i; ++j) { |
for (int j = 0; j < i; ++j) { |
343 |
Integer I = (Integer)(p.remove()); |
Integer x = (Integer)(p.remove()); |
344 |
assertFalse(q.contains(I)); |
assertFalse(q.contains(x)); |
345 |
} |
} |
346 |
} |
} |
347 |
} |
} |
397 |
*/ |
*/ |
398 |
public void testIterator() { |
public void testIterator() { |
399 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
|
int i = 0; |
|
400 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
401 |
while (it.hasNext()) { |
int i; |
402 |
|
for (i = 0; it.hasNext(); i++) |
403 |
assertTrue(q.contains(it.next())); |
assertTrue(q.contains(it.next())); |
|
++i; |
|
|
} |
|
404 |
assertEquals(i, SIZE); |
assertEquals(i, SIZE); |
405 |
|
assertIteratorExhausted(it); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
|
* iterator of empty collection has no elements |
410 |
|
*/ |
411 |
|
public void testEmptyIterator() { |
412 |
|
assertIteratorExhausted(new LinkedList().iterator()); |
413 |
} |
} |
414 |
|
|
415 |
/** |
/** |