Search in sources :

Example 31 with SplittableRandom

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

the class SplittableRandomTest method testNextDouble.

/**
 * Repeated calls to nextDouble produce at least two distinct results
 */
public void testNextDouble() {
    SplittableRandom sr = new SplittableRandom();
    double f = sr.nextDouble();
    int i = 0;
    while (i < NCALLS && sr.nextDouble() == f) ++i;
    assertTrue(i < NCALLS);
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 32 with SplittableRandom

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

the class SplittableRandomTest method testNextIntBoundedNeg.

/**
 * nextInt(negative) throws IllegalArgumentException
 */
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextIntBoundedNeg() {
    SplittableRandom sr = new SplittableRandom();
    int f = sr.nextInt(-17);
}
Also used : SplittableRandom(java8.util.SplittableRandom) Test(org.testng.annotations.Test)

Example 33 with SplittableRandom

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

the class SplittableRandomTest method testUnsizedLongsCount.

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

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

Example 34 with SplittableRandom

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

the class SplittableRandomTest method testBadStreamBounds.

/**
 * Invoking bounded ints, long, doubles, with illegal bounds throws
 * IllegalArgumentException
 */
public void testBadStreamBounds() {
    final SplittableRandom r = new SplittableRandom();
    executeAndCatchIAE(new Runnable() {

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

        @Override
        public void run() {
            r.ints(10, 42, 42);
        }
    });
    executeAndCatchIAE(new Runnable() {

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

        @Override
        public void run() {
            r.longs(10, 1L, -2L);
        }
    });
    testDoubleBadOriginBound(new BiConsumer<Double, Double>() {

        @Override
        public void accept(Double o, Double b) {
            r.doubles(10, o, b);
        }
    });
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 35 with SplittableRandom

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

the class SplittableRandomTest method longsDataProvider.

@DataProvider(name = "longs")
public static Object[][] longsDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Long>, LongStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToLong(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs().limit(%d)", SIZE), () -> new SplittableRandom().longs().limit(SIZE)), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
    data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d)", SIZE), () -> new SplittableRandom().longs(SIZE)), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
    data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong())", SIZE), () -> rbsf.apply(sr -> sr.nextLong())), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final long origin = o;
            final long bound = b;
            data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d, %d).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().longs(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d, %d, %d)", SIZE, origin, bound), () -> new SplittableRandom().longs(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextLong(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d, %d))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextLong(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) LongStream(java8.util.stream.LongStream) SplittableRandom(java8.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

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