10 |
|
import java.util.ArrayList; |
11 |
|
import java.util.Collection; |
12 |
|
import java.util.Collections; |
13 |
+ |
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 |
+ |
import java.util.concurrent.CountDownLatch; |
21 |
|
import java.util.concurrent.Executors; |
22 |
|
import java.util.concurrent.ExecutorService; |
23 |
|
import java.util.concurrent.Future; |
24 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
25 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
26 |
|
import java.util.concurrent.atomic.AtomicLong; |
27 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
28 |
|
import java.util.function.Consumer; |
29 |
+ |
import java.util.function.Predicate; |
30 |
|
|
31 |
|
import junit.framework.Test; |
32 |
|
|
49 |
|
impl); |
50 |
|
} |
51 |
|
|
52 |
+ |
Object bomb() { |
53 |
+ |
return new Object() { |
54 |
+ |
public boolean equals(Object x) { throw new AssertionError(); } |
55 |
+ |
public int hashCode() { throw new AssertionError(); } |
56 |
+ |
}; |
57 |
+ |
} |
58 |
+ |
|
59 |
+ |
/** Checks properties of empty collections. */ |
60 |
+ |
public void testEmptyMeansEmpty() { |
61 |
+ |
Collection c = impl.emptyCollection(); |
62 |
+ |
emptyMeansEmpty(c); |
63 |
+ |
|
64 |
+ |
if (c instanceof java.io.Serializable) |
65 |
+ |
emptyMeansEmpty(serialClone(c)); |
66 |
+ |
|
67 |
+ |
Collection clone = cloneableClone(c); |
68 |
+ |
if (clone != null) |
69 |
+ |
emptyMeansEmpty(clone); |
70 |
+ |
} |
71 |
+ |
|
72 |
+ |
void emptyMeansEmpty(Collection c) { |
73 |
+ |
assertTrue(c.isEmpty()); |
74 |
+ |
assertEquals(0, c.size()); |
75 |
+ |
assertEquals("[]", c.toString()); |
76 |
+ |
{ |
77 |
+ |
Object[] a = c.toArray(); |
78 |
+ |
assertEquals(0, a.length); |
79 |
+ |
assertSame(Object[].class, a.getClass()); |
80 |
+ |
} |
81 |
+ |
{ |
82 |
+ |
Object[] a = new Object[0]; |
83 |
+ |
assertSame(a, c.toArray(a)); |
84 |
+ |
} |
85 |
+ |
{ |
86 |
+ |
Integer[] a = new Integer[0]; |
87 |
+ |
assertSame(a, c.toArray(a)); |
88 |
+ |
} |
89 |
+ |
{ |
90 |
+ |
Integer[] a = { 1, 2, 3}; |
91 |
+ |
assertSame(a, c.toArray(a)); |
92 |
+ |
assertNull(a[0]); |
93 |
+ |
assertSame(2, a[1]); |
94 |
+ |
assertSame(3, a[2]); |
95 |
+ |
} |
96 |
+ |
assertIteratorExhausted(c.iterator()); |
97 |
+ |
Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; |
98 |
+ |
c.forEach(alwaysThrows); |
99 |
+ |
c.iterator().forEachRemaining(alwaysThrows); |
100 |
+ |
c.spliterator().forEachRemaining(alwaysThrows); |
101 |
+ |
assertFalse(c.spliterator().tryAdvance(alwaysThrows)); |
102 |
+ |
if (c.spliterator().hasCharacteristics(Spliterator.SIZED)) |
103 |
+ |
assertEquals(0, c.spliterator().estimateSize()); |
104 |
+ |
assertFalse(c.contains(bomb())); |
105 |
+ |
assertFalse(c.remove(bomb())); |
106 |
+ |
if (c instanceof Queue) { |
107 |
+ |
Queue q = (Queue) c; |
108 |
+ |
assertNull(q.peek()); |
109 |
+ |
assertNull(q.poll()); |
110 |
+ |
} |
111 |
+ |
if (c instanceof Deque) { |
112 |
+ |
Deque d = (Deque) c; |
113 |
+ |
assertNull(d.peekFirst()); |
114 |
+ |
assertNull(d.peekLast()); |
115 |
+ |
assertNull(d.pollFirst()); |
116 |
+ |
assertNull(d.pollLast()); |
117 |
+ |
assertIteratorExhausted(d.descendingIterator()); |
118 |
+ |
d.descendingIterator().forEachRemaining(alwaysThrows); |
119 |
+ |
assertFalse(d.removeFirstOccurrence(bomb())); |
120 |
+ |
assertFalse(d.removeLastOccurrence(bomb())); |
121 |
+ |
} |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
public void testNullPointerExceptions() { |
125 |
+ |
Collection c = impl.emptyCollection(); |
126 |
+ |
assertThrows( |
127 |
+ |
NullPointerException.class, |
128 |
+ |
() -> c.addAll(null), |
129 |
+ |
() -> c.containsAll(null), |
130 |
+ |
() -> c.retainAll(null), |
131 |
+ |
() -> c.removeAll(null), |
132 |
+ |
() -> c.removeIf(null), |
133 |
+ |
() -> c.forEach(null), |
134 |
+ |
() -> c.iterator().forEachRemaining(null), |
135 |
+ |
() -> c.spliterator().forEachRemaining(null), |
136 |
+ |
() -> c.spliterator().tryAdvance(null), |
137 |
+ |
() -> c.toArray(null)); |
138 |
+ |
|
139 |
+ |
if (!impl.permitsNulls()) { |
140 |
+ |
assertThrows( |
141 |
+ |
NullPointerException.class, |
142 |
+ |
() -> c.add(null)); |
143 |
+ |
} |
144 |
+ |
if (!impl.permitsNulls() |
145 |
+ |
&& Queue.class.isAssignableFrom(impl.klazz())) { |
146 |
+ |
Queue q = (Queue) c; |
147 |
+ |
assertThrows( |
148 |
+ |
NullPointerException.class, |
149 |
+ |
() -> q.offer(null)); |
150 |
+ |
} |
151 |
+ |
if (!impl.permitsNulls() |
152 |
+ |
&& Deque.class.isAssignableFrom(impl.klazz())) { |
153 |
+ |
Deque d = (Deque) c; |
154 |
+ |
assertThrows( |
155 |
+ |
NullPointerException.class, |
156 |
+ |
() -> d.addFirst(null), |
157 |
+ |
() -> d.addLast(null), |
158 |
+ |
() -> d.offerFirst(null), |
159 |
+ |
() -> d.offerLast(null), |
160 |
+ |
() -> d.push(null), |
161 |
+ |
() -> d.descendingIterator().forEachRemaining(null)); |
162 |
+ |
} |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
public void testNoSuchElementExceptions() { |
166 |
+ |
Collection c = impl.emptyCollection(); |
167 |
+ |
assertThrows( |
168 |
+ |
NoSuchElementException.class, |
169 |
+ |
() -> c.iterator().next()); |
170 |
+ |
|
171 |
+ |
if (Queue.class.isAssignableFrom(impl.klazz())) { |
172 |
+ |
Queue q = (Queue) c; |
173 |
+ |
assertThrows( |
174 |
+ |
NoSuchElementException.class, |
175 |
+ |
() -> q.element(), |
176 |
+ |
() -> q.remove()); |
177 |
+ |
} |
178 |
+ |
if (Deque.class.isAssignableFrom(impl.klazz())) { |
179 |
+ |
Deque d = (Deque) c; |
180 |
+ |
assertThrows( |
181 |
+ |
NoSuchElementException.class, |
182 |
+ |
() -> d.getFirst(), |
183 |
+ |
() -> d.getLast(), |
184 |
+ |
() -> d.removeFirst(), |
185 |
+ |
() -> d.removeLast(), |
186 |
+ |
() -> d.pop(), |
187 |
+ |
() -> d.descendingIterator().next()); |
188 |
+ |
} |
189 |
+ |
} |
190 |
+ |
|
191 |
+ |
public void testRemoveIf() { |
192 |
+ |
Collection c = impl.emptyCollection(); |
193 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
194 |
+ |
int n = rnd.nextInt(6); |
195 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
196 |
+ |
AtomicReference threwAt = new AtomicReference(null); |
197 |
+ |
ArrayList survivors = new ArrayList(c); |
198 |
+ |
ArrayList accepts = new ArrayList(); |
199 |
+ |
ArrayList rejects = new ArrayList(); |
200 |
+ |
Predicate randomPredicate = (e) -> { |
201 |
+ |
assertNull(threwAt.get()); |
202 |
+ |
switch (rnd.nextInt(3)) { |
203 |
+ |
case 0: accepts.add(e); return true; |
204 |
+ |
case 1: rejects.add(e); return false; |
205 |
+ |
case 2: threwAt.set(e); throw new ArithmeticException(); |
206 |
+ |
default: throw new AssertionError(); |
207 |
+ |
} |
208 |
+ |
}; |
209 |
+ |
try { |
210 |
+ |
assertFalse(survivors.contains(null)); |
211 |
+ |
try { |
212 |
+ |
boolean modified = c.removeIf(randomPredicate); |
213 |
+ |
if (!modified) { |
214 |
+ |
assertNull(threwAt.get()); |
215 |
+ |
assertEquals(n, rejects.size()); |
216 |
+ |
assertEquals(0, accepts.size()); |
217 |
+ |
} |
218 |
+ |
} catch (ArithmeticException ok) {} |
219 |
+ |
survivors.removeAll(accepts); |
220 |
+ |
assertEquals(n - accepts.size(), c.size()); |
221 |
+ |
assertTrue(c.containsAll(survivors)); |
222 |
+ |
assertTrue(survivors.containsAll(rejects)); |
223 |
+ |
for (Object x : accepts) assertFalse(c.contains(x)); |
224 |
+ |
if (threwAt.get() == null) |
225 |
+ |
assertEquals(accepts.size() + rejects.size(), n); |
226 |
+ |
} catch (Throwable ex) { |
227 |
+ |
System.err.println(impl.klazz()); |
228 |
+ |
System.err.printf("c=%s%n", c); |
229 |
+ |
System.err.printf("n=%d%n", n); |
230 |
+ |
System.err.printf("accepts=%s%n", accepts); |
231 |
+ |
System.err.printf("rejects=%s%n", rejects); |
232 |
+ |
System.err.printf("survivors=%s%n", survivors); |
233 |
+ |
System.err.printf("threw=%s%n", threwAt.get()); |
234 |
+ |
throw ex; |
235 |
+ |
} |
236 |
+ |
} |
237 |
+ |
|
238 |
+ |
/** |
239 |
+ |
* Various ways of traversing a collection yield same elements |
240 |
+ |
*/ |
241 |
+ |
public void testIteratorEquivalence() { |
242 |
+ |
Collection c = impl.emptyCollection(); |
243 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
244 |
+ |
int n = rnd.nextInt(6); |
245 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
246 |
+ |
ArrayList iterated = new ArrayList(); |
247 |
+ |
ArrayList iteratedForEachRemaining = new ArrayList(); |
248 |
+ |
ArrayList tryAdvanced = new ArrayList(); |
249 |
+ |
ArrayList spliterated = new ArrayList(); |
250 |
+ |
ArrayList forEached = new ArrayList(); |
251 |
+ |
ArrayList removeIfed = new ArrayList(); |
252 |
+ |
for (Object x : c) iterated.add(x); |
253 |
+ |
c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); |
254 |
+ |
for (Spliterator s = c.spliterator(); |
255 |
+ |
s.tryAdvance(e -> tryAdvanced.add(e)); ) {} |
256 |
+ |
c.spliterator().forEachRemaining(e -> spliterated.add(e)); |
257 |
+ |
c.forEach(e -> forEached.add(e)); |
258 |
+ |
c.removeIf(e -> { removeIfed.add(e); return false; }); |
259 |
+ |
boolean ordered = |
260 |
+ |
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
261 |
+ |
if (c instanceof List || c instanceof Deque) |
262 |
+ |
assertTrue(ordered); |
263 |
+ |
if (ordered) { |
264 |
+ |
assertEquals(iterated, iteratedForEachRemaining); |
265 |
+ |
assertEquals(iterated, tryAdvanced); |
266 |
+ |
assertEquals(iterated, spliterated); |
267 |
+ |
assertEquals(iterated, forEached); |
268 |
+ |
assertEquals(iterated, removeIfed); |
269 |
+ |
} else { |
270 |
+ |
HashSet cset = new HashSet(c); |
271 |
+ |
assertEquals(cset, new HashSet(iterated)); |
272 |
+ |
assertEquals(cset, new HashSet(iteratedForEachRemaining)); |
273 |
+ |
assertEquals(cset, new HashSet(tryAdvanced)); |
274 |
+ |
assertEquals(cset, new HashSet(spliterated)); |
275 |
+ |
assertEquals(cset, new HashSet(forEached)); |
276 |
+ |
assertEquals(cset, new HashSet(removeIfed)); |
277 |
+ |
} |
278 |
+ |
if (c instanceof Deque) { |
279 |
+ |
Deque d = (Deque) c; |
280 |
+ |
ArrayList descending = new ArrayList(); |
281 |
+ |
ArrayList descendingForEachRemaining = new ArrayList(); |
282 |
+ |
for (Iterator it = d.descendingIterator(); it.hasNext(); ) |
283 |
+ |
descending.add(it.next()); |
284 |
+ |
d.descendingIterator().forEachRemaining( |
285 |
+ |
e -> descendingForEachRemaining.add(e)); |
286 |
+ |
Collections.reverse(descending); |
287 |
+ |
Collections.reverse(descendingForEachRemaining); |
288 |
+ |
assertEquals(iterated, descending); |
289 |
+ |
assertEquals(iterated, descendingForEachRemaining); |
290 |
+ |
} |
291 |
+ |
} |
292 |
+ |
|
293 |
+ |
/** |
294 |
+ |
* Calling Iterator#remove() after Iterator#forEachRemaining |
295 |
+ |
* should remove last element |
296 |
+ |
*/ |
297 |
+ |
public void testRemoveAfterForEachRemaining() { |
298 |
+ |
Collection c = impl.emptyCollection(); |
299 |
+ |
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
300 |
+ |
{ |
301 |
+ |
int n = 3 + rnd.nextInt(2); |
302 |
+ |
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
303 |
+ |
Iterator it = c.iterator(); |
304 |
+ |
assertTrue(it.hasNext()); |
305 |
+ |
assertEquals(impl.makeElement(0), it.next()); |
306 |
+ |
assertTrue(it.hasNext()); |
307 |
+ |
assertEquals(impl.makeElement(1), it.next()); |
308 |
+ |
it.forEachRemaining((e) -> {}); |
309 |
+ |
it.remove(); |
310 |
+ |
assertEquals(n - 1, c.size()); |
311 |
+ |
for (int i = 0; i < n - 1; i++) |
312 |
+ |
assertTrue(c.contains(impl.makeElement(i))); |
313 |
+ |
assertFalse(c.contains(impl.makeElement(n - 1))); |
314 |
+ |
} |
315 |
+ |
if (c instanceof Deque) { |
316 |
+ |
Deque d = (Deque) impl.emptyCollection(); |
317 |
+ |
int n = 3 + rnd.nextInt(2); |
318 |
+ |
for (int i = 0; i < n; i++) d.add(impl.makeElement(i)); |
319 |
+ |
Iterator it = d.descendingIterator(); |
320 |
+ |
assertTrue(it.hasNext()); |
321 |
+ |
assertEquals(impl.makeElement(n - 1), it.next()); |
322 |
+ |
assertTrue(it.hasNext()); |
323 |
+ |
assertEquals(impl.makeElement(n - 2), it.next()); |
324 |
+ |
it.forEachRemaining((e) -> {}); |
325 |
+ |
it.remove(); |
326 |
+ |
assertEquals(n - 1, d.size()); |
327 |
+ |
for (int i = 1; i < n; i++) |
328 |
+ |
assertTrue(d.contains(impl.makeElement(i))); |
329 |
+ |
assertFalse(d.contains(impl.makeElement(0))); |
330 |
+ |
} |
331 |
+ |
} |
332 |
+ |
|
333 |
|
/** |
334 |
|
* stream().forEach returns elements in the collection |
335 |
|
*/ |
336 |
< |
public void testForEach() throws Throwable { |
336 |
> |
public void testStreamForEach() throws Throwable { |
337 |
|
final Collection c = impl.emptyCollection(); |
338 |
|
final AtomicLong count = new AtomicLong(0L); |
339 |
|
final Object x = impl.makeElement(1); |
360 |
|
assertTrue(found.isEmpty()); |
361 |
|
} |
362 |
|
|
363 |
+ |
public void testStreamForEachConcurrentStressTest() throws Throwable { |
364 |
+ |
if (!impl.isConcurrent()) return; |
365 |
+ |
final Collection c = impl.emptyCollection(); |
366 |
+ |
final long testDurationMillis = timeoutMillis(); |
367 |
+ |
final AtomicBoolean done = new AtomicBoolean(false); |
368 |
+ |
final Object elt = impl.makeElement(1); |
369 |
+ |
final Future<?> f1, f2; |
370 |
+ |
final ExecutorService pool = Executors.newCachedThreadPool(); |
371 |
+ |
try (PoolCleaner cleaner = cleaner(pool, done)) { |
372 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
373 |
+ |
Runnable checkElt = () -> { |
374 |
+ |
threadsStarted.countDown(); |
375 |
+ |
while (!done.get()) |
376 |
+ |
c.stream().forEach((x) -> { assertSame(x, elt); }); }; |
377 |
+ |
Runnable addRemove = () -> { |
378 |
+ |
threadsStarted.countDown(); |
379 |
+ |
while (!done.get()) { |
380 |
+ |
assertTrue(c.add(elt)); |
381 |
+ |
assertTrue(c.remove(elt)); |
382 |
+ |
}}; |
383 |
+ |
f1 = pool.submit(checkElt); |
384 |
+ |
f2 = pool.submit(addRemove); |
385 |
+ |
Thread.sleep(testDurationMillis); |
386 |
+ |
} |
387 |
+ |
assertNull(f1.get(0L, MILLISECONDS)); |
388 |
+ |
assertNull(f2.get(0L, MILLISECONDS)); |
389 |
+ |
} |
390 |
+ |
|
391 |
+ |
/** |
392 |
+ |
* collection.forEach returns elements in the collection |
393 |
+ |
*/ |
394 |
+ |
public void testForEach() throws Throwable { |
395 |
+ |
final Collection c = impl.emptyCollection(); |
396 |
+ |
final AtomicLong count = new AtomicLong(0L); |
397 |
+ |
final Object x = impl.makeElement(1); |
398 |
+ |
final Object y = impl.makeElement(2); |
399 |
+ |
final ArrayList found = new ArrayList(); |
400 |
+ |
Consumer<Object> spy = (o) -> { found.add(o); }; |
401 |
+ |
c.forEach(spy); |
402 |
+ |
assertTrue(found.isEmpty()); |
403 |
+ |
|
404 |
+ |
assertTrue(c.add(x)); |
405 |
+ |
c.forEach(spy); |
406 |
+ |
assertEquals(Collections.singletonList(x), found); |
407 |
+ |
found.clear(); |
408 |
+ |
|
409 |
+ |
assertTrue(c.add(y)); |
410 |
+ |
c.forEach(spy); |
411 |
+ |
assertEquals(2, found.size()); |
412 |
+ |
assertTrue(found.contains(x)); |
413 |
+ |
assertTrue(found.contains(y)); |
414 |
+ |
found.clear(); |
415 |
+ |
|
416 |
+ |
c.clear(); |
417 |
+ |
c.forEach(spy); |
418 |
+ |
assertTrue(found.isEmpty()); |
419 |
+ |
} |
420 |
+ |
|
421 |
|
public void testForEachConcurrentStressTest() throws Throwable { |
422 |
|
if (!impl.isConcurrent()) return; |
423 |
|
final Collection c = impl.emptyCollection(); |
424 |
< |
final long testDurationMillis = SHORT_DELAY_MS; |
424 |
> |
final long testDurationMillis = timeoutMillis(); |
425 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
426 |
|
final Object elt = impl.makeElement(1); |
427 |
< |
ExecutorService pool = Executors.newCachedThreadPool(); |
428 |
< |
Runnable checkElt = () -> { |
429 |
< |
while (!done.get()) |
430 |
< |
c.stream().forEach((x) -> { assertSame(x, elt); }); }; |
431 |
< |
Runnable addRemove = () -> { |
432 |
< |
while (!done.get()) { |
433 |
< |
assertTrue(c.add(elt)); |
434 |
< |
assertTrue(c.remove(elt)); |
435 |
< |
}}; |
436 |
< |
Future<?> f1 = pool.submit(checkElt); |
437 |
< |
Future<?> f2 = pool.submit(addRemove); |
438 |
< |
Thread.sleep(testDurationMillis); |
439 |
< |
done.set(true); |
440 |
< |
pool.shutdown(); |
441 |
< |
assertTrue(pool.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
442 |
< |
assertNull(f1.get(LONG_DELAY_MS, MILLISECONDS)); |
443 |
< |
assertNull(f2.get(LONG_DELAY_MS, MILLISECONDS)); |
427 |
> |
final Future<?> f1, f2; |
428 |
> |
final ExecutorService pool = Executors.newCachedThreadPool(); |
429 |
> |
try (PoolCleaner cleaner = cleaner(pool, done)) { |
430 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(2); |
431 |
> |
Runnable checkElt = () -> { |
432 |
> |
threadsStarted.countDown(); |
433 |
> |
while (!done.get()) |
434 |
> |
c.forEach((x) -> { assertSame(x, elt); }); }; |
435 |
> |
Runnable addRemove = () -> { |
436 |
> |
threadsStarted.countDown(); |
437 |
> |
while (!done.get()) { |
438 |
> |
assertTrue(c.add(elt)); |
439 |
> |
assertTrue(c.remove(elt)); |
440 |
> |
}}; |
441 |
> |
f1 = pool.submit(checkElt); |
442 |
> |
f2 = pool.submit(addRemove); |
443 |
> |
Thread.sleep(testDurationMillis); |
444 |
> |
} |
445 |
> |
assertNull(f1.get(0L, MILLISECONDS)); |
446 |
> |
assertNull(f2.get(0L, MILLISECONDS)); |
447 |
|
} |
448 |
|
|
449 |
< |
// public void testCollection8DebugFail() { fail(); } |
449 |
> |
// public void testCollection8DebugFail() { |
450 |
> |
// fail(impl.klazz().getSimpleName()); |
451 |
> |
// } |
452 |
|
} |