1 |
jsr166 |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea and Martin Buchholz with assistance from |
3 |
|
|
* members of JCP JSR-166 Expert Group and released to the public |
4 |
|
|
* domain, as explained at |
5 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
|
|
10 |
|
|
import java.util.ArrayList; |
11 |
|
|
import java.util.Collection; |
12 |
|
|
import java.util.Collections; |
13 |
jsr166 |
1.5 |
import java.util.Deque; |
14 |
|
|
import java.util.HashSet; |
15 |
|
|
import java.util.Iterator; |
16 |
|
|
import java.util.List; |
17 |
|
|
import java.util.NoSuchElementException; |
18 |
|
|
import java.util.Queue; |
19 |
|
|
import java.util.Spliterator; |
20 |
jsr166 |
1.14 |
import java.util.concurrent.BlockingDeque; |
21 |
|
|
import java.util.concurrent.BlockingQueue; |
22 |
jsr166 |
1.2 |
import java.util.concurrent.CountDownLatch; |
23 |
jsr166 |
1.1 |
import java.util.concurrent.Executors; |
24 |
|
|
import java.util.concurrent.ExecutorService; |
25 |
|
|
import java.util.concurrent.Future; |
26 |
jsr166 |
1.5 |
import java.util.concurrent.ThreadLocalRandom; |
27 |
jsr166 |
1.1 |
import java.util.concurrent.atomic.AtomicBoolean; |
28 |
|
|
import java.util.concurrent.atomic.AtomicLong; |
29 |
jsr166 |
1.5 |
import java.util.concurrent.atomic.AtomicReference; |
30 |
jsr166 |
1.1 |
import java.util.function.Consumer; |
31 |
jsr166 |
1.5 |
import java.util.function.Predicate; |
32 |
jsr166 |
1.1 |
|
33 |
|
|
import junit.framework.Test; |
34 |
|
|
|
35 |
|
|
/** |
36 |
|
|
* Contains tests applicable to all jdk8+ Collection implementations. |
37 |
|
|
* An extension of CollectionTest. |
38 |
|
|
*/ |
39 |
|
|
public class Collection8Test extends JSR166TestCase { |
40 |
|
|
final CollectionImplementation impl; |
41 |
|
|
|
42 |
|
|
/** Tests are parameterized by a Collection implementation. */ |
43 |
|
|
Collection8Test(CollectionImplementation impl, String methodName) { |
44 |
|
|
super(methodName); |
45 |
|
|
this.impl = impl; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
public static Test testSuite(CollectionImplementation impl) { |
49 |
|
|
return parameterizedTestSuite(Collection8Test.class, |
50 |
|
|
CollectionImplementation.class, |
51 |
|
|
impl); |
52 |
|
|
} |
53 |
|
|
|
54 |
jsr166 |
1.10 |
Object bomb() { |
55 |
|
|
return new Object() { |
56 |
|
|
public boolean equals(Object x) { throw new AssertionError(); } |
57 |
|
|
public int hashCode() { throw new AssertionError(); } |
58 |
|
|
}; |
59 |
|
|
} |
60 |
|
|
|
61 |
jsr166 |
1.5 |
/** Checks properties of empty collections. */ |
62 |
jsr166 |
1.14 |
public void testEmptyMeansEmpty() throws InterruptedException { |
63 |
jsr166 |
1.5 |
Collection c = impl.emptyCollection(); |
64 |
jsr166 |
1.12 |
emptyMeansEmpty(c); |
65 |
|
|
|
66 |
|
|
if (c instanceof java.io.Serializable) |
67 |
|
|
emptyMeansEmpty(serialClone(c)); |
68 |
|
|
|
69 |
|
|
Collection clone = cloneableClone(c); |
70 |
|
|
if (clone != null) |
71 |
|
|
emptyMeansEmpty(clone); |
72 |
|
|
} |
73 |
|
|
|
74 |
jsr166 |
1.14 |
void emptyMeansEmpty(Collection c) throws InterruptedException { |
75 |
jsr166 |
1.5 |
assertTrue(c.isEmpty()); |
76 |
|
|
assertEquals(0, c.size()); |
77 |
|
|
assertEquals("[]", c.toString()); |
78 |
|
|
{ |
79 |
|
|
Object[] a = c.toArray(); |
80 |
|
|
assertEquals(0, a.length); |
81 |
|
|
assertSame(Object[].class, a.getClass()); |
82 |
|
|
} |
83 |
|
|
{ |
84 |
|
|
Object[] a = new Object[0]; |
85 |
|
|
assertSame(a, c.toArray(a)); |
86 |
|
|
} |
87 |
|
|
{ |
88 |
|
|
Integer[] a = new Integer[0]; |
89 |
|
|
assertSame(a, c.toArray(a)); |
90 |
|
|
} |
91 |
|
|
{ |
92 |
|
|
Integer[] a = { 1, 2, 3}; |
93 |
|
|
assertSame(a, c.toArray(a)); |
94 |
|
|
assertNull(a[0]); |
95 |
|
|
assertSame(2, a[1]); |
96 |
|
|
assertSame(3, a[2]); |
97 |
|
|
} |
98 |
|
|
assertIteratorExhausted(c.iterator()); |
99 |
|
|
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
100 |
|
|
c.forEach(alwaysThrows); |
101 |
|
|
c.iterator().forEachRemaining(alwaysThrows); |
102 |
|
|
c.spliterator().forEachRemaining(alwaysThrows); |
103 |
|
|
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
104 |
jsr166 |
1.9 |
if (c.spliterator().hasCharacteristics(Spliterator.SIZED)) |
105 |
|
|
assertEquals(0, c.spliterator().estimateSize()); |
106 |
jsr166 |
1.10 |
assertFalse(c.contains(bomb())); |
107 |
|
|
assertFalse(c.remove(bomb())); |
108 |
jsr166 |
1.11 |
if (c instanceof Queue) { |
109 |
jsr166 |
1.5 |
Queue q = (Queue) c; |
110 |
|
|
assertNull(q.peek()); |
111 |
|
|
assertNull(q.poll()); |
112 |
|
|
} |
113 |
jsr166 |
1.11 |
if (c instanceof Deque) { |
114 |
jsr166 |
1.5 |
Deque d = (Deque) c; |
115 |
|
|
assertNull(d.peekFirst()); |
116 |
|
|
assertNull(d.peekLast()); |
117 |
|
|
assertNull(d.pollFirst()); |
118 |
|
|
assertNull(d.pollLast()); |
119 |
|
|
assertIteratorExhausted(d.descendingIterator()); |
120 |
jsr166 |
1.9 |
d.descendingIterator().forEachRemaining(alwaysThrows); |
121 |
jsr166 |
1.10 |
assertFalse(d.removeFirstOccurrence(bomb())); |
122 |
|
|
assertFalse(d.removeLastOccurrence(bomb())); |
123 |
jsr166 |
1.5 |
} |
124 |
jsr166 |
1.14 |
if (c instanceof BlockingQueue) { |
125 |
|
|
BlockingQueue q = (BlockingQueue) c; |
126 |
|
|
assertNull(q.poll(0L, MILLISECONDS)); |
127 |
|
|
} |
128 |
|
|
if (c instanceof BlockingDeque) { |
129 |
|
|
BlockingDeque q = (BlockingDeque) c; |
130 |
|
|
assertNull(q.pollFirst(0L, MILLISECONDS)); |
131 |
|
|
assertNull(q.pollLast(0L, MILLISECONDS)); |
132 |
|
|
} |
133 |
jsr166 |
1.5 |
} |
134 |
|
|
|
135 |
jsr166 |
1.14 |
public void testNullPointerExceptions() throws InterruptedException { |
136 |
jsr166 |
1.5 |
Collection c = impl.emptyCollection(); |
137 |
|
|
assertThrows( |
138 |
|
|
NullPointerException.class, |
139 |
|
|
() -> c.addAll(null), |
140 |
|
|
() -> c.containsAll(null), |
141 |
|
|
() -> c.retainAll(null), |
142 |
|
|
() -> c.removeAll(null), |
143 |
|
|
() -> c.removeIf(null), |
144 |
jsr166 |
1.6 |
() -> c.forEach(null), |
145 |
|
|
() -> c.iterator().forEachRemaining(null), |
146 |
|
|
() -> c.spliterator().forEachRemaining(null), |
147 |
|
|
() -> c.spliterator().tryAdvance(null), |
148 |
jsr166 |
1.5 |
() -> c.toArray(null)); |
149 |
|
|
|
150 |
|
|
if (!impl.permitsNulls()) { |
151 |
|
|
assertThrows( |
152 |
|
|
NullPointerException.class, |
153 |
|
|
() -> c.add(null)); |
154 |
|
|
} |
155 |
jsr166 |
1.14 |
if (!impl.permitsNulls() && c instanceof Queue) { |
156 |
jsr166 |
1.5 |
Queue q = (Queue) c; |
157 |
|
|
assertThrows( |
158 |
|
|
NullPointerException.class, |
159 |
|
|
() -> q.offer(null)); |
160 |
|
|
} |
161 |
jsr166 |
1.14 |
if (!impl.permitsNulls() && c instanceof Deque) { |
162 |
jsr166 |
1.5 |
Deque d = (Deque) c; |
163 |
|
|
assertThrows( |
164 |
|
|
NullPointerException.class, |
165 |
|
|
() -> d.addFirst(null), |
166 |
|
|
() -> d.addLast(null), |
167 |
|
|
() -> d.offerFirst(null), |
168 |
|
|
() -> d.offerLast(null), |
169 |
jsr166 |
1.6 |
() -> d.push(null), |
170 |
|
|
() -> d.descendingIterator().forEachRemaining(null)); |
171 |
jsr166 |
1.5 |
} |
172 |
jsr166 |
1.15 |
if (c instanceof BlockingQueue) { |
173 |
jsr166 |
1.14 |
BlockingQueue q = (BlockingQueue) c; |
174 |
|
|
assertThrows( |
175 |
|
|
NullPointerException.class, |
176 |
|
|
() -> { |
177 |
|
|
try { q.offer(null, 1L, MILLISECONDS); } |
178 |
|
|
catch (InterruptedException ex) { |
179 |
|
|
throw new AssertionError(ex); |
180 |
jsr166 |
1.15 |
}}, |
181 |
|
|
() -> { |
182 |
|
|
try { q.put(null); } |
183 |
|
|
catch (InterruptedException ex) { |
184 |
|
|
throw new AssertionError(ex); |
185 |
jsr166 |
1.14 |
}}); |
186 |
|
|
} |
187 |
jsr166 |
1.15 |
if (c instanceof BlockingDeque) { |
188 |
jsr166 |
1.14 |
BlockingDeque q = (BlockingDeque) c; |
189 |
|
|
assertThrows( |
190 |
|
|
NullPointerException.class, |
191 |
|
|
() -> { |
192 |
|
|
try { q.offerFirst(null, 1L, MILLISECONDS); } |
193 |
|
|
catch (InterruptedException ex) { |
194 |
|
|
throw new AssertionError(ex); |
195 |
|
|
}}, |
196 |
|
|
() -> { |
197 |
|
|
try { q.offerLast(null, 1L, MILLISECONDS); } |
198 |
|
|
catch (InterruptedException ex) { |
199 |
|
|
throw new AssertionError(ex); |
200 |
jsr166 |
1.15 |
}}, |
201 |
|
|
() -> { |
202 |
|
|
try { q.putFirst(null); } |
203 |
|
|
catch (InterruptedException ex) { |
204 |
|
|
throw new AssertionError(ex); |
205 |
|
|
}}, |
206 |
|
|
() -> { |
207 |
|
|
try { q.putLast(null); } |
208 |
|
|
catch (InterruptedException ex) { |
209 |
|
|
throw new AssertionError(ex); |
210 |
jsr166 |
1.14 |
}}); |
211 |
|
|
} |
212 |
jsr166 |
1.5 |
} |
213 |
|
|
|
214 |
|
|
public void testNoSuchElementExceptions() { |
215 |
|
|
Collection c = impl.emptyCollection(); |
216 |
|
|
assertThrows( |
217 |
|
|
NoSuchElementException.class, |
218 |
|
|
() -> c.iterator().next()); |
219 |
|
|
|
220 |
jsr166 |
1.14 |
if (c instanceof Queue) { |
221 |
jsr166 |
1.5 |
Queue q = (Queue) c; |
222 |
|
|
assertThrows( |
223 |
|
|
NoSuchElementException.class, |
224 |
|
|
() -> q.element(), |
225 |
|
|
() -> q.remove()); |
226 |
|
|
} |
227 |
jsr166 |
1.14 |
if (c instanceof Deque) { |
228 |
jsr166 |
1.5 |
Deque d = (Deque) c; |
229 |
|
|
assertThrows( |
230 |
|
|
NoSuchElementException.class, |
231 |
|
|
() -> d.getFirst(), |
232 |
|
|
() -> d.getLast(), |
233 |
|
|
() -> d.removeFirst(), |
234 |
|
|
() -> d.removeLast(), |
235 |
|
|
() -> d.pop(), |
236 |
|
|
() -> d.descendingIterator().next()); |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
public void testRemoveIf() { |
241 |
|
|
Collection c = impl.emptyCollection(); |
242 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
243 |
|
|
int n = rnd.nextInt(6); |
244 |
|
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
245 |
|
|
AtomicReference threwAt = new AtomicReference(null); |
246 |
|
|
ArrayList survivors = new ArrayList(c); |
247 |
|
|
ArrayList accepts = new ArrayList(); |
248 |
|
|
ArrayList rejects = new ArrayList(); |
249 |
|
|
Predicate randomPredicate = (e) -> { |
250 |
|
|
assertNull(threwAt.get()); |
251 |
|
|
switch (rnd.nextInt(3)) { |
252 |
|
|
case 0: accepts.add(e); return true; |
253 |
|
|
case 1: rejects.add(e); return false; |
254 |
|
|
case 2: threwAt.set(e); throw new ArithmeticException(); |
255 |
|
|
default: throw new AssertionError(); |
256 |
|
|
} |
257 |
|
|
}; |
258 |
|
|
try { |
259 |
jsr166 |
1.8 |
assertFalse(survivors.contains(null)); |
260 |
jsr166 |
1.7 |
try { |
261 |
|
|
boolean modified = c.removeIf(randomPredicate); |
262 |
|
|
if (!modified) { |
263 |
|
|
assertNull(threwAt.get()); |
264 |
|
|
assertEquals(n, rejects.size()); |
265 |
|
|
assertEquals(0, accepts.size()); |
266 |
|
|
} |
267 |
|
|
} catch (ArithmeticException ok) {} |
268 |
|
|
survivors.removeAll(accepts); |
269 |
|
|
assertEquals(n - accepts.size(), c.size()); |
270 |
|
|
assertTrue(c.containsAll(survivors)); |
271 |
|
|
assertTrue(survivors.containsAll(rejects)); |
272 |
|
|
for (Object x : accepts) assertFalse(c.contains(x)); |
273 |
|
|
if (threwAt.get() == null) |
274 |
|
|
assertEquals(accepts.size() + rejects.size(), n); |
275 |
|
|
} catch (Throwable ex) { |
276 |
jsr166 |
1.5 |
System.err.println(impl.klazz()); |
277 |
jsr166 |
1.7 |
System.err.printf("c=%s%n", c); |
278 |
|
|
System.err.printf("n=%d%n", n); |
279 |
|
|
System.err.printf("accepts=%s%n", accepts); |
280 |
|
|
System.err.printf("rejects=%s%n", rejects); |
281 |
jsr166 |
1.8 |
System.err.printf("survivors=%s%n", survivors); |
282 |
jsr166 |
1.7 |
System.err.printf("threw=%s%n", threwAt.get()); |
283 |
|
|
throw ex; |
284 |
jsr166 |
1.5 |
} |
285 |
|
|
} |
286 |
|
|
|
287 |
|
|
/** |
288 |
|
|
* Various ways of traversing a collection yield same elements |
289 |
|
|
*/ |
290 |
|
|
public void testIteratorEquivalence() { |
291 |
|
|
Collection c = impl.emptyCollection(); |
292 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
293 |
|
|
int n = rnd.nextInt(6); |
294 |
|
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
295 |
|
|
ArrayList iterated = new ArrayList(); |
296 |
|
|
ArrayList iteratedForEachRemaining = new ArrayList(); |
297 |
jsr166 |
1.13 |
ArrayList tryAdvanced = new ArrayList(); |
298 |
jsr166 |
1.5 |
ArrayList spliterated = new ArrayList(); |
299 |
jsr166 |
1.13 |
ArrayList forEached = new ArrayList(); |
300 |
|
|
ArrayList removeIfed = new ArrayList(); |
301 |
jsr166 |
1.5 |
for (Object x : c) iterated.add(x); |
302 |
|
|
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
303 |
jsr166 |
1.13 |
for (Spliterator s = c.spliterator(); |
304 |
|
|
s.tryAdvance(e -> tryAdvanced.add(e)); ) {} |
305 |
jsr166 |
1.5 |
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
306 |
jsr166 |
1.13 |
c.forEach(e -> forEached.add(e)); |
307 |
|
|
c.removeIf(e -> { removeIfed.add(e); return false; }); |
308 |
jsr166 |
1.5 |
boolean ordered = |
309 |
|
|
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
310 |
|
|
if (c instanceof List || c instanceof Deque) |
311 |
|
|
assertTrue(ordered); |
312 |
|
|
if (ordered) { |
313 |
|
|
assertEquals(iterated, iteratedForEachRemaining); |
314 |
jsr166 |
1.13 |
assertEquals(iterated, tryAdvanced); |
315 |
jsr166 |
1.5 |
assertEquals(iterated, spliterated); |
316 |
jsr166 |
1.13 |
assertEquals(iterated, forEached); |
317 |
|
|
assertEquals(iterated, removeIfed); |
318 |
jsr166 |
1.5 |
} else { |
319 |
|
|
HashSet cset = new HashSet(c); |
320 |
|
|
assertEquals(cset, new HashSet(iterated)); |
321 |
|
|
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
322 |
jsr166 |
1.13 |
assertEquals(cset, new HashSet(tryAdvanced)); |
323 |
jsr166 |
1.5 |
assertEquals(cset, new HashSet(spliterated)); |
324 |
jsr166 |
1.13 |
assertEquals(cset, new HashSet(forEached)); |
325 |
|
|
assertEquals(cset, new HashSet(removeIfed)); |
326 |
jsr166 |
1.5 |
} |
327 |
|
|
if (c instanceof Deque) { |
328 |
|
|
Deque d = (Deque) c; |
329 |
|
|
ArrayList descending = new ArrayList(); |
330 |
|
|
ArrayList descendingForEachRemaining = new ArrayList(); |
331 |
|
|
for (Iterator it = d.descendingIterator(); it.hasNext(); ) |
332 |
|
|
descending.add(it.next()); |
333 |
|
|
d.descendingIterator().forEachRemaining( |
334 |
|
|
e -> descendingForEachRemaining.add(e)); |
335 |
|
|
Collections.reverse(descending); |
336 |
|
|
Collections.reverse(descendingForEachRemaining); |
337 |
|
|
assertEquals(iterated, descending); |
338 |
|
|
assertEquals(iterated, descendingForEachRemaining); |
339 |
|
|
} |
340 |
|
|
} |
341 |
|
|
|
342 |
|
|
/** |
343 |
|
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
344 |
|
|
* should remove last element |
345 |
|
|
*/ |
346 |
|
|
public void testRemoveAfterForEachRemaining() { |
347 |
|
|
Collection c = impl.emptyCollection(); |
348 |
|
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
349 |
|
|
{ |
350 |
|
|
int n = 3 + rnd.nextInt(2); |
351 |
|
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
352 |
|
|
Iterator it = c.iterator(); |
353 |
|
|
assertTrue(it.hasNext()); |
354 |
|
|
assertEquals(impl.makeElement(0), it.next()); |
355 |
|
|
assertTrue(it.hasNext()); |
356 |
|
|
assertEquals(impl.makeElement(1), it.next()); |
357 |
|
|
it.forEachRemaining((e) -> {}); |
358 |
|
|
it.remove(); |
359 |
|
|
assertEquals(n - 1, c.size()); |
360 |
|
|
for (int i = 0; i < n - 1; i++) |
361 |
|
|
assertTrue(c.contains(impl.makeElement(i))); |
362 |
|
|
assertFalse(c.contains(impl.makeElement(n - 1))); |
363 |
|
|
} |
364 |
|
|
if (c instanceof Deque) { |
365 |
|
|
Deque d = (Deque) impl.emptyCollection(); |
366 |
|
|
int n = 3 + rnd.nextInt(2); |
367 |
|
|
for (int i = 0; i < n; i++) d.add(impl.makeElement(i)); |
368 |
|
|
Iterator it = d.descendingIterator(); |
369 |
|
|
assertTrue(it.hasNext()); |
370 |
|
|
assertEquals(impl.makeElement(n - 1), it.next()); |
371 |
|
|
assertTrue(it.hasNext()); |
372 |
|
|
assertEquals(impl.makeElement(n - 2), it.next()); |
373 |
|
|
it.forEachRemaining((e) -> {}); |
374 |
|
|
it.remove(); |
375 |
|
|
assertEquals(n - 1, d.size()); |
376 |
|
|
for (int i = 1; i < n; i++) |
377 |
|
|
assertTrue(d.contains(impl.makeElement(i))); |
378 |
|
|
assertFalse(d.contains(impl.makeElement(0))); |
379 |
|
|
} |
380 |
|
|
} |
381 |
|
|
|
382 |
jsr166 |
1.1 |
/** |
383 |
|
|
* stream().forEach returns elements in the collection |
384 |
|
|
*/ |
385 |
jsr166 |
1.3 |
public void testStreamForEach() throws Throwable { |
386 |
jsr166 |
1.1 |
final Collection c = impl.emptyCollection(); |
387 |
|
|
final AtomicLong count = new AtomicLong(0L); |
388 |
|
|
final Object x = impl.makeElement(1); |
389 |
|
|
final Object y = impl.makeElement(2); |
390 |
|
|
final ArrayList found = new ArrayList(); |
391 |
|
|
Consumer<Object> spy = (o) -> { found.add(o); }; |
392 |
|
|
c.stream().forEach(spy); |
393 |
|
|
assertTrue(found.isEmpty()); |
394 |
|
|
|
395 |
|
|
assertTrue(c.add(x)); |
396 |
|
|
c.stream().forEach(spy); |
397 |
|
|
assertEquals(Collections.singletonList(x), found); |
398 |
|
|
found.clear(); |
399 |
|
|
|
400 |
|
|
assertTrue(c.add(y)); |
401 |
|
|
c.stream().forEach(spy); |
402 |
|
|
assertEquals(2, found.size()); |
403 |
|
|
assertTrue(found.contains(x)); |
404 |
|
|
assertTrue(found.contains(y)); |
405 |
|
|
found.clear(); |
406 |
|
|
|
407 |
|
|
c.clear(); |
408 |
|
|
c.stream().forEach(spy); |
409 |
|
|
assertTrue(found.isEmpty()); |
410 |
|
|
} |
411 |
|
|
|
412 |
jsr166 |
1.3 |
public void testStreamForEachConcurrentStressTest() throws Throwable { |
413 |
|
|
if (!impl.isConcurrent()) return; |
414 |
|
|
final Collection c = impl.emptyCollection(); |
415 |
|
|
final long testDurationMillis = timeoutMillis(); |
416 |
|
|
final AtomicBoolean done = new AtomicBoolean(false); |
417 |
|
|
final Object elt = impl.makeElement(1); |
418 |
|
|
final Future<?> f1, f2; |
419 |
|
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
420 |
|
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
421 |
|
|
final CountDownLatch threadsStarted = new CountDownLatch(2); |
422 |
|
|
Runnable checkElt = () -> { |
423 |
|
|
threadsStarted.countDown(); |
424 |
|
|
while (!done.get()) |
425 |
|
|
c.stream().forEach((x) -> { assertSame(x, elt); }); }; |
426 |
|
|
Runnable addRemove = () -> { |
427 |
|
|
threadsStarted.countDown(); |
428 |
|
|
while (!done.get()) { |
429 |
|
|
assertTrue(c.add(elt)); |
430 |
|
|
assertTrue(c.remove(elt)); |
431 |
|
|
}}; |
432 |
|
|
f1 = pool.submit(checkElt); |
433 |
|
|
f2 = pool.submit(addRemove); |
434 |
|
|
Thread.sleep(testDurationMillis); |
435 |
|
|
} |
436 |
|
|
assertNull(f1.get(0L, MILLISECONDS)); |
437 |
|
|
assertNull(f2.get(0L, MILLISECONDS)); |
438 |
|
|
} |
439 |
|
|
|
440 |
|
|
/** |
441 |
|
|
* collection.forEach returns elements in the collection |
442 |
|
|
*/ |
443 |
|
|
public void testForEach() throws Throwable { |
444 |
|
|
final Collection c = impl.emptyCollection(); |
445 |
|
|
final AtomicLong count = new AtomicLong(0L); |
446 |
|
|
final Object x = impl.makeElement(1); |
447 |
|
|
final Object y = impl.makeElement(2); |
448 |
|
|
final ArrayList found = new ArrayList(); |
449 |
|
|
Consumer<Object> spy = (o) -> { found.add(o); }; |
450 |
|
|
c.forEach(spy); |
451 |
|
|
assertTrue(found.isEmpty()); |
452 |
|
|
|
453 |
|
|
assertTrue(c.add(x)); |
454 |
|
|
c.forEach(spy); |
455 |
|
|
assertEquals(Collections.singletonList(x), found); |
456 |
|
|
found.clear(); |
457 |
|
|
|
458 |
|
|
assertTrue(c.add(y)); |
459 |
|
|
c.forEach(spy); |
460 |
|
|
assertEquals(2, found.size()); |
461 |
|
|
assertTrue(found.contains(x)); |
462 |
|
|
assertTrue(found.contains(y)); |
463 |
|
|
found.clear(); |
464 |
|
|
|
465 |
|
|
c.clear(); |
466 |
|
|
c.forEach(spy); |
467 |
|
|
assertTrue(found.isEmpty()); |
468 |
|
|
} |
469 |
|
|
|
470 |
jsr166 |
1.1 |
public void testForEachConcurrentStressTest() throws Throwable { |
471 |
|
|
if (!impl.isConcurrent()) return; |
472 |
|
|
final Collection c = impl.emptyCollection(); |
473 |
jsr166 |
1.2 |
final long testDurationMillis = timeoutMillis(); |
474 |
jsr166 |
1.1 |
final AtomicBoolean done = new AtomicBoolean(false); |
475 |
|
|
final Object elt = impl.makeElement(1); |
476 |
jsr166 |
1.2 |
final Future<?> f1, f2; |
477 |
|
|
final ExecutorService pool = Executors.newCachedThreadPool(); |
478 |
|
|
try (PoolCleaner cleaner = cleaner(pool, done)) { |
479 |
|
|
final CountDownLatch threadsStarted = new CountDownLatch(2); |
480 |
|
|
Runnable checkElt = () -> { |
481 |
|
|
threadsStarted.countDown(); |
482 |
|
|
while (!done.get()) |
483 |
jsr166 |
1.3 |
c.forEach((x) -> { assertSame(x, elt); }); }; |
484 |
jsr166 |
1.2 |
Runnable addRemove = () -> { |
485 |
|
|
threadsStarted.countDown(); |
486 |
|
|
while (!done.get()) { |
487 |
|
|
assertTrue(c.add(elt)); |
488 |
|
|
assertTrue(c.remove(elt)); |
489 |
|
|
}}; |
490 |
|
|
f1 = pool.submit(checkElt); |
491 |
|
|
f2 = pool.submit(addRemove); |
492 |
|
|
Thread.sleep(testDurationMillis); |
493 |
|
|
} |
494 |
|
|
assertNull(f1.get(0L, MILLISECONDS)); |
495 |
|
|
assertNull(f2.get(0L, MILLISECONDS)); |
496 |
jsr166 |
1.1 |
} |
497 |
|
|
|
498 |
jsr166 |
1.4 |
// public void testCollection8DebugFail() { |
499 |
|
|
// fail(impl.klazz().getSimpleName()); |
500 |
|
|
// } |
501 |
jsr166 |
1.1 |
} |