Search in sources :

Example 6 with ThreadLocalRandom

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;
    }
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 7 with ThreadLocalRandom

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 8 with ThreadLocalRandom

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);
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 9 with ThreadLocalRandom

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) {
        }
    }
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 10 with ThreadLocalRandom

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) {
        }
    }
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Aggregations

ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)48 LongAdder (java8.util.concurrent.atomic.LongAdder)18 Collection (java.util.Collection)9 Test (org.testng.annotations.Test)9 ArrayList (java.util.ArrayList)8 LinkedList (java.util.LinkedList)8 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)8 ArrayDeque (java.util.ArrayDeque)7 Deque (java.util.Deque)7 HashSet (java.util.HashSet)7 List (java.util.List)7 BlockingDeque (java.util.concurrent.BlockingDeque)7 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)7 ConcurrentModificationException (java.util.ConcurrentModificationException)6 Set (java.util.Set)6 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)6 CopyOnWriteArraySet (java.util.concurrent.CopyOnWriteArraySet)6 AtomicLong (java.util.concurrent.atomic.AtomicLong)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)6 Spliterator (java8.util.Spliterator)6