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.32 by jsr166, Mon Nov 28 03:30:28 2016 UTC vs.
Revision 1.38 by jsr166, Wed Dec 7 22:03:41 2016 UTC

# Line 353 | 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();
# Line 362 | Line 363 | public class Collection8Test extends JSR
363          for (Spliterator s = c.spliterator();
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);
# Line 376 | Line 382 | public class Collection8Test extends JSR
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);
# Line 384 | Line 391 | public class Collection8Test extends JSR
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));
# Line 542 | Line 550 | public class Collection8Test extends JSR
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       */
# Line 549 | Line 592 | public class Collection8Test extends JSR
592          if (!impl.isConcurrent()) return;
593          final ThreadLocalRandom rnd = ThreadLocalRandom.current();
594          final Collection c = impl.emptyCollection();
595 <        final long testDurationMillis = timeoutMillis();
595 >        final long testDurationMillis
596 >            = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
597          final AtomicBoolean done = new AtomicBoolean(false);
598          final Object one = impl.makeElement(1);
599          final Object two = impl.makeElement(2);
600 +        final Consumer checkSanity = x -> assertTrue(x == one || x == two);
601 +        final Consumer<Object[]> checkArraySanity = array -> {
602 +            // assertTrue(array.length <= 2); // duplicates are permitted
603 +            for (Object x : array) assertTrue(x == one || x == two);
604 +        };
605          final Object[] emptyArray =
606              (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
607          final List<Future<?>> futures;
608          final Phaser threadsStarted = new Phaser(1); // register this thread
560        final Consumer checkSanity = x -> assertTrue(x == one || x == two);
609          final Runnable[] frobbers = {
610              () -> c.forEach(checkSanity),
611              () -> c.stream().forEach(checkSanity),
# Line 573 | Line 621 | public class Collection8Test extends JSR
621                  do {} while (s.tryAdvance(checkSanity));
622              },
623              () -> { for (Object x : c) checkSanity.accept(x); },
624 <            () -> { for (Object x : c.toArray()) checkSanity.accept(x); },
625 <            () -> { for (Object x : c.toArray(emptyArray)) checkSanity.accept(x); },
578 <            () -> {
579 <                assertTrue(c.add(one));
580 <                assertTrue(c.contains(one));
581 <                assertTrue(c.remove(one));
582 <                assertFalse(c.contains(one));
583 <            },
624 >            () -> checkArraySanity.accept(c.toArray()),
625 >            () -> checkArraySanity.accept(c.toArray(emptyArray)),
626              () -> {
627 <                assertTrue(c.add(two));
628 <                assertTrue(c.contains(two));
629 <                assertTrue(c.remove(two));
630 <                assertFalse(c.contains(two));
631 <            },
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)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines