Search in sources :

Example 16 with SplittableRandom

use of java.util.SplittableRandom in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method intsDataProvider.

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

Example 17 with SplittableRandom

use of java.util.SplittableRandom in project jdk8u_jdk by JetBrains.

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 : IntStream(java.util.stream.IntStream) LongStream(java.util.stream.LongStream) DataProvider(org.testng.annotations.DataProvider) DoubleStreamTestScenario(java.util.stream.DoubleStreamTestScenario) TestData(java.util.stream.TestData) Set(java.util.Set) Test(org.testng.annotations.Test) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java.util.stream.DoubleStream) HashSet(java.util.HashSet) Consumer(java.util.function.Consumer) List(java.util.List) OpTestCase(java.util.stream.OpTestCase) SplittableRandom(java.util.SplittableRandom) StreamSupport(java.util.stream.StreamSupport) Spliterator(java.util.Spliterator) IntStreamTestScenario(java.util.stream.IntStreamTestScenario) LongStreamTestScenario(java.util.stream.LongStreamTestScenario) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java.util.stream.DoubleStream) SplittableRandom(java.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

Example 18 with SplittableRandom

use of java.util.SplittableRandom in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method testNextLongBoundedNeg.

/**
     * nextLong(negative) throws IllegalArgumentException
     */
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNextLongBoundedNeg() {
    SplittableRandom sr = new SplittableRandom();
    long f = sr.nextLong(-17);
}
Also used : SplittableRandom(java.util.SplittableRandom) Test(org.testng.annotations.Test)

Example 19 with SplittableRandom

use of java.util.SplittableRandom in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method testBadStreamBounds.

/**
     * Invoking bounded ints, long, doubles, with illegal bounds throws
     * IllegalArgumentException
     */
public void testBadStreamBounds() {
    SplittableRandom r = new SplittableRandom();
    executeAndCatchIAE(() -> r.ints(2, 1));
    executeAndCatchIAE(() -> r.ints(10, 42, 42));
    executeAndCatchIAE(() -> r.longs(-1L, -1L));
    executeAndCatchIAE(() -> r.longs(10, 1L, -2L));
    testDoubleBadOriginBound((o, b) -> r.doubles(10, o, b));
}
Also used : SplittableRandom(java.util.SplittableRandom)

Example 20 with SplittableRandom

use of java.util.SplittableRandom in project jdk8u_jdk by JetBrains.

the class SplittableRandomTest method testUnsizedIntsCountSeq.

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

Aggregations

SplittableRandom (java.util.SplittableRandom)34 LongAdder (java.util.concurrent.atomic.LongAdder)9 Test (org.testng.annotations.Test)7 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 Spliterator (java.util.Spliterator)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 Function (java.util.function.Function)3 DoubleStream (java.util.stream.DoubleStream)3 DoubleStreamTestScenario (java.util.stream.DoubleStreamTestScenario)3 IntStream (java.util.stream.IntStream)3 IntStreamTestScenario (java.util.stream.IntStreamTestScenario)3 LongStream (java.util.stream.LongStream)3 LongStreamTestScenario (java.util.stream.LongStreamTestScenario)3 OpTestCase (java.util.stream.OpTestCase)3 StreamSupport (java.util.stream.StreamSupport)3 TestData (java.util.stream.TestData)3