Search in sources :

Example 36 with SplittableRandom

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

the class SplittableRandomTest method doublesDataProvider.

@DataProvider(name = "doubles")
public static Object[][] doublesDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Double>, DoubleStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToDouble(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles().limit(%d)", SIZE), () -> new SplittableRandom().doubles().limit(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d)", SIZE), () -> new SplittableRandom().doubles(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble())", SIZE), () -> rbsf.apply(sr -> sr.nextDouble())), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final double origin = o;
            final double bound = b;
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%f, %f).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().doubles(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d, %f, %f)", SIZE, origin, bound), () -> new SplittableRandom().doubles(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextDouble(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f, %f))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextDouble(origin, bound))), randomAsserter(SIZE, origin, bound) });
        }
    }
    return data.toArray(new Object[0][]);
}
Also used : DataProvider(org.testng.annotations.DataProvider) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Set(java.util.Set) Test(org.testng.annotations.Test) Spliterators(java8.util.Spliterators) SplittableRandom(java8.util.SplittableRandom) LongStreamTestScenario(java8.util.stream.LongStreamTestScenario) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) IntStreamTestScenario(java8.util.stream.IntStreamTestScenario) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Consumer(java8.util.function.Consumer) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) DoubleStreamTestScenario(java8.util.stream.DoubleStreamTestScenario) Function(java8.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java8.util.stream.DoubleStream) SplittableRandom(java8.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

Example 37 with SplittableRandom

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

the class SplittableRandomTest method testNextDoubleBounded2.

/**
 * nextDouble(least, bound) returns least <= value < bound;
 * repeated calls produce at least two distinct results
 */
public void testNextDoubleBounded2() {
    SplittableRandom sr = new SplittableRandom();
    for (double least = 0.0001; least < 1.0e20; least *= 8) {
        for (double bound = least * 1.001; bound < 1.0e20; bound *= 16) {
            double f = sr.nextDouble(least, bound);
            assertTrue(least <= f && f < bound);
            int i = 0;
            double j;
            while (i < NCALLS && (j = sr.nextDouble(least, bound)) == f) {
                assertTrue(least <= j && j < bound);
                ++i;
            }
            assertTrue(i < NCALLS);
        }
    }
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 38 with SplittableRandom

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

the class SplittableRandomTest method testUnsizedDoublesCountSeq.

/**
 * A sequential unsized stream of doubles generates at least 100 values
 */
public void testUnsizedDoublesCountSeq() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.doubles().limit(size).forEach(new DoubleConsumer() {

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

Example 39 with SplittableRandom

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

the class SplittableRandomTest method testBadStreamSize.

/**
 * Invoking sized ints, long, doubles, with negative sizes throws
 * IllegalArgumentException
 */
public void testBadStreamSize() {
    final SplittableRandom r = new SplittableRandom();
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            r.ints(-1L);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            r.ints(-1L, 2, 3);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            r.longs(-1L);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            r.longs(-1L, -1L, 1L);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            r.doubles(-1L);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            r.doubles(-1L, .5, .6);
        }
    });
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 40 with SplittableRandom

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

Aggregations

SplittableRandom (java8.util.SplittableRandom)66 LongAdder (java8.util.concurrent.atomic.LongAdder)18 Test (org.testng.annotations.Test)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 DoubleConsumer (java8.util.function.DoubleConsumer)4 IntConsumer (java8.util.function.IntConsumer)4 LongConsumer (java8.util.function.LongConsumer)4 ArrayList (java.util.ArrayList)3 Comparator (java.util.Comparator)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Spliterator (java8.util.Spliterator)3 Spliterators (java8.util.Spliterators)3 Consumer (java8.util.function.Consumer)3 Function (java8.util.function.Function)3 DoubleStream (java8.util.stream.DoubleStream)3 DoubleStreamTestScenario (java8.util.stream.DoubleStreamTestScenario)3 IntStream (java8.util.stream.IntStream)3 IntStreamTestScenario (java8.util.stream.IntStreamTestScenario)3