1 |
/* |
2 |
* 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 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import java.util.Arrays; |
10 |
import java.util.Collection; |
11 |
import java.util.Deque; |
12 |
import java.util.Iterator; |
13 |
import java.util.NoSuchElementException; |
14 |
import java.util.Queue; |
15 |
import java.util.Random; |
16 |
import java.util.concurrent.ConcurrentLinkedDeque; |
17 |
|
18 |
import junit.framework.Test; |
19 |
|
20 |
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
21 |
|
22 |
public static void main(String[] args) { |
23 |
main(suite(), args); |
24 |
} |
25 |
|
26 |
public static Test suite() { |
27 |
class Implementation implements CollectionImplementation { |
28 |
public Class<?> klazz() { return ConcurrentLinkedDeque.class; } |
29 |
public Collection emptyCollection() { return new ConcurrentLinkedDeque(); } |
30 |
public Object makeElement(int i) { return i; } |
31 |
public boolean isConcurrent() { return true; } |
32 |
public boolean permitsNulls() { return false; } |
33 |
} |
34 |
return newTestSuite(ConcurrentLinkedDequeTest.class, |
35 |
CollectionTest.testSuite(new Implementation())); |
36 |
} |
37 |
|
38 |
/** |
39 |
* Returns a new deque of given size containing consecutive |
40 |
* Integers 0 ... n - 1. |
41 |
*/ |
42 |
private static ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
43 |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>(); |
44 |
assertTrue(q.isEmpty()); |
45 |
for (int i = 0; i < n; ++i) |
46 |
assertTrue(q.offer(new Integer(i))); |
47 |
assertFalse(q.isEmpty()); |
48 |
assertEquals(n, q.size()); |
49 |
assertEquals((Integer) 0, q.peekFirst()); |
50 |
assertEquals((Integer) (n - 1), q.peekLast()); |
51 |
return q; |
52 |
} |
53 |
|
54 |
/** |
55 |
* new deque is empty |
56 |
*/ |
57 |
public void testConstructor1() { |
58 |
assertTrue(new ConcurrentLinkedDeque().isEmpty()); |
59 |
assertEquals(0, new ConcurrentLinkedDeque().size()); |
60 |
} |
61 |
|
62 |
/** |
63 |
* Initializing from null Collection throws NPE |
64 |
*/ |
65 |
public void testConstructor3() { |
66 |
try { |
67 |
new ConcurrentLinkedDeque((Collection)null); |
68 |
shouldThrow(); |
69 |
} catch (NullPointerException success) {} |
70 |
} |
71 |
|
72 |
/** |
73 |
* Initializing from Collection of null elements throws NPE |
74 |
*/ |
75 |
public void testConstructor4() { |
76 |
try { |
77 |
new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE])); |
78 |
shouldThrow(); |
79 |
} catch (NullPointerException success) {} |
80 |
} |
81 |
|
82 |
/** |
83 |
* Initializing from Collection with some null elements throws NPE |
84 |
*/ |
85 |
public void testConstructor5() { |
86 |
Integer[] ints = new Integer[SIZE]; |
87 |
for (int i = 0; i < SIZE - 1; ++i) |
88 |
ints[i] = new Integer(i); |
89 |
try { |
90 |
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
91 |
shouldThrow(); |
92 |
} catch (NullPointerException success) {} |
93 |
} |
94 |
|
95 |
/** |
96 |
* Deque contains all elements of collection used to initialize |
97 |
*/ |
98 |
public void testConstructor6() { |
99 |
Integer[] ints = new Integer[SIZE]; |
100 |
for (int i = 0; i < SIZE; ++i) |
101 |
ints[i] = new Integer(i); |
102 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); |
103 |
for (int i = 0; i < SIZE; ++i) |
104 |
assertEquals(ints[i], q.poll()); |
105 |
} |
106 |
|
107 |
/** |
108 |
* isEmpty is true before add, false after |
109 |
*/ |
110 |
public void testEmpty() { |
111 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
112 |
assertTrue(q.isEmpty()); |
113 |
q.add(one); |
114 |
assertFalse(q.isEmpty()); |
115 |
q.add(two); |
116 |
q.remove(); |
117 |
q.remove(); |
118 |
assertTrue(q.isEmpty()); |
119 |
} |
120 |
|
121 |
/** |
122 |
* size() changes when elements added and removed |
123 |
*/ |
124 |
public void testSize() { |
125 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
126 |
for (int i = 0; i < SIZE; ++i) { |
127 |
assertEquals(SIZE - i, q.size()); |
128 |
q.remove(); |
129 |
} |
130 |
for (int i = 0; i < SIZE; ++i) { |
131 |
assertEquals(i, q.size()); |
132 |
q.add(new Integer(i)); |
133 |
} |
134 |
} |
135 |
|
136 |
/** |
137 |
* push(null) throws NPE |
138 |
*/ |
139 |
public void testPushNull() { |
140 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
141 |
try { |
142 |
q.push(null); |
143 |
shouldThrow(); |
144 |
} catch (NullPointerException success) {} |
145 |
} |
146 |
|
147 |
/** |
148 |
* peekFirst() returns element inserted with push |
149 |
*/ |
150 |
public void testPush() { |
151 |
ConcurrentLinkedDeque q = populatedDeque(3); |
152 |
q.pollLast(); |
153 |
q.push(four); |
154 |
assertSame(four, q.peekFirst()); |
155 |
} |
156 |
|
157 |
/** |
158 |
* pop() removes first element, or throws NSEE if empty |
159 |
*/ |
160 |
public void testPop() { |
161 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
162 |
for (int i = 0; i < SIZE; ++i) { |
163 |
assertEquals(i, q.pop()); |
164 |
} |
165 |
try { |
166 |
q.pop(); |
167 |
shouldThrow(); |
168 |
} catch (NoSuchElementException success) {} |
169 |
} |
170 |
|
171 |
/** |
172 |
* offer(null) throws NPE |
173 |
*/ |
174 |
public void testOfferNull() { |
175 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
176 |
try { |
177 |
q.offer(null); |
178 |
shouldThrow(); |
179 |
} catch (NullPointerException success) {} |
180 |
} |
181 |
|
182 |
/** |
183 |
* offerFirst(null) throws NPE |
184 |
*/ |
185 |
public void testOfferFirstNull() { |
186 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
187 |
try { |
188 |
q.offerFirst(null); |
189 |
shouldThrow(); |
190 |
} catch (NullPointerException success) {} |
191 |
} |
192 |
|
193 |
/** |
194 |
* offerLast(null) throws NPE |
195 |
*/ |
196 |
public void testOfferLastNull() { |
197 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
198 |
try { |
199 |
q.offerLast(null); |
200 |
shouldThrow(); |
201 |
} catch (NullPointerException success) {} |
202 |
} |
203 |
|
204 |
/** |
205 |
* offer(x) succeeds |
206 |
*/ |
207 |
public void testOffer() { |
208 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
209 |
assertTrue(q.offer(zero)); |
210 |
assertTrue(q.offer(one)); |
211 |
assertSame(zero, q.peekFirst()); |
212 |
assertSame(one, q.peekLast()); |
213 |
} |
214 |
|
215 |
/** |
216 |
* offerFirst(x) succeeds |
217 |
*/ |
218 |
public void testOfferFirst() { |
219 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
220 |
assertTrue(q.offerFirst(zero)); |
221 |
assertTrue(q.offerFirst(one)); |
222 |
assertSame(one, q.peekFirst()); |
223 |
assertSame(zero, q.peekLast()); |
224 |
} |
225 |
|
226 |
/** |
227 |
* offerLast(x) succeeds |
228 |
*/ |
229 |
public void testOfferLast() { |
230 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
231 |
assertTrue(q.offerLast(zero)); |
232 |
assertTrue(q.offerLast(one)); |
233 |
assertSame(zero, q.peekFirst()); |
234 |
assertSame(one, q.peekLast()); |
235 |
} |
236 |
|
237 |
/** |
238 |
* add(null) throws NPE |
239 |
*/ |
240 |
public void testAddNull() { |
241 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
242 |
try { |
243 |
q.add(null); |
244 |
shouldThrow(); |
245 |
} catch (NullPointerException success) {} |
246 |
} |
247 |
|
248 |
/** |
249 |
* addFirst(null) throws NPE |
250 |
*/ |
251 |
public void testAddFirstNull() { |
252 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
253 |
try { |
254 |
q.addFirst(null); |
255 |
shouldThrow(); |
256 |
} catch (NullPointerException success) {} |
257 |
} |
258 |
|
259 |
/** |
260 |
* addLast(null) throws NPE |
261 |
*/ |
262 |
public void testAddLastNull() { |
263 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
264 |
try { |
265 |
q.addLast(null); |
266 |
shouldThrow(); |
267 |
} catch (NullPointerException success) {} |
268 |
} |
269 |
|
270 |
/** |
271 |
* add(x) succeeds |
272 |
*/ |
273 |
public void testAdd() { |
274 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
275 |
assertTrue(q.add(zero)); |
276 |
assertTrue(q.add(one)); |
277 |
assertSame(zero, q.peekFirst()); |
278 |
assertSame(one, q.peekLast()); |
279 |
} |
280 |
|
281 |
/** |
282 |
* addFirst(x) succeeds |
283 |
*/ |
284 |
public void testAddFirst() { |
285 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
286 |
q.addFirst(zero); |
287 |
q.addFirst(one); |
288 |
assertSame(one, q.peekFirst()); |
289 |
assertSame(zero, q.peekLast()); |
290 |
} |
291 |
|
292 |
/** |
293 |
* addLast(x) succeeds |
294 |
*/ |
295 |
public void testAddLast() { |
296 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
297 |
q.addLast(zero); |
298 |
q.addLast(one); |
299 |
assertSame(zero, q.peekFirst()); |
300 |
assertSame(one, q.peekLast()); |
301 |
} |
302 |
|
303 |
/** |
304 |
* addAll(null) throws NPE |
305 |
*/ |
306 |
public void testAddAll1() { |
307 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
308 |
try { |
309 |
q.addAll(null); |
310 |
shouldThrow(); |
311 |
} catch (NullPointerException success) {} |
312 |
} |
313 |
|
314 |
/** |
315 |
* addAll(this) throws IllegalArgumentException |
316 |
*/ |
317 |
public void testAddAllSelf() { |
318 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
319 |
try { |
320 |
q.addAll(q); |
321 |
shouldThrow(); |
322 |
} catch (IllegalArgumentException success) {} |
323 |
} |
324 |
|
325 |
/** |
326 |
* addAll of a collection with null elements throws NPE |
327 |
*/ |
328 |
public void testAddAll2() { |
329 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
330 |
try { |
331 |
q.addAll(Arrays.asList(new Integer[SIZE])); |
332 |
shouldThrow(); |
333 |
} catch (NullPointerException success) {} |
334 |
} |
335 |
|
336 |
/** |
337 |
* addAll of a collection with any null elements throws NPE after |
338 |
* possibly adding some elements |
339 |
*/ |
340 |
public void testAddAll3() { |
341 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
342 |
Integer[] ints = new Integer[SIZE]; |
343 |
for (int i = 0; i < SIZE - 1; ++i) |
344 |
ints[i] = new Integer(i); |
345 |
try { |
346 |
q.addAll(Arrays.asList(ints)); |
347 |
shouldThrow(); |
348 |
} catch (NullPointerException success) {} |
349 |
} |
350 |
|
351 |
/** |
352 |
* Deque contains all elements, in traversal order, of successful addAll |
353 |
*/ |
354 |
public void testAddAll5() { |
355 |
Integer[] empty = new Integer[0]; |
356 |
Integer[] ints = new Integer[SIZE]; |
357 |
for (int i = 0; i < SIZE; ++i) |
358 |
ints[i] = new Integer(i); |
359 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
360 |
assertFalse(q.addAll(Arrays.asList(empty))); |
361 |
assertTrue(q.addAll(Arrays.asList(ints))); |
362 |
for (int i = 0; i < SIZE; ++i) |
363 |
assertEquals(ints[i], q.poll()); |
364 |
} |
365 |
|
366 |
/** |
367 |
* pollFirst() succeeds unless empty |
368 |
*/ |
369 |
public void testPollFirst() { |
370 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
371 |
for (int i = 0; i < SIZE; ++i) { |
372 |
assertEquals(i, q.pollFirst()); |
373 |
} |
374 |
assertNull(q.pollFirst()); |
375 |
} |
376 |
|
377 |
/** |
378 |
* pollLast() succeeds unless empty |
379 |
*/ |
380 |
public void testPollLast() { |
381 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
382 |
for (int i = SIZE - 1; i >= 0; --i) { |
383 |
assertEquals(i, q.pollLast()); |
384 |
} |
385 |
assertNull(q.pollLast()); |
386 |
} |
387 |
|
388 |
/** |
389 |
* poll() succeeds unless empty |
390 |
*/ |
391 |
public void testPoll() { |
392 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
393 |
for (int i = 0; i < SIZE; ++i) { |
394 |
assertEquals(i, q.poll()); |
395 |
} |
396 |
assertNull(q.poll()); |
397 |
} |
398 |
|
399 |
/** |
400 |
* peek() returns next element, or null if empty |
401 |
*/ |
402 |
public void testPeek() { |
403 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
404 |
for (int i = 0; i < SIZE; ++i) { |
405 |
assertEquals(i, q.peek()); |
406 |
assertEquals(i, q.poll()); |
407 |
assertTrue(q.peek() == null || |
408 |
!q.peek().equals(i)); |
409 |
} |
410 |
assertNull(q.peek()); |
411 |
} |
412 |
|
413 |
/** |
414 |
* element() returns first element, or throws NSEE if empty |
415 |
*/ |
416 |
public void testElement() { |
417 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
418 |
for (int i = 0; i < SIZE; ++i) { |
419 |
assertEquals(i, q.element()); |
420 |
assertEquals(i, q.poll()); |
421 |
} |
422 |
try { |
423 |
q.element(); |
424 |
shouldThrow(); |
425 |
} catch (NoSuchElementException success) {} |
426 |
} |
427 |
|
428 |
/** |
429 |
* remove() removes next element, or throws NSEE if empty |
430 |
*/ |
431 |
public void testRemove() { |
432 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
433 |
for (int i = 0; i < SIZE; ++i) { |
434 |
assertEquals(i, q.remove()); |
435 |
} |
436 |
try { |
437 |
q.remove(); |
438 |
shouldThrow(); |
439 |
} catch (NoSuchElementException success) {} |
440 |
} |
441 |
|
442 |
/** |
443 |
* remove(x) removes x and returns true if present |
444 |
*/ |
445 |
public void testRemoveElement() { |
446 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
447 |
for (int i = 1; i < SIZE; i += 2) { |
448 |
assertTrue(q.contains(i)); |
449 |
assertTrue(q.remove(i)); |
450 |
assertFalse(q.contains(i)); |
451 |
assertTrue(q.contains(i - 1)); |
452 |
} |
453 |
for (int i = 0; i < SIZE; i += 2) { |
454 |
assertTrue(q.contains(i)); |
455 |
assertTrue(q.remove(i)); |
456 |
assertFalse(q.contains(i)); |
457 |
assertFalse(q.remove(i + 1)); |
458 |
assertFalse(q.contains(i + 1)); |
459 |
} |
460 |
assertTrue(q.isEmpty()); |
461 |
} |
462 |
|
463 |
/** |
464 |
* peekFirst() returns next element, or null if empty |
465 |
*/ |
466 |
public void testPeekFirst() { |
467 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
468 |
for (int i = 0; i < SIZE; ++i) { |
469 |
assertEquals(i, q.peekFirst()); |
470 |
assertEquals(i, q.pollFirst()); |
471 |
assertTrue(q.peekFirst() == null || |
472 |
!q.peekFirst().equals(i)); |
473 |
} |
474 |
assertNull(q.peekFirst()); |
475 |
} |
476 |
|
477 |
/** |
478 |
* peekLast() returns next element, or null if empty |
479 |
*/ |
480 |
public void testPeekLast() { |
481 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
482 |
for (int i = SIZE - 1; i >= 0; --i) { |
483 |
assertEquals(i, q.peekLast()); |
484 |
assertEquals(i, q.pollLast()); |
485 |
assertTrue(q.peekLast() == null || |
486 |
!q.peekLast().equals(i)); |
487 |
} |
488 |
assertNull(q.peekLast()); |
489 |
} |
490 |
|
491 |
/** |
492 |
* getFirst() returns first element, or throws NSEE if empty |
493 |
*/ |
494 |
public void testFirstElement() { |
495 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
496 |
for (int i = 0; i < SIZE; ++i) { |
497 |
assertEquals(i, q.getFirst()); |
498 |
assertEquals(i, q.pollFirst()); |
499 |
} |
500 |
try { |
501 |
q.getFirst(); |
502 |
shouldThrow(); |
503 |
} catch (NoSuchElementException success) {} |
504 |
} |
505 |
|
506 |
/** |
507 |
* getLast() returns last element, or throws NSEE if empty |
508 |
*/ |
509 |
public void testLastElement() { |
510 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
511 |
for (int i = SIZE - 1; i >= 0; --i) { |
512 |
assertEquals(i, q.getLast()); |
513 |
assertEquals(i, q.pollLast()); |
514 |
} |
515 |
try { |
516 |
q.getLast(); |
517 |
shouldThrow(); |
518 |
} catch (NoSuchElementException success) {} |
519 |
assertNull(q.peekLast()); |
520 |
} |
521 |
|
522 |
/** |
523 |
* removeFirst() removes first element, or throws NSEE if empty |
524 |
*/ |
525 |
public void testRemoveFirst() { |
526 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
527 |
for (int i = 0; i < SIZE; ++i) { |
528 |
assertEquals(i, q.removeFirst()); |
529 |
} |
530 |
try { |
531 |
q.removeFirst(); |
532 |
shouldThrow(); |
533 |
} catch (NoSuchElementException success) {} |
534 |
assertNull(q.peekFirst()); |
535 |
} |
536 |
|
537 |
/** |
538 |
* removeLast() removes last element, or throws NSEE if empty |
539 |
*/ |
540 |
public void testRemoveLast() { |
541 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
542 |
for (int i = SIZE - 1; i >= 0; --i) { |
543 |
assertEquals(i, q.removeLast()); |
544 |
} |
545 |
try { |
546 |
q.removeLast(); |
547 |
shouldThrow(); |
548 |
} catch (NoSuchElementException success) {} |
549 |
assertNull(q.peekLast()); |
550 |
} |
551 |
|
552 |
/** |
553 |
* removeFirstOccurrence(x) removes x and returns true if present |
554 |
*/ |
555 |
public void testRemoveFirstOccurrence() { |
556 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
557 |
for (int i = 1; i < SIZE; i += 2) { |
558 |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
559 |
} |
560 |
for (int i = 0; i < SIZE; i += 2) { |
561 |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
562 |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
563 |
} |
564 |
assertTrue(q.isEmpty()); |
565 |
} |
566 |
|
567 |
/** |
568 |
* removeLastOccurrence(x) removes x and returns true if present |
569 |
*/ |
570 |
public void testRemoveLastOccurrence() { |
571 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
572 |
for (int i = 1; i < SIZE; i += 2) { |
573 |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
574 |
} |
575 |
for (int i = 0; i < SIZE; i += 2) { |
576 |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
577 |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
578 |
} |
579 |
assertTrue(q.isEmpty()); |
580 |
} |
581 |
|
582 |
/** |
583 |
* contains(x) reports true when elements added but not yet removed |
584 |
*/ |
585 |
public void testContains() { |
586 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
587 |
for (int i = 0; i < SIZE; ++i) { |
588 |
assertTrue(q.contains(new Integer(i))); |
589 |
q.poll(); |
590 |
assertFalse(q.contains(new Integer(i))); |
591 |
} |
592 |
} |
593 |
|
594 |
/** |
595 |
* clear() removes all elements |
596 |
*/ |
597 |
public void testClear() { |
598 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
599 |
q.clear(); |
600 |
assertTrue(q.isEmpty()); |
601 |
assertEquals(0, q.size()); |
602 |
q.add(one); |
603 |
assertFalse(q.isEmpty()); |
604 |
q.clear(); |
605 |
assertTrue(q.isEmpty()); |
606 |
} |
607 |
|
608 |
/** |
609 |
* containsAll(c) is true when c contains a subset of elements |
610 |
*/ |
611 |
public void testContainsAll() { |
612 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
613 |
ConcurrentLinkedDeque p = new ConcurrentLinkedDeque(); |
614 |
for (int i = 0; i < SIZE; ++i) { |
615 |
assertTrue(q.containsAll(p)); |
616 |
assertFalse(p.containsAll(q)); |
617 |
p.add(new Integer(i)); |
618 |
} |
619 |
assertTrue(p.containsAll(q)); |
620 |
} |
621 |
|
622 |
/** |
623 |
* retainAll(c) retains only those elements of c and reports true if change |
624 |
*/ |
625 |
public void testRetainAll() { |
626 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
627 |
ConcurrentLinkedDeque p = populatedDeque(SIZE); |
628 |
for (int i = 0; i < SIZE; ++i) { |
629 |
boolean changed = q.retainAll(p); |
630 |
if (i == 0) |
631 |
assertFalse(changed); |
632 |
else |
633 |
assertTrue(changed); |
634 |
|
635 |
assertTrue(q.containsAll(p)); |
636 |
assertEquals(SIZE - i, q.size()); |
637 |
p.remove(); |
638 |
} |
639 |
} |
640 |
|
641 |
/** |
642 |
* removeAll(c) removes only those elements of c and reports true if changed |
643 |
*/ |
644 |
public void testRemoveAll() { |
645 |
for (int i = 1; i < SIZE; ++i) { |
646 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
647 |
ConcurrentLinkedDeque p = populatedDeque(i); |
648 |
assertTrue(q.removeAll(p)); |
649 |
assertEquals(SIZE - i, q.size()); |
650 |
for (int j = 0; j < i; ++j) { |
651 |
Integer x = (Integer)(p.remove()); |
652 |
assertFalse(q.contains(x)); |
653 |
} |
654 |
} |
655 |
} |
656 |
|
657 |
/** |
658 |
* toArray() contains all elements in FIFO order |
659 |
*/ |
660 |
public void testToArray() { |
661 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
662 |
Object[] o = q.toArray(); |
663 |
for (int i = 0; i < o.length; i++) |
664 |
assertSame(o[i], q.poll()); |
665 |
} |
666 |
|
667 |
/** |
668 |
* toArray(a) contains all elements in FIFO order |
669 |
*/ |
670 |
public void testToArray2() { |
671 |
ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE); |
672 |
Integer[] ints = new Integer[SIZE]; |
673 |
Integer[] array = q.toArray(ints); |
674 |
assertSame(ints, array); |
675 |
for (int i = 0; i < ints.length; i++) |
676 |
assertSame(ints[i], q.poll()); |
677 |
} |
678 |
|
679 |
/** |
680 |
* toArray(null) throws NullPointerException |
681 |
*/ |
682 |
public void testToArray_NullArg() { |
683 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
684 |
try { |
685 |
q.toArray(null); |
686 |
shouldThrow(); |
687 |
} catch (NullPointerException success) {} |
688 |
} |
689 |
|
690 |
/** |
691 |
* toArray(incompatible array type) throws ArrayStoreException |
692 |
*/ |
693 |
public void testToArray1_BadArg() { |
694 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
695 |
try { |
696 |
q.toArray(new String[10]); |
697 |
shouldThrow(); |
698 |
} catch (ArrayStoreException success) {} |
699 |
} |
700 |
|
701 |
/** |
702 |
* Iterator iterates through all elements |
703 |
*/ |
704 |
public void testIterator() { |
705 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
706 |
Iterator it = q.iterator(); |
707 |
int i; |
708 |
for (i = 0; it.hasNext(); i++) |
709 |
assertTrue(q.contains(it.next())); |
710 |
assertEquals(i, SIZE); |
711 |
assertIteratorExhausted(it); |
712 |
} |
713 |
|
714 |
/** |
715 |
* iterator of empty collection has no elements |
716 |
*/ |
717 |
public void testEmptyIterator() { |
718 |
Deque c = new ConcurrentLinkedDeque(); |
719 |
assertIteratorExhausted(c.iterator()); |
720 |
assertIteratorExhausted(c.descendingIterator()); |
721 |
} |
722 |
|
723 |
/** |
724 |
* Iterator ordering is FIFO |
725 |
*/ |
726 |
public void testIteratorOrdering() { |
727 |
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
728 |
q.add(one); |
729 |
q.add(two); |
730 |
q.add(three); |
731 |
|
732 |
int k = 0; |
733 |
for (Iterator it = q.iterator(); it.hasNext();) { |
734 |
assertEquals(++k, it.next()); |
735 |
} |
736 |
|
737 |
assertEquals(3, k); |
738 |
} |
739 |
|
740 |
/** |
741 |
* Modifications do not cause iterators to fail |
742 |
*/ |
743 |
public void testWeaklyConsistentIteration() { |
744 |
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
745 |
q.add(one); |
746 |
q.add(two); |
747 |
q.add(three); |
748 |
|
749 |
for (Iterator it = q.iterator(); it.hasNext();) { |
750 |
q.remove(); |
751 |
it.next(); |
752 |
} |
753 |
|
754 |
assertEquals("deque should be empty again", 0, q.size()); |
755 |
} |
756 |
|
757 |
/** |
758 |
* iterator.remove() removes current element |
759 |
*/ |
760 |
public void testIteratorRemove() { |
761 |
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
762 |
final Random rng = new Random(); |
763 |
for (int iters = 0; iters < 100; ++iters) { |
764 |
int max = rng.nextInt(5) + 2; |
765 |
int split = rng.nextInt(max - 1) + 1; |
766 |
for (int j = 1; j <= max; ++j) |
767 |
q.add(new Integer(j)); |
768 |
Iterator it = q.iterator(); |
769 |
for (int j = 1; j <= split; ++j) |
770 |
assertEquals(it.next(), new Integer(j)); |
771 |
it.remove(); |
772 |
assertEquals(it.next(), new Integer(split + 1)); |
773 |
for (int j = 1; j <= split; ++j) |
774 |
q.remove(new Integer(j)); |
775 |
it = q.iterator(); |
776 |
for (int j = split + 1; j <= max; ++j) { |
777 |
assertEquals(it.next(), new Integer(j)); |
778 |
it.remove(); |
779 |
} |
780 |
assertFalse(it.hasNext()); |
781 |
assertTrue(q.isEmpty()); |
782 |
} |
783 |
} |
784 |
|
785 |
/** |
786 |
* Descending iterator iterates through all elements |
787 |
*/ |
788 |
public void testDescendingIterator() { |
789 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
790 |
int i = 0; |
791 |
Iterator it = q.descendingIterator(); |
792 |
while (it.hasNext()) { |
793 |
assertTrue(q.contains(it.next())); |
794 |
++i; |
795 |
} |
796 |
assertEquals(i, SIZE); |
797 |
assertFalse(it.hasNext()); |
798 |
try { |
799 |
it.next(); |
800 |
shouldThrow(); |
801 |
} catch (NoSuchElementException success) {} |
802 |
} |
803 |
|
804 |
/** |
805 |
* Descending iterator ordering is reverse FIFO |
806 |
*/ |
807 |
public void testDescendingIteratorOrdering() { |
808 |
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
809 |
for (int iters = 0; iters < 100; ++iters) { |
810 |
q.add(new Integer(3)); |
811 |
q.add(new Integer(2)); |
812 |
q.add(new Integer(1)); |
813 |
int k = 0; |
814 |
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
815 |
assertEquals(++k, it.next()); |
816 |
} |
817 |
|
818 |
assertEquals(3, k); |
819 |
q.remove(); |
820 |
q.remove(); |
821 |
q.remove(); |
822 |
} |
823 |
} |
824 |
|
825 |
/** |
826 |
* descendingIterator.remove() removes current element |
827 |
*/ |
828 |
public void testDescendingIteratorRemove() { |
829 |
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
830 |
final Random rng = new Random(); |
831 |
for (int iters = 0; iters < 100; ++iters) { |
832 |
int max = rng.nextInt(5) + 2; |
833 |
int split = rng.nextInt(max - 1) + 1; |
834 |
for (int j = max; j >= 1; --j) |
835 |
q.add(new Integer(j)); |
836 |
Iterator it = q.descendingIterator(); |
837 |
for (int j = 1; j <= split; ++j) |
838 |
assertEquals(it.next(), new Integer(j)); |
839 |
it.remove(); |
840 |
assertEquals(it.next(), new Integer(split + 1)); |
841 |
for (int j = 1; j <= split; ++j) |
842 |
q.remove(new Integer(j)); |
843 |
it = q.descendingIterator(); |
844 |
for (int j = split + 1; j <= max; ++j) { |
845 |
assertEquals(it.next(), new Integer(j)); |
846 |
it.remove(); |
847 |
} |
848 |
assertFalse(it.hasNext()); |
849 |
assertTrue(q.isEmpty()); |
850 |
} |
851 |
} |
852 |
|
853 |
/** |
854 |
* toString() contains toStrings of elements |
855 |
*/ |
856 |
public void testToString() { |
857 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
858 |
String s = q.toString(); |
859 |
for (int i = 0; i < SIZE; ++i) { |
860 |
assertTrue(s.contains(String.valueOf(i))); |
861 |
} |
862 |
} |
863 |
|
864 |
/** |
865 |
* A deserialized/reserialized deque has same elements in same order |
866 |
*/ |
867 |
public void testSerialization() throws Exception { |
868 |
Queue x = populatedDeque(SIZE); |
869 |
Queue y = serialClone(x); |
870 |
|
871 |
assertNotSame(x, y); |
872 |
assertEquals(x.size(), y.size()); |
873 |
assertEquals(x.toString(), y.toString()); |
874 |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
875 |
while (!x.isEmpty()) { |
876 |
assertFalse(y.isEmpty()); |
877 |
assertEquals(x.remove(), y.remove()); |
878 |
} |
879 |
assertTrue(y.isEmpty()); |
880 |
} |
881 |
|
882 |
/** |
883 |
* contains(null) always return false. |
884 |
* remove(null) always throws NullPointerException. |
885 |
*/ |
886 |
public void testNeverContainsNull() { |
887 |
Deque<?>[] qs = { |
888 |
new ConcurrentLinkedDeque<Object>(), |
889 |
populatedDeque(2), |
890 |
}; |
891 |
|
892 |
for (Deque<?> q : qs) { |
893 |
assertFalse(q.contains(null)); |
894 |
try { |
895 |
assertFalse(q.remove(null)); |
896 |
shouldThrow(); |
897 |
} catch (NullPointerException success) {} |
898 |
try { |
899 |
assertFalse(q.removeFirstOccurrence(null)); |
900 |
shouldThrow(); |
901 |
} catch (NullPointerException success) {} |
902 |
try { |
903 |
assertFalse(q.removeLastOccurrence(null)); |
904 |
shouldThrow(); |
905 |
} catch (NullPointerException success) {} |
906 |
} |
907 |
} |
908 |
} |