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