1 |
dl |
1.1 |
/* |
2 |
dl |
1.6 |
* 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.25 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.8 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.27 |
import java.util.Arrays; |
10 |
|
|
import java.util.Collection; |
11 |
|
|
import java.util.Iterator; |
12 |
|
|
import java.util.NoSuchElementException; |
13 |
|
|
import java.util.Queue; |
14 |
|
|
import java.util.concurrent.ConcurrentLinkedQueue; |
15 |
dl |
1.1 |
|
16 |
jsr166 |
1.32 |
import junit.framework.Test; |
17 |
|
|
|
18 |
dl |
1.3 |
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
19 |
dl |
1.1 |
|
20 |
|
|
public static void main(String[] args) { |
21 |
jsr166 |
1.37 |
main(suite(), args); |
22 |
dl |
1.1 |
} |
23 |
|
|
|
24 |
|
|
public static Test suite() { |
25 |
jsr166 |
1.41 |
class Implementation implements CollectionImplementation { |
26 |
|
|
public Class<?> klazz() { return ConcurrentLinkedQueue.class; } |
27 |
|
|
public Collection emptyCollection() { return new ConcurrentLinkedQueue(); } |
28 |
dl |
1.50 |
public Object makeElement(int i) { return JSR166TestCase.itemFor(i); } |
29 |
jsr166 |
1.41 |
public boolean isConcurrent() { return true; } |
30 |
|
|
public boolean permitsNulls() { return false; } |
31 |
|
|
} |
32 |
|
|
return newTestSuite(ConcurrentLinkedQueueTest.class, |
33 |
|
|
CollectionTest.testSuite(new Implementation())); |
34 |
dl |
1.1 |
} |
35 |
|
|
|
36 |
|
|
/** |
37 |
jsr166 |
1.29 |
* Returns a new queue of given size containing consecutive |
38 |
dl |
1.50 |
* Items 0 ... n - 1. |
39 |
dl |
1.1 |
*/ |
40 |
dl |
1.50 |
private static ConcurrentLinkedQueue<Item> populatedQueue(int n) { |
41 |
|
|
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
42 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
43 |
jsr166 |
1.12 |
for (int i = 0; i < n; ++i) |
44 |
dl |
1.50 |
mustOffer(q, i); |
45 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
46 |
dl |
1.50 |
mustEqual(n, q.size()); |
47 |
|
|
mustEqual(0, q.peek()); |
48 |
dl |
1.1 |
return q; |
49 |
|
|
} |
50 |
jsr166 |
1.8 |
|
51 |
dl |
1.4 |
/** |
52 |
dl |
1.5 |
* new queue is empty |
53 |
dl |
1.4 |
*/ |
54 |
|
|
public void testConstructor1() { |
55 |
dl |
1.50 |
mustEqual(0, new ConcurrentLinkedQueue<Item>().size()); |
56 |
dl |
1.1 |
} |
57 |
|
|
|
58 |
dl |
1.4 |
/** |
59 |
jsr166 |
1.19 |
* Initializing from null Collection throws NPE |
60 |
dl |
1.4 |
*/ |
61 |
dl |
1.1 |
public void testConstructor3() { |
62 |
|
|
try { |
63 |
dl |
1.50 |
new ConcurrentLinkedQueue<Item>((Collection<Item>)null); |
64 |
dl |
1.4 |
shouldThrow(); |
65 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
66 |
dl |
1.1 |
} |
67 |
|
|
|
68 |
dl |
1.4 |
/** |
69 |
dl |
1.5 |
* Initializing from Collection of null elements throws NPE |
70 |
dl |
1.4 |
*/ |
71 |
|
|
public void testConstructor4() { |
72 |
dl |
1.1 |
try { |
73 |
dl |
1.50 |
new ConcurrentLinkedQueue<Item>(Arrays.asList(new Item[SIZE])); |
74 |
dl |
1.4 |
shouldThrow(); |
75 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
76 |
dl |
1.1 |
} |
77 |
|
|
|
78 |
dl |
1.4 |
/** |
79 |
dl |
1.5 |
* Initializing from Collection with some null elements throws NPE |
80 |
dl |
1.4 |
*/ |
81 |
|
|
public void testConstructor5() { |
82 |
dl |
1.50 |
Item[] items = new Item[2]; |
83 |
|
|
items[0] = zero; |
84 |
dl |
1.1 |
try { |
85 |
dl |
1.50 |
new ConcurrentLinkedQueue<Item>(Arrays.asList(items)); |
86 |
dl |
1.4 |
shouldThrow(); |
87 |
jsr166 |
1.15 |
} catch (NullPointerException success) {} |
88 |
dl |
1.1 |
} |
89 |
|
|
|
90 |
dl |
1.4 |
/** |
91 |
dl |
1.5 |
* Queue contains all elements of collection used to initialize |
92 |
dl |
1.4 |
*/ |
93 |
|
|
public void testConstructor6() { |
94 |
dl |
1.50 |
Item[] items = defaultItems; |
95 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(Arrays.asList(items)); |
96 |
jsr166 |
1.14 |
for (int i = 0; i < SIZE; ++i) |
97 |
dl |
1.50 |
mustEqual(items[i], q.poll()); |
98 |
dl |
1.1 |
} |
99 |
|
|
|
100 |
dl |
1.4 |
/** |
101 |
dl |
1.5 |
* isEmpty is true before add, false after |
102 |
dl |
1.4 |
*/ |
103 |
dl |
1.1 |
public void testEmpty() { |
104 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
105 |
dl |
1.1 |
assertTrue(q.isEmpty()); |
106 |
dl |
1.3 |
q.add(one); |
107 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
108 |
dl |
1.3 |
q.add(two); |
109 |
dl |
1.1 |
q.remove(); |
110 |
|
|
q.remove(); |
111 |
|
|
assertTrue(q.isEmpty()); |
112 |
|
|
} |
113 |
|
|
|
114 |
dl |
1.4 |
/** |
115 |
dl |
1.5 |
* size changes when elements added and removed |
116 |
dl |
1.4 |
*/ |
117 |
dl |
1.1 |
public void testSize() { |
118 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
119 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
120 |
dl |
1.50 |
mustEqual(SIZE - i, q.size()); |
121 |
dl |
1.1 |
q.remove(); |
122 |
|
|
} |
123 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
124 |
dl |
1.50 |
mustEqual(i, q.size()); |
125 |
|
|
mustAdd(q, i); |
126 |
dl |
1.1 |
} |
127 |
|
|
} |
128 |
|
|
|
129 |
dl |
1.4 |
/** |
130 |
dl |
1.5 |
* offer(null) throws NPE |
131 |
dl |
1.4 |
*/ |
132 |
|
|
public void testOfferNull() { |
133 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
134 |
jsr166 |
1.12 |
try { |
135 |
dl |
1.1 |
q.offer(null); |
136 |
dl |
1.4 |
shouldThrow(); |
137 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
138 |
dl |
1.1 |
} |
139 |
|
|
|
140 |
dl |
1.4 |
/** |
141 |
dl |
1.5 |
* add(null) throws NPE |
142 |
|
|
*/ |
143 |
|
|
public void testAddNull() { |
144 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
145 |
jsr166 |
1.12 |
try { |
146 |
dl |
1.5 |
q.add(null); |
147 |
|
|
shouldThrow(); |
148 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
149 |
dl |
1.5 |
} |
150 |
|
|
|
151 |
|
|
/** |
152 |
|
|
* Offer returns true |
153 |
dl |
1.4 |
*/ |
154 |
dl |
1.1 |
public void testOffer() { |
155 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
156 |
dl |
1.3 |
assertTrue(q.offer(zero)); |
157 |
|
|
assertTrue(q.offer(one)); |
158 |
dl |
1.1 |
} |
159 |
|
|
|
160 |
dl |
1.4 |
/** |
161 |
dl |
1.5 |
* add returns true |
162 |
dl |
1.4 |
*/ |
163 |
|
|
public void testAdd() { |
164 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
165 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
166 |
dl |
1.50 |
mustEqual(i, q.size()); |
167 |
|
|
mustAdd(q, i); |
168 |
dl |
1.1 |
} |
169 |
|
|
} |
170 |
|
|
|
171 |
dl |
1.4 |
/** |
172 |
jsr166 |
1.46 |
* addAll(null) throws NullPointerException |
173 |
dl |
1.4 |
*/ |
174 |
|
|
public void testAddAll1() { |
175 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
176 |
dl |
1.1 |
try { |
177 |
|
|
q.addAll(null); |
178 |
dl |
1.4 |
shouldThrow(); |
179 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
180 |
dl |
1.1 |
} |
181 |
dl |
1.5 |
|
182 |
|
|
/** |
183 |
jsr166 |
1.46 |
* addAll(this) throws IllegalArgumentException |
184 |
dl |
1.5 |
*/ |
185 |
|
|
public void testAddAllSelf() { |
186 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
187 |
dl |
1.5 |
try { |
188 |
|
|
q.addAll(q); |
189 |
|
|
shouldThrow(); |
190 |
jsr166 |
1.13 |
} catch (IllegalArgumentException success) {} |
191 |
dl |
1.5 |
} |
192 |
|
|
|
193 |
dl |
1.4 |
/** |
194 |
jsr166 |
1.46 |
* addAll of a collection with null elements throws NullPointerException |
195 |
dl |
1.4 |
*/ |
196 |
|
|
public void testAddAll2() { |
197 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
198 |
dl |
1.1 |
try { |
199 |
dl |
1.50 |
q.addAll(Arrays.asList(new Item[SIZE])); |
200 |
dl |
1.4 |
shouldThrow(); |
201 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
202 |
dl |
1.1 |
} |
203 |
jsr166 |
1.18 |
|
204 |
dl |
1.4 |
/** |
205 |
jsr166 |
1.18 |
* addAll of a collection with any null elements throws NPE after |
206 |
dl |
1.5 |
* possibly adding some elements |
207 |
dl |
1.4 |
*/ |
208 |
|
|
public void testAddAll3() { |
209 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
210 |
dl |
1.50 |
Item[] items = new Item[2]; |
211 |
|
|
items[0] = zero; |
212 |
dl |
1.1 |
try { |
213 |
dl |
1.50 |
q.addAll(Arrays.asList(items)); |
214 |
dl |
1.4 |
shouldThrow(); |
215 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
216 |
dl |
1.1 |
} |
217 |
|
|
|
218 |
dl |
1.4 |
/** |
219 |
dl |
1.5 |
* Queue contains all elements, in traversal order, of successful addAll |
220 |
dl |
1.4 |
*/ |
221 |
|
|
public void testAddAll5() { |
222 |
dl |
1.50 |
Item[] empty = new Item[0]; |
223 |
|
|
Item[] items = defaultItems; |
224 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
225 |
jsr166 |
1.14 |
assertFalse(q.addAll(Arrays.asList(empty))); |
226 |
dl |
1.50 |
assertTrue(q.addAll(Arrays.asList(items))); |
227 |
jsr166 |
1.14 |
for (int i = 0; i < SIZE; ++i) |
228 |
dl |
1.50 |
mustEqual(items[i], q.poll()); |
229 |
dl |
1.1 |
} |
230 |
|
|
|
231 |
dl |
1.4 |
/** |
232 |
dl |
1.5 |
* poll succeeds unless empty |
233 |
dl |
1.4 |
*/ |
234 |
|
|
public void testPoll() { |
235 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
236 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
237 |
dl |
1.50 |
mustEqual(i, q.poll()); |
238 |
dl |
1.1 |
} |
239 |
jsr166 |
1.12 |
assertNull(q.poll()); |
240 |
dl |
1.1 |
} |
241 |
|
|
|
242 |
dl |
1.4 |
/** |
243 |
dl |
1.5 |
* peek returns next element, or null if empty |
244 |
dl |
1.4 |
*/ |
245 |
|
|
public void testPeek() { |
246 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
247 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
248 |
dl |
1.50 |
mustEqual(i, q.peek()); |
249 |
|
|
mustEqual(i, q.poll()); |
250 |
dl |
1.1 |
assertTrue(q.peek() == null || |
251 |
jsr166 |
1.16 |
!q.peek().equals(i)); |
252 |
dl |
1.1 |
} |
253 |
jsr166 |
1.12 |
assertNull(q.peek()); |
254 |
dl |
1.1 |
} |
255 |
|
|
|
256 |
dl |
1.4 |
/** |
257 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
258 |
dl |
1.4 |
*/ |
259 |
|
|
public void testElement() { |
260 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
261 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
262 |
dl |
1.50 |
mustEqual(i, q.element()); |
263 |
|
|
mustEqual(i, q.poll()); |
264 |
dl |
1.1 |
} |
265 |
|
|
try { |
266 |
|
|
q.element(); |
267 |
dl |
1.4 |
shouldThrow(); |
268 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
269 |
dl |
1.1 |
} |
270 |
|
|
|
271 |
dl |
1.4 |
/** |
272 |
jsr166 |
1.19 |
* remove removes next element, or throws NSEE if empty |
273 |
dl |
1.4 |
*/ |
274 |
|
|
public void testRemove() { |
275 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
276 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
277 |
dl |
1.50 |
mustEqual(i, q.remove()); |
278 |
dl |
1.1 |
} |
279 |
|
|
try { |
280 |
|
|
q.remove(); |
281 |
dl |
1.4 |
shouldThrow(); |
282 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
283 |
dl |
1.1 |
} |
284 |
|
|
|
285 |
dl |
1.4 |
/** |
286 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
287 |
dl |
1.4 |
*/ |
288 |
|
|
public void testRemoveElement() { |
289 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
290 |
jsr166 |
1.33 |
for (int i = 1; i < SIZE; i += 2) { |
291 |
dl |
1.50 |
mustContain(q, i); |
292 |
|
|
mustRemove(q, i); |
293 |
|
|
mustNotContain(q, i); |
294 |
|
|
mustContain(q, i - 1); |
295 |
dl |
1.1 |
} |
296 |
jsr166 |
1.33 |
for (int i = 0; i < SIZE; i += 2) { |
297 |
dl |
1.50 |
mustContain(q, i); |
298 |
|
|
mustRemove(q, i); |
299 |
|
|
mustNotContain(q, i); |
300 |
|
|
mustNotRemove(q, i + 1); |
301 |
|
|
mustNotContain(q, i + 1); |
302 |
dl |
1.1 |
} |
303 |
dl |
1.2 |
assertTrue(q.isEmpty()); |
304 |
dl |
1.1 |
} |
305 |
jsr166 |
1.8 |
|
306 |
dl |
1.4 |
/** |
307 |
dl |
1.5 |
* contains(x) reports true when elements added but not yet removed |
308 |
dl |
1.4 |
*/ |
309 |
|
|
public void testContains() { |
310 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
311 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
312 |
dl |
1.50 |
mustContain(q, i); |
313 |
dl |
1.1 |
q.poll(); |
314 |
dl |
1.50 |
mustNotContain(q, i); |
315 |
dl |
1.1 |
} |
316 |
|
|
} |
317 |
|
|
|
318 |
dl |
1.4 |
/** |
319 |
dl |
1.5 |
* clear removes all elements |
320 |
dl |
1.4 |
*/ |
321 |
|
|
public void testClear() { |
322 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
323 |
dl |
1.1 |
q.clear(); |
324 |
|
|
assertTrue(q.isEmpty()); |
325 |
dl |
1.50 |
mustEqual(0, q.size()); |
326 |
dl |
1.3 |
q.add(one); |
327 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
328 |
|
|
q.clear(); |
329 |
|
|
assertTrue(q.isEmpty()); |
330 |
|
|
} |
331 |
|
|
|
332 |
dl |
1.4 |
/** |
333 |
dl |
1.5 |
* containsAll(c) is true when c contains a subset of elements |
334 |
dl |
1.4 |
*/ |
335 |
|
|
public void testContainsAll() { |
336 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
337 |
jsr166 |
1.51 |
ConcurrentLinkedQueue<Item> p = new ConcurrentLinkedQueue<>(); |
338 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
339 |
dl |
1.1 |
assertTrue(q.containsAll(p)); |
340 |
|
|
assertFalse(p.containsAll(q)); |
341 |
dl |
1.50 |
mustAdd(p, i); |
342 |
dl |
1.1 |
} |
343 |
|
|
assertTrue(p.containsAll(q)); |
344 |
|
|
} |
345 |
|
|
|
346 |
dl |
1.4 |
/** |
347 |
dl |
1.5 |
* retainAll(c) retains only those elements of c and reports true if change |
348 |
dl |
1.4 |
*/ |
349 |
|
|
public void testRetainAll() { |
350 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
351 |
|
|
ConcurrentLinkedQueue<Item> p = populatedQueue(SIZE); |
352 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
353 |
dl |
1.1 |
boolean changed = q.retainAll(p); |
354 |
|
|
if (i == 0) |
355 |
|
|
assertFalse(changed); |
356 |
|
|
else |
357 |
|
|
assertTrue(changed); |
358 |
|
|
|
359 |
|
|
assertTrue(q.containsAll(p)); |
360 |
dl |
1.50 |
mustEqual(SIZE - i, q.size()); |
361 |
dl |
1.1 |
p.remove(); |
362 |
|
|
} |
363 |
|
|
} |
364 |
|
|
|
365 |
dl |
1.4 |
/** |
366 |
dl |
1.5 |
* removeAll(c) removes only those elements of c and reports true if changed |
367 |
dl |
1.4 |
*/ |
368 |
|
|
public void testRemoveAll() { |
369 |
dl |
1.3 |
for (int i = 1; i < SIZE; ++i) { |
370 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
371 |
|
|
ConcurrentLinkedQueue<Item> p = populatedQueue(i); |
372 |
dl |
1.1 |
assertTrue(q.removeAll(p)); |
373 |
dl |
1.50 |
mustEqual(SIZE - i, q.size()); |
374 |
dl |
1.1 |
for (int j = 0; j < i; ++j) { |
375 |
dl |
1.50 |
Item x = p.remove(); |
376 |
jsr166 |
1.34 |
assertFalse(q.contains(x)); |
377 |
dl |
1.1 |
} |
378 |
|
|
} |
379 |
|
|
} |
380 |
|
|
|
381 |
dl |
1.4 |
/** |
382 |
jsr166 |
1.22 |
* toArray contains all elements in FIFO order |
383 |
dl |
1.4 |
*/ |
384 |
|
|
public void testToArray() { |
385 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
386 |
jsr166 |
1.48 |
Object[] a = q.toArray(); |
387 |
|
|
assertSame(Object[].class, a.getClass()); |
388 |
|
|
for (Object o : a) |
389 |
|
|
assertSame(o, q.poll()); |
390 |
|
|
assertTrue(q.isEmpty()); |
391 |
dl |
1.1 |
} |
392 |
|
|
|
393 |
dl |
1.4 |
/** |
394 |
jsr166 |
1.22 |
* toArray(a) contains all elements in FIFO order |
395 |
dl |
1.4 |
*/ |
396 |
|
|
public void testToArray2() { |
397 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
398 |
|
|
Item[] items = new Item[SIZE]; |
399 |
|
|
Item[] array = q.toArray(items); |
400 |
|
|
assertSame(items, array); |
401 |
|
|
for (Item o : items) |
402 |
jsr166 |
1.48 |
assertSame(o, q.poll()); |
403 |
|
|
assertTrue(q.isEmpty()); |
404 |
dl |
1.1 |
} |
405 |
dl |
1.5 |
|
406 |
|
|
/** |
407 |
jsr166 |
1.21 |
* toArray(null) throws NullPointerException |
408 |
dl |
1.5 |
*/ |
409 |
jsr166 |
1.21 |
public void testToArray_NullArg() { |
410 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
411 |
jsr166 |
1.12 |
try { |
412 |
jsr166 |
1.49 |
q.toArray((Object[])null); |
413 |
jsr166 |
1.12 |
shouldThrow(); |
414 |
|
|
} catch (NullPointerException success) {} |
415 |
dl |
1.5 |
} |
416 |
|
|
|
417 |
|
|
/** |
418 |
jsr166 |
1.20 |
* toArray(incompatible array type) throws ArrayStoreException |
419 |
dl |
1.5 |
*/ |
420 |
jsr166 |
1.52 |
@SuppressWarnings("CollectionToArraySafeParameter") |
421 |
|
|
public void testToArray_incompatibleArrayType() { |
422 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
423 |
jsr166 |
1.12 |
try { |
424 |
jsr166 |
1.20 |
q.toArray(new String[10]); |
425 |
jsr166 |
1.12 |
shouldThrow(); |
426 |
|
|
} catch (ArrayStoreException success) {} |
427 |
dl |
1.5 |
} |
428 |
jsr166 |
1.8 |
|
429 |
dl |
1.4 |
/** |
430 |
jsr166 |
1.19 |
* iterator iterates through all elements |
431 |
dl |
1.4 |
*/ |
432 |
|
|
public void testIterator() { |
433 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
434 |
|
|
Iterator<? extends Item> it = q.iterator(); |
435 |
jsr166 |
1.35 |
int i; |
436 |
|
|
for (i = 0; it.hasNext(); i++) |
437 |
dl |
1.50 |
mustContain(q, it.next()); |
438 |
|
|
mustEqual(i, SIZE); |
439 |
jsr166 |
1.35 |
assertIteratorExhausted(it); |
440 |
|
|
} |
441 |
|
|
|
442 |
|
|
/** |
443 |
|
|
* iterator of empty collection has no elements |
444 |
|
|
*/ |
445 |
|
|
public void testEmptyIterator() { |
446 |
jsr166 |
1.51 |
assertIteratorExhausted(new ConcurrentLinkedQueue<>().iterator()); |
447 |
dl |
1.1 |
} |
448 |
|
|
|
449 |
dl |
1.4 |
/** |
450 |
dl |
1.5 |
* iterator ordering is FIFO |
451 |
dl |
1.4 |
*/ |
452 |
dl |
1.1 |
public void testIteratorOrdering() { |
453 |
jsr166 |
1.51 |
final ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
454 |
dl |
1.3 |
q.add(one); |
455 |
|
|
q.add(two); |
456 |
|
|
q.add(three); |
457 |
dl |
1.1 |
|
458 |
|
|
int k = 0; |
459 |
dl |
1.50 |
for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) { |
460 |
|
|
mustEqual(++k, it.next()); |
461 |
dl |
1.1 |
} |
462 |
|
|
|
463 |
dl |
1.50 |
mustEqual(3, k); |
464 |
dl |
1.1 |
} |
465 |
|
|
|
466 |
dl |
1.4 |
/** |
467 |
dl |
1.5 |
* Modifications do not cause iterators to fail |
468 |
dl |
1.4 |
*/ |
469 |
jsr166 |
1.18 |
public void testWeaklyConsistentIteration() { |
470 |
jsr166 |
1.51 |
final ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
471 |
dl |
1.3 |
q.add(one); |
472 |
|
|
q.add(two); |
473 |
|
|
q.add(three); |
474 |
dl |
1.1 |
|
475 |
dl |
1.50 |
for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) { |
476 |
jsr166 |
1.13 |
q.remove(); |
477 |
|
|
it.next(); |
478 |
dl |
1.1 |
} |
479 |
|
|
|
480 |
dl |
1.50 |
mustEqual(0, q.size()); |
481 |
dl |
1.1 |
} |
482 |
|
|
|
483 |
dl |
1.4 |
/** |
484 |
dl |
1.5 |
* iterator.remove removes current element |
485 |
dl |
1.4 |
*/ |
486 |
jsr166 |
1.18 |
public void testIteratorRemove() { |
487 |
jsr166 |
1.51 |
final ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(); |
488 |
dl |
1.3 |
q.add(one); |
489 |
|
|
q.add(two); |
490 |
|
|
q.add(three); |
491 |
dl |
1.50 |
Iterator<? extends Item> it = q.iterator(); |
492 |
dl |
1.1 |
it.next(); |
493 |
|
|
it.remove(); |
494 |
|
|
it = q.iterator(); |
495 |
jsr166 |
1.17 |
assertSame(it.next(), two); |
496 |
|
|
assertSame(it.next(), three); |
497 |
dl |
1.1 |
assertFalse(it.hasNext()); |
498 |
|
|
} |
499 |
|
|
|
500 |
dl |
1.4 |
/** |
501 |
dl |
1.5 |
* toString contains toStrings of elements |
502 |
dl |
1.4 |
*/ |
503 |
|
|
public void testToString() { |
504 |
dl |
1.50 |
ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE); |
505 |
dl |
1.1 |
String s = q.toString(); |
506 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
507 |
jsr166 |
1.26 |
assertTrue(s.contains(String.valueOf(i))); |
508 |
dl |
1.1 |
} |
509 |
jsr166 |
1.8 |
} |
510 |
dl |
1.2 |
|
511 |
dl |
1.4 |
/** |
512 |
jsr166 |
1.47 |
* A deserialized/reserialized queue has same elements in same order |
513 |
dl |
1.4 |
*/ |
514 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
515 |
dl |
1.50 |
Queue<Item> x = populatedQueue(SIZE); |
516 |
|
|
Queue<Item> y = serialClone(x); |
517 |
jsr166 |
1.27 |
|
518 |
jsr166 |
1.30 |
assertNotSame(x, y); |
519 |
dl |
1.50 |
mustEqual(x.size(), y.size()); |
520 |
|
|
mustEqual(x.toString(), y.toString()); |
521 |
jsr166 |
1.27 |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
522 |
|
|
while (!x.isEmpty()) { |
523 |
|
|
assertFalse(y.isEmpty()); |
524 |
dl |
1.50 |
mustEqual(x.remove(), y.remove()); |
525 |
jsr166 |
1.27 |
} |
526 |
|
|
assertTrue(y.isEmpty()); |
527 |
dl |
1.2 |
} |
528 |
dl |
1.1 |
|
529 |
jsr166 |
1.31 |
/** |
530 |
|
|
* remove(null), contains(null) always return false |
531 |
|
|
*/ |
532 |
|
|
public void testNeverContainsNull() { |
533 |
|
|
Collection<?>[] qs = { |
534 |
jsr166 |
1.51 |
new ConcurrentLinkedQueue<>(), |
535 |
jsr166 |
1.31 |
populatedQueue(2), |
536 |
|
|
}; |
537 |
|
|
|
538 |
|
|
for (Collection<?> q : qs) { |
539 |
|
|
assertFalse(q.contains(null)); |
540 |
|
|
assertFalse(q.remove(null)); |
541 |
|
|
} |
542 |
|
|
} |
543 |
dl |
1.1 |
} |