Search in sources :

Example 26 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testUnsizedLongsCountSeq.

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

Example 27 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testNextDoubleBadBound.

/**
 * nextDouble(bound) throws IllegalArgumentException
 */
public void testNextDoubleBadBound() {
    ThreadLocalRandom r = ThreadLocalRandom.current();
    executeAndCatchIAE(() -> r.nextDouble(0.0));
    executeAndCatchIAE(() -> r.nextDouble(-0.0));
    executeAndCatchIAE(() -> r.nextDouble(+0.0));
    executeAndCatchIAE(() -> r.nextDouble(-1.0));
    executeAndCatchIAE(() -> r.nextDouble(Double.NaN));
    executeAndCatchIAE(() -> r.nextDouble(Double.NEGATIVE_INFINITY));
// Returns Double.MAX_VALUE
// executeAndCatchIAE(() -> r.nextDouble(Double.POSITIVE_INFINITY));
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 28 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testBadStreamBounds.

/**
 * Invoking bounded ints, long, doubles, with illegal bounds throws
 * IllegalArgumentException
 */
public void testBadStreamBounds() {
    ThreadLocalRandom r = ThreadLocalRandom.current();
    executeAndCatchIAE(() -> r.ints(2, 1));
    executeAndCatchIAE(() -> r.ints(10, 42, 42));
    executeAndCatchIAE(() -> r.longs(-1L, -1L));
    executeAndCatchIAE(() -> r.longs(10, 1L, -2L));
    testDoubleBadOriginBound((o, b) -> r.doubles(10, o, b));
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 29 with ThreadLocalRandom

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

the class ThreadLocalRandomTest method testUnsizedIntsCountSeq.

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

Example 30 with ThreadLocalRandom

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

the class Collection8Test method testObjectMethods.

@Test(dataProvider = "Source")
public void testObjectMethods(String description, Supplier<CollectionImplementation> sci) {
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    Collection c = sci.get().emptyCollection();
    for (int n = rnd.nextInt(3); n-- > 0; ) c.add(sci.get().makeElement(rnd.nextInt()));
    assertEquals(c, c);
    if (c instanceof List) {
        List<?> copy = new ArrayList(c);
        assertEquals(copy, c);
        assertEquals(c, copy);
        assertEquals(copy.hashCode(), c.hashCode());
    }
    if (c instanceof Set) {
        Set<?> copy = new HashSet(c);
        assertEquals(copy, c);
        assertEquals(c, copy);
        assertEquals(copy.hashCode(), c.hashCode());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

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