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