16 |
|
import java.util.concurrent.ConcurrentLinkedDeque; |
17 |
|
|
18 |
|
import junit.framework.Test; |
19 |
– |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
21 |
|
|
24 |
|
} |
25 |
|
|
26 |
|
public static Test suite() { |
27 |
< |
return new TestSuite(ConcurrentLinkedDequeTest.class); |
27 |
> |
class Implementation implements CollectionImplementation { |
28 |
> |
public Class<?> klazz() { return ConcurrentLinkedDeque.class; } |
29 |
> |
public Collection emptyCollection() { return new ConcurrentLinkedDeque(); } |
30 |
> |
public Object makeElement(int i) { return i; } |
31 |
> |
public boolean isConcurrent() { return true; } |
32 |
> |
public boolean permitsNulls() { return false; } |
33 |
> |
} |
34 |
> |
return newTestSuite(ConcurrentLinkedDequeTest.class, |
35 |
> |
CollectionTest.testSuite(new Implementation())); |
36 |
|
} |
37 |
|
|
38 |
|
/** |
39 |
|
* Returns a new deque of given size containing consecutive |
40 |
< |
* Integers 0 ... n. |
40 |
> |
* Integers 0 ... n - 1. |
41 |
|
*/ |
42 |
|
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
43 |
< |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>(); |
43 |
> |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>(); |
44 |
|
assertTrue(q.isEmpty()); |
45 |
|
for (int i = 0; i < n; ++i) |
46 |
|
assertTrue(q.offer(new Integer(i))); |
47 |
|
assertFalse(q.isEmpty()); |
48 |
|
assertEquals(n, q.size()); |
49 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
50 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
51 |
|
return q; |
52 |
|
} |
53 |
|
|
448 |
|
assertTrue(q.contains(i)); |
449 |
|
assertTrue(q.remove(i)); |
450 |
|
assertFalse(q.contains(i)); |
451 |
< |
assertTrue(q.contains(i-1)); |
451 |
> |
assertTrue(q.contains(i - 1)); |
452 |
|
} |
453 |
|
for (int i = 0; i < SIZE; i += 2) { |
454 |
|
assertTrue(q.contains(i)); |
455 |
|
assertTrue(q.remove(i)); |
456 |
|
assertFalse(q.contains(i)); |
457 |
< |
assertFalse(q.remove(i+1)); |
458 |
< |
assertFalse(q.contains(i+1)); |
457 |
> |
assertFalse(q.remove(i + 1)); |
458 |
> |
assertFalse(q.contains(i + 1)); |
459 |
|
} |
460 |
|
assertTrue(q.isEmpty()); |
461 |
|
} |
559 |
|
} |
560 |
|
for (int i = 0; i < SIZE; i += 2) { |
561 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
562 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
562 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
563 |
|
} |
564 |
|
assertTrue(q.isEmpty()); |
565 |
|
} |
574 |
|
} |
575 |
|
for (int i = 0; i < SIZE; i += 2) { |
576 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
577 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
577 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
578 |
|
} |
579 |
|
assertTrue(q.isEmpty()); |
580 |
|
} |
762 |
|
final Random rng = new Random(); |
763 |
|
for (int iters = 0; iters < 100; ++iters) { |
764 |
|
int max = rng.nextInt(5) + 2; |
765 |
< |
int split = rng.nextInt(max-1) + 1; |
765 |
> |
int split = rng.nextInt(max - 1) + 1; |
766 |
|
for (int j = 1; j <= max; ++j) |
767 |
|
q.add(new Integer(j)); |
768 |
|
Iterator it = q.iterator(); |
769 |
|
for (int j = 1; j <= split; ++j) |
770 |
|
assertEquals(it.next(), new Integer(j)); |
771 |
|
it.remove(); |
772 |
< |
assertEquals(it.next(), new Integer(split+1)); |
772 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
773 |
|
for (int j = 1; j <= split; ++j) |
774 |
|
q.remove(new Integer(j)); |
775 |
|
it = q.iterator(); |
776 |
< |
for (int j = split+1; j <= max; ++j) { |
776 |
> |
for (int j = split + 1; j <= max; ++j) { |
777 |
|
assertEquals(it.next(), new Integer(j)); |
778 |
|
it.remove(); |
779 |
|
} |
830 |
|
final Random rng = new Random(); |
831 |
|
for (int iters = 0; iters < 100; ++iters) { |
832 |
|
int max = rng.nextInt(5) + 2; |
833 |
< |
int split = rng.nextInt(max-1) + 1; |
833 |
> |
int split = rng.nextInt(max - 1) + 1; |
834 |
|
for (int j = max; j >= 1; --j) |
835 |
|
q.add(new Integer(j)); |
836 |
|
Iterator it = q.descendingIterator(); |
837 |
|
for (int j = 1; j <= split; ++j) |
838 |
|
assertEquals(it.next(), new Integer(j)); |
839 |
|
it.remove(); |
840 |
< |
assertEquals(it.next(), new Integer(split+1)); |
840 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
841 |
|
for (int j = 1; j <= split; ++j) |
842 |
|
q.remove(new Integer(j)); |
843 |
|
it = q.descendingIterator(); |
844 |
< |
for (int j = split+1; j <= max; ++j) { |
844 |
> |
for (int j = split + 1; j <= max; ++j) { |
845 |
|
assertEquals(it.next(), new Integer(j)); |
846 |
|
it.remove(); |
847 |
|
} |