Search in sources :

Example 16 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testUnsizedDoublesCountSeq.

/**
 * A sequential unsized stream of doubles generates at least 100 values
 */
public void testUnsizedDoublesCountSeq() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 100;
    r.doubles().limit(size).forEach(x -> {
        counter.increment();
    });
    assertEquals(counter.sum(), size);
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 17 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testLongsCount.

/**
 * A parallel sized stream of longs generates the given number of values
 */
public void testLongsCount() {
    LongAdder counter = new LongAdder();
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.longs(size).parallel().forEach(x -> {
            counter.increment();
        });
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
Also used : LongAdder(java8.util.concurrent.atomic.LongAdder) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 18 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testBoundedInts.

/**
 * Each of a parallel sized stream of bounded ints is within bounds
 */
public void testBoundedInts() {
    AtomicInteger fails = new AtomicInteger(0);
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 12345L;
    for (int least = -15485867; least < MAX_INT_BOUND; least += 524959) {
        for (int bound = least + 2; bound < least && bound < MAX_INT_BOUND; bound += 67867967) {
            final int lo = least, hi = bound;
            r.ints(size, lo, hi).parallel().forEach(x -> {
                if (x < lo || x >= hi)
                    fails.getAndIncrement();
            });
        }
    }
    assertEquals(fails.get(), 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 19 with ThreadLocalRandom

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

the class ThreadLocalRandomTest 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(fails.get(), 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 20 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testBoundedDoubles.

/**
 * Each of a parallel sized stream of bounded doubles is within bounds
 */
public void testBoundedDoubles() {
    AtomicInteger fails = new AtomicInteger(0);
    ThreadLocalRandom r = ThreadLocalRandom.current();
    long size = 456;
    for (double least = 0.00011; least < 1.0e20; least *= 9) {
        for (double bound = least * 1.0011; bound < 1.0e20; bound *= 17) {
            final double lo = least, hi = bound;
            r.doubles(size, lo, hi).parallel().forEach(x -> {
                if (x < lo || x >= hi)
                    fails.getAndIncrement();
            });
        }
    }
    assertEquals(fails.get(), 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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