Search in sources :

Example 6 with Longs

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

the class LongAccumulatorTest method testGetThenReset.

/**
 * getThenReset() returns current value; subsequent get() returns zero
 */
public void testGetThenReset() {
    LongAccumulator acc = new LongAccumulator(Longs::max, 0L);
    acc.accumulate(2);
    assertEquals(2, acc.get());
    assertEquals(2, acc.getThenReset());
    assertEquals(0, acc.get());
}
Also used : LongAccumulator(java8.util.concurrent.atomic.LongAccumulator) Longs(java8.lang.Longs)

Example 7 with Longs

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

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

the class SplittableRandomTest method testBoundedLongs.

/**
 * Each of a parallel sized stream of bounded longs is within bounds
 */
public void testBoundedLongs() {
    final AtomicInteger fails = new AtomicInteger(0);
    SplittableRandom r = new SplittableRandom();
    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(new LongConsumer() {

                @Override
                public void accept(long x) {
                    if (x < lo || x >= hi)
                        fails.getAndIncrement();
                }
            });
        }
    }
    assertEquals(fails.get(), 0);
}
Also used : LongConsumer(java8.util.function.LongConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SplittableRandom(java8.util.SplittableRandom)

Example 9 with Longs

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

the class SplittableRandomTest method testLongsCount.

/**
 * A parallel sized stream of longs generates the given number of values
 */
public void testLongsCount() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 0;
    for (int reps = 0; reps < REPS; ++reps) {
        counter.reset();
        r.longs(size).parallel().forEach(new LongConsumer() {

            @Override
            public void accept(long x) {
                counter.increment();
            }
        });
        assertEquals(counter.sum(), size);
        size += 524959;
    }
}
Also used : LongConsumer(java8.util.function.LongConsumer) LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

Example 10 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() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.longs().limit(size).parallel().forEach(new LongConsumer() {

        @Override
        public void accept(long x) {
            counter.increment();
        }
    });
    assertEquals(counter.sum(), size);
}
Also used : LongConsumer(java8.util.function.LongConsumer) LongAdder(java8.util.concurrent.atomic.LongAdder) SplittableRandom(java8.util.SplittableRandom)

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