Search in sources :

Example 16 with Longs

use of java8.lang.Longs 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 17 with Longs

use of java8.lang.Longs in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testUnsizedLongsCount.

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

Example 18 with Longs

use of java8.lang.Longs in project streamsupport by stefan-zobel.

the class LongAccumulatorTest method testAccumulateAndGet.

/**
 * accumulate accumulates given value to current, and get returns current value
 */
public void testAccumulateAndGet() {
    LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
    acc.accumulate(2);
    assertEquals(2, acc.get());
    acc.accumulate(-4);
    assertEquals(2, acc.get());
    acc.accumulate(4);
    assertEquals(4, acc.get());
}
Also used : LongAccumulator(java8.util.concurrent.atomic.LongAccumulator) Longs(java8.lang.Longs)

Example 19 with Longs

use of java8.lang.Longs in project streamsupport by stefan-zobel.

the class LongAccumulatorTest method testToString.

/**
 * toString returns current value.
 */
public void testToString() {
    LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
    assertEquals("0", acc.toString());
    acc.accumulate(1);
    assertEquals(Long.toString(1), acc.toString());
}
Also used : LongAccumulator(java8.util.concurrent.atomic.LongAccumulator) Longs(java8.lang.Longs)

Example 20 with Longs

use of java8.lang.Longs in project streamsupport by stefan-zobel.

the class LongAccumulatorTest method testReset.

/**
 * reset() causes subsequent get() to return zero
 */
public void testReset() {
    LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
    acc.accumulate(2);
    assertEquals(2, acc.get());
    acc.reset();
    assertEquals(0, acc.get());
}
Also used : LongAccumulator(java8.util.concurrent.atomic.LongAccumulator) Longs(java8.lang.Longs)

Aggregations

LongAdder (java8.util.concurrent.atomic.LongAdder)12 SplittableRandom (java8.util.SplittableRandom)9 Longs (java8.lang.Longs)8 ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)8 LongAccumulator (java8.util.concurrent.atomic.LongAccumulator)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 LongConsumer (java8.util.function.LongConsumer)4 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Spliterator (java8.util.Spliterator)1 Spliterators (java8.util.Spliterators)1 Consumer (java8.util.function.Consumer)1 Function (java8.util.function.Function)1 DoubleStream (java8.util.stream.DoubleStream)1 DoubleStreamTestScenario (java8.util.stream.DoubleStreamTestScenario)1 IntStream (java8.util.stream.IntStream)1 IntStreamTestScenario (java8.util.stream.IntStreamTestScenario)1