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