Search in sources :

Example 61 with SplittableRandom

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

the class SplittableRandomTest method testBadStreamSize.

/**
 * Invoking sized ints, long, doubles, with negative sizes throws
 * IllegalArgumentException
 */
@SuppressWarnings("unused")
public void testBadStreamSize() {
    SplittableRandom r = new SplittableRandom();
    Runnable[] throwingActions = { () -> {
        java8.util.stream.IntStream x = r.ints(-1L);
    }, () -> {
        java8.util.stream.IntStream x = r.ints(-1L, 2, 3);
    }, () -> {
        java8.util.stream.LongStream x = r.longs(-1L);
    }, () -> {
        java8.util.stream.LongStream x = r.longs(-1L, -1L, 1L);
    }, () -> {
        java8.util.stream.DoubleStream x = r.doubles(-1L);
    }, () -> {
        java8.util.stream.DoubleStream x = r.doubles(-1L, .5, .6);
    } };
    assertThrows(IllegalArgumentException.class, throwingActions);
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 62 with SplittableRandom

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

the class SplittableRandomTest method testNextIntBadBounds.

/**
 * nextInt(least >= bound) throws IllegalArgumentException
 */
public void testNextIntBadBounds() {
    SplittableRandom sr = new SplittableRandom();
    Runnable[] throwingActions = { () -> sr.nextInt(17, 2), () -> sr.nextInt(-42, -42), () -> sr.nextInt(Integer.MAX_VALUE, Integer.MIN_VALUE) };
    assertThrows(IllegalArgumentException.class, throwingActions);
}
Also used : SplittableRandom(java8.util.SplittableRandom)

Example 63 with SplittableRandom

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

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(size, counter.sum());
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Example 64 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 65 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() {
    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(0, fails.get());
}
Also used : 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