Search in sources :

Example 56 with SplittableRandom

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

the class SplittableRandomTest method testBoundedDoubles.

/**
 * Each of a parallel sized stream of bounded doubles is within bounds
 */
public void testBoundedDoubles() {
    AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    long size = 456;
    for (double least = 0.00011; least < 1.0e20; least *= 9) {
        for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
            final double lo = least, hi = bound;
            r.doubles(size, lo, hi).parallel().forEach(x -> {
                if (x < lo || x >= hi)
                    fails.getAndIncrement();
            });
        }
    }
    assertEquals(0, fails.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SplittableRandom(java8.util.SplittableRandom)

Example 57 with SplittableRandom

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

the class SplittableRandomTest method testBoundedInts.

/**
 * Each of a parallel sized stream of bounded ints is within bounds
 */
public void testBoundedInts() {
    AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    long size = 12345L;
    for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
        for (int bound = least + 2; bound > least && bound < MAX_INT_BOUND; bound += 67867967) {
            final int lo = least, hi = bound;
            r.ints(size, lo, hi).parallel().forEach(x -> {
                if (x < lo || x >= hi)
                    fails.getAndIncrement();
            });
        }
    }
    assertEquals(0, fails.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SplittableRandom(java8.util.SplittableRandom)

Example 58 with SplittableRandom

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

the class SplittableRandomTest method testNextDoubleBadBounds.

/**
 * nextDouble(! (least < bound)) throws IllegalArgumentException
 */
public void testNextDoubleBadBounds() {
    SplittableRandom sr = new SplittableRandom();
    Runnable[] throwingActions = { () -> sr.nextDouble(17.0d, 2.0d), () -> sr.nextDouble(-42.0d, -42.0d), () -> sr.nextDouble(Double.MAX_VALUE, Double.MIN_VALUE), () -> sr.nextDouble(Double.NaN, 0.0d), () -> sr.nextDouble(0.0d, Double.NaN) };
    assertThrows(IllegalArgumentException.class, throwingActions);
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 59 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() {
    LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).parallel().forEach(x -> counter.increment());
    assertEquals(size, counter.sum());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Example 60 with SplittableRandom

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

the class SplittableRandomTest method testSplit1.

/**
 * A SplittableRandom produced by split() of a default-constructed
 * SplittableRandom generates a different sequence
 */
public void testSplit1() {
    SplittableRandom sr = new SplittableRandom();
    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)

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