1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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. |
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 |
< |
|
36 |
< |
public void testConstructor1(){ |
35 |
> |
|
36 |
> |
/** |
37 |
> |
* new queue is empty |
38 |
> |
*/ |
39 |
> |
public void testConstructor1() { |
40 |
|
assertEquals(0, new LinkedList().size()); |
41 |
|
} |
42 |
|
|
43 |
+ |
/** |
44 |
+ |
* Initializing from null Collection throws NPE |
45 |
+ |
*/ |
46 |
|
public void testConstructor3() { |
47 |
|
try { |
48 |
|
LinkedList q = new LinkedList((Collection)null); |
49 |
< |
fail("Cannot make from null collection"); |
49 |
> |
shouldThrow(); |
50 |
|
} |
51 |
|
catch (NullPointerException success) {} |
52 |
|
} |
53 |
|
|
54 |
< |
public void testConstructor6(){ |
54 |
> |
/** |
55 |
> |
* Queue contains all elements of collection used to initialize |
56 |
> |
|
57 |
> |
*/ |
58 |
> |
public void testConstructor6() { |
59 |
|
try { |
60 |
|
Integer[] ints = new Integer[SIZE]; |
61 |
|
for (int i = 0; i < SIZE; ++i) |
67 |
|
finally {} |
68 |
|
} |
69 |
|
|
70 |
+ |
/** |
71 |
+ |
* isEmpty is true before add, false after |
72 |
+ |
*/ |
73 |
|
public void testEmpty() { |
74 |
|
LinkedList q = new LinkedList(); |
75 |
|
assertTrue(q.isEmpty()); |
81 |
|
assertTrue(q.isEmpty()); |
82 |
|
} |
83 |
|
|
84 |
+ |
/** |
85 |
+ |
* size changes when elements added and removed |
86 |
+ |
*/ |
87 |
|
public void testSize() { |
88 |
|
LinkedList q = populatedQueue(SIZE); |
89 |
|
for (int i = 0; i < SIZE; ++i) { |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
< |
public void testOfferNull(){ |
99 |
> |
/** |
100 |
> |
* offer(null) succeeds |
101 |
> |
*/ |
102 |
> |
public void testOfferNull() { |
103 |
|
try { |
104 |
|
LinkedList q = new LinkedList(); |
105 |
|
q.offer(null); |
106 |
< |
} catch (NullPointerException ie) { |
107 |
< |
fail("should not throw NPE"); |
108 |
< |
} |
106 |
> |
} catch (NullPointerException ie) { |
107 |
> |
unexpectedException(); |
108 |
> |
} |
109 |
|
} |
110 |
|
|
111 |
+ |
/** |
112 |
+ |
* Offer succeeds |
113 |
+ |
*/ |
114 |
|
public void testOffer() { |
115 |
|
LinkedList q = new LinkedList(); |
116 |
|
assertTrue(q.offer(new Integer(0))); |
117 |
|
assertTrue(q.offer(new Integer(1))); |
118 |
|
} |
119 |
|
|
120 |
< |
public void testAdd(){ |
120 |
> |
/** |
121 |
> |
* add succeeds |
122 |
> |
*/ |
123 |
> |
public void testAdd() { |
124 |
|
LinkedList q = new LinkedList(); |
125 |
|
for (int i = 0; i < SIZE; ++i) { |
126 |
|
assertEquals(i, q.size()); |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
< |
public void testAddAll1(){ |
131 |
> |
/** |
132 |
> |
* addAll(null) throws NPE |
133 |
> |
*/ |
134 |
> |
public void testAddAll1() { |
135 |
|
try { |
136 |
|
LinkedList q = new LinkedList(); |
137 |
|
q.addAll(null); |
138 |
< |
fail("Cannot add null collection"); |
138 |
> |
shouldThrow(); |
139 |
|
} |
140 |
|
catch (NullPointerException success) {} |
141 |
|
} |
142 |
|
|
143 |
< |
public void testAddAll5(){ |
143 |
> |
/** |
144 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
145 |
> |
*/ |
146 |
> |
public void testAddAll5() { |
147 |
|
try { |
148 |
|
Integer[] empty = new Integer[0]; |
149 |
|
Integer[] ints = new Integer[SIZE]; |
158 |
|
finally {} |
159 |
|
} |
160 |
|
|
161 |
< |
public void testPoll(){ |
161 |
> |
/** |
162 |
> |
* addAll with too large an index throws IOOBE |
163 |
> |
*/ |
164 |
> |
public void testAddAll2_IndexOutOfBoundsException() { |
165 |
> |
try { |
166 |
> |
LinkedList l = new LinkedList(); |
167 |
> |
l.add(new Object()); |
168 |
> |
LinkedList m = new LinkedList(); |
169 |
> |
m.add(new Object()); |
170 |
> |
l.addAll(4,m); |
171 |
> |
shouldThrow(); |
172 |
> |
} catch (IndexOutOfBoundsException success) {} |
173 |
> |
} |
174 |
> |
|
175 |
> |
/** |
176 |
> |
* addAll with negative index throws IOOBE |
177 |
> |
*/ |
178 |
> |
public void testAddAll4_BadIndex() { |
179 |
> |
try { |
180 |
> |
LinkedList l = new LinkedList(); |
181 |
> |
l.add(new Object()); |
182 |
> |
LinkedList m = new LinkedList(); |
183 |
> |
m.add(new Object()); |
184 |
> |
l.addAll(-1,m); |
185 |
> |
shouldThrow(); |
186 |
> |
} catch (IndexOutOfBoundsException success){} |
187 |
> |
} |
188 |
> |
|
189 |
> |
/** |
190 |
> |
* poll succeeds unless empty |
191 |
> |
*/ |
192 |
> |
public void testPoll() { |
193 |
|
LinkedList q = populatedQueue(SIZE); |
194 |
|
for (int i = 0; i < SIZE; ++i) { |
195 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
197 |
|
assertNull(q.poll()); |
198 |
|
} |
199 |
|
|
200 |
< |
public void testPeek(){ |
200 |
> |
/** |
201 |
> |
* peek returns next element, or null if empty |
202 |
> |
*/ |
203 |
> |
public void testPeek() { |
204 |
|
LinkedList q = populatedQueue(SIZE); |
205 |
|
for (int i = 0; i < SIZE; ++i) { |
206 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
211 |
|
assertNull(q.peek()); |
212 |
|
} |
213 |
|
|
214 |
< |
public void testElement(){ |
214 |
> |
/** |
215 |
> |
* element returns next element, or throws NSEE if empty |
216 |
> |
*/ |
217 |
> |
public void testElement() { |
218 |
|
LinkedList q = populatedQueue(SIZE); |
219 |
|
for (int i = 0; i < SIZE; ++i) { |
220 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
222 |
|
} |
223 |
|
try { |
224 |
|
q.element(); |
225 |
< |
fail("no such element"); |
225 |
> |
shouldThrow(); |
226 |
|
} |
227 |
|
catch (NoSuchElementException success) {} |
228 |
|
} |
229 |
|
|
230 |
< |
public void testRemove(){ |
230 |
> |
/** |
231 |
> |
* remove removes next element, or throws NSEE if empty |
232 |
> |
*/ |
233 |
> |
public void testRemove() { |
234 |
|
LinkedList q = populatedQueue(SIZE); |
235 |
|
for (int i = 0; i < SIZE; ++i) { |
236 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
237 |
|
} |
238 |
|
try { |
239 |
|
q.remove(); |
240 |
< |
fail("remove should throw"); |
240 |
> |
shouldThrow(); |
241 |
|
} catch (NoSuchElementException success){ |
242 |
< |
} |
242 |
> |
} |
243 |
|
} |
244 |
|
|
245 |
< |
public void testRemoveElement(){ |
245 |
> |
/** |
246 |
> |
* remove(x) removes x and returns true if present |
247 |
> |
*/ |
248 |
> |
public void testRemoveElement() { |
249 |
|
LinkedList q = populatedQueue(SIZE); |
250 |
|
for (int i = 1; i < SIZE; i+=2) { |
251 |
|
assertTrue(q.remove(new Integer(i))); |
256 |
|
} |
257 |
|
assertTrue(q.isEmpty()); |
258 |
|
} |
259 |
< |
|
260 |
< |
public void testContains(){ |
259 |
> |
|
260 |
> |
/** |
261 |
> |
* contains(x) reports true when elements added but not yet removed |
262 |
> |
*/ |
263 |
> |
public void testContains() { |
264 |
|
LinkedList q = populatedQueue(SIZE); |
265 |
|
for (int i = 0; i < SIZE; ++i) { |
266 |
|
assertTrue(q.contains(new Integer(i))); |
269 |
|
} |
270 |
|
} |
271 |
|
|
272 |
< |
public void testClear(){ |
272 |
> |
/** |
273 |
> |
* clear removes all elements |
274 |
> |
*/ |
275 |
> |
public void testClear() { |
276 |
|
LinkedList q = populatedQueue(SIZE); |
277 |
|
q.clear(); |
278 |
|
assertTrue(q.isEmpty()); |
283 |
|
assertTrue(q.isEmpty()); |
284 |
|
} |
285 |
|
|
286 |
< |
public void testContainsAll(){ |
286 |
> |
/** |
287 |
> |
* containsAll(c) is true when c contains a subset of elements |
288 |
> |
*/ |
289 |
> |
public void testContainsAll() { |
290 |
|
LinkedList q = populatedQueue(SIZE); |
291 |
|
LinkedList p = new LinkedList(); |
292 |
|
for (int i = 0; i < SIZE; ++i) { |
297 |
|
assertTrue(p.containsAll(q)); |
298 |
|
} |
299 |
|
|
300 |
< |
public void testRetainAll(){ |
300 |
> |
/** |
301 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
302 |
> |
*/ |
303 |
> |
public void testRetainAll() { |
304 |
|
LinkedList q = populatedQueue(SIZE); |
305 |
|
LinkedList p = populatedQueue(SIZE); |
306 |
|
for (int i = 0; i < SIZE; ++i) { |
316 |
|
} |
317 |
|
} |
318 |
|
|
319 |
< |
public void testRemoveAll(){ |
319 |
> |
/** |
320 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
321 |
> |
*/ |
322 |
> |
public void testRemoveAll() { |
323 |
|
for (int i = 1; i < SIZE; ++i) { |
324 |
|
LinkedList q = populatedQueue(SIZE); |
325 |
|
LinkedList p = populatedQueue(i); |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
< |
public void testToArray(){ |
335 |
> |
/** |
336 |
> |
* toArray contains all elements |
337 |
> |
*/ |
338 |
> |
public void testToArray() { |
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 |
|
|
346 |
< |
public void testToArray2(){ |
346 |
> |
/** |
347 |
> |
* toArray(a) contains all elements |
348 |
> |
*/ |
349 |
> |
public void testToArray2() { |
350 |
|
LinkedList q = populatedQueue(SIZE); |
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 |
< |
|
358 |
< |
public void testIterator(){ |
357 |
> |
|
358 |
> |
/** |
359 |
> |
* toArray(null) throws NPE |
360 |
> |
*/ |
361 |
> |
public void testToArray_BadArg() { |
362 |
> |
try { |
363 |
> |
LinkedList l = new LinkedList(); |
364 |
> |
l.add(new Object()); |
365 |
> |
Object o[] = l.toArray(null); |
366 |
> |
shouldThrow(); |
367 |
> |
} catch (NullPointerException success){} |
368 |
> |
} |
369 |
> |
|
370 |
> |
/** |
371 |
> |
* toArray with incompatable aray type throws CCE |
372 |
> |
*/ |
373 |
> |
public void testToArray1_BadArg() { |
374 |
> |
try { |
375 |
> |
LinkedList l = new LinkedList(); |
376 |
> |
l.add(new Integer(5)); |
377 |
> |
Object o[] = l.toArray(new String[10] ); |
378 |
> |
shouldThrow(); |
379 |
> |
} catch (ArrayStoreException success){} |
380 |
> |
} |
381 |
> |
|
382 |
> |
/** |
383 |
> |
* iterator iterates through all elements |
384 |
> |
*/ |
385 |
> |
public void testIterator() { |
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 |
|
} |
393 |
|
assertEquals(i, SIZE); |
394 |
|
} |
395 |
|
|
396 |
+ |
/** |
397 |
+ |
* iterator ordering is FIFO |
398 |
+ |
*/ |
399 |
|
public void testIteratorOrdering() { |
274 |
– |
|
400 |
|
final LinkedList q = new LinkedList(); |
276 |
– |
|
401 |
|
q.add(new Integer(1)); |
402 |
|
q.add(new Integer(2)); |
403 |
|
q.add(new Integer(3)); |
280 |
– |
|
404 |
|
int k = 0; |
405 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
406 |
|
int i = ((Integer)(it.next())).intValue(); |
407 |
< |
assertEquals("items should come out in order", ++k, i); |
407 |
> |
assertEquals(++k, i); |
408 |
|
} |
409 |
|
|
410 |
< |
assertEquals("should go through 3 elements", 3, k); |
410 |
> |
assertEquals(3, k); |
411 |
|
} |
412 |
|
|
413 |
+ |
/** |
414 |
+ |
* iterator.remove removes current element |
415 |
+ |
*/ |
416 |
|
public void testIteratorRemove () { |
417 |
|
final LinkedList q = new LinkedList(); |
292 |
– |
|
418 |
|
q.add(new Integer(1)); |
419 |
|
q.add(new Integer(2)); |
420 |
|
q.add(new Integer(3)); |
296 |
– |
|
421 |
|
Iterator it = q.iterator(); |
422 |
|
it.next(); |
423 |
|
it.remove(); |
300 |
– |
|
424 |
|
it = q.iterator(); |
425 |
|
assertEquals(it.next(), new Integer(2)); |
426 |
|
assertEquals(it.next(), new Integer(3)); |
427 |
|
assertFalse(it.hasNext()); |
428 |
|
} |
429 |
|
|
430 |
< |
|
431 |
< |
public void testToString(){ |
430 |
> |
/** |
431 |
> |
* Descending iterator iterates through all elements |
432 |
> |
*/ |
433 |
> |
public void testDescendingIterator() { |
434 |
|
LinkedList q = populatedQueue(SIZE); |
435 |
< |
String s = q.toString(); |
436 |
< |
for (int i = 0; i < SIZE; ++i) { |
437 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
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 |
|
} |
314 |
– |
} |
315 |
– |
|
316 |
– |
public void testAddFirst(){ |
317 |
– |
LinkedList q = populatedQueue(3); |
318 |
– |
q.addFirst(new Integer(4)); |
319 |
– |
assertEquals(new Integer(4),q.get(0)); |
320 |
– |
} |
321 |
– |
|
322 |
– |
public void testAddLast(){ |
323 |
– |
LinkedList q = populatedQueue(3); |
324 |
– |
q.addLast(new Integer(3)); |
325 |
– |
assertEquals(new Integer(3),q.get(3)); |
326 |
– |
} |
327 |
– |
|
328 |
– |
public void testGetFirst() { |
329 |
– |
LinkedList q = populatedQueue(3); |
330 |
– |
assertEquals(new Integer(0),q.getFirst()); |
331 |
– |
} |
332 |
– |
|
333 |
– |
public void testGetLast() { |
334 |
– |
LinkedList q = populatedQueue(3); |
335 |
– |
assertEquals(new Integer(2),q.getLast()); |
336 |
– |
} |
337 |
– |
|
338 |
– |
public void testIndexOf(){ |
339 |
– |
LinkedList q = populatedQueue(3); |
340 |
– |
assertEquals(0,q.indexOf(new Integer(0))); |
341 |
– |
assertEquals(1,q.indexOf(new Integer(1))); |
342 |
– |
assertEquals(2,q.indexOf(new Integer(2))); |
343 |
– |
assertEquals(-1, q.indexOf("not there")); |
447 |
|
} |
448 |
|
|
449 |
< |
public void testLastIndexOf(){ |
450 |
< |
LinkedList q = populatedQueue(3); |
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 |
< |
assertEquals(3,q.lastIndexOf(new Integer(2))); |
457 |
< |
assertEquals(-1, q.lastIndexOf("not there")); |
458 |
< |
} |
459 |
< |
|
460 |
< |
public void testSet(){ |
461 |
< |
LinkedList q = populatedQueue(3); |
355 |
< |
q.set(0,(new Integer(1))); |
356 |
< |
assertFalse(q.contains(new Integer(0))); |
357 |
< |
assertEquals(new Integer(1), q.get(0)); |
358 |
< |
} |
359 |
< |
|
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 |
< |
public void testGetFirst_NoSuchElementException(){ |
362 |
< |
try { |
363 |
< |
LinkedList l = new LinkedList(); |
364 |
< |
l.getFirst(); |
365 |
< |
fail("First Element"); |
366 |
< |
} |
367 |
< |
catch(NoSuchElementException success) {} |
368 |
< |
} |
369 |
< |
|
370 |
< |
public void testRemoveFirst() { |
371 |
< |
try { |
372 |
< |
LinkedList l = new LinkedList(); |
373 |
< |
l.removeFirst(); |
374 |
< |
fail("R: First Element"); |
375 |
< |
} |
376 |
< |
catch(NoSuchElementException success) {} |
463 |
> |
assertEquals(3, k); |
464 |
|
} |
465 |
< |
|
466 |
< |
public void testRemoveLast() { |
467 |
< |
try { |
468 |
< |
LinkedList l = new LinkedList(); |
469 |
< |
l.removeLast(); |
470 |
< |
fail("R: Last Element"); |
471 |
< |
} |
472 |
< |
catch(NoSuchElementException success) {} |
473 |
< |
} |
474 |
< |
|
475 |
< |
public void testGetLast_NoSuchElementException(){ |
476 |
< |
try { |
477 |
< |
LinkedList l = new LinkedList(); |
478 |
< |
l.getLast(); |
479 |
< |
fail("Last Element"); |
480 |
< |
} |
394 |
< |
catch(NoSuchElementException success) {} |
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 |
< |
public void testAddAll_NullPointerException(){ |
485 |
< |
try { |
486 |
< |
LinkedList l = new LinkedList(); |
487 |
< |
l.addAll((Collection)null); |
488 |
< |
fail("Add All Failed"); |
489 |
< |
} |
490 |
< |
catch(NullPointerException success){} |
491 |
< |
} |
492 |
< |
|
407 |
< |
|
408 |
< |
public void testAddAll1_OutOfBounds() { |
409 |
< |
try { |
410 |
< |
LinkedList l = new LinkedList(); |
411 |
< |
l.addAll(4,new LinkedList()); |
412 |
< |
fail("boolean addAll(int, Collection) should throw IndexOutOfBoundsException"); |
413 |
< |
} |
414 |
< |
catch(IndexOutOfBoundsException success) {} |
415 |
< |
} |
416 |
< |
|
417 |
< |
|
418 |
< |
public void testAddAll2_IndexOutOfBoundsException() { |
419 |
< |
try { |
420 |
< |
LinkedList l = new LinkedList(); |
421 |
< |
l.add(new Object()); |
422 |
< |
LinkedList m = new LinkedList(); |
423 |
< |
m.add(new Object()); |
424 |
< |
l.addAll(4,m); |
425 |
< |
fail("Add All Failed " + l.size()); |
426 |
< |
}catch(IndexOutOfBoundsException success) {} |
484 |
> |
/** |
485 |
> |
* toString contains toStrings of elements |
486 |
> |
*/ |
487 |
> |
public void testToString() { |
488 |
> |
LinkedList q = populatedQueue(SIZE); |
489 |
> |
String s = q.toString(); |
490 |
> |
for (int i = 0; i < SIZE; ++i) { |
491 |
> |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
492 |
> |
} |
493 |
|
} |
494 |
|
|
495 |
< |
public void testAddAll4_BadIndex() { |
496 |
< |
try { |
497 |
< |
LinkedList l = new LinkedList(); |
498 |
< |
l.add(new Object()); |
499 |
< |
LinkedList m = new LinkedList(); |
500 |
< |
m.add(new Object()); |
501 |
< |
l.addAll(-1,m); |
436 |
< |
fail("Add All Failed " + l.size()); |
437 |
< |
}catch(IndexOutOfBoundsException success){} |
495 |
> |
/** |
496 |
> |
* peek returns element inserted with addFirst |
497 |
> |
*/ |
498 |
> |
public void testAddFirst() { |
499 |
> |
LinkedList q = populatedQueue(3); |
500 |
> |
q.addFirst(four); |
501 |
> |
assertEquals(four,q.peek()); |
502 |
|
} |
503 |
|
|
504 |
< |
public void testget1() { |
505 |
< |
try { |
506 |
< |
LinkedList l = new LinkedList(); |
507 |
< |
l.add(new Object()); |
508 |
< |
l.get(-1); |
509 |
< |
fail("get Failed - l.get(-1)"); |
510 |
< |
}catch(IndexOutOfBoundsException success) {} |
504 |
> |
/** |
505 |
> |
* peekFirst returns element inserted with push |
506 |
> |
*/ |
507 |
> |
public void testPush() { |
508 |
> |
LinkedList q = populatedQueue(3); |
509 |
> |
q.pollLast(); |
510 |
> |
q.push(four); |
511 |
> |
assertEquals(four,q.peekFirst()); |
512 |
|
} |
513 |
|
|
514 |
< |
public void testget2() { |
515 |
< |
try { |
516 |
< |
LinkedList l = new LinkedList(); |
517 |
< |
l.add(new Object()); |
518 |
< |
l.get(5); |
519 |
< |
fail("get Failed - l.get(5) l.size(): " + l.size()); |
520 |
< |
}catch(IndexOutOfBoundsException success){} |
514 |
> |
/** |
515 |
> |
* pop removes next element, or throws NSEE if empty |
516 |
> |
*/ |
517 |
> |
public void testPop() { |
518 |
> |
LinkedList q = populatedQueue(SIZE); |
519 |
> |
for (int i = 0; i < SIZE; ++i) { |
520 |
> |
assertEquals(i, ((Integer)q.pop()).intValue()); |
521 |
> |
} |
522 |
> |
try { |
523 |
> |
q.pop(); |
524 |
> |
shouldThrow(); |
525 |
> |
} catch (NoSuchElementException success){ |
526 |
> |
} |
527 |
|
} |
528 |
|
|
529 |
< |
public void testset1() { |
530 |
< |
try { |
531 |
< |
LinkedList l = new LinkedList(); |
532 |
< |
l.add(new Object()); |
533 |
< |
l.set(-1,new Object()); |
534 |
< |
fail("set failed - l.set(-1,...)" + l.size()); |
535 |
< |
}catch(IndexOutOfBoundsException success){} |
529 |
> |
/** |
530 |
> |
* OfferFirst succeeds |
531 |
> |
*/ |
532 |
> |
public void testOfferFirst() { |
533 |
> |
LinkedList q = new LinkedList(); |
534 |
> |
assertTrue(q.offerFirst(new Integer(0))); |
535 |
> |
assertTrue(q.offerFirst(new Integer(1))); |
536 |
|
} |
537 |
|
|
538 |
< |
public void testset2() { |
539 |
< |
try { |
540 |
< |
LinkedList l = new LinkedList(); |
541 |
< |
l.add(new Object()); |
542 |
< |
l.set(5,new Object()); |
543 |
< |
fail("set failed = l.set(5,..) l.size():" + l.size()); |
544 |
< |
}catch(IndexOutOfBoundsException success){} |
538 |
> |
/** |
539 |
> |
* OfferLast succeeds |
540 |
> |
*/ |
541 |
> |
public void testOfferLast() { |
542 |
> |
LinkedList q = new LinkedList(); |
543 |
> |
assertTrue(q.offerLast(new Integer(0))); |
544 |
> |
assertTrue(q.offerLast(new Integer(1))); |
545 |
|
} |
546 |
|
|
547 |
< |
public void testadd1() { |
548 |
< |
try { |
549 |
< |
LinkedList l = new LinkedList(); |
550 |
< |
l.add(new Object()); |
551 |
< |
l.add(-1,new Object()); |
552 |
< |
fail("Add Failed - l.add(-1) l.size(): " + l.size()); |
553 |
< |
}catch(IndexOutOfBoundsException success){} |
547 |
> |
/** |
548 |
> |
* pollLast succeeds unless empty |
549 |
> |
*/ |
550 |
> |
public void testPollLast() { |
551 |
> |
LinkedList q = populatedQueue(SIZE); |
552 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
553 |
> |
assertEquals(i, ((Integer)q.pollLast()).intValue()); |
554 |
> |
} |
555 |
> |
assertNull(q.pollLast()); |
556 |
|
} |
557 |
|
|
558 |
< |
public void add2(){ |
559 |
< |
try { |
560 |
< |
LinkedList l = new LinkedList(); |
561 |
< |
l.add(new Object()); |
562 |
< |
l.add(5,new Object()); |
563 |
< |
fail("Add Failed l.add(f,...)"); |
564 |
< |
}catch(IndexOutOfBoundsException success) {} |
558 |
> |
/** |
559 |
> |
* peekFirst returns next element, or null if empty |
560 |
> |
*/ |
561 |
> |
public void testPeekFirst() { |
562 |
> |
LinkedList q = populatedQueue(SIZE); |
563 |
> |
for (int i = 0; i < SIZE; ++i) { |
564 |
> |
assertEquals(i, ((Integer)q.peekFirst()).intValue()); |
565 |
> |
q.pollFirst(); |
566 |
> |
assertTrue(q.peekFirst() == null || |
567 |
> |
i != ((Integer)q.peekFirst()).intValue()); |
568 |
> |
} |
569 |
> |
assertNull(q.peekFirst()); |
570 |
|
} |
571 |
|
|
494 |
– |
public void testremove(){ |
495 |
– |
try { |
496 |
– |
LinkedList l = new LinkedList(); |
497 |
– |
l.add(new Object()); |
498 |
– |
l.remove(-1); |
499 |
– |
fail("Remove Failed l.remove(-1); l.size():" + l.size()); |
500 |
– |
}catch(IndexOutOfBoundsException success){} |
501 |
– |
} |
502 |
– |
|
503 |
– |
public void testremove1(){ |
504 |
– |
try { |
505 |
– |
LinkedList l = new LinkedList(); |
506 |
– |
l.add(new Object()); |
507 |
– |
l.remove(5); |
508 |
– |
fail("Remove Failed l.remove(5); l.size():" + l.size()); |
509 |
– |
}catch(IndexOutOfBoundsException success){} |
510 |
– |
} |
572 |
|
|
573 |
< |
|
574 |
< |
public void testremove2(){ |
575 |
< |
try{ |
576 |
< |
LinkedList l = new LinkedList(); |
577 |
< |
l.remove(); |
578 |
< |
fail("LinkedList - Object remove() should throw a NoSuchElementException"); |
579 |
< |
}catch(NoSuchElementException e){} |
573 |
> |
/** |
574 |
> |
* peekLast returns next element, or null if empty |
575 |
> |
*/ |
576 |
> |
public void testPeekLast() { |
577 |
> |
LinkedList q = populatedQueue(SIZE); |
578 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
579 |
> |
assertEquals(i, ((Integer)q.peekLast()).intValue()); |
580 |
> |
q.pollLast(); |
581 |
> |
assertTrue(q.peekLast() == null || |
582 |
> |
i != ((Integer)q.peekLast()).intValue()); |
583 |
> |
} |
584 |
> |
assertNull(q.peekLast()); |
585 |
|
} |
586 |
|
|
587 |
< |
public void testlistIt1() { |
588 |
< |
try { |
589 |
< |
LinkedList l = new LinkedList(); |
590 |
< |
l.add(new Object()); |
591 |
< |
l.listIterator(5); |
592 |
< |
fail("l.listIterator(5) l.size():" + l.size()); |
593 |
< |
}catch(IndexOutOfBoundsException success){} |
594 |
< |
} |
595 |
< |
|
596 |
< |
public void testlistIt2() { |
597 |
< |
try { |
532 |
< |
LinkedList l = new LinkedList(); |
533 |
< |
l.add(new Object()); |
534 |
< |
l.listIterator(-1); |
535 |
< |
fail("l.listIterator(-1) l.size():" + l.size()); |
536 |
< |
}catch(IndexOutOfBoundsException success){} |
587 |
> |
public void testFirstElement() { |
588 |
> |
LinkedList q = populatedQueue(SIZE); |
589 |
> |
for (int i = 0; i < SIZE; ++i) { |
590 |
> |
assertEquals(i, ((Integer)q.getFirst()).intValue()); |
591 |
> |
q.pollFirst(); |
592 |
> |
} |
593 |
> |
try { |
594 |
> |
q.getFirst(); |
595 |
> |
shouldThrow(); |
596 |
> |
} |
597 |
> |
catch (NoSuchElementException success) {} |
598 |
|
} |
599 |
< |
|
600 |
< |
public void testlistIt3() { |
601 |
< |
try { |
602 |
< |
LinkedList l = new LinkedList(); |
603 |
< |
l.add(new Object()); |
604 |
< |
ListIterator a = l.listIterator(0); |
605 |
< |
l.removeFirst(); |
606 |
< |
a.next(); |
607 |
< |
fail("l.listIterator(-1) l.size():" + l.size()); |
608 |
< |
}catch(ConcurrentModificationException success){} |
599 |
> |
|
600 |
> |
/** |
601 |
> |
* getLast returns next element, or throws NSEE if empty |
602 |
> |
*/ |
603 |
> |
public void testLastElement() { |
604 |
> |
LinkedList q = populatedQueue(SIZE); |
605 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
606 |
> |
assertEquals(i, ((Integer)q.getLast()).intValue()); |
607 |
> |
q.pollLast(); |
608 |
> |
} |
609 |
> |
try { |
610 |
> |
q.getLast(); |
611 |
> |
shouldThrow(); |
612 |
> |
} |
613 |
> |
catch (NoSuchElementException success) {} |
614 |
> |
assertNull(q.peekLast()); |
615 |
|
} |
616 |
|
|
617 |
< |
public void testToArray_BadArg() { |
618 |
< |
try { |
619 |
< |
LinkedList l = new LinkedList(); |
620 |
< |
l.add(new Object()); |
621 |
< |
Object o[] = l.toArray(null); |
622 |
< |
fail("l.toArray(null) did not throw an exception"); |
623 |
< |
}catch(NullPointerException success){} |
617 |
> |
/** |
618 |
> |
* removeFirstOccurrence(x) removes x and returns true if present |
619 |
> |
*/ |
620 |
> |
public void testRemoveFirstOccurrence() { |
621 |
> |
LinkedList q = populatedQueue(SIZE); |
622 |
> |
for (int i = 1; i < SIZE; i+=2) { |
623 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
624 |
> |
} |
625 |
> |
for (int i = 0; i < SIZE; i+=2) { |
626 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
627 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
628 |
> |
} |
629 |
> |
assertTrue(q.isEmpty()); |
630 |
|
} |
631 |
|
|
632 |
< |
public void testToArray1_BadArg() { |
633 |
< |
try { |
634 |
< |
LinkedList l = new LinkedList(); |
635 |
< |
l.add(new Integer(5)); |
636 |
< |
Object o[] = l.toArray(new String[10] ); |
637 |
< |
fail("l.toArray(String[] f) did not throw an exception, an Integer was added"); |
638 |
< |
}catch(ArrayStoreException success){} |
632 |
> |
/** |
633 |
> |
* removeLastOccurrence(x) removes x and returns true if present |
634 |
> |
*/ |
635 |
> |
public void testRemoveLastOccurrence() { |
636 |
> |
LinkedList q = populatedQueue(SIZE); |
637 |
> |
for (int i = 1; i < SIZE; i+=2) { |
638 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
639 |
> |
} |
640 |
> |
for (int i = 0; i < SIZE; i+=2) { |
641 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
642 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
643 |
> |
} |
644 |
> |
assertTrue(q.isEmpty()); |
645 |
|
} |
646 |
|
|
647 |
|
} |