6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Deque; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
import java.util.Queue; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.concurrent.ConcurrentLinkedDeque; |
17 |
> |
|
18 |
> |
import junit.framework.Test; |
19 |
> |
import junit.framework.TestSuite; |
20 |
|
|
21 |
|
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
22 |
|
|
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
|
27 |
|
public static Test suite() { |
28 |
< |
return new TestSuite(ConcurrentLinkedDequeTest.class); |
28 |
> |
class Implementation implements CollectionImplementation { |
29 |
> |
public Class<?> klazz() { return ConcurrentLinkedDeque.class; } |
30 |
> |
public Collection emptyCollection() { return new ConcurrentLinkedDeque(); } |
31 |
> |
public Object makeElement(int i) { return i; } |
32 |
> |
public boolean isConcurrent() { return true; } |
33 |
> |
public boolean permitsNulls() { return false; } |
34 |
> |
} |
35 |
> |
return newTestSuite(ConcurrentLinkedDequeTest.class, |
36 |
> |
CollectionTest.testSuite(new Implementation())); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
< |
* Create a deque of given size containing consecutive |
41 |
< |
* Integers 0 ... n. |
40 |
> |
* Returns a new deque of given size containing consecutive |
41 |
> |
* Integers 0 ... n - 1. |
42 |
|
*/ |
43 |
|
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
44 |
|
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>(); |
47 |
|
assertTrue(q.offer(new Integer(i))); |
48 |
|
assertFalse(q.isEmpty()); |
49 |
|
assertEquals(n, q.size()); |
50 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
51 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
52 |
|
return q; |
53 |
|
} |
54 |
|
|
65 |
|
*/ |
66 |
|
public void testConstructor3() { |
67 |
|
try { |
68 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null); |
68 |
> |
new ConcurrentLinkedDeque((Collection)null); |
69 |
|
shouldThrow(); |
70 |
|
} catch (NullPointerException success) {} |
71 |
|
} |
75 |
|
*/ |
76 |
|
public void testConstructor4() { |
77 |
|
try { |
78 |
< |
Integer[] ints = new Integer[SIZE]; |
62 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); |
78 |
> |
new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE])); |
79 |
|
shouldThrow(); |
80 |
|
} catch (NullPointerException success) {} |
81 |
|
} |
84 |
|
* Initializing from Collection with some null elements throws NPE |
85 |
|
*/ |
86 |
|
public void testConstructor5() { |
87 |
+ |
Integer[] ints = new Integer[SIZE]; |
88 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
89 |
+ |
ints[i] = new Integer(i); |
90 |
|
try { |
91 |
< |
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)); |
91 |
> |
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
92 |
|
shouldThrow(); |
93 |
|
} catch (NullPointerException success) {} |
94 |
|
} |
125 |
|
public void testSize() { |
126 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
127 |
|
for (int i = 0; i < SIZE; ++i) { |
128 |
< |
assertEquals(SIZE-i, q.size()); |
128 |
> |
assertEquals(SIZE - i, q.size()); |
129 |
|
q.remove(); |
130 |
|
} |
131 |
|
for (int i = 0; i < SIZE; ++i) { |
138 |
|
* push(null) throws NPE |
139 |
|
*/ |
140 |
|
public void testPushNull() { |
141 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
142 |
|
try { |
126 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
143 |
|
q.push(null); |
144 |
|
shouldThrow(); |
145 |
|
} catch (NullPointerException success) {} |
173 |
|
* offer(null) throws NPE |
174 |
|
*/ |
175 |
|
public void testOfferNull() { |
176 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
177 |
|
try { |
161 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
178 |
|
q.offer(null); |
179 |
|
shouldThrow(); |
180 |
|
} catch (NullPointerException success) {} |
184 |
|
* offerFirst(null) throws NPE |
185 |
|
*/ |
186 |
|
public void testOfferFirstNull() { |
187 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
188 |
|
try { |
172 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
189 |
|
q.offerFirst(null); |
190 |
|
shouldThrow(); |
191 |
|
} catch (NullPointerException success) {} |
195 |
|
* offerLast(null) throws NPE |
196 |
|
*/ |
197 |
|
public void testOfferLastNull() { |
198 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
199 |
|
try { |
183 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
200 |
|
q.offerLast(null); |
201 |
|
shouldThrow(); |
202 |
|
} catch (NullPointerException success) {} |
239 |
|
* add(null) throws NPE |
240 |
|
*/ |
241 |
|
public void testAddNull() { |
242 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
243 |
|
try { |
227 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
244 |
|
q.add(null); |
245 |
|
shouldThrow(); |
246 |
|
} catch (NullPointerException success) {} |
250 |
|
* addFirst(null) throws NPE |
251 |
|
*/ |
252 |
|
public void testAddFirstNull() { |
253 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
254 |
|
try { |
238 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
255 |
|
q.addFirst(null); |
256 |
|
shouldThrow(); |
257 |
|
} catch (NullPointerException success) {} |
261 |
|
* addLast(null) throws NPE |
262 |
|
*/ |
263 |
|
public void testAddLastNull() { |
264 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
265 |
|
try { |
249 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
266 |
|
q.addLast(null); |
267 |
|
shouldThrow(); |
268 |
|
} catch (NullPointerException success) {} |
305 |
|
* addAll(null) throws NPE |
306 |
|
*/ |
307 |
|
public void testAddAll1() { |
308 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
309 |
|
try { |
293 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
310 |
|
q.addAll(null); |
311 |
|
shouldThrow(); |
312 |
|
} catch (NullPointerException success) {} |
316 |
|
* addAll(this) throws IAE |
317 |
|
*/ |
318 |
|
public void testAddAllSelf() { |
319 |
+ |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
320 |
|
try { |
304 |
– |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
321 |
|
q.addAll(q); |
322 |
|
shouldThrow(); |
323 |
|
} catch (IllegalArgumentException success) {} |
327 |
|
* addAll of a collection with null elements throws NPE |
328 |
|
*/ |
329 |
|
public void testAddAll2() { |
330 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
331 |
|
try { |
332 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
316 |
< |
Integer[] ints = new Integer[SIZE]; |
317 |
< |
q.addAll(Arrays.asList(ints)); |
332 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
333 |
|
shouldThrow(); |
334 |
|
} catch (NullPointerException success) {} |
335 |
|
} |
339 |
|
* possibly adding some elements |
340 |
|
*/ |
341 |
|
public void testAddAll3() { |
342 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
343 |
+ |
Integer[] ints = new Integer[SIZE]; |
344 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
345 |
+ |
ints[i] = new Integer(i); |
346 |
|
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); |
347 |
|
q.addAll(Arrays.asList(ints)); |
348 |
|
shouldThrow(); |
349 |
|
} catch (NullPointerException success) {} |
380 |
|
*/ |
381 |
|
public void testPollLast() { |
382 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
383 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
383 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
384 |
|
assertEquals(i, q.pollLast()); |
385 |
|
} |
386 |
|
assertNull(q.pollLast()); |
445 |
|
*/ |
446 |
|
public void testRemoveElement() { |
447 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
448 |
< |
for (int i = 1; i < SIZE; i+=2) { |
448 |
> |
for (int i = 1; i < SIZE; i += 2) { |
449 |
|
assertTrue(q.contains(i)); |
450 |
|
assertTrue(q.remove(i)); |
451 |
|
assertFalse(q.contains(i)); |
452 |
< |
assertTrue(q.contains(i-1)); |
452 |
> |
assertTrue(q.contains(i - 1)); |
453 |
|
} |
454 |
< |
for (int i = 0; i < SIZE; i+=2) { |
454 |
> |
for (int i = 0; i < SIZE; i += 2) { |
455 |
|
assertTrue(q.contains(i)); |
456 |
|
assertTrue(q.remove(i)); |
457 |
|
assertFalse(q.contains(i)); |
458 |
< |
assertFalse(q.remove(i+1)); |
459 |
< |
assertFalse(q.contains(i+1)); |
458 |
> |
assertFalse(q.remove(i + 1)); |
459 |
> |
assertFalse(q.contains(i + 1)); |
460 |
|
} |
461 |
|
assertTrue(q.isEmpty()); |
462 |
|
} |
480 |
|
*/ |
481 |
|
public void testPeekLast() { |
482 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
483 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
483 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
484 |
|
assertEquals(i, q.peekLast()); |
485 |
|
assertEquals(i, q.pollLast()); |
486 |
|
assertTrue(q.peekLast() == null || |
509 |
|
*/ |
510 |
|
public void testLastElement() { |
511 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
512 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
512 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
513 |
|
assertEquals(i, q.getLast()); |
514 |
|
assertEquals(i, q.pollLast()); |
515 |
|
} |
555 |
|
*/ |
556 |
|
public void testRemoveFirstOccurrence() { |
557 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
558 |
< |
for (int i = 1; i < SIZE; i+=2) { |
558 |
> |
for (int i = 1; i < SIZE; i += 2) { |
559 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
560 |
|
} |
561 |
< |
for (int i = 0; i < SIZE; i+=2) { |
561 |
> |
for (int i = 0; i < SIZE; i += 2) { |
562 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
563 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
563 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
564 |
|
} |
565 |
|
assertTrue(q.isEmpty()); |
566 |
|
} |
570 |
|
*/ |
571 |
|
public void testRemoveLastOccurrence() { |
572 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
573 |
< |
for (int i = 1; i < SIZE; i+=2) { |
573 |
> |
for (int i = 1; i < SIZE; i += 2) { |
574 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
575 |
|
} |
576 |
< |
for (int i = 0; i < SIZE; i+=2) { |
576 |
> |
for (int i = 0; i < SIZE; i += 2) { |
577 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
578 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
578 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
579 |
|
} |
580 |
|
assertTrue(q.isEmpty()); |
581 |
|
} |
634 |
|
assertTrue(changed); |
635 |
|
|
636 |
|
assertTrue(q.containsAll(p)); |
637 |
< |
assertEquals(SIZE-i, q.size()); |
637 |
> |
assertEquals(SIZE - i, q.size()); |
638 |
|
p.remove(); |
639 |
|
} |
640 |
|
} |
647 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
648 |
|
ConcurrentLinkedDeque p = populatedDeque(i); |
649 |
|
assertTrue(q.removeAll(p)); |
650 |
< |
assertEquals(SIZE-i, q.size()); |
650 |
> |
assertEquals(SIZE - i, q.size()); |
651 |
|
for (int j = 0; j < i; ++j) { |
652 |
< |
Integer I = (Integer)(p.remove()); |
653 |
< |
assertFalse(q.contains(I)); |
652 |
> |
Integer x = (Integer)(p.remove()); |
653 |
> |
assertFalse(q.contains(x)); |
654 |
|
} |
655 |
|
} |
656 |
|
} |
704 |
|
*/ |
705 |
|
public void testIterator() { |
706 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
692 |
– |
int i = 0; |
707 |
|
Iterator it = q.iterator(); |
708 |
< |
while (it.hasNext()) { |
708 |
> |
int i; |
709 |
> |
for (i = 0; it.hasNext(); i++) |
710 |
|
assertTrue(q.contains(it.next())); |
696 |
– |
++i; |
697 |
– |
} |
711 |
|
assertEquals(i, SIZE); |
712 |
+ |
assertIteratorExhausted(it); |
713 |
+ |
} |
714 |
+ |
|
715 |
+ |
/** |
716 |
+ |
* iterator of empty collection has no elements |
717 |
+ |
*/ |
718 |
+ |
public void testEmptyIterator() { |
719 |
+ |
Deque c = new ConcurrentLinkedDeque(); |
720 |
+ |
assertIteratorExhausted(c.iterator()); |
721 |
+ |
assertIteratorExhausted(c.descendingIterator()); |
722 |
|
} |
723 |
|
|
724 |
|
/** |
763 |
|
final Random rng = new Random(); |
764 |
|
for (int iters = 0; iters < 100; ++iters) { |
765 |
|
int max = rng.nextInt(5) + 2; |
766 |
< |
int split = rng.nextInt(max-1) + 1; |
766 |
> |
int split = rng.nextInt(max - 1) + 1; |
767 |
|
for (int j = 1; j <= max; ++j) |
768 |
|
q.add(new Integer(j)); |
769 |
|
Iterator it = q.iterator(); |
770 |
|
for (int j = 1; j <= split; ++j) |
771 |
|
assertEquals(it.next(), new Integer(j)); |
772 |
|
it.remove(); |
773 |
< |
assertEquals(it.next(), new Integer(split+1)); |
773 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
774 |
|
for (int j = 1; j <= split; ++j) |
775 |
|
q.remove(new Integer(j)); |
776 |
|
it = q.iterator(); |
777 |
< |
for (int j = split+1; j <= max; ++j) { |
777 |
> |
for (int j = split + 1; j <= max; ++j) { |
778 |
|
assertEquals(it.next(), new Integer(j)); |
779 |
|
it.remove(); |
780 |
|
} |
831 |
|
final Random rng = new Random(); |
832 |
|
for (int iters = 0; iters < 100; ++iters) { |
833 |
|
int max = rng.nextInt(5) + 2; |
834 |
< |
int split = rng.nextInt(max-1) + 1; |
834 |
> |
int split = rng.nextInt(max - 1) + 1; |
835 |
|
for (int j = max; j >= 1; --j) |
836 |
|
q.add(new Integer(j)); |
837 |
|
Iterator it = q.descendingIterator(); |
838 |
|
for (int j = 1; j <= split; ++j) |
839 |
|
assertEquals(it.next(), new Integer(j)); |
840 |
|
it.remove(); |
841 |
< |
assertEquals(it.next(), new Integer(split+1)); |
841 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
842 |
|
for (int j = 1; j <= split; ++j) |
843 |
|
q.remove(new Integer(j)); |
844 |
|
it = q.descendingIterator(); |
845 |
< |
for (int j = split+1; j <= max; ++j) { |
845 |
> |
for (int j = split + 1; j <= max; ++j) { |
846 |
|
assertEquals(it.next(), new Integer(j)); |
847 |
|
it.remove(); |
848 |
|
} |
866 |
|
* A deserialized serialized deque has same elements in same order |
867 |
|
*/ |
868 |
|
public void testSerialization() throws Exception { |
869 |
< |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
870 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
871 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
872 |
< |
out.writeObject(q); |
873 |
< |
out.close(); |
874 |
< |
|
875 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
876 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
877 |
< |
ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject(); |
878 |
< |
assertEquals(q.size(), r.size()); |
879 |
< |
while (!q.isEmpty()) |
880 |
< |
assertEquals(q.remove(), r.remove()); |
869 |
> |
Queue x = populatedDeque(SIZE); |
870 |
> |
Queue y = serialClone(x); |
871 |
> |
|
872 |
> |
assertNotSame(x, y); |
873 |
> |
assertEquals(x.size(), y.size()); |
874 |
> |
assertEquals(x.toString(), y.toString()); |
875 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
876 |
> |
while (!x.isEmpty()) { |
877 |
> |
assertFalse(y.isEmpty()); |
878 |
> |
assertEquals(x.remove(), y.remove()); |
879 |
> |
} |
880 |
> |
assertTrue(y.isEmpty()); |
881 |
|
} |
882 |
|
|
883 |
+ |
/** |
884 |
+ |
* contains(null) always return false. |
885 |
+ |
* remove(null) always throws NullPointerException. |
886 |
+ |
*/ |
887 |
+ |
public void testNeverContainsNull() { |
888 |
+ |
Deque<?>[] qs = { |
889 |
+ |
new ConcurrentLinkedDeque<Object>(), |
890 |
+ |
populatedDeque(2), |
891 |
+ |
}; |
892 |
+ |
|
893 |
+ |
for (Deque<?> q : qs) { |
894 |
+ |
assertFalse(q.contains(null)); |
895 |
+ |
try { |
896 |
+ |
assertFalse(q.remove(null)); |
897 |
+ |
shouldThrow(); |
898 |
+ |
} catch (NullPointerException success) {} |
899 |
+ |
try { |
900 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
901 |
+ |
shouldThrow(); |
902 |
+ |
} catch (NullPointerException success) {} |
903 |
+ |
try { |
904 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
905 |
+ |
shouldThrow(); |
906 |
+ |
} catch (NullPointerException success) {} |
907 |
+ |
} |
908 |
+ |
} |
909 |
|
} |