Search in sources :

Example 21 with SplittableRandom

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

the class SplittableRandomTest method testNextIntBounded.

/**
     * nextInt(bound) returns 0 <= value < bound;
     * repeated calls produce at least two distinct results
     */
public void testNextIntBounded() {
    SplittableRandom sr = new SplittableRandom();
    // sample bound space across prime number increments
    for (int bound = 2; bound < MAX_INT_BOUND; bound += 524959) {
        int f = sr.nextInt(bound);
        assertTrue(0 <= f && f < bound);
        int i = 0;
        int j;
        while (i < NCALLS && (j = sr.nextInt(bound)) == f) {
            assertTrue(0 <= j && j < bound);
            ++i;
        }
        assertTrue(i < NCALLS);
    }
}
Also used : SplittableRandom(java.util.SplittableRandom)

Example 22 with SplittableRandom

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

the class SplittableRandomTest method testBoundedLongs.

/**
     * Each of a parallel sized stream of bounded longs is within bounds
     */
public void testBoundedLongs() {
    AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    long size = 123L;
    for (long least = -86028121; least < MAX_LONG_BOUND; least += 1982451653L) {
        for (long bound = least + 2; bound > least && bound < MAX_LONG_BOUND; bound += Math.abs(bound * 7919)) {
            final long lo = least, hi = bound;
            r.longs(size, lo, hi).parallel().forEach(x -> {
                if (x < lo || x >= hi)
                    fails.getAndIncrement();
            });
        }
    }
    assertEquals(fails.get(), 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SplittableRandom(java.util.SplittableRandom)

Example 23 with SplittableRandom

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

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(java.util.SplittableRandom)

Example 24 with SplittableRandom

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

the class SplittableRandomTest method testUnsizedDoublesCount.

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

Example 25 with SplittableRandom

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

the class SplittableRandomTest method testUnsizedIntsCount.

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