use of java8.util.SplittableRandom in project streamsupport by stefan-zobel.
the class SplittableRandomTest method doublesDataProvider.
@DataProvider(name = "doubles")
public static Object[][] doublesDataProvider() {
List<Object[]> data = new ArrayList<>();
// Function to create a stream using a RandomBoxedSpliterator
Function<Function<SplittableRandom, Double>, DoubleStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToDouble(i -> i);
// Unbounded
data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles().limit(%d)", SIZE), () -> new SplittableRandom().doubles().limit(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d)", SIZE), () -> new SplittableRandom().doubles(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble())", SIZE), () -> rbsf.apply(sr -> sr.nextDouble())), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
for (int b : BOUNDS) {
for (int o : ORIGINS) {
final double origin = o;
final double bound = b;
data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%f, %f).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().doubles(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d, %f, %f)", SIZE, origin, bound), () -> new SplittableRandom().doubles(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
if (origin == 0) {
data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextDouble(bound))), randomAsserter(SIZE, origin, bound) });
}
data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f, %f))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextDouble(origin, bound))), randomAsserter(SIZE, origin, bound) });
}
}
return data.toArray(new Object[0][]);
}
use of java8.util.SplittableRandom in project streamsupport by stefan-zobel.
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);
}
}
}
use of java8.util.SplittableRandom in project streamsupport by stefan-zobel.
the class SplittableRandomTest method testUnsizedDoublesCountSeq.
/**
* A sequential unsized stream of doubles generates at least 100 values
*/
public void testUnsizedDoublesCountSeq() {
final LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 100;
r.doubles().limit(size).forEach(new DoubleConsumer() {
@Override
public void accept(double x) {
counter.increment();
}
});
assertEquals(counter.sum(), size);
}
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
*/
public void testBadStreamSize() {
final SplittableRandom r = new SplittableRandom();
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.ints(-1L);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.ints(-1L, 2, 3);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.longs(-1L);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.longs(-1L, -1L, 1L);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.doubles(-1L);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.doubles(-1L, .5, .6);
}
});
}
use of java8.util.SplittableRandom in project streamsupport by stefan-zobel.
the class SplittableRandomTest method testIntsCount.
/**
* A parallel sized stream of ints generates the given number of values
*/
public void testIntsCount() {
final LongAdder counter = new LongAdder();
SplittableRandom r = new SplittableRandom();
long size = 0;
for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.ints(size).parallel().forEach(new IntConsumer() {
@Override
public void accept(int x) {
counter.increment();
}
});
assertEquals(counter.sum(), size);
size += 524959;
}
}
Aggregations