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 |
|
*/ |
12 |
|
|
13 |
|
public class LinkedListTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
15 |
< |
junit.textui.TestRunner.run (suite()); |
15 |
> |
junit.textui.TestRunner.run(suite()); |
16 |
|
} |
17 |
|
|
18 |
|
public static Test suite() { |
23 |
|
* Create a queue of given size containing consecutive |
24 |
|
* Integers 0 ... n. |
25 |
|
*/ |
26 |
< |
private LinkedList populatedQueue(int n) { |
27 |
< |
LinkedList q = new LinkedList(); |
26 |
> |
private LinkedList<Integer> populatedQueue(int n) { |
27 |
> |
LinkedList<Integer> q = new LinkedList<Integer>(); |
28 |
|
assertTrue(q.isEmpty()); |
29 |
|
for (int i = 0; i < n; ++i) |
30 |
|
assertTrue(q.offer(new Integer(i))); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* poll succeeds unless empty |
177 |
> |
* poll succeeds unless empty |
178 |
|
*/ |
179 |
|
public void testPoll() { |
180 |
|
LinkedList q = populatedQueue(SIZE); |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
< |
* peek returns next element, or null if empty |
188 |
> |
* peek returns next element, or null if empty |
189 |
|
*/ |
190 |
|
public void testPeek() { |
191 |
|
LinkedList q = populatedQueue(SIZE); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
< |
* remove removes next element, or throws NSEE if empty |
217 |
> |
* remove removes next element, or throws NSEE if empty |
218 |
|
*/ |
219 |
|
public void testRemove() { |
220 |
|
LinkedList q = populatedQueue(SIZE); |
233 |
|
public void testRemoveElement() { |
234 |
|
LinkedList q = populatedQueue(SIZE); |
235 |
|
for (int i = 1; i < SIZE; i+=2) { |
236 |
< |
assertTrue(q.remove(new Integer(i))); |
236 |
> |
assertTrue(q.contains(i)); |
237 |
> |
assertTrue(q.remove((Integer)i)); |
238 |
> |
assertFalse(q.contains(i)); |
239 |
> |
assertTrue(q.contains(i-1)); |
240 |
|
} |
241 |
|
for (int i = 0; i < SIZE; i+=2) { |
242 |
< |
assertTrue(q.remove(new Integer(i))); |
243 |
< |
assertFalse(q.remove(new Integer(i+1))); |
242 |
> |
assertTrue(q.contains(i)); |
243 |
> |
assertTrue(q.remove((Integer)i)); |
244 |
> |
assertFalse(q.contains(i)); |
245 |
> |
assertFalse(q.remove((Integer)(i+1))); |
246 |
> |
assertFalse(q.contains(i+1)); |
247 |
|
} |
248 |
|
assertTrue(q.isEmpty()); |
249 |
|
} |
324 |
|
} |
325 |
|
|
326 |
|
/** |
327 |
< |
* toArray contains all elements |
327 |
> |
* toArray contains all elements in FIFO order |
328 |
|
*/ |
329 |
|
public void testToArray() { |
330 |
|
LinkedList q = populatedQueue(SIZE); |
331 |
|
Object[] o = q.toArray(); |
326 |
– |
Arrays.sort(o); |
332 |
|
for (int i = 0; i < o.length; i++) |
333 |
< |
assertEquals(o[i], q.poll()); |
333 |
> |
assertSame(o[i], q.poll()); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* toArray(a) contains all elements |
337 |
> |
* toArray(a) contains all elements in FIFO order |
338 |
|
*/ |
339 |
|
public void testToArray2() { |
340 |
< |
LinkedList q = populatedQueue(SIZE); |
340 |
> |
LinkedList<Integer> q = populatedQueue(SIZE); |
341 |
|
Integer[] ints = new Integer[SIZE]; |
342 |
< |
ints = (Integer[])q.toArray(ints); |
343 |
< |
Arrays.sort(ints); |
342 |
> |
Integer[] array = q.toArray(ints); |
343 |
> |
assertSame(ints, array); |
344 |
|
for (int i = 0; i < ints.length; i++) |
345 |
< |
assertEquals(ints[i], q.poll()); |
345 |
> |
assertSame(ints[i], q.poll()); |
346 |
|
} |
347 |
|
|
348 |
|
/** |
349 |
< |
* toArray(null) throws NPE |
349 |
> |
* toArray(null) throws NullPointerException |
350 |
|
*/ |
351 |
< |
public void testToArray_BadArg() { |
351 |
> |
public void testToArray_NullArg() { |
352 |
|
LinkedList l = new LinkedList(); |
353 |
|
l.add(new Object()); |
354 |
|
try { |
355 |
< |
Object o[] = l.toArray(null); |
355 |
> |
l.toArray(null); |
356 |
|
shouldThrow(); |
357 |
|
} catch (NullPointerException success) {} |
358 |
|
} |
359 |
|
|
360 |
|
/** |
361 |
< |
* toArray with incompatible array type throws CCE |
361 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
362 |
|
*/ |
363 |
|
public void testToArray1_BadArg() { |
364 |
|
LinkedList l = new LinkedList(); |
365 |
|
l.add(new Integer(5)); |
366 |
|
try { |
367 |
< |
Object o[] = l.toArray(new String[10]); |
367 |
> |
l.toArray(new String[10]); |
368 |
|
shouldThrow(); |
369 |
|
} catch (ArrayStoreException success) {} |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
< |
* iterator iterates through all elements |
373 |
> |
* iterator iterates through all elements |
374 |
|
*/ |
375 |
|
public void testIterator() { |
376 |
|
LinkedList q = populatedQueue(SIZE); |
384 |
|
} |
385 |
|
|
386 |
|
/** |
387 |
< |
* iterator ordering is FIFO |
387 |
> |
* iterator ordering is FIFO |
388 |
|
*/ |
389 |
|
public void testIteratorOrdering() { |
390 |
|
final LinkedList q = new LinkedList(); |
402 |
|
/** |
403 |
|
* iterator.remove removes current element |
404 |
|
*/ |
405 |
< |
public void testIteratorRemove () { |
405 |
> |
public void testIteratorRemove() { |
406 |
|
final LinkedList q = new LinkedList(); |
407 |
|
q.add(new Integer(1)); |
408 |
|
q.add(new Integer(2)); |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
< |
* Descending iterator iterates through all elements |
420 |
> |
* Descending iterator iterates through all elements |
421 |
|
*/ |
422 |
|
public void testDescendingIterator() { |
423 |
|
LinkedList q = populatedQueue(SIZE); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
< |
* Descending iterator ordering is reverse FIFO |
439 |
> |
* Descending iterator ordering is reverse FIFO |
440 |
|
*/ |
441 |
|
public void testDescendingIteratorOrdering() { |
442 |
|
final LinkedList q = new LinkedList(); |
454 |
|
/** |
455 |
|
* descendingIterator.remove removes current element |
456 |
|
*/ |
457 |
< |
public void testDescendingIteratorRemove () { |
457 |
> |
public void testDescendingIteratorRemove() { |
458 |
|
final LinkedList q = new LinkedList(); |
459 |
|
q.add(three); |
460 |
|
q.add(two); |
468 |
|
assertFalse(it.hasNext()); |
469 |
|
} |
470 |
|
|
466 |
– |
|
471 |
|
/** |
472 |
|
* toString contains toStrings of elements |
473 |
|
*/ |
475 |
|
LinkedList q = populatedQueue(SIZE); |
476 |
|
String s = q.toString(); |
477 |
|
for (int i = 0; i < SIZE; ++i) { |
478 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
478 |
> |
assertTrue(s.contains(String.valueOf(i))); |
479 |
|
} |
480 |
|
} |
481 |
|
|
498 |
|
} |
499 |
|
|
500 |
|
/** |
501 |
< |
* pop removes next element, or throws NSEE if empty |
501 |
> |
* pop removes next element, or throws NSEE if empty |
502 |
|
*/ |
503 |
|
public void testPop() { |
504 |
|
LinkedList q = populatedQueue(SIZE); |
530 |
|
} |
531 |
|
|
532 |
|
/** |
533 |
< |
* pollLast succeeds unless empty |
533 |
> |
* pollLast succeeds unless empty |
534 |
|
*/ |
535 |
|
public void testPollLast() { |
536 |
|
LinkedList q = populatedQueue(SIZE); |
541 |
|
} |
542 |
|
|
543 |
|
/** |
544 |
< |
* peekFirst returns next element, or null if empty |
544 |
> |
* peekFirst returns next element, or null if empty |
545 |
|
*/ |
546 |
|
public void testPeekFirst() { |
547 |
|
LinkedList q = populatedQueue(SIZE); |
554 |
|
assertNull(q.peekFirst()); |
555 |
|
} |
556 |
|
|
553 |
– |
|
557 |
|
/** |
558 |
< |
* peekLast returns next element, or null if empty |
558 |
> |
* peekLast returns next element, or null if empty |
559 |
|
*/ |
560 |
|
public void testPeekLast() { |
561 |
|
LinkedList q = populatedQueue(SIZE); |
581 |
|
} |
582 |
|
|
583 |
|
/** |
584 |
< |
* getLast returns next element, or throws NSEE if empty |
584 |
> |
* getLast returns next element, or throws NSEE if empty |
585 |
|
*/ |
586 |
|
public void testLastElement() { |
587 |
|
LinkedList q = populatedQueue(SIZE); |