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.21 by jsr166, Mon Nov 7 00:37:53 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 244 | Line 245 | public class Collection8Test extends JSR
245          int n = rnd.nextInt(6);
246          for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
247          AtomicReference threwAt = new AtomicReference(null);
248 <        ArrayList survivors = new ArrayList(c);
248 >        List orig = rnd.nextBoolean()
249 >            ? new ArrayList(c)
250 >            : Arrays.asList(c.toArray());
251 >
252 >        // Merely creating an iterator can change ArrayBlockingQueue behavior
253 >        Iterator it = rnd.nextBoolean() ? c.iterator() : null;
254 >
255 >        ArrayList survivors = new ArrayList();
256          ArrayList accepts = new ArrayList();
257          ArrayList rejects = new ArrayList();
258 <        Predicate randomPredicate = (e) -> {
258 >
259 >        Predicate randomPredicate = e -> {
260              assertNull(threwAt.get());
261              switch (rnd.nextInt(3)) {
262              case 0: accepts.add(e); return true;
# Line 257 | Line 266 | public class Collection8Test extends JSR
266              }
267          };
268          try {
260            assertFalse(survivors.contains(null));
269              try {
270                  boolean modified = c.removeIf(randomPredicate);
271 <                if (!modified) {
272 <                    assertNull(threwAt.get());
273 <                    assertEquals(n, rejects.size());
274 <                    assertEquals(0, accepts.size());
275 <                }
276 <            } catch (ArithmeticException ok) {}
277 <            survivors.removeAll(accepts);
278 <            assertEquals(n - accepts.size(), c.size());
271 >                assertNull(threwAt.get());
272 >                assertEquals(modified, accepts.size() > 0);
273 >                assertEquals(modified, rejects.size() != n);
274 >                assertEquals(accepts.size() + rejects.size(), n);
275 >                assertEquals(rejects, Arrays.asList(c.toArray()));
276 >            } catch (ArithmeticException ok) {
277 >                assertNotNull(threwAt.get());
278 >                assertTrue(c.contains(threwAt.get()));
279 >            }
280 >            if (it != null && impl.isConcurrent())
281 >                // check for weakly consistent iterator
282 >                while (it.hasNext()) assertTrue(orig.contains(it.next()));
283 >            switch (rnd.nextInt(4)) {
284 >            case 0: survivors.addAll(c); break;
285 >            case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
286 >            case 2: c.forEach(e -> survivors.add(e)); break;
287 >            case 3: for (Object e : c) survivors.add(e); break;
288 >            }
289 >            assertTrue(orig.containsAll(accepts));
290 >            assertTrue(orig.containsAll(rejects));
291 >            assertTrue(orig.containsAll(survivors));
292 >            assertTrue(orig.containsAll(c));
293 >            assertTrue(c.containsAll(rejects));
294              assertTrue(c.containsAll(survivors));
295              assertTrue(survivors.containsAll(rejects));
296 +            assertEquals(n - accepts.size(), c.size());
297              for (Object x : accepts) assertFalse(c.contains(x));
274            if (threwAt.get() == null)
275                assertEquals(accepts.size() + rejects.size(), n);
298          } catch (Throwable ex) {
299              System.err.println(impl.klazz());
300              System.err.printf("c=%s%n", c);
301              System.err.printf("n=%d%n", n);
302 +            System.err.printf("orig=%s%n", orig);
303              System.err.printf("accepts=%s%n", accepts);
304              System.err.printf("rejects=%s%n", rejects);
305              System.err.printf("survivors=%s%n", survivors);
306 <            System.err.printf("threw=%s%n", threwAt.get());
306 >            System.err.printf("threwAt=%s%n", threwAt.get());
307              throw ex;
308          }
309      }
# Line 342 | Line 365 | public class Collection8Test extends JSR
365  
366      /**
367       * Calling Iterator#remove() after Iterator#forEachRemaining
368 <     * should remove last element
368 >     * should (maybe) remove last element
369       */
370      public void testRemoveAfterForEachRemaining() {
371          Collection c = impl.emptyCollection();
# Line 355 | Line 378 | public class Collection8Test extends JSR
378              assertEquals(impl.makeElement(0), it.next());
379              assertTrue(it.hasNext());
380              assertEquals(impl.makeElement(1), it.next());
381 <            it.forEachRemaining((e) -> {});
382 <            it.remove();
383 <            assertEquals(n - 1, c.size());
384 <            for (int i = 0; i < n - 1; i++)
385 <                assertTrue(c.contains(impl.makeElement(i)));
386 <            assertFalse(c.contains(impl.makeElement(n - 1)));
381 >            it.forEachRemaining(e -> assertTrue(c.contains(e)));
382 >            if (testImplementationDetails) {
383 >                if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
384 >                    assertIteratorExhausted(it);
385 >                } else {
386 >                    it.remove();
387 >                    assertEquals(n - 1, c.size());
388 >                    for (int i = 0; i < n - 1; i++)
389 >                        assertTrue(c.contains(impl.makeElement(i)));
390 >                    assertFalse(c.contains(impl.makeElement(n - 1)));
391 >                }
392 >            }
393          }
394          if (c instanceof Deque) {
395              Deque d = (Deque) impl.emptyCollection();
# Line 371 | Line 400 | public class Collection8Test extends JSR
400              assertEquals(impl.makeElement(n - 1), it.next());
401              assertTrue(it.hasNext());
402              assertEquals(impl.makeElement(n - 2), it.next());
403 <            it.forEachRemaining((e) -> {});
404 <            it.remove();
405 <            assertEquals(n - 1, d.size());
406 <            for (int i = 1; i < n; i++)
407 <                assertTrue(d.contains(impl.makeElement(i)));
408 <            assertFalse(d.contains(impl.makeElement(0)));
403 >            it.forEachRemaining(e -> assertTrue(c.contains(e)));
404 >            if (testImplementationDetails) {
405 >                it.remove();
406 >                assertEquals(n - 1, d.size());
407 >                for (int i = 1; i < n; i++)
408 >                    assertTrue(d.contains(impl.makeElement(i)));
409 >                assertFalse(d.contains(impl.makeElement(0)));
410 >            }
411          }
412      }
413  
# Line 389 | Line 420 | public class Collection8Test extends JSR
420          final Object x = impl.makeElement(1);
421          final Object y = impl.makeElement(2);
422          final ArrayList found = new ArrayList();
423 <        Consumer<Object> spy = (o) -> { found.add(o); };
423 >        Consumer<Object> spy = o -> found.add(o);
424          c.stream().forEach(spy);
425          assertTrue(found.isEmpty());
426  
# Line 423 | Line 454 | public class Collection8Test extends JSR
454              Runnable checkElt = () -> {
455                  threadsStarted.countDown();
456                  while (!done.get())
457 <                    c.stream().forEach((x) -> { assertSame(x, elt); }); };
457 >                    c.stream().forEach(x -> assertSame(x, elt)); };
458              Runnable addRemove = () -> {
459                  threadsStarted.countDown();
460                  while (!done.get()) {
# Line 447 | Line 478 | public class Collection8Test extends JSR
478          final Object x = impl.makeElement(1);
479          final Object y = impl.makeElement(2);
480          final ArrayList found = new ArrayList();
481 <        Consumer<Object> spy = (o) -> { found.add(o); };
481 >        Consumer<Object> spy = o -> found.add(o);
482          c.forEach(spy);
483          assertTrue(found.isEmpty());
484  
# Line 481 | Line 512 | public class Collection8Test extends JSR
512              Runnable checkElt = () -> {
513                  threadsStarted.countDown();
514                  while (!done.get())
515 <                    c.forEach((x) -> { assertSame(x, elt); }); };
515 >                    c.forEach(x -> assertSame(x, elt)); };
516              Runnable addRemove = () -> {
517                  threadsStarted.countDown();
518                  while (!done.get()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines