ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedDequeTest.java
Revision: 1.37
Committed: Tue Jan 26 13:33:05 2021 UTC (3 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.36: +172 -181 lines
Log Message:
Replace Integer with Item class

File Contents

# User Rev Content
1 jsr166 1.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 jsr166 1.7 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.1 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7     */
8    
9 jsr166 1.9 import java.util.Arrays;
10     import java.util.Collection;
11 jsr166 1.13 import java.util.Deque;
12 jsr166 1.9 import java.util.Iterator;
13     import java.util.NoSuchElementException;
14     import java.util.Queue;
15     import java.util.Random;
16 jsr166 1.30 import java.util.concurrent.CompletableFuture;
17 jsr166 1.9 import java.util.concurrent.ConcurrentLinkedDeque;
18 jsr166 1.30 import java.util.concurrent.ThreadLocalRandom;
19     import java.util.concurrent.atomic.LongAdder;
20 jsr166 1.1
21 jsr166 1.14 import junit.framework.Test;
22    
23 jsr166 1.1 public class ConcurrentLinkedDequeTest extends JSR166TestCase {
24    
25     public static void main(String[] args) {
26 jsr166 1.19 main(suite(), args);
27 jsr166 1.1 }
28    
29     public static Test suite() {
30 jsr166 1.23 class Implementation implements CollectionImplementation {
31     public Class<?> klazz() { return ConcurrentLinkedDeque.class; }
32     public Collection emptyCollection() { return new ConcurrentLinkedDeque(); }
33 dl 1.37 public Object makeElement(int i) { return JSR166TestCase.itemFor(i); }
34 jsr166 1.23 public boolean isConcurrent() { return true; }
35     public boolean permitsNulls() { return false; }
36     }
37     return newTestSuite(ConcurrentLinkedDequeTest.class,
38     CollectionTest.testSuite(new Implementation()));
39 jsr166 1.1 }
40    
41     /**
42 jsr166 1.11 * Returns a new deque of given size containing consecutive
43 dl 1.37 * Items 0 ... n - 1.
44 jsr166 1.1 */
45 dl 1.37 private static ConcurrentLinkedDeque<Item> populatedDeque(int n) {
46     ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<>();
47 jsr166 1.1 assertTrue(q.isEmpty());
48     for (int i = 0; i < n; ++i)
49 dl 1.37 mustOffer(q, i);
50 jsr166 1.1 assertFalse(q.isEmpty());
51 dl 1.37 mustEqual(n, q.size());
52     mustEqual(0, q.peekFirst());
53     mustEqual((n - 1), q.peekLast());
54 jsr166 1.1 return q;
55     }
56    
57     /**
58     * new deque is empty
59     */
60     public void testConstructor1() {
61 dl 1.37 assertTrue(new ConcurrentLinkedDeque<Item>().isEmpty());
62     mustEqual(0, new ConcurrentLinkedDeque<Item>().size());
63 jsr166 1.1 }
64    
65     /**
66     * Initializing from null Collection throws NPE
67     */
68     public void testConstructor3() {
69     try {
70 dl 1.37 new ConcurrentLinkedDeque<Item>((Collection<Item>)null);
71 jsr166 1.1 shouldThrow();
72     } catch (NullPointerException success) {}
73     }
74    
75     /**
76     * Initializing from Collection of null elements throws NPE
77     */
78     public void testConstructor4() {
79     try {
80 dl 1.37 new ConcurrentLinkedDeque<Item>(Arrays.asList(new Item[SIZE]));
81 jsr166 1.1 shouldThrow();
82     } catch (NullPointerException success) {}
83     }
84    
85     /**
86     * Initializing from Collection with some null elements throws NPE
87     */
88     public void testConstructor5() {
89 dl 1.37 Item[] items = new Item[2]; items[0] = zero;
90 jsr166 1.1 try {
91 dl 1.37 new ConcurrentLinkedDeque<Item>(Arrays.asList(items));
92 jsr166 1.1 shouldThrow();
93     } catch (NullPointerException success) {}
94     }
95    
96     /**
97     * Deque contains all elements of collection used to initialize
98     */
99     public void testConstructor6() {
100 dl 1.37 Item[] items = defaultItems;
101     ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>(Arrays.asList(items));
102 jsr166 1.1 for (int i = 0; i < SIZE; ++i)
103 dl 1.37 mustEqual(items[i], q.poll());
104 jsr166 1.1 }
105    
106     /**
107     * isEmpty is true before add, false after
108     */
109     public void testEmpty() {
110 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
111 jsr166 1.1 assertTrue(q.isEmpty());
112     q.add(one);
113     assertFalse(q.isEmpty());
114     q.add(two);
115     q.remove();
116     q.remove();
117     assertTrue(q.isEmpty());
118     }
119    
120     /**
121     * size() changes when elements added and removed
122     */
123     public void testSize() {
124 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
125 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
126 dl 1.37 mustEqual(SIZE - i, q.size());
127 jsr166 1.1 q.remove();
128     }
129     for (int i = 0; i < SIZE; ++i) {
130 dl 1.37 mustEqual(i, q.size());
131     mustAdd(q, i);
132 jsr166 1.1 }
133     }
134    
135     /**
136     * push(null) throws NPE
137     */
138     public void testPushNull() {
139 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
140 jsr166 1.1 try {
141     q.push(null);
142     shouldThrow();
143     } catch (NullPointerException success) {}
144     }
145    
146     /**
147     * peekFirst() returns element inserted with push
148     */
149     public void testPush() {
150 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(3);
151 jsr166 1.1 q.pollLast();
152     q.push(four);
153     assertSame(four, q.peekFirst());
154     }
155    
156     /**
157     * pop() removes first element, or throws NSEE if empty
158     */
159     public void testPop() {
160 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
161 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
162 dl 1.37 mustEqual(i, q.pop());
163 jsr166 1.1 }
164     try {
165     q.pop();
166     shouldThrow();
167     } catch (NoSuchElementException success) {}
168     }
169    
170     /**
171     * offer(null) throws NPE
172     */
173     public void testOfferNull() {
174 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
175 jsr166 1.1 try {
176     q.offer(null);
177     shouldThrow();
178     } catch (NullPointerException success) {}
179     }
180    
181     /**
182     * offerFirst(null) throws NPE
183     */
184     public void testOfferFirstNull() {
185 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
186 jsr166 1.1 try {
187     q.offerFirst(null);
188     shouldThrow();
189     } catch (NullPointerException success) {}
190     }
191    
192     /**
193     * offerLast(null) throws NPE
194     */
195     public void testOfferLastNull() {
196 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
197 jsr166 1.1 try {
198     q.offerLast(null);
199     shouldThrow();
200     } catch (NullPointerException success) {}
201     }
202    
203     /**
204     * offer(x) succeeds
205     */
206     public void testOffer() {
207 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
208 jsr166 1.1 assertTrue(q.offer(zero));
209     assertTrue(q.offer(one));
210     assertSame(zero, q.peekFirst());
211     assertSame(one, q.peekLast());
212     }
213    
214     /**
215     * offerFirst(x) succeeds
216     */
217     public void testOfferFirst() {
218 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
219 jsr166 1.1 assertTrue(q.offerFirst(zero));
220     assertTrue(q.offerFirst(one));
221     assertSame(one, q.peekFirst());
222     assertSame(zero, q.peekLast());
223     }
224    
225     /**
226     * offerLast(x) succeeds
227     */
228     public void testOfferLast() {
229 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
230 jsr166 1.1 assertTrue(q.offerLast(zero));
231     assertTrue(q.offerLast(one));
232     assertSame(zero, q.peekFirst());
233     assertSame(one, q.peekLast());
234     }
235    
236     /**
237     * add(null) throws NPE
238     */
239     public void testAddNull() {
240 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
241 jsr166 1.1 try {
242     q.add(null);
243     shouldThrow();
244     } catch (NullPointerException success) {}
245     }
246    
247     /**
248     * addFirst(null) throws NPE
249     */
250     public void testAddFirstNull() {
251 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
252 jsr166 1.1 try {
253     q.addFirst(null);
254     shouldThrow();
255     } catch (NullPointerException success) {}
256     }
257    
258     /**
259     * addLast(null) throws NPE
260     */
261     public void testAddLastNull() {
262 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
263 jsr166 1.1 try {
264     q.addLast(null);
265     shouldThrow();
266     } catch (NullPointerException success) {}
267     }
268    
269     /**
270     * add(x) succeeds
271     */
272     public void testAdd() {
273 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
274 jsr166 1.1 assertTrue(q.add(zero));
275     assertTrue(q.add(one));
276     assertSame(zero, q.peekFirst());
277     assertSame(one, q.peekLast());
278     }
279    
280     /**
281     * addFirst(x) succeeds
282     */
283     public void testAddFirst() {
284 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
285 jsr166 1.1 q.addFirst(zero);
286     q.addFirst(one);
287     assertSame(one, q.peekFirst());
288     assertSame(zero, q.peekLast());
289     }
290    
291     /**
292     * addLast(x) succeeds
293     */
294     public void testAddLast() {
295 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
296 jsr166 1.1 q.addLast(zero);
297     q.addLast(one);
298     assertSame(zero, q.peekFirst());
299     assertSame(one, q.peekLast());
300     }
301    
302     /**
303     * addAll(null) throws NPE
304     */
305     public void testAddAll1() {
306 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
307 jsr166 1.1 try {
308     q.addAll(null);
309     shouldThrow();
310     } catch (NullPointerException success) {}
311     }
312    
313     /**
314 jsr166 1.28 * addAll(this) throws IllegalArgumentException
315 jsr166 1.1 */
316     public void testAddAllSelf() {
317 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
318 jsr166 1.1 try {
319     q.addAll(q);
320     shouldThrow();
321     } catch (IllegalArgumentException success) {}
322     }
323    
324     /**
325     * addAll of a collection with null elements throws NPE
326     */
327     public void testAddAll2() {
328 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
329 jsr166 1.1 try {
330 dl 1.37 q.addAll(Arrays.asList(new Item[SIZE]));
331 jsr166 1.1 shouldThrow();
332     } catch (NullPointerException success) {}
333     }
334    
335     /**
336     * addAll of a collection with any null elements throws NPE after
337     * possibly adding some elements
338     */
339     public void testAddAll3() {
340 dl 1.37 ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
341     Item[] items = new Item[2]; items[0] = zero;
342 jsr166 1.1 try {
343 dl 1.37 q.addAll(Arrays.asList(items));
344 jsr166 1.1 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 dl 1.37 Item[] empty = new Item[0];
353     Item[] items = defaultItems;
354     ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
355 jsr166 1.1 assertFalse(q.addAll(Arrays.asList(empty)));
356 dl 1.37 assertTrue(q.addAll(Arrays.asList(items)));
357 jsr166 1.1 for (int i = 0; i < SIZE; ++i)
358 dl 1.37 mustEqual(items[i], q.poll());
359 jsr166 1.1 }
360    
361     /**
362     * pollFirst() succeeds unless empty
363     */
364     public void testPollFirst() {
365 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
366 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
367 dl 1.37 mustEqual(i, q.pollFirst());
368 jsr166 1.1 }
369     assertNull(q.pollFirst());
370     }
371    
372     /**
373     * pollLast() succeeds unless empty
374     */
375     public void testPollLast() {
376 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
377 jsr166 1.21 for (int i = SIZE - 1; i >= 0; --i) {
378 dl 1.37 mustEqual(i, q.pollLast());
379 jsr166 1.1 }
380     assertNull(q.pollLast());
381     }
382    
383     /**
384     * poll() succeeds unless empty
385     */
386     public void testPoll() {
387 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
388 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
389 dl 1.37 mustEqual(i, q.poll());
390 jsr166 1.1 }
391     assertNull(q.poll());
392     }
393    
394     /**
395     * peek() returns next element, or null if empty
396     */
397     public void testPeek() {
398 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
399 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
400 dl 1.37 mustEqual(i, q.peek());
401     mustEqual(i, q.poll());
402 jsr166 1.1 assertTrue(q.peek() == null ||
403     !q.peek().equals(i));
404     }
405     assertNull(q.peek());
406     }
407    
408     /**
409     * element() returns first element, or throws NSEE if empty
410     */
411     public void testElement() {
412 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
413 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
414 dl 1.37 mustEqual(i, q.element());
415     mustEqual(i, q.poll());
416 jsr166 1.1 }
417     try {
418     q.element();
419     shouldThrow();
420     } catch (NoSuchElementException success) {}
421     }
422    
423     /**
424     * remove() removes next element, or throws NSEE if empty
425     */
426     public void testRemove() {
427 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
428 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
429 dl 1.37 mustEqual(i, q.remove());
430 jsr166 1.1 }
431     try {
432     q.remove();
433     shouldThrow();
434     } catch (NoSuchElementException success) {}
435     }
436    
437     /**
438     * remove(x) removes x and returns true if present
439     */
440     public void testRemoveElement() {
441 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
442 jsr166 1.15 for (int i = 1; i < SIZE; i += 2) {
443 dl 1.37 mustContain(q, i);
444     mustRemove(q, i);
445     mustNotContain(q, i);
446     mustContain(q, i - 1);
447 jsr166 1.1 }
448 jsr166 1.15 for (int i = 0; i < SIZE; i += 2) {
449 dl 1.37 mustContain(q, i);
450     mustRemove(q, i);
451     mustNotContain(q, i);
452     mustNotRemove(q, i + 1);
453     mustNotContain(q, i + 1);
454 jsr166 1.1 }
455     assertTrue(q.isEmpty());
456     }
457    
458     /**
459     * peekFirst() returns next element, or null if empty
460     */
461     public void testPeekFirst() {
462 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
463 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
464 dl 1.37 mustEqual(i, q.peekFirst());
465     mustEqual(i, q.pollFirst());
466 jsr166 1.1 assertTrue(q.peekFirst() == null ||
467     !q.peekFirst().equals(i));
468     }
469     assertNull(q.peekFirst());
470     }
471    
472     /**
473     * peekLast() returns next element, or null if empty
474     */
475     public void testPeekLast() {
476 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
477 jsr166 1.21 for (int i = SIZE - 1; i >= 0; --i) {
478 dl 1.37 mustEqual(i, q.peekLast());
479     mustEqual(i, q.pollLast());
480 jsr166 1.1 assertTrue(q.peekLast() == null ||
481     !q.peekLast().equals(i));
482     }
483     assertNull(q.peekLast());
484     }
485    
486     /**
487     * getFirst() returns first element, or throws NSEE if empty
488     */
489     public void testFirstElement() {
490 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
491 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
492 dl 1.37 mustEqual(i, q.getFirst());
493     mustEqual(i, q.pollFirst());
494 jsr166 1.1 }
495     try {
496     q.getFirst();
497     shouldThrow();
498     } catch (NoSuchElementException success) {}
499     }
500    
501     /**
502     * getLast() returns last element, or throws NSEE if empty
503     */
504     public void testLastElement() {
505 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
506 jsr166 1.21 for (int i = SIZE - 1; i >= 0; --i) {
507 dl 1.37 mustEqual(i, q.getLast());
508     mustEqual(i, q.pollLast());
509 jsr166 1.1 }
510     try {
511     q.getLast();
512     shouldThrow();
513     } catch (NoSuchElementException success) {}
514     assertNull(q.peekLast());
515     }
516    
517     /**
518     * removeFirst() removes first element, or throws NSEE if empty
519     */
520     public void testRemoveFirst() {
521 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
522 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
523 dl 1.37 mustEqual(i, q.removeFirst());
524 jsr166 1.1 }
525     try {
526     q.removeFirst();
527     shouldThrow();
528     } catch (NoSuchElementException success) {}
529     assertNull(q.peekFirst());
530     }
531    
532     /**
533     * removeLast() removes last element, or throws NSEE if empty
534     */
535     public void testRemoveLast() {
536 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
537 jsr166 1.1 for (int i = SIZE - 1; i >= 0; --i) {
538 dl 1.37 mustEqual(i, q.removeLast());
539 jsr166 1.1 }
540     try {
541     q.removeLast();
542     shouldThrow();
543     } catch (NoSuchElementException success) {}
544     assertNull(q.peekLast());
545     }
546    
547     /**
548     * removeFirstOccurrence(x) removes x and returns true if present
549     */
550     public void testRemoveFirstOccurrence() {
551 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
552 jsr166 1.15 for (int i = 1; i < SIZE; i += 2) {
553 dl 1.37 assertTrue(q.removeFirstOccurrence(itemFor(i)));
554 jsr166 1.1 }
555 jsr166 1.15 for (int i = 0; i < SIZE; i += 2) {
556 dl 1.37 assertTrue(q.removeFirstOccurrence(itemFor(i)));
557     assertFalse(q.removeFirstOccurrence(itemFor(i + 1)));
558 jsr166 1.1 }
559     assertTrue(q.isEmpty());
560     }
561    
562     /**
563     * removeLastOccurrence(x) removes x and returns true if present
564     */
565     public void testRemoveLastOccurrence() {
566 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
567 jsr166 1.15 for (int i = 1; i < SIZE; i += 2) {
568 dl 1.37 assertTrue(q.removeLastOccurrence(itemFor(i)));
569 jsr166 1.1 }
570 jsr166 1.15 for (int i = 0; i < SIZE; i += 2) {
571 dl 1.37 assertTrue(q.removeLastOccurrence(itemFor(i)));
572     assertFalse(q.removeLastOccurrence(itemFor(i + 1)));
573 jsr166 1.1 }
574     assertTrue(q.isEmpty());
575     }
576    
577     /**
578     * contains(x) reports true when elements added but not yet removed
579     */
580     public void testContains() {
581 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
582 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
583 dl 1.37 mustContain(q, i);
584 jsr166 1.1 q.poll();
585 dl 1.37 mustNotContain(q, i);
586 jsr166 1.1 }
587     }
588    
589     /**
590     * clear() removes all elements
591     */
592     public void testClear() {
593 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
594 jsr166 1.1 q.clear();
595     assertTrue(q.isEmpty());
596 dl 1.37 mustEqual(0, q.size());
597 jsr166 1.1 q.add(one);
598     assertFalse(q.isEmpty());
599     q.clear();
600     assertTrue(q.isEmpty());
601     }
602    
603     /**
604     * containsAll(c) is true when c contains a subset of elements
605     */
606     public void testContainsAll() {
607 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
608     ConcurrentLinkedDeque<Item> p = new ConcurrentLinkedDeque<Item>();
609 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
610     assertTrue(q.containsAll(p));
611     assertFalse(p.containsAll(q));
612 dl 1.37 mustAdd(p, i);
613 jsr166 1.1 }
614     assertTrue(p.containsAll(q));
615     }
616    
617     /**
618     * retainAll(c) retains only those elements of c and reports true if change
619     */
620     public void testRetainAll() {
621 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
622     ConcurrentLinkedDeque<Item> p = populatedDeque(SIZE);
623 jsr166 1.1 for (int i = 0; i < SIZE; ++i) {
624     boolean changed = q.retainAll(p);
625     if (i == 0)
626     assertFalse(changed);
627     else
628     assertTrue(changed);
629    
630     assertTrue(q.containsAll(p));
631 dl 1.37 mustEqual(SIZE - i, q.size());
632 jsr166 1.1 p.remove();
633     }
634     }
635    
636     /**
637     * removeAll(c) removes only those elements of c and reports true if changed
638     */
639     public void testRemoveAll() {
640     for (int i = 1; i < SIZE; ++i) {
641 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
642     ConcurrentLinkedDeque<Item> p = populatedDeque(i);
643 jsr166 1.1 assertTrue(q.removeAll(p));
644 dl 1.37 mustEqual(SIZE - i, q.size());
645 jsr166 1.1 for (int j = 0; j < i; ++j) {
646 dl 1.37 mustNotContain(q, p.remove());
647 jsr166 1.1 }
648     }
649     }
650    
651     /**
652 jsr166 1.4 * toArray() contains all elements in FIFO order
653 jsr166 1.1 */
654     public void testToArray() {
655 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
656 jsr166 1.32 Object[] a = q.toArray();
657     assertSame(Object[].class, a.getClass());
658     for (Object o : a)
659     assertSame(o, q.poll());
660     assertTrue(q.isEmpty());
661 jsr166 1.1 }
662    
663     /**
664 jsr166 1.4 * toArray(a) contains all elements in FIFO order
665 jsr166 1.1 */
666     public void testToArray2() {
667 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
668     Item[] items = new Item[SIZE];
669     Item[] array = q.toArray(items);
670     assertSame(items, array);
671     for (Item o : items)
672 jsr166 1.32 assertSame(o, q.poll());
673     assertTrue(q.isEmpty());
674 jsr166 1.1 }
675    
676     /**
677 jsr166 1.3 * toArray(null) throws NullPointerException
678 jsr166 1.1 */
679 jsr166 1.3 public void testToArray_NullArg() {
680 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
681 jsr166 1.1 try {
682 jsr166 1.33 q.toArray((Object[])null);
683 jsr166 1.1 shouldThrow();
684     } catch (NullPointerException success) {}
685     }
686    
687     /**
688 jsr166 1.2 * toArray(incompatible array type) throws ArrayStoreException
689 jsr166 1.1 */
690     public void testToArray1_BadArg() {
691 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
692 jsr166 1.1 try {
693 jsr166 1.2 q.toArray(new String[10]);
694 jsr166 1.1 shouldThrow();
695     } catch (ArrayStoreException success) {}
696     }
697    
698     /**
699     * Iterator iterates through all elements
700     */
701     public void testIterator() {
702 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
703     Iterator<Item> it = q.iterator();
704 jsr166 1.17 int i;
705     for (i = 0; it.hasNext(); i++)
706 dl 1.37 mustContain(q, it.next());
707     mustEqual(i, SIZE);
708 jsr166 1.17 assertIteratorExhausted(it);
709     }
710    
711     /**
712     * iterator of empty collection has no elements
713     */
714     public void testEmptyIterator() {
715 dl 1.37 Deque<Item> c = new ConcurrentLinkedDeque<Item>();
716 jsr166 1.17 assertIteratorExhausted(c.iterator());
717     assertIteratorExhausted(c.descendingIterator());
718 jsr166 1.1 }
719    
720     /**
721     * Iterator ordering is FIFO
722     */
723     public void testIteratorOrdering() {
724 dl 1.37 final ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
725 jsr166 1.1 q.add(one);
726     q.add(two);
727     q.add(three);
728    
729     int k = 0;
730 dl 1.37 for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
731     mustEqual(++k, it.next());
732 jsr166 1.1 }
733    
734 dl 1.37 mustEqual(3, k);
735 jsr166 1.1 }
736    
737     /**
738     * Modifications do not cause iterators to fail
739     */
740     public void testWeaklyConsistentIteration() {
741 dl 1.37 final ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
742 jsr166 1.1 q.add(one);
743     q.add(two);
744     q.add(three);
745    
746 dl 1.37 for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
747 jsr166 1.1 q.remove();
748     it.next();
749     }
750    
751 dl 1.37 mustEqual(0, q.size());
752 jsr166 1.1 }
753    
754     /**
755     * iterator.remove() removes current element
756     */
757     public void testIteratorRemove() {
758 dl 1.37 final ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
759 jsr166 1.1 final Random rng = new Random();
760     for (int iters = 0; iters < 100; ++iters) {
761     int max = rng.nextInt(5) + 2;
762 jsr166 1.22 int split = rng.nextInt(max - 1) + 1;
763 jsr166 1.1 for (int j = 1; j <= max; ++j)
764 dl 1.37 mustAdd(q, j);
765     Iterator<? extends Item> it = q.iterator();
766 jsr166 1.1 for (int j = 1; j <= split; ++j)
767 dl 1.37 mustEqual(it.next(), j);
768 jsr166 1.1 it.remove();
769 dl 1.37 mustEqual(it.next(), itemFor(split + 1));
770 jsr166 1.1 for (int j = 1; j <= split; ++j)
771 dl 1.37 q.remove(itemFor(j));
772 jsr166 1.1 it = q.iterator();
773 jsr166 1.22 for (int j = split + 1; j <= max; ++j) {
774 dl 1.37 mustEqual(it.next(), j);
775 jsr166 1.1 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 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
787 jsr166 1.1 int i = 0;
788 dl 1.37 Iterator<? extends Item> it = q.descendingIterator();
789 jsr166 1.1 while (it.hasNext()) {
790 dl 1.37 mustContain(q, it.next());
791 jsr166 1.1 ++i;
792     }
793 dl 1.37 mustEqual(i, SIZE);
794 jsr166 1.1 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 dl 1.37 final ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
806 jsr166 1.1 for (int iters = 0; iters < 100; ++iters) {
807 dl 1.37 mustAdd(q, three);
808     mustAdd(q, two);
809     mustAdd(q, one);
810 jsr166 1.1 int k = 0;
811 dl 1.37 for (Iterator<? extends Item> it = q.descendingIterator(); it.hasNext();) {
812     mustEqual(++k, it.next());
813 jsr166 1.1 }
814    
815 dl 1.37 mustEqual(3, k);
816 jsr166 1.1 q.remove();
817     q.remove();
818     q.remove();
819     }
820     }
821    
822     /**
823     * descendingIterator.remove() removes current element
824     */
825     public void testDescendingIteratorRemove() {
826 dl 1.37 final ConcurrentLinkedDeque<Item> q = new ConcurrentLinkedDeque<Item>();
827 jsr166 1.1 final Random rng = new Random();
828     for (int iters = 0; iters < 100; ++iters) {
829     int max = rng.nextInt(5) + 2;
830 jsr166 1.22 int split = rng.nextInt(max - 1) + 1;
831 jsr166 1.1 for (int j = max; j >= 1; --j)
832 dl 1.37 mustAdd(q, j);
833     Iterator<? extends Item> it = q.descendingIterator();
834 jsr166 1.1 for (int j = 1; j <= split; ++j)
835 dl 1.37 mustEqual(it.next(), j);
836 jsr166 1.1 it.remove();
837 dl 1.37 mustEqual(it.next(), itemFor(split + 1));
838 jsr166 1.1 for (int j = 1; j <= split; ++j)
839 dl 1.37 q.remove(itemFor(j));
840 jsr166 1.1 it = q.descendingIterator();
841 jsr166 1.22 for (int j = split + 1; j <= max; ++j) {
842 dl 1.37 mustEqual(it.next(), j);
843 jsr166 1.1 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 dl 1.37 ConcurrentLinkedDeque<Item> q = populatedDeque(SIZE);
855 jsr166 1.1 String s = q.toString();
856     for (int i = 0; i < SIZE; ++i) {
857 jsr166 1.8 assertTrue(s.contains(String.valueOf(i)));
858 jsr166 1.1 }
859     }
860    
861     /**
862 jsr166 1.29 * A deserialized/reserialized deque has same elements in same order
863 jsr166 1.1 */
864     public void testSerialization() throws Exception {
865 dl 1.37 Queue<Item> x = populatedDeque(SIZE);
866     Queue<Item> y = serialClone(x);
867 jsr166 1.9
868 jsr166 1.12 assertNotSame(x, y);
869 dl 1.37 mustEqual(x.size(), y.size());
870     mustEqual(x.toString(), y.toString());
871 jsr166 1.9 assertTrue(Arrays.equals(x.toArray(), y.toArray()));
872     while (!x.isEmpty()) {
873     assertFalse(y.isEmpty());
874 dl 1.37 mustEqual(x.remove(), y.remove());
875 jsr166 1.9 }
876     assertTrue(y.isEmpty());
877 jsr166 1.1 }
878    
879 jsr166 1.13 /**
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     }
905 jsr166 1.30
906 jsr166 1.31 void runAsync(Runnable r1, Runnable r2) {
907 jsr166 1.34 boolean b = randomBoolean();
908 jsr166 1.31 CompletableFuture<Void> f1 = CompletableFuture.runAsync(b ? r1 : r2);
909     CompletableFuture<Void> f2 = CompletableFuture.runAsync(b ? r2 : r1);
910     f1.join();
911     f2.join();
912     }
913    
914 jsr166 1.30 /**
915     * Non-traversing Deque operations are linearizable.
916     * https://bugs.openjdk.java.net/browse/JDK-8188900
917     * ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8188900 tck
918     */
919     public void testBug8188900() {
920     final ThreadLocalRandom rnd = ThreadLocalRandom.current();
921     final LongAdder nulls = new LongAdder(), zeros = new LongAdder();
922     for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) {
923 dl 1.37 ConcurrentLinkedDeque<Item> d = new ConcurrentLinkedDeque<>();
924 jsr166 1.30
925     boolean peek = rnd.nextBoolean();
926     Runnable getter = () -> {
927 dl 1.37 Item x = peek ? d.peekFirst() : d.pollFirst();
928 jsr166 1.30 if (x == null) nulls.increment();
929 dl 1.37 else if (x.value == 0) zeros.increment();
930 jsr166 1.30 else
931     throw new AssertionError(
932     String.format(
933     "unexpected value %d after %d nulls and %d zeros",
934     x, nulls.sum(), zeros.sum()));
935     };
936    
937 dl 1.37 Runnable adder = () -> { d.addFirst(zero); d.addLast(fortytwo); };
938 jsr166 1.30
939 jsr166 1.31 runAsync(getter, adder);
940 jsr166 1.30 }
941     }
942    
943     /**
944     * Reverse direction variant of testBug8188900
945     */
946     public void testBug8188900_reverse() {
947     final ThreadLocalRandom rnd = ThreadLocalRandom.current();
948     final LongAdder nulls = new LongAdder(), zeros = new LongAdder();
949     for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) {
950 dl 1.37 ConcurrentLinkedDeque<Item> d = new ConcurrentLinkedDeque<>();
951 jsr166 1.30
952     boolean peek = rnd.nextBoolean();
953     Runnable getter = () -> {
954 dl 1.37 Item x = peek ? d.peekLast() : d.pollLast();
955 jsr166 1.30 if (x == null) nulls.increment();
956 dl 1.37 else if (x.value == 0) zeros.increment();
957 jsr166 1.30 else
958     throw new AssertionError(
959     String.format(
960     "unexpected value %d after %d nulls and %d zeros",
961     x, nulls.sum(), zeros.sum()));
962     };
963    
964 dl 1.37 Runnable adder = () -> { d.addLast(zero); d.addFirst(fortytwo); };
965 jsr166 1.31
966     runAsync(getter, adder);
967     }
968     }
969    
970     /**
971     * Non-traversing Deque operations (that return null) are linearizable.
972     * Don't return null when the deque is observably never empty.
973     * https://bugs.openjdk.java.net/browse/JDK-8189387
974     * ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8189387 tck
975     */
976     public void testBug8189387() {
977     Object x = new Object();
978     for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) {
979     ConcurrentLinkedDeque<Object> d = new ConcurrentLinkedDeque<>();
980     Runnable add = chooseRandomly(
981     () -> d.addFirst(x),
982     () -> d.offerFirst(x),
983     () -> d.addLast(x),
984     () -> d.offerLast(x));
985    
986     Runnable get = chooseRandomly(
987     () -> assertFalse(d.isEmpty()),
988     () -> assertSame(x, d.peekFirst()),
989     () -> assertSame(x, d.peekLast()),
990     () -> assertSame(x, d.pollFirst()),
991     () -> assertSame(x, d.pollLast()));
992    
993     Runnable addRemove = chooseRandomly(
994     () -> { d.addFirst(x); d.pollLast(); },
995     () -> { d.offerFirst(x); d.removeFirst(); },
996     () -> { d.offerLast(x); d.removeLast(); },
997     () -> { d.addLast(x); d.pollFirst(); });
998 jsr166 1.30
999 jsr166 1.31 add.run();
1000     runAsync(get, addRemove);
1001 jsr166 1.30 }
1002     }
1003 jsr166 1.1 }