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 |
/** |
36 |
* new queue is empty |
37 |
*/ |
38 |
public void testConstructor1() { |
39 |
assertEquals(0, new LinkedList().size()); |
40 |
} |
41 |
|
42 |
/** |
43 |
* Initializing from null Collection throws NPE |
44 |
*/ |
45 |
public void testConstructor3() { |
46 |
try { |
47 |
LinkedList q = new LinkedList((Collection)null); |
48 |
shouldThrow(); |
49 |
} |
50 |
catch (NullPointerException success) {} |
51 |
} |
52 |
|
53 |
/** |
54 |
* Queue contains all elements of collection used to initialize |
55 |
|
56 |
*/ |
57 |
public void testConstructor6() { |
58 |
try { |
59 |
Integer[] ints = new Integer[SIZE]; |
60 |
for (int i = 0; i < SIZE; ++i) |
61 |
ints[i] = new Integer(i); |
62 |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
63 |
for (int i = 0; i < SIZE; ++i) |
64 |
assertEquals(ints[i], q.poll()); |
65 |
} |
66 |
finally {} |
67 |
} |
68 |
|
69 |
/** |
70 |
* isEmpty is true before add, false after |
71 |
*/ |
72 |
public void testEmpty() { |
73 |
LinkedList q = new LinkedList(); |
74 |
assertTrue(q.isEmpty()); |
75 |
q.add(new Integer(1)); |
76 |
assertFalse(q.isEmpty()); |
77 |
q.add(new Integer(2)); |
78 |
q.remove(); |
79 |
q.remove(); |
80 |
assertTrue(q.isEmpty()); |
81 |
} |
82 |
|
83 |
/** |
84 |
* size changes when elements added and removed |
85 |
*/ |
86 |
public void testSize() { |
87 |
LinkedList q = populatedQueue(SIZE); |
88 |
for (int i = 0; i < SIZE; ++i) { |
89 |
assertEquals(SIZE-i, q.size()); |
90 |
q.remove(); |
91 |
} |
92 |
for (int i = 0; i < SIZE; ++i) { |
93 |
assertEquals(i, q.size()); |
94 |
q.add(new Integer(i)); |
95 |
} |
96 |
} |
97 |
|
98 |
/** |
99 |
* offer(null) succeeds |
100 |
*/ |
101 |
public void testOfferNull() { |
102 |
try { |
103 |
LinkedList q = new LinkedList(); |
104 |
q.offer(null); |
105 |
} catch (NullPointerException ie) { |
106 |
unexpectedException(); |
107 |
} |
108 |
} |
109 |
|
110 |
/** |
111 |
* Offer succeeds |
112 |
*/ |
113 |
public void testOffer() { |
114 |
LinkedList q = new LinkedList(); |
115 |
assertTrue(q.offer(new Integer(0))); |
116 |
assertTrue(q.offer(new Integer(1))); |
117 |
} |
118 |
|
119 |
/** |
120 |
* add succeeds |
121 |
*/ |
122 |
public void testAdd() { |
123 |
LinkedList q = new LinkedList(); |
124 |
for (int i = 0; i < SIZE; ++i) { |
125 |
assertEquals(i, q.size()); |
126 |
assertTrue(q.add(new Integer(i))); |
127 |
} |
128 |
} |
129 |
|
130 |
/** |
131 |
* addAll(null) throws NPE |
132 |
*/ |
133 |
public void testAddAll1() { |
134 |
try { |
135 |
LinkedList q = new LinkedList(); |
136 |
q.addAll(null); |
137 |
shouldThrow(); |
138 |
} |
139 |
catch (NullPointerException success) {} |
140 |
} |
141 |
|
142 |
/** |
143 |
* Queue contains all elements, in traversal order, of successful addAll |
144 |
*/ |
145 |
public void testAddAll5() { |
146 |
try { |
147 |
Integer[] empty = new Integer[0]; |
148 |
Integer[] ints = new Integer[SIZE]; |
149 |
for (int i = 0; i < SIZE; ++i) |
150 |
ints[i] = new Integer(i); |
151 |
LinkedList q = new LinkedList(); |
152 |
assertFalse(q.addAll(Arrays.asList(empty))); |
153 |
assertTrue(q.addAll(Arrays.asList(ints))); |
154 |
for (int i = 0; i < SIZE; ++i) |
155 |
assertEquals(ints[i], q.poll()); |
156 |
} |
157 |
finally {} |
158 |
} |
159 |
|
160 |
/** |
161 |
* addAll with too large an index throws IOOBE |
162 |
*/ |
163 |
public void testAddAll2_IndexOutOfBoundsException() { |
164 |
try { |
165 |
LinkedList l = new LinkedList(); |
166 |
l.add(new Object()); |
167 |
LinkedList m = new LinkedList(); |
168 |
m.add(new Object()); |
169 |
l.addAll(4,m); |
170 |
shouldThrow(); |
171 |
} catch(IndexOutOfBoundsException success) {} |
172 |
} |
173 |
|
174 |
/** |
175 |
* addAll with negative index throws IOOBE |
176 |
*/ |
177 |
public void testAddAll4_BadIndex() { |
178 |
try { |
179 |
LinkedList l = new LinkedList(); |
180 |
l.add(new Object()); |
181 |
LinkedList m = new LinkedList(); |
182 |
m.add(new Object()); |
183 |
l.addAll(-1,m); |
184 |
shouldThrow(); |
185 |
} catch(IndexOutOfBoundsException success){} |
186 |
} |
187 |
|
188 |
/** |
189 |
* poll succeeds unless empty |
190 |
*/ |
191 |
public void testPoll() { |
192 |
LinkedList q = populatedQueue(SIZE); |
193 |
for (int i = 0; i < SIZE; ++i) { |
194 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
195 |
} |
196 |
assertNull(q.poll()); |
197 |
} |
198 |
|
199 |
/** |
200 |
* peek returns next element, or null if empty |
201 |
*/ |
202 |
public void testPeek() { |
203 |
LinkedList q = populatedQueue(SIZE); |
204 |
for (int i = 0; i < SIZE; ++i) { |
205 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
206 |
q.poll(); |
207 |
assertTrue(q.peek() == null || |
208 |
i != ((Integer)q.peek()).intValue()); |
209 |
} |
210 |
assertNull(q.peek()); |
211 |
} |
212 |
|
213 |
/** |
214 |
* |
215 |
*/ |
216 |
public void testElement() { |
217 |
LinkedList q = populatedQueue(SIZE); |
218 |
for (int i = 0; i < SIZE; ++i) { |
219 |
assertEquals(i, ((Integer)q.element()).intValue()); |
220 |
q.poll(); |
221 |
} |
222 |
try { |
223 |
q.element(); |
224 |
shouldThrow(); |
225 |
} |
226 |
catch (NoSuchElementException success) {} |
227 |
} |
228 |
|
229 |
/** |
230 |
* element returns next element, or throws NSEE if empty |
231 |
*/ |
232 |
public void testRemove() { |
233 |
LinkedList q = populatedQueue(SIZE); |
234 |
for (int i = 0; i < SIZE; ++i) { |
235 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
236 |
} |
237 |
try { |
238 |
q.remove(); |
239 |
shouldThrow(); |
240 |
} catch (NoSuchElementException success){ |
241 |
} |
242 |
} |
243 |
|
244 |
/** |
245 |
* remove(x) removes x and returns true if present |
246 |
*/ |
247 |
public void testRemoveElement() { |
248 |
LinkedList q = populatedQueue(SIZE); |
249 |
for (int i = 1; i < SIZE; i+=2) { |
250 |
assertTrue(q.remove(new Integer(i))); |
251 |
} |
252 |
for (int i = 0; i < SIZE; i+=2) { |
253 |
assertTrue(q.remove(new Integer(i))); |
254 |
assertFalse(q.remove(new Integer(i+1))); |
255 |
} |
256 |
assertTrue(q.isEmpty()); |
257 |
} |
258 |
|
259 |
/** |
260 |
* contains(x) reports true when elements added but not yet removed |
261 |
*/ |
262 |
public void testContains() { |
263 |
LinkedList q = populatedQueue(SIZE); |
264 |
for (int i = 0; i < SIZE; ++i) { |
265 |
assertTrue(q.contains(new Integer(i))); |
266 |
q.poll(); |
267 |
assertFalse(q.contains(new Integer(i))); |
268 |
} |
269 |
} |
270 |
|
271 |
/** |
272 |
* clear removes all elements |
273 |
*/ |
274 |
public void testClear() { |
275 |
LinkedList q = populatedQueue(SIZE); |
276 |
q.clear(); |
277 |
assertTrue(q.isEmpty()); |
278 |
assertEquals(0, q.size()); |
279 |
q.add(new Integer(1)); |
280 |
assertFalse(q.isEmpty()); |
281 |
q.clear(); |
282 |
assertTrue(q.isEmpty()); |
283 |
} |
284 |
|
285 |
/** |
286 |
* containsAll(c) is true when c contains a subset of elements |
287 |
*/ |
288 |
public void testContainsAll() { |
289 |
LinkedList q = populatedQueue(SIZE); |
290 |
LinkedList p = new LinkedList(); |
291 |
for (int i = 0; i < SIZE; ++i) { |
292 |
assertTrue(q.containsAll(p)); |
293 |
assertFalse(p.containsAll(q)); |
294 |
p.add(new Integer(i)); |
295 |
} |
296 |
assertTrue(p.containsAll(q)); |
297 |
} |
298 |
|
299 |
/** |
300 |
* retainAll(c) retains only those elements of c and reports true if changed |
301 |
*/ |
302 |
public void testRetainAll() { |
303 |
LinkedList q = populatedQueue(SIZE); |
304 |
LinkedList p = populatedQueue(SIZE); |
305 |
for (int i = 0; i < SIZE; ++i) { |
306 |
boolean changed = q.retainAll(p); |
307 |
if (i == 0) |
308 |
assertFalse(changed); |
309 |
else |
310 |
assertTrue(changed); |
311 |
|
312 |
assertTrue(q.containsAll(p)); |
313 |
assertEquals(SIZE-i, q.size()); |
314 |
p.remove(); |
315 |
} |
316 |
} |
317 |
|
318 |
/** |
319 |
* removeAll(c) removes only those elements of c and reports true if changed |
320 |
*/ |
321 |
public void testRemoveAll() { |
322 |
for (int i = 1; i < SIZE; ++i) { |
323 |
LinkedList q = populatedQueue(SIZE); |
324 |
LinkedList p = populatedQueue(i); |
325 |
assertTrue(q.removeAll(p)); |
326 |
assertEquals(SIZE-i, q.size()); |
327 |
for (int j = 0; j < i; ++j) { |
328 |
Integer I = (Integer)(p.remove()); |
329 |
assertFalse(q.contains(I)); |
330 |
} |
331 |
} |
332 |
} |
333 |
|
334 |
/** |
335 |
* toArray contains all elements |
336 |
*/ |
337 |
public void testToArray() { |
338 |
LinkedList q = populatedQueue(SIZE); |
339 |
Object[] o = q.toArray(); |
340 |
Arrays.sort(o); |
341 |
for(int i = 0; i < o.length; i++) |
342 |
assertEquals(o[i], q.poll()); |
343 |
} |
344 |
|
345 |
/** |
346 |
* toArray(a) contains all elements |
347 |
*/ |
348 |
public void testToArray2() { |
349 |
LinkedList q = populatedQueue(SIZE); |
350 |
Integer[] ints = new Integer[SIZE]; |
351 |
ints = (Integer[])q.toArray(ints); |
352 |
Arrays.sort(ints); |
353 |
for(int i = 0; i < ints.length; i++) |
354 |
assertEquals(ints[i], q.poll()); |
355 |
} |
356 |
|
357 |
/** |
358 |
* toArray(null) throws NPE |
359 |
*/ |
360 |
public void testToArray_BadArg() { |
361 |
try { |
362 |
LinkedList l = new LinkedList(); |
363 |
l.add(new Object()); |
364 |
Object o[] = l.toArray(null); |
365 |
shouldThrow(); |
366 |
} catch(NullPointerException success){} |
367 |
} |
368 |
|
369 |
/** |
370 |
* toArray with incompatable aray type throws CCE |
371 |
*/ |
372 |
public void testToArray1_BadArg() { |
373 |
try { |
374 |
LinkedList l = new LinkedList(); |
375 |
l.add(new Integer(5)); |
376 |
Object o[] = l.toArray(new String[10] ); |
377 |
shouldThrow(); |
378 |
} catch(ArrayStoreException success){} |
379 |
} |
380 |
|
381 |
/** |
382 |
* iterator iterates through all elements |
383 |
*/ |
384 |
public void testIterator() { |
385 |
LinkedList q = populatedQueue(SIZE); |
386 |
int i = 0; |
387 |
Iterator it = q.iterator(); |
388 |
while(it.hasNext()) { |
389 |
assertTrue(q.contains(it.next())); |
390 |
++i; |
391 |
} |
392 |
assertEquals(i, SIZE); |
393 |
} |
394 |
|
395 |
/** |
396 |
* iterator ordering is FIFO |
397 |
*/ |
398 |
public void testIteratorOrdering() { |
399 |
final LinkedList q = new LinkedList(); |
400 |
q.add(new Integer(1)); |
401 |
q.add(new Integer(2)); |
402 |
q.add(new Integer(3)); |
403 |
int k = 0; |
404 |
for (Iterator it = q.iterator(); it.hasNext();) { |
405 |
int i = ((Integer)(it.next())).intValue(); |
406 |
assertEquals(++k, i); |
407 |
} |
408 |
|
409 |
assertEquals(3, k); |
410 |
} |
411 |
|
412 |
/** |
413 |
* iterator.remove removes current element |
414 |
*/ |
415 |
public void testIteratorRemove () { |
416 |
final LinkedList q = new LinkedList(); |
417 |
q.add(new Integer(1)); |
418 |
q.add(new Integer(2)); |
419 |
q.add(new Integer(3)); |
420 |
Iterator it = q.iterator(); |
421 |
it.next(); |
422 |
it.remove(); |
423 |
it = q.iterator(); |
424 |
assertEquals(it.next(), new Integer(2)); |
425 |
assertEquals(it.next(), new Integer(3)); |
426 |
assertFalse(it.hasNext()); |
427 |
} |
428 |
|
429 |
|
430 |
/** |
431 |
* toString contains toStrings of elements |
432 |
*/ |
433 |
public void testToString() { |
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); |
438 |
} |
439 |
} |
440 |
|
441 |
/** |
442 |
* peek returns element inserted with addFirst |
443 |
*/ |
444 |
public void testAddFirst() { |
445 |
LinkedList q = populatedQueue(3); |
446 |
q.addFirst(four); |
447 |
assertEquals(four,q.peek()); |
448 |
} |
449 |
|
450 |
} |