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; |
16 |
– |
import junit.framework.TestSuite; |
18 |
|
|
19 |
|
public class LinkedListTest extends JSR166TestCase { |
20 |
|
public static void main(String[] args) { |
24 |
|
public static Test suite() { |
25 |
|
class Implementation implements CollectionImplementation { |
26 |
|
public Class<?> klazz() { return LinkedList.class; } |
27 |
< |
public Collection emptyCollection() { return new LinkedList(); } |
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 Collection emptyCollection() { |
34 |
< |
return new LinkedList().subList(0, 0); |
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( |
49 |
|
* Returns a new queue of given size containing consecutive |
50 |
|
* Integers 0 ... n - 1. |
51 |
|
*/ |
52 |
< |
private LinkedList<Integer> populatedQueue(int n) { |
53 |
< |
LinkedList<Integer> q = new LinkedList<Integer>(); |
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))); |
358 |
|
*/ |
359 |
|
public void testToArray() { |
360 |
|
LinkedList q = populatedQueue(SIZE); |
361 |
< |
Object[] o = q.toArray(); |
362 |
< |
for (int i = 0; i < o.length; i++) |
363 |
< |
assertSame(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 |
|
/** |
373 |
|
Integer[] ints = new Integer[SIZE]; |
374 |
|
Integer[] array = q.toArray(ints); |
375 |
|
assertSame(ints, array); |
376 |
< |
for (int i = 0; i < ints.length; i++) |
377 |
< |
assertSame(ints[i], q.poll()); |
376 |
> |
for (Integer o : ints) |
377 |
> |
assertSame(o, q.poll()); |
378 |
> |
assertTrue(q.isEmpty()); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
385 |
|
LinkedList l = new LinkedList(); |
386 |
|
l.add(new Object()); |
387 |
|
try { |
388 |
< |
l.toArray(null); |
388 |
> |
l.toArray((Object[])null); |
389 |
|
shouldThrow(); |
390 |
|
} catch (NullPointerException success) {} |
391 |
|
} |