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 |
|
|
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
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>(); |
42 |
> |
private static ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
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 |
|
|
74 |
|
*/ |
75 |
|
public void testConstructor4() { |
76 |
|
try { |
77 |
< |
Integer[] ints = new Integer[SIZE]; |
69 |
< |
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
77 |
> |
new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE])); |
78 |
|
shouldThrow(); |
79 |
|
} catch (NullPointerException success) {} |
80 |
|
} |
83 |
|
* Initializing from Collection with some null elements throws NPE |
84 |
|
*/ |
85 |
|
public void testConstructor5() { |
86 |
+ |
Integer[] ints = new Integer[SIZE]; |
87 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
88 |
+ |
ints[i] = new Integer(i); |
89 |
|
try { |
79 |
– |
Integer[] ints = new Integer[SIZE]; |
80 |
– |
for (int i = 0; i < SIZE-1; ++i) |
81 |
– |
ints[i] = new Integer(i); |
90 |
|
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
91 |
|
shouldThrow(); |
92 |
|
} catch (NullPointerException success) {} |
124 |
|
public void testSize() { |
125 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
126 |
|
for (int i = 0; i < SIZE; ++i) { |
127 |
< |
assertEquals(SIZE-i, q.size()); |
127 |
> |
assertEquals(SIZE - i, q.size()); |
128 |
|
q.remove(); |
129 |
|
} |
130 |
|
for (int i = 0; i < SIZE; ++i) { |
137 |
|
* push(null) throws NPE |
138 |
|
*/ |
139 |
|
public void testPushNull() { |
140 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
141 |
|
try { |
133 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
142 |
|
q.push(null); |
143 |
|
shouldThrow(); |
144 |
|
} catch (NullPointerException success) {} |
172 |
|
* offer(null) throws NPE |
173 |
|
*/ |
174 |
|
public void testOfferNull() { |
175 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
176 |
|
try { |
168 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
177 |
|
q.offer(null); |
178 |
|
shouldThrow(); |
179 |
|
} catch (NullPointerException success) {} |
183 |
|
* offerFirst(null) throws NPE |
184 |
|
*/ |
185 |
|
public void testOfferFirstNull() { |
186 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
187 |
|
try { |
179 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
188 |
|
q.offerFirst(null); |
189 |
|
shouldThrow(); |
190 |
|
} catch (NullPointerException success) {} |
194 |
|
* offerLast(null) throws NPE |
195 |
|
*/ |
196 |
|
public void testOfferLastNull() { |
197 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
198 |
|
try { |
190 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
199 |
|
q.offerLast(null); |
200 |
|
shouldThrow(); |
201 |
|
} catch (NullPointerException success) {} |
238 |
|
* add(null) throws NPE |
239 |
|
*/ |
240 |
|
public void testAddNull() { |
241 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
242 |
|
try { |
234 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
243 |
|
q.add(null); |
244 |
|
shouldThrow(); |
245 |
|
} catch (NullPointerException success) {} |
249 |
|
* addFirst(null) throws NPE |
250 |
|
*/ |
251 |
|
public void testAddFirstNull() { |
252 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
253 |
|
try { |
245 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
254 |
|
q.addFirst(null); |
255 |
|
shouldThrow(); |
256 |
|
} catch (NullPointerException success) {} |
260 |
|
* addLast(null) throws NPE |
261 |
|
*/ |
262 |
|
public void testAddLastNull() { |
263 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
264 |
|
try { |
256 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
265 |
|
q.addLast(null); |
266 |
|
shouldThrow(); |
267 |
|
} catch (NullPointerException success) {} |
304 |
|
* addAll(null) throws NPE |
305 |
|
*/ |
306 |
|
public void testAddAll1() { |
307 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
308 |
|
try { |
300 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
309 |
|
q.addAll(null); |
310 |
|
shouldThrow(); |
311 |
|
} catch (NullPointerException success) {} |
315 |
|
* addAll(this) throws IAE |
316 |
|
*/ |
317 |
|
public void testAddAllSelf() { |
318 |
+ |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
319 |
|
try { |
311 |
– |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
320 |
|
q.addAll(q); |
321 |
|
shouldThrow(); |
322 |
|
} catch (IllegalArgumentException success) {} |
326 |
|
* addAll of a collection with null elements throws NPE |
327 |
|
*/ |
328 |
|
public void testAddAll2() { |
329 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
330 |
|
try { |
331 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
323 |
< |
Integer[] ints = new Integer[SIZE]; |
324 |
< |
q.addAll(Arrays.asList(ints)); |
331 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
332 |
|
shouldThrow(); |
333 |
|
} catch (NullPointerException success) {} |
334 |
|
} |
338 |
|
* possibly adding some elements |
339 |
|
*/ |
340 |
|
public void testAddAll3() { |
341 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
342 |
+ |
Integer[] ints = new Integer[SIZE]; |
343 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
344 |
+ |
ints[i] = new Integer(i); |
345 |
|
try { |
335 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
336 |
– |
Integer[] ints = new Integer[SIZE]; |
337 |
– |
for (int i = 0; i < SIZE-1; ++i) |
338 |
– |
ints[i] = new Integer(i); |
346 |
|
q.addAll(Arrays.asList(ints)); |
347 |
|
shouldThrow(); |
348 |
|
} catch (NullPointerException success) {} |
379 |
|
*/ |
380 |
|
public void testPollLast() { |
381 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
382 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
382 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
383 |
|
assertEquals(i, q.pollLast()); |
384 |
|
} |
385 |
|
assertNull(q.pollLast()); |
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 |
|
} |
479 |
|
*/ |
480 |
|
public void testPeekLast() { |
481 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
482 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
482 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
483 |
|
assertEquals(i, q.peekLast()); |
484 |
|
assertEquals(i, q.pollLast()); |
485 |
|
assertTrue(q.peekLast() == null || |
508 |
|
*/ |
509 |
|
public void testLastElement() { |
510 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
511 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
511 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
512 |
|
assertEquals(i, q.getLast()); |
513 |
|
assertEquals(i, q.pollLast()); |
514 |
|
} |
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 |
|
} |
633 |
|
assertTrue(changed); |
634 |
|
|
635 |
|
assertTrue(q.containsAll(p)); |
636 |
< |
assertEquals(SIZE-i, q.size()); |
636 |
> |
assertEquals(SIZE - i, q.size()); |
637 |
|
p.remove(); |
638 |
|
} |
639 |
|
} |
646 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
647 |
|
ConcurrentLinkedDeque p = populatedDeque(i); |
648 |
|
assertTrue(q.removeAll(p)); |
649 |
< |
assertEquals(SIZE-i, q.size()); |
649 |
> |
assertEquals(SIZE - i, q.size()); |
650 |
|
for (int j = 0; j < i; ++j) { |
651 |
|
Integer x = (Integer)(p.remove()); |
652 |
|
assertFalse(q.contains(x)); |
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 |
|
} |