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