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.*; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collection; |
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 |
|
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
19 |
|
|
26 |
|
} |
27 |
|
|
28 |
|
/** |
29 |
< |
* Create a deque of given size containing consecutive |
29 |
> |
* Returns a new deque of given size containing consecutive |
30 |
|
* Integers 0 ... n. |
31 |
|
*/ |
32 |
< |
private ConcurrentLinkedDeque populatedDeque(int n) { |
33 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
32 |
> |
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
33 |
> |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>(); |
34 |
|
assertTrue(q.isEmpty()); |
35 |
|
for (int i = 0; i < n; ++i) |
36 |
|
assertTrue(q.offer(new Integer(i))); |
435 |
|
public void testRemoveElement() { |
436 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
437 |
|
for (int i = 1; i < SIZE; i+=2) { |
438 |
< |
assertTrue(q.remove(new Integer(i))); |
438 |
> |
assertTrue(q.contains(i)); |
439 |
> |
assertTrue(q.remove(i)); |
440 |
> |
assertFalse(q.contains(i)); |
441 |
> |
assertTrue(q.contains(i-1)); |
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))); |
444 |
> |
assertTrue(q.contains(i)); |
445 |
> |
assertTrue(q.remove(i)); |
446 |
> |
assertFalse(q.contains(i)); |
447 |
> |
assertFalse(q.remove(i+1)); |
448 |
> |
assertFalse(q.contains(i+1)); |
449 |
|
} |
450 |
|
assertTrue(q.isEmpty()); |
451 |
|
} |
645 |
|
} |
646 |
|
|
647 |
|
/** |
648 |
< |
* toArray() contains all elements |
648 |
> |
* toArray() contains all elements in FIFO order |
649 |
|
*/ |
650 |
|
public void testToArray() { |
651 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
652 |
|
Object[] o = q.toArray(); |
643 |
– |
Arrays.sort(o); |
653 |
|
for (int i = 0; i < o.length; i++) |
654 |
< |
assertEquals(o[i], q.poll()); |
654 |
> |
assertSame(o[i], q.poll()); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
< |
* toArray(a) contains all elements |
658 |
> |
* toArray(a) contains all elements in FIFO order |
659 |
|
*/ |
660 |
|
public void testToArray2() { |
661 |
< |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
661 |
> |
ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE); |
662 |
|
Integer[] ints = new Integer[SIZE]; |
663 |
< |
ints = (Integer[])q.toArray(ints); |
664 |
< |
Arrays.sort(ints); |
663 |
> |
Integer[] array = q.toArray(ints); |
664 |
> |
assertSame(ints, array); |
665 |
|
for (int i = 0; i < ints.length; i++) |
666 |
< |
assertEquals(ints[i], q.poll()); |
666 |
> |
assertSame(ints[i], q.poll()); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* toArray(null) throws NPE |
670 |
> |
* toArray(null) throws NullPointerException |
671 |
|
*/ |
672 |
< |
public void testToArray_BadArg() { |
672 |
> |
public void testToArray_NullArg() { |
673 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
674 |
|
try { |
675 |
< |
Object o[] = q.toArray(null); |
675 |
> |
q.toArray(null); |
676 |
|
shouldThrow(); |
677 |
|
} catch (NullPointerException success) {} |
678 |
|
} |
679 |
|
|
680 |
|
/** |
681 |
< |
* toArray() with incompatible array type throws ArrayStoreException |
681 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
682 |
|
*/ |
683 |
|
public void testToArray1_BadArg() { |
684 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
685 |
|
try { |
686 |
< |
Object o[] = q.toArray(new String[10]); |
686 |
> |
q.toArray(new String[10]); |
687 |
|
shouldThrow(); |
688 |
|
} catch (ArrayStoreException success) {} |
689 |
|
} |
839 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
840 |
|
String s = q.toString(); |
841 |
|
for (int i = 0; i < SIZE; ++i) { |
842 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
842 |
> |
assertTrue(s.contains(String.valueOf(i))); |
843 |
|
} |
844 |
|
} |
845 |
|
|
847 |
|
* A deserialized serialized deque has same elements in same order |
848 |
|
*/ |
849 |
|
public void testSerialization() throws Exception { |
850 |
< |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
851 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
852 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
853 |
< |
out.writeObject(q); |
854 |
< |
out.close(); |
855 |
< |
|
856 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
857 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
858 |
< |
ConcurrentLinkedDeque r = (ConcurrentLinkedDeque)in.readObject(); |
859 |
< |
assertEquals(q.size(), r.size()); |
860 |
< |
while (!q.isEmpty()) |
861 |
< |
assertEquals(q.remove(), r.remove()); |
850 |
> |
Queue x = populatedDeque(SIZE); |
851 |
> |
Queue y = serialClone(x); |
852 |
> |
|
853 |
> |
assertTrue(x != y); |
854 |
> |
assertEquals(x.size(), y.size()); |
855 |
> |
assertEquals(x.toString(), y.toString()); |
856 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
857 |
> |
while (!x.isEmpty()) { |
858 |
> |
assertFalse(y.isEmpty()); |
859 |
> |
assertEquals(x.remove(), y.remove()); |
860 |
> |
} |
861 |
> |
assertTrue(y.isEmpty()); |
862 |
|
} |
863 |
|
|
864 |
|
} |