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.26 by jsr166, Tue Nov 15 22:59:09 2016 UTC vs.
Revision 1.33 by jsr166, Mon Nov 28 15:31:40 2016 UTC

# Line 21 | Line 21 | import java.util.Queue;
21   import java.util.Spliterator;
22   import java.util.concurrent.BlockingDeque;
23   import java.util.concurrent.BlockingQueue;
24 + import java.util.concurrent.ConcurrentLinkedQueue;
25   import java.util.concurrent.CountDownLatch;
26   import java.util.concurrent.Executors;
27   import java.util.concurrent.ExecutorService;
# Line 304 | Line 305 | public class Collection8Test extends JSR
305              switch (rnd.nextInt(4)) {
306              case 0: survivors.addAll(c); break;
307              case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
308 <            case 2: c.forEach(e -> survivors.add(e)); break;
308 >            case 2: c.forEach(survivors::add); break;
309              case 3: for (Object e : c) survivors.add(e); break;
310              }
311              assertTrue(orig.containsAll(accepts));
# Line 352 | Line 353 | public class Collection8Test extends JSR
353          ArrayList iteratedForEachRemaining = new ArrayList();
354          ArrayList tryAdvanced = new ArrayList();
355          ArrayList spliterated = new ArrayList();
356 +        ArrayList splitonced = new ArrayList();
357          ArrayList forEached = new ArrayList();
358 +        ArrayList streamForEached = new ArrayList();
359 +        ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue();
360          ArrayList removeIfed = new ArrayList();
361          for (Object x : c) iterated.add(x);
362 <        c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e));
362 >        c.iterator().forEachRemaining(iteratedForEachRemaining::add);
363          for (Spliterator s = c.spliterator();
364 <             s.tryAdvance(e -> tryAdvanced.add(e)); ) {}
365 <        c.spliterator().forEachRemaining(e -> spliterated.add(e));
366 <        c.forEach(e -> forEached.add(e));
364 >             s.tryAdvance(tryAdvanced::add); ) {}
365 >        c.spliterator().forEachRemaining(spliterated::add);
366 >        {                       // trySplit returns "strict prefix"
367 >            Spliterator s1 = c.spliterator(), s2 = s1.trySplit();
368 >            if (s2 != null) s2.forEachRemaining(splitonced::add);
369 >            s1.forEachRemaining(splitonced::add);
370 >        }
371 >        c.forEach(forEached::add);
372 >        c.stream().forEach(streamForEached::add);
373 >        c.parallelStream().forEach(parallelStreamForEached::add);
374          c.removeIf(e -> { removeIfed.add(e); return false; });
375          boolean ordered =
376              c.spliterator().hasCharacteristics(Spliterator.ORDERED);
377          if (c instanceof List || c instanceof Deque)
378              assertTrue(ordered);
379 +        HashSet cset = new HashSet(c);
380 +        assertEquals(cset, new HashSet(parallelStreamForEached));
381          if (ordered) {
382              assertEquals(iterated, iteratedForEachRemaining);
383              assertEquals(iterated, tryAdvanced);
384              assertEquals(iterated, spliterated);
385 +            assertEquals(iterated, splitonced);
386              assertEquals(iterated, forEached);
387 +            assertEquals(iterated, streamForEached);
388              assertEquals(iterated, removeIfed);
389          } else {
375            HashSet cset = new HashSet(c);
390              assertEquals(cset, new HashSet(iterated));
391              assertEquals(cset, new HashSet(iteratedForEachRemaining));
392              assertEquals(cset, new HashSet(tryAdvanced));
393              assertEquals(cset, new HashSet(spliterated));
394 +            assertEquals(cset, new HashSet(splitonced));
395              assertEquals(cset, new HashSet(forEached));
396 +            assertEquals(cset, new HashSet(streamForEached));
397              assertEquals(cset, new HashSet(removeIfed));
398          }
399          if (c instanceof Deque) {
# Line 545 | Line 561 | public class Collection8Test extends JSR
561          final AtomicBoolean done = new AtomicBoolean(false);
562          final Object one = impl.makeElement(1);
563          final Object two = impl.makeElement(2);
564 +        final Object[] emptyArray =
565 +            (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
566          final List<Future<?>> futures;
567          final Phaser threadsStarted = new Phaser(1); // register this thread
568 <        final List<Runnable> tasks = List.<Runnable>of(
569 <            () -> c.forEach(x -> assertTrue(x == one || x == two)),
570 <            () -> c.stream().forEach(x -> assertTrue(x == one || x == two)),
568 >        final Consumer checkSanity = x -> assertTrue(x == one || x == two);
569 >        final Runnable[] frobbers = {
570 >            () -> c.forEach(checkSanity),
571 >            () -> c.stream().forEach(checkSanity),
572 >            () -> c.parallelStream().forEach(checkSanity),
573              () -> c.spliterator().trySplit(),
574              () -> {
575                  Spliterator s = c.spliterator();
576 <                s.tryAdvance(x -> assertTrue(x == one || x == two));
576 >                s.tryAdvance(checkSanity);
577                  s.trySplit();
578              },
579              () -> {
580                  Spliterator s = c.spliterator();
581 <                do {} while (s.tryAdvance(x -> assertTrue(x == one || x == two)));
562 <            },
563 <            () -> {
564 <                for (Object x : c) assertTrue(x == one || x == two);
581 >                do {} while (s.tryAdvance(checkSanity));
582              },
583 +            () -> { for (Object x : c) checkSanity.accept(x); },
584 +            () -> { for (Object x : c.toArray()) checkSanity.accept(x); },
585 +            () -> { for (Object x : c.toArray(emptyArray)) checkSanity.accept(x); },
586              () -> {
587                  assertTrue(c.add(one));
588                  assertTrue(c.contains(one));
# Line 574 | Line 594 | public class Collection8Test extends JSR
594                  assertTrue(c.contains(two));
595                  assertTrue(c.remove(two));
596                  assertFalse(c.contains(two));
597 <            })
598 <            .stream()
597 >            },
598 >        };
599 >        final List<Runnable> tasks =
600 >            Arrays.stream(frobbers)
601              .filter(task -> rnd.nextBoolean()) // random subset
602              .map(task -> (Runnable) () -> {
603                       threadsStarted.arriveAndAwaitAdvance();
# Line 587 | Line 609 | public class Collection8Test extends JSR
609          try (PoolCleaner cleaner = cleaner(pool, done)) {
610              threadsStarted.bulkRegister(tasks.size());
611              futures = tasks.stream()
612 <                .map(task -> pool.submit(task))
612 >                .map(pool::submit)
613                  .collect(Collectors.toList());
614              threadsStarted.arriveAndDeregister();
615              Thread.sleep(testDurationMillis);
# Line 596 | Line 618 | public class Collection8Test extends JSR
618              assertNull(future.get(0L, MILLISECONDS));
619      }
620  
621 +    /**
622 +     * Spliterators are either IMMUTABLE or truly late-binding or, if
623 +     * concurrent, use the same "late-binding style" of returning
624 +     * elements added between creation and first use.
625 +     */
626 +    public void testLateBindingStyle() {
627 +        if (!testImplementationDetails) return;
628 +        if (impl.klazz() == ArrayList.class) return; // for jdk8
629 +        // Immutable (snapshot) spliterators are exempt
630 +        if (impl.emptyCollection().spliterator()
631 +            .hasCharacteristics(Spliterator.IMMUTABLE))
632 +            return;
633 +        final Object one = impl.makeElement(1);
634 +        {
635 +            final Collection c = impl.emptyCollection();
636 +            final Spliterator split = c.spliterator();
637 +            c.add(one);
638 +            assertTrue(split.tryAdvance(e -> { assertSame(e, one); }));
639 +            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
640 +            assertTrue(c.contains(one));
641 +        }
642 +        {
643 +            final AtomicLong count = new AtomicLong(0);
644 +            final Collection c = impl.emptyCollection();
645 +            final Spliterator split = c.spliterator();
646 +            c.add(one);
647 +            split.forEachRemaining(
648 +                e -> { assertSame(e, one); count.getAndIncrement(); });
649 +            assertEquals(1L, count.get());
650 +            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
651 +            assertTrue(c.contains(one));
652 +        }
653 +    }
654 +
655   //     public void testCollection8DebugFail() {
656   //         fail(impl.klazz().getSimpleName());
657   //     }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines