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