7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collection; |
12 |
> |
import java.util.Deque; |
13 |
> |
import java.util.Iterator; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Queue; |
16 |
> |
import java.util.Random; |
17 |
> |
import java.util.concurrent.ConcurrentLinkedDeque; |
18 |
|
|
19 |
|
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
20 |
|
|
27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
< |
* Create a deque of given size containing consecutive |
30 |
> |
* Returns a new deque of given size containing consecutive |
31 |
|
* Integers 0 ... n. |
32 |
|
*/ |
33 |
|
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
848 |
|
* A deserialized serialized deque has same elements in same order |
849 |
|
*/ |
850 |
|
public void testSerialization() throws Exception { |
851 |
< |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
852 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
853 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
854 |
< |
out.writeObject(q); |
855 |
< |
out.close(); |
856 |
< |
|
857 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
858 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
859 |
< |
ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject(); |
860 |
< |
assertEquals(q.size(), r.size()); |
861 |
< |
while (!q.isEmpty()) |
862 |
< |
assertEquals(q.remove(), r.remove()); |
851 |
> |
Queue x = populatedDeque(SIZE); |
852 |
> |
Queue y = serialClone(x); |
853 |
> |
|
854 |
> |
assertNotSame(x, y); |
855 |
> |
assertEquals(x.size(), y.size()); |
856 |
> |
assertEquals(x.toString(), y.toString()); |
857 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
858 |
> |
while (!x.isEmpty()) { |
859 |
> |
assertFalse(y.isEmpty()); |
860 |
> |
assertEquals(x.remove(), y.remove()); |
861 |
> |
} |
862 |
> |
assertTrue(y.isEmpty()); |
863 |
|
} |
864 |
|
|
865 |
+ |
/** |
866 |
+ |
* contains(null) always return false. |
867 |
+ |
* remove(null) always throws NullPointerException. |
868 |
+ |
*/ |
869 |
+ |
public void testNeverContainsNull() { |
870 |
+ |
Deque<?>[] qs = { |
871 |
+ |
new ConcurrentLinkedDeque<Object>(), |
872 |
+ |
populatedDeque(2), |
873 |
+ |
}; |
874 |
+ |
|
875 |
+ |
for (Deque<?> q : qs) { |
876 |
+ |
assertFalse(q.contains(null)); |
877 |
+ |
try { |
878 |
+ |
assertFalse(q.remove(null)); |
879 |
+ |
shouldThrow(); |
880 |
+ |
} catch (NullPointerException success) {} |
881 |
+ |
try { |
882 |
+ |
assertFalse(q.removeFirstOccurrence(null)); |
883 |
+ |
shouldThrow(); |
884 |
+ |
} catch (NullPointerException success) {} |
885 |
+ |
try { |
886 |
+ |
assertFalse(q.removeLastOccurrence(null)); |
887 |
+ |
shouldThrow(); |
888 |
+ |
} catch (NullPointerException success) {} |
889 |
+ |
} |
890 |
+ |
} |
891 |
|
} |