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