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.List; |
14 |
import java.util.NoSuchElementException; |
import java.util.NoSuchElementException; |
15 |
|
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
|
17 |
import junit.framework.Test; |
import junit.framework.Test; |
|
import junit.framework.TestSuite; |
|
18 |
|
|
19 |
public class LinkedListTest extends JSR166TestCase { |
public class LinkedListTest extends JSR166TestCase { |
20 |
public static void main(String[] args) { |
public static void main(String[] args) { |
21 |
junit.textui.TestRunner.run(suite()); |
main(suite(), args); |
22 |
} |
} |
23 |
|
|
24 |
public static Test suite() { |
public static Test suite() { |
25 |
return new TestSuite(LinkedListTest.class); |
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 |
* Returns a new queue of given size containing consecutive |
* Returns a new queue of given size containing consecutive |
50 |
* Integers 0 ... n. |
* Integers 0 ... n - 1. |
51 |
*/ |
*/ |
52 |
private LinkedList<Integer> populatedQueue(int n) { |
private static LinkedList<Integer> populatedQueue(int n) { |
53 |
LinkedList<Integer> q = new LinkedList<Integer>(); |
LinkedList<Integer> q = new LinkedList<>(); |
54 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
55 |
for (int i = 0; i < n; ++i) |
for (int i = 0; i < n; ++i) |
56 |
assertTrue(q.offer(new Integer(i))); |
assertTrue(q.offer(new Integer(i))); |
57 |
assertFalse(q.isEmpty()); |
assertFalse(q.isEmpty()); |
58 |
assertEquals(n, q.size()); |
assertEquals(n, q.size()); |
59 |
|
assertEquals((Integer) 0, q.peekFirst()); |
60 |
|
assertEquals((Integer) (n - 1), q.peekLast()); |
61 |
return q; |
return q; |
62 |
} |
} |
63 |
|
|
73 |
*/ |
*/ |
74 |
public void testConstructor3() { |
public void testConstructor3() { |
75 |
try { |
try { |
76 |
LinkedList q = new LinkedList((Collection)null); |
new LinkedList((Collection)null); |
77 |
shouldThrow(); |
shouldThrow(); |
78 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
79 |
} |
} |
125 |
public void testOfferNull() { |
public void testOfferNull() { |
126 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
127 |
q.offer(null); |
q.offer(null); |
128 |
|
assertNull(q.get(0)); |
129 |
|
assertTrue(q.contains(null)); |
130 |
} |
} |
131 |
|
|
132 |
/** |
/** |
153 |
* addAll(null) throws NPE |
* addAll(null) throws NPE |
154 |
*/ |
*/ |
155 |
public void testAddAll1() { |
public void testAddAll1() { |
|
try { |
|
156 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
157 |
|
try { |
158 |
q.addAll(null); |
q.addAll(null); |
159 |
shouldThrow(); |
shouldThrow(); |
160 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
347 |
assertTrue(q.removeAll(p)); |
assertTrue(q.removeAll(p)); |
348 |
assertEquals(SIZE-i, q.size()); |
assertEquals(SIZE-i, q.size()); |
349 |
for (int j = 0; j < i; ++j) { |
for (int j = 0; j < i; ++j) { |
350 |
Integer I = (Integer)(p.remove()); |
Integer x = (Integer)(p.remove()); |
351 |
assertFalse(q.contains(I)); |
assertFalse(q.contains(x)); |
352 |
} |
} |
353 |
} |
} |
354 |
} |
} |
358 |
*/ |
*/ |
359 |
public void testToArray() { |
public void testToArray() { |
360 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
361 |
Object[] o = q.toArray(); |
Object[] a = q.toArray(); |
362 |
for (int i = 0; i < o.length; i++) |
assertSame(Object[].class, a.getClass()); |
363 |
assertSame(o[i], q.poll()); |
for (Object o : a) |
364 |
|
assertSame(o, q.poll()); |
365 |
|
assertTrue(q.isEmpty()); |
366 |
} |
} |
367 |
|
|
368 |
/** |
/** |
373 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
374 |
Integer[] array = q.toArray(ints); |
Integer[] array = q.toArray(ints); |
375 |
assertSame(ints, array); |
assertSame(ints, array); |
376 |
for (int i = 0; i < ints.length; i++) |
for (Integer o : ints) |
377 |
assertSame(ints[i], q.poll()); |
assertSame(o, q.poll()); |
378 |
|
assertTrue(q.isEmpty()); |
379 |
} |
} |
380 |
|
|
381 |
/** |
/** |
407 |
*/ |
*/ |
408 |
public void testIterator() { |
public void testIterator() { |
409 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
|
int i = 0; |
|
410 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
411 |
while (it.hasNext()) { |
int i; |
412 |
|
for (i = 0; it.hasNext(); i++) |
413 |
assertTrue(q.contains(it.next())); |
assertTrue(q.contains(it.next())); |
|
++i; |
|
|
} |
|
414 |
assertEquals(i, SIZE); |
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 |
/** |
/** |