ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedDequeTest.java
Revision: 1.8
Committed: Fri May 27 19:47:55 2011 UTC (12 years, 11 months ago) by jsr166
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
indexOf => contains

File Contents

# Content
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.*;
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<Integer> populatedDeque(int n) {
29 ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>();
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.contains(i));
435 assertTrue(q.remove(i));
436 assertFalse(q.contains(i));
437 assertTrue(q.contains(i-1));
438 }
439 for (int i = 0; i < SIZE; i+=2) {
440 assertTrue(q.contains(i));
441 assertTrue(q.remove(i));
442 assertFalse(q.contains(i));
443 assertFalse(q.remove(i+1));
444 assertFalse(q.contains(i+1));
445 }
446 assertTrue(q.isEmpty());
447 }
448
449 /**
450 * peekFirst() returns next element, or null if empty
451 */
452 public void testPeekFirst() {
453 ConcurrentLinkedDeque q = populatedDeque(SIZE);
454 for (int i = 0; i < SIZE; ++i) {
455 assertEquals(i, q.peekFirst());
456 assertEquals(i, q.pollFirst());
457 assertTrue(q.peekFirst() == null ||
458 !q.peekFirst().equals(i));
459 }
460 assertNull(q.peekFirst());
461 }
462
463 /**
464 * peekLast() returns next element, or null if empty
465 */
466 public void testPeekLast() {
467 ConcurrentLinkedDeque q = populatedDeque(SIZE);
468 for (int i = SIZE-1; i >= 0; --i) {
469 assertEquals(i, q.peekLast());
470 assertEquals(i, q.pollLast());
471 assertTrue(q.peekLast() == null ||
472 !q.peekLast().equals(i));
473 }
474 assertNull(q.peekLast());
475 }
476
477 /**
478 * getFirst() returns first element, or throws NSEE if empty
479 */
480 public void testFirstElement() {
481 ConcurrentLinkedDeque q = populatedDeque(SIZE);
482 for (int i = 0; i < SIZE; ++i) {
483 assertEquals(i, q.getFirst());
484 assertEquals(i, q.pollFirst());
485 }
486 try {
487 q.getFirst();
488 shouldThrow();
489 } catch (NoSuchElementException success) {}
490 }
491
492 /**
493 * getLast() returns last element, or throws NSEE if empty
494 */
495 public void testLastElement() {
496 ConcurrentLinkedDeque q = populatedDeque(SIZE);
497 for (int i = SIZE-1; i >= 0; --i) {
498 assertEquals(i, q.getLast());
499 assertEquals(i, q.pollLast());
500 }
501 try {
502 q.getLast();
503 shouldThrow();
504 } catch (NoSuchElementException success) {}
505 assertNull(q.peekLast());
506 }
507
508 /**
509 * removeFirst() removes first element, or throws NSEE if empty
510 */
511 public void testRemoveFirst() {
512 ConcurrentLinkedDeque q = populatedDeque(SIZE);
513 for (int i = 0; i < SIZE; ++i) {
514 assertEquals(i, q.removeFirst());
515 }
516 try {
517 q.removeFirst();
518 shouldThrow();
519 } catch (NoSuchElementException success) {}
520 assertNull(q.peekFirst());
521 }
522
523 /**
524 * removeLast() removes last element, or throws NSEE if empty
525 */
526 public void testRemoveLast() {
527 ConcurrentLinkedDeque q = populatedDeque(SIZE);
528 for (int i = SIZE - 1; i >= 0; --i) {
529 assertEquals(i, q.removeLast());
530 }
531 try {
532 q.removeLast();
533 shouldThrow();
534 } catch (NoSuchElementException success) {}
535 assertNull(q.peekLast());
536 }
537
538 /**
539 * removeFirstOccurrence(x) removes x and returns true if present
540 */
541 public void testRemoveFirstOccurrence() {
542 ConcurrentLinkedDeque q = populatedDeque(SIZE);
543 for (int i = 1; i < SIZE; i+=2) {
544 assertTrue(q.removeFirstOccurrence(new Integer(i)));
545 }
546 for (int i = 0; i < SIZE; i+=2) {
547 assertTrue(q.removeFirstOccurrence(new Integer(i)));
548 assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
549 }
550 assertTrue(q.isEmpty());
551 }
552
553 /**
554 * removeLastOccurrence(x) removes x and returns true if present
555 */
556 public void testRemoveLastOccurrence() {
557 ConcurrentLinkedDeque q = populatedDeque(SIZE);
558 for (int i = 1; i < SIZE; i+=2) {
559 assertTrue(q.removeLastOccurrence(new Integer(i)));
560 }
561 for (int i = 0; i < SIZE; i+=2) {
562 assertTrue(q.removeLastOccurrence(new Integer(i)));
563 assertFalse(q.removeLastOccurrence(new Integer(i+1)));
564 }
565 assertTrue(q.isEmpty());
566 }
567
568 /**
569 * contains(x) reports true when elements added but not yet removed
570 */
571 public void testContains() {
572 ConcurrentLinkedDeque q = populatedDeque(SIZE);
573 for (int i = 0; i < SIZE; ++i) {
574 assertTrue(q.contains(new Integer(i)));
575 q.poll();
576 assertFalse(q.contains(new Integer(i)));
577 }
578 }
579
580 /**
581 * clear() removes all elements
582 */
583 public void testClear() {
584 ConcurrentLinkedDeque q = populatedDeque(SIZE);
585 q.clear();
586 assertTrue(q.isEmpty());
587 assertEquals(0, q.size());
588 q.add(one);
589 assertFalse(q.isEmpty());
590 q.clear();
591 assertTrue(q.isEmpty());
592 }
593
594 /**
595 * containsAll(c) is true when c contains a subset of elements
596 */
597 public void testContainsAll() {
598 ConcurrentLinkedDeque q = populatedDeque(SIZE);
599 ConcurrentLinkedDeque p = new ConcurrentLinkedDeque();
600 for (int i = 0; i < SIZE; ++i) {
601 assertTrue(q.containsAll(p));
602 assertFalse(p.containsAll(q));
603 p.add(new Integer(i));
604 }
605 assertTrue(p.containsAll(q));
606 }
607
608 /**
609 * retainAll(c) retains only those elements of c and reports true if change
610 */
611 public void testRetainAll() {
612 ConcurrentLinkedDeque q = populatedDeque(SIZE);
613 ConcurrentLinkedDeque p = populatedDeque(SIZE);
614 for (int i = 0; i < SIZE; ++i) {
615 boolean changed = q.retainAll(p);
616 if (i == 0)
617 assertFalse(changed);
618 else
619 assertTrue(changed);
620
621 assertTrue(q.containsAll(p));
622 assertEquals(SIZE-i, q.size());
623 p.remove();
624 }
625 }
626
627 /**
628 * removeAll(c) removes only those elements of c and reports true if changed
629 */
630 public void testRemoveAll() {
631 for (int i = 1; i < SIZE; ++i) {
632 ConcurrentLinkedDeque q = populatedDeque(SIZE);
633 ConcurrentLinkedDeque p = populatedDeque(i);
634 assertTrue(q.removeAll(p));
635 assertEquals(SIZE-i, q.size());
636 for (int j = 0; j < i; ++j) {
637 Integer I = (Integer)(p.remove());
638 assertFalse(q.contains(I));
639 }
640 }
641 }
642
643 /**
644 * toArray() contains all elements in FIFO order
645 */
646 public void testToArray() {
647 ConcurrentLinkedDeque q = populatedDeque(SIZE);
648 Object[] o = q.toArray();
649 for (int i = 0; i < o.length; i++)
650 assertSame(o[i], q.poll());
651 }
652
653 /**
654 * toArray(a) contains all elements in FIFO order
655 */
656 public void testToArray2() {
657 ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE);
658 Integer[] ints = new Integer[SIZE];
659 Integer[] array = q.toArray(ints);
660 assertSame(ints, array);
661 for (int i = 0; i < ints.length; i++)
662 assertSame(ints[i], q.poll());
663 }
664
665 /**
666 * toArray(null) throws NullPointerException
667 */
668 public void testToArray_NullArg() {
669 ConcurrentLinkedDeque q = populatedDeque(SIZE);
670 try {
671 q.toArray(null);
672 shouldThrow();
673 } catch (NullPointerException success) {}
674 }
675
676 /**
677 * toArray(incompatible array type) throws ArrayStoreException
678 */
679 public void testToArray1_BadArg() {
680 ConcurrentLinkedDeque q = populatedDeque(SIZE);
681 try {
682 q.toArray(new String[10]);
683 shouldThrow();
684 } catch (ArrayStoreException success) {}
685 }
686
687 /**
688 * Iterator iterates through all elements
689 */
690 public void testIterator() {
691 ConcurrentLinkedDeque q = populatedDeque(SIZE);
692 int i = 0;
693 Iterator it = q.iterator();
694 while (it.hasNext()) {
695 assertTrue(q.contains(it.next()));
696 ++i;
697 }
698 assertEquals(i, SIZE);
699 }
700
701 /**
702 * Iterator ordering is FIFO
703 */
704 public void testIteratorOrdering() {
705 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
706 q.add(one);
707 q.add(two);
708 q.add(three);
709
710 int k = 0;
711 for (Iterator it = q.iterator(); it.hasNext();) {
712 assertEquals(++k, it.next());
713 }
714
715 assertEquals(3, k);
716 }
717
718 /**
719 * Modifications do not cause iterators to fail
720 */
721 public void testWeaklyConsistentIteration() {
722 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
723 q.add(one);
724 q.add(two);
725 q.add(three);
726
727 for (Iterator it = q.iterator(); it.hasNext();) {
728 q.remove();
729 it.next();
730 }
731
732 assertEquals("deque should be empty again", 0, q.size());
733 }
734
735 /**
736 * iterator.remove() removes current element
737 */
738 public void testIteratorRemove() {
739 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
740 final Random rng = new Random();
741 for (int iters = 0; iters < 100; ++iters) {
742 int max = rng.nextInt(5) + 2;
743 int split = rng.nextInt(max-1) + 1;
744 for (int j = 1; j <= max; ++j)
745 q.add(new Integer(j));
746 Iterator it = q.iterator();
747 for (int j = 1; j <= split; ++j)
748 assertEquals(it.next(), new Integer(j));
749 it.remove();
750 assertEquals(it.next(), new Integer(split+1));
751 for (int j = 1; j <= split; ++j)
752 q.remove(new Integer(j));
753 it = q.iterator();
754 for (int j = split+1; j <= max; ++j) {
755 assertEquals(it.next(), new Integer(j));
756 it.remove();
757 }
758 assertFalse(it.hasNext());
759 assertTrue(q.isEmpty());
760 }
761 }
762
763 /**
764 * Descending iterator iterates through all elements
765 */
766 public void testDescendingIterator() {
767 ConcurrentLinkedDeque q = populatedDeque(SIZE);
768 int i = 0;
769 Iterator it = q.descendingIterator();
770 while (it.hasNext()) {
771 assertTrue(q.contains(it.next()));
772 ++i;
773 }
774 assertEquals(i, SIZE);
775 assertFalse(it.hasNext());
776 try {
777 it.next();
778 shouldThrow();
779 } catch (NoSuchElementException success) {}
780 }
781
782 /**
783 * Descending iterator ordering is reverse FIFO
784 */
785 public void testDescendingIteratorOrdering() {
786 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
787 for (int iters = 0; iters < 100; ++iters) {
788 q.add(new Integer(3));
789 q.add(new Integer(2));
790 q.add(new Integer(1));
791 int k = 0;
792 for (Iterator it = q.descendingIterator(); it.hasNext();) {
793 assertEquals(++k, it.next());
794 }
795
796 assertEquals(3, k);
797 q.remove();
798 q.remove();
799 q.remove();
800 }
801 }
802
803 /**
804 * descendingIterator.remove() removes current element
805 */
806 public void testDescendingIteratorRemove() {
807 final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
808 final Random rng = new Random();
809 for (int iters = 0; iters < 100; ++iters) {
810 int max = rng.nextInt(5) + 2;
811 int split = rng.nextInt(max-1) + 1;
812 for (int j = max; j >= 1; --j)
813 q.add(new Integer(j));
814 Iterator it = q.descendingIterator();
815 for (int j = 1; j <= split; ++j)
816 assertEquals(it.next(), new Integer(j));
817 it.remove();
818 assertEquals(it.next(), new Integer(split+1));
819 for (int j = 1; j <= split; ++j)
820 q.remove(new Integer(j));
821 it = q.descendingIterator();
822 for (int j = split+1; j <= max; ++j) {
823 assertEquals(it.next(), new Integer(j));
824 it.remove();
825 }
826 assertFalse(it.hasNext());
827 assertTrue(q.isEmpty());
828 }
829 }
830
831 /**
832 * toString() contains toStrings of elements
833 */
834 public void testToString() {
835 ConcurrentLinkedDeque q = populatedDeque(SIZE);
836 String s = q.toString();
837 for (int i = 0; i < SIZE; ++i) {
838 assertTrue(s.contains(String.valueOf(i)));
839 }
840 }
841
842 /**
843 * A deserialized serialized deque has same elements in same order
844 */
845 public void testSerialization() throws Exception {
846 ConcurrentLinkedDeque q = populatedDeque(SIZE);
847 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
848 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
849 out.writeObject(q);
850 out.close();
851
852 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
853 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
854 ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject();
855 assertEquals(q.size(), r.size());
856 while (!q.isEmpty())
857 assertEquals(q.remove(), r.remove());
858 }
859
860 }