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