ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Collection8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/Collection8Test.java (file contents):
Revision 1.16 by jsr166, Sun Nov 6 05:45:09 2016 UTC vs.
Revision 1.23 by jsr166, Mon Nov 14 21:33:58 2016 UTC

# Line 9 | Line 9 | import static java.util.concurrent.TimeU
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11   import java.util.ArrayList;
12 + import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Collections;
15   import java.util.Deque;
# Line 97 | Line 98 | public class Collection8Test extends JSR
98              assertSame(3, a[2]);
99          }
100          assertIteratorExhausted(c.iterator());
101 <        Consumer alwaysThrows = (e) -> { throw new AssertionError(); };
101 >        Consumer alwaysThrows = e -> { throw new AssertionError(); };
102          c.forEach(alwaysThrows);
103          c.iterator().forEachRemaining(alwaysThrows);
104          c.spliterator().forEachRemaining(alwaysThrows);
# Line 240 | Line 241 | public class Collection8Test extends JSR
241  
242      public void testRemoveIf() {
243          Collection c = impl.emptyCollection();
244 +        boolean ordered =
245 +            c.spliterator().hasCharacteristics(Spliterator.ORDERED);
246          ThreadLocalRandom rnd = ThreadLocalRandom.current();
247          int n = rnd.nextInt(6);
248          for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
249          AtomicReference threwAt = new AtomicReference(null);
250 <        ArrayList survivors = new ArrayList(c);
250 >        List orig = rnd.nextBoolean()
251 >            ? new ArrayList(c)
252 >            : Arrays.asList(c.toArray());
253 >
254 >        // Merely creating an iterator can change ArrayBlockingQueue behavior
255 >        Iterator it = rnd.nextBoolean() ? c.iterator() : null;
256 >
257 >        ArrayList survivors = new ArrayList();
258          ArrayList accepts = new ArrayList();
259          ArrayList rejects = new ArrayList();
260 <        Predicate randomPredicate = (e) -> {
260 >
261 >        Predicate randomPredicate = e -> {
262              assertNull(threwAt.get());
263              switch (rnd.nextInt(3)) {
264              case 0: accepts.add(e); return true;
# Line 257 | Line 268 | public class Collection8Test extends JSR
268              }
269          };
270          try {
260            assertFalse(survivors.contains(null));
271              try {
272                  boolean modified = c.removeIf(randomPredicate);
273 <                if (!modified) {
274 <                    assertNull(threwAt.get());
275 <                    assertEquals(n, rejects.size());
276 <                    assertEquals(0, accepts.size());
273 >                assertNull(threwAt.get());
274 >                assertEquals(modified, accepts.size() > 0);
275 >                assertEquals(modified, rejects.size() != n);
276 >                assertEquals(accepts.size() + rejects.size(), n);
277 >                if (ordered) {
278 >                    assertEquals(rejects,
279 >                                 Arrays.asList(c.toArray()));
280 >                } else {
281 >                    assertEquals(new HashSet(rejects),
282 >                                 new HashSet(Arrays.asList(c.toArray())));
283                  }
284 <            } catch (ArithmeticException ok) {}
285 <            survivors.removeAll(accepts);
286 <            assertEquals(n - accepts.size(), c.size());
284 >            } catch (ArithmeticException ok) {
285 >                assertNotNull(threwAt.get());
286 >                assertTrue(c.contains(threwAt.get()));
287 >            }
288 >            if (it != null && impl.isConcurrent())
289 >                // check for weakly consistent iterator
290 >                while (it.hasNext()) assertTrue(orig.contains(it.next()));
291 >            switch (rnd.nextInt(4)) {
292 >            case 0: survivors.addAll(c); break;
293 >            case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
294 >            case 2: c.forEach(e -> survivors.add(e)); break;
295 >            case 3: for (Object e : c) survivors.add(e); break;
296 >            }
297 >            assertTrue(orig.containsAll(accepts));
298 >            assertTrue(orig.containsAll(rejects));
299 >            assertTrue(orig.containsAll(survivors));
300 >            assertTrue(orig.containsAll(c));
301 >            assertTrue(c.containsAll(rejects));
302              assertTrue(c.containsAll(survivors));
303              assertTrue(survivors.containsAll(rejects));
304 <            for (Object x : accepts) assertFalse(c.contains(x));
305 <            if (threwAt.get() == null)
306 <                assertEquals(accepts.size() + rejects.size(), n);
304 >            if (threwAt.get() == null) {
305 >                assertEquals(n - accepts.size(), c.size());
306 >                for (Object x : accepts) assertFalse(c.contains(x));
307 >            } else {
308 >                // Two acceptable behaviors: entire removeIf call is one
309 >                // transaction, or each element processed is one transaction.
310 >                assertTrue(n == c.size() || n == c.size() + accepts.size());
311 >                int k = 0;
312 >                for (Object x : accepts) if (c.contains(x)) k++;
313 >                assertTrue(k == accepts.size() || k == 0);
314 >            }
315          } catch (Throwable ex) {
316              System.err.println(impl.klazz());
317 <            System.err.printf("c=%s%n", c);
317 >            // c is at risk of corruption if we got here, so be lenient
318 >            try { System.err.printf("c=%s%n", c); }
319 >            catch (Throwable t) { t.printStackTrace(); }
320              System.err.printf("n=%d%n", n);
321 +            System.err.printf("orig=%s%n", orig);
322              System.err.printf("accepts=%s%n", accepts);
323              System.err.printf("rejects=%s%n", rejects);
324              System.err.printf("survivors=%s%n", survivors);
325 <            System.err.printf("threw=%s%n", threwAt.get());
325 >            System.err.printf("threwAt=%s%n", threwAt.get());
326              throw ex;
327          }
328      }
# Line 342 | Line 384 | public class Collection8Test extends JSR
384  
385      /**
386       * Calling Iterator#remove() after Iterator#forEachRemaining
387 <     * should remove last element
387 >     * should (maybe) remove last element
388       */
389      public void testRemoveAfterForEachRemaining() {
390          Collection c = impl.emptyCollection();
# Line 355 | Line 397 | public class Collection8Test extends JSR
397              assertEquals(impl.makeElement(0), it.next());
398              assertTrue(it.hasNext());
399              assertEquals(impl.makeElement(1), it.next());
400 <            it.forEachRemaining((e) -> {});
401 <            it.remove();
402 <            assertEquals(n - 1, c.size());
403 <            for (int i = 0; i < n - 1; i++)
404 <                assertTrue(c.contains(impl.makeElement(i)));
405 <            assertFalse(c.contains(impl.makeElement(n - 1)));
400 >            it.forEachRemaining(e -> assertTrue(c.contains(e)));
401 >            if (testImplementationDetails) {
402 >                if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
403 >                    assertIteratorExhausted(it);
404 >                } else {
405 >                    it.remove();
406 >                    assertEquals(n - 1, c.size());
407 >                    for (int i = 0; i < n - 1; i++)
408 >                        assertTrue(c.contains(impl.makeElement(i)));
409 >                    assertFalse(c.contains(impl.makeElement(n - 1)));
410 >                }
411 >            }
412          }
413          if (c instanceof Deque) {
414              Deque d = (Deque) impl.emptyCollection();
# Line 371 | Line 419 | public class Collection8Test extends JSR
419              assertEquals(impl.makeElement(n - 1), it.next());
420              assertTrue(it.hasNext());
421              assertEquals(impl.makeElement(n - 2), it.next());
422 <            it.forEachRemaining((e) -> {});
423 <            it.remove();
424 <            assertEquals(n - 1, d.size());
425 <            for (int i = 1; i < n; i++)
426 <                assertTrue(d.contains(impl.makeElement(i)));
427 <            assertFalse(d.contains(impl.makeElement(0)));
422 >            it.forEachRemaining(e -> assertTrue(c.contains(e)));
423 >            if (testImplementationDetails) {
424 >                it.remove();
425 >                assertEquals(n - 1, d.size());
426 >                for (int i = 1; i < n; i++)
427 >                    assertTrue(d.contains(impl.makeElement(i)));
428 >                assertFalse(d.contains(impl.makeElement(0)));
429 >            }
430          }
431      }
432  
# Line 389 | Line 439 | public class Collection8Test extends JSR
439          final Object x = impl.makeElement(1);
440          final Object y = impl.makeElement(2);
441          final ArrayList found = new ArrayList();
442 <        Consumer<Object> spy = (o) -> { found.add(o); };
442 >        Consumer<Object> spy = o -> found.add(o);
443          c.stream().forEach(spy);
444          assertTrue(found.isEmpty());
445  
# Line 423 | Line 473 | public class Collection8Test extends JSR
473              Runnable checkElt = () -> {
474                  threadsStarted.countDown();
475                  while (!done.get())
476 <                    c.stream().forEach((x) -> { assertSame(x, elt); }); };
476 >                    c.stream().forEach(x -> assertSame(x, elt)); };
477              Runnable addRemove = () -> {
478                  threadsStarted.countDown();
479                  while (!done.get()) {
# Line 447 | Line 497 | public class Collection8Test extends JSR
497          final Object x = impl.makeElement(1);
498          final Object y = impl.makeElement(2);
499          final ArrayList found = new ArrayList();
500 <        Consumer<Object> spy = (o) -> { found.add(o); };
500 >        Consumer<Object> spy = o -> found.add(o);
501          c.forEach(spy);
502          assertTrue(found.isEmpty());
503  
# Line 481 | Line 531 | public class Collection8Test extends JSR
531              Runnable checkElt = () -> {
532                  threadsStarted.countDown();
533                  while (!done.get())
534 <                    c.forEach((x) -> { assertSame(x, elt); }); };
534 >                    c.forEach(x -> assertSame(x, elt)); };
535              Runnable addRemove = () -> {
536                  threadsStarted.countDown();
537                  while (!done.get()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines