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 |
> |
junit.textui.TestRunner.run(suite()); |
21 |
|
} |
22 |
|
|
23 |
|
public static Test suite() { |
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 |
|
} |
61 |
|
public void testConstructor6() { |
62 |
|
Integer[] ints = new Integer[SIZE]; |
63 |
|
for (int i = 0; i < SIZE; ++i) |
64 |
< |
ints[i] = new Integer(i); |
64 |
> |
ints[i] = i; |
65 |
|
LinkedList q = new LinkedList(Arrays.asList(ints)); |
66 |
|
for (int i = 0; i < SIZE; ++i) |
67 |
|
assertEquals(ints[i], q.poll()); |
142 |
|
Integer[] empty = new Integer[0]; |
143 |
|
Integer[] ints = new Integer[SIZE]; |
144 |
|
for (int i = 0; i < SIZE; ++i) |
145 |
< |
ints[i] = new Integer(i); |
145 |
> |
ints[i] = i; |
146 |
|
LinkedList q = new LinkedList(); |
147 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
148 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
< |
* poll succeeds unless empty |
182 |
> |
* poll succeeds unless empty |
183 |
|
*/ |
184 |
|
public void testPoll() { |
185 |
|
LinkedList q = populatedQueue(SIZE); |
186 |
|
for (int i = 0; i < SIZE; ++i) { |
187 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
187 |
> |
assertEquals(i, q.poll()); |
188 |
|
} |
189 |
|
assertNull(q.poll()); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
193 |
< |
* peek returns next element, or null if empty |
193 |
> |
* peek returns next element, or null if empty |
194 |
|
*/ |
195 |
|
public void testPeek() { |
196 |
|
LinkedList q = populatedQueue(SIZE); |
197 |
|
for (int i = 0; i < SIZE; ++i) { |
198 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
199 |
< |
q.poll(); |
198 |
> |
assertEquals(i, q.peek()); |
199 |
> |
assertEquals(i, q.poll()); |
200 |
|
assertTrue(q.peek() == null || |
201 |
< |
i != ((Integer)q.peek()).intValue()); |
201 |
> |
!q.peek().equals(i)); |
202 |
|
} |
203 |
|
assertNull(q.peek()); |
204 |
|
} |
209 |
|
public void testElement() { |
210 |
|
LinkedList q = populatedQueue(SIZE); |
211 |
|
for (int i = 0; i < SIZE; ++i) { |
212 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
213 |
< |
q.poll(); |
212 |
> |
assertEquals(i, q.element()); |
213 |
> |
assertEquals(i, q.poll()); |
214 |
|
} |
215 |
|
try { |
216 |
|
q.element(); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* remove removes next element, or throws NSEE if empty |
222 |
> |
* remove removes next element, or throws NSEE if empty |
223 |
|
*/ |
224 |
|
public void testRemove() { |
225 |
|
LinkedList q = populatedQueue(SIZE); |
226 |
|
for (int i = 0; i < SIZE; ++i) { |
227 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
227 |
> |
assertEquals(i, q.remove()); |
228 |
|
} |
229 |
|
try { |
230 |
|
q.remove(); |
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 |
|
} |
273 |
|
q.clear(); |
274 |
|
assertTrue(q.isEmpty()); |
275 |
|
assertEquals(0, q.size()); |
276 |
< |
q.add(new Integer(1)); |
276 |
> |
assertTrue(q.add(new Integer(1))); |
277 |
|
assertFalse(q.isEmpty()); |
278 |
|
q.clear(); |
279 |
|
assertTrue(q.isEmpty()); |
288 |
|
for (int i = 0; i < SIZE; ++i) { |
289 |
|
assertTrue(q.containsAll(p)); |
290 |
|
assertFalse(p.containsAll(q)); |
291 |
< |
p.add(new Integer(i)); |
291 |
> |
assertTrue(p.add(new Integer(i))); |
292 |
|
} |
293 |
|
assertTrue(p.containsAll(q)); |
294 |
|
} |
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 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* toArray contains all elements |
332 |
> |
* toArray contains all elements in FIFO order |
333 |
|
*/ |
334 |
|
public void testToArray() { |
335 |
|
LinkedList q = populatedQueue(SIZE); |
336 |
|
Object[] o = q.toArray(); |
326 |
– |
Arrays.sort(o); |
337 |
|
for (int i = 0; i < o.length; i++) |
338 |
< |
assertEquals(o[i], q.poll()); |
338 |
> |
assertSame(o[i], q.poll()); |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
< |
* toArray(a) contains all elements |
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 |
< |
ints = (Integer[])q.toArray(ints); |
348 |
< |
Arrays.sort(ints); |
347 |
> |
Integer[] array = q.toArray(ints); |
348 |
> |
assertSame(ints, array); |
349 |
|
for (int i = 0; i < ints.length; i++) |
350 |
< |
assertEquals(ints[i], q.poll()); |
350 |
> |
assertSame(ints[i], q.poll()); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
354 |
< |
* toArray(null) throws NPE |
354 |
> |
* toArray(null) throws NullPointerException |
355 |
|
*/ |
356 |
< |
public void testToArray_BadArg() { |
356 |
> |
public void testToArray_NullArg() { |
357 |
> |
LinkedList l = new LinkedList(); |
358 |
> |
l.add(new Object()); |
359 |
|
try { |
360 |
< |
LinkedList l = new LinkedList(); |
349 |
< |
l.add(new Object()); |
350 |
< |
Object o[] = l.toArray(null); |
360 |
> |
l.toArray(null); |
361 |
|
shouldThrow(); |
362 |
|
} catch (NullPointerException success) {} |
363 |
|
} |
364 |
|
|
365 |
|
/** |
366 |
< |
* toArray with incompatable aray type throws CCE |
366 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
367 |
|
*/ |
368 |
|
public void testToArray1_BadArg() { |
369 |
+ |
LinkedList l = new LinkedList(); |
370 |
+ |
l.add(new Integer(5)); |
371 |
|
try { |
372 |
< |
LinkedList l = new LinkedList(); |
361 |
< |
l.add(new Integer(5)); |
362 |
< |
Object o[] = l.toArray(new String[10] ); |
372 |
> |
l.toArray(new String[10]); |
373 |
|
shouldThrow(); |
374 |
|
} catch (ArrayStoreException success) {} |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* iterator iterates through all elements |
378 |
> |
* iterator iterates through all elements |
379 |
|
*/ |
380 |
|
public void testIterator() { |
381 |
|
LinkedList q = populatedQueue(SIZE); |
372 |
– |
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())); |
376 |
– |
++i; |
377 |
– |
} |
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 |
|
/** |
398 |
< |
* iterator ordering is FIFO |
398 |
> |
* iterator ordering is FIFO |
399 |
|
*/ |
400 |
|
public void testIteratorOrdering() { |
401 |
|
final LinkedList q = new LinkedList(); |
404 |
|
q.add(new Integer(3)); |
405 |
|
int k = 0; |
406 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
407 |
< |
int i = ((Integer)(it.next())).intValue(); |
392 |
< |
assertEquals(++k, i); |
407 |
> |
assertEquals(++k, it.next()); |
408 |
|
} |
409 |
|
|
410 |
|
assertEquals(3, k); |
413 |
|
/** |
414 |
|
* iterator.remove removes current element |
415 |
|
*/ |
416 |
< |
public void testIteratorRemove () { |
416 |
> |
public void testIteratorRemove() { |
417 |
|
final LinkedList q = new LinkedList(); |
418 |
|
q.add(new Integer(1)); |
419 |
|
q.add(new Integer(2)); |
420 |
|
q.add(new Integer(3)); |
421 |
|
Iterator it = q.iterator(); |
422 |
< |
it.next(); |
422 |
> |
assertEquals(1, it.next()); |
423 |
|
it.remove(); |
424 |
|
it = q.iterator(); |
425 |
< |
assertEquals(it.next(), new Integer(2)); |
426 |
< |
assertEquals(it.next(), new Integer(3)); |
425 |
> |
assertEquals(2, it.next()); |
426 |
> |
assertEquals(3, it.next()); |
427 |
|
assertFalse(it.hasNext()); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
431 |
< |
* Descending iterator iterates through all elements |
431 |
> |
* Descending iterator iterates through all elements |
432 |
|
*/ |
433 |
|
public void testDescendingIterator() { |
434 |
|
LinkedList q = populatedQueue(SIZE); |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
< |
* Descending iterator ordering is reverse FIFO |
450 |
> |
* Descending iterator ordering is reverse FIFO |
451 |
|
*/ |
452 |
|
public void testDescendingIteratorOrdering() { |
453 |
|
final LinkedList q = new LinkedList(); |
456 |
|
q.add(new Integer(1)); |
457 |
|
int k = 0; |
458 |
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
459 |
< |
int i = ((Integer)(it.next())).intValue(); |
445 |
< |
assertEquals(++k, i); |
459 |
> |
assertEquals(++k, it.next()); |
460 |
|
} |
461 |
|
|
462 |
|
assertEquals(3, k); |
465 |
|
/** |
466 |
|
* descendingIterator.remove removes current element |
467 |
|
*/ |
468 |
< |
public void testDescendingIteratorRemove () { |
468 |
> |
public void testDescendingIteratorRemove() { |
469 |
|
final LinkedList q = new LinkedList(); |
470 |
< |
q.add(new Integer(3)); |
471 |
< |
q.add(new Integer(2)); |
472 |
< |
q.add(new Integer(1)); |
470 |
> |
q.add(three); |
471 |
> |
q.add(two); |
472 |
> |
q.add(one); |
473 |
|
Iterator it = q.descendingIterator(); |
474 |
|
it.next(); |
475 |
|
it.remove(); |
476 |
|
it = q.descendingIterator(); |
477 |
< |
assertEquals(it.next(), new Integer(2)); |
478 |
< |
assertEquals(it.next(), new Integer(3)); |
477 |
> |
assertSame(it.next(), two); |
478 |
> |
assertSame(it.next(), three); |
479 |
|
assertFalse(it.hasNext()); |
480 |
|
} |
481 |
|
|
468 |
– |
|
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 |
|
|
496 |
|
public void testAddFirst() { |
497 |
|
LinkedList q = populatedQueue(3); |
498 |
|
q.addFirst(four); |
499 |
< |
assertEquals(four,q.peek()); |
499 |
> |
assertSame(four, q.peek()); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
504 |
|
*/ |
505 |
|
public void testPush() { |
506 |
|
LinkedList q = populatedQueue(3); |
494 |
– |
q.pollLast(); |
507 |
|
q.push(four); |
508 |
< |
assertEquals(four,q.peekFirst()); |
508 |
> |
assertSame(four, q.peekFirst()); |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
< |
* pop removes next element, or throws NSEE if empty |
512 |
> |
* pop removes next element, or throws NSEE if empty |
513 |
|
*/ |
514 |
|
public void testPop() { |
515 |
|
LinkedList q = populatedQueue(SIZE); |
516 |
|
for (int i = 0; i < SIZE; ++i) { |
517 |
< |
assertEquals(i, ((Integer)q.pop()).intValue()); |
517 |
> |
assertEquals(i, q.pop()); |
518 |
|
} |
519 |
|
try { |
520 |
|
q.pop(); |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
< |
* pollLast succeeds unless empty |
544 |
> |
* pollLast succeeds unless empty |
545 |
|
*/ |
546 |
|
public void testPollLast() { |
547 |
|
LinkedList q = populatedQueue(SIZE); |
548 |
|
for (int i = SIZE-1; i >= 0; --i) { |
549 |
< |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
549 |
> |
assertEquals(i, q.pollLast()); |
550 |
|
} |
551 |
|
assertNull(q.pollLast()); |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
< |
* peekFirst returns next element, or null if empty |
555 |
> |
* peekFirst returns next element, or null if empty |
556 |
|
*/ |
557 |
|
public void testPeekFirst() { |
558 |
|
LinkedList q = populatedQueue(SIZE); |
559 |
|
for (int i = 0; i < SIZE; ++i) { |
560 |
< |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
561 |
< |
q.pollFirst(); |
560 |
> |
assertEquals(i, q.peekFirst()); |
561 |
> |
assertEquals(i, q.pollFirst()); |
562 |
|
assertTrue(q.peekFirst() == null || |
563 |
< |
i != ((Integer)q.peekFirst()).intValue()); |
563 |
> |
!q.peekFirst().equals(i)); |
564 |
|
} |
565 |
|
assertNull(q.peekFirst()); |
566 |
|
} |
567 |
|
|
556 |
– |
|
568 |
|
/** |
569 |
< |
* peekLast returns next element, or null if empty |
569 |
> |
* peekLast returns next element, or null if empty |
570 |
|
*/ |
571 |
|
public void testPeekLast() { |
572 |
|
LinkedList q = populatedQueue(SIZE); |
573 |
|
for (int i = SIZE-1; i >= 0; --i) { |
574 |
< |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
575 |
< |
q.pollLast(); |
574 |
> |
assertEquals(i, q.peekLast()); |
575 |
> |
assertEquals(i, q.pollLast()); |
576 |
|
assertTrue(q.peekLast() == null || |
577 |
< |
i != ((Integer)q.peekLast()).intValue()); |
577 |
> |
!q.peekLast().equals(i)); |
578 |
|
} |
579 |
|
assertNull(q.peekLast()); |
580 |
|
} |
582 |
|
public void testFirstElement() { |
583 |
|
LinkedList q = populatedQueue(SIZE); |
584 |
|
for (int i = 0; i < SIZE; ++i) { |
585 |
< |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
586 |
< |
q.pollFirst(); |
585 |
> |
assertEquals(i, q.getFirst()); |
586 |
> |
assertEquals(i, q.pollFirst()); |
587 |
|
} |
588 |
|
try { |
589 |
|
q.getFirst(); |
592 |
|
} |
593 |
|
|
594 |
|
/** |
595 |
< |
* getLast returns next element, or throws NSEE if empty |
595 |
> |
* getLast returns next element, or throws NSEE if empty |
596 |
|
*/ |
597 |
|
public void testLastElement() { |
598 |
|
LinkedList q = populatedQueue(SIZE); |
599 |
|
for (int i = SIZE-1; i >= 0; --i) { |
600 |
< |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
601 |
< |
q.pollLast(); |
600 |
> |
assertEquals(i, q.getLast()); |
601 |
> |
assertEquals(i, q.pollLast()); |
602 |
|
} |
603 |
|
try { |
604 |
|
q.getLast(); |
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 |
|
} |