Search in sources :

Example 6 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testUnsizedIntsCountSeq.

/**
 * A sequential unsized stream of ints generates at least 100 values
 */
public void testUnsizedIntsCountSeq() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.ints().limit(size).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 7 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testIntsCount.

/**
 * A parallel sized stream of ints generates the given number of values
 */
public void testIntsCount() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.ints(size).parallel().forEach(new IntConsumer() {

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

Example 8 with IntConsumer

use of java8.util.function.IntConsumer in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testBoundedInts.

/**
 * Each of a parallel sized stream of bounded ints is within bounds
 */
public void testBoundedInts() {
    final AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    long size = 12345L;
    for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
        for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
            final int lo = least, hi = bound;
            r.ints(size, lo, hi).parallel().forEach(new IntConsumer() {

                @Override
                public void accept(int x) {
                    if (x < lo || x >= hi)
                        fails.getAndIncrement();
                }
            });
        }
    }
    assertEquals(fails.get(), 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SplittableRandom(java8.util.SplittableRandom) IntConsumer(java8.util.function.IntConsumer)

Example 9 with IntConsumer

use of java8.util.function.IntConsumer 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)

Aggregations

IntConsumer (java8.util.function.IntConsumer)9 SplittableRandom (java8.util.SplittableRandom)4 LongAdder (java8.util.concurrent.atomic.LongAdder)3 IntPredicate (java8.util.function.IntPredicate)3 Test (org.testng.annotations.Test)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 NoSuchElementException (java.util.NoSuchElementException)1 Spliterator (java8.util.Spliterator)1 ObjIntConsumer (java8.util.function.ObjIntConsumer)1