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() { |
19 |
< |
return new TestSuite(LinkedListTest.class); |
19 |
> |
return new TestSuite(LinkedListTest.class); |
20 |
|
} |
21 |
|
|
22 |
|
/** |
26 |
|
private LinkedList populatedQueue(int n) { |
27 |
|
LinkedList q = new LinkedList(); |
28 |
|
assertTrue(q.isEmpty()); |
29 |
< |
for(int i = 0; i < n; ++i) |
30 |
< |
assertTrue(q.offer(new Integer(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()); |
32 |
> |
assertEquals(n, q.size()); |
33 |
|
return q; |
34 |
|
} |
35 |
< |
|
35 |
> |
|
36 |
|
/** |
37 |
< |
* |
37 |
> |
* new queue is empty |
38 |
|
*/ |
39 |
|
public void testConstructor1() { |
40 |
|
assertEquals(0, new LinkedList().size()); |
41 |
|
} |
42 |
|
|
43 |
|
/** |
44 |
< |
* |
44 |
> |
* Initializing from null Collection throws NPE |
45 |
|
*/ |
46 |
|
public void testConstructor3() { |
47 |
|
try { |
48 |
|
LinkedList q = new LinkedList((Collection)null); |
49 |
|
shouldThrow(); |
50 |
< |
} |
50 |
< |
catch (NullPointerException success) {} |
50 |
> |
} catch (NullPointerException success) {} |
51 |
|
} |
52 |
|
|
53 |
|
/** |
54 |
< |
* |
54 |
> |
* Queue contains all elements of collection used to initialize |
55 |
|
*/ |
56 |
|
public void testConstructor6() { |
57 |
< |
try { |
58 |
< |
Integer[] ints = new Integer[SIZE]; |
59 |
< |
for (int i = 0; i < SIZE; ++i) |
60 |
< |
ints[i] = new Integer(i); |
61 |
< |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
62 |
< |
for (int i = 0; i < SIZE; ++i) |
63 |
< |
assertEquals(ints[i], q.poll()); |
64 |
< |
} |
65 |
< |
finally {} |
57 |
> |
Integer[] ints = new Integer[SIZE]; |
58 |
> |
for (int i = 0; i < SIZE; ++i) |
59 |
> |
ints[i] = i; |
60 |
> |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
61 |
> |
for (int i = 0; i < SIZE; ++i) |
62 |
> |
assertEquals(ints[i], q.poll()); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* |
66 |
> |
* isEmpty is true before add, false after |
67 |
|
*/ |
68 |
|
public void testEmpty() { |
69 |
|
LinkedList q = new LinkedList(); |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
< |
* |
80 |
> |
* size changes when elements added and removed |
81 |
|
*/ |
82 |
|
public void testSize() { |
83 |
|
LinkedList q = populatedQueue(SIZE); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
< |
* |
95 |
> |
* offer(null) succeeds |
96 |
|
*/ |
97 |
|
public void testOfferNull() { |
98 |
< |
try { |
99 |
< |
LinkedList q = new LinkedList(); |
103 |
< |
q.offer(null); |
104 |
< |
} catch (NullPointerException ie) { |
105 |
< |
unexpectedException(); |
106 |
< |
} |
98 |
> |
LinkedList q = new LinkedList(); |
99 |
> |
q.offer(null); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
< |
* |
103 |
> |
* Offer succeeds |
104 |
|
*/ |
105 |
|
public void testOffer() { |
106 |
|
LinkedList q = new LinkedList(); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* |
112 |
> |
* add succeeds |
113 |
|
*/ |
114 |
|
public void testAdd() { |
115 |
|
LinkedList q = new LinkedList(); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* |
123 |
> |
* addAll(null) throws NPE |
124 |
|
*/ |
125 |
|
public void testAddAll1() { |
126 |
|
try { |
127 |
|
LinkedList q = new LinkedList(); |
128 |
|
q.addAll(null); |
129 |
|
shouldThrow(); |
130 |
< |
} |
138 |
< |
catch (NullPointerException success) {} |
130 |
> |
} catch (NullPointerException success) {} |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* |
134 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
135 |
|
*/ |
136 |
|
public void testAddAll5() { |
137 |
+ |
Integer[] empty = new Integer[0]; |
138 |
+ |
Integer[] ints = new Integer[SIZE]; |
139 |
+ |
for (int i = 0; i < SIZE; ++i) |
140 |
+ |
ints[i] = i; |
141 |
+ |
LinkedList q = new LinkedList(); |
142 |
+ |
assertFalse(q.addAll(Arrays.asList(empty))); |
143 |
+ |
assertTrue(q.addAll(Arrays.asList(ints))); |
144 |
+ |
for (int i = 0; i < SIZE; ++i) |
145 |
+ |
assertEquals(ints[i], q.poll()); |
146 |
+ |
} |
147 |
+ |
|
148 |
+ |
/** |
149 |
+ |
* addAll with too large an index throws IOOBE |
150 |
+ |
*/ |
151 |
+ |
public void testAddAll2_IndexOutOfBoundsException() { |
152 |
+ |
LinkedList l = new LinkedList(); |
153 |
+ |
l.add(new Object()); |
154 |
+ |
LinkedList m = new LinkedList(); |
155 |
+ |
m.add(new Object()); |
156 |
|
try { |
157 |
< |
Integer[] empty = new Integer[0]; |
158 |
< |
Integer[] ints = new Integer[SIZE]; |
159 |
< |
for (int i = 0; i < SIZE; ++i) |
160 |
< |
ints[i] = new Integer(i); |
161 |
< |
LinkedList q = new LinkedList(); |
162 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
163 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
164 |
< |
for (int i = 0; i < SIZE; ++i) |
165 |
< |
assertEquals(ints[i], q.poll()); |
166 |
< |
} |
167 |
< |
finally {} |
157 |
> |
l.addAll(4,m); |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (IndexOutOfBoundsException success) {} |
160 |
> |
} |
161 |
> |
|
162 |
> |
/** |
163 |
> |
* addAll with negative index throws IOOBE |
164 |
> |
*/ |
165 |
> |
public void testAddAll4_BadIndex() { |
166 |
> |
LinkedList l = new LinkedList(); |
167 |
> |
l.add(new Object()); |
168 |
> |
LinkedList m = new LinkedList(); |
169 |
> |
m.add(new Object()); |
170 |
> |
try { |
171 |
> |
l.addAll(-1,m); |
172 |
> |
shouldThrow(); |
173 |
> |
} catch (IndexOutOfBoundsException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* |
177 |
> |
* poll succeeds unless empty |
178 |
|
*/ |
179 |
|
public void testPoll() { |
180 |
|
LinkedList q = populatedQueue(SIZE); |
181 |
|
for (int i = 0; i < SIZE; ++i) { |
182 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
182 |
> |
assertEquals(i, q.poll()); |
183 |
|
} |
184 |
< |
assertNull(q.poll()); |
184 |
> |
assertNull(q.poll()); |
185 |
|
} |
186 |
|
|
187 |
|
/** |
188 |
< |
* |
188 |
> |
* peek returns next element, or null if empty |
189 |
|
*/ |
190 |
|
public void testPeek() { |
191 |
|
LinkedList q = populatedQueue(SIZE); |
192 |
|
for (int i = 0; i < SIZE; ++i) { |
193 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
194 |
< |
q.poll(); |
193 |
> |
assertEquals(i, q.peek()); |
194 |
> |
assertEquals(i, q.poll()); |
195 |
|
assertTrue(q.peek() == null || |
196 |
< |
i != ((Integer)q.peek()).intValue()); |
196 |
> |
!q.peek().equals(i)); |
197 |
|
} |
198 |
< |
assertNull(q.peek()); |
198 |
> |
assertNull(q.peek()); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
< |
* |
202 |
> |
* element returns next element, or throws NSEE if empty |
203 |
|
*/ |
204 |
|
public void testElement() { |
205 |
|
LinkedList q = populatedQueue(SIZE); |
206 |
|
for (int i = 0; i < SIZE; ++i) { |
207 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
208 |
< |
q.poll(); |
207 |
> |
assertEquals(i, q.element()); |
208 |
> |
assertEquals(i, q.poll()); |
209 |
|
} |
210 |
|
try { |
211 |
|
q.element(); |
212 |
|
shouldThrow(); |
213 |
< |
} |
197 |
< |
catch (NoSuchElementException success) {} |
213 |
> |
} catch (NoSuchElementException success) {} |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
< |
* |
217 |
> |
* remove removes next element, or throws NSEE if empty |
218 |
|
*/ |
219 |
|
public void testRemove() { |
220 |
|
LinkedList q = populatedQueue(SIZE); |
221 |
|
for (int i = 0; i < SIZE; ++i) { |
222 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
222 |
> |
assertEquals(i, q.remove()); |
223 |
|
} |
224 |
|
try { |
225 |
|
q.remove(); |
226 |
|
shouldThrow(); |
227 |
< |
} catch (NoSuchElementException success){ |
212 |
< |
} |
227 |
> |
} catch (NoSuchElementException success) {} |
228 |
|
} |
229 |
|
|
230 |
|
/** |
231 |
< |
* |
231 |
> |
* remove(x) removes x and returns true if present |
232 |
|
*/ |
233 |
|
public void testRemoveElement() { |
234 |
|
LinkedList q = populatedQueue(SIZE); |
241 |
|
} |
242 |
|
assertTrue(q.isEmpty()); |
243 |
|
} |
244 |
< |
|
244 |
> |
|
245 |
|
/** |
246 |
< |
* |
246 |
> |
* contains(x) reports true when elements added but not yet removed |
247 |
|
*/ |
248 |
|
public void testContains() { |
249 |
|
LinkedList q = populatedQueue(SIZE); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
< |
* |
258 |
> |
* clear removes all elements |
259 |
|
*/ |
260 |
|
public void testClear() { |
261 |
|
LinkedList q = populatedQueue(SIZE); |
262 |
|
q.clear(); |
263 |
|
assertTrue(q.isEmpty()); |
264 |
|
assertEquals(0, q.size()); |
265 |
< |
q.add(new Integer(1)); |
265 |
> |
assertTrue(q.add(new Integer(1))); |
266 |
|
assertFalse(q.isEmpty()); |
267 |
|
q.clear(); |
268 |
|
assertTrue(q.isEmpty()); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
272 |
< |
* |
272 |
> |
* containsAll(c) is true when c contains a subset of elements |
273 |
|
*/ |
274 |
|
public void testContainsAll() { |
275 |
|
LinkedList q = populatedQueue(SIZE); |
277 |
|
for (int i = 0; i < SIZE; ++i) { |
278 |
|
assertTrue(q.containsAll(p)); |
279 |
|
assertFalse(p.containsAll(q)); |
280 |
< |
p.add(new Integer(i)); |
280 |
> |
assertTrue(p.add(new Integer(i))); |
281 |
|
} |
282 |
|
assertTrue(p.containsAll(q)); |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
< |
* |
286 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
287 |
|
*/ |
288 |
|
public void testRetainAll() { |
289 |
|
LinkedList q = populatedQueue(SIZE); |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
< |
* |
305 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
306 |
|
*/ |
307 |
|
public void testRemoveAll() { |
308 |
|
for (int i = 1; i < SIZE; ++i) { |
318 |
|
} |
319 |
|
|
320 |
|
/** |
321 |
< |
* |
321 |
> |
* toArray contains all elements |
322 |
|
*/ |
323 |
|
public void testToArray() { |
324 |
|
LinkedList q = populatedQueue(SIZE); |
325 |
< |
Object[] o = q.toArray(); |
325 |
> |
Object[] o = q.toArray(); |
326 |
|
Arrays.sort(o); |
327 |
< |
for(int i = 0; i < o.length; i++) |
328 |
< |
assertEquals(o[i], q.poll()); |
327 |
> |
for (int i = 0; i < o.length; i++) |
328 |
> |
assertEquals(o[i], q.poll()); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* |
332 |
> |
* toArray(a) contains all elements |
333 |
|
*/ |
334 |
|
public void testToArray2() { |
335 |
|
LinkedList q = populatedQueue(SIZE); |
336 |
< |
Integer[] ints = new Integer[SIZE]; |
337 |
< |
ints = (Integer[])q.toArray(ints); |
336 |
> |
Integer[] ints = new Integer[SIZE]; |
337 |
> |
ints = (Integer[])q.toArray(ints); |
338 |
|
Arrays.sort(ints); |
339 |
< |
for(int i = 0; i < ints.length; i++) |
339 |
> |
for (int i = 0; i < ints.length; i++) |
340 |
|
assertEquals(ints[i], q.poll()); |
341 |
|
} |
342 |
< |
|
342 |
> |
|
343 |
> |
/** |
344 |
> |
* toArray(null) throws NPE |
345 |
> |
*/ |
346 |
> |
public void testToArray_BadArg() { |
347 |
> |
LinkedList l = new LinkedList(); |
348 |
> |
l.add(new Object()); |
349 |
> |
try { |
350 |
> |
Object o[] = l.toArray(null); |
351 |
> |
shouldThrow(); |
352 |
> |
} catch (NullPointerException success) {} |
353 |
> |
} |
354 |
> |
|
355 |
> |
/** |
356 |
> |
* toArray with incompatable aray type throws CCE |
357 |
> |
*/ |
358 |
> |
public void testToArray1_BadArg() { |
359 |
> |
LinkedList l = new LinkedList(); |
360 |
> |
l.add(new Integer(5)); |
361 |
> |
try { |
362 |
> |
Object o[] = l.toArray(new String[10]); |
363 |
> |
shouldThrow(); |
364 |
> |
} catch (ArrayStoreException success) {} |
365 |
> |
} |
366 |
> |
|
367 |
|
/** |
368 |
< |
* |
368 |
> |
* iterator iterates through all elements |
369 |
|
*/ |
370 |
|
public void testIterator() { |
371 |
|
LinkedList q = populatedQueue(SIZE); |
372 |
|
int i = 0; |
373 |
< |
Iterator it = q.iterator(); |
374 |
< |
while(it.hasNext()) { |
373 |
> |
Iterator it = q.iterator(); |
374 |
> |
while (it.hasNext()) { |
375 |
|
assertTrue(q.contains(it.next())); |
376 |
|
++i; |
377 |
|
} |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* |
382 |
> |
* iterator ordering is FIFO |
383 |
|
*/ |
384 |
|
public void testIteratorOrdering() { |
346 |
– |
|
385 |
|
final LinkedList q = new LinkedList(); |
348 |
– |
|
386 |
|
q.add(new Integer(1)); |
387 |
|
q.add(new Integer(2)); |
388 |
|
q.add(new Integer(3)); |
352 |
– |
|
389 |
|
int k = 0; |
390 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
391 |
< |
int i = ((Integer)(it.next())).intValue(); |
356 |
< |
assertEquals(++k, i); |
391 |
> |
assertEquals(++k, it.next()); |
392 |
|
} |
393 |
|
|
394 |
|
assertEquals(3, k); |
395 |
|
} |
396 |
|
|
397 |
|
/** |
398 |
< |
* |
398 |
> |
* iterator.remove removes current element |
399 |
|
*/ |
400 |
|
public void testIteratorRemove () { |
401 |
|
final LinkedList q = new LinkedList(); |
367 |
– |
|
402 |
|
q.add(new Integer(1)); |
403 |
|
q.add(new Integer(2)); |
404 |
|
q.add(new Integer(3)); |
371 |
– |
|
405 |
|
Iterator it = q.iterator(); |
406 |
< |
it.next(); |
406 |
> |
assertEquals(it.next(), 1); |
407 |
|
it.remove(); |
375 |
– |
|
408 |
|
it = q.iterator(); |
409 |
< |
assertEquals(it.next(), new Integer(2)); |
410 |
< |
assertEquals(it.next(), new Integer(3)); |
409 |
> |
assertEquals(it.next(), 2); |
410 |
> |
assertEquals(it.next(), 3); |
411 |
|
assertFalse(it.hasNext()); |
412 |
|
} |
413 |
|
|
382 |
– |
|
414 |
|
/** |
415 |
< |
* |
415 |
> |
* Descending iterator iterates through all elements |
416 |
|
*/ |
417 |
< |
public void testToString() { |
417 |
> |
public void testDescendingIterator() { |
418 |
|
LinkedList q = populatedQueue(SIZE); |
419 |
< |
String s = q.toString(); |
420 |
< |
for (int i = 0; i < SIZE; ++i) { |
421 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
419 |
> |
int i = 0; |
420 |
> |
Iterator it = q.descendingIterator(); |
421 |
> |
while (it.hasNext()) { |
422 |
> |
assertTrue(q.contains(it.next())); |
423 |
> |
++i; |
424 |
|
} |
425 |
< |
} |
426 |
< |
|
427 |
< |
/** |
428 |
< |
* |
429 |
< |
*/ |
430 |
< |
public void testAddFirst() { |
398 |
< |
LinkedList q = populatedQueue(3); |
399 |
< |
q.addFirst(new Integer(4)); |
400 |
< |
assertEquals(new Integer(4),q.get(0)); |
401 |
< |
} |
402 |
< |
|
403 |
< |
/** |
404 |
< |
* |
405 |
< |
*/ |
406 |
< |
public void testAddLast() { |
407 |
< |
LinkedList q = populatedQueue(3); |
408 |
< |
q.addLast(new Integer(3)); |
409 |
< |
assertEquals(new Integer(3),q.get(3)); |
410 |
< |
} |
411 |
< |
|
412 |
< |
/** |
413 |
< |
* |
414 |
< |
*/ |
415 |
< |
public void testGetFirst() { |
416 |
< |
LinkedList q = populatedQueue(3); |
417 |
< |
assertEquals(new Integer(0),q.getFirst()); |
418 |
< |
} |
419 |
< |
|
420 |
< |
/** |
421 |
< |
* |
422 |
< |
*/ |
423 |
< |
public void testGetLast() { |
424 |
< |
LinkedList q = populatedQueue(3); |
425 |
< |
assertEquals(new Integer(2),q.getLast()); |
426 |
< |
} |
427 |
< |
|
428 |
< |
/** |
429 |
< |
* |
430 |
< |
*/ |
431 |
< |
public void testIndexOf() { |
432 |
< |
LinkedList q = populatedQueue(3); |
433 |
< |
assertEquals(0,q.indexOf(new Integer(0))); |
434 |
< |
assertEquals(1,q.indexOf(new Integer(1))); |
435 |
< |
assertEquals(2,q.indexOf(new Integer(2))); |
436 |
< |
assertEquals(-1, q.indexOf("not there")); |
425 |
> |
assertEquals(i, SIZE); |
426 |
> |
assertFalse(it.hasNext()); |
427 |
> |
try { |
428 |
> |
it.next(); |
429 |
> |
shouldThrow(); |
430 |
> |
} catch (NoSuchElementException success) {} |
431 |
|
} |
432 |
|
|
433 |
|
/** |
434 |
< |
* |
434 |
> |
* Descending iterator ordering is reverse FIFO |
435 |
|
*/ |
436 |
< |
public void testLastIndexOf() { |
437 |
< |
LinkedList q = populatedQueue(3); |
436 |
> |
public void testDescendingIteratorOrdering() { |
437 |
> |
final LinkedList q = new LinkedList(); |
438 |
> |
q.add(new Integer(3)); |
439 |
|
q.add(new Integer(2)); |
440 |
< |
assertEquals(3,q.lastIndexOf(new Integer(2))); |
441 |
< |
assertEquals(-1, q.lastIndexOf("not there")); |
442 |
< |
} |
443 |
< |
|
444 |
< |
/** |
450 |
< |
* |
451 |
< |
*/ |
452 |
< |
public void testSet() { |
453 |
< |
LinkedList q = populatedQueue(3); |
454 |
< |
q.set(0,(new Integer(1))); |
455 |
< |
assertFalse(q.contains(new Integer(0))); |
456 |
< |
assertEquals(new Integer(1), q.get(0)); |
457 |
< |
} |
458 |
< |
|
440 |
> |
q.add(new Integer(1)); |
441 |
> |
int k = 0; |
442 |
> |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
443 |
> |
assertEquals(++k, it.next()); |
444 |
> |
} |
445 |
|
|
446 |
< |
/** |
461 |
< |
* |
462 |
< |
*/ |
463 |
< |
public void testGetFirst_NoSuchElementException() { |
464 |
< |
try { |
465 |
< |
LinkedList l = new LinkedList(); |
466 |
< |
l.getFirst(); |
467 |
< |
shouldThrow(); |
468 |
< |
} |
469 |
< |
catch(NoSuchElementException success) {} |
470 |
< |
} |
471 |
< |
|
472 |
< |
/** |
473 |
< |
* |
474 |
< |
*/ |
475 |
< |
public void testRemoveFirst() { |
476 |
< |
try { |
477 |
< |
LinkedList l = new LinkedList(); |
478 |
< |
l.removeFirst(); |
479 |
< |
shouldThrow(); |
480 |
< |
} |
481 |
< |
catch(NoSuchElementException success) {} |
482 |
< |
} |
483 |
< |
|
484 |
< |
/** |
485 |
< |
* |
486 |
< |
*/ |
487 |
< |
public void testRemoveLast() { |
488 |
< |
try { |
489 |
< |
LinkedList l = new LinkedList(); |
490 |
< |
l.removeLast(); |
491 |
< |
shouldThrow(); |
492 |
< |
} |
493 |
< |
catch(NoSuchElementException success) {} |
446 |
> |
assertEquals(3, k); |
447 |
|
} |
448 |
< |
|
448 |
> |
|
449 |
|
/** |
450 |
< |
* |
450 |
> |
* descendingIterator.remove removes current element |
451 |
|
*/ |
452 |
< |
public void testGetLast_NoSuchElementException() { |
453 |
< |
try { |
454 |
< |
LinkedList l = new LinkedList(); |
455 |
< |
l.getLast(); |
456 |
< |
shouldThrow(); |
457 |
< |
} |
458 |
< |
catch(NoSuchElementException success) {} |
452 |
> |
public void testDescendingIteratorRemove () { |
453 |
> |
final LinkedList q = new LinkedList(); |
454 |
> |
q.add(new Integer(3)); |
455 |
> |
q.add(new Integer(2)); |
456 |
> |
q.add(new Integer(1)); |
457 |
> |
Iterator it = q.descendingIterator(); |
458 |
> |
it.next(); |
459 |
> |
it.remove(); |
460 |
> |
it = q.descendingIterator(); |
461 |
> |
assertEquals(it.next(), 2); |
462 |
> |
assertEquals(it.next(), 3); |
463 |
> |
assertFalse(it.hasNext()); |
464 |
|
} |
465 |
|
|
466 |
|
|
467 |
|
/** |
468 |
< |
* |
511 |
< |
*/ |
512 |
< |
public void testAddAll_NullPointerException() { |
513 |
< |
try { |
514 |
< |
LinkedList l = new LinkedList(); |
515 |
< |
l.addAll((Collection)null); |
516 |
< |
shouldThrow(); |
517 |
< |
} |
518 |
< |
catch(NullPointerException success){} |
519 |
< |
} |
520 |
< |
|
521 |
< |
|
522 |
< |
/** |
523 |
< |
* |
468 |
> |
* toString contains toStrings of elements |
469 |
|
*/ |
470 |
< |
public void testAddAll1_OutOfBounds() { |
471 |
< |
try { |
472 |
< |
LinkedList l = new LinkedList(); |
473 |
< |
l.addAll(4,new LinkedList()); |
474 |
< |
shouldThrow(); |
475 |
< |
} |
531 |
< |
catch(IndexOutOfBoundsException success) {} |
470 |
> |
public void testToString() { |
471 |
> |
LinkedList q = populatedQueue(SIZE); |
472 |
> |
String s = q.toString(); |
473 |
> |
for (int i = 0; i < SIZE; ++i) { |
474 |
> |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
475 |
> |
} |
476 |
|
} |
477 |
< |
|
534 |
< |
|
477 |
> |
|
478 |
|
/** |
479 |
< |
* |
479 |
> |
* peek returns element inserted with addFirst |
480 |
|
*/ |
481 |
< |
public void testAddAll2_IndexOutOfBoundsException() { |
482 |
< |
try { |
483 |
< |
LinkedList l = new LinkedList(); |
484 |
< |
l.add(new Object()); |
542 |
< |
LinkedList m = new LinkedList(); |
543 |
< |
m.add(new Object()); |
544 |
< |
l.addAll(4,m); |
545 |
< |
shouldThrow(); |
546 |
< |
} catch(IndexOutOfBoundsException success) {} |
481 |
> |
public void testAddFirst() { |
482 |
> |
LinkedList q = populatedQueue(3); |
483 |
> |
q.addFirst(four); |
484 |
> |
assertEquals(four,q.peek()); |
485 |
|
} |
486 |
|
|
487 |
|
/** |
488 |
< |
* |
488 |
> |
* peekFirst returns element inserted with push |
489 |
|
*/ |
490 |
< |
public void testAddAll4_BadIndex() { |
491 |
< |
try { |
492 |
< |
LinkedList l = new LinkedList(); |
493 |
< |
l.add(new Object()); |
494 |
< |
LinkedList m = new LinkedList(); |
557 |
< |
m.add(new Object()); |
558 |
< |
l.addAll(-1,m); |
559 |
< |
shouldThrow(); |
560 |
< |
} catch(IndexOutOfBoundsException success){} |
490 |
> |
public void testPush() { |
491 |
> |
LinkedList q = populatedQueue(3); |
492 |
> |
q.pollLast(); |
493 |
> |
q.push(four); |
494 |
> |
assertEquals(four,q.peekFirst()); |
495 |
|
} |
496 |
|
|
497 |
|
/** |
498 |
< |
* |
498 |
> |
* pop removes next element, or throws NSEE if empty |
499 |
|
*/ |
500 |
< |
public void testget1() { |
501 |
< |
try { |
502 |
< |
LinkedList l = new LinkedList(); |
503 |
< |
l.add(new Object()); |
504 |
< |
l.get(-1); |
505 |
< |
shouldThrow(); |
506 |
< |
} catch(IndexOutOfBoundsException success) {} |
500 |
> |
public void testPop() { |
501 |
> |
LinkedList q = populatedQueue(SIZE); |
502 |
> |
for (int i = 0; i < SIZE; ++i) { |
503 |
> |
assertEquals(i, q.pop()); |
504 |
> |
} |
505 |
> |
try { |
506 |
> |
q.pop(); |
507 |
> |
shouldThrow(); |
508 |
> |
} catch (NoSuchElementException success) {} |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
< |
* |
512 |
> |
* OfferFirst succeeds |
513 |
|
*/ |
514 |
< |
public void testget2() { |
515 |
< |
try { |
516 |
< |
LinkedList l = new LinkedList(); |
517 |
< |
l.add(new Object()); |
582 |
< |
l.get(5); |
583 |
< |
shouldThrow(); |
584 |
< |
} catch(IndexOutOfBoundsException success){} |
514 |
> |
public void testOfferFirst() { |
515 |
> |
LinkedList q = new LinkedList(); |
516 |
> |
assertTrue(q.offerFirst(new Integer(0))); |
517 |
> |
assertTrue(q.offerFirst(new Integer(1))); |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
< |
* |
521 |
> |
* OfferLast succeeds |
522 |
|
*/ |
523 |
< |
public void testset1() { |
524 |
< |
try { |
525 |
< |
LinkedList l = new LinkedList(); |
526 |
< |
l.add(new Object()); |
594 |
< |
l.set(-1,new Object()); |
595 |
< |
shouldThrow(); |
596 |
< |
} catch(IndexOutOfBoundsException success){} |
523 |
> |
public void testOfferLast() { |
524 |
> |
LinkedList q = new LinkedList(); |
525 |
> |
assertTrue(q.offerLast(new Integer(0))); |
526 |
> |
assertTrue(q.offerLast(new Integer(1))); |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* |
530 |
> |
* pollLast succeeds unless empty |
531 |
|
*/ |
532 |
< |
public void testset2() { |
533 |
< |
try { |
534 |
< |
LinkedList l = new LinkedList(); |
535 |
< |
l.add(new Object()); |
536 |
< |
l.set(5,new Object()); |
537 |
< |
shouldThrow(); |
608 |
< |
} catch(IndexOutOfBoundsException success){} |
532 |
> |
public void testPollLast() { |
533 |
> |
LinkedList q = populatedQueue(SIZE); |
534 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
535 |
> |
assertEquals(i, q.pollLast()); |
536 |
> |
} |
537 |
> |
assertNull(q.pollLast()); |
538 |
|
} |
539 |
|
|
540 |
|
/** |
541 |
< |
* |
541 |
> |
* peekFirst returns next element, or null if empty |
542 |
|
*/ |
543 |
< |
public void testadd1() { |
544 |
< |
try { |
545 |
< |
LinkedList l = new LinkedList(); |
546 |
< |
l.add(new Object()); |
547 |
< |
l.add(-1,new Object()); |
548 |
< |
shouldThrow(); |
549 |
< |
} catch(IndexOutOfBoundsException success){} |
543 |
> |
public void testPeekFirst() { |
544 |
> |
LinkedList q = populatedQueue(SIZE); |
545 |
> |
for (int i = 0; i < SIZE; ++i) { |
546 |
> |
assertEquals(i, q.peekFirst()); |
547 |
> |
assertEquals(i, q.pollFirst()); |
548 |
> |
assertTrue(q.peekFirst() == null || |
549 |
> |
!q.peekFirst().equals(i)); |
550 |
> |
} |
551 |
> |
assertNull(q.peekFirst()); |
552 |
|
} |
553 |
|
|
623 |
– |
public void add2() { |
624 |
– |
try { |
625 |
– |
LinkedList l = new LinkedList(); |
626 |
– |
l.add(new Object()); |
627 |
– |
l.add(5,new Object()); |
628 |
– |
shouldThrow(); |
629 |
– |
} catch(IndexOutOfBoundsException success) {} |
630 |
– |
} |
554 |
|
|
555 |
|
/** |
556 |
< |
* |
634 |
< |
*/ |
635 |
< |
public void testremove() { |
636 |
< |
try { |
637 |
< |
LinkedList l = new LinkedList(); |
638 |
< |
l.add(new Object()); |
639 |
< |
l.remove(-1); |
640 |
< |
shouldThrow(); |
641 |
< |
} catch(IndexOutOfBoundsException success){} |
642 |
< |
} |
643 |
< |
|
644 |
< |
/** |
645 |
< |
* |
556 |
> |
* peekLast returns next element, or null if empty |
557 |
|
*/ |
558 |
< |
public void testremove1() { |
559 |
< |
try { |
560 |
< |
LinkedList l = new LinkedList(); |
561 |
< |
l.add(new Object()); |
562 |
< |
l.remove(5); |
563 |
< |
shouldThrow(); |
564 |
< |
} catch(IndexOutOfBoundsException success){} |
558 |
> |
public void testPeekLast() { |
559 |
> |
LinkedList q = populatedQueue(SIZE); |
560 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
561 |
> |
assertEquals(i, q.peekLast()); |
562 |
> |
assertEquals(i, q.pollLast()); |
563 |
> |
assertTrue(q.peekLast() == null || |
564 |
> |
!q.peekLast().equals(i)); |
565 |
> |
} |
566 |
> |
assertNull(q.peekLast()); |
567 |
|
} |
568 |
|
|
569 |
< |
|
570 |
< |
/** |
571 |
< |
* |
572 |
< |
*/ |
573 |
< |
public void testremove2() { |
569 |
> |
public void testFirstElement() { |
570 |
> |
LinkedList q = populatedQueue(SIZE); |
571 |
> |
for (int i = 0; i < SIZE; ++i) { |
572 |
> |
assertEquals(i, q.getFirst()); |
573 |
> |
assertEquals(i, q.pollFirst()); |
574 |
> |
} |
575 |
|
try { |
576 |
< |
LinkedList l = new LinkedList(); |
663 |
< |
l.remove(); |
576 |
> |
q.getFirst(); |
577 |
|
shouldThrow(); |
578 |
< |
} catch(NoSuchElementException e){} |
578 |
> |
} catch (NoSuchElementException success) {} |
579 |
|
} |
580 |
|
|
581 |
|
/** |
582 |
< |
* |
670 |
< |
*/ |
671 |
< |
public void testlistIt1() { |
672 |
< |
try { |
673 |
< |
LinkedList l = new LinkedList(); |
674 |
< |
l.add(new Object()); |
675 |
< |
l.listIterator(5); |
676 |
< |
shouldThrow(); |
677 |
< |
} catch(IndexOutOfBoundsException success){} |
678 |
< |
} |
679 |
< |
|
680 |
< |
/** |
681 |
< |
* |
582 |
> |
* getLast returns next element, or throws NSEE if empty |
583 |
|
*/ |
584 |
< |
public void testlistIt2() { |
585 |
< |
try { |
586 |
< |
LinkedList l = new LinkedList(); |
587 |
< |
l.add(new Object()); |
588 |
< |
l.listIterator(-1); |
589 |
< |
shouldThrow(); |
590 |
< |
} catch(IndexOutOfBoundsException success){} |
591 |
< |
} |
592 |
< |
|
593 |
< |
/** |
594 |
< |
* |
694 |
< |
*/ |
695 |
< |
public void testlistIt3() { |
696 |
< |
try { |
697 |
< |
LinkedList l = new LinkedList(); |
698 |
< |
l.add(new Object()); |
699 |
< |
ListIterator a = l.listIterator(0); |
700 |
< |
l.removeFirst(); |
701 |
< |
a.next(); |
702 |
< |
shouldThrow(); |
703 |
< |
} catch(ConcurrentModificationException success){} |
584 |
> |
public void testLastElement() { |
585 |
> |
LinkedList q = populatedQueue(SIZE); |
586 |
> |
for (int i = SIZE-1; i >= 0; --i) { |
587 |
> |
assertEquals(i, q.getLast()); |
588 |
> |
assertEquals(i, q.pollLast()); |
589 |
> |
} |
590 |
> |
try { |
591 |
> |
q.getLast(); |
592 |
> |
shouldThrow(); |
593 |
> |
} catch (NoSuchElementException success) {} |
594 |
> |
assertNull(q.peekLast()); |
595 |
|
} |
596 |
|
|
597 |
|
/** |
598 |
< |
* |
598 |
> |
* removeFirstOccurrence(x) removes x and returns true if present |
599 |
|
*/ |
600 |
< |
public void testToArray_BadArg() { |
601 |
< |
try { |
602 |
< |
LinkedList l = new LinkedList(); |
603 |
< |
l.add(new Object()); |
604 |
< |
Object o[] = l.toArray(null); |
605 |
< |
shouldThrow(); |
606 |
< |
} catch(NullPointerException success){} |
600 |
> |
public void testRemoveFirstOccurrence() { |
601 |
> |
LinkedList q = populatedQueue(SIZE); |
602 |
> |
for (int i = 1; i < SIZE; i+=2) { |
603 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
604 |
> |
} |
605 |
> |
for (int i = 0; i < SIZE; i+=2) { |
606 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
607 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
608 |
> |
} |
609 |
> |
assertTrue(q.isEmpty()); |
610 |
|
} |
611 |
|
|
612 |
|
/** |
613 |
< |
* |
613 |
> |
* removeLastOccurrence(x) removes x and returns true if present |
614 |
|
*/ |
615 |
< |
public void testToArray1_BadArg() { |
616 |
< |
try { |
617 |
< |
LinkedList l = new LinkedList(); |
618 |
< |
l.add(new Integer(5)); |
619 |
< |
Object o[] = l.toArray(new String[10] ); |
620 |
< |
shouldThrow(); |
621 |
< |
} catch(ArrayStoreException success){} |
615 |
> |
public void testRemoveLastOccurrence() { |
616 |
> |
LinkedList q = populatedQueue(SIZE); |
617 |
> |
for (int i = 1; i < SIZE; i+=2) { |
618 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
619 |
> |
} |
620 |
> |
for (int i = 0; i < SIZE; i+=2) { |
621 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
622 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
623 |
> |
} |
624 |
> |
assertTrue(q.isEmpty()); |
625 |
|
} |
626 |
|
|
627 |
|
} |