1 |
/* |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
* Pat Fisher, Mike Judd. |
7 |
*/ |
*/ |
8 |
|
|
9 |
import junit.framework.*; |
import java.util.Arrays; |
10 |
import java.util.*; |
import java.util.Collection; |
11 |
import java.util.concurrent.*; |
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 { |
public class LinkedListTest extends JSR166TestCase { |
19 |
public static void main(String[] args) { |
public static void main(String[] args) { |
20 |
junit.textui.TestRunner.run (suite()); |
main(suite(), args); |
21 |
} |
} |
22 |
|
|
23 |
public static Test suite() { |
public static Test suite() { |
24 |
return new TestSuite(LinkedListTest.class); |
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 |
* Returns a new queue of given size containing consecutive |
44 |
* Integers 0 ... n. |
* Integers 0 ... n - 1. |
45 |
*/ |
*/ |
46 |
private LinkedList populatedQueue(int n) { |
private LinkedList<Integer> populatedQueue(int n) { |
47 |
LinkedList q = new LinkedList(); |
LinkedList<Integer> q = new LinkedList<>(); |
48 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
49 |
for (int i = 0; i < n; ++i) |
for (int i = 0; i < n; ++i) |
50 |
assertTrue(q.offer(new Integer(i))); |
assertTrue(q.offer(new Integer(i))); |
51 |
assertFalse(q.isEmpty()); |
assertFalse(q.isEmpty()); |
52 |
assertEquals(n, q.size()); |
assertEquals(n, q.size()); |
53 |
|
assertEquals((Integer) 0, q.peekFirst()); |
54 |
|
assertEquals((Integer) (n - 1), q.peekLast()); |
55 |
return q; |
return q; |
56 |
} |
} |
57 |
|
|
67 |
*/ |
*/ |
68 |
public void testConstructor3() { |
public void testConstructor3() { |
69 |
try { |
try { |
70 |
LinkedList q = new LinkedList((Collection)null); |
new LinkedList((Collection)null); |
71 |
shouldThrow(); |
shouldThrow(); |
72 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
73 |
} |
} |
119 |
public void testOfferNull() { |
public void testOfferNull() { |
120 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
121 |
q.offer(null); |
q.offer(null); |
122 |
|
assertNull(q.get(0)); |
123 |
|
assertTrue(q.contains(null)); |
124 |
} |
} |
125 |
|
|
126 |
/** |
/** |
147 |
* addAll(null) throws NPE |
* addAll(null) throws NPE |
148 |
*/ |
*/ |
149 |
public void testAddAll1() { |
public void testAddAll1() { |
|
try { |
|
150 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
151 |
|
try { |
152 |
q.addAll(null); |
q.addAll(null); |
153 |
shouldThrow(); |
shouldThrow(); |
154 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
257 |
public void testRemoveElement() { |
public void testRemoveElement() { |
258 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
259 |
for (int i = 1; i < SIZE; i+=2) { |
for (int i = 1; i < SIZE; i+=2) { |
260 |
assertTrue(q.remove(new Integer(i))); |
assertTrue(q.contains(i)); |
261 |
|
assertTrue(q.remove((Integer)i)); |
262 |
|
assertFalse(q.contains(i)); |
263 |
|
assertTrue(q.contains(i - 1)); |
264 |
} |
} |
265 |
for (int i = 0; i < SIZE; i+=2) { |
for (int i = 0; i < SIZE; i+=2) { |
266 |
assertTrue(q.remove(new Integer(i))); |
assertTrue(q.contains(i)); |
267 |
assertFalse(q.remove(new Integer(i+1))); |
assertTrue(q.remove((Integer)i)); |
268 |
|
assertFalse(q.contains(i)); |
269 |
|
assertFalse(q.remove((Integer)(i + 1))); |
270 |
|
assertFalse(q.contains(i + 1)); |
271 |
} |
} |
272 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
273 |
} |
} |
341 |
assertTrue(q.removeAll(p)); |
assertTrue(q.removeAll(p)); |
342 |
assertEquals(SIZE-i, q.size()); |
assertEquals(SIZE-i, q.size()); |
343 |
for (int j = 0; j < i; ++j) { |
for (int j = 0; j < i; ++j) { |
344 |
Integer I = (Integer)(p.remove()); |
Integer x = (Integer)(p.remove()); |
345 |
assertFalse(q.contains(I)); |
assertFalse(q.contains(x)); |
346 |
} |
} |
347 |
} |
} |
348 |
} |
} |
349 |
|
|
350 |
/** |
/** |
351 |
* toArray contains all elements |
* toArray contains all elements in FIFO order |
352 |
*/ |
*/ |
353 |
public void testToArray() { |
public void testToArray() { |
354 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
355 |
Object[] o = q.toArray(); |
Object[] o = q.toArray(); |
|
Arrays.sort(o); |
|
356 |
for (int i = 0; i < o.length; i++) |
for (int i = 0; i < o.length; i++) |
357 |
assertEquals(o[i], q.poll()); |
assertSame(o[i], q.poll()); |
358 |
} |
} |
359 |
|
|
360 |
/** |
/** |
361 |
* toArray(a) contains all elements |
* toArray(a) contains all elements in FIFO order |
362 |
*/ |
*/ |
363 |
public void testToArray2() { |
public void testToArray2() { |
364 |
LinkedList q = populatedQueue(SIZE); |
LinkedList<Integer> q = populatedQueue(SIZE); |
365 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
366 |
ints = (Integer[])q.toArray(ints); |
Integer[] array = q.toArray(ints); |
367 |
Arrays.sort(ints); |
assertSame(ints, array); |
368 |
for (int i = 0; i < ints.length; i++) |
for (int i = 0; i < ints.length; i++) |
369 |
assertEquals(ints[i], q.poll()); |
assertSame(ints[i], q.poll()); |
370 |
} |
} |
371 |
|
|
372 |
/** |
/** |
373 |
* toArray(null) throws NPE |
* toArray(null) throws NullPointerException |
374 |
*/ |
*/ |
375 |
public void testToArray_BadArg() { |
public void testToArray_NullArg() { |
376 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
377 |
l.add(new Object()); |
l.add(new Object()); |
378 |
try { |
try { |
379 |
Object o[] = l.toArray(null); |
l.toArray(null); |
380 |
shouldThrow(); |
shouldThrow(); |
381 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
382 |
} |
} |
383 |
|
|
384 |
/** |
/** |
385 |
* toArray with incompatable aray type throws CCE |
* toArray(incompatible array type) throws ArrayStoreException |
386 |
*/ |
*/ |
387 |
public void testToArray1_BadArg() { |
public void testToArray1_BadArg() { |
388 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
389 |
l.add(new Integer(5)); |
l.add(new Integer(5)); |
390 |
try { |
try { |
391 |
Object o[] = l.toArray(new String[10]); |
l.toArray(new String[10]); |
392 |
shouldThrow(); |
shouldThrow(); |
393 |
} catch (ArrayStoreException success) {} |
} catch (ArrayStoreException success) {} |
394 |
} |
} |
398 |
*/ |
*/ |
399 |
public void testIterator() { |
public void testIterator() { |
400 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
|
int i = 0; |
|
401 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
402 |
while (it.hasNext()) { |
int i; |
403 |
|
for (i = 0; it.hasNext(); i++) |
404 |
assertTrue(q.contains(it.next())); |
assertTrue(q.contains(it.next())); |
|
++i; |
|
|
} |
|
405 |
assertEquals(i, SIZE); |
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)); |
q.add(new Integer(2)); |
439 |
q.add(new Integer(3)); |
q.add(new Integer(3)); |
440 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
441 |
assertEquals(it.next(), 1); |
assertEquals(1, it.next()); |
442 |
it.remove(); |
it.remove(); |
443 |
it = q.iterator(); |
it = q.iterator(); |
444 |
assertEquals(it.next(), 2); |
assertEquals(2, it.next()); |
445 |
assertEquals(it.next(), 3); |
assertEquals(3, it.next()); |
446 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
447 |
} |
} |
448 |
|
|
498 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
499 |
} |
} |
500 |
|
|
|
|
|
501 |
/** |
/** |
502 |
* toString contains toStrings of elements |
* toString contains toStrings of elements |
503 |
*/ |
*/ |
505 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
506 |
String s = q.toString(); |
String s = q.toString(); |
507 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
508 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
assertTrue(s.contains(String.valueOf(i))); |
509 |
} |
} |
510 |
} |
} |
511 |
|
|
584 |
assertNull(q.peekFirst()); |
assertNull(q.peekFirst()); |
585 |
} |
} |
586 |
|
|
|
|
|
587 |
/** |
/** |
588 |
* peekLast returns next element, or null if empty |
* peekLast returns next element, or null if empty |
589 |
*/ |
*/ |