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 |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
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() { |
26 |
|
private LinkedList populatedQueue(int n) { |
27 |
|
LinkedList q = new LinkedList(); |
28 |
|
assertTrue(q.isEmpty()); |
29 |
< |
for(int i = 0; i < n; ++i) |
29 |
> |
for (int i = 0; i < n; ++i) |
30 |
|
assertTrue(q.offer(new Integer(i))); |
31 |
|
assertFalse(q.isEmpty()); |
32 |
|
assertEquals(n, q.size()); |
33 |
|
return q; |
34 |
|
} |
35 |
< |
|
35 |
> |
|
36 |
|
/** |
37 |
|
* new queue is empty |
38 |
|
*/ |
103 |
|
try { |
104 |
|
LinkedList q = new LinkedList(); |
105 |
|
q.offer(null); |
106 |
< |
} catch (NullPointerException ie) { |
106 |
> |
} catch (NullPointerException ie) { |
107 |
|
unexpectedException(); |
108 |
< |
} |
108 |
> |
} |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* Offer succeeds |
112 |
> |
* Offer succeeds |
113 |
|
*/ |
114 |
|
public void testOffer() { |
115 |
|
LinkedList q = new LinkedList(); |
169 |
|
m.add(new Object()); |
170 |
|
l.addAll(4,m); |
171 |
|
shouldThrow(); |
172 |
< |
} catch(IndexOutOfBoundsException success) {} |
172 |
> |
} catch (IndexOutOfBoundsException success) {} |
173 |
|
} |
174 |
|
|
175 |
|
/** |
183 |
|
m.add(new Object()); |
184 |
|
l.addAll(-1,m); |
185 |
|
shouldThrow(); |
186 |
< |
} catch(IndexOutOfBoundsException success){} |
186 |
> |
} catch (IndexOutOfBoundsException success) {} |
187 |
|
} |
188 |
|
|
189 |
|
/** |
238 |
|
try { |
239 |
|
q.remove(); |
240 |
|
shouldThrow(); |
241 |
< |
} catch (NoSuchElementException success){ |
242 |
< |
} |
241 |
> |
} catch (NoSuchElementException success) { |
242 |
> |
} |
243 |
|
} |
244 |
|
|
245 |
|
/** |
256 |
|
} |
257 |
|
assertTrue(q.isEmpty()); |
258 |
|
} |
259 |
< |
|
259 |
> |
|
260 |
|
/** |
261 |
|
* contains(x) reports true when elements added but not yet removed |
262 |
|
*/ |
339 |
|
LinkedList q = populatedQueue(SIZE); |
340 |
|
Object[] o = q.toArray(); |
341 |
|
Arrays.sort(o); |
342 |
< |
for(int i = 0; i < o.length; i++) |
342 |
> |
for (int i = 0; i < o.length; i++) |
343 |
|
assertEquals(o[i], q.poll()); |
344 |
|
} |
345 |
|
|
351 |
|
Integer[] ints = new Integer[SIZE]; |
352 |
|
ints = (Integer[])q.toArray(ints); |
353 |
|
Arrays.sort(ints); |
354 |
< |
for(int i = 0; i < ints.length; i++) |
354 |
> |
for (int i = 0; i < ints.length; i++) |
355 |
|
assertEquals(ints[i], q.poll()); |
356 |
|
} |
357 |
|
|
364 |
|
l.add(new Object()); |
365 |
|
Object o[] = l.toArray(null); |
366 |
|
shouldThrow(); |
367 |
< |
} catch(NullPointerException success){} |
367 |
> |
} catch (NullPointerException success) {} |
368 |
|
} |
369 |
|
|
370 |
|
/** |
376 |
|
l.add(new Integer(5)); |
377 |
|
Object o[] = l.toArray(new String[10] ); |
378 |
|
shouldThrow(); |
379 |
< |
} catch(ArrayStoreException success){} |
379 |
> |
} catch (ArrayStoreException success) {} |
380 |
|
} |
381 |
< |
|
381 |
> |
|
382 |
|
/** |
383 |
|
* iterator iterates through all elements |
384 |
|
*/ |
386 |
|
LinkedList q = populatedQueue(SIZE); |
387 |
|
int i = 0; |
388 |
|
Iterator it = q.iterator(); |
389 |
< |
while(it.hasNext()) { |
389 |
> |
while (it.hasNext()) { |
390 |
|
assertTrue(q.contains(it.next())); |
391 |
|
++i; |
392 |
|
} |
427 |
|
assertFalse(it.hasNext()); |
428 |
|
} |
429 |
|
|
430 |
+ |
/** |
431 |
+ |
* Descending iterator iterates through all elements |
432 |
+ |
*/ |
433 |
+ |
public void testDescendingIterator() { |
434 |
+ |
LinkedList q = populatedQueue(SIZE); |
435 |
+ |
int i = 0; |
436 |
+ |
Iterator it = q.descendingIterator(); |
437 |
+ |
while (it.hasNext()) { |
438 |
+ |
assertTrue(q.contains(it.next())); |
439 |
+ |
++i; |
440 |
+ |
} |
441 |
+ |
assertEquals(i, SIZE); |
442 |
+ |
assertFalse(it.hasNext()); |
443 |
+ |
try { |
444 |
+ |
it.next(); |
445 |
+ |
} catch (NoSuchElementException success) { |
446 |
+ |
} |
447 |
+ |
} |
448 |
+ |
|
449 |
+ |
/** |
450 |
+ |
* Descending iterator ordering is reverse FIFO |
451 |
+ |
*/ |
452 |
+ |
public void testDescendingIteratorOrdering() { |
453 |
+ |
final LinkedList q = new LinkedList(); |
454 |
+ |
q.add(new Integer(3)); |
455 |
+ |
q.add(new Integer(2)); |
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(); |
460 |
+ |
assertEquals(++k, i); |
461 |
+ |
} |
462 |
+ |
|
463 |
+ |
assertEquals(3, k); |
464 |
+ |
} |
465 |
+ |
|
466 |
+ |
/** |
467 |
+ |
* descendingIterator.remove removes current element |
468 |
+ |
*/ |
469 |
+ |
public void testDescendingIteratorRemove () { |
470 |
+ |
final LinkedList q = new LinkedList(); |
471 |
+ |
q.add(new Integer(3)); |
472 |
+ |
q.add(new Integer(2)); |
473 |
+ |
q.add(new Integer(1)); |
474 |
+ |
Iterator it = q.descendingIterator(); |
475 |
+ |
it.next(); |
476 |
+ |
it.remove(); |
477 |
+ |
it = q.descendingIterator(); |
478 |
+ |
assertEquals(it.next(), new Integer(2)); |
479 |
+ |
assertEquals(it.next(), new Integer(3)); |
480 |
+ |
assertFalse(it.hasNext()); |
481 |
+ |
} |
482 |
+ |
|
483 |
|
|
484 |
|
/** |
485 |
|
* toString contains toStrings of elements |
490 |
|
for (int i = 0; i < SIZE; ++i) { |
491 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
492 |
|
} |
493 |
< |
} |
493 |
> |
} |
494 |
|
|
495 |
|
/** |
496 |
|
* peek returns element inserted with addFirst |
499 |
|
LinkedList q = populatedQueue(3); |
500 |
|
q.addFirst(four); |
501 |
|
assertEquals(four,q.peek()); |
502 |
< |
} |
502 |
> |
} |
503 |
|
|
504 |
|
/** |
505 |
|
* peekFirst returns element inserted with push |
509 |
|
q.pollLast(); |
510 |
|
q.push(four); |
511 |
|
assertEquals(four,q.peekFirst()); |
512 |
< |
} |
512 |
> |
} |
513 |
|
|
514 |
|
/** |
515 |
|
* pop removes next element, or throws NSEE if empty |
522 |
|
try { |
523 |
|
q.pop(); |
524 |
|
shouldThrow(); |
525 |
< |
} catch (NoSuchElementException success){ |
526 |
< |
} |
525 |
> |
} catch (NoSuchElementException success) { |
526 |
> |
} |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* OfferFirst succeeds |
530 |
> |
* OfferFirst succeeds |
531 |
|
*/ |
532 |
|
public void testOfferFirst() { |
533 |
|
LinkedList q = new LinkedList(); |
536 |
|
} |
537 |
|
|
538 |
|
/** |
539 |
< |
* OfferLast succeeds |
539 |
> |
* OfferLast succeeds |
540 |
|
*/ |
541 |
|
public void testOfferLast() { |
542 |
|
LinkedList q = new LinkedList(); |