Search in sources :

Example 16 with SplittableRandom

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

the class SplittableRandomTest method testNextDoubleBounded2.

// TODO: Test infinite bounds!
// () -> sr.nextDouble(Double.NEGATIVE_INFINITY, 0.0d),
// () -> sr.nextDouble(0.0d, Double.POSITIVE_INFINITY),
/**
 * 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 17 with SplittableRandom

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

the class SplittableRandomTest method testNextLongBounded.

/**
 * nextLong(bound) returns 0 <= value < bound;
 * repeated calls produce at least two distinct results
 */
public void testNextLongBounded() {
    SplittableRandom sr = new SplittableRandom();
    for (int i = 0; i < 2; i++) assertEquals(0L, sr.nextLong(1L));
    for (long bound = 2; bound < MAX_LONG_BOUND; bound += 15485863) {
        long f = sr.nextLong(bound);
        assertTrue(0 <= f && f < bound);
        int i = 0;
        long j;
        while (i < NCALLS && (j = sr.nextLong(bound)) == f) {
            assertTrue(0 <= j && j < bound);
            ++i;
        }
        assertTrue(i < NCALLS);
    }
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 18 with SplittableRandom

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

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();
    for (int i = 0; i < 2; i++) assertEquals(0, sr.nextInt(1));
    // 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(java8.util.SplittableRandom)

Example 19 with SplittableRandom

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

the class SplittableRandomTest method testSplit2.

/**
 * A SplittableRandom produced by split() of a seeded-constructed
 * SplittableRandom generates a different sequence
 */
public void testSplit2() {
    SplittableRandom sr = new SplittableRandom(12345);
    for (int reps = 0; reps < REPS; ++reps) {
        SplittableRandom sc = sr.split();
        int i = 0;
        while (i < NCALLS && sr.nextLong() == sc.nextLong()) ++i;
        assertTrue(i < NCALLS);
    }
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 20 with SplittableRandom

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

the class SplittableRandomTest method testBoundedLongs.

/**
 * Each of a parallel sized stream of bounded longs is within bounds
 */
public void testBoundedLongs() {
    final 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(new LongConsumer() {

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

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