Search in sources :

Example 41 with SplittableRandom

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

the class SplittableRandomTest method testNextDoubleBadBound.

/**
 * nextDouble(bound) throws IllegalArgumentException
 */
public void testNextDoubleBadBound() {
    final SplittableRandom sr = new SplittableRandom();
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            sr.nextDouble(0.0);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            sr.nextDouble(-0.0);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            sr.nextDouble(+0.0);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            sr.nextDouble(-1.0);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            sr.nextDouble(Double.NaN);
        }
    });
    executeAndCatchIAE(new Runnable() {

        @Override
        public void run() {
            sr.nextDouble(Double.NEGATIVE_INFINITY);
        }
    });
// Returns Double.MAX_VALUE
// executeAndCatchIAE(() -> r.nextDouble(Double.POSITIVE_INFINITY));
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 42 with SplittableRandom

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

the class SplittableRandomTest method testNextInt.

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

Example 43 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 (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 44 with SplittableRandom

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

the class SplittableRandomTest method testUnsizedLongsCountSeq.

/**
 * A sequential unsized stream of longs generates at least 100 values
 */
public void testUnsizedLongsCountSeq() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).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 45 with SplittableRandom

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

the class SplittableRandomTest method testUnsizedDoublesCount.

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

        @Override
        public void accept(double x) {
            counter.increment();
        }
    });
    assertEquals(counter.sum(), size);
}
Also used : DoubleConsumer(java8.util.function.DoubleConsumer) LongAdder(java8.util.concurrent.atomic.LongAdder) 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