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