1 |
jsr166 |
1.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 |
jsr166 |
1.7 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.1 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
|
|
*/ |
8 |
|
|
|
9 |
jsr166 |
1.9 |
import java.util.Arrays; |
10 |
|
|
import java.util.Collection; |
11 |
jsr166 |
1.13 |
import java.util.Deque; |
12 |
jsr166 |
1.9 |
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 |
jsr166 |
1.1 |
|
18 |
jsr166 |
1.14 |
import junit.framework.Test; |
19 |
|
|
import junit.framework.TestSuite; |
20 |
|
|
|
21 |
jsr166 |
1.1 |
public class ConcurrentLinkedDequeTest extends JSR166TestCase { |
22 |
|
|
|
23 |
|
|
public static void main(String[] args) { |
24 |
jsr166 |
1.19 |
main(suite(), args); |
25 |
jsr166 |
1.1 |
} |
26 |
|
|
|
27 |
|
|
public static Test suite() { |
28 |
jsr166 |
1.23 |
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 |
jsr166 |
1.1 |
} |
38 |
|
|
|
39 |
|
|
/** |
40 |
jsr166 |
1.11 |
* Returns a new deque of given size containing consecutive |
41 |
jsr166 |
1.24 |
* Integers 0 ... n - 1. |
42 |
jsr166 |
1.1 |
*/ |
43 |
jsr166 |
1.5 |
private ConcurrentLinkedDeque<Integer> populatedDeque(int n) { |
44 |
jsr166 |
1.25 |
ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>(); |
45 |
jsr166 |
1.1 |
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 |
jsr166 |
1.24 |
assertEquals((Integer) 0, q.peekFirst()); |
51 |
|
|
assertEquals((Integer) (n - 1), q.peekLast()); |
52 |
jsr166 |
1.1 |
return q; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
/** |
56 |
|
|
* new deque is empty |
57 |
|
|
*/ |
58 |
|
|
public void testConstructor1() { |
59 |
|
|
assertTrue(new ConcurrentLinkedDeque().isEmpty()); |
60 |
|
|
assertEquals(0, new ConcurrentLinkedDeque().size()); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/** |
64 |
|
|
* Initializing from null Collection throws NPE |
65 |
|
|
*/ |
66 |
|
|
public void testConstructor3() { |
67 |
|
|
try { |
68 |
jsr166 |
1.18 |
new ConcurrentLinkedDeque((Collection)null); |
69 |
jsr166 |
1.1 |
shouldThrow(); |
70 |
|
|
} catch (NullPointerException success) {} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/** |
74 |
|
|
* Initializing from Collection of null elements throws NPE |
75 |
|
|
*/ |
76 |
|
|
public void testConstructor4() { |
77 |
|
|
try { |
78 |
jsr166 |
1.20 |
new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE])); |
79 |
jsr166 |
1.1 |
shouldThrow(); |
80 |
|
|
} catch (NullPointerException success) {} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
/** |
84 |
|
|
* Initializing from Collection with some null elements throws NPE |
85 |
|
|
*/ |
86 |
|
|
public void testConstructor5() { |
87 |
jsr166 |
1.20 |
Integer[] ints = new Integer[SIZE]; |
88 |
jsr166 |
1.21 |
for (int i = 0; i < SIZE - 1; ++i) |
89 |
jsr166 |
1.20 |
ints[i] = new Integer(i); |
90 |
jsr166 |
1.1 |
try { |
91 |
jsr166 |
1.18 |
new ConcurrentLinkedDeque(Arrays.asList(ints)); |
92 |
jsr166 |
1.1 |
shouldThrow(); |
93 |
|
|
} catch (NullPointerException success) {} |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/** |
97 |
|
|
* Deque contains all elements of collection used to initialize |
98 |
|
|
*/ |
99 |
|
|
public void testConstructor6() { |
100 |
|
|
Integer[] ints = new Integer[SIZE]; |
101 |
|
|
for (int i = 0; i < SIZE; ++i) |
102 |
|
|
ints[i] = new Integer(i); |
103 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints)); |
104 |
|
|
for (int i = 0; i < SIZE; ++i) |
105 |
|
|
assertEquals(ints[i], q.poll()); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
/** |
109 |
|
|
* isEmpty is true before add, false after |
110 |
|
|
*/ |
111 |
|
|
public void testEmpty() { |
112 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
113 |
|
|
assertTrue(q.isEmpty()); |
114 |
|
|
q.add(one); |
115 |
|
|
assertFalse(q.isEmpty()); |
116 |
|
|
q.add(two); |
117 |
|
|
q.remove(); |
118 |
|
|
q.remove(); |
119 |
|
|
assertTrue(q.isEmpty()); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
/** |
123 |
|
|
* size() changes when elements added and removed |
124 |
|
|
*/ |
125 |
|
|
public void testSize() { |
126 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
127 |
|
|
for (int i = 0; i < SIZE; ++i) { |
128 |
jsr166 |
1.21 |
assertEquals(SIZE - i, q.size()); |
129 |
jsr166 |
1.1 |
q.remove(); |
130 |
|
|
} |
131 |
|
|
for (int i = 0; i < SIZE; ++i) { |
132 |
|
|
assertEquals(i, q.size()); |
133 |
|
|
q.add(new Integer(i)); |
134 |
|
|
} |
135 |
|
|
} |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* push(null) throws NPE |
139 |
|
|
*/ |
140 |
|
|
public void testPushNull() { |
141 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
142 |
jsr166 |
1.1 |
try { |
143 |
|
|
q.push(null); |
144 |
|
|
shouldThrow(); |
145 |
|
|
} catch (NullPointerException success) {} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
/** |
149 |
|
|
* peekFirst() returns element inserted with push |
150 |
|
|
*/ |
151 |
|
|
public void testPush() { |
152 |
|
|
ConcurrentLinkedDeque q = populatedDeque(3); |
153 |
|
|
q.pollLast(); |
154 |
|
|
q.push(four); |
155 |
|
|
assertSame(four, q.peekFirst()); |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
/** |
159 |
|
|
* pop() removes first element, or throws NSEE if empty |
160 |
|
|
*/ |
161 |
|
|
public void testPop() { |
162 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
163 |
|
|
for (int i = 0; i < SIZE; ++i) { |
164 |
|
|
assertEquals(i, q.pop()); |
165 |
|
|
} |
166 |
|
|
try { |
167 |
|
|
q.pop(); |
168 |
|
|
shouldThrow(); |
169 |
|
|
} catch (NoSuchElementException success) {} |
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
/** |
173 |
|
|
* offer(null) throws NPE |
174 |
|
|
*/ |
175 |
|
|
public void testOfferNull() { |
176 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
177 |
jsr166 |
1.1 |
try { |
178 |
|
|
q.offer(null); |
179 |
|
|
shouldThrow(); |
180 |
|
|
} catch (NullPointerException success) {} |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
/** |
184 |
|
|
* offerFirst(null) throws NPE |
185 |
|
|
*/ |
186 |
|
|
public void testOfferFirstNull() { |
187 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
188 |
jsr166 |
1.1 |
try { |
189 |
|
|
q.offerFirst(null); |
190 |
|
|
shouldThrow(); |
191 |
|
|
} catch (NullPointerException success) {} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
/** |
195 |
|
|
* offerLast(null) throws NPE |
196 |
|
|
*/ |
197 |
|
|
public void testOfferLastNull() { |
198 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
199 |
jsr166 |
1.1 |
try { |
200 |
|
|
q.offerLast(null); |
201 |
|
|
shouldThrow(); |
202 |
|
|
} catch (NullPointerException success) {} |
203 |
|
|
} |
204 |
|
|
|
205 |
|
|
/** |
206 |
|
|
* offer(x) succeeds |
207 |
|
|
*/ |
208 |
|
|
public void testOffer() { |
209 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
210 |
|
|
assertTrue(q.offer(zero)); |
211 |
|
|
assertTrue(q.offer(one)); |
212 |
|
|
assertSame(zero, q.peekFirst()); |
213 |
|
|
assertSame(one, q.peekLast()); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
/** |
217 |
|
|
* offerFirst(x) succeeds |
218 |
|
|
*/ |
219 |
|
|
public void testOfferFirst() { |
220 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
221 |
|
|
assertTrue(q.offerFirst(zero)); |
222 |
|
|
assertTrue(q.offerFirst(one)); |
223 |
|
|
assertSame(one, q.peekFirst()); |
224 |
|
|
assertSame(zero, q.peekLast()); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
/** |
228 |
|
|
* offerLast(x) succeeds |
229 |
|
|
*/ |
230 |
|
|
public void testOfferLast() { |
231 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
232 |
|
|
assertTrue(q.offerLast(zero)); |
233 |
|
|
assertTrue(q.offerLast(one)); |
234 |
|
|
assertSame(zero, q.peekFirst()); |
235 |
|
|
assertSame(one, q.peekLast()); |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
/** |
239 |
|
|
* add(null) throws NPE |
240 |
|
|
*/ |
241 |
|
|
public void testAddNull() { |
242 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
243 |
jsr166 |
1.1 |
try { |
244 |
|
|
q.add(null); |
245 |
|
|
shouldThrow(); |
246 |
|
|
} catch (NullPointerException success) {} |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
/** |
250 |
|
|
* addFirst(null) throws NPE |
251 |
|
|
*/ |
252 |
|
|
public void testAddFirstNull() { |
253 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
254 |
jsr166 |
1.1 |
try { |
255 |
|
|
q.addFirst(null); |
256 |
|
|
shouldThrow(); |
257 |
|
|
} catch (NullPointerException success) {} |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
/** |
261 |
|
|
* addLast(null) throws NPE |
262 |
|
|
*/ |
263 |
|
|
public void testAddLastNull() { |
264 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
265 |
jsr166 |
1.1 |
try { |
266 |
|
|
q.addLast(null); |
267 |
|
|
shouldThrow(); |
268 |
|
|
} catch (NullPointerException success) {} |
269 |
|
|
} |
270 |
|
|
|
271 |
|
|
/** |
272 |
|
|
* add(x) succeeds |
273 |
|
|
*/ |
274 |
|
|
public void testAdd() { |
275 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
276 |
|
|
assertTrue(q.add(zero)); |
277 |
|
|
assertTrue(q.add(one)); |
278 |
|
|
assertSame(zero, q.peekFirst()); |
279 |
|
|
assertSame(one, q.peekLast()); |
280 |
|
|
} |
281 |
|
|
|
282 |
|
|
/** |
283 |
|
|
* addFirst(x) succeeds |
284 |
|
|
*/ |
285 |
|
|
public void testAddFirst() { |
286 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
287 |
|
|
q.addFirst(zero); |
288 |
|
|
q.addFirst(one); |
289 |
|
|
assertSame(one, q.peekFirst()); |
290 |
|
|
assertSame(zero, q.peekLast()); |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
/** |
294 |
|
|
* addLast(x) succeeds |
295 |
|
|
*/ |
296 |
|
|
public void testAddLast() { |
297 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
298 |
|
|
q.addLast(zero); |
299 |
|
|
q.addLast(one); |
300 |
|
|
assertSame(zero, q.peekFirst()); |
301 |
|
|
assertSame(one, q.peekLast()); |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
/** |
305 |
|
|
* addAll(null) throws NPE |
306 |
|
|
*/ |
307 |
|
|
public void testAddAll1() { |
308 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
309 |
jsr166 |
1.1 |
try { |
310 |
|
|
q.addAll(null); |
311 |
|
|
shouldThrow(); |
312 |
|
|
} catch (NullPointerException success) {} |
313 |
|
|
} |
314 |
|
|
|
315 |
|
|
/** |
316 |
|
|
* addAll(this) throws IAE |
317 |
|
|
*/ |
318 |
|
|
public void testAddAllSelf() { |
319 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
320 |
jsr166 |
1.1 |
try { |
321 |
|
|
q.addAll(q); |
322 |
|
|
shouldThrow(); |
323 |
|
|
} catch (IllegalArgumentException success) {} |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
/** |
327 |
|
|
* addAll of a collection with null elements throws NPE |
328 |
|
|
*/ |
329 |
|
|
public void testAddAll2() { |
330 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
331 |
jsr166 |
1.1 |
try { |
332 |
jsr166 |
1.20 |
q.addAll(Arrays.asList(new Integer[SIZE])); |
333 |
jsr166 |
1.1 |
shouldThrow(); |
334 |
|
|
} catch (NullPointerException success) {} |
335 |
|
|
} |
336 |
|
|
|
337 |
|
|
/** |
338 |
|
|
* addAll of a collection with any null elements throws NPE after |
339 |
|
|
* possibly adding some elements |
340 |
|
|
*/ |
341 |
|
|
public void testAddAll3() { |
342 |
jsr166 |
1.20 |
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
343 |
|
|
Integer[] ints = new Integer[SIZE]; |
344 |
jsr166 |
1.21 |
for (int i = 0; i < SIZE - 1; ++i) |
345 |
jsr166 |
1.20 |
ints[i] = new Integer(i); |
346 |
jsr166 |
1.1 |
try { |
347 |
|
|
q.addAll(Arrays.asList(ints)); |
348 |
|
|
shouldThrow(); |
349 |
|
|
} catch (NullPointerException success) {} |
350 |
|
|
} |
351 |
|
|
|
352 |
|
|
/** |
353 |
|
|
* Deque contains all elements, in traversal order, of successful addAll |
354 |
|
|
*/ |
355 |
|
|
public void testAddAll5() { |
356 |
|
|
Integer[] empty = new Integer[0]; |
357 |
|
|
Integer[] ints = new Integer[SIZE]; |
358 |
|
|
for (int i = 0; i < SIZE; ++i) |
359 |
|
|
ints[i] = new Integer(i); |
360 |
|
|
ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
361 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
362 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
363 |
|
|
for (int i = 0; i < SIZE; ++i) |
364 |
|
|
assertEquals(ints[i], q.poll()); |
365 |
|
|
} |
366 |
|
|
|
367 |
|
|
/** |
368 |
|
|
* pollFirst() succeeds unless empty |
369 |
|
|
*/ |
370 |
|
|
public void testPollFirst() { |
371 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
372 |
|
|
for (int i = 0; i < SIZE; ++i) { |
373 |
|
|
assertEquals(i, q.pollFirst()); |
374 |
|
|
} |
375 |
|
|
assertNull(q.pollFirst()); |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
/** |
379 |
|
|
* pollLast() succeeds unless empty |
380 |
|
|
*/ |
381 |
|
|
public void testPollLast() { |
382 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
383 |
jsr166 |
1.21 |
for (int i = SIZE - 1; i >= 0; --i) { |
384 |
jsr166 |
1.1 |
assertEquals(i, q.pollLast()); |
385 |
|
|
} |
386 |
|
|
assertNull(q.pollLast()); |
387 |
|
|
} |
388 |
|
|
|
389 |
|
|
/** |
390 |
|
|
* poll() succeeds unless empty |
391 |
|
|
*/ |
392 |
|
|
public void testPoll() { |
393 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
394 |
|
|
for (int i = 0; i < SIZE; ++i) { |
395 |
|
|
assertEquals(i, q.poll()); |
396 |
|
|
} |
397 |
|
|
assertNull(q.poll()); |
398 |
|
|
} |
399 |
|
|
|
400 |
|
|
/** |
401 |
|
|
* peek() returns next element, or null if empty |
402 |
|
|
*/ |
403 |
|
|
public void testPeek() { |
404 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
405 |
|
|
for (int i = 0; i < SIZE; ++i) { |
406 |
|
|
assertEquals(i, q.peek()); |
407 |
|
|
assertEquals(i, q.poll()); |
408 |
|
|
assertTrue(q.peek() == null || |
409 |
|
|
!q.peek().equals(i)); |
410 |
|
|
} |
411 |
|
|
assertNull(q.peek()); |
412 |
|
|
} |
413 |
|
|
|
414 |
|
|
/** |
415 |
|
|
* element() returns first element, or throws NSEE if empty |
416 |
|
|
*/ |
417 |
|
|
public void testElement() { |
418 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
419 |
|
|
for (int i = 0; i < SIZE; ++i) { |
420 |
|
|
assertEquals(i, q.element()); |
421 |
|
|
assertEquals(i, q.poll()); |
422 |
|
|
} |
423 |
|
|
try { |
424 |
|
|
q.element(); |
425 |
|
|
shouldThrow(); |
426 |
|
|
} catch (NoSuchElementException success) {} |
427 |
|
|
} |
428 |
|
|
|
429 |
|
|
/** |
430 |
|
|
* remove() removes next element, or throws NSEE if empty |
431 |
|
|
*/ |
432 |
|
|
public void testRemove() { |
433 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
434 |
|
|
for (int i = 0; i < SIZE; ++i) { |
435 |
|
|
assertEquals(i, q.remove()); |
436 |
|
|
} |
437 |
|
|
try { |
438 |
|
|
q.remove(); |
439 |
|
|
shouldThrow(); |
440 |
|
|
} catch (NoSuchElementException success) {} |
441 |
|
|
} |
442 |
|
|
|
443 |
|
|
/** |
444 |
|
|
* remove(x) removes x and returns true if present |
445 |
|
|
*/ |
446 |
|
|
public void testRemoveElement() { |
447 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
448 |
jsr166 |
1.15 |
for (int i = 1; i < SIZE; i += 2) { |
449 |
jsr166 |
1.6 |
assertTrue(q.contains(i)); |
450 |
|
|
assertTrue(q.remove(i)); |
451 |
|
|
assertFalse(q.contains(i)); |
452 |
jsr166 |
1.22 |
assertTrue(q.contains(i - 1)); |
453 |
jsr166 |
1.1 |
} |
454 |
jsr166 |
1.15 |
for (int i = 0; i < SIZE; i += 2) { |
455 |
jsr166 |
1.6 |
assertTrue(q.contains(i)); |
456 |
|
|
assertTrue(q.remove(i)); |
457 |
|
|
assertFalse(q.contains(i)); |
458 |
jsr166 |
1.22 |
assertFalse(q.remove(i + 1)); |
459 |
|
|
assertFalse(q.contains(i + 1)); |
460 |
jsr166 |
1.1 |
} |
461 |
|
|
assertTrue(q.isEmpty()); |
462 |
|
|
} |
463 |
|
|
|
464 |
|
|
/** |
465 |
|
|
* peekFirst() returns next element, or null if empty |
466 |
|
|
*/ |
467 |
|
|
public void testPeekFirst() { |
468 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
469 |
|
|
for (int i = 0; i < SIZE; ++i) { |
470 |
|
|
assertEquals(i, q.peekFirst()); |
471 |
|
|
assertEquals(i, q.pollFirst()); |
472 |
|
|
assertTrue(q.peekFirst() == null || |
473 |
|
|
!q.peekFirst().equals(i)); |
474 |
|
|
} |
475 |
|
|
assertNull(q.peekFirst()); |
476 |
|
|
} |
477 |
|
|
|
478 |
|
|
/** |
479 |
|
|
* peekLast() returns next element, or null if empty |
480 |
|
|
*/ |
481 |
|
|
public void testPeekLast() { |
482 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
483 |
jsr166 |
1.21 |
for (int i = SIZE - 1; i >= 0; --i) { |
484 |
jsr166 |
1.1 |
assertEquals(i, q.peekLast()); |
485 |
|
|
assertEquals(i, q.pollLast()); |
486 |
|
|
assertTrue(q.peekLast() == null || |
487 |
|
|
!q.peekLast().equals(i)); |
488 |
|
|
} |
489 |
|
|
assertNull(q.peekLast()); |
490 |
|
|
} |
491 |
|
|
|
492 |
|
|
/** |
493 |
|
|
* getFirst() returns first element, or throws NSEE if empty |
494 |
|
|
*/ |
495 |
|
|
public void testFirstElement() { |
496 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
497 |
|
|
for (int i = 0; i < SIZE; ++i) { |
498 |
|
|
assertEquals(i, q.getFirst()); |
499 |
|
|
assertEquals(i, q.pollFirst()); |
500 |
|
|
} |
501 |
|
|
try { |
502 |
|
|
q.getFirst(); |
503 |
|
|
shouldThrow(); |
504 |
|
|
} catch (NoSuchElementException success) {} |
505 |
|
|
} |
506 |
|
|
|
507 |
|
|
/** |
508 |
|
|
* getLast() returns last element, or throws NSEE if empty |
509 |
|
|
*/ |
510 |
|
|
public void testLastElement() { |
511 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
512 |
jsr166 |
1.21 |
for (int i = SIZE - 1; i >= 0; --i) { |
513 |
jsr166 |
1.1 |
assertEquals(i, q.getLast()); |
514 |
|
|
assertEquals(i, q.pollLast()); |
515 |
|
|
} |
516 |
|
|
try { |
517 |
|
|
q.getLast(); |
518 |
|
|
shouldThrow(); |
519 |
|
|
} catch (NoSuchElementException success) {} |
520 |
|
|
assertNull(q.peekLast()); |
521 |
|
|
} |
522 |
|
|
|
523 |
|
|
/** |
524 |
|
|
* removeFirst() removes first element, or throws NSEE if empty |
525 |
|
|
*/ |
526 |
|
|
public void testRemoveFirst() { |
527 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
528 |
|
|
for (int i = 0; i < SIZE; ++i) { |
529 |
|
|
assertEquals(i, q.removeFirst()); |
530 |
|
|
} |
531 |
|
|
try { |
532 |
|
|
q.removeFirst(); |
533 |
|
|
shouldThrow(); |
534 |
|
|
} catch (NoSuchElementException success) {} |
535 |
|
|
assertNull(q.peekFirst()); |
536 |
|
|
} |
537 |
|
|
|
538 |
|
|
/** |
539 |
|
|
* removeLast() removes last element, or throws NSEE if empty |
540 |
|
|
*/ |
541 |
|
|
public void testRemoveLast() { |
542 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
543 |
|
|
for (int i = SIZE - 1; i >= 0; --i) { |
544 |
|
|
assertEquals(i, q.removeLast()); |
545 |
|
|
} |
546 |
|
|
try { |
547 |
|
|
q.removeLast(); |
548 |
|
|
shouldThrow(); |
549 |
|
|
} catch (NoSuchElementException success) {} |
550 |
|
|
assertNull(q.peekLast()); |
551 |
|
|
} |
552 |
|
|
|
553 |
|
|
/** |
554 |
|
|
* removeFirstOccurrence(x) removes x and returns true if present |
555 |
|
|
*/ |
556 |
|
|
public void testRemoveFirstOccurrence() { |
557 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
558 |
jsr166 |
1.15 |
for (int i = 1; i < SIZE; i += 2) { |
559 |
jsr166 |
1.1 |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
560 |
|
|
} |
561 |
jsr166 |
1.15 |
for (int i = 0; i < SIZE; i += 2) { |
562 |
jsr166 |
1.1 |
assertTrue(q.removeFirstOccurrence(new Integer(i))); |
563 |
jsr166 |
1.22 |
assertFalse(q.removeFirstOccurrence(new Integer(i + 1))); |
564 |
jsr166 |
1.1 |
} |
565 |
|
|
assertTrue(q.isEmpty()); |
566 |
|
|
} |
567 |
|
|
|
568 |
|
|
/** |
569 |
|
|
* removeLastOccurrence(x) removes x and returns true if present |
570 |
|
|
*/ |
571 |
|
|
public void testRemoveLastOccurrence() { |
572 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
573 |
jsr166 |
1.15 |
for (int i = 1; i < SIZE; i += 2) { |
574 |
jsr166 |
1.1 |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
575 |
|
|
} |
576 |
jsr166 |
1.15 |
for (int i = 0; i < SIZE; i += 2) { |
577 |
jsr166 |
1.1 |
assertTrue(q.removeLastOccurrence(new Integer(i))); |
578 |
jsr166 |
1.22 |
assertFalse(q.removeLastOccurrence(new Integer(i + 1))); |
579 |
jsr166 |
1.1 |
} |
580 |
|
|
assertTrue(q.isEmpty()); |
581 |
|
|
} |
582 |
|
|
|
583 |
|
|
/** |
584 |
|
|
* contains(x) reports true when elements added but not yet removed |
585 |
|
|
*/ |
586 |
|
|
public void testContains() { |
587 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
588 |
|
|
for (int i = 0; i < SIZE; ++i) { |
589 |
|
|
assertTrue(q.contains(new Integer(i))); |
590 |
|
|
q.poll(); |
591 |
|
|
assertFalse(q.contains(new Integer(i))); |
592 |
|
|
} |
593 |
|
|
} |
594 |
|
|
|
595 |
|
|
/** |
596 |
|
|
* clear() removes all elements |
597 |
|
|
*/ |
598 |
|
|
public void testClear() { |
599 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
600 |
|
|
q.clear(); |
601 |
|
|
assertTrue(q.isEmpty()); |
602 |
|
|
assertEquals(0, q.size()); |
603 |
|
|
q.add(one); |
604 |
|
|
assertFalse(q.isEmpty()); |
605 |
|
|
q.clear(); |
606 |
|
|
assertTrue(q.isEmpty()); |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
/** |
610 |
|
|
* containsAll(c) is true when c contains a subset of elements |
611 |
|
|
*/ |
612 |
|
|
public void testContainsAll() { |
613 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
614 |
|
|
ConcurrentLinkedDeque p = new ConcurrentLinkedDeque(); |
615 |
|
|
for (int i = 0; i < SIZE; ++i) { |
616 |
|
|
assertTrue(q.containsAll(p)); |
617 |
|
|
assertFalse(p.containsAll(q)); |
618 |
|
|
p.add(new Integer(i)); |
619 |
|
|
} |
620 |
|
|
assertTrue(p.containsAll(q)); |
621 |
|
|
} |
622 |
|
|
|
623 |
|
|
/** |
624 |
|
|
* retainAll(c) retains only those elements of c and reports true if change |
625 |
|
|
*/ |
626 |
|
|
public void testRetainAll() { |
627 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
628 |
|
|
ConcurrentLinkedDeque p = populatedDeque(SIZE); |
629 |
|
|
for (int i = 0; i < SIZE; ++i) { |
630 |
|
|
boolean changed = q.retainAll(p); |
631 |
|
|
if (i == 0) |
632 |
|
|
assertFalse(changed); |
633 |
|
|
else |
634 |
|
|
assertTrue(changed); |
635 |
|
|
|
636 |
|
|
assertTrue(q.containsAll(p)); |
637 |
jsr166 |
1.21 |
assertEquals(SIZE - i, q.size()); |
638 |
jsr166 |
1.1 |
p.remove(); |
639 |
|
|
} |
640 |
|
|
} |
641 |
|
|
|
642 |
|
|
/** |
643 |
|
|
* removeAll(c) removes only those elements of c and reports true if changed |
644 |
|
|
*/ |
645 |
|
|
public void testRemoveAll() { |
646 |
|
|
for (int i = 1; i < SIZE; ++i) { |
647 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
648 |
|
|
ConcurrentLinkedDeque p = populatedDeque(i); |
649 |
|
|
assertTrue(q.removeAll(p)); |
650 |
jsr166 |
1.21 |
assertEquals(SIZE - i, q.size()); |
651 |
jsr166 |
1.1 |
for (int j = 0; j < i; ++j) { |
652 |
jsr166 |
1.16 |
Integer x = (Integer)(p.remove()); |
653 |
|
|
assertFalse(q.contains(x)); |
654 |
jsr166 |
1.1 |
} |
655 |
|
|
} |
656 |
|
|
} |
657 |
|
|
|
658 |
|
|
/** |
659 |
jsr166 |
1.4 |
* toArray() contains all elements in FIFO order |
660 |
jsr166 |
1.1 |
*/ |
661 |
|
|
public void testToArray() { |
662 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
663 |
|
|
Object[] o = q.toArray(); |
664 |
|
|
for (int i = 0; i < o.length; i++) |
665 |
jsr166 |
1.4 |
assertSame(o[i], q.poll()); |
666 |
jsr166 |
1.1 |
} |
667 |
|
|
|
668 |
|
|
/** |
669 |
jsr166 |
1.4 |
* toArray(a) contains all elements in FIFO order |
670 |
jsr166 |
1.1 |
*/ |
671 |
|
|
public void testToArray2() { |
672 |
jsr166 |
1.5 |
ConcurrentLinkedDeque<Integer> q = populatedDeque(SIZE); |
673 |
jsr166 |
1.1 |
Integer[] ints = new Integer[SIZE]; |
674 |
jsr166 |
1.5 |
Integer[] array = q.toArray(ints); |
675 |
|
|
assertSame(ints, array); |
676 |
jsr166 |
1.1 |
for (int i = 0; i < ints.length; i++) |
677 |
jsr166 |
1.4 |
assertSame(ints[i], q.poll()); |
678 |
jsr166 |
1.1 |
} |
679 |
|
|
|
680 |
|
|
/** |
681 |
jsr166 |
1.3 |
* toArray(null) throws NullPointerException |
682 |
jsr166 |
1.1 |
*/ |
683 |
jsr166 |
1.3 |
public void testToArray_NullArg() { |
684 |
jsr166 |
1.1 |
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
685 |
|
|
try { |
686 |
jsr166 |
1.3 |
q.toArray(null); |
687 |
jsr166 |
1.1 |
shouldThrow(); |
688 |
|
|
} catch (NullPointerException success) {} |
689 |
|
|
} |
690 |
|
|
|
691 |
|
|
/** |
692 |
jsr166 |
1.2 |
* toArray(incompatible array type) throws ArrayStoreException |
693 |
jsr166 |
1.1 |
*/ |
694 |
|
|
public void testToArray1_BadArg() { |
695 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
696 |
|
|
try { |
697 |
jsr166 |
1.2 |
q.toArray(new String[10]); |
698 |
jsr166 |
1.1 |
shouldThrow(); |
699 |
|
|
} catch (ArrayStoreException success) {} |
700 |
|
|
} |
701 |
|
|
|
702 |
|
|
/** |
703 |
|
|
* Iterator iterates through all elements |
704 |
|
|
*/ |
705 |
|
|
public void testIterator() { |
706 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
707 |
|
|
Iterator it = q.iterator(); |
708 |
jsr166 |
1.17 |
int i; |
709 |
|
|
for (i = 0; it.hasNext(); i++) |
710 |
jsr166 |
1.1 |
assertTrue(q.contains(it.next())); |
711 |
|
|
assertEquals(i, SIZE); |
712 |
jsr166 |
1.17 |
assertIteratorExhausted(it); |
713 |
|
|
} |
714 |
|
|
|
715 |
|
|
/** |
716 |
|
|
* iterator of empty collection has no elements |
717 |
|
|
*/ |
718 |
|
|
public void testEmptyIterator() { |
719 |
|
|
Deque c = new ConcurrentLinkedDeque(); |
720 |
|
|
assertIteratorExhausted(c.iterator()); |
721 |
|
|
assertIteratorExhausted(c.descendingIterator()); |
722 |
jsr166 |
1.1 |
} |
723 |
|
|
|
724 |
|
|
/** |
725 |
|
|
* Iterator ordering is FIFO |
726 |
|
|
*/ |
727 |
|
|
public void testIteratorOrdering() { |
728 |
|
|
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
729 |
|
|
q.add(one); |
730 |
|
|
q.add(two); |
731 |
|
|
q.add(three); |
732 |
|
|
|
733 |
|
|
int k = 0; |
734 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
735 |
|
|
assertEquals(++k, it.next()); |
736 |
|
|
} |
737 |
|
|
|
738 |
|
|
assertEquals(3, k); |
739 |
|
|
} |
740 |
|
|
|
741 |
|
|
/** |
742 |
|
|
* Modifications do not cause iterators to fail |
743 |
|
|
*/ |
744 |
|
|
public void testWeaklyConsistentIteration() { |
745 |
|
|
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
746 |
|
|
q.add(one); |
747 |
|
|
q.add(two); |
748 |
|
|
q.add(three); |
749 |
|
|
|
750 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
751 |
|
|
q.remove(); |
752 |
|
|
it.next(); |
753 |
|
|
} |
754 |
|
|
|
755 |
|
|
assertEquals("deque should be empty again", 0, q.size()); |
756 |
|
|
} |
757 |
|
|
|
758 |
|
|
/** |
759 |
|
|
* iterator.remove() removes current element |
760 |
|
|
*/ |
761 |
|
|
public void testIteratorRemove() { |
762 |
|
|
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
763 |
|
|
final Random rng = new Random(); |
764 |
|
|
for (int iters = 0; iters < 100; ++iters) { |
765 |
|
|
int max = rng.nextInt(5) + 2; |
766 |
jsr166 |
1.22 |
int split = rng.nextInt(max - 1) + 1; |
767 |
jsr166 |
1.1 |
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 |
jsr166 |
1.22 |
assertEquals(it.next(), new Integer(split + 1)); |
774 |
jsr166 |
1.1 |
for (int j = 1; j <= split; ++j) |
775 |
|
|
q.remove(new Integer(j)); |
776 |
|
|
it = q.iterator(); |
777 |
jsr166 |
1.22 |
for (int j = split + 1; j <= max; ++j) { |
778 |
jsr166 |
1.1 |
assertEquals(it.next(), new Integer(j)); |
779 |
|
|
it.remove(); |
780 |
|
|
} |
781 |
|
|
assertFalse(it.hasNext()); |
782 |
|
|
assertTrue(q.isEmpty()); |
783 |
|
|
} |
784 |
|
|
} |
785 |
|
|
|
786 |
|
|
/** |
787 |
|
|
* Descending iterator iterates through all elements |
788 |
|
|
*/ |
789 |
|
|
public void testDescendingIterator() { |
790 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
791 |
|
|
int i = 0; |
792 |
|
|
Iterator it = q.descendingIterator(); |
793 |
|
|
while (it.hasNext()) { |
794 |
|
|
assertTrue(q.contains(it.next())); |
795 |
|
|
++i; |
796 |
|
|
} |
797 |
|
|
assertEquals(i, SIZE); |
798 |
|
|
assertFalse(it.hasNext()); |
799 |
|
|
try { |
800 |
|
|
it.next(); |
801 |
|
|
shouldThrow(); |
802 |
|
|
} catch (NoSuchElementException success) {} |
803 |
|
|
} |
804 |
|
|
|
805 |
|
|
/** |
806 |
|
|
* Descending iterator ordering is reverse FIFO |
807 |
|
|
*/ |
808 |
|
|
public void testDescendingIteratorOrdering() { |
809 |
|
|
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
810 |
|
|
for (int iters = 0; iters < 100; ++iters) { |
811 |
|
|
q.add(new Integer(3)); |
812 |
|
|
q.add(new Integer(2)); |
813 |
|
|
q.add(new Integer(1)); |
814 |
|
|
int k = 0; |
815 |
|
|
for (Iterator it = q.descendingIterator(); it.hasNext();) { |
816 |
|
|
assertEquals(++k, it.next()); |
817 |
|
|
} |
818 |
|
|
|
819 |
|
|
assertEquals(3, k); |
820 |
|
|
q.remove(); |
821 |
|
|
q.remove(); |
822 |
|
|
q.remove(); |
823 |
|
|
} |
824 |
|
|
} |
825 |
|
|
|
826 |
|
|
/** |
827 |
|
|
* descendingIterator.remove() removes current element |
828 |
|
|
*/ |
829 |
|
|
public void testDescendingIteratorRemove() { |
830 |
|
|
final ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(); |
831 |
|
|
final Random rng = new Random(); |
832 |
|
|
for (int iters = 0; iters < 100; ++iters) { |
833 |
|
|
int max = rng.nextInt(5) + 2; |
834 |
jsr166 |
1.22 |
int split = rng.nextInt(max - 1) + 1; |
835 |
jsr166 |
1.1 |
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 |
jsr166 |
1.22 |
assertEquals(it.next(), new Integer(split + 1)); |
842 |
jsr166 |
1.1 |
for (int j = 1; j <= split; ++j) |
843 |
|
|
q.remove(new Integer(j)); |
844 |
|
|
it = q.descendingIterator(); |
845 |
jsr166 |
1.22 |
for (int j = split + 1; j <= max; ++j) { |
846 |
jsr166 |
1.1 |
assertEquals(it.next(), new Integer(j)); |
847 |
|
|
it.remove(); |
848 |
|
|
} |
849 |
|
|
assertFalse(it.hasNext()); |
850 |
|
|
assertTrue(q.isEmpty()); |
851 |
|
|
} |
852 |
|
|
} |
853 |
|
|
|
854 |
|
|
/** |
855 |
|
|
* toString() contains toStrings of elements |
856 |
|
|
*/ |
857 |
|
|
public void testToString() { |
858 |
|
|
ConcurrentLinkedDeque q = populatedDeque(SIZE); |
859 |
|
|
String s = q.toString(); |
860 |
|
|
for (int i = 0; i < SIZE; ++i) { |
861 |
jsr166 |
1.8 |
assertTrue(s.contains(String.valueOf(i))); |
862 |
jsr166 |
1.1 |
} |
863 |
|
|
} |
864 |
|
|
|
865 |
|
|
/** |
866 |
|
|
* A deserialized serialized deque has same elements in same order |
867 |
|
|
*/ |
868 |
|
|
public void testSerialization() throws Exception { |
869 |
jsr166 |
1.9 |
Queue x = populatedDeque(SIZE); |
870 |
|
|
Queue y = serialClone(x); |
871 |
|
|
|
872 |
jsr166 |
1.12 |
assertNotSame(x, y); |
873 |
jsr166 |
1.9 |
assertEquals(x.size(), y.size()); |
874 |
|
|
assertEquals(x.toString(), y.toString()); |
875 |
|
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
876 |
|
|
while (!x.isEmpty()) { |
877 |
|
|
assertFalse(y.isEmpty()); |
878 |
|
|
assertEquals(x.remove(), y.remove()); |
879 |
|
|
} |
880 |
|
|
assertTrue(y.isEmpty()); |
881 |
jsr166 |
1.1 |
} |
882 |
|
|
|
883 |
jsr166 |
1.13 |
/** |
884 |
|
|
* contains(null) always return false. |
885 |
|
|
* remove(null) always throws NullPointerException. |
886 |
|
|
*/ |
887 |
|
|
public void testNeverContainsNull() { |
888 |
|
|
Deque<?>[] qs = { |
889 |
|
|
new ConcurrentLinkedDeque<Object>(), |
890 |
|
|
populatedDeque(2), |
891 |
|
|
}; |
892 |
|
|
|
893 |
|
|
for (Deque<?> q : qs) { |
894 |
|
|
assertFalse(q.contains(null)); |
895 |
|
|
try { |
896 |
|
|
assertFalse(q.remove(null)); |
897 |
|
|
shouldThrow(); |
898 |
|
|
} catch (NullPointerException success) {} |
899 |
|
|
try { |
900 |
|
|
assertFalse(q.removeFirstOccurrence(null)); |
901 |
|
|
shouldThrow(); |
902 |
|
|
} catch (NullPointerException success) {} |
903 |
|
|
try { |
904 |
|
|
assertFalse(q.removeLastOccurrence(null)); |
905 |
|
|
shouldThrow(); |
906 |
|
|
} catch (NullPointerException success) {} |
907 |
|
|
} |
908 |
|
|
} |
909 |
jsr166 |
1.1 |
} |