550 |
|
assertTrue(found.isEmpty()); |
551 |
|
} |
552 |
|
|
553 |
+ |
/** TODO: promote to a common utility */ |
554 |
+ |
static <T> T chooseOne(T ... ts) { |
555 |
+ |
return ts[ThreadLocalRandom.current().nextInt(ts.length)]; |
556 |
+ |
} |
557 |
+ |
|
558 |
+ |
/** TODO: more random adders and removers */ |
559 |
+ |
static <E> Runnable adderRemover(Collection<E> c, E e) { |
560 |
+ |
return chooseOne( |
561 |
+ |
() -> { |
562 |
+ |
assertTrue(c.add(e)); |
563 |
+ |
assertTrue(c.contains(e)); |
564 |
+ |
assertTrue(c.remove(e)); |
565 |
+ |
assertFalse(c.contains(e)); |
566 |
+ |
}, |
567 |
+ |
() -> { |
568 |
+ |
assertTrue(c.add(e)); |
569 |
+ |
assertTrue(c.contains(e)); |
570 |
+ |
assertTrue(c.removeIf(x -> x == e)); |
571 |
+ |
assertFalse(c.contains(e)); |
572 |
+ |
}, |
573 |
+ |
() -> { |
574 |
+ |
assertTrue(c.add(e)); |
575 |
+ |
assertTrue(c.contains(e)); |
576 |
+ |
for (Iterator it = c.iterator();; ) |
577 |
+ |
if (it.next() == e) { |
578 |
+ |
try { it.remove(); } |
579 |
+ |
catch (UnsupportedOperationException ok) { |
580 |
+ |
c.remove(e); |
581 |
+ |
} |
582 |
+ |
break; |
583 |
+ |
} |
584 |
+ |
assertFalse(c.contains(e)); |
585 |
+ |
}); |
586 |
+ |
} |
587 |
+ |
|
588 |
|
/** |
589 |
|
* Motley crew of threads concurrently randomly hammer the collection. |
590 |
|
*/ |
624 |
|
() -> checkArraySanity.accept(c.toArray()), |
625 |
|
() -> checkArraySanity.accept(c.toArray(emptyArray)), |
626 |
|
() -> { |
627 |
< |
assertTrue(c.add(one)); |
628 |
< |
assertTrue(c.contains(one)); |
629 |
< |
assertTrue(c.remove(one)); |
630 |
< |
assertFalse(c.contains(one)); |
631 |
< |
}, |
632 |
< |
() -> { |
633 |
< |
assertTrue(c.add(two)); |
634 |
< |
assertTrue(c.contains(two)); |
635 |
< |
assertTrue(c.remove(two)); |
636 |
< |
assertFalse(c.contains(two)); |
637 |
< |
}, |
627 |
> |
Object[] a = new Object[5]; |
628 |
> |
Object three = impl.makeElement(3); |
629 |
> |
Arrays.fill(a, 0, a.length, three); |
630 |
> |
Object[] x = c.toArray(a); |
631 |
> |
if (x == a) |
632 |
> |
for (int i = 0; i < a.length && a[i] != null; i++) |
633 |
> |
checkSanity.accept(a[i]); |
634 |
> |
// A careful reading of the spec does not support: |
635 |
> |
// for (i++; i < a.length; i++) assertSame(three, a[i]); |
636 |
> |
else |
637 |
> |
checkArraySanity.accept(x); |
638 |
> |
}, |
639 |
> |
adderRemover(c, one), |
640 |
> |
adderRemover(c, two), |
641 |
|
}; |
642 |
|
final List<Runnable> tasks = |
643 |
|
Arrays.stream(frobbers) |