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