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.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collection; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.LinkedList; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
|
|
16 |
|
public class LinkedListTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
|
21 |
|
public static Test suite() { |
23 |
|
} |
24 |
|
|
25 |
|
/** |
26 |
< |
* Create a queue of given size containing consecutive |
26 |
> |
* Returns a new queue of given size containing consecutive |
27 |
|
* Integers 0 ... n. |
28 |
|
*/ |
29 |
< |
private LinkedList populatedQueue(int n) { |
30 |
< |
LinkedList q = new LinkedList(); |
29 |
> |
private LinkedList<Integer> populatedQueue(int n) { |
30 |
> |
LinkedList<Integer> q = new LinkedList<Integer>(); |
31 |
|
assertTrue(q.isEmpty()); |
32 |
|
for (int i = 0; i < n; ++i) |
33 |
|
assertTrue(q.offer(new Integer(i))); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
180 |
< |
* poll succeeds unless empty |
180 |
> |
* poll succeeds unless empty |
181 |
|
*/ |
182 |
|
public void testPoll() { |
183 |
|
LinkedList q = populatedQueue(SIZE); |
188 |
|
} |
189 |
|
|
190 |
|
/** |
191 |
< |
* peek returns next element, or null if empty |
191 |
> |
* peek returns next element, or null if empty |
192 |
|
*/ |
193 |
|
public void testPeek() { |
194 |
|
LinkedList q = populatedQueue(SIZE); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* remove removes next element, or throws NSEE if empty |
220 |
> |
* remove removes next element, or throws NSEE if empty |
221 |
|
*/ |
222 |
|
public void testRemove() { |
223 |
|
LinkedList q = populatedQueue(SIZE); |
236 |
|
public void testRemoveElement() { |
237 |
|
LinkedList q = populatedQueue(SIZE); |
238 |
|
for (int i = 1; i < SIZE; i+=2) { |
239 |
< |
assertTrue(q.remove(new Integer(i))); |
239 |
> |
assertTrue(q.contains(i)); |
240 |
> |
assertTrue(q.remove((Integer)i)); |
241 |
> |
assertFalse(q.contains(i)); |
242 |
> |
assertTrue(q.contains(i-1)); |
243 |
|
} |
244 |
|
for (int i = 0; i < SIZE; i+=2) { |
245 |
< |
assertTrue(q.remove(new Integer(i))); |
246 |
< |
assertFalse(q.remove(new Integer(i+1))); |
245 |
> |
assertTrue(q.contains(i)); |
246 |
> |
assertTrue(q.remove((Integer)i)); |
247 |
> |
assertFalse(q.contains(i)); |
248 |
> |
assertFalse(q.remove((Integer)(i+1))); |
249 |
> |
assertFalse(q.contains(i+1)); |
250 |
|
} |
251 |
|
assertTrue(q.isEmpty()); |
252 |
|
} |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* toArray contains all elements |
330 |
> |
* toArray contains all elements in FIFO order |
331 |
|
*/ |
332 |
|
public void testToArray() { |
333 |
|
LinkedList q = populatedQueue(SIZE); |
334 |
|
Object[] o = q.toArray(); |
326 |
– |
Arrays.sort(o); |
335 |
|
for (int i = 0; i < o.length; i++) |
336 |
< |
assertEquals(o[i], q.poll()); |
336 |
> |
assertSame(o[i], q.poll()); |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
< |
* toArray(a) contains all elements |
340 |
> |
* toArray(a) contains all elements in FIFO order |
341 |
|
*/ |
342 |
|
public void testToArray2() { |
343 |
< |
LinkedList q = populatedQueue(SIZE); |
343 |
> |
LinkedList<Integer> q = populatedQueue(SIZE); |
344 |
|
Integer[] ints = new Integer[SIZE]; |
345 |
< |
ints = (Integer[])q.toArray(ints); |
346 |
< |
Arrays.sort(ints); |
345 |
> |
Integer[] array = q.toArray(ints); |
346 |
> |
assertSame(ints, array); |
347 |
|
for (int i = 0; i < ints.length; i++) |
348 |
< |
assertEquals(ints[i], q.poll()); |
348 |
> |
assertSame(ints[i], q.poll()); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
352 |
< |
* toArray(null) throws NPE |
352 |
> |
* toArray(null) throws NullPointerException |
353 |
|
*/ |
354 |
< |
public void testToArray_BadArg() { |
354 |
> |
public void testToArray_NullArg() { |
355 |
|
LinkedList l = new LinkedList(); |
356 |
|
l.add(new Object()); |
357 |
|
try { |
358 |
< |
Object o[] = l.toArray(null); |
358 |
> |
l.toArray(null); |
359 |
|
shouldThrow(); |
360 |
|
} catch (NullPointerException success) {} |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
< |
* toArray with incompatible array type throws CCE |
364 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
365 |
|
*/ |
366 |
|
public void testToArray1_BadArg() { |
367 |
|
LinkedList l = new LinkedList(); |
368 |
|
l.add(new Integer(5)); |
369 |
|
try { |
370 |
< |
Object o[] = l.toArray(new String[10]); |
370 |
> |
l.toArray(new String[10]); |
371 |
|
shouldThrow(); |
372 |
|
} catch (ArrayStoreException success) {} |
373 |
|
} |
374 |
|
|
375 |
|
/** |
376 |
< |
* iterator iterates through all elements |
376 |
> |
* iterator iterates through all elements |
377 |
|
*/ |
378 |
|
public void testIterator() { |
379 |
|
LinkedList q = populatedQueue(SIZE); |
387 |
|
} |
388 |
|
|
389 |
|
/** |
390 |
< |
* iterator ordering is FIFO |
390 |
> |
* iterator ordering is FIFO |
391 |
|
*/ |
392 |
|
public void testIteratorOrdering() { |
393 |
|
final LinkedList q = new LinkedList(); |
405 |
|
/** |
406 |
|
* iterator.remove removes current element |
407 |
|
*/ |
408 |
< |
public void testIteratorRemove () { |
408 |
> |
public void testIteratorRemove() { |
409 |
|
final LinkedList q = new LinkedList(); |
410 |
|
q.add(new Integer(1)); |
411 |
|
q.add(new Integer(2)); |
412 |
|
q.add(new Integer(3)); |
413 |
|
Iterator it = q.iterator(); |
414 |
< |
assertEquals(it.next(), 1); |
414 |
> |
assertEquals(1, it.next()); |
415 |
|
it.remove(); |
416 |
|
it = q.iterator(); |
417 |
< |
assertEquals(it.next(), 2); |
418 |
< |
assertEquals(it.next(), 3); |
417 |
> |
assertEquals(2, it.next()); |
418 |
> |
assertEquals(3, it.next()); |
419 |
|
assertFalse(it.hasNext()); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* Descending iterator iterates through all elements |
423 |
> |
* Descending iterator iterates through all elements |
424 |
|
*/ |
425 |
|
public void testDescendingIterator() { |
426 |
|
LinkedList q = populatedQueue(SIZE); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
< |
* Descending iterator ordering is reverse FIFO |
442 |
> |
* Descending iterator ordering is reverse FIFO |
443 |
|
*/ |
444 |
|
public void testDescendingIteratorOrdering() { |
445 |
|
final LinkedList q = new LinkedList(); |
457 |
|
/** |
458 |
|
* descendingIterator.remove removes current element |
459 |
|
*/ |
460 |
< |
public void testDescendingIteratorRemove () { |
460 |
> |
public void testDescendingIteratorRemove() { |
461 |
|
final LinkedList q = new LinkedList(); |
462 |
|
q.add(three); |
463 |
|
q.add(two); |
471 |
|
assertFalse(it.hasNext()); |
472 |
|
} |
473 |
|
|
466 |
– |
|
474 |
|
/** |
475 |
|
* toString contains toStrings of elements |
476 |
|
*/ |
478 |
|
LinkedList q = populatedQueue(SIZE); |
479 |
|
String s = q.toString(); |
480 |
|
for (int i = 0; i < SIZE; ++i) { |
481 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
481 |
> |
assertTrue(s.contains(String.valueOf(i))); |
482 |
|
} |
483 |
|
} |
484 |
|
|
501 |
|
} |
502 |
|
|
503 |
|
/** |
504 |
< |
* pop removes next element, or throws NSEE if empty |
504 |
> |
* pop removes next element, or throws NSEE if empty |
505 |
|
*/ |
506 |
|
public void testPop() { |
507 |
|
LinkedList q = populatedQueue(SIZE); |
533 |
|
} |
534 |
|
|
535 |
|
/** |
536 |
< |
* pollLast succeeds unless empty |
536 |
> |
* pollLast succeeds unless empty |
537 |
|
*/ |
538 |
|
public void testPollLast() { |
539 |
|
LinkedList q = populatedQueue(SIZE); |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
< |
* peekFirst returns next element, or null if empty |
547 |
> |
* peekFirst returns next element, or null if empty |
548 |
|
*/ |
549 |
|
public void testPeekFirst() { |
550 |
|
LinkedList q = populatedQueue(SIZE); |
557 |
|
assertNull(q.peekFirst()); |
558 |
|
} |
559 |
|
|
553 |
– |
|
560 |
|
/** |
561 |
< |
* peekLast returns next element, or null if empty |
561 |
> |
* peekLast returns next element, or null if empty |
562 |
|
*/ |
563 |
|
public void testPeekLast() { |
564 |
|
LinkedList q = populatedQueue(SIZE); |
584 |
|
} |
585 |
|
|
586 |
|
/** |
587 |
< |
* getLast returns next element, or throws NSEE if empty |
587 |
> |
* getLast returns next element, or throws NSEE if empty |
588 |
|
*/ |
589 |
|
public void testLastElement() { |
590 |
|
LinkedList q = populatedQueue(SIZE); |