Search in sources :

Example 36 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testUnsizedIntsCount.

/**
 * A parallel unsized stream of ints generates at least 100 values
 */
public void testUnsizedIntsCount() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.ints().limit(size).parallel().forEach(new IntConsumer() {

        @Override
        public void accept(int x) {
            counter.increment();
        }
    });
    assertEquals(counter.sum(), size);
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom) IntConsumer(java8.util.function.IntConsumer)

Example 37 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class StringJoinerTest method addAlladdAll.

// The following tests do two successive adds of different types
public void addAlladdAll() {
    final StringJoiner sj = new StringJoiner(DASH, "{", "}");
    ArrayList<String> firstOne = new ArrayList<String>();
    firstOne.add(ONE);
    firstOne.add(TWO);
    firstOne.add(THREE);
    Stream<String> stream = StreamSupport.stream(firstOne);
    stream.forEachOrdered(new java8.util.function.Consumer<String>() {

        @Override
        public void accept(String t) {
            sj.add(t);
        }
    });
    ArrayList<String> nextOne = new ArrayList<String>();
    nextOne.add(FOUR);
    nextOne.add(FIVE);
    Stream<String> stream2 = StreamSupport.stream(nextOne);
    stream2.forEachOrdered(new java8.util.function.Consumer<String>() {

        @Override
        public void accept(String t) {
            sj.add(t);
        }
    });
    String expected = "{" + ONE + DASH + TWO + DASH + THREE + DASH + FOUR + DASH + FIVE + "}";
    assertEquals(sj.toString(), expected);
}
Also used : ArrayList(java.util.ArrayList) StringJoiner(java8.util.StringJoiner)

Example 38 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testDoublesCount.

/**
 * A parallel sized stream of doubles generates the given number of values
 */
public void testDoublesCount() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.doubles(size).parallel().forEach(x -> {
            counter.increment();
        });
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 39 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testUnsizedLongsCountSeq.

/**
 * A sequential unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCountSeq() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 100;
    r.longs().limit(size).forEach(x -> {
        counter.increment();
    });
    assertEquals(counter.sum(), size);
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 40 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class ThreadLocalRandomTest method testUnsizedIntsCountSeq.

/**
 * A sequential unsized stream of ints generates at least 100 values
 */
public void testUnsizedIntsCountSeq() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 100;
    r.ints().limit(size).forEach(x -> {
        counter.increment();
    });
    assertEquals(counter.sum(), size);
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Aggregations

LongAdder (java8.util.concurrent.atomic.LongAdder)36 SplittableRandom (java8.util.SplittableRandom)27 ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 ArrayList (java.util.ArrayList)9 Test (org.testng.annotations.Test)9 Function (java8.util.function.Function)8 OpTestCase (java8.util.stream.OpTestCase)8 List (java.util.List)7 StreamSupport (java8.util.stream.StreamSupport)7 TestData (java8.util.stream.TestData)7 Set (java.util.Set)6 Supplier (java8.util.function.Supplier)6 DoubleStream (java8.util.stream.DoubleStream)6 IntStream (java8.util.stream.IntStream)6 LongStream (java8.util.stream.LongStream)6 HashSet (java.util.HashSet)5 Spliterator (java8.util.Spliterator)5 Consumer (java8.util.function.Consumer)5 Collection (java.util.Collection)4