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