1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
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.*; |
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() { |
29 |
|
} |
30 |
|
|
31 |
|
/** |
32 |
< |
* Create a deque of given size containing consecutive |
32 |
> |
* Returns a new deque of given size containing consecutive |
33 |
|
* Integers 0 ... n. |
34 |
|
*/ |
35 |
< |
private ConcurrentLinkedDeque populatedDeque(int n) { |
36 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
35 |
> |
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
36 |
> |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>(); |
37 |
|
assertTrue(q.isEmpty()); |
38 |
|
for (int i = 0; i < n; ++i) |
39 |
|
assertTrue(q.offer(new Integer(i))); |
55 |
|
*/ |
56 |
|
public void testConstructor3() { |
57 |
|
try { |
58 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null); |
58 |
> |
new ConcurrentLinkedDeque((Collection)null); |
59 |
|
shouldThrow(); |
60 |
|
} catch (NullPointerException success) {} |
61 |
|
} |
66 |
|
public void testConstructor4() { |
67 |
|
try { |
68 |
|
Integer[] ints = new Integer[SIZE]; |
69 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); |
69 |
> |
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
70 |
|
shouldThrow(); |
71 |
|
} catch (NullPointerException success) {} |
72 |
|
} |
79 |
|
Integer[] ints = new Integer[SIZE]; |
80 |
|
for (int i = 0; i < SIZE-1; ++i) |
81 |
|
ints[i] = new Integer(i); |
82 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); |
82 |
> |
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
83 |
|
shouldThrow(); |
84 |
|
} catch (NullPointerException success) {} |
85 |
|
} |
437 |
|
*/ |
438 |
|
public void testRemoveElement() { |
439 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
440 |
< |
for (int i = 1; i < SIZE; i+=2) { |
441 |
< |
assertTrue(q.remove(new Integer(i))); |
442 |
< |
} |
443 |
< |
for (int i = 0; i < SIZE; i+=2) { |
444 |
< |
assertTrue(q.remove(new Integer(i))); |
445 |
< |
assertFalse(q.remove(new Integer(i+1))); |
440 |
> |
for (int i = 1; i < SIZE; i += 2) { |
441 |
> |
assertTrue(q.contains(i)); |
442 |
> |
assertTrue(q.remove(i)); |
443 |
> |
assertFalse(q.contains(i)); |
444 |
> |
assertTrue(q.contains(i-1)); |
445 |
> |
} |
446 |
> |
for (int i = 0; i < SIZE; i += 2) { |
447 |
> |
assertTrue(q.contains(i)); |
448 |
> |
assertTrue(q.remove(i)); |
449 |
> |
assertFalse(q.contains(i)); |
450 |
> |
assertFalse(q.remove(i+1)); |
451 |
> |
assertFalse(q.contains(i+1)); |
452 |
|
} |
453 |
|
assertTrue(q.isEmpty()); |
454 |
|
} |
547 |
|
*/ |
548 |
|
public void testRemoveFirstOccurrence() { |
549 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
550 |
< |
for (int i = 1; i < SIZE; i+=2) { |
550 |
> |
for (int i = 1; i < SIZE; i += 2) { |
551 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
552 |
|
} |
553 |
< |
for (int i = 0; i < SIZE; i+=2) { |
553 |
> |
for (int i = 0; i < SIZE; i += 2) { |
554 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
555 |
|
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
556 |
|
} |
562 |
|
*/ |
563 |
|
public void testRemoveLastOccurrence() { |
564 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
565 |
< |
for (int i = 1; i < SIZE; i+=2) { |
565 |
> |
for (int i = 1; i < SIZE; i += 2) { |
566 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
567 |
|
} |
568 |
< |
for (int i = 0; i < SIZE; i+=2) { |
568 |
> |
for (int i = 0; i < SIZE; i += 2) { |
569 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
570 |
|
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
571 |
|
} |
641 |
|
assertTrue(q.removeAll(p)); |
642 |
|
assertEquals(SIZE-i, q.size()); |
643 |
|
for (int j = 0; j < i; ++j) { |
644 |
< |
Integer I = (Integer)(p.remove()); |
645 |
< |
assertFalse(q.contains(I)); |
644 |
> |
Integer x = (Integer)(p.remove()); |
645 |
> |
assertFalse(q.contains(x)); |
646 |
|
} |
647 |
|
} |
648 |
|
} |
649 |
|
|
650 |
|
/** |
651 |
< |
* toArray() contains all elements |
651 |
> |
* toArray() contains all elements in FIFO order |
652 |
|
*/ |
653 |
|
public void testToArray() { |
654 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
655 |
|
Object[] o = q.toArray(); |
643 |
– |
Arrays.sort(o); |
656 |
|
for (int i = 0; i < o.length; i++) |
657 |
< |
assertEquals(o[i], q.poll()); |
657 |
> |
assertSame(o[i], q.poll()); |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
< |
* toArray(a) contains all elements |
661 |
> |
* toArray(a) contains all elements in FIFO order |
662 |
|
*/ |
663 |
|
public void testToArray2() { |
664 |
< |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
664 |
> |
ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE); |
665 |
|
Integer[] ints = new Integer[SIZE]; |
666 |
< |
ints = (Integer[])q.toArray(ints); |
667 |
< |
Arrays.sort(ints); |
666 |
> |
Integer[] array = q.toArray(ints); |
667 |
> |
assertSame(ints, array); |
668 |
|
for (int i = 0; i < ints.length; i++) |
669 |
< |
assertEquals(ints[i], q.poll()); |
669 |
> |
assertSame(ints[i], q.poll()); |
670 |
|
} |
671 |
|
|
672 |
|
/** |
696 |
|
*/ |
697 |
|
public void testIterator() { |
698 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
687 |
– |
int i = 0; |
699 |
|
Iterator it = q.iterator(); |
700 |
< |
while (it.hasNext()) { |
700 |
> |
int i; |
701 |
> |
for (i = 0; it.hasNext(); i++) |
702 |
|
assertTrue(q.contains(it.next())); |
691 |
– |
++i; |
692 |
– |
} |
703 |
|
assertEquals(i, SIZE); |
704 |
+ |
assertIteratorExhausted(it); |
705 |
+ |
} |
706 |
+ |
|
707 |
+ |
/** |
708 |
+ |
* iterator of empty collection has no elements |
709 |
+ |
*/ |
710 |
+ |
public void testEmptyIterator() { |
711 |
+ |
Deque c = new ConcurrentLinkedDeque(); |
712 |
+ |
assertIteratorExhausted(c.iterator()); |
713 |
+ |
assertIteratorExhausted(c.descendingIterator()); |
714 |
|
} |
715 |
|
|
716 |
|
/** |
850 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
851 |
|
String s = q.toString(); |
852 |
|
for (int i = 0; i < SIZE; ++i) { |
853 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
853 |
> |
assertTrue(s.contains(String.valueOf(i))); |
854 |
|
} |
855 |
|
} |
856 |
|
|
858 |
|
* A deserialized serialized deque has same elements in same order |
859 |
|
*/ |
860 |
|
public void testSerialization() throws Exception { |
861 |
< |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
862 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
863 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
864 |
< |
out.writeObject(q); |
865 |
< |
out.close(); |
866 |
< |
|
867 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
868 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
869 |
< |
ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject(); |
870 |
< |
assertEquals(q.size(), r.size()); |
871 |
< |
while (!q.isEmpty()) |
872 |
< |
assertEquals(q.remove(), r.remove()); |
861 |
> |
Queue x = populatedDeque(SIZE); |
862 |
> |
Queue y = serialClone(x); |
863 |
> |
|
864 |
> |
assertNotSame(x, y); |
865 |
> |
assertEquals(x.size(), y.size()); |
866 |
> |
assertEquals(x.toString(), y.toString()); |
867 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
868 |
> |
while (!x.isEmpty()) { |
869 |
> |
assertFalse(y.isEmpty()); |
870 |
> |
assertEquals(x.remove(), y.remove()); |
871 |
> |
} |
872 |
> |
assertTrue(y.isEmpty()); |
873 |
|
} |
874 |
|
|
875 |
+ |
/** |
876 |
+ |
* contains(null) always return false. |
877 |
+ |
* remove(null) always throws NullPointerException. |
878 |
+ |
*/ |
879 |
+ |
public void testNeverContainsNull() { |
880 |
+ |
Deque<?>[] qs = { |
881 |
+ |
new ConcurrentLinkedDeque<Object>(), |
882 |
+ |
populatedDeque(2), |
883 |
+ |
}; |
884 |
+ |
|
885 |
+ |
for (Deque<?> q : qs) { |
886 |
+ |
assertFalse(q.contains(null)); |
887 |
+ |
try { |
888 |
+ |
assertFalse(q.remove(null)); |
889 |
+ |
shouldThrow(); |
890 |
+ |
} catch (NullPointerException success) {} |
891 |
+ |
try { |
892 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
893 |
+ |
shouldThrow(); |
894 |
+ |
} catch (NullPointerException success) {} |
895 |
+ |
try { |
896 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
897 |
+ |
shouldThrow(); |
898 |
+ |
} catch (NullPointerException success) {} |
899 |
+ |
} |
900 |
+ |
} |
901 |
|
} |