34 |
|
return q; |
35 |
|
} |
36 |
|
|
37 |
< |
public void testConstructor1(){ |
37 |
> |
/** |
38 |
> |
* new queue is empty |
39 |
> |
*/ |
40 |
> |
public void testConstructor1() { |
41 |
|
assertEquals(0, new ConcurrentLinkedQueue().size()); |
42 |
|
} |
43 |
|
|
44 |
+ |
/** |
45 |
+ |
* Initializing from null Collection throws NPE |
46 |
+ |
*/ |
47 |
|
public void testConstructor3() { |
48 |
|
try { |
49 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); |
50 |
< |
fail("Cannot make from null collection"); |
50 |
> |
shouldThrow(); |
51 |
|
} |
52 |
|
catch (NullPointerException success) {} |
53 |
|
} |
54 |
|
|
55 |
< |
public void testConstructor4(){ |
55 |
> |
/** |
56 |
> |
* Initializing from Collection of null elements throws NPE |
57 |
> |
*/ |
58 |
> |
public void testConstructor4() { |
59 |
|
try { |
60 |
|
Integer[] ints = new Integer[SIZE]; |
61 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
62 |
< |
fail("Cannot make with null elements"); |
62 |
> |
shouldThrow(); |
63 |
|
} |
64 |
|
catch (NullPointerException success) {} |
65 |
|
} |
66 |
|
|
67 |
< |
public void testConstructor5(){ |
67 |
> |
/** |
68 |
> |
* Initializing from Collection with some null elements throws NPE |
69 |
> |
*/ |
70 |
> |
public void testConstructor5() { |
71 |
|
try { |
72 |
|
Integer[] ints = new Integer[SIZE]; |
73 |
|
for (int i = 0; i < SIZE-1; ++i) |
74 |
|
ints[i] = new Integer(i); |
75 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
76 |
< |
fail("Cannot make with null elements"); |
76 |
> |
shouldThrow(); |
77 |
|
} |
78 |
|
catch (NullPointerException success) {} |
79 |
|
} |
80 |
|
|
81 |
< |
public void testConstructor6(){ |
81 |
> |
/** |
82 |
> |
* Queue contains all elements of collection used to initialize |
83 |
> |
*/ |
84 |
> |
public void testConstructor6() { |
85 |
|
try { |
86 |
|
Integer[] ints = new Integer[SIZE]; |
87 |
|
for (int i = 0; i < SIZE; ++i) |
93 |
|
finally {} |
94 |
|
} |
95 |
|
|
96 |
+ |
/** |
97 |
+ |
* isEmpty is true before add, false after |
98 |
+ |
*/ |
99 |
|
public void testEmpty() { |
100 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
101 |
|
assertTrue(q.isEmpty()); |
107 |
|
assertTrue(q.isEmpty()); |
108 |
|
} |
109 |
|
|
110 |
+ |
/** |
111 |
+ |
* size changes when elements added and removed |
112 |
+ |
*/ |
113 |
|
public void testSize() { |
114 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
115 |
|
for (int i = 0; i < SIZE; ++i) { |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
< |
public void testOfferNull(){ |
125 |
> |
/** |
126 |
> |
* offer(null) throws NPE |
127 |
> |
*/ |
128 |
> |
public void testOfferNull() { |
129 |
|
try { |
130 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
131 |
|
q.offer(null); |
132 |
< |
fail("should throw NPE"); |
132 |
> |
shouldThrow(); |
133 |
|
} catch (NullPointerException success) { } |
134 |
|
} |
135 |
|
|
136 |
+ |
/** |
137 |
+ |
* add(null) throws NPE |
138 |
+ |
*/ |
139 |
+ |
public void testAddNull() { |
140 |
+ |
try { |
141 |
+ |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
142 |
+ |
q.add(null); |
143 |
+ |
shouldThrow(); |
144 |
+ |
} catch (NullPointerException success) { } |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
|
148 |
+ |
/** |
149 |
+ |
* Offer returns true |
150 |
+ |
*/ |
151 |
|
public void testOffer() { |
152 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
153 |
|
assertTrue(q.offer(zero)); |
154 |
|
assertTrue(q.offer(one)); |
155 |
|
} |
156 |
|
|
157 |
< |
public void testAdd(){ |
157 |
> |
/** |
158 |
> |
* add returns true |
159 |
> |
*/ |
160 |
> |
public void testAdd() { |
161 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
162 |
|
for (int i = 0; i < SIZE; ++i) { |
163 |
|
assertEquals(i, q.size()); |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
< |
public void testAddAll1(){ |
168 |
> |
/** |
169 |
> |
* addAll(null) throws NPE |
170 |
> |
*/ |
171 |
> |
public void testAddAll1() { |
172 |
|
try { |
173 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
174 |
|
q.addAll(null); |
175 |
< |
fail("Cannot add null collection"); |
175 |
> |
shouldThrow(); |
176 |
|
} |
177 |
|
catch (NullPointerException success) {} |
178 |
|
} |
179 |
< |
public void testAddAll2(){ |
179 |
> |
|
180 |
> |
/** |
181 |
> |
* addAll(this) throws IAE |
182 |
> |
*/ |
183 |
> |
public void testAddAllSelf() { |
184 |
> |
try { |
185 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
186 |
> |
q.addAll(q); |
187 |
> |
shouldThrow(); |
188 |
> |
} |
189 |
> |
catch (IllegalArgumentException success) {} |
190 |
> |
} |
191 |
> |
|
192 |
> |
/** |
193 |
> |
* addAll of a collection with null elements throws NPE |
194 |
> |
*/ |
195 |
> |
public void testAddAll2() { |
196 |
|
try { |
197 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
198 |
|
Integer[] ints = new Integer[SIZE]; |
199 |
|
q.addAll(Arrays.asList(ints)); |
200 |
< |
fail("Cannot add null elements"); |
200 |
> |
shouldThrow(); |
201 |
|
} |
202 |
|
catch (NullPointerException success) {} |
203 |
|
} |
204 |
< |
public void testAddAll3(){ |
204 |
> |
/** |
205 |
> |
* addAll of a collection with any null elements throws NPE after |
206 |
> |
* possibly adding some elements |
207 |
> |
*/ |
208 |
> |
public void testAddAll3() { |
209 |
|
try { |
210 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
211 |
|
Integer[] ints = new Integer[SIZE]; |
212 |
|
for (int i = 0; i < SIZE-1; ++i) |
213 |
|
ints[i] = new Integer(i); |
214 |
|
q.addAll(Arrays.asList(ints)); |
215 |
< |
fail("Cannot add null elements"); |
215 |
> |
shouldThrow(); |
216 |
|
} |
217 |
|
catch (NullPointerException success) {} |
218 |
|
} |
219 |
|
|
220 |
< |
public void testAddAll5(){ |
220 |
> |
/** |
221 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
222 |
> |
*/ |
223 |
> |
public void testAddAll5() { |
224 |
|
try { |
225 |
|
Integer[] empty = new Integer[0]; |
226 |
|
Integer[] ints = new Integer[SIZE]; |
235 |
|
finally {} |
236 |
|
} |
237 |
|
|
238 |
< |
public void testPoll(){ |
238 |
> |
/** |
239 |
> |
* poll succeeds unless empty |
240 |
> |
*/ |
241 |
> |
public void testPoll() { |
242 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
243 |
|
for (int i = 0; i < SIZE; ++i) { |
244 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
246 |
|
assertNull(q.poll()); |
247 |
|
} |
248 |
|
|
249 |
< |
public void testPeek(){ |
249 |
> |
/** |
250 |
> |
* peek returns next element, or null if empty |
251 |
> |
*/ |
252 |
> |
public void testPeek() { |
253 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
254 |
|
for (int i = 0; i < SIZE; ++i) { |
255 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
260 |
|
assertNull(q.peek()); |
261 |
|
} |
262 |
|
|
263 |
< |
public void testElement(){ |
263 |
> |
/** |
264 |
> |
* element returns next element, or throws NSEE if empty |
265 |
> |
*/ |
266 |
> |
public void testElement() { |
267 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
268 |
|
for (int i = 0; i < SIZE; ++i) { |
269 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
271 |
|
} |
272 |
|
try { |
273 |
|
q.element(); |
274 |
< |
fail("no such element"); |
274 |
> |
shouldThrow(); |
275 |
|
} |
276 |
|
catch (NoSuchElementException success) {} |
277 |
|
} |
278 |
|
|
279 |
< |
public void testRemove(){ |
279 |
> |
/** |
280 |
> |
* remove removes next element, or throws NSEE if empty |
281 |
> |
*/ |
282 |
> |
public void testRemove() { |
283 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
284 |
|
for (int i = 0; i < SIZE; ++i) { |
285 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
286 |
|
} |
287 |
|
try { |
288 |
|
q.remove(); |
289 |
< |
fail("remove should throw"); |
289 |
> |
shouldThrow(); |
290 |
|
} catch (NoSuchElementException success){ |
291 |
|
} |
292 |
|
} |
293 |
|
|
294 |
< |
public void testRemoveElement(){ |
294 |
> |
/** |
295 |
> |
* remove(x) removes x and returns true if present |
296 |
> |
*/ |
297 |
> |
public void testRemoveElement() { |
298 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
299 |
|
for (int i = 1; i < SIZE; i+=2) { |
300 |
|
assertTrue(q.remove(new Integer(i))); |
306 |
|
assertTrue(q.isEmpty()); |
307 |
|
} |
308 |
|
|
309 |
< |
public void testContains(){ |
309 |
> |
/** |
310 |
> |
* contains(x) reports true when elements added but not yet removed |
311 |
> |
*/ |
312 |
> |
public void testContains() { |
313 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
314 |
|
for (int i = 0; i < SIZE; ++i) { |
315 |
|
assertTrue(q.contains(new Integer(i))); |
318 |
|
} |
319 |
|
} |
320 |
|
|
321 |
< |
public void testClear(){ |
321 |
> |
/** |
322 |
> |
* clear removes all elements |
323 |
> |
*/ |
324 |
> |
public void testClear() { |
325 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
326 |
|
q.clear(); |
327 |
|
assertTrue(q.isEmpty()); |
332 |
|
assertTrue(q.isEmpty()); |
333 |
|
} |
334 |
|
|
335 |
< |
public void testContainsAll(){ |
335 |
> |
/** |
336 |
> |
* containsAll(c) is true when c contains a subset of elements |
337 |
> |
*/ |
338 |
> |
public void testContainsAll() { |
339 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
340 |
|
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue(); |
341 |
|
for (int i = 0; i < SIZE; ++i) { |
346 |
|
assertTrue(p.containsAll(q)); |
347 |
|
} |
348 |
|
|
349 |
< |
public void testRetainAll(){ |
349 |
> |
/** |
350 |
> |
* retainAll(c) retains only those elements of c and reports true if change |
351 |
> |
*/ |
352 |
> |
public void testRetainAll() { |
353 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
354 |
|
ConcurrentLinkedQueue p = populatedQueue(SIZE); |
355 |
|
for (int i = 0; i < SIZE; ++i) { |
365 |
|
} |
366 |
|
} |
367 |
|
|
368 |
< |
public void testRemoveAll(){ |
368 |
> |
/** |
369 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
370 |
> |
*/ |
371 |
> |
public void testRemoveAll() { |
372 |
|
for (int i = 1; i < SIZE; ++i) { |
373 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
374 |
|
ConcurrentLinkedQueue p = populatedQueue(i); |
381 |
|
} |
382 |
|
} |
383 |
|
|
384 |
< |
public void testToArray(){ |
384 |
> |
/** |
385 |
> |
* toArray contains all elements |
386 |
> |
*/ |
387 |
> |
public void testToArray() { |
388 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
389 |
|
Object[] o = q.toArray(); |
390 |
|
Arrays.sort(o); |
392 |
|
assertEquals(o[i], q.poll()); |
393 |
|
} |
394 |
|
|
395 |
< |
public void testToArray2(){ |
395 |
> |
/** |
396 |
> |
* toArray(a) contains all elements |
397 |
> |
*/ |
398 |
> |
public void testToArray2() { |
399 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
400 |
|
Integer[] ints = new Integer[SIZE]; |
401 |
|
ints = (Integer[])q.toArray(ints); |
403 |
|
for(int i = 0; i < ints.length; i++) |
404 |
|
assertEquals(ints[i], q.poll()); |
405 |
|
} |
406 |
+ |
|
407 |
+ |
/** |
408 |
+ |
* toArray(null) throws NPE |
409 |
+ |
*/ |
410 |
+ |
public void testToArray_BadArg() { |
411 |
+ |
try { |
412 |
+ |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
413 |
+ |
Object o[] = q.toArray(null); |
414 |
+ |
shouldThrow(); |
415 |
+ |
} catch(NullPointerException success){} |
416 |
+ |
} |
417 |
+ |
|
418 |
+ |
/** |
419 |
+ |
* toArray with incompatable array type throws CCE |
420 |
+ |
*/ |
421 |
+ |
public void testToArray1_BadArg() { |
422 |
+ |
try { |
423 |
+ |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
424 |
+ |
Object o[] = q.toArray(new String[10] ); |
425 |
+ |
shouldThrow(); |
426 |
+ |
} catch(ArrayStoreException success){} |
427 |
+ |
} |
428 |
|
|
429 |
< |
public void testIterator(){ |
429 |
> |
/** |
430 |
> |
* iterator iterates through all elements |
431 |
> |
*/ |
432 |
> |
public void testIterator() { |
433 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
434 |
|
int i = 0; |
435 |
|
Iterator it = q.iterator(); |
440 |
|
assertEquals(i, SIZE); |
441 |
|
} |
442 |
|
|
443 |
+ |
/** |
444 |
+ |
* iterator ordering is FIFO |
445 |
+ |
*/ |
446 |
|
public void testIteratorOrdering() { |
447 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
448 |
|
q.add(one); |
452 |
|
int k = 0; |
453 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
454 |
|
int i = ((Integer)(it.next())).intValue(); |
455 |
< |
assertEquals("items should come out in order", ++k, i); |
455 |
> |
assertEquals(++k, i); |
456 |
|
} |
457 |
|
|
458 |
< |
assertEquals("should go through 3 elements", 3, k); |
458 |
> |
assertEquals(3, k); |
459 |
|
} |
460 |
|
|
461 |
+ |
/** |
462 |
+ |
* Modifications do not cause iterators to fail |
463 |
+ |
*/ |
464 |
|
public void testWeaklyConsistentIteration () { |
465 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
466 |
|
q.add(one); |
474 |
|
} |
475 |
|
} |
476 |
|
catch (ConcurrentModificationException e) { |
477 |
< |
fail("weakly consistent iterator; should not get CME"); |
477 |
> |
shouldThrow(); |
478 |
|
} |
479 |
|
|
480 |
|
assertEquals("queue should be empty again", 0, q.size()); |
481 |
|
} |
482 |
|
|
483 |
+ |
/** |
484 |
+ |
* iterator.remove removes current element |
485 |
+ |
*/ |
486 |
|
public void testIteratorRemove () { |
487 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
488 |
|
q.add(one); |
498 |
|
} |
499 |
|
|
500 |
|
|
501 |
< |
public void testToString(){ |
501 |
> |
/** |
502 |
> |
* toString contains toStrings of elements |
503 |
> |
*/ |
504 |
> |
public void testToString() { |
505 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
506 |
|
String s = q.toString(); |
507 |
|
for (int i = 0; i < SIZE; ++i) { |
509 |
|
} |
510 |
|
} |
511 |
|
|
512 |
+ |
/** |
513 |
+ |
* A deserialized serialized queue has same elements in same order |
514 |
+ |
*/ |
515 |
|
public void testSerialization() { |
516 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
517 |
|
try { |
527 |
|
while (!q.isEmpty()) |
528 |
|
assertEquals(q.remove(), r.remove()); |
529 |
|
} catch(Exception e){ |
530 |
< |
e.printStackTrace(); |
387 |
< |
fail("unexpected exception"); |
530 |
> |
unexpectedException(); |
531 |
|
} |
532 |
|
} |
533 |
|
|