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/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.LinkedList; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
|
15 |
> |
import junit.framework.Test; |
16 |
> |
import junit.framework.TestSuite; |
17 |
|
|
18 |
|
public class LinkedListTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run (suite()); |
20 |
> |
main(suite(), args); |
21 |
|
} |
22 |
|
|
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(LinkedListTest.class); |
24 |
> |
return new TestSuite(LinkedListTest.class); |
25 |
|
} |
26 |
|
|
27 |
|
/** |
28 |
< |
* Create a queue of given size containing consecutive |
29 |
< |
* Integers 0 ... n. |
28 |
> |
* Returns a new queue of given size containing consecutive |
29 |
> |
* Integers 0 ... n - 1. |
30 |
|
*/ |
31 |
< |
private LinkedList populatedQueue(int n) { |
32 |
< |
LinkedList q = new LinkedList(); |
31 |
> |
private LinkedList<Integer> populatedQueue(int n) { |
32 |
> |
LinkedList<Integer> q = new LinkedList<Integer>(); |
33 |
|
assertTrue(q.isEmpty()); |
34 |
< |
for(int i = 0; i < n; ++i) |
35 |
< |
assertTrue(q.offer(new Integer(i))); |
34 |
> |
for (int i = 0; i < n; ++i) |
35 |
> |
assertTrue(q.offer(new Integer(i))); |
36 |
|
assertFalse(q.isEmpty()); |
37 |
< |
assertEquals(n, q.size()); |
37 |
> |
assertEquals(n, q.size()); |
38 |
> |
assertEquals((Integer) 0, q.peekFirst()); |
39 |
> |
assertEquals((Integer) (n - 1), q.peekLast()); |
40 |
|
return q; |
41 |
|
} |
42 |
< |
|
42 |
> |
|
43 |
|
/** |
44 |
< |
* |
44 |
> |
* new queue is empty |
45 |
|
*/ |
46 |
|
public void testConstructor1() { |
47 |
|
assertEquals(0, new LinkedList().size()); |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* |
51 |
> |
* Initializing from null Collection throws NPE |
52 |
|
*/ |
53 |
|
public void testConstructor3() { |
54 |
|
try { |
55 |
< |
LinkedList q = new LinkedList((Collection)null); |
55 |
> |
new LinkedList((Collection)null); |
56 |
|
shouldThrow(); |
57 |
< |
} |
50 |
< |
catch (NullPointerException success) {} |
57 |
> |
} catch (NullPointerException success) {} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
< |
* |
61 |
> |
* Queue contains all elements of collection used to initialize |
62 |
|
*/ |
63 |
|
public void testConstructor6() { |
64 |
< |
try { |
65 |
< |
Integer[] ints = new Integer[SIZE]; |
66 |
< |
for (int i = 0; i < SIZE; ++i) |
67 |
< |
ints[i] = new Integer(i); |
68 |
< |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
69 |
< |
for (int i = 0; i < SIZE; ++i) |
63 |
< |
assertEquals(ints[i], q.poll()); |
64 |
< |
} |
65 |
< |
finally {} |
64 |
> |
Integer[] ints = new Integer[SIZE]; |
65 |
> |
for (int i = 0; i < SIZE; ++i) |
66 |
> |
ints[i] = i; |
67 |
> |
LinkedList q = new LinkedList(Arrays.asList(ints)); |
68 |
> |
for (int i = 0; i < SIZE; ++i) |
69 |
> |
assertEquals(ints[i], q.poll()); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
< |
* |
73 |
> |
* isEmpty is true before add, false after |
74 |
|
*/ |
75 |
|
public void testEmpty() { |
76 |
|
LinkedList q = new LinkedList(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* |
87 |
> |
* size changes when elements added and removed |
88 |
|
*/ |
89 |
|
public void testSize() { |
90 |
|
LinkedList q = populatedQueue(SIZE); |
91 |
|
for (int i = 0; i < SIZE; ++i) { |
92 |
< |
assertEquals(SIZE-i, q.size()); |
92 |
> |
assertEquals(SIZE - i, q.size()); |
93 |
|
q.remove(); |
94 |
|
} |
95 |
|
for (int i = 0; i < SIZE; ++i) { |
99 |
|
} |
100 |
|
|
101 |
|
/** |
102 |
< |
* |
102 |
> |
* offer(null) succeeds |
103 |
|
*/ |
104 |
|
public void testOfferNull() { |
105 |
< |
try { |
106 |
< |
LinkedList q = new LinkedList(); |
107 |
< |
q.offer(null); |
108 |
< |
} catch (NullPointerException ie) { |
105 |
< |
unexpectedException(); |
106 |
< |
} |
105 |
> |
LinkedList q = new LinkedList(); |
106 |
> |
q.offer(null); |
107 |
> |
assertNull(q.get(0)); |
108 |
> |
assertTrue(q.contains(null)); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* |
112 |
> |
* Offer succeeds |
113 |
|
*/ |
114 |
|
public void testOffer() { |
115 |
|
LinkedList q = new LinkedList(); |
118 |
|
} |
119 |
|
|
120 |
|
/** |
121 |
< |
* |
121 |
> |
* add succeeds |
122 |
|
*/ |
123 |
|
public void testAdd() { |
124 |
|
LinkedList q = new LinkedList(); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
< |
* |
132 |
> |
* addAll(null) throws NPE |
133 |
|
*/ |
134 |
|
public void testAddAll1() { |
135 |
+ |
LinkedList q = new LinkedList(); |
136 |
|
try { |
134 |
– |
LinkedList q = new LinkedList(); |
137 |
|
q.addAll(null); |
138 |
|
shouldThrow(); |
139 |
< |
} |
138 |
< |
catch (NullPointerException success) {} |
139 |
> |
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
< |
* |
143 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
144 |
|
*/ |
145 |
|
public void testAddAll5() { |
146 |
+ |
Integer[] empty = new Integer[0]; |
147 |
+ |
Integer[] ints = new Integer[SIZE]; |
148 |
+ |
for (int i = 0; i < SIZE; ++i) |
149 |
+ |
ints[i] = i; |
150 |
+ |
LinkedList q = new LinkedList(); |
151 |
+ |
assertFalse(q.addAll(Arrays.asList(empty))); |
152 |
+ |
assertTrue(q.addAll(Arrays.asList(ints))); |
153 |
+ |
for (int i = 0; i < SIZE; ++i) |
154 |
+ |
assertEquals(ints[i], q.poll()); |
155 |
+ |
} |
156 |
+ |
|
157 |
+ |
/** |
158 |
+ |
* addAll with too large an index throws IOOBE |
159 |
+ |
*/ |
160 |
+ |
public void testAddAll2_IndexOutOfBoundsException() { |
161 |
+ |
LinkedList l = new LinkedList(); |
162 |
+ |
l.add(new Object()); |
163 |
+ |
LinkedList m = new LinkedList(); |
164 |
+ |
m.add(new Object()); |
165 |
|
try { |
166 |
< |
Integer[] empty = new Integer[0]; |
167 |
< |
Integer[] ints = new Integer[SIZE]; |
168 |
< |
for (int i = 0; i < SIZE; ++i) |
149 |
< |
ints[i] = new Integer(i); |
150 |
< |
LinkedList q = new LinkedList(); |
151 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
152 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
153 |
< |
for (int i = 0; i < SIZE; ++i) |
154 |
< |
assertEquals(ints[i], q.poll()); |
155 |
< |
} |
156 |
< |
finally {} |
166 |
> |
l.addAll(4,m); |
167 |
> |
shouldThrow(); |
168 |
> |
} catch (IndexOutOfBoundsException success) {} |
169 |
|
} |
170 |
|
|
171 |
|
/** |
172 |
< |
* |
172 |
> |
* addAll with negative index throws IOOBE |
173 |
> |
*/ |
174 |
> |
public void testAddAll4_BadIndex() { |
175 |
> |
LinkedList l = new LinkedList(); |
176 |
> |
l.add(new Object()); |
177 |
> |
LinkedList m = new LinkedList(); |
178 |
> |
m.add(new Object()); |
179 |
> |
try { |
180 |
> |
l.addAll(-1,m); |
181 |
> |
shouldThrow(); |
182 |
> |
} catch (IndexOutOfBoundsException success) {} |
183 |
> |
} |
184 |
> |
|
185 |
> |
/** |
186 |
> |
* poll succeeds unless empty |
187 |
|
*/ |
188 |
|
public void testPoll() { |
189 |
|
LinkedList q = populatedQueue(SIZE); |
190 |
|
for (int i = 0; i < SIZE; ++i) { |
191 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
191 |
> |
assertEquals(i, q.poll()); |
192 |
|
} |
193 |
< |
assertNull(q.poll()); |
193 |
> |
assertNull(q.poll()); |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
< |
* |
197 |
> |
* peek returns next element, or null if empty |
198 |
|
*/ |
199 |
|
public void testPeek() { |
200 |
|
LinkedList q = populatedQueue(SIZE); |
201 |
|
for (int i = 0; i < SIZE; ++i) { |
202 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
203 |
< |
q.poll(); |
202 |
> |
assertEquals(i, q.peek()); |
203 |
> |
assertEquals(i, q.poll()); |
204 |
|
assertTrue(q.peek() == null || |
205 |
< |
i != ((Integer)q.peek()).intValue()); |
205 |
> |
!q.peek().equals(i)); |
206 |
|
} |
207 |
< |
assertNull(q.peek()); |
207 |
> |
assertNull(q.peek()); |
208 |
|
} |
209 |
|
|
210 |
|
/** |
211 |
< |
* |
211 |
> |
* element returns next element, or throws NSEE if empty |
212 |
|
*/ |
213 |
|
public void testElement() { |
214 |
|
LinkedList q = populatedQueue(SIZE); |
215 |
|
for (int i = 0; i < SIZE; ++i) { |
216 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
217 |
< |
q.poll(); |
216 |
> |
assertEquals(i, q.element()); |
217 |
> |
assertEquals(i, q.poll()); |
218 |
|
} |
219 |
|
try { |
220 |
|
q.element(); |
221 |
|
shouldThrow(); |
222 |
< |
} |
197 |
< |
catch (NoSuchElementException success) {} |
222 |
> |
} catch (NoSuchElementException success) {} |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
< |
* |
226 |
> |
* remove removes next element, or throws NSEE if empty |
227 |
|
*/ |
228 |
|
public void testRemove() { |
229 |
|
LinkedList q = populatedQueue(SIZE); |
230 |
|
for (int i = 0; i < SIZE; ++i) { |
231 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
231 |
> |
assertEquals(i, q.remove()); |
232 |
|
} |
233 |
|
try { |
234 |
|
q.remove(); |
235 |
|
shouldThrow(); |
236 |
< |
} catch (NoSuchElementException success){ |
212 |
< |
} |
236 |
> |
} catch (NoSuchElementException success) {} |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* |
240 |
> |
* remove(x) removes x and returns true if present |
241 |
|
*/ |
242 |
|
public void testRemoveElement() { |
243 |
|
LinkedList q = populatedQueue(SIZE); |
244 |
< |
for (int i = 1; i < SIZE; i+=2) { |
245 |
< |
assertTrue(q.remove(new Integer(i))); |
246 |
< |
} |
247 |
< |
for (int i = 0; i < SIZE; i+=2) { |
248 |
< |
assertTrue(q.remove(new Integer(i))); |
249 |
< |
assertFalse(q.remove(new Integer(i+1))); |
244 |
> |
for (int i = 1; i < SIZE; i += 2) { |
245 |
> |
assertTrue(q.contains(i)); |
246 |
> |
assertTrue(q.remove((Integer)i)); |
247 |
> |
assertFalse(q.contains(i)); |
248 |
> |
assertTrue(q.contains(i - 1)); |
249 |
> |
} |
250 |
> |
for (int i = 0; i < SIZE; i += 2) { |
251 |
> |
assertTrue(q.contains(i)); |
252 |
> |
assertTrue(q.remove((Integer)i)); |
253 |
> |
assertFalse(q.contains(i)); |
254 |
> |
assertFalse(q.remove((Integer)(i + 1))); |
255 |
> |
assertFalse(q.contains(i + 1)); |
256 |
|
} |
257 |
|
assertTrue(q.isEmpty()); |
258 |
|
} |
259 |
< |
|
259 |
> |
|
260 |
|
/** |
261 |
< |
* |
261 |
> |
* contains(x) reports true when elements added but not yet removed |
262 |
|
*/ |
263 |
|
public void testContains() { |
264 |
|
LinkedList q = populatedQueue(SIZE); |
270 |
|
} |
271 |
|
|
272 |
|
/** |
273 |
< |
* |
273 |
> |
* clear removes all elements |
274 |
|
*/ |
275 |
|
public void testClear() { |
276 |
|
LinkedList q = populatedQueue(SIZE); |
277 |
|
q.clear(); |
278 |
|
assertTrue(q.isEmpty()); |
279 |
|
assertEquals(0, q.size()); |
280 |
< |
q.add(new Integer(1)); |
280 |
> |
assertTrue(q.add(new Integer(1))); |
281 |
|
assertFalse(q.isEmpty()); |
282 |
|
q.clear(); |
283 |
|
assertTrue(q.isEmpty()); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
< |
* |
287 |
> |
* containsAll(c) is true when c contains a subset of elements |
288 |
|
*/ |
289 |
|
public void testContainsAll() { |
290 |
|
LinkedList q = populatedQueue(SIZE); |
292 |
|
for (int i = 0; i < SIZE; ++i) { |
293 |
|
assertTrue(q.containsAll(p)); |
294 |
|
assertFalse(p.containsAll(q)); |
295 |
< |
p.add(new Integer(i)); |
295 |
> |
assertTrue(p.add(new Integer(i))); |
296 |
|
} |
297 |
|
assertTrue(p.containsAll(q)); |
298 |
|
} |
299 |
|
|
300 |
|
/** |
301 |
< |
* |
301 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
302 |
|
*/ |
303 |
|
public void testRetainAll() { |
304 |
|
LinkedList q = populatedQueue(SIZE); |
311 |
|
assertTrue(changed); |
312 |
|
|
313 |
|
assertTrue(q.containsAll(p)); |
314 |
< |
assertEquals(SIZE-i, q.size()); |
314 |
> |
assertEquals(SIZE - i, q.size()); |
315 |
|
p.remove(); |
316 |
|
} |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
< |
* |
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); |
326 |
|
assertTrue(q.removeAll(p)); |
327 |
< |
assertEquals(SIZE-i, q.size()); |
327 |
> |
assertEquals(SIZE - i, q.size()); |
328 |
|
for (int j = 0; j < i; ++j) { |
329 |
< |
Integer I = (Integer)(p.remove()); |
330 |
< |
assertFalse(q.contains(I)); |
329 |
> |
Integer x = (Integer)(p.remove()); |
330 |
> |
assertFalse(q.contains(x)); |
331 |
|
} |
332 |
|
} |
333 |
|
} |
334 |
|
|
335 |
|
/** |
336 |
< |
* |
336 |
> |
* toArray contains all elements in FIFO order |
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++) |
313 |
< |
assertEquals(o[i], q.poll()); |
340 |
> |
Object[] o = q.toArray(); |
341 |
> |
for (int i = 0; i < o.length; i++) |
342 |
> |
assertSame(o[i], q.poll()); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
< |
* |
346 |
> |
* toArray(a) contains all elements in FIFO order |
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()); |
349 |
> |
LinkedList<Integer> q = populatedQueue(SIZE); |
350 |
> |
Integer[] ints = new Integer[SIZE]; |
351 |
> |
Integer[] array = q.toArray(ints); |
352 |
> |
assertSame(ints, array); |
353 |
> |
for (int i = 0; i < ints.length; i++) |
354 |
> |
assertSame(ints[i], q.poll()); |
355 |
|
} |
356 |
< |
|
356 |
> |
|
357 |
|
/** |
358 |
< |
* |
358 |
> |
* toArray(null) throws NullPointerException |
359 |
> |
*/ |
360 |
> |
public void testToArray_NullArg() { |
361 |
> |
LinkedList l = new LinkedList(); |
362 |
> |
l.add(new Object()); |
363 |
> |
try { |
364 |
> |
l.toArray(null); |
365 |
> |
shouldThrow(); |
366 |
> |
} catch (NullPointerException success) {} |
367 |
> |
} |
368 |
> |
|
369 |
> |
/** |
370 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
371 |
> |
*/ |
372 |
> |
public void testToArray1_BadArg() { |
373 |
> |
LinkedList l = new LinkedList(); |
374 |
> |
l.add(new Integer(5)); |
375 |
> |
try { |
376 |
> |
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()) { |
386 |
> |
Iterator it = q.iterator(); |
387 |
> |
int i; |
388 |
> |
for (i = 0; it.hasNext(); i++) |
389 |
|
assertTrue(q.contains(it.next())); |
337 |
– |
++i; |
338 |
– |
} |
390 |
|
assertEquals(i, SIZE); |
391 |
+ |
assertIteratorExhausted(it); |
392 |
|
} |
393 |
|
|
394 |
|
/** |
395 |
< |
* |
395 |
> |
* iterator of empty collection has no elements |
396 |
|
*/ |
397 |
< |
public void testIteratorOrdering() { |
397 |
> |
public void testEmptyIterator() { |
398 |
> |
assertIteratorExhausted(new LinkedList().iterator()); |
399 |
> |
} |
400 |
|
|
401 |
+ |
/** |
402 |
+ |
* iterator ordering is FIFO |
403 |
+ |
*/ |
404 |
+ |
public void testIteratorOrdering() { |
405 |
|
final LinkedList q = new LinkedList(); |
348 |
– |
|
406 |
|
q.add(new Integer(1)); |
407 |
|
q.add(new Integer(2)); |
408 |
|
q.add(new Integer(3)); |
352 |
– |
|
409 |
|
int k = 0; |
410 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
411 |
< |
int i = ((Integer)(it.next())).intValue(); |
356 |
< |
assertEquals(++k, i); |
411 |
> |
assertEquals(++k, it.next()); |
412 |
|
} |
413 |
|
|
414 |
|
assertEquals(3, k); |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* |
418 |
> |
* iterator.remove removes current element |
419 |
|
*/ |
420 |
< |
public void testIteratorRemove () { |
420 |
> |
public void testIteratorRemove() { |
421 |
|
final LinkedList q = new LinkedList(); |
367 |
– |
|
422 |
|
q.add(new Integer(1)); |
423 |
|
q.add(new Integer(2)); |
424 |
|
q.add(new Integer(3)); |
371 |
– |
|
425 |
|
Iterator it = q.iterator(); |
426 |
< |
it.next(); |
426 |
> |
assertEquals(1, it.next()); |
427 |
|
it.remove(); |
375 |
– |
|
428 |
|
it = q.iterator(); |
429 |
< |
assertEquals(it.next(), new Integer(2)); |
430 |
< |
assertEquals(it.next(), new Integer(3)); |
429 |
> |
assertEquals(2, it.next()); |
430 |
> |
assertEquals(3, it.next()); |
431 |
|
assertFalse(it.hasNext()); |
432 |
|
} |
433 |
|
|
382 |
– |
|
434 |
|
/** |
435 |
< |
* |
435 |
> |
* Descending iterator iterates through all elements |
436 |
|
*/ |
437 |
< |
public void testToString() { |
437 |
> |
public void testDescendingIterator() { |
438 |
|
LinkedList q = populatedQueue(SIZE); |
439 |
< |
String s = q.toString(); |
440 |
< |
for (int i = 0; i < SIZE; ++i) { |
441 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
439 |
> |
int i = 0; |
440 |
> |
Iterator it = q.descendingIterator(); |
441 |
> |
while (it.hasNext()) { |
442 |
> |
assertTrue(q.contains(it.next())); |
443 |
> |
++i; |
444 |
|
} |
445 |
< |
} |
446 |
< |
|
447 |
< |
/** |
448 |
< |
* |
449 |
< |
*/ |
450 |
< |
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")); |
445 |
> |
assertEquals(i, SIZE); |
446 |
> |
assertFalse(it.hasNext()); |
447 |
> |
try { |
448 |
> |
it.next(); |
449 |
> |
shouldThrow(); |
450 |
> |
} catch (NoSuchElementException success) {} |
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
< |
* |
454 |
> |
* Descending iterator ordering is reverse FIFO |
455 |
|
*/ |
456 |
< |
public void testLastIndexOf() { |
457 |
< |
LinkedList q = populatedQueue(3); |
456 |
> |
public void testDescendingIteratorOrdering() { |
457 |
> |
final LinkedList q = new LinkedList(); |
458 |
> |
q.add(new Integer(3)); |
459 |
|
q.add(new Integer(2)); |
460 |
< |
assertEquals(3,q.lastIndexOf(new Integer(2))); |
461 |
< |
assertEquals(-1, q.lastIndexOf("not there")); |
462 |
< |
} |
463 |
< |
|
464 |
< |
/** |
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 |
< |
|
460 |
> |
q.add(new Integer(1)); |
461 |
> |
int k = 0; |
462 |
> |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
463 |
> |
assertEquals(++k, it.next()); |
464 |
> |
} |
465 |
|
|
466 |
< |
/** |
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) {} |
494 |
< |
} |
495 |
< |
|
496 |
< |
/** |
497 |
< |
* |
498 |
< |
*/ |
499 |
< |
public void testGetLast_NoSuchElementException() { |
500 |
< |
try { |
501 |
< |
LinkedList l = new LinkedList(); |
502 |
< |
l.getLast(); |
503 |
< |
shouldThrow(); |
504 |
< |
} |
505 |
< |
catch(NoSuchElementException success) {} |
466 |
> |
assertEquals(3, k); |
467 |
|
} |
468 |
|
|
508 |
– |
|
469 |
|
/** |
470 |
< |
* |
470 |
> |
* descendingIterator.remove removes current element |
471 |
|
*/ |
472 |
< |
public void testAddAll_NullPointerException() { |
473 |
< |
try { |
474 |
< |
LinkedList l = new LinkedList(); |
475 |
< |
l.addAll((Collection)null); |
476 |
< |
shouldThrow(); |
477 |
< |
} |
478 |
< |
catch(NullPointerException success){} |
479 |
< |
} |
480 |
< |
|
481 |
< |
|
482 |
< |
/** |
483 |
< |
* |
524 |
< |
*/ |
525 |
< |
public void testAddAll1_OutOfBounds() { |
526 |
< |
try { |
527 |
< |
LinkedList l = new LinkedList(); |
528 |
< |
l.addAll(4,new LinkedList()); |
529 |
< |
shouldThrow(); |
530 |
< |
} |
531 |
< |
catch(IndexOutOfBoundsException success) {} |
472 |
> |
public void testDescendingIteratorRemove() { |
473 |
> |
final LinkedList q = new LinkedList(); |
474 |
> |
q.add(three); |
475 |
> |
q.add(two); |
476 |
> |
q.add(one); |
477 |
> |
Iterator it = q.descendingIterator(); |
478 |
> |
it.next(); |
479 |
> |
it.remove(); |
480 |
> |
it = q.descendingIterator(); |
481 |
> |
assertSame(it.next(), two); |
482 |
> |
assertSame(it.next(), three); |
483 |
> |
assertFalse(it.hasNext()); |
484 |
|
} |
485 |
< |
|
534 |
< |
|
485 |
> |
|
486 |
|
/** |
487 |
< |
* |
487 |
> |
* toString contains toStrings of elements |
488 |
|
*/ |
489 |
< |
public void testAddAll2_IndexOutOfBoundsException() { |
490 |
< |
try { |
491 |
< |
LinkedList l = new LinkedList(); |
492 |
< |
l.add(new Object()); |
493 |
< |
LinkedList m = new LinkedList(); |
494 |
< |
m.add(new Object()); |
544 |
< |
l.addAll(4,m); |
545 |
< |
shouldThrow(); |
546 |
< |
} catch(IndexOutOfBoundsException success) {} |
489 |
> |
public void testToString() { |
490 |
> |
LinkedList q = populatedQueue(SIZE); |
491 |
> |
String s = q.toString(); |
492 |
> |
for (int i = 0; i < SIZE; ++i) { |
493 |
> |
assertTrue(s.contains(String.valueOf(i))); |
494 |
> |
} |
495 |
|
} |
496 |
|
|
497 |
|
/** |
498 |
< |
* |
498 |
> |
* peek returns element inserted with addFirst |
499 |
|
*/ |
500 |
< |
public void testAddAll4_BadIndex() { |
501 |
< |
try { |
502 |
< |
LinkedList l = new LinkedList(); |
503 |
< |
l.add(new Object()); |
556 |
< |
LinkedList m = new LinkedList(); |
557 |
< |
m.add(new Object()); |
558 |
< |
l.addAll(-1,m); |
559 |
< |
shouldThrow(); |
560 |
< |
} catch(IndexOutOfBoundsException success){} |
500 |
> |
public void testAddFirst() { |
501 |
> |
LinkedList q = populatedQueue(3); |
502 |
> |
q.addFirst(four); |
503 |
> |
assertSame(four, q.peek()); |
504 |
|
} |
505 |
|
|
506 |
|
/** |
507 |
< |
* |
507 |
> |
* peekFirst returns element inserted with push |
508 |
|
*/ |
509 |
< |
public void testget1() { |
510 |
< |
try { |
511 |
< |
LinkedList l = new LinkedList(); |
512 |
< |
l.add(new Object()); |
570 |
< |
l.get(-1); |
571 |
< |
shouldThrow(); |
572 |
< |
} catch(IndexOutOfBoundsException success) {} |
509 |
> |
public void testPush() { |
510 |
> |
LinkedList q = populatedQueue(3); |
511 |
> |
q.push(four); |
512 |
> |
assertSame(four, q.peekFirst()); |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
< |
* |
516 |
> |
* pop removes next element, or throws NSEE if empty |
517 |
|
*/ |
518 |
< |
public void testget2() { |
519 |
< |
try { |
520 |
< |
LinkedList l = new LinkedList(); |
521 |
< |
l.add(new Object()); |
522 |
< |
l.get(5); |
523 |
< |
shouldThrow(); |
524 |
< |
} catch(IndexOutOfBoundsException success){} |
518 |
> |
public void testPop() { |
519 |
> |
LinkedList q = populatedQueue(SIZE); |
520 |
> |
for (int i = 0; i < SIZE; ++i) { |
521 |
> |
assertEquals(i, q.pop()); |
522 |
> |
} |
523 |
> |
try { |
524 |
> |
q.pop(); |
525 |
> |
shouldThrow(); |
526 |
> |
} catch (NoSuchElementException success) {} |
527 |
|
} |
528 |
|
|
529 |
|
/** |
530 |
< |
* |
530 |
> |
* OfferFirst succeeds |
531 |
|
*/ |
532 |
< |
public void testset1() { |
533 |
< |
try { |
534 |
< |
LinkedList l = new LinkedList(); |
535 |
< |
l.add(new Object()); |
594 |
< |
l.set(-1,new Object()); |
595 |
< |
shouldThrow(); |
596 |
< |
} catch(IndexOutOfBoundsException success){} |
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 |
|
/** |
539 |
< |
* |
539 |
> |
* OfferLast succeeds |
540 |
|
*/ |
541 |
< |
public void testset2() { |
542 |
< |
try { |
543 |
< |
LinkedList l = new LinkedList(); |
544 |
< |
l.add(new Object()); |
606 |
< |
l.set(5,new Object()); |
607 |
< |
shouldThrow(); |
608 |
< |
} catch(IndexOutOfBoundsException success){} |
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 |
|
/** |
548 |
< |
* |
548 |
> |
* pollLast succeeds unless empty |
549 |
|
*/ |
550 |
< |
public void testadd1() { |
551 |
< |
try { |
552 |
< |
LinkedList l = new LinkedList(); |
553 |
< |
l.add(new Object()); |
554 |
< |
l.add(-1,new Object()); |
555 |
< |
shouldThrow(); |
620 |
< |
} catch(IndexOutOfBoundsException success){} |
621 |
< |
} |
622 |
< |
|
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) {} |
550 |
> |
public void testPollLast() { |
551 |
> |
LinkedList q = populatedQueue(SIZE); |
552 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
553 |
> |
assertEquals(i, q.pollLast()); |
554 |
> |
} |
555 |
> |
assertNull(q.pollLast()); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* |
559 |
> |
* peekFirst returns next element, or null if empty |
560 |
|
*/ |
561 |
< |
public void testremove() { |
562 |
< |
try { |
563 |
< |
LinkedList l = new LinkedList(); |
564 |
< |
l.add(new Object()); |
565 |
< |
l.remove(-1); |
566 |
< |
shouldThrow(); |
567 |
< |
} catch(IndexOutOfBoundsException success){} |
561 |
> |
public void testPeekFirst() { |
562 |
> |
LinkedList q = populatedQueue(SIZE); |
563 |
> |
for (int i = 0; i < SIZE; ++i) { |
564 |
> |
assertEquals(i, q.peekFirst()); |
565 |
> |
assertEquals(i, q.pollFirst()); |
566 |
> |
assertTrue(q.peekFirst() == null || |
567 |
> |
!q.peekFirst().equals(i)); |
568 |
> |
} |
569 |
> |
assertNull(q.peekFirst()); |
570 |
|
} |
571 |
< |
|
571 |
> |
|
572 |
|
/** |
573 |
< |
* |
573 |
> |
* peekLast returns next element, or null if empty |
574 |
|
*/ |
575 |
< |
public void testremove1() { |
576 |
< |
try { |
577 |
< |
LinkedList l = new LinkedList(); |
578 |
< |
l.add(new Object()); |
579 |
< |
l.remove(5); |
580 |
< |
shouldThrow(); |
581 |
< |
} catch(IndexOutOfBoundsException success){} |
575 |
> |
public void testPeekLast() { |
576 |
> |
LinkedList q = populatedQueue(SIZE); |
577 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
578 |
> |
assertEquals(i, q.peekLast()); |
579 |
> |
assertEquals(i, q.pollLast()); |
580 |
> |
assertTrue(q.peekLast() == null || |
581 |
> |
!q.peekLast().equals(i)); |
582 |
> |
} |
583 |
> |
assertNull(q.peekLast()); |
584 |
|
} |
585 |
|
|
586 |
< |
|
587 |
< |
/** |
588 |
< |
* |
589 |
< |
*/ |
590 |
< |
public void testremove2() { |
586 |
> |
public void testFirstElement() { |
587 |
> |
LinkedList q = populatedQueue(SIZE); |
588 |
> |
for (int i = 0; i < SIZE; ++i) { |
589 |
> |
assertEquals(i, q.getFirst()); |
590 |
> |
assertEquals(i, q.pollFirst()); |
591 |
> |
} |
592 |
|
try { |
593 |
< |
LinkedList l = new LinkedList(); |
663 |
< |
l.remove(); |
593 |
> |
q.getFirst(); |
594 |
|
shouldThrow(); |
595 |
< |
} catch(NoSuchElementException e){} |
595 |
> |
} catch (NoSuchElementException success) {} |
596 |
|
} |
597 |
|
|
598 |
|
/** |
599 |
< |
* |
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 |
< |
* |
599 |
> |
* getLast returns next element, or throws NSEE if empty |
600 |
|
*/ |
601 |
< |
public void testlistIt2() { |
602 |
< |
try { |
603 |
< |
LinkedList l = new LinkedList(); |
604 |
< |
l.add(new Object()); |
605 |
< |
l.listIterator(-1); |
606 |
< |
shouldThrow(); |
607 |
< |
} catch(IndexOutOfBoundsException success){} |
608 |
< |
} |
609 |
< |
|
610 |
< |
/** |
611 |
< |
* |
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){} |
601 |
> |
public void testLastElement() { |
602 |
> |
LinkedList q = populatedQueue(SIZE); |
603 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
604 |
> |
assertEquals(i, q.getLast()); |
605 |
> |
assertEquals(i, q.pollLast()); |
606 |
> |
} |
607 |
> |
try { |
608 |
> |
q.getLast(); |
609 |
> |
shouldThrow(); |
610 |
> |
} catch (NoSuchElementException success) {} |
611 |
> |
assertNull(q.peekLast()); |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
< |
* |
615 |
> |
* removeFirstOccurrence(x) removes x and returns true if present |
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 |
< |
shouldThrow(); |
623 |
< |
} catch(NullPointerException success){} |
617 |
> |
public void testRemoveFirstOccurrence() { |
618 |
> |
LinkedList q = populatedQueue(SIZE); |
619 |
> |
for (int i = 1; i < SIZE; i += 2) { |
620 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
621 |
> |
} |
622 |
> |
for (int i = 0; i < SIZE; i += 2) { |
623 |
> |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
624 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
625 |
> |
} |
626 |
> |
assertTrue(q.isEmpty()); |
627 |
|
} |
628 |
|
|
629 |
|
/** |
630 |
< |
* |
630 |
> |
* removeLastOccurrence(x) removes x and returns true if present |
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 |
< |
shouldThrow(); |
638 |
< |
} catch(ArrayStoreException success){} |
632 |
> |
public void testRemoveLastOccurrence() { |
633 |
> |
LinkedList q = populatedQueue(SIZE); |
634 |
> |
for (int i = 1; i < SIZE; i += 2) { |
635 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
636 |
> |
} |
637 |
> |
for (int i = 0; i < SIZE; i += 2) { |
638 |
> |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
639 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
640 |
> |
} |
641 |
> |
assertTrue(q.isEmpty()); |
642 |
|
} |
643 |
|
|
644 |
|
} |