< prev index next >

test/jdk/java/util/concurrent/tck/Collection8Test.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


 423         int n = rnd.nextInt(6);
 424         ArrayList copy = new ArrayList();
 425         for (int i = 0; i < n; i++) {
 426             Object x = impl.makeElement(i);
 427             copy.add(x);
 428             c.add(x);
 429         }
 430         ArrayList iterated = new ArrayList();
 431         ArrayList spliterated = new ArrayList();
 432         ArrayList removed = new ArrayList();
 433         Spliterator s = c.spliterator();
 434         Iterator it = c.iterator();
 435         if (! (s.hasCharacteristics(Spliterator.CONCURRENT) ||
 436                s.hasCharacteristics(Spliterator.IMMUTABLE)))
 437             return;
 438         for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
 439             assertTrue(s.tryAdvance(e -> {}));
 440             if (rnd.nextBoolean()) assertTrue(it.hasNext());
 441             it.next();
 442         }
 443         Consumer alwaysThrows = e -> { throw new AssertionError(); };
 444         // TODO: many more removal methods
 445         if (rnd.nextBoolean()) {
 446             for (Iterator z = c.iterator(); z.hasNext(); ) {
 447                 Object e = z.next();
 448                 if (rnd.nextBoolean()) {
 449                     try {
 450                         z.remove();
 451                     } catch (UnsupportedOperationException ok) { return; }
 452                     removed.add(e);
 453                 }
 454             }
 455         } else {
 456             Predicate randomlyRemove = e -> {
 457                 if (rnd.nextBoolean()) { removed.add(e); return true; }
 458                 else return false;
 459             };
 460             c.removeIf(randomlyRemove);
 461         }
 462         s.forEachRemaining(spliterated::add);
 463         while (it.hasNext())


 644             assertTrue(it.hasNext());
 645             assertEquals(impl.makeElement(n - 1), it.next());
 646             assertTrue(it.hasNext());
 647             assertEquals(impl.makeElement(n - 2), it.next());
 648             it.forEachRemaining(e -> assertTrue(c.contains(e)));
 649             if (testImplementationDetails) {
 650                 it.remove();
 651                 assertEquals(n - 1, d.size());
 652                 for (int i = 1; i < n; i++)
 653                     assertTrue(d.contains(impl.makeElement(i)));
 654                 assertFalse(d.contains(impl.makeElement(0)));
 655             }
 656         }
 657     }
 658 
 659     /**
 660      * stream().forEach returns elements in the collection
 661      */
 662     public void testStreamForEach() throws Throwable {
 663         final Collection c = impl.emptyCollection();
 664         final AtomicLong count = new AtomicLong(0L);
 665         final Object x = impl.makeElement(1);
 666         final Object y = impl.makeElement(2);
 667         final ArrayList found = new ArrayList();
 668         Consumer<Object> spy = o -> found.add(o);
 669         c.stream().forEach(spy);
 670         assertTrue(found.isEmpty());
 671 
 672         assertTrue(c.add(x));
 673         c.stream().forEach(spy);
 674         assertEquals(Collections.singletonList(x), found);
 675         found.clear();
 676 
 677         assertTrue(c.add(y));
 678         c.stream().forEach(spy);
 679         assertEquals(2, found.size());
 680         assertTrue(found.contains(x));
 681         assertTrue(found.contains(y));
 682         found.clear();
 683 
 684         c.clear();


 702                     c.stream().forEach(x -> assertSame(x, elt)); };
 703             Runnable addRemove = () -> {
 704                 threadsStarted.countDown();
 705                 while (!done.get()) {
 706                     assertTrue(c.add(elt));
 707                     assertTrue(c.remove(elt));
 708                 }};
 709             f1 = pool.submit(checkElt);
 710             f2 = pool.submit(addRemove);
 711             Thread.sleep(testDurationMillis);
 712         }
 713         assertNull(f1.get(0L, MILLISECONDS));
 714         assertNull(f2.get(0L, MILLISECONDS));
 715     }
 716 
 717     /**
 718      * collection.forEach returns elements in the collection
 719      */
 720     public void testForEach() throws Throwable {
 721         final Collection c = impl.emptyCollection();
 722         final AtomicLong count = new AtomicLong(0L);
 723         final Object x = impl.makeElement(1);
 724         final Object y = impl.makeElement(2);
 725         final ArrayList found = new ArrayList();
 726         Consumer<Object> spy = o -> found.add(o);
 727         c.forEach(spy);
 728         assertTrue(found.isEmpty());
 729 
 730         assertTrue(c.add(x));
 731         c.forEach(spy);
 732         assertEquals(Collections.singletonList(x), found);
 733         found.clear();
 734 
 735         assertTrue(c.add(y));
 736         c.forEach(spy);
 737         assertEquals(2, found.size());
 738         assertTrue(found.contains(x));
 739         assertTrue(found.contains(y));
 740         found.clear();
 741 
 742         c.clear();




 423         int n = rnd.nextInt(6);
 424         ArrayList copy = new ArrayList();
 425         for (int i = 0; i < n; i++) {
 426             Object x = impl.makeElement(i);
 427             copy.add(x);
 428             c.add(x);
 429         }
 430         ArrayList iterated = new ArrayList();
 431         ArrayList spliterated = new ArrayList();
 432         ArrayList removed = new ArrayList();
 433         Spliterator s = c.spliterator();
 434         Iterator it = c.iterator();
 435         if (! (s.hasCharacteristics(Spliterator.CONCURRENT) ||
 436                s.hasCharacteristics(Spliterator.IMMUTABLE)))
 437             return;
 438         for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
 439             assertTrue(s.tryAdvance(e -> {}));
 440             if (rnd.nextBoolean()) assertTrue(it.hasNext());
 441             it.next();
 442         }

 443         // TODO: many more removal methods
 444         if (rnd.nextBoolean()) {
 445             for (Iterator z = c.iterator(); z.hasNext(); ) {
 446                 Object e = z.next();
 447                 if (rnd.nextBoolean()) {
 448                     try {
 449                         z.remove();
 450                     } catch (UnsupportedOperationException ok) { return; }
 451                     removed.add(e);
 452                 }
 453             }
 454         } else {
 455             Predicate randomlyRemove = e -> {
 456                 if (rnd.nextBoolean()) { removed.add(e); return true; }
 457                 else return false;
 458             };
 459             c.removeIf(randomlyRemove);
 460         }
 461         s.forEachRemaining(spliterated::add);
 462         while (it.hasNext())


 643             assertTrue(it.hasNext());
 644             assertEquals(impl.makeElement(n - 1), it.next());
 645             assertTrue(it.hasNext());
 646             assertEquals(impl.makeElement(n - 2), it.next());
 647             it.forEachRemaining(e -> assertTrue(c.contains(e)));
 648             if (testImplementationDetails) {
 649                 it.remove();
 650                 assertEquals(n - 1, d.size());
 651                 for (int i = 1; i < n; i++)
 652                     assertTrue(d.contains(impl.makeElement(i)));
 653                 assertFalse(d.contains(impl.makeElement(0)));
 654             }
 655         }
 656     }
 657 
 658     /**
 659      * stream().forEach returns elements in the collection
 660      */
 661     public void testStreamForEach() throws Throwable {
 662         final Collection c = impl.emptyCollection();

 663         final Object x = impl.makeElement(1);
 664         final Object y = impl.makeElement(2);
 665         final ArrayList found = new ArrayList();
 666         Consumer<Object> spy = o -> found.add(o);
 667         c.stream().forEach(spy);
 668         assertTrue(found.isEmpty());
 669 
 670         assertTrue(c.add(x));
 671         c.stream().forEach(spy);
 672         assertEquals(Collections.singletonList(x), found);
 673         found.clear();
 674 
 675         assertTrue(c.add(y));
 676         c.stream().forEach(spy);
 677         assertEquals(2, found.size());
 678         assertTrue(found.contains(x));
 679         assertTrue(found.contains(y));
 680         found.clear();
 681 
 682         c.clear();


 700                     c.stream().forEach(x -> assertSame(x, elt)); };
 701             Runnable addRemove = () -> {
 702                 threadsStarted.countDown();
 703                 while (!done.get()) {
 704                     assertTrue(c.add(elt));
 705                     assertTrue(c.remove(elt));
 706                 }};
 707             f1 = pool.submit(checkElt);
 708             f2 = pool.submit(addRemove);
 709             Thread.sleep(testDurationMillis);
 710         }
 711         assertNull(f1.get(0L, MILLISECONDS));
 712         assertNull(f2.get(0L, MILLISECONDS));
 713     }
 714 
 715     /**
 716      * collection.forEach returns elements in the collection
 717      */
 718     public void testForEach() throws Throwable {
 719         final Collection c = impl.emptyCollection();

 720         final Object x = impl.makeElement(1);
 721         final Object y = impl.makeElement(2);
 722         final ArrayList found = new ArrayList();
 723         Consumer<Object> spy = o -> found.add(o);
 724         c.forEach(spy);
 725         assertTrue(found.isEmpty());
 726 
 727         assertTrue(c.add(x));
 728         c.forEach(spy);
 729         assertEquals(Collections.singletonList(x), found);
 730         found.clear();
 731 
 732         assertTrue(c.add(y));
 733         c.forEach(spy);
 734         assertEquals(2, found.size());
 735         assertTrue(found.contains(x));
 736         assertTrue(found.contains(y));
 737         found.clear();
 738 
 739         c.clear();


< prev index next >