21 |
|
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
22 |
|
|
23 |
|
public static void main(String[] args) { |
24 |
< |
junit.textui.TestRunner.run(suite()); |
24 |
> |
main(suite(), args); |
25 |
|
} |
26 |
|
|
27 |
|
public static Test suite() { |
28 |
< |
return new TestSuite(ConcurrentLinkedDequeTest.class); |
28 |
> |
class Implementation implements CollectionImplementation { |
29 |
> |
public Class<?> klazz() { return ConcurrentLinkedDeque.class; } |
30 |
> |
public Collection emptyCollection() { return new ConcurrentLinkedDeque(); } |
31 |
> |
public Object makeElement(int i) { return i; } |
32 |
> |
public boolean isConcurrent() { return true; } |
33 |
> |
public boolean permitsNulls() { return false; } |
34 |
> |
} |
35 |
> |
return newTestSuite(ConcurrentLinkedDequeTest.class, |
36 |
> |
CollectionTest.testSuite(new Implementation())); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
|
* Returns a new deque of given size containing consecutive |
41 |
< |
* Integers 0 ... n. |
41 |
> |
* Integers 0 ... n - 1. |
42 |
|
*/ |
43 |
|
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
44 |
< |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>(); |
44 |
> |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>(); |
45 |
|
assertTrue(q.isEmpty()); |
46 |
|
for (int i = 0; i < n; ++i) |
47 |
|
assertTrue(q.offer(new Integer(i))); |
48 |
|
assertFalse(q.isEmpty()); |
49 |
|
assertEquals(n, q.size()); |
50 |
+ |
assertEquals((Integer) 0, q.peekFirst()); |
51 |
+ |
assertEquals((Integer) (n - 1), q.peekLast()); |
52 |
|
return q; |
53 |
|
} |
54 |
|
|
65 |
|
*/ |
66 |
|
public void testConstructor3() { |
67 |
|
try { |
68 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null); |
68 |
> |
new ConcurrentLinkedDeque((Collection)null); |
69 |
|
shouldThrow(); |
70 |
|
} catch (NullPointerException success) {} |
71 |
|
} |
75 |
|
*/ |
76 |
|
public void testConstructor4() { |
77 |
|
try { |
78 |
< |
Integer[] ints = new Integer[SIZE]; |
69 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); |
78 |
> |
new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE])); |
79 |
|
shouldThrow(); |
80 |
|
} catch (NullPointerException success) {} |
81 |
|
} |
84 |
|
* Initializing from Collection with some null elements throws NPE |
85 |
|
*/ |
86 |
|
public void testConstructor5() { |
87 |
+ |
Integer[] ints = new Integer[SIZE]; |
88 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
89 |
+ |
ints[i] = new Integer(i); |
90 |
|
try { |
91 |
< |
Integer[] ints = new Integer[SIZE]; |
80 |
< |
for (int i = 0; i < SIZE-1; ++i) |
81 |
< |
ints[i] = new Integer(i); |
82 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); |
91 |
> |
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
92 |
|
shouldThrow(); |
93 |
|
} catch (NullPointerException success) {} |
94 |
|
} |
125 |
|
public void testSize() { |
126 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
127 |
|
for (int i = 0; i < SIZE; ++i) { |
128 |
< |
assertEquals(SIZE-i, q.size()); |
128 |
> |
assertEquals(SIZE - i, q.size()); |
129 |
|
q.remove(); |
130 |
|
} |
131 |
|
for (int i = 0; i < SIZE; ++i) { |
138 |
|
* push(null) throws NPE |
139 |
|
*/ |
140 |
|
public void testPushNull() { |
141 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
142 |
|
try { |
133 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
143 |
|
q.push(null); |
144 |
|
shouldThrow(); |
145 |
|
} catch (NullPointerException success) {} |
173 |
|
* offer(null) throws NPE |
174 |
|
*/ |
175 |
|
public void testOfferNull() { |
176 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
177 |
|
try { |
168 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
178 |
|
q.offer(null); |
179 |
|
shouldThrow(); |
180 |
|
} catch (NullPointerException success) {} |
184 |
|
* offerFirst(null) throws NPE |
185 |
|
*/ |
186 |
|
public void testOfferFirstNull() { |
187 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
188 |
|
try { |
179 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
189 |
|
q.offerFirst(null); |
190 |
|
shouldThrow(); |
191 |
|
} catch (NullPointerException success) {} |
195 |
|
* offerLast(null) throws NPE |
196 |
|
*/ |
197 |
|
public void testOfferLastNull() { |
198 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
199 |
|
try { |
190 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
200 |
|
q.offerLast(null); |
201 |
|
shouldThrow(); |
202 |
|
} catch (NullPointerException success) {} |
239 |
|
* add(null) throws NPE |
240 |
|
*/ |
241 |
|
public void testAddNull() { |
242 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
243 |
|
try { |
234 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
244 |
|
q.add(null); |
245 |
|
shouldThrow(); |
246 |
|
} catch (NullPointerException success) {} |
250 |
|
* addFirst(null) throws NPE |
251 |
|
*/ |
252 |
|
public void testAddFirstNull() { |
253 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
254 |
|
try { |
245 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
255 |
|
q.addFirst(null); |
256 |
|
shouldThrow(); |
257 |
|
} catch (NullPointerException success) {} |
261 |
|
* addLast(null) throws NPE |
262 |
|
*/ |
263 |
|
public void testAddLastNull() { |
264 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
265 |
|
try { |
256 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
266 |
|
q.addLast(null); |
267 |
|
shouldThrow(); |
268 |
|
} catch (NullPointerException success) {} |
305 |
|
* addAll(null) throws NPE |
306 |
|
*/ |
307 |
|
public void testAddAll1() { |
308 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
309 |
|
try { |
300 |
– |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
310 |
|
q.addAll(null); |
311 |
|
shouldThrow(); |
312 |
|
} catch (NullPointerException success) {} |
316 |
|
* addAll(this) throws IAE |
317 |
|
*/ |
318 |
|
public void testAddAllSelf() { |
319 |
+ |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
320 |
|
try { |
311 |
– |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
321 |
|
q.addAll(q); |
322 |
|
shouldThrow(); |
323 |
|
} catch (IllegalArgumentException success) {} |
327 |
|
* addAll of a collection with null elements throws NPE |
328 |
|
*/ |
329 |
|
public void testAddAll2() { |
330 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
331 |
|
try { |
332 |
< |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
323 |
< |
Integer[] ints = new Integer[SIZE]; |
324 |
< |
q.addAll(Arrays.asList(ints)); |
332 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
333 |
|
shouldThrow(); |
334 |
|
} catch (NullPointerException success) {} |
335 |
|
} |
339 |
|
* possibly adding some elements |
340 |
|
*/ |
341 |
|
public void testAddAll3() { |
342 |
+ |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
343 |
+ |
Integer[] ints = new Integer[SIZE]; |
344 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
345 |
+ |
ints[i] = new Integer(i); |
346 |
|
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); |
347 |
|
q.addAll(Arrays.asList(ints)); |
348 |
|
shouldThrow(); |
349 |
|
} catch (NullPointerException success) {} |
380 |
|
*/ |
381 |
|
public void testPollLast() { |
382 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
383 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
383 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
384 |
|
assertEquals(i, q.pollLast()); |
385 |
|
} |
386 |
|
assertNull(q.pollLast()); |
449 |
|
assertTrue(q.contains(i)); |
450 |
|
assertTrue(q.remove(i)); |
451 |
|
assertFalse(q.contains(i)); |
452 |
< |
assertTrue(q.contains(i-1)); |
452 |
> |
assertTrue(q.contains(i - 1)); |
453 |
|
} |
454 |
|
for (int i = 0; i < SIZE; i += 2) { |
455 |
|
assertTrue(q.contains(i)); |
456 |
|
assertTrue(q.remove(i)); |
457 |
|
assertFalse(q.contains(i)); |
458 |
< |
assertFalse(q.remove(i+1)); |
459 |
< |
assertFalse(q.contains(i+1)); |
458 |
> |
assertFalse(q.remove(i + 1)); |
459 |
> |
assertFalse(q.contains(i + 1)); |
460 |
|
} |
461 |
|
assertTrue(q.isEmpty()); |
462 |
|
} |
480 |
|
*/ |
481 |
|
public void testPeekLast() { |
482 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
483 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
483 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
484 |
|
assertEquals(i, q.peekLast()); |
485 |
|
assertEquals(i, q.pollLast()); |
486 |
|
assertTrue(q.peekLast() == null || |
509 |
|
*/ |
510 |
|
public void testLastElement() { |
511 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
512 |
< |
for (int i = SIZE-1; i >= 0; --i) { |
512 |
> |
for (int i = SIZE - 1; i >= 0; --i) { |
513 |
|
assertEquals(i, q.getLast()); |
514 |
|
assertEquals(i, q.pollLast()); |
515 |
|
} |
560 |
|
} |
561 |
|
for (int i = 0; i < SIZE; i += 2) { |
562 |
|
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
563 |
< |
assertFalse(q.removeFirstOccurrence(new Integer(i+1))); |
563 |
> |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
564 |
|
} |
565 |
|
assertTrue(q.isEmpty()); |
566 |
|
} |
575 |
|
} |
576 |
|
for (int i = 0; i < SIZE; i += 2) { |
577 |
|
assertTrue(q.removeLastOccurrence(new Integer(i))); |
578 |
< |
assertFalse(q.removeLastOccurrence(new Integer(i+1))); |
578 |
> |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
579 |
|
} |
580 |
|
assertTrue(q.isEmpty()); |
581 |
|
} |
634 |
|
assertTrue(changed); |
635 |
|
|
636 |
|
assertTrue(q.containsAll(p)); |
637 |
< |
assertEquals(SIZE-i, q.size()); |
637 |
> |
assertEquals(SIZE - i, q.size()); |
638 |
|
p.remove(); |
639 |
|
} |
640 |
|
} |
647 |
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
648 |
|
ConcurrentLinkedDeque p = populatedDeque(i); |
649 |
|
assertTrue(q.removeAll(p)); |
650 |
< |
assertEquals(SIZE-i, q.size()); |
650 |
> |
assertEquals(SIZE - i, q.size()); |
651 |
|
for (int j = 0; j < i; ++j) { |
652 |
|
Integer x = (Integer)(p.remove()); |
653 |
|
assertFalse(q.contains(x)); |
763 |
|
final Random rng = new Random(); |
764 |
|
for (int iters = 0; iters < 100; ++iters) { |
765 |
|
int max = rng.nextInt(5) + 2; |
766 |
< |
int split = rng.nextInt(max-1) + 1; |
766 |
> |
int split = rng.nextInt(max - 1) + 1; |
767 |
|
for (int j = 1; j <= max; ++j) |
768 |
|
q.add(new Integer(j)); |
769 |
|
Iterator it = q.iterator(); |
770 |
|
for (int j = 1; j <= split; ++j) |
771 |
|
assertEquals(it.next(), new Integer(j)); |
772 |
|
it.remove(); |
773 |
< |
assertEquals(it.next(), new Integer(split+1)); |
773 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
774 |
|
for (int j = 1; j <= split; ++j) |
775 |
|
q.remove(new Integer(j)); |
776 |
|
it = q.iterator(); |
777 |
< |
for (int j = split+1; j <= max; ++j) { |
777 |
> |
for (int j = split + 1; j <= max; ++j) { |
778 |
|
assertEquals(it.next(), new Integer(j)); |
779 |
|
it.remove(); |
780 |
|
} |
831 |
|
final Random rng = new Random(); |
832 |
|
for (int iters = 0; iters < 100; ++iters) { |
833 |
|
int max = rng.nextInt(5) + 2; |
834 |
< |
int split = rng.nextInt(max-1) + 1; |
834 |
> |
int split = rng.nextInt(max - 1) + 1; |
835 |
|
for (int j = max; j >= 1; --j) |
836 |
|
q.add(new Integer(j)); |
837 |
|
Iterator it = q.descendingIterator(); |
838 |
|
for (int j = 1; j <= split; ++j) |
839 |
|
assertEquals(it.next(), new Integer(j)); |
840 |
|
it.remove(); |
841 |
< |
assertEquals(it.next(), new Integer(split+1)); |
841 |
> |
assertEquals(it.next(), new Integer(split + 1)); |
842 |
|
for (int j = 1; j <= split; ++j) |
843 |
|
q.remove(new Integer(j)); |
844 |
|
it = q.descendingIterator(); |
845 |
< |
for (int j = split+1; j <= max; ++j) { |
845 |
> |
for (int j = split + 1; j <= max; ++j) { |
846 |
|
assertEquals(it.next(), new Integer(j)); |
847 |
|
it.remove(); |
848 |
|
} |