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) { |
20 |
< |
junit.textui.TestRunner.run (suite()); |
20 |
> |
main(suite(), args); |
21 |
|
} |
22 |
|
|
23 |
|
public static Test suite() { |
25 |
|
} |
26 |
|
|
27 |
|
/** |
28 |
< |
* Create a queue of given size containing consecutive |
29 |
< |
* Integers 0 ... n. |
28 |
> |
* Returns a new queue of given size containing consecutive |
29 |
> |
* Integers 0 ... n - 1. |
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))); |
36 |
|
assertFalse(q.isEmpty()); |
37 |
|
assertEquals(n, q.size()); |
38 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
39 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
40 |
|
return q; |
41 |
|
} |
42 |
|
|
52 |
|
*/ |
53 |
|
public void testConstructor3() { |
54 |
|
try { |
55 |
< |
LinkedList q = new LinkedList((Collection)null); |
55 |
> |
new LinkedList((Collection)null); |
56 |
|
shouldThrow(); |
57 |
|
} catch (NullPointerException success) {} |
58 |
|
} |
89 |
|
public void testSize() { |
90 |
|
LinkedList q = populatedQueue(SIZE); |
91 |
|
for (int i = 0; i < SIZE; ++i) { |
92 |
< |
assertEquals(SIZE-i, q.size()); |
92 |
> |
assertEquals(SIZE - i, q.size()); |
93 |
|
q.remove(); |
94 |
|
} |
95 |
|
for (int i = 0; i < SIZE; ++i) { |
104 |
|
public void testOfferNull() { |
105 |
|
LinkedList q = new LinkedList(); |
106 |
|
q.offer(null); |
107 |
+ |
assertNull(q.get(0)); |
108 |
+ |
assertTrue(q.contains(null)); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
132 |
|
* addAll(null) throws NPE |
133 |
|
*/ |
134 |
|
public void testAddAll1() { |
135 |
+ |
LinkedList q = new LinkedList(); |
136 |
|
try { |
127 |
– |
LinkedList q = new LinkedList(); |
137 |
|
q.addAll(null); |
138 |
|
shouldThrow(); |
139 |
|
} catch (NullPointerException success) {} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
186 |
< |
* poll succeeds unless empty |
186 |
> |
* poll succeeds unless empty |
187 |
|
*/ |
188 |
|
public void testPoll() { |
189 |
|
LinkedList q = populatedQueue(SIZE); |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
< |
* peek returns next element, or null if empty |
197 |
> |
* peek returns next element, or null if empty |
198 |
|
*/ |
199 |
|
public void testPeek() { |
200 |
|
LinkedList q = populatedQueue(SIZE); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
< |
* remove removes next element, or throws NSEE if empty |
226 |
> |
* remove removes next element, or throws NSEE if empty |
227 |
|
*/ |
228 |
|
public void testRemove() { |
229 |
|
LinkedList q = populatedQueue(SIZE); |
241 |
|
*/ |
242 |
|
public void testRemoveElement() { |
243 |
|
LinkedList q = populatedQueue(SIZE); |
244 |
< |
for (int i = 1; i < SIZE; i+=2) { |
245 |
< |
assertTrue(q.remove(new Integer(i))); |
246 |
< |
} |
247 |
< |
for (int i = 0; i < SIZE; i+=2) { |
248 |
< |
assertTrue(q.remove(new Integer(i))); |
249 |
< |
assertFalse(q.remove(new Integer(i+1))); |
244 |
> |
for (int i = 1; i < SIZE; i += 2) { |
245 |
> |
assertTrue(q.contains(i)); |
246 |
> |
assertTrue(q.remove((Integer)i)); |
247 |
> |
assertFalse(q.contains(i)); |
248 |
> |
assertTrue(q.contains(i - 1)); |
249 |
> |
} |
250 |
> |
for (int i = 0; i < SIZE; i += 2) { |
251 |
> |
assertTrue(q.contains(i)); |
252 |
> |
assertTrue(q.remove((Integer)i)); |
253 |
> |
assertFalse(q.contains(i)); |
254 |
> |
assertFalse(q.remove((Integer)(i + 1))); |
255 |
> |
assertFalse(q.contains(i + 1)); |
256 |
|
} |
257 |
|
assertTrue(q.isEmpty()); |
258 |
|
} |
311 |
|
assertTrue(changed); |
312 |
|
|
313 |
|
assertTrue(q.containsAll(p)); |
314 |
< |
assertEquals(SIZE-i, q.size()); |
314 |
> |
assertEquals(SIZE - i, q.size()); |
315 |
|
p.remove(); |
316 |
|
} |
317 |
|
} |
324 |
|
LinkedList q = populatedQueue(SIZE); |
325 |
|
LinkedList p = populatedQueue(i); |
326 |
|
assertTrue(q.removeAll(p)); |
327 |
< |
assertEquals(SIZE-i, q.size()); |
327 |
> |
assertEquals(SIZE - i, q.size()); |
328 |
|
for (int j = 0; j < i; ++j) { |
329 |
< |
Integer I = (Integer)(p.remove()); |
330 |
< |
assertFalse(q.contains(I)); |
329 |
> |
Integer x = (Integer)(p.remove()); |
330 |
> |
assertFalse(q.contains(x)); |
331 |
|
} |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
/** |
336 |
< |
* toArray contains all elements |
336 |
> |
* toArray contains all elements in FIFO order |
337 |
|
*/ |
338 |
|
public void testToArray() { |
339 |
|
LinkedList q = populatedQueue(SIZE); |
340 |
|
Object[] o = q.toArray(); |
326 |
– |
Arrays.sort(o); |
341 |
|
for (int i = 0; i < o.length; i++) |
342 |
< |
assertEquals(o[i], q.poll()); |
342 |
> |
assertSame(o[i], q.poll()); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* toArray(a) contains all elements |
346 |
> |
* toArray(a) contains all elements in FIFO order |
347 |
|
*/ |
348 |
|
public void testToArray2() { |
349 |
< |
LinkedList q = populatedQueue(SIZE); |
349 |
> |
LinkedList<Integer> q = populatedQueue(SIZE); |
350 |
|
Integer[] ints = new Integer[SIZE]; |
351 |
< |
ints = (Integer[])q.toArray(ints); |
352 |
< |
Arrays.sort(ints); |
351 |
> |
Integer[] array = q.toArray(ints); |
352 |
> |
assertSame(ints, array); |
353 |
|
for (int i = 0; i < ints.length; i++) |
354 |
< |
assertEquals(ints[i], q.poll()); |
354 |
> |
assertSame(ints[i], q.poll()); |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
< |
* toArray(null) throws NPE |
358 |
> |
* toArray(null) throws NullPointerException |
359 |
|
*/ |
360 |
< |
public void testToArray_BadArg() { |
360 |
> |
public void testToArray_NullArg() { |
361 |
|
LinkedList l = new LinkedList(); |
362 |
|
l.add(new Object()); |
363 |
|
try { |
364 |
< |
Object o[] = l.toArray(null); |
364 |
> |
l.toArray(null); |
365 |
|
shouldThrow(); |
366 |
|
} catch (NullPointerException success) {} |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
< |
* toArray with incompatible array type throws CCE |
370 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
371 |
|
*/ |
372 |
|
public void testToArray1_BadArg() { |
373 |
|
LinkedList l = new LinkedList(); |
374 |
|
l.add(new Integer(5)); |
375 |
|
try { |
376 |
< |
Object o[] = l.toArray(new String[10]); |
376 |
> |
l.toArray(new String[10]); |
377 |
|
shouldThrow(); |
378 |
|
} catch (ArrayStoreException success) {} |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* iterator iterates through all elements |
382 |
> |
* iterator iterates through all elements |
383 |
|
*/ |
384 |
|
public void testIterator() { |
385 |
|
LinkedList q = populatedQueue(SIZE); |
372 |
– |
int i = 0; |
386 |
|
Iterator it = q.iterator(); |
387 |
< |
while (it.hasNext()) { |
387 |
> |
int i; |
388 |
> |
for (i = 0; it.hasNext(); i++) |
389 |
|
assertTrue(q.contains(it.next())); |
376 |
– |
++i; |
377 |
– |
} |
390 |
|
assertEquals(i, SIZE); |
391 |
+ |
assertIteratorExhausted(it); |
392 |
+ |
} |
393 |
+ |
|
394 |
+ |
/** |
395 |
+ |
* iterator of empty collection has no elements |
396 |
+ |
*/ |
397 |
+ |
public void testEmptyIterator() { |
398 |
+ |
assertIteratorExhausted(new LinkedList().iterator()); |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
< |
* iterator ordering is FIFO |
402 |
> |
* iterator ordering is FIFO |
403 |
|
*/ |
404 |
|
public void testIteratorOrdering() { |
405 |
|
final LinkedList q = new LinkedList(); |
417 |
|
/** |
418 |
|
* iterator.remove removes current element |
419 |
|
*/ |
420 |
< |
public void testIteratorRemove () { |
420 |
> |
public void testIteratorRemove() { |
421 |
|
final LinkedList q = new LinkedList(); |
422 |
|
q.add(new Integer(1)); |
423 |
|
q.add(new Integer(2)); |
424 |
|
q.add(new Integer(3)); |
425 |
|
Iterator it = q.iterator(); |
426 |
< |
assertEquals(it.next(), 1); |
426 |
> |
assertEquals(1, it.next()); |
427 |
|
it.remove(); |
428 |
|
it = q.iterator(); |
429 |
< |
assertEquals(it.next(), 2); |
430 |
< |
assertEquals(it.next(), 3); |
429 |
> |
assertEquals(2, it.next()); |
430 |
> |
assertEquals(3, it.next()); |
431 |
|
assertFalse(it.hasNext()); |
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
< |
* Descending iterator iterates through all elements |
435 |
> |
* Descending iterator iterates through all elements |
436 |
|
*/ |
437 |
|
public void testDescendingIterator() { |
438 |
|
LinkedList q = populatedQueue(SIZE); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
< |
* Descending iterator ordering is reverse FIFO |
454 |
> |
* Descending iterator ordering is reverse FIFO |
455 |
|
*/ |
456 |
|
public void testDescendingIteratorOrdering() { |
457 |
|
final LinkedList q = new LinkedList(); |
469 |
|
/** |
470 |
|
* descendingIterator.remove removes current element |
471 |
|
*/ |
472 |
< |
public void testDescendingIteratorRemove () { |
472 |
> |
public void testDescendingIteratorRemove() { |
473 |
|
final LinkedList q = new LinkedList(); |
474 |
|
q.add(three); |
475 |
|
q.add(two); |
483 |
|
assertFalse(it.hasNext()); |
484 |
|
} |
485 |
|
|
466 |
– |
|
486 |
|
/** |
487 |
|
* toString contains toStrings of elements |
488 |
|
*/ |
490 |
|
LinkedList q = populatedQueue(SIZE); |
491 |
|
String s = q.toString(); |
492 |
|
for (int i = 0; i < SIZE; ++i) { |
493 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
493 |
> |
assertTrue(s.contains(String.valueOf(i))); |
494 |
|
} |
495 |
|
} |
496 |
|
|
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
< |
* pop removes next element, or throws NSEE if empty |
516 |
> |
* pop removes next element, or throws NSEE if empty |
517 |
|
*/ |
518 |
|
public void testPop() { |
519 |
|
LinkedList q = populatedQueue(SIZE); |
545 |
|
} |
546 |
|
|
547 |
|
/** |
548 |
< |
* pollLast succeeds unless empty |
548 |
> |
* pollLast succeeds unless empty |
549 |
|
*/ |
550 |
|
public void testPollLast() { |
551 |
|
LinkedList q = populatedQueue(SIZE); |
552 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
552 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
553 |
|
assertEquals(i, q.pollLast()); |
554 |
|
} |
555 |
|
assertNull(q.pollLast()); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* peekFirst returns next element, or null if empty |
559 |
> |
* peekFirst returns next element, or null if empty |
560 |
|
*/ |
561 |
|
public void testPeekFirst() { |
562 |
|
LinkedList q = populatedQueue(SIZE); |
569 |
|
assertNull(q.peekFirst()); |
570 |
|
} |
571 |
|
|
553 |
– |
|
572 |
|
/** |
573 |
< |
* peekLast returns next element, or null if empty |
573 |
> |
* peekLast returns next element, or null if empty |
574 |
|
*/ |
575 |
|
public void testPeekLast() { |
576 |
|
LinkedList q = populatedQueue(SIZE); |
577 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
577 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
578 |
|
assertEquals(i, q.peekLast()); |
579 |
|
assertEquals(i, q.pollLast()); |
580 |
|
assertTrue(q.peekLast() == null || |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* getLast returns next element, or throws NSEE if empty |
599 |
> |
* getLast returns next element, or throws NSEE if empty |
600 |
|
*/ |
601 |
|
public void testLastElement() { |
602 |
|
LinkedList q = populatedQueue(SIZE); |
603 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
603 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
604 |
|
assertEquals(i, q.getLast()); |
605 |
|
assertEquals(i, q.pollLast()); |
606 |
|
} |
616 |
|
*/ |
617 |
|
public void testRemoveFirstOccurrence() { |
618 |
|
LinkedList q = populatedQueue(SIZE); |
619 |
< |
for (int i = 1; i < SIZE; i+=2) { |
619 |
> |
for (int i = 1; i < SIZE; i += 2) { |
620 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
621 |
|
} |
622 |
< |
for (int i = 0; i < SIZE; i+=2) { |
622 |
> |
for (int i = 0; i < SIZE; i += 2) { |
623 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
624 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
624 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
625 |
|
} |
626 |
|
assertTrue(q.isEmpty()); |
627 |
|
} |
631 |
|
*/ |
632 |
|
public void testRemoveLastOccurrence() { |
633 |
|
LinkedList q = populatedQueue(SIZE); |
634 |
< |
for (int i = 1; i < SIZE; i+=2) { |
634 |
> |
for (int i = 1; i < SIZE; i += 2) { |
635 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
636 |
|
} |
637 |
< |
for (int i = 0; i < SIZE; i+=2) { |
637 |
> |
for (int i = 0; i < SIZE; i += 2) { |
638 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
639 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
639 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
640 |
|
} |
641 |
|
assertTrue(q.isEmpty()); |
642 |
|
} |