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