33 |
} |
} |
34 |
|
|
35 |
/** |
/** |
36 |
* |
* new queue is empty |
37 |
*/ |
*/ |
38 |
public void testConstructor1() { |
public void testConstructor1() { |
39 |
assertEquals(0, new LinkedList().size()); |
assertEquals(0, new LinkedList().size()); |
40 |
} |
} |
41 |
|
|
42 |
/** |
/** |
43 |
* |
* Initializing from null Collection throws NPE |
44 |
*/ |
*/ |
45 |
public void testConstructor3() { |
public void testConstructor3() { |
46 |
try { |
try { |
51 |
} |
} |
52 |
|
|
53 |
/** |
/** |
54 |
* |
* Queue contains all elements of collection used to initialize |
55 |
|
|
56 |
*/ |
*/ |
57 |
public void testConstructor6() { |
public void testConstructor6() { |
58 |
try { |
try { |
67 |
} |
} |
68 |
|
|
69 |
/** |
/** |
70 |
* |
* isEmpty is true before add, false after |
71 |
*/ |
*/ |
72 |
public void testEmpty() { |
public void testEmpty() { |
73 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* |
* size changes when elements added and removed |
85 |
*/ |
*/ |
86 |
public void testSize() { |
public void testSize() { |
87 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
96 |
} |
} |
97 |
|
|
98 |
/** |
/** |
99 |
* |
* offer(null) succeeds |
100 |
*/ |
*/ |
101 |
public void testOfferNull() { |
public void testOfferNull() { |
102 |
try { |
try { |
108 |
} |
} |
109 |
|
|
110 |
/** |
/** |
111 |
* |
* Offer succeeds |
112 |
*/ |
*/ |
113 |
public void testOffer() { |
public void testOffer() { |
114 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
117 |
} |
} |
118 |
|
|
119 |
/** |
/** |
120 |
* |
* add succeeds |
121 |
*/ |
*/ |
122 |
public void testAdd() { |
public void testAdd() { |
123 |
LinkedList q = new LinkedList(); |
LinkedList q = new LinkedList(); |
128 |
} |
} |
129 |
|
|
130 |
/** |
/** |
131 |
* |
* addAll(null) throws NPE |
132 |
*/ |
*/ |
133 |
public void testAddAll1() { |
public void testAddAll1() { |
134 |
try { |
try { |
140 |
} |
} |
141 |
|
|
142 |
/** |
/** |
143 |
* |
* Queue contains all elements, in traversal order, of successful addAll |
144 |
*/ |
*/ |
145 |
public void testAddAll5() { |
public void testAddAll5() { |
146 |
try { |
try { |
158 |
} |
} |
159 |
|
|
160 |
/** |
/** |
161 |
* |
* addAll with too large an index throws IOOBE |
162 |
|
*/ |
163 |
|
public void testAddAll2_IndexOutOfBoundsException() { |
164 |
|
try { |
165 |
|
LinkedList l = new LinkedList(); |
166 |
|
l.add(new Object()); |
167 |
|
LinkedList m = new LinkedList(); |
168 |
|
m.add(new Object()); |
169 |
|
l.addAll(4,m); |
170 |
|
shouldThrow(); |
171 |
|
} catch(IndexOutOfBoundsException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
|
* addAll with negative index throws IOOBE |
176 |
|
*/ |
177 |
|
public void testAddAll4_BadIndex() { |
178 |
|
try { |
179 |
|
LinkedList l = new LinkedList(); |
180 |
|
l.add(new Object()); |
181 |
|
LinkedList m = new LinkedList(); |
182 |
|
m.add(new Object()); |
183 |
|
l.addAll(-1,m); |
184 |
|
shouldThrow(); |
185 |
|
} catch(IndexOutOfBoundsException success){} |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
|
* poll succeeds unless empty |
190 |
*/ |
*/ |
191 |
public void testPoll() { |
public void testPoll() { |
192 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
197 |
} |
} |
198 |
|
|
199 |
/** |
/** |
200 |
* |
* peek returns next element, or null if empty |
201 |
*/ |
*/ |
202 |
public void testPeek() { |
public void testPeek() { |
203 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
227 |
} |
} |
228 |
|
|
229 |
/** |
/** |
230 |
* |
* element returns next element, or throws NSEE if empty |
231 |
*/ |
*/ |
232 |
public void testRemove() { |
public void testRemove() { |
233 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
242 |
} |
} |
243 |
|
|
244 |
/** |
/** |
245 |
* |
* remove(x) removes x and returns true if present |
246 |
*/ |
*/ |
247 |
public void testRemoveElement() { |
public void testRemoveElement() { |
248 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
257 |
} |
} |
258 |
|
|
259 |
/** |
/** |
260 |
* |
* contains(x) reports true when elements added but not yet removed |
261 |
*/ |
*/ |
262 |
public void testContains() { |
public void testContains() { |
263 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
269 |
} |
} |
270 |
|
|
271 |
/** |
/** |
272 |
* |
* clear removes all elements |
273 |
*/ |
*/ |
274 |
public void testClear() { |
public void testClear() { |
275 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
283 |
} |
} |
284 |
|
|
285 |
/** |
/** |
286 |
* |
* containsAll(c) is true when c contains a subset of elements |
287 |
*/ |
*/ |
288 |
public void testContainsAll() { |
public void testContainsAll() { |
289 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
297 |
} |
} |
298 |
|
|
299 |
/** |
/** |
300 |
* |
* retainAll(c) retains only those elements of c and reports true if changed |
301 |
*/ |
*/ |
302 |
public void testRetainAll() { |
public void testRetainAll() { |
303 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
316 |
} |
} |
317 |
|
|
318 |
/** |
/** |
319 |
* |
* removeAll(c) removes only those elements of c and reports true if changed |
320 |
*/ |
*/ |
321 |
public void testRemoveAll() { |
public void testRemoveAll() { |
322 |
for (int i = 1; i < SIZE; ++i) { |
for (int i = 1; i < SIZE; ++i) { |
332 |
} |
} |
333 |
|
|
334 |
/** |
/** |
335 |
* |
* toArray contains all elements |
336 |
*/ |
*/ |
337 |
public void testToArray() { |
public void testToArray() { |
338 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
343 |
} |
} |
344 |
|
|
345 |
/** |
/** |
346 |
* |
* toArray(a) contains all elements |
347 |
*/ |
*/ |
348 |
public void testToArray2() { |
public void testToArray2() { |
349 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
355 |
} |
} |
356 |
|
|
357 |
/** |
/** |
358 |
* |
* toArray(null) throws NPE |
359 |
|
*/ |
360 |
|
public void testToArray_BadArg() { |
361 |
|
try { |
362 |
|
LinkedList l = new LinkedList(); |
363 |
|
l.add(new Object()); |
364 |
|
Object o[] = l.toArray(null); |
365 |
|
shouldThrow(); |
366 |
|
} catch(NullPointerException success){} |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
|
* toArray with incompatable aray type throws CCE |
371 |
|
*/ |
372 |
|
public void testToArray1_BadArg() { |
373 |
|
try { |
374 |
|
LinkedList l = new LinkedList(); |
375 |
|
l.add(new Integer(5)); |
376 |
|
Object o[] = l.toArray(new String[10] ); |
377 |
|
shouldThrow(); |
378 |
|
} catch(ArrayStoreException success){} |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
|
* iterator iterates through all elements |
383 |
*/ |
*/ |
384 |
public void testIterator() { |
public void testIterator() { |
385 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
393 |
} |
} |
394 |
|
|
395 |
/** |
/** |
396 |
* |
* iterator ordering is FIFO |
397 |
*/ |
*/ |
398 |
public void testIteratorOrdering() { |
public void testIteratorOrdering() { |
|
|
|
399 |
final LinkedList q = new LinkedList(); |
final LinkedList q = new LinkedList(); |
|
|
|
400 |
q.add(new Integer(1)); |
q.add(new Integer(1)); |
401 |
q.add(new Integer(2)); |
q.add(new Integer(2)); |
402 |
q.add(new Integer(3)); |
q.add(new Integer(3)); |
|
|
|
403 |
int k = 0; |
int k = 0; |
404 |
for (Iterator it = q.iterator(); it.hasNext();) { |
for (Iterator it = q.iterator(); it.hasNext();) { |
405 |
int i = ((Integer)(it.next())).intValue(); |
int i = ((Integer)(it.next())).intValue(); |
410 |
} |
} |
411 |
|
|
412 |
/** |
/** |
413 |
* |
* iterator.remove removes current element |
414 |
*/ |
*/ |
415 |
public void testIteratorRemove () { |
public void testIteratorRemove () { |
416 |
final LinkedList q = new LinkedList(); |
final LinkedList q = new LinkedList(); |
|
|
|
417 |
q.add(new Integer(1)); |
q.add(new Integer(1)); |
418 |
q.add(new Integer(2)); |
q.add(new Integer(2)); |
419 |
q.add(new Integer(3)); |
q.add(new Integer(3)); |
|
|
|
420 |
Iterator it = q.iterator(); |
Iterator it = q.iterator(); |
421 |
it.next(); |
it.next(); |
422 |
it.remove(); |
it.remove(); |
|
|
|
423 |
it = q.iterator(); |
it = q.iterator(); |
424 |
assertEquals(it.next(), new Integer(2)); |
assertEquals(it.next(), new Integer(2)); |
425 |
assertEquals(it.next(), new Integer(3)); |
assertEquals(it.next(), new Integer(3)); |
428 |
|
|
429 |
|
|
430 |
/** |
/** |
431 |
* |
* toString contains toStrings of elements |
432 |
*/ |
*/ |
433 |
public void testToString() { |
public void testToString() { |
434 |
LinkedList q = populatedQueue(SIZE); |
LinkedList q = populatedQueue(SIZE); |
439 |
} |
} |
440 |
|
|
441 |
/** |
/** |
442 |
* |
* peek returns element inserted with addFirst |
443 |
*/ |
*/ |
444 |
public void testAddFirst() { |
public void testAddFirst() { |
445 |
LinkedList q = populatedQueue(3); |
LinkedList q = populatedQueue(3); |
446 |
q.addFirst(new Integer(4)); |
q.addFirst(four); |
447 |
assertEquals(new Integer(4),q.get(0)); |
assertEquals(four,q.peek()); |
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testAddLast() { |
|
|
LinkedList q = populatedQueue(3); |
|
|
q.addLast(new Integer(3)); |
|
|
assertEquals(new Integer(3),q.get(3)); |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testGetFirst() { |
|
|
LinkedList q = populatedQueue(3); |
|
|
assertEquals(new Integer(0),q.getFirst()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testGetLast() { |
|
|
LinkedList q = populatedQueue(3); |
|
|
assertEquals(new Integer(2),q.getLast()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testIndexOf() { |
|
|
LinkedList q = populatedQueue(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 = populatedQueue(3); |
|
|
q.add(new Integer(2)); |
|
|
assertEquals(3,q.lastIndexOf(new Integer(2))); |
|
|
assertEquals(-1, q.lastIndexOf("not there")); |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testSet() { |
|
|
LinkedList q = populatedQueue(3); |
|
|
q.set(0,(new Integer(1))); |
|
|
assertFalse(q.contains(new Integer(0))); |
|
|
assertEquals(new Integer(1), q.get(0)); |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testGetFirst_NoSuchElementException() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.getFirst(); |
|
|
shouldThrow(); |
|
|
} |
|
|
catch(NoSuchElementException success) {} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testRemoveFirst() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.removeFirst(); |
|
|
shouldThrow(); |
|
|
} |
|
|
catch(NoSuchElementException success) {} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testRemoveLast() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.removeLast(); |
|
|
shouldThrow(); |
|
|
} |
|
|
catch(NoSuchElementException success) {} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testGetLast_NoSuchElementException() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.getLast(); |
|
|
shouldThrow(); |
|
|
} |
|
|
catch(NoSuchElementException success) {} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testAddAll_NullPointerException() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.addAll((Collection)null); |
|
|
shouldThrow(); |
|
|
} |
|
|
catch(NullPointerException success){} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testAddAll1_OutOfBounds() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.addAll(4,new LinkedList()); |
|
|
shouldThrow(); |
|
|
} |
|
|
catch(IndexOutOfBoundsException success) {} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testAddAll2_IndexOutOfBoundsException() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
LinkedList m = new LinkedList(); |
|
|
m.add(new Object()); |
|
|
l.addAll(4,m); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success) {} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testAddAll4_BadIndex() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
LinkedList m = new LinkedList(); |
|
|
m.add(new Object()); |
|
|
l.addAll(-1,m); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testget1() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.get(-1); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success) {} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testget2() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.get(5); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testset1() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.set(-1,new Object()); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testset2() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.set(5,new Object()); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testadd1() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.add(-1,new Object()); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
public void add2() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.add(5,new Object()); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success) {} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testremove() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.remove(-1); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testremove1() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.remove(5); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testremove2() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.remove(); |
|
|
shouldThrow(); |
|
|
} catch(NoSuchElementException e){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testlistIt1() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.listIterator(5); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testlistIt2() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
l.listIterator(-1); |
|
|
shouldThrow(); |
|
|
} catch(IndexOutOfBoundsException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testlistIt3() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
ListIterator a = l.listIterator(0); |
|
|
l.removeFirst(); |
|
|
a.next(); |
|
|
shouldThrow(); |
|
|
} catch(ConcurrentModificationException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testToArray_BadArg() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Object()); |
|
|
Object o[] = l.toArray(null); |
|
|
shouldThrow(); |
|
|
} catch(NullPointerException success){} |
|
|
} |
|
|
|
|
|
/** |
|
|
* |
|
|
*/ |
|
|
public void testToArray1_BadArg() { |
|
|
try { |
|
|
LinkedList l = new LinkedList(); |
|
|
l.add(new Integer(5)); |
|
|
Object o[] = l.toArray(new String[10] ); |
|
|
shouldThrow(); |
|
|
} catch(ArrayStoreException success){} |
|
448 |
} |
} |
449 |
|
|
450 |
} |
} |