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