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.List; |
14 |
|
import java.util.NoSuchElementException; |
15 |
|
import java.util.concurrent.ThreadLocalRandom; |
16 |
|
|
17 |
|
import junit.framework.Test; |
18 |
|
|
19 |
public class LinkedListTest extends JSR166TestCase { |
public class LinkedListTest extends JSR166TestCase { |
20 |
public static void main(String[] args) { |
public static void main(String[] args) { |
21 |
junit.textui.TestRunner.run (suite()); |
main(suite(), args); |
22 |
} |
} |
23 |
|
|
24 |
public static Test suite() { |
public static Test suite() { |
25 |
return new TestSuite(LinkedListTest.class); |
class Implementation implements CollectionImplementation { |
26 |
|
public Class<?> klazz() { return LinkedList.class; } |
27 |
|
public List emptyCollection() { return new LinkedList(); } |
28 |
|
public Object makeElement(int i) { return i; } |
29 |
|
public boolean isConcurrent() { return false; } |
30 |
|
public boolean permitsNulls() { return true; } |
31 |
|
} |
32 |
|
class SubListImplementation extends Implementation { |
33 |
|
public List emptyCollection() { |
34 |
|
List list = super.emptyCollection(); |
35 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
36 |
|
if (rnd.nextBoolean()) |
37 |
|
list.add(makeElement(rnd.nextInt())); |
38 |
|
int i = rnd.nextInt(list.size() + 1); |
39 |
|
return list.subList(i, i); |
40 |
|
} |
41 |
|
} |
42 |
|
return newTestSuite( |
43 |
|
LinkedListTest.class, |
44 |
|
CollectionTest.testSuite(new Implementation()), |
45 |
|
CollectionTest.testSuite(new SubListImplementation())); |
46 |
} |
} |
47 |
|
|
48 |
/** |
/** |
49 |
* Create a queue of given size containing consecutive |
* Returns a new queue of given size containing consecutive |
50 |
* Integers 0 ... n. |
* Integers 0 ... n - 1. |
51 |
*/ |
*/ |
52 |
private LinkedList populatedQueue(int n) { |
private static LinkedList<Integer> populatedQueue(int n) { |
53 |
LinkedList q = new LinkedList(); |
LinkedList<Integer> q = new LinkedList<>(); |
54 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
55 |
for (int i = 0; i < n; ++i) |
for (int i = 0; i < n; ++i) |
56 |
assertTrue(q.offer(new Integer(i))); |
assertTrue(q.offer(new Integer(i))); |
57 |
assertFalse(q.isEmpty()); |
assertFalse(q.isEmpty()); |
58 |
assertEquals(n, q.size()); |
assertEquals(n, q.size()); |
59 |
|
assertEquals((Integer) 0, q.peekFirst()); |
60 |
|
assertEquals((Integer) (n - 1), q.peekLast()); |
61 |
return q; |
return q; |
62 |
} |
} |
63 |
|
|
73 |
*/ |
*/ |
74 |
public void testConstructor3() { |
public void testConstructor3() { |
75 |
try { |
try { |
76 |
LinkedList q = new LinkedList((Collection)null); |
new LinkedList((Collection)null); |
77 |
shouldThrow(); |
shouldThrow(); |
78 |
} |
} catch (NullPointerException success) {} |
|
catch (NullPointerException success) {} |
|
79 |
} |
} |
80 |
|
|
81 |
/** |
/** |
82 |
* Queue contains all elements of collection used to initialize |
* Queue contains all elements of collection used to initialize |
|
|
|
83 |
*/ |
*/ |
84 |
public void testConstructor6() { |
public void testConstructor6() { |
|
try { |
|
85 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
86 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
87 |
ints[i] = new Integer(i); |
ints[i] = i; |
88 |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
89 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
90 |
assertEquals(ints[i], q.poll()); |
assertEquals(ints[i], q.poll()); |
91 |
} |
} |
|
finally {} |
|
|
} |
|
92 |
|
|
93 |
/** |
/** |
94 |
* isEmpty is true before add, false after |
* isEmpty is true before add, false after |
123 |
* offer(null) succeeds |
* offer(null) succeeds |
124 |
*/ |
*/ |
125 |
public void testOfferNull() { |
public void testOfferNull() { |
|
try { |
|
126 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
127 |
q.offer(null); |
q.offer(null); |
128 |
} catch (NullPointerException ie) { |
assertNull(q.get(0)); |
129 |
unexpectedException(); |
assertTrue(q.contains(null)); |
|
} |
|
130 |
} |
} |
131 |
|
|
132 |
/** |
/** |
153 |
* addAll(null) throws NPE |
* addAll(null) throws NPE |
154 |
*/ |
*/ |
155 |
public void testAddAll1() { |
public void testAddAll1() { |
|
try { |
|
156 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
157 |
|
try { |
158 |
q.addAll(null); |
q.addAll(null); |
159 |
shouldThrow(); |
shouldThrow(); |
160 |
} |
} catch (NullPointerException success) {} |
|
catch (NullPointerException success) {} |
|
161 |
} |
} |
162 |
|
|
163 |
/** |
/** |
164 |
* Queue contains all elements, in traversal order, of successful addAll |
* Queue contains all elements, in traversal order, of successful addAll |
165 |
*/ |
*/ |
166 |
public void testAddAll5() { |
public void testAddAll5() { |
|
try { |
|
167 |
Integer[] empty = new Integer[0]; |
Integer[] empty = new Integer[0]; |
168 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
169 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
170 |
ints[i] = new Integer(i); |
ints[i] = i; |
171 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
172 |
assertFalse(q.addAll(Arrays.asList(empty))); |
assertFalse(q.addAll(Arrays.asList(empty))); |
173 |
assertTrue(q.addAll(Arrays.asList(ints))); |
assertTrue(q.addAll(Arrays.asList(ints))); |
174 |
for (int i = 0; i < SIZE; ++i) |
for (int i = 0; i < SIZE; ++i) |
175 |
assertEquals(ints[i], q.poll()); |
assertEquals(ints[i], q.poll()); |
176 |
} |
} |
|
finally {} |
|
|
} |
|
177 |
|
|
178 |
/** |
/** |
179 |
* addAll with too large an index throws IOOBE |
* addAll with too large an index throws IOOBE |
180 |
*/ |
*/ |
181 |
public void testAddAll2_IndexOutOfBoundsException() { |
public void testAddAll2_IndexOutOfBoundsException() { |
|
try { |
|
182 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
183 |
l.add(new Object()); |
l.add(new Object()); |
184 |
LinkedList m = new LinkedList(); |
LinkedList m = new LinkedList(); |
185 |
m.add(new Object()); |
m.add(new Object()); |
186 |
|
try { |
187 |
l.addAll(4,m); |
l.addAll(4,m); |
188 |
shouldThrow(); |
shouldThrow(); |
189 |
} catch (IndexOutOfBoundsException success) {} |
} catch (IndexOutOfBoundsException success) {} |
193 |
* addAll with negative index throws IOOBE |
* addAll with negative index throws IOOBE |
194 |
*/ |
*/ |
195 |
public void testAddAll4_BadIndex() { |
public void testAddAll4_BadIndex() { |
|
try { |
|
196 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
197 |
l.add(new Object()); |
l.add(new Object()); |
198 |
LinkedList m = new LinkedList(); |
LinkedList m = new LinkedList(); |
199 |
m.add(new Object()); |
m.add(new Object()); |
200 |
|
try { |
201 |
l.addAll(-1,m); |
l.addAll(-1,m); |
202 |
shouldThrow(); |
shouldThrow(); |
203 |
} catch (IndexOutOfBoundsException success) {} |
} catch (IndexOutOfBoundsException success) {} |
209 |
public void testPoll() { |
public void testPoll() { |
210 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
211 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
212 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
assertEquals(i, q.poll()); |
213 |
} |
} |
214 |
assertNull(q.poll()); |
assertNull(q.poll()); |
215 |
} |
} |
220 |
public void testPeek() { |
public void testPeek() { |
221 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
222 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
223 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
assertEquals(i, q.peek()); |
224 |
q.poll(); |
assertEquals(i, q.poll()); |
225 |
assertTrue(q.peek() == null || |
assertTrue(q.peek() == null || |
226 |
i != ((Integer)q.peek()).intValue()); |
!q.peek().equals(i)); |
227 |
} |
} |
228 |
assertNull(q.peek()); |
assertNull(q.peek()); |
229 |
} |
} |
234 |
public void testElement() { |
public void testElement() { |
235 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
236 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
237 |
assertEquals(i, ((Integer)q.element()).intValue()); |
assertEquals(i, q.element()); |
238 |
q.poll(); |
assertEquals(i, q.poll()); |
239 |
} |
} |
240 |
try { |
try { |
241 |
q.element(); |
q.element(); |
242 |
shouldThrow(); |
shouldThrow(); |
243 |
} |
} catch (NoSuchElementException success) {} |
|
catch (NoSuchElementException success) {} |
|
244 |
} |
} |
245 |
|
|
246 |
/** |
/** |
249 |
public void testRemove() { |
public void testRemove() { |
250 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
251 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
252 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
assertEquals(i, q.remove()); |
253 |
} |
} |
254 |
try { |
try { |
255 |
q.remove(); |
q.remove(); |
256 |
shouldThrow(); |
shouldThrow(); |
257 |
} catch (NoSuchElementException success) { |
} catch (NoSuchElementException success) {} |
|
} |
|
258 |
} |
} |
259 |
|
|
260 |
/** |
/** |
263 |
public void testRemoveElement() { |
public void testRemoveElement() { |
264 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
265 |
for (int i = 1; i < SIZE; i+=2) { |
for (int i = 1; i < SIZE; i+=2) { |
266 |
assertTrue(q.remove(new Integer(i))); |
assertTrue(q.contains(i)); |
267 |
|
assertTrue(q.remove((Integer)i)); |
268 |
|
assertFalse(q.contains(i)); |
269 |
|
assertTrue(q.contains(i - 1)); |
270 |
} |
} |
271 |
for (int i = 0; i < SIZE; i+=2) { |
for (int i = 0; i < SIZE; i+=2) { |
272 |
assertTrue(q.remove(new Integer(i))); |
assertTrue(q.contains(i)); |
273 |
assertFalse(q.remove(new Integer(i+1))); |
assertTrue(q.remove((Integer)i)); |
274 |
|
assertFalse(q.contains(i)); |
275 |
|
assertFalse(q.remove((Integer)(i + 1))); |
276 |
|
assertFalse(q.contains(i + 1)); |
277 |
} |
} |
278 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
279 |
} |
} |
298 |
q.clear(); |
q.clear(); |
299 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
300 |
assertEquals(0, q.size()); |
assertEquals(0, q.size()); |
301 |
q.add(new Integer(1)); |
assertTrue(q.add(new Integer(1))); |
302 |
assertFalse(q.isEmpty()); |
assertFalse(q.isEmpty()); |
303 |
q.clear(); |
q.clear(); |
304 |
assertTrue(q.isEmpty()); |
assertTrue(q.isEmpty()); |
313 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
314 |
assertTrue(q.containsAll(p)); |
assertTrue(q.containsAll(p)); |
315 |
assertFalse(p.containsAll(q)); |
assertFalse(p.containsAll(q)); |
316 |
p.add(new Integer(i)); |
assertTrue(p.add(new Integer(i))); |
317 |
} |
} |
318 |
assertTrue(p.containsAll(q)); |
assertTrue(p.containsAll(q)); |
319 |
} |
} |
347 |
assertTrue(q.removeAll(p)); |
assertTrue(q.removeAll(p)); |
348 |
assertEquals(SIZE-i, q.size()); |
assertEquals(SIZE-i, q.size()); |
349 |
for (int j = 0; j < i; ++j) { |
for (int j = 0; j < i; ++j) { |
350 |
Integer I = (Integer)(p.remove()); |
Integer x = (Integer)(p.remove()); |
351 |
assertFalse(q.contains(I)); |
assertFalse(q.contains(x)); |
352 |
} |
} |
353 |
} |
} |
354 |
} |
} |
355 |
|
|
356 |
/** |
/** |
357 |
* toArray contains all elements |
* toArray contains all elements in FIFO order |
358 |
*/ |
*/ |
359 |
public void testToArray() { |
public void testToArray() { |
360 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
361 |
Object[] o = q.toArray(); |
Object[] a = q.toArray(); |
362 |
Arrays.sort(o); |
assertSame(Object[].class, a.getClass()); |
363 |
for (int i = 0; i < o.length; i++) |
for (Object o : a) |
364 |
assertEquals(o[i], q.poll()); |
assertSame(o, q.poll()); |
365 |
|
assertTrue(q.isEmpty()); |
366 |
} |
} |
367 |
|
|
368 |
/** |
/** |
369 |
* toArray(a) contains all elements |
* toArray(a) contains all elements in FIFO order |
370 |
*/ |
*/ |
371 |
public void testToArray2() { |
public void testToArray2() { |
372 |
LinkedList q = populatedQueue(SIZE); |
LinkedList<Integer> q = populatedQueue(SIZE); |
373 |
Integer[] ints = new Integer[SIZE]; |
Integer[] ints = new Integer[SIZE]; |
374 |
ints = (Integer[])q.toArray(ints); |
Integer[] array = q.toArray(ints); |
375 |
Arrays.sort(ints); |
assertSame(ints, array); |
376 |
for (int i = 0; i < ints.length; i++) |
for (Integer o : ints) |
377 |
assertEquals(ints[i], q.poll()); |
assertSame(o, q.poll()); |
378 |
|
assertTrue(q.isEmpty()); |
379 |
} |
} |
380 |
|
|
381 |
/** |
/** |
382 |
* toArray(null) throws NPE |
* toArray(null) throws NullPointerException |
383 |
*/ |
*/ |
384 |
public void testToArray_BadArg() { |
public void testToArray_NullArg() { |
|
try { |
|
385 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
386 |
l.add(new Object()); |
l.add(new Object()); |
387 |
Object o[] = l.toArray(null); |
try { |
388 |
|
l.toArray(null); |
389 |
shouldThrow(); |
shouldThrow(); |
390 |
} catch (NullPointerException success) {} |
} catch (NullPointerException success) {} |
391 |
} |
} |
392 |
|
|
393 |
/** |
/** |
394 |
* toArray with incompatable aray type throws CCE |
* toArray(incompatible array type) throws ArrayStoreException |
395 |
*/ |
*/ |
396 |
public void testToArray1_BadArg() { |
public void testToArray1_BadArg() { |
|
try { |
|
397 |
LinkedList l = new LinkedList(); |
LinkedList l = new LinkedList(); |
398 |
l.add(new Integer(5)); |
l.add(new Integer(5)); |
399 |
Object o[] = l.toArray(new String[10] ); |
try { |
400 |
|
l.toArray(new String[10]); |
401 |
shouldThrow(); |
shouldThrow(); |
402 |
} catch (ArrayStoreException success) {} |
} catch (ArrayStoreException success) {} |
403 |
} |
} |
407 |
*/ |
*/ |
408 |
public void testIterator() { |
public void testIterator() { |
409 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
|
int i = 0; |
|
410 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
411 |
while (it.hasNext()) { |
int i; |
412 |
|
for (i = 0; it.hasNext(); i++) |
413 |
assertTrue(q.contains(it.next())); |
assertTrue(q.contains(it.next())); |
|
++i; |
|
|
} |
|
414 |
assertEquals(i, SIZE); |
assertEquals(i, SIZE); |
415 |
|
assertIteratorExhausted(it); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
419 |
|
* iterator of empty collection has no elements |
420 |
|
*/ |
421 |
|
public void testEmptyIterator() { |
422 |
|
assertIteratorExhausted(new LinkedList().iterator()); |
423 |
} |
} |
424 |
|
|
425 |
/** |
/** |
432 |
q.add(new Integer(3)); |
q.add(new Integer(3)); |
433 |
int k = 0; |
int k = 0; |
434 |
for (Iterator it = q.iterator(); it.hasNext();) { |
for (Iterator it = q.iterator(); it.hasNext();) { |
435 |
int i = ((Integer)(it.next())).intValue(); |
assertEquals(++k, it.next()); |
|
assertEquals(++k, i); |
|
436 |
} |
} |
437 |
|
|
438 |
assertEquals(3, k); |
assertEquals(3, k); |
447 |
q.add(new Integer(2)); |
q.add(new Integer(2)); |
448 |
q.add(new Integer(3)); |
q.add(new Integer(3)); |
449 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
450 |
it.next(); |
assertEquals(1, it.next()); |
451 |
it.remove(); |
it.remove(); |
452 |
it = q.iterator(); |
it = q.iterator(); |
453 |
assertEquals(it.next(), new Integer(2)); |
assertEquals(2, it.next()); |
454 |
assertEquals(it.next(), new Integer(3)); |
assertEquals(3, it.next()); |
455 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
456 |
} |
} |
457 |
|
|
470 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
471 |
try { |
try { |
472 |
it.next(); |
it.next(); |
473 |
} catch (NoSuchElementException success) { |
shouldThrow(); |
474 |
} |
} catch (NoSuchElementException success) {} |
475 |
} |
} |
476 |
|
|
477 |
/** |
/** |
484 |
q.add(new Integer(1)); |
q.add(new Integer(1)); |
485 |
int k = 0; |
int k = 0; |
486 |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
487 |
int i = ((Integer)(it.next())).intValue(); |
assertEquals(++k, it.next()); |
|
assertEquals(++k, i); |
|
488 |
} |
} |
489 |
|
|
490 |
assertEquals(3, k); |
assertEquals(3, k); |
495 |
*/ |
*/ |
496 |
public void testDescendingIteratorRemove () { |
public void testDescendingIteratorRemove () { |
497 |
final LinkedList q = new LinkedList(); |
final LinkedList q = new LinkedList(); |
498 |
q.add(new Integer(3)); |
q.add(three); |
499 |
q.add(new Integer(2)); |
q.add(two); |
500 |
q.add(new Integer(1)); |
q.add(one); |
501 |
Iterator it = q.descendingIterator(); |
Iterator it = q.descendingIterator(); |
502 |
it.next(); |
it.next(); |
503 |
it.remove(); |
it.remove(); |
504 |
it = q.descendingIterator(); |
it = q.descendingIterator(); |
505 |
assertEquals(it.next(), new Integer(2)); |
assertSame(it.next(), two); |
506 |
assertEquals(it.next(), new Integer(3)); |
assertSame(it.next(), three); |
507 |
assertFalse(it.hasNext()); |
assertFalse(it.hasNext()); |
508 |
} |
} |
509 |
|
|
|
|
|
510 |
/** |
/** |
511 |
* toString contains toStrings of elements |
* toString contains toStrings of elements |
512 |
*/ |
*/ |
514 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
515 |
String s = q.toString(); |
String s = q.toString(); |
516 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
517 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
assertTrue(s.contains(String.valueOf(i))); |
518 |
} |
} |
519 |
} |
} |
520 |
|
|
524 |
public void testAddFirst() { |
public void testAddFirst() { |
525 |
LinkedList q = populatedQueue(3); |
LinkedList q = populatedQueue(3); |
526 |
q.addFirst(four); |
q.addFirst(four); |
527 |
assertEquals(four,q.peek()); |
assertSame(four, q.peek()); |
528 |
} |
} |
529 |
|
|
530 |
/** |
/** |
532 |
*/ |
*/ |
533 |
public void testPush() { |
public void testPush() { |
534 |
LinkedList q = populatedQueue(3); |
LinkedList q = populatedQueue(3); |
|
q.pollLast(); |
|
535 |
q.push(four); |
q.push(four); |
536 |
assertEquals(four,q.peekFirst()); |
assertSame(four, q.peekFirst()); |
537 |
} |
} |
538 |
|
|
539 |
/** |
/** |
542 |
public void testPop() { |
public void testPop() { |
543 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
544 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
545 |
assertEquals(i, ((Integer)q.pop()).intValue()); |
assertEquals(i, q.pop()); |
546 |
} |
} |
547 |
try { |
try { |
548 |
q.pop(); |
q.pop(); |
549 |
shouldThrow(); |
shouldThrow(); |
550 |
} catch (NoSuchElementException success) { |
} catch (NoSuchElementException success) {} |
|
} |
|
551 |
} |
} |
552 |
|
|
553 |
/** |
/** |
574 |
public void testPollLast() { |
public void testPollLast() { |
575 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
576 |
for (int i = SIZE-1; i >= 0; --i) { |
for (int i = SIZE-1; i >= 0; --i) { |
577 |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
assertEquals(i, q.pollLast()); |
578 |
} |
} |
579 |
assertNull(q.pollLast()); |
assertNull(q.pollLast()); |
580 |
} |
} |
585 |
public void testPeekFirst() { |
public void testPeekFirst() { |
586 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
587 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
588 |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
assertEquals(i, q.peekFirst()); |
589 |
q.pollFirst(); |
assertEquals(i, q.pollFirst()); |
590 |
assertTrue(q.peekFirst() == null || |
assertTrue(q.peekFirst() == null || |
591 |
i != ((Integer)q.peekFirst()).intValue()); |
!q.peekFirst().equals(i)); |
592 |
} |
} |
593 |
assertNull(q.peekFirst()); |
assertNull(q.peekFirst()); |
594 |
} |
} |
595 |
|
|
|
|
|
596 |
/** |
/** |
597 |
* peekLast returns next element, or null if empty |
* peekLast returns next element, or null if empty |
598 |
*/ |
*/ |
599 |
public void testPeekLast() { |
public void testPeekLast() { |
600 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
601 |
for (int i = SIZE-1; i >= 0; --i) { |
for (int i = SIZE-1; i >= 0; --i) { |
602 |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
assertEquals(i, q.peekLast()); |
603 |
q.pollLast(); |
assertEquals(i, q.pollLast()); |
604 |
assertTrue(q.peekLast() == null || |
assertTrue(q.peekLast() == null || |
605 |
i != ((Integer)q.peekLast()).intValue()); |
!q.peekLast().equals(i)); |
606 |
} |
} |
607 |
assertNull(q.peekLast()); |
assertNull(q.peekLast()); |
608 |
} |
} |
610 |
public void testFirstElement() { |
public void testFirstElement() { |
611 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
612 |
for (int i = 0; i < SIZE; ++i) { |
for (int i = 0; i < SIZE; ++i) { |
613 |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
assertEquals(i, q.getFirst()); |
614 |
q.pollFirst(); |
assertEquals(i, q.pollFirst()); |
615 |
} |
} |
616 |
try { |
try { |
617 |
q.getFirst(); |
q.getFirst(); |
618 |
shouldThrow(); |
shouldThrow(); |
619 |
} |
} catch (NoSuchElementException success) {} |
|
catch (NoSuchElementException success) {} |
|
620 |
} |
} |
621 |
|
|
622 |
/** |
/** |
625 |
public void testLastElement() { |
public void testLastElement() { |
626 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
627 |
for (int i = SIZE-1; i >= 0; --i) { |
for (int i = SIZE-1; i >= 0; --i) { |
628 |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
assertEquals(i, q.getLast()); |
629 |
q.pollLast(); |
assertEquals(i, q.pollLast()); |
630 |
} |
} |
631 |
try { |
try { |
632 |
q.getLast(); |
q.getLast(); |
633 |
shouldThrow(); |
shouldThrow(); |
634 |
} |
} catch (NoSuchElementException success) {} |
|
catch (NoSuchElementException success) {} |
|
635 |
assertNull(q.peekLast()); |
assertNull(q.peekLast()); |
636 |
} |
} |
637 |
|
|