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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
|
12 |
public class LinkedListTest extends JSR166TestCase { |
13 |
public static void main(String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
|
17 |
public static Test suite() { |
18 |
return new TestSuite(LinkedListTest.class); |
19 |
} |
20 |
|
21 |
/** |
22 |
* Create a queue of given size containing consecutive |
23 |
* Integers 0 ... n. |
24 |
*/ |
25 |
private LinkedList populatedQueue(int n) { |
26 |
LinkedList q = new LinkedList(); |
27 |
assertTrue(q.isEmpty()); |
28 |
for(int i = 0; i < n; ++i) |
29 |
assertTrue(q.offer(new Integer(i))); |
30 |
assertFalse(q.isEmpty()); |
31 |
assertEquals(n, q.size()); |
32 |
return q; |
33 |
} |
34 |
|
35 |
public void testConstructor1(){ |
36 |
assertEquals(0, new LinkedList().size()); |
37 |
} |
38 |
|
39 |
public void testConstructor3() { |
40 |
try { |
41 |
LinkedList q = new LinkedList((Collection)null); |
42 |
fail("Cannot make from null collection"); |
43 |
} |
44 |
catch (NullPointerException success) {} |
45 |
} |
46 |
|
47 |
public void testConstructor6(){ |
48 |
try { |
49 |
Integer[] ints = new Integer[SIZE]; |
50 |
for (int i = 0; i < SIZE; ++i) |
51 |
ints[i] = new Integer(i); |
52 |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
53 |
for (int i = 0; i < SIZE; ++i) |
54 |
assertEquals(ints[i], q.poll()); |
55 |
} |
56 |
finally {} |
57 |
} |
58 |
|
59 |
public void testEmpty() { |
60 |
LinkedList q = new LinkedList(); |
61 |
assertTrue(q.isEmpty()); |
62 |
q.add(new Integer(1)); |
63 |
assertFalse(q.isEmpty()); |
64 |
q.add(new Integer(2)); |
65 |
q.remove(); |
66 |
q.remove(); |
67 |
assertTrue(q.isEmpty()); |
68 |
} |
69 |
|
70 |
public void testSize() { |
71 |
LinkedList q = populatedQueue(SIZE); |
72 |
for (int i = 0; i < SIZE; ++i) { |
73 |
assertEquals(SIZE-i, q.size()); |
74 |
q.remove(); |
75 |
} |
76 |
for (int i = 0; i < SIZE; ++i) { |
77 |
assertEquals(i, q.size()); |
78 |
q.add(new Integer(i)); |
79 |
} |
80 |
} |
81 |
|
82 |
public void testOfferNull(){ |
83 |
try { |
84 |
LinkedList q = new LinkedList(); |
85 |
q.offer(null); |
86 |
} catch (NullPointerException ie) { |
87 |
fail("should not throw NPE"); |
88 |
} |
89 |
} |
90 |
|
91 |
public void testOffer() { |
92 |
LinkedList q = new LinkedList(); |
93 |
assertTrue(q.offer(new Integer(0))); |
94 |
assertTrue(q.offer(new Integer(1))); |
95 |
} |
96 |
|
97 |
public void testAdd(){ |
98 |
LinkedList q = new LinkedList(); |
99 |
for (int i = 0; i < SIZE; ++i) { |
100 |
assertEquals(i, q.size()); |
101 |
assertTrue(q.add(new Integer(i))); |
102 |
} |
103 |
} |
104 |
|
105 |
public void testAddAll1(){ |
106 |
try { |
107 |
LinkedList q = new LinkedList(); |
108 |
q.addAll(null); |
109 |
fail("Cannot add null collection"); |
110 |
} |
111 |
catch (NullPointerException success) {} |
112 |
} |
113 |
|
114 |
public void testAddAll5(){ |
115 |
try { |
116 |
Integer[] empty = new Integer[0]; |
117 |
Integer[] ints = new Integer[SIZE]; |
118 |
for (int i = 0; i < SIZE; ++i) |
119 |
ints[i] = new Integer(i); |
120 |
LinkedList q = new LinkedList(); |
121 |
assertFalse(q.addAll(Arrays.asList(empty))); |
122 |
assertTrue(q.addAll(Arrays.asList(ints))); |
123 |
for (int i = 0; i < SIZE; ++i) |
124 |
assertEquals(ints[i], q.poll()); |
125 |
} |
126 |
finally {} |
127 |
} |
128 |
|
129 |
public void testPoll(){ |
130 |
LinkedList q = populatedQueue(SIZE); |
131 |
for (int i = 0; i < SIZE; ++i) { |
132 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
133 |
} |
134 |
assertNull(q.poll()); |
135 |
} |
136 |
|
137 |
public void testPeek(){ |
138 |
LinkedList q = populatedQueue(SIZE); |
139 |
for (int i = 0; i < SIZE; ++i) { |
140 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
141 |
q.poll(); |
142 |
assertTrue(q.peek() == null || |
143 |
i != ((Integer)q.peek()).intValue()); |
144 |
} |
145 |
assertNull(q.peek()); |
146 |
} |
147 |
|
148 |
public void testElement(){ |
149 |
LinkedList q = populatedQueue(SIZE); |
150 |
for (int i = 0; i < SIZE; ++i) { |
151 |
assertEquals(i, ((Integer)q.element()).intValue()); |
152 |
q.poll(); |
153 |
} |
154 |
try { |
155 |
q.element(); |
156 |
fail("no such element"); |
157 |
} |
158 |
catch (NoSuchElementException success) {} |
159 |
} |
160 |
|
161 |
public void testRemove(){ |
162 |
LinkedList q = populatedQueue(SIZE); |
163 |
for (int i = 0; i < SIZE; ++i) { |
164 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
165 |
} |
166 |
try { |
167 |
q.remove(); |
168 |
fail("remove should throw"); |
169 |
} catch (NoSuchElementException success){ |
170 |
} |
171 |
} |
172 |
|
173 |
public void testRemoveElement(){ |
174 |
LinkedList q = populatedQueue(SIZE); |
175 |
for (int i = 1; i < SIZE; i+=2) { |
176 |
assertTrue(q.remove(new Integer(i))); |
177 |
} |
178 |
for (int i = 0; i < SIZE; i+=2) { |
179 |
assertTrue(q.remove(new Integer(i))); |
180 |
assertFalse(q.remove(new Integer(i+1))); |
181 |
} |
182 |
assertTrue(q.isEmpty()); |
183 |
} |
184 |
|
185 |
public void testContains(){ |
186 |
LinkedList q = populatedQueue(SIZE); |
187 |
for (int i = 0; i < SIZE; ++i) { |
188 |
assertTrue(q.contains(new Integer(i))); |
189 |
q.poll(); |
190 |
assertFalse(q.contains(new Integer(i))); |
191 |
} |
192 |
} |
193 |
|
194 |
public void testClear(){ |
195 |
LinkedList q = populatedQueue(SIZE); |
196 |
q.clear(); |
197 |
assertTrue(q.isEmpty()); |
198 |
assertEquals(0, q.size()); |
199 |
q.add(new Integer(1)); |
200 |
assertFalse(q.isEmpty()); |
201 |
q.clear(); |
202 |
assertTrue(q.isEmpty()); |
203 |
} |
204 |
|
205 |
public void testContainsAll(){ |
206 |
LinkedList q = populatedQueue(SIZE); |
207 |
LinkedList p = new LinkedList(); |
208 |
for (int i = 0; i < SIZE; ++i) { |
209 |
assertTrue(q.containsAll(p)); |
210 |
assertFalse(p.containsAll(q)); |
211 |
p.add(new Integer(i)); |
212 |
} |
213 |
assertTrue(p.containsAll(q)); |
214 |
} |
215 |
|
216 |
public void testRetainAll(){ |
217 |
LinkedList q = populatedQueue(SIZE); |
218 |
LinkedList p = populatedQueue(SIZE); |
219 |
for (int i = 0; i < SIZE; ++i) { |
220 |
boolean changed = q.retainAll(p); |
221 |
if (i == 0) |
222 |
assertFalse(changed); |
223 |
else |
224 |
assertTrue(changed); |
225 |
|
226 |
assertTrue(q.containsAll(p)); |
227 |
assertEquals(SIZE-i, q.size()); |
228 |
p.remove(); |
229 |
} |
230 |
} |
231 |
|
232 |
public void testRemoveAll(){ |
233 |
for (int i = 1; i < SIZE; ++i) { |
234 |
LinkedList q = populatedQueue(SIZE); |
235 |
LinkedList p = populatedQueue(i); |
236 |
assertTrue(q.removeAll(p)); |
237 |
assertEquals(SIZE-i, q.size()); |
238 |
for (int j = 0; j < i; ++j) { |
239 |
Integer I = (Integer)(p.remove()); |
240 |
assertFalse(q.contains(I)); |
241 |
} |
242 |
} |
243 |
} |
244 |
|
245 |
public void testToArray(){ |
246 |
LinkedList q = populatedQueue(SIZE); |
247 |
Object[] o = q.toArray(); |
248 |
Arrays.sort(o); |
249 |
for(int i = 0; i < o.length; i++) |
250 |
assertEquals(o[i], q.poll()); |
251 |
} |
252 |
|
253 |
public void testToArray2(){ |
254 |
LinkedList q = populatedQueue(SIZE); |
255 |
Integer[] ints = new Integer[SIZE]; |
256 |
ints = (Integer[])q.toArray(ints); |
257 |
Arrays.sort(ints); |
258 |
for(int i = 0; i < ints.length; i++) |
259 |
assertEquals(ints[i], q.poll()); |
260 |
} |
261 |
|
262 |
public void testIterator(){ |
263 |
LinkedList q = populatedQueue(SIZE); |
264 |
int i = 0; |
265 |
Iterator it = q.iterator(); |
266 |
while(it.hasNext()) { |
267 |
assertTrue(q.contains(it.next())); |
268 |
++i; |
269 |
} |
270 |
assertEquals(i, SIZE); |
271 |
} |
272 |
|
273 |
public void testIteratorOrdering() { |
274 |
|
275 |
final LinkedList q = new LinkedList(); |
276 |
|
277 |
q.add(new Integer(1)); |
278 |
q.add(new Integer(2)); |
279 |
q.add(new Integer(3)); |
280 |
|
281 |
int k = 0; |
282 |
for (Iterator it = q.iterator(); it.hasNext();) { |
283 |
int i = ((Integer)(it.next())).intValue(); |
284 |
assertEquals("items should come out in order", ++k, i); |
285 |
} |
286 |
|
287 |
assertEquals("should go through 3 elements", 3, k); |
288 |
} |
289 |
|
290 |
public void testIteratorRemove () { |
291 |
final LinkedList q = new LinkedList(); |
292 |
|
293 |
q.add(new Integer(1)); |
294 |
q.add(new Integer(2)); |
295 |
q.add(new Integer(3)); |
296 |
|
297 |
Iterator it = q.iterator(); |
298 |
it.next(); |
299 |
it.remove(); |
300 |
|
301 |
it = q.iterator(); |
302 |
assertEquals(it.next(), new Integer(2)); |
303 |
assertEquals(it.next(), new Integer(3)); |
304 |
assertFalse(it.hasNext()); |
305 |
} |
306 |
|
307 |
|
308 |
public void testToString(){ |
309 |
LinkedList q = populatedQueue(SIZE); |
310 |
String s = q.toString(); |
311 |
for (int i = 0; i < SIZE; ++i) { |
312 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
313 |
} |
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")); |
344 |
} |
345 |
|
346 |
public void testLastIndexOf(){ |
347 |
LinkedList q = populatedQueue(3); |
348 |
q.add(new Integer(2)); |
349 |
assertEquals(3,q.lastIndexOf(new Integer(2))); |
350 |
assertEquals(-1, q.lastIndexOf("not there")); |
351 |
} |
352 |
|
353 |
public void testSet(){ |
354 |
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 |
|
360 |
|
361 |
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) {} |
377 |
} |
378 |
|
379 |
public void testRemoveLast() { |
380 |
try { |
381 |
LinkedList l = new LinkedList(); |
382 |
l.removeLast(); |
383 |
fail("R: Last Element"); |
384 |
} |
385 |
catch(NoSuchElementException success) {} |
386 |
} |
387 |
|
388 |
public void testGetLast_NoSuchElementException(){ |
389 |
try { |
390 |
LinkedList l = new LinkedList(); |
391 |
l.getLast(); |
392 |
fail("Last Element"); |
393 |
} |
394 |
catch(NoSuchElementException success) {} |
395 |
} |
396 |
|
397 |
|
398 |
public void testAddAll_NullPointerException(){ |
399 |
try { |
400 |
LinkedList l = new LinkedList(); |
401 |
l.addAll((Collection)null); |
402 |
fail("Add All Failed"); |
403 |
} |
404 |
catch(NullPointerException success){} |
405 |
} |
406 |
|
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) {} |
427 |
} |
428 |
|
429 |
public void testAddAll4_BadIndex() { |
430 |
try { |
431 |
LinkedList l = new LinkedList(); |
432 |
l.add(new Object()); |
433 |
LinkedList m = new LinkedList(); |
434 |
m.add(new Object()); |
435 |
l.addAll(-1,m); |
436 |
fail("Add All Failed " + l.size()); |
437 |
}catch(IndexOutOfBoundsException success){} |
438 |
} |
439 |
|
440 |
public void testget1() { |
441 |
try { |
442 |
LinkedList l = new LinkedList(); |
443 |
l.add(new Object()); |
444 |
l.get(-1); |
445 |
fail("get Failed - l.get(-1)"); |
446 |
}catch(IndexOutOfBoundsException success) {} |
447 |
} |
448 |
|
449 |
public void testget2() { |
450 |
try { |
451 |
LinkedList l = new LinkedList(); |
452 |
l.add(new Object()); |
453 |
l.get(5); |
454 |
fail("get Failed - l.get(5) l.size(): " + l.size()); |
455 |
}catch(IndexOutOfBoundsException success){} |
456 |
} |
457 |
|
458 |
public void testset1() { |
459 |
try { |
460 |
LinkedList l = new LinkedList(); |
461 |
l.add(new Object()); |
462 |
l.set(-1,new Object()); |
463 |
fail("set failed - l.set(-1,...)" + l.size()); |
464 |
}catch(IndexOutOfBoundsException success){} |
465 |
} |
466 |
|
467 |
public void testset2() { |
468 |
try { |
469 |
LinkedList l = new LinkedList(); |
470 |
l.add(new Object()); |
471 |
l.set(5,new Object()); |
472 |
fail("set failed = l.set(5,..) l.size():" + l.size()); |
473 |
}catch(IndexOutOfBoundsException success){} |
474 |
} |
475 |
|
476 |
public void testadd1() { |
477 |
try { |
478 |
LinkedList l = new LinkedList(); |
479 |
l.add(new Object()); |
480 |
l.add(-1,new Object()); |
481 |
fail("Add Failed - l.add(-1) l.size(): " + l.size()); |
482 |
}catch(IndexOutOfBoundsException success){} |
483 |
} |
484 |
|
485 |
public void add2(){ |
486 |
try { |
487 |
LinkedList l = new LinkedList(); |
488 |
l.add(new Object()); |
489 |
l.add(5,new Object()); |
490 |
fail("Add Failed l.add(f,...)"); |
491 |
}catch(IndexOutOfBoundsException success) {} |
492 |
} |
493 |
|
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 |
} |
511 |
|
512 |
|
513 |
public void testremove2(){ |
514 |
try{ |
515 |
LinkedList l = new LinkedList(); |
516 |
l.remove(); |
517 |
fail("LinkedList - Object remove() should throw a NoSuchElementException"); |
518 |
}catch(NoSuchElementException e){} |
519 |
} |
520 |
|
521 |
public void testlistIt1() { |
522 |
try { |
523 |
LinkedList l = new LinkedList(); |
524 |
l.add(new Object()); |
525 |
l.listIterator(5); |
526 |
fail("l.listIterator(5) l.size():" + l.size()); |
527 |
}catch(IndexOutOfBoundsException success){} |
528 |
} |
529 |
|
530 |
public void testlistIt2() { |
531 |
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){} |
537 |
} |
538 |
|
539 |
public void testlistIt3() { |
540 |
try { |
541 |
LinkedList l = new LinkedList(); |
542 |
l.add(new Object()); |
543 |
ListIterator a = l.listIterator(0); |
544 |
l.removeFirst(); |
545 |
a.next(); |
546 |
fail("l.listIterator(-1) l.size():" + l.size()); |
547 |
}catch(ConcurrentModificationException success){} |
548 |
} |
549 |
|
550 |
public void testToArray_BadArg() { |
551 |
try { |
552 |
LinkedList l = new LinkedList(); |
553 |
l.add(new Object()); |
554 |
Object o[] = l.toArray(null); |
555 |
fail("l.toArray(null) did not throw an exception"); |
556 |
}catch(NullPointerException success){} |
557 |
} |
558 |
|
559 |
public void testToArray1_BadArg() { |
560 |
try { |
561 |
LinkedList l = new LinkedList(); |
562 |
l.add(new Integer(5)); |
563 |
Object o[] = l.toArray(new String[10] ); |
564 |
fail("l.toArray(String[] f) did not throw an exception, an Integer was added"); |
565 |
}catch(ArrayStoreException success){} |
566 |
} |
567 |
|
568 |
} |