use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandom8Test method testIntsCount.
/**
* A parallel sized stream of ints generates the given number of values
*/
public void testIntsCount() {
LongAdder counter = new LongAdder();
ThreadLocalRandom r = ThreadLocalRandom.current();
long size = 0;
for (int reps = 0; reps < REPS; ++reps) {
counter.reset();
r.ints(size).parallel().forEach(x -> counter.increment());
assertEquals(size, counter.sum());
size += 524959;
}
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandom8Test method testBoundedLongs.
/**
* Each of a parallel sized stream of bounded longs is within bounds
*/
public void testBoundedLongs() {
AtomicInteger fails = new AtomicInteger(0);
ThreadLocalRandom r = ThreadLocalRandom.current();
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());
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandom8Test method testBadStreamBounds.
/**
* Invoking bounded ints, long, doubles, with illegal bounds throws
* IllegalArgumentException
*/
public void testBadStreamBounds() {
ThreadLocalRandom r = ThreadLocalRandom.current();
Runnable[] throwingActions = { () -> r.ints(2, 1), () -> r.ints(10, 42, 42), () -> r.longs(-1L, -1L), () -> r.longs(10, 1L, -2L), () -> r.doubles(0.0, 0.0), () -> r.doubles(10, .5, .4) };
assertThrows(IllegalArgumentException.class, throwingActions);
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testNextLongBoundNonPositive.
/**
* nextLong(non-positive) throws IllegalArgumentException
*/
public void testNextLongBoundNonPositive() {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (long bound : new long[] { 0L, -17L, Long.MIN_VALUE }) {
try {
rnd.nextLong(bound);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
}
use of java8.util.concurrent.ThreadLocalRandom in project streamsupport by stefan-zobel.
the class ThreadLocalRandomTest method testNextLongBadBounds.
/**
* nextLong(least >= bound) throws IllegalArgumentException
*/
public void testNextLongBadBounds() {
long[][] badBoundss = { { 17L, 2L }, { -42L, -42L }, { Long.MAX_VALUE, Long.MIN_VALUE } };
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (long[] badBounds : badBoundss) {
try {
rnd.nextLong(badBounds[0], badBounds[1]);
shouldThrow();
} catch (IllegalArgumentException success) {
}
}
}
Aggregations