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