61 |
|
} |
62 |
|
|
63 |
|
/** Checks properties of empty collections. */ |
64 |
< |
public void testEmptyMeansEmpty() throws InterruptedException { |
64 |
> |
public void testEmptyMeansEmpty() throws Throwable { |
65 |
|
Collection c = impl.emptyCollection(); |
66 |
|
emptyMeansEmpty(c); |
67 |
|
|
68 |
< |
if (c instanceof java.io.Serializable) |
69 |
< |
emptyMeansEmpty(serialClone(c)); |
68 |
> |
if (c instanceof java.io.Serializable) { |
69 |
> |
try { |
70 |
> |
emptyMeansEmpty(serialClonePossiblyFailing(c)); |
71 |
> |
} catch (java.io.NotSerializableException ex) { |
72 |
> |
// excusable when we have a serializable wrapper around |
73 |
> |
// a non-serializable collection, as can happen with: |
74 |
> |
// Vector.subList() => wrapped AbstractList$RandomAccessSubList |
75 |
> |
if (testImplementationDetails |
76 |
> |
&& (! c.getClass().getName().matches( |
77 |
> |
"java.util.Collections.*"))) |
78 |
> |
throw ex; |
79 |
> |
} |
80 |
> |
} |
81 |
|
|
82 |
|
Collection clone = cloneableClone(c); |
83 |
|
if (clone != null) |
252 |
|
|
253 |
|
public void testRemoveIf() { |
254 |
|
Collection c = impl.emptyCollection(); |
255 |
+ |
boolean ordered = |
256 |
+ |
c.spliterator().hasCharacteristics(Spliterator.ORDERED); |
257 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
258 |
|
int n = rnd.nextInt(6); |
259 |
|
for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); |
285 |
|
assertEquals(modified, accepts.size() > 0); |
286 |
|
assertEquals(modified, rejects.size() != n); |
287 |
|
assertEquals(accepts.size() + rejects.size(), n); |
288 |
< |
assertEquals(rejects, Arrays.asList(c.toArray())); |
288 |
> |
if (ordered) { |
289 |
> |
assertEquals(rejects, |
290 |
> |
Arrays.asList(c.toArray())); |
291 |
> |
} else { |
292 |
> |
assertEquals(new HashSet(rejects), |
293 |
> |
new HashSet(Arrays.asList(c.toArray()))); |
294 |
> |
} |
295 |
|
} catch (ArithmeticException ok) { |
296 |
|
assertNotNull(threwAt.get()); |
297 |
|
assertTrue(c.contains(threwAt.get())); |
312 |
|
assertTrue(c.containsAll(rejects)); |
313 |
|
assertTrue(c.containsAll(survivors)); |
314 |
|
assertTrue(survivors.containsAll(rejects)); |
315 |
< |
assertEquals(n - accepts.size(), c.size()); |
316 |
< |
for (Object x : accepts) assertFalse(c.contains(x)); |
315 |
> |
if (threwAt.get() == null) { |
316 |
> |
assertEquals(n - accepts.size(), c.size()); |
317 |
> |
for (Object x : accepts) assertFalse(c.contains(x)); |
318 |
> |
} else { |
319 |
> |
// Two acceptable behaviors: entire removeIf call is one |
320 |
> |
// transaction, or each element processed is one transaction. |
321 |
> |
assertTrue(n == c.size() || n == c.size() + accepts.size()); |
322 |
> |
int k = 0; |
323 |
> |
for (Object x : accepts) if (c.contains(x)) k++; |
324 |
> |
assertTrue(k == accepts.size() || k == 0); |
325 |
> |
} |
326 |
|
} catch (Throwable ex) { |
327 |
|
System.err.println(impl.klazz()); |
328 |
< |
System.err.printf("c=%s%n", c); |
328 |
> |
// c is at risk of corruption if we got here, so be lenient |
329 |
> |
try { System.err.printf("c=%s%n", c); } |
330 |
> |
catch (Throwable t) { t.printStackTrace(); } |
331 |
|
System.err.printf("n=%d%n", n); |
332 |
|
System.err.printf("orig=%s%n", orig); |
333 |
|
System.err.printf("accepts=%s%n", accepts); |
395 |
|
|
396 |
|
/** |
397 |
|
* Calling Iterator#remove() after Iterator#forEachRemaining |
398 |
< |
* should remove last element |
398 |
> |
* should (maybe) remove last element |
399 |
|
*/ |
400 |
|
public void testRemoveAfterForEachRemaining() { |
401 |
|
Collection c = impl.emptyCollection(); |
408 |
|
assertEquals(impl.makeElement(0), it.next()); |
409 |
|
assertTrue(it.hasNext()); |
410 |
|
assertEquals(impl.makeElement(1), it.next()); |
411 |
< |
it.forEachRemaining(e -> {}); |
412 |
< |
it.remove(); |
413 |
< |
assertEquals(n - 1, c.size()); |
414 |
< |
for (int i = 0; i < n - 1; i++) |
415 |
< |
assertTrue(c.contains(impl.makeElement(i))); |
416 |
< |
assertFalse(c.contains(impl.makeElement(n - 1))); |
411 |
> |
it.forEachRemaining(e -> assertTrue(c.contains(e))); |
412 |
> |
if (testImplementationDetails) { |
413 |
> |
if (c instanceof java.util.concurrent.ArrayBlockingQueue) { |
414 |
> |
assertIteratorExhausted(it); |
415 |
> |
} else { |
416 |
> |
it.remove(); |
417 |
> |
assertEquals(n - 1, c.size()); |
418 |
> |
for (int i = 0; i < n - 1; i++) |
419 |
> |
assertTrue(c.contains(impl.makeElement(i))); |
420 |
> |
assertFalse(c.contains(impl.makeElement(n - 1))); |
421 |
> |
} |
422 |
> |
} |
423 |
|
} |
424 |
|
if (c instanceof Deque) { |
425 |
|
Deque d = (Deque) impl.emptyCollection(); |
430 |
|
assertEquals(impl.makeElement(n - 1), it.next()); |
431 |
|
assertTrue(it.hasNext()); |
432 |
|
assertEquals(impl.makeElement(n - 2), it.next()); |
433 |
< |
it.forEachRemaining(e -> {}); |
434 |
< |
it.remove(); |
435 |
< |
assertEquals(n - 1, d.size()); |
436 |
< |
for (int i = 1; i < n; i++) |
437 |
< |
assertTrue(d.contains(impl.makeElement(i))); |
438 |
< |
assertFalse(d.contains(impl.makeElement(0))); |
433 |
> |
it.forEachRemaining(e -> assertTrue(c.contains(e))); |
434 |
> |
if (testImplementationDetails) { |
435 |
> |
it.remove(); |
436 |
> |
assertEquals(n - 1, d.size()); |
437 |
> |
for (int i = 1; i < n; i++) |
438 |
> |
assertTrue(d.contains(impl.makeElement(i))); |
439 |
> |
assertFalse(d.contains(impl.makeElement(0))); |
440 |
> |
} |
441 |
|
} |
442 |
|
} |
443 |
|
|
450 |
|
final Object x = impl.makeElement(1); |
451 |
|
final Object y = impl.makeElement(2); |
452 |
|
final ArrayList found = new ArrayList(); |
453 |
< |
Consumer<Object> spy = o -> { found.add(o); }; |
453 |
> |
Consumer<Object> spy = o -> found.add(o); |
454 |
|
c.stream().forEach(spy); |
455 |
|
assertTrue(found.isEmpty()); |
456 |
|
|
484 |
|
Runnable checkElt = () -> { |
485 |
|
threadsStarted.countDown(); |
486 |
|
while (!done.get()) |
487 |
< |
c.stream().forEach(x -> { assertSame(x, elt); }); }; |
487 |
> |
c.stream().forEach(x -> assertSame(x, elt)); }; |
488 |
|
Runnable addRemove = () -> { |
489 |
|
threadsStarted.countDown(); |
490 |
|
while (!done.get()) { |
508 |
|
final Object x = impl.makeElement(1); |
509 |
|
final Object y = impl.makeElement(2); |
510 |
|
final ArrayList found = new ArrayList(); |
511 |
< |
Consumer<Object> spy = o -> { found.add(o); }; |
511 |
> |
Consumer<Object> spy = o -> found.add(o); |
512 |
|
c.forEach(spy); |
513 |
|
assertTrue(found.isEmpty()); |
514 |
|
|
542 |
|
Runnable checkElt = () -> { |
543 |
|
threadsStarted.countDown(); |
544 |
|
while (!done.get()) |
545 |
< |
c.forEach(x -> { assertSame(x, elt); }); }; |
545 |
> |
c.forEach(x -> assertSame(x, elt)); }; |
546 |
|
Runnable addRemove = () -> { |
547 |
|
threadsStarted.countDown(); |
548 |
|
while (!done.get()) { |