13 |
|
import java.util.NoSuchElementException; |
14 |
|
import java.util.Queue; |
15 |
|
import java.util.Random; |
16 |
+ |
import java.util.concurrent.CompletableFuture; |
17 |
|
import java.util.concurrent.ConcurrentLinkedDeque; |
18 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
19 |
+ |
import java.util.concurrent.atomic.LongAdder; |
20 |
|
|
21 |
|
import junit.framework.Test; |
19 |
– |
import junit.framework.TestSuite; |
22 |
|
|
23 |
|
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
24 |
|
|
27 |
|
} |
28 |
|
|
29 |
|
public static Test suite() { |
30 |
< |
return new TestSuite(ConcurrentLinkedDequeTest.class); |
30 |
> |
class Implementation implements CollectionImplementation { |
31 |
> |
public Class<?> klazz() { return ConcurrentLinkedDeque.class; } |
32 |
> |
public Collection emptyCollection() { return new ConcurrentLinkedDeque(); } |
33 |
> |
public Object makeElement(int i) { return i; } |
34 |
> |
public boolean isConcurrent() { return true; } |
35 |
> |
public boolean permitsNulls() { return false; } |
36 |
> |
} |
37 |
> |
return newTestSuite(ConcurrentLinkedDequeTest.class, |
38 |
> |
CollectionTest.testSuite(new Implementation())); |
39 |
|
} |
40 |
|
|
41 |
|
/** |
42 |
|
* Returns a new deque of given size containing consecutive |
43 |
< |
* Integers 0 ... n. |
43 |
> |
* Integers 0 ... n - 1. |
44 |
|
*/ |
45 |
< |
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
46 |
< |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>(); |
45 |
> |
private static ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
46 |
> |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>(); |
47 |
|
assertTrue(q.isEmpty()); |
48 |
|
for (int i = 0; i < n; ++i) |
49 |
|
assertTrue(q.offer(new Integer(i))); |
50 |
|
assertFalse(q.isEmpty()); |
51 |
|
assertEquals(n, q.size()); |
52 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
53 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
54 |
|
return q; |
55 |
|
} |
56 |
|
|
315 |
|
} |
316 |
|
|
317 |
|
/** |
318 |
< |
* addAll(this) throws IAE |
318 |
> |
* addAll(this) throws IllegalArgumentException |
319 |
|
*/ |
320 |
|
public void testAddAllSelf() { |
321 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
451 |
|
assertTrue(q.contains(i)); |
452 |
|
assertTrue(q.remove(i)); |
453 |
|
assertFalse(q.contains(i)); |
454 |
< |
assertTrue(q.contains(i-1)); |
454 |
> |
assertTrue(q.contains(i - 1)); |
455 |
|
} |
456 |
|
for (int i = 0; i < SIZE; i += 2) { |
457 |
|
assertTrue(q.contains(i)); |
458 |
|
assertTrue(q.remove(i)); |
459 |
|
assertFalse(q.contains(i)); |
460 |
< |
assertFalse(q.remove(i+1)); |
461 |
< |
assertFalse(q.contains(i+1)); |
460 |
> |
assertFalse(q.remove(i + 1)); |
461 |
> |
assertFalse(q.contains(i + 1)); |
462 |
|
} |
463 |
|
assertTrue(q.isEmpty()); |
464 |
|
} |
562 |
|
} |
563 |
|
for (int i = 0; i < SIZE; i += 2) { |
564 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
565 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
565 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
566 |
|
} |
567 |
|
assertTrue(q.isEmpty()); |
568 |
|
} |
577 |
|
} |
578 |
|
for (int i = 0; i < SIZE; i += 2) { |
579 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
580 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
580 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
581 |
|
} |
582 |
|
assertTrue(q.isEmpty()); |
583 |
|
} |
765 |
|
final Random rng = new Random(); |
766 |
|
for (int iters = 0; iters < 100; ++iters) { |
767 |
|
int max = rng.nextInt(5) + 2; |
768 |
< |
int split = rng.nextInt(max-1) + 1; |
768 |
> |
int split = rng.nextInt(max - 1) + 1; |
769 |
|
for (int j = 1; j <= max; ++j) |
770 |
|
q.add(new Integer(j)); |
771 |
|
Iterator it = q.iterator(); |
772 |
|
for (int j = 1; j <= split; ++j) |
773 |
|
assertEquals(it.next(), new Integer(j)); |
774 |
|
it.remove(); |
775 |
< |
assertEquals(it.next(), new Integer(split+1)); |
775 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
776 |
|
for (int j = 1; j <= split; ++j) |
777 |
|
q.remove(new Integer(j)); |
778 |
|
it = q.iterator(); |
779 |
< |
for (int j = split+1; j <= max; ++j) { |
779 |
> |
for (int j = split + 1; j <= max; ++j) { |
780 |
|
assertEquals(it.next(), new Integer(j)); |
781 |
|
it.remove(); |
782 |
|
} |
833 |
|
final Random rng = new Random(); |
834 |
|
for (int iters = 0; iters < 100; ++iters) { |
835 |
|
int max = rng.nextInt(5) + 2; |
836 |
< |
int split = rng.nextInt(max-1) + 1; |
836 |
> |
int split = rng.nextInt(max - 1) + 1; |
837 |
|
for (int j = max; j >= 1; --j) |
838 |
|
q.add(new Integer(j)); |
839 |
|
Iterator it = q.descendingIterator(); |
840 |
|
for (int j = 1; j <= split; ++j) |
841 |
|
assertEquals(it.next(), new Integer(j)); |
842 |
|
it.remove(); |
843 |
< |
assertEquals(it.next(), new Integer(split+1)); |
843 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
844 |
|
for (int j = 1; j <= split; ++j) |
845 |
|
q.remove(new Integer(j)); |
846 |
|
it = q.descendingIterator(); |
847 |
< |
for (int j = split+1; j <= max; ++j) { |
847 |
> |
for (int j = split + 1; j <= max; ++j) { |
848 |
|
assertEquals(it.next(), new Integer(j)); |
849 |
|
it.remove(); |
850 |
|
} |
865 |
|
} |
866 |
|
|
867 |
|
/** |
868 |
< |
* A deserialized serialized deque has same elements in same order |
868 |
> |
* A deserialized/reserialized deque has same elements in same order |
869 |
|
*/ |
870 |
|
public void testSerialization() throws Exception { |
871 |
|
Queue x = populatedDeque(SIZE); |
908 |
|
} catch (NullPointerException success) {} |
909 |
|
} |
910 |
|
} |
911 |
+ |
|
912 |
+ |
/** |
913 |
+ |
* Non-traversing Deque operations are linearizable. |
914 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8188900 |
915 |
+ |
* ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ConcurrentLinkedDequeTest -Djsr166.methodFilter=testBug8188900 tck |
916 |
+ |
*/ |
917 |
+ |
public void testBug8188900() { |
918 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
919 |
+ |
final LongAdder nulls = new LongAdder(), zeros = new LongAdder(); |
920 |
+ |
for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) { |
921 |
+ |
ConcurrentLinkedDeque<Integer> d = new ConcurrentLinkedDeque<>(); |
922 |
+ |
|
923 |
+ |
boolean peek = rnd.nextBoolean(); |
924 |
+ |
Runnable getter = () -> { |
925 |
+ |
Integer x = peek ? d.peekFirst() : d.pollFirst(); |
926 |
+ |
if (x == null) nulls.increment(); |
927 |
+ |
else if (x == 0) zeros.increment(); |
928 |
+ |
else |
929 |
+ |
throw new AssertionError( |
930 |
+ |
String.format( |
931 |
+ |
"unexpected value %d after %d nulls and %d zeros", |
932 |
+ |
x, nulls.sum(), zeros.sum())); |
933 |
+ |
}; |
934 |
+ |
|
935 |
+ |
Runnable adder = () -> { |
936 |
+ |
d.addFirst(0); |
937 |
+ |
d.addLast(42); |
938 |
+ |
}; |
939 |
+ |
|
940 |
+ |
boolean b = rnd.nextBoolean(); |
941 |
+ |
Runnable r1 = b ? getter : adder; |
942 |
+ |
Runnable r2 = b ? adder : getter; |
943 |
+ |
CompletableFuture<Void> f1 = CompletableFuture.runAsync(r1); |
944 |
+ |
CompletableFuture<Void> f2 = CompletableFuture.runAsync(r2); |
945 |
+ |
f1.join(); |
946 |
+ |
f2.join(); |
947 |
+ |
} |
948 |
+ |
} |
949 |
+ |
|
950 |
+ |
/** |
951 |
+ |
* Reverse direction variant of testBug8188900 |
952 |
+ |
*/ |
953 |
+ |
public void testBug8188900_reverse() { |
954 |
+ |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
955 |
+ |
final LongAdder nulls = new LongAdder(), zeros = new LongAdder(); |
956 |
+ |
for (int n = expensiveTests ? 100_000 : 10; n--> 0; ) { |
957 |
+ |
ConcurrentLinkedDeque<Integer> d = new ConcurrentLinkedDeque<>(); |
958 |
+ |
|
959 |
+ |
boolean peek = rnd.nextBoolean(); |
960 |
+ |
Runnable getter = () -> { |
961 |
+ |
Integer x = peek ? d.peekLast() : d.pollLast(); |
962 |
+ |
if (x == null) nulls.increment(); |
963 |
+ |
else if (x == 0) zeros.increment(); |
964 |
+ |
else |
965 |
+ |
throw new AssertionError( |
966 |
+ |
String.format( |
967 |
+ |
"unexpected value %d after %d nulls and %d zeros", |
968 |
+ |
x, nulls.sum(), zeros.sum())); |
969 |
+ |
}; |
970 |
+ |
|
971 |
+ |
Runnable adder = () -> { |
972 |
+ |
d.addLast(0); |
973 |
+ |
d.addFirst(42); |
974 |
+ |
}; |
975 |
+ |
|
976 |
+ |
boolean b = rnd.nextBoolean(); |
977 |
+ |
Runnable r1 = b ? getter : adder; |
978 |
+ |
Runnable r2 = b ? adder : getter; |
979 |
+ |
CompletableFuture<Void> f1 = CompletableFuture.runAsync(r1); |
980 |
+ |
CompletableFuture<Void> f2 = CompletableFuture.runAsync(r2); |
981 |
+ |
f1.join(); |
982 |
+ |
f2.join(); |
983 |
+ |
} |
984 |
+ |
} |
985 |
|
} |