1 |
/* |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/licenses/publicdomain |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
* Pat Fisher, Mike Judd. |
7 |
*/ |
*/ |
8 |
|
|
9 |
import junit.framework.*; |
import java.util.Arrays; |
10 |
import java.util.*; |
import java.util.Collection; |
11 |
import java.util.concurrent.*; |
import java.util.Iterator; |
12 |
|
import java.util.LinkedList; |
13 |
|
import java.util.NoSuchElementException; |
14 |
|
|
15 |
|
import junit.framework.Test; |
16 |
|
|
17 |
public class LinkedListTest extends JSR166TestCase { |
public class LinkedListTest extends JSR166TestCase { |
18 |
public static void main(String[] args) { |
public static void main(String[] args) { |
19 |
junit.textui.TestRunner.run (suite()); |
main(suite(), args); |
20 |
} |
} |
21 |
|
|
22 |
public static Test suite() { |
public static Test suite() { |
23 |
return new TestSuite(LinkedListTest.class); |
class Implementation implements CollectionImplementation { |
24 |
|
public Class<?> klazz() { return LinkedList.class; } |
25 |
|
public Collection emptyCollection() { return new LinkedList(); } |
26 |
|
public Object makeElement(int i) { return i; } |
27 |
|
public boolean isConcurrent() { return false; } |
28 |
|
public boolean permitsNulls() { return true; } |
29 |
|
} |
30 |
|
class SubListImplementation extends Implementation { |
31 |
|
public Collection emptyCollection() { |
32 |
|
return new LinkedList().subList(0, 0); |
33 |
|
} |
34 |
|
} |
35 |
|
return newTestSuite( |
36 |
|
LinkedListTest.class, |
37 |
|
CollectionTest.testSuite(new Implementation()), |
38 |
|
CollectionTest.testSuite(new SubListImplementation())); |
39 |
} |
} |
40 |
|
|
41 |
/** |
/** |
42 |
* Create a queue of given size containing consecutive |
* Returns a new queue of given size containing consecutive |
43 |
* Integers 0 ... n. |
* Integers 0 ... n - 1. |
44 |
*/ |
*/ |
45 |
private LinkedList populatedQueue(int n) { |
private LinkedList<Integer> populatedQueue(int n) { |
46 |
LinkedList q = new LinkedList(); |
LinkedList<Integer> q = new LinkedList<>(); |
47 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
48 |
for(int i = 0; i < n; ++i) |
for(int i = 0; i < n; ++i) |
49 |
assertTrue(q.offer(new Integer(i))); |
assertTrue(q.offer(new Integer(i))); |
50 |
assertFalse(q.isEmpty()); |
assertFalse(q.isEmpty()); |
51 |
assertEquals(n, q.size()); |
assertEquals(n, q.size()); |
52 |
|
assertEquals((Integer) 0, q.peekFirst()); |
53 |
|
assertEquals((Integer) (n - 1), q.peekLast()); |
54 |
return q; |
return q; |
55 |
} |
} |
56 |
|
|
66 |
*/ |
*/ |
67 |
public void testConstructor3() { |
public void testConstructor3() { |
68 |
try { |
try { |
69 |
LinkedList q = new LinkedList((Collection)null); |
new LinkedList((Collection)null); |
70 |
shouldThrow(); |
shouldThrow(); |
71 |
} |
} catch (NullPointerException success) {} |
|
catch (NullPointerException success) {} |
|
72 |
} |
} |
73 |
|
|
74 |
/** |
/** |
75 |
* Queue contains all elements of collection used to initialize |
* Queue contains all elements of collection used to initialize |
|
|
|
76 |
*/ |
*/ |
77 |
public void testConstructor6() { |
public void testConstructor6() { |
|
try { |
|
78 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
79 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
80 |
ints[i] = new Integer(i); |
ints[i] = i; |
81 |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
82 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
83 |
assertEquals(ints[i], q.poll()); |
assertEquals(ints[i], q.poll()); |
84 |
} |
} |
|
finally {} |
|
|
} |
|
85 |
|
|
86 |
/** |
/** |
87 |
* isEmpty is true before add, false after |
* isEmpty is true before add, false after |
116 |
* offer(null) succeeds |
* offer(null) succeeds |
117 |
*/ |
*/ |
118 |
public void testOfferNull() { |
public void testOfferNull() { |
|
try { |
|
119 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
120 |
q.offer(null); |
q.offer(null); |
121 |
} catch (NullPointerException ie) { |
assertNull(q.get(0)); |
122 |
unexpectedException(); |
assertTrue(q.contains(null)); |
|
} |
|
123 |
} |
} |
124 |
|
|
125 |
/** |
/** |
146 |
* addAll(null) throws NPE |
* addAll(null) throws NPE |
147 |
*/ |
*/ |
148 |
public void testAddAll1() { |
public void testAddAll1() { |
|
try { |
|
149 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
150 |
|
try { |
151 |
q.addAll(null); |
q.addAll(null); |
152 |
shouldThrow(); |
shouldThrow(); |
153 |
} |
} catch (NullPointerException success) {} |
|
catch (NullPointerException success) {} |
|
154 |
} |
} |
155 |
|
|
156 |
/** |
/** |
157 |
* Queue contains all elements, in traversal order, of successful addAll |
* Queue contains all elements, in traversal order, of successful addAll |
158 |
*/ |
*/ |
159 |
public void testAddAll5() { |
public void testAddAll5() { |
|
try { |
|
160 |
Integer[] empty = new Integer[0]; |
Integer[] empty = new Integer[0]; |
161 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
162 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
163 |
ints[i] = new Integer(i); |
ints[i] = i; |
164 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
165 |
assertFalse(q.addAll(Arrays.asList(empty))); |
assertFalse(q.addAll(Arrays.asList(empty))); |
166 |
assertTrue(q.addAll(Arrays.asList(ints))); |
assertTrue(q.addAll(Arrays.asList(ints))); |
167 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
168 |
assertEquals(ints[i], q.poll()); |
assertEquals(ints[i], q.poll()); |
169 |
} |
} |
|
finally {} |
|
|
} |
|
170 |
|
|
171 |
/** |
/** |
172 |
* addAll with too large an index throws IOOBE |
* addAll with too large an index throws IOOBE |
173 |
*/ |
*/ |
174 |
public void testAddAll2_IndexOutOfBoundsException() { |
public void testAddAll2_IndexOutOfBoundsException() { |
|
try { |
|
175 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
176 |
l.add(new Object()); |
l.add(new Object()); |
177 |
LinkedList m = new LinkedList(); |
LinkedList m = new LinkedList(); |
178 |
m.add(new Object()); |
m.add(new Object()); |
179 |
|
try { |
180 |
l.addAll(4,m); |
l.addAll(4,m); |
181 |
shouldThrow(); |
shouldThrow(); |
182 |
} catch(IndexOutOfBoundsException success) {} |
} catch(IndexOutOfBoundsException success) {} |
186 |
* addAll with negative index throws IOOBE |
* addAll with negative index throws IOOBE |
187 |
*/ |
*/ |
188 |
public void testAddAll4_BadIndex() { |
public void testAddAll4_BadIndex() { |
|
try { |
|
189 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
190 |
l.add(new Object()); |
l.add(new Object()); |
191 |
LinkedList m = new LinkedList(); |
LinkedList m = new LinkedList(); |
192 |
m.add(new Object()); |
m.add(new Object()); |
193 |
|
try { |
194 |
l.addAll(-1,m); |
l.addAll(-1,m); |
195 |
shouldThrow(); |
shouldThrow(); |
196 |
} catch(IndexOutOfBoundsException success){} |
} catch(IndexOutOfBoundsException success){} |
202 |
public void testPoll() { |
public void testPoll() { |
203 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
204 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
205 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
assertEquals(i, q.poll()); |
206 |
} |
} |
207 |
assertNull(q.poll()); |
assertNull(q.poll()); |
208 |
} |
} |
213 |
public void testPeek() { |
public void testPeek() { |
214 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
215 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
216 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
assertEquals(i, q.peek()); |
217 |
q.poll(); |
assertEquals(i, q.poll()); |
218 |
assertTrue(q.peek() == null || |
assertTrue(q.peek() == null || |
219 |
i != ((Integer)q.peek()).intValue()); |
!q.peek().equals(i)); |
220 |
} |
} |
221 |
assertNull(q.peek()); |
assertNull(q.peek()); |
222 |
} |
} |
227 |
public void testElement() { |
public void testElement() { |
228 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
229 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
230 |
assertEquals(i, ((Integer)q.element()).intValue()); |
assertEquals(i, q.element()); |
231 |
q.poll(); |
assertEquals(i, q.poll()); |
232 |
} |
} |
233 |
try { |
try { |
234 |
q.element(); |
q.element(); |
235 |
shouldThrow(); |
shouldThrow(); |
236 |
} |
} catch (NoSuchElementException success) {} |
|
catch (NoSuchElementException success) {} |
|
237 |
} |
} |
238 |
|
|
239 |
/** |
/** |
242 |
public void testRemove() { |
public void testRemove() { |
243 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
244 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
245 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
assertEquals(i, q.remove()); |
246 |
} |
} |
247 |
try { |
try { |
248 |
q.remove(); |
q.remove(); |
249 |
shouldThrow(); |
shouldThrow(); |
250 |
} catch (NoSuchElementException success){ |
} catch (NoSuchElementException success) {} |
|
} |
|
251 |
} |
} |
252 |
|
|
253 |
/** |
/** |
256 |
public void testRemoveElement() { |
public void testRemoveElement() { |
257 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
258 |
for (int i = 1; i < SIZE; i+=2) { |
for (int i = 1; i < SIZE; i+=2) { |
259 |
assertTrue(q.remove(new Integer(i))); |
assertTrue(q.contains(i)); |
260 |
|
assertTrue(q.remove((Integer)i)); |
261 |
|
assertFalse(q.contains(i)); |
262 |
|
assertTrue(q.contains(i - 1)); |
263 |
} |
} |
264 |
for (int i = 0; i < SIZE; i+=2) { |
for (int i = 0; i < SIZE; i+=2) { |
265 |
assertTrue(q.remove(new Integer(i))); |
assertTrue(q.contains(i)); |
266 |
assertFalse(q.remove(new Integer(i+1))); |
assertTrue(q.remove((Integer)i)); |
267 |
|
assertFalse(q.contains(i)); |
268 |
|
assertFalse(q.remove((Integer)(i + 1))); |
269 |
|
assertFalse(q.contains(i + 1)); |
270 |
} |
} |
271 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
272 |
} |
} |
291 |
q.clear(); |
q.clear(); |
292 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
293 |
assertEquals(0, q.size()); |
assertEquals(0, q.size()); |
294 |
q.add(new Integer(1)); |
assertTrue(q.add(new Integer(1))); |
295 |
assertFalse(q.isEmpty()); |
assertFalse(q.isEmpty()); |
296 |
q.clear(); |
q.clear(); |
297 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
306 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
307 |
assertTrue(q.containsAll(p)); |
assertTrue(q.containsAll(p)); |
308 |
assertFalse(p.containsAll(q)); |
assertFalse(p.containsAll(q)); |
309 |
p.add(new Integer(i)); |
assertTrue(p.add(new Integer(i))); |
310 |
} |
} |
311 |
assertTrue(p.containsAll(q)); |
assertTrue(p.containsAll(q)); |
312 |
} |
} |
340 |
assertTrue(q.removeAll(p)); |
assertTrue(q.removeAll(p)); |
341 |
assertEquals(SIZE-i, q.size()); |
assertEquals(SIZE-i, q.size()); |
342 |
for (int j = 0; j < i; ++j) { |
for (int j = 0; j < i; ++j) { |
343 |
Integer I = (Integer)(p.remove()); |
Integer x = (Integer)(p.remove()); |
344 |
assertFalse(q.contains(I)); |
assertFalse(q.contains(x)); |
345 |
} |
} |
346 |
} |
} |
347 |
} |
} |
348 |
|
|
349 |
/** |
/** |
350 |
* toArray contains all elements |
* toArray contains all elements in FIFO order |
351 |
*/ |
*/ |
352 |
public void testToArray() { |
public void testToArray() { |
353 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
354 |
Object[] o = q.toArray(); |
Object[] o = q.toArray(); |
|
Arrays.sort(o); |
|
355 |
for(int i = 0; i < o.length; i++) |
for(int i = 0; i < o.length; i++) |
356 |
assertEquals(o[i], q.poll()); |
assertSame(o[i], q.poll()); |
357 |
} |
} |
358 |
|
|
359 |
/** |
/** |
360 |
* toArray(a) contains all elements |
* toArray(a) contains all elements in FIFO order |
361 |
*/ |
*/ |
362 |
public void testToArray2() { |
public void testToArray2() { |
363 |
LinkedList q = populatedQueue(SIZE); |
LinkedList<Integer> q = populatedQueue(SIZE); |
364 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
365 |
ints = (Integer[])q.toArray(ints); |
Integer[] array = q.toArray(ints); |
366 |
Arrays.sort(ints); |
assertSame(ints, array); |
367 |
for(int i = 0; i < ints.length; i++) |
for(int i = 0; i < ints.length; i++) |
368 |
assertEquals(ints[i], q.poll()); |
assertSame(ints[i], q.poll()); |
369 |
} |
} |
370 |
|
|
371 |
/** |
/** |
372 |
* toArray(null) throws NPE |
* toArray(null) throws NullPointerException |
373 |
*/ |
*/ |
374 |
public void testToArray_BadArg() { |
public void testToArray_NullArg() { |
|
try { |
|
375 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
376 |
l.add(new Object()); |
l.add(new Object()); |
377 |
Object o[] = l.toArray(null); |
try { |
378 |
|
l.toArray(null); |
379 |
shouldThrow(); |
shouldThrow(); |
380 |
} catch(NullPointerException success){} |
} catch(NullPointerException success){} |
381 |
} |
} |
382 |
|
|
383 |
/** |
/** |
384 |
* toArray with incompatable aray type throws CCE |
* toArray(incompatible array type) throws ArrayStoreException |
385 |
*/ |
*/ |
386 |
public void testToArray1_BadArg() { |
public void testToArray1_BadArg() { |
|
try { |
|
387 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
388 |
l.add(new Integer(5)); |
l.add(new Integer(5)); |
389 |
Object o[] = l.toArray(new String[10] ); |
try { |
390 |
|
l.toArray(new String[10]); |
391 |
shouldThrow(); |
shouldThrow(); |
392 |
} catch(ArrayStoreException success){} |
} catch(ArrayStoreException success){} |
393 |
} |
} |
397 |
*/ |
*/ |
398 |
public void testIterator() { |
public void testIterator() { |
399 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
|
int i = 0; |
|
400 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
401 |
while(it.hasNext()) { |
int i; |
402 |
|
for (i = 0; it.hasNext(); i++) |
403 |
assertTrue(q.contains(it.next())); |
assertTrue(q.contains(it.next())); |
|
++i; |
|
|
} |
|
404 |
assertEquals(i, SIZE); |
assertEquals(i, SIZE); |
405 |
|
assertIteratorExhausted(it); |
406 |
|
} |
407 |
|
|
408 |
|
/** |
409 |
|
* iterator of empty collection has no elements |
410 |
|
*/ |
411 |
|
public void testEmptyIterator() { |
412 |
|
assertIteratorExhausted(new LinkedList().iterator()); |
413 |
} |
} |
414 |
|
|
415 |
/** |
/** |
422 |
q.add(new Integer(3)); |
q.add(new Integer(3)); |
423 |
int k = 0; |
int k = 0; |
424 |
for (Iterator it = q.iterator(); it.hasNext();) { |
for (Iterator it = q.iterator(); it.hasNext();) { |
425 |
int i = ((Integer)(it.next())).intValue(); |
assertEquals(++k, it.next()); |
|
assertEquals(++k, i); |
|
426 |
} |
} |
427 |
|
|
428 |
assertEquals(3, k); |
assertEquals(3, k); |
437 |
q.add(new Integer(2)); |
q.add(new Integer(2)); |
438 |
q.add(new Integer(3)); |
q.add(new Integer(3)); |
439 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
440 |
it.next(); |
assertEquals(1, it.next()); |
441 |
it.remove(); |
it.remove(); |
442 |
it = q.iterator(); |
it = q.iterator(); |
443 |
assertEquals(it.next(), new Integer(2)); |
assertEquals(2, it.next()); |
444 |
assertEquals(it.next(), new Integer(3)); |
assertEquals(3, it.next()); |
445 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
446 |
} |
} |
447 |
|
|
460 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
461 |
try { |
try { |
462 |
it.next(); |
it.next(); |
463 |
} catch(NoSuchElementException success) { |
shouldThrow(); |
464 |
} |
} catch (NoSuchElementException success) {} |
465 |
} |
} |
466 |
|
|
467 |
/** |
/** |
474 |
q.add(new Integer(1)); |
q.add(new Integer(1)); |
475 |
int k = 0; |
int k = 0; |
476 |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
477 |
int i = ((Integer)(it.next())).intValue(); |
assertEquals(++k, it.next()); |
|
assertEquals(++k, i); |
|
478 |
} |
} |
479 |
|
|
480 |
assertEquals(3, k); |
assertEquals(3, k); |
485 |
*/ |
*/ |
486 |
public void testDescendingIteratorRemove () { |
public void testDescendingIteratorRemove () { |
487 |
final LinkedList q = new LinkedList(); |
final LinkedList q = new LinkedList(); |
488 |
q.add(new Integer(3)); |
q.add(three); |
489 |
q.add(new Integer(2)); |
q.add(two); |
490 |
q.add(new Integer(1)); |
q.add(one); |
491 |
Iterator it = q.descendingIterator(); |
Iterator it = q.descendingIterator(); |
492 |
it.next(); |
it.next(); |
493 |
it.remove(); |
it.remove(); |
494 |
it = q.descendingIterator(); |
it = q.descendingIterator(); |
495 |
assertEquals(it.next(), new Integer(2)); |
assertSame(it.next(), two); |
496 |
assertEquals(it.next(), new Integer(3)); |
assertSame(it.next(), three); |
497 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
498 |
} |
} |
499 |
|
|
|
|
|
500 |
/** |
/** |
501 |
* toString contains toStrings of elements |
* toString contains toStrings of elements |
502 |
*/ |
*/ |
504 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
505 |
String s = q.toString(); |
String s = q.toString(); |
506 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
507 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
assertTrue(s.contains(String.valueOf(i))); |
508 |
} |
} |
509 |
} |
} |
510 |
|
|
514 |
public void testAddFirst() { |
public void testAddFirst() { |
515 |
LinkedList q = populatedQueue(3); |
LinkedList q = populatedQueue(3); |
516 |
q.addFirst(four); |
q.addFirst(four); |
517 |
assertEquals(four,q.peek()); |
assertSame(four, q.peek()); |
518 |
} |
} |
519 |
|
|
520 |
/** |
/** |
522 |
*/ |
*/ |
523 |
public void testPush() { |
public void testPush() { |
524 |
LinkedList q = populatedQueue(3); |
LinkedList q = populatedQueue(3); |
|
q.pollLast(); |
|
525 |
q.push(four); |
q.push(four); |
526 |
assertEquals(four,q.peekFirst()); |
assertSame(four, q.peekFirst()); |
527 |
} |
} |
528 |
|
|
529 |
/** |
/** |
532 |
public void testPop() { |
public void testPop() { |
533 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
534 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
535 |
assertEquals(i, ((Integer)q.pop()).intValue()); |
assertEquals(i, q.pop()); |
536 |
} |
} |
537 |
try { |
try { |
538 |
q.pop(); |
q.pop(); |
539 |
shouldThrow(); |
shouldThrow(); |
540 |
} catch (NoSuchElementException success){ |
} catch (NoSuchElementException success) {} |
|
} |
|
541 |
} |
} |
542 |
|
|
543 |
/** |
/** |
564 |
public void testPollLast() { |
public void testPollLast() { |
565 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
566 |
for (int i = SIZE-1; i >= 0; --i) { |
for (int i = SIZE-1; i >= 0; --i) { |
567 |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
assertEquals(i, q.pollLast()); |
568 |
} |
} |
569 |
assertNull(q.pollLast()); |
assertNull(q.pollLast()); |
570 |
} |
} |
575 |
public void testPeekFirst() { |
public void testPeekFirst() { |
576 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
577 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
578 |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
assertEquals(i, q.peekFirst()); |
579 |
q.pollFirst(); |
assertEquals(i, q.pollFirst()); |
580 |
assertTrue(q.peekFirst() == null || |
assertTrue(q.peekFirst() == null || |
581 |
i != ((Integer)q.peekFirst()).intValue()); |
!q.peekFirst().equals(i)); |
582 |
} |
} |
583 |
assertNull(q.peekFirst()); |
assertNull(q.peekFirst()); |
584 |
} |
} |
585 |
|
|
|
|
|
586 |
/** |
/** |
587 |
* peekLast returns next element, or null if empty |
* peekLast returns next element, or null if empty |
588 |
*/ |
*/ |
589 |
public void testPeekLast() { |
public void testPeekLast() { |
590 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
591 |
for (int i = SIZE-1; i >= 0; --i) { |
for (int i = SIZE-1; i >= 0; --i) { |
592 |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
assertEquals(i, q.peekLast()); |
593 |
q.pollLast(); |
assertEquals(i, q.pollLast()); |
594 |
assertTrue(q.peekLast() == null || |
assertTrue(q.peekLast() == null || |
595 |
i != ((Integer)q.peekLast()).intValue()); |
!q.peekLast().equals(i)); |
596 |
} |
} |
597 |
assertNull(q.peekLast()); |
assertNull(q.peekLast()); |
598 |
} |
} |
600 |
public void testFirstElement() { |
public void testFirstElement() { |
601 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
602 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
603 |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
assertEquals(i, q.getFirst()); |
604 |
q.pollFirst(); |
assertEquals(i, q.pollFirst()); |
605 |
} |
} |
606 |
try { |
try { |
607 |
q.getFirst(); |
q.getFirst(); |
608 |
shouldThrow(); |
shouldThrow(); |
609 |
} |
} catch (NoSuchElementException success) {} |
|
catch (NoSuchElementException success) {} |
|
610 |
} |
} |
611 |
|
|
612 |
/** |
/** |
615 |
public void testLastElement() { |
public void testLastElement() { |
616 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
617 |
for (int i = SIZE-1; i >= 0; --i) { |
for (int i = SIZE-1; i >= 0; --i) { |
618 |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
assertEquals(i, q.getLast()); |
619 |
q.pollLast(); |
assertEquals(i, q.pollLast()); |
620 |
} |
} |
621 |
try { |
try { |
622 |
q.getLast(); |
q.getLast(); |
623 |
shouldThrow(); |
shouldThrow(); |
624 |
} |
} catch (NoSuchElementException success) {} |
|
catch (NoSuchElementException success) {} |
|
625 |
assertNull(q.peekLast()); |
assertNull(q.peekLast()); |
626 |
} |
} |
627 |
|
|