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