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