1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
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) { |
25 |
|
} |
26 |
|
|
27 |
|
/** |
28 |
< |
* Create a queue of given size containing consecutive |
28 |
> |
* Returns a new queue of given size containing consecutive |
29 |
|
* Integers 0 ... n. |
30 |
|
*/ |
31 |
< |
private LinkedList populatedQueue(int n) { |
32 |
< |
LinkedList q = new LinkedList(); |
31 |
> |
private LinkedList<Integer> populatedQueue(int n) { |
32 |
> |
LinkedList<Integer> q = new LinkedList<Integer>(); |
33 |
|
assertTrue(q.isEmpty()); |
34 |
|
for (int i = 0; i < n; ++i) |
35 |
|
assertTrue(q.offer(new Integer(i))); |
50 |
|
*/ |
51 |
|
public void testConstructor3() { |
52 |
|
try { |
53 |
< |
LinkedList q = new LinkedList((Collection)null); |
53 |
> |
new LinkedList((Collection)null); |
54 |
|
shouldThrow(); |
55 |
|
} catch (NullPointerException success) {} |
56 |
|
} |
237 |
|
*/ |
238 |
|
public void testRemoveElement() { |
239 |
|
LinkedList q = populatedQueue(SIZE); |
240 |
< |
for (int i = 1; i < SIZE; i+=2) { |
241 |
< |
assertTrue(q.remove(new Integer(i))); |
242 |
< |
} |
243 |
< |
for (int i = 0; i < SIZE; i+=2) { |
244 |
< |
assertTrue(q.remove(new Integer(i))); |
245 |
< |
assertFalse(q.remove(new Integer(i+1))); |
240 |
> |
for (int i = 1; i < SIZE; i += 2) { |
241 |
> |
assertTrue(q.contains(i)); |
242 |
> |
assertTrue(q.remove((Integer)i)); |
243 |
> |
assertFalse(q.contains(i)); |
244 |
> |
assertTrue(q.contains(i-1)); |
245 |
> |
} |
246 |
> |
for (int i = 0; i < SIZE; i += 2) { |
247 |
> |
assertTrue(q.contains(i)); |
248 |
> |
assertTrue(q.remove((Integer)i)); |
249 |
> |
assertFalse(q.contains(i)); |
250 |
> |
assertFalse(q.remove((Integer)(i+1))); |
251 |
> |
assertFalse(q.contains(i+1)); |
252 |
|
} |
253 |
|
assertTrue(q.isEmpty()); |
254 |
|
} |
322 |
|
assertTrue(q.removeAll(p)); |
323 |
|
assertEquals(SIZE-i, q.size()); |
324 |
|
for (int j = 0; j < i; ++j) { |
325 |
< |
Integer I = (Integer)(p.remove()); |
326 |
< |
assertFalse(q.contains(I)); |
325 |
> |
Integer x = (Integer)(p.remove()); |
326 |
> |
assertFalse(q.contains(x)); |
327 |
|
} |
328 |
|
} |
329 |
|
} |
342 |
|
* toArray(a) contains all elements in FIFO order |
343 |
|
*/ |
344 |
|
public void testToArray2() { |
345 |
< |
LinkedList q = populatedQueue(SIZE); |
345 |
> |
LinkedList<Integer> q = populatedQueue(SIZE); |
346 |
|
Integer[] ints = new Integer[SIZE]; |
347 |
< |
assertSame(ints, q.toArray(ints)); |
347 |
> |
Integer[] array = q.toArray(ints); |
348 |
> |
assertSame(ints, array); |
349 |
|
for (int i = 0; i < ints.length; i++) |
350 |
|
assertSame(ints[i], q.poll()); |
351 |
|
} |
379 |
|
*/ |
380 |
|
public void testIterator() { |
381 |
|
LinkedList q = populatedQueue(SIZE); |
370 |
– |
int i = 0; |
382 |
|
Iterator it = q.iterator(); |
383 |
< |
while (it.hasNext()) { |
383 |
> |
int i; |
384 |
> |
for (i = 0; it.hasNext(); i++) |
385 |
|
assertTrue(q.contains(it.next())); |
374 |
– |
++i; |
375 |
– |
} |
386 |
|
assertEquals(i, SIZE); |
387 |
+ |
assertIteratorExhausted(it); |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
/** |
391 |
+ |
* iterator of empty collection has no elements |
392 |
+ |
*/ |
393 |
+ |
public void testEmptyIterator() { |
394 |
+ |
assertIteratorExhausted(new LinkedList().iterator()); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
419 |
|
q.add(new Integer(2)); |
420 |
|
q.add(new Integer(3)); |
421 |
|
Iterator it = q.iterator(); |
422 |
< |
assertEquals(it.next(), 1); |
422 |
> |
assertEquals(1, it.next()); |
423 |
|
it.remove(); |
424 |
|
it = q.iterator(); |
425 |
< |
assertEquals(it.next(), 2); |
426 |
< |
assertEquals(it.next(), 3); |
425 |
> |
assertEquals(2, it.next()); |
426 |
> |
assertEquals(3, it.next()); |
427 |
|
assertFalse(it.hasNext()); |
428 |
|
} |
429 |
|
|
479 |
|
assertFalse(it.hasNext()); |
480 |
|
} |
481 |
|
|
464 |
– |
|
482 |
|
/** |
483 |
|
* toString contains toStrings of elements |
484 |
|
*/ |
486 |
|
LinkedList q = populatedQueue(SIZE); |
487 |
|
String s = q.toString(); |
488 |
|
for (int i = 0; i < SIZE; ++i) { |
489 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
489 |
> |
assertTrue(s.contains(String.valueOf(i))); |
490 |
|
} |
491 |
|
} |
492 |
|
|
565 |
|
assertNull(q.peekFirst()); |
566 |
|
} |
567 |
|
|
551 |
– |
|
568 |
|
/** |
569 |
|
* peekLast returns next element, or null if empty |
570 |
|
*/ |
612 |
|
*/ |
613 |
|
public void testRemoveFirstOccurrence() { |
614 |
|
LinkedList q = populatedQueue(SIZE); |
615 |
< |
for (int i = 1; i < SIZE; i+=2) { |
615 |
> |
for (int i = 1; i < SIZE; i += 2) { |
616 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
617 |
|
} |
618 |
< |
for (int i = 0; i < SIZE; i+=2) { |
618 |
> |
for (int i = 0; i < SIZE; i += 2) { |
619 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
620 |
|
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
621 |
|
} |
627 |
|
*/ |
628 |
|
public void testRemoveLastOccurrence() { |
629 |
|
LinkedList q = populatedQueue(SIZE); |
630 |
< |
for (int i = 1; i < SIZE; i+=2) { |
630 |
> |
for (int i = 1; i < SIZE; i += 2) { |
631 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
632 |
|
} |
633 |
< |
for (int i = 0; i < SIZE; i+=2) { |
633 |
> |
for (int i = 0; i < SIZE; i += 2) { |
634 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
635 |
|
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
636 |
|
} |