Search in sources :

Example 11 with Doubles

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

the class ThreadLocalRandomTest method testUnsizedDoublesCount.

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

Example 12 with Doubles

use of java8.lang.Doubles 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 13 with Doubles

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

Example 14 with Doubles

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

the class ThreadLocalRandomTest method testBadStreamSize.

/**
 * Invoking sized ints, long, doubles, with negative sizes throws
 * IllegalArgumentException
 */
public void testBadStreamSize() {
    ThreadLocalRandom r = ThreadLocalRandom.current();
    executeAndCatchIAE(() -> r.ints(-1L));
    executeAndCatchIAE(() -> r.ints(-1L, 2, 3));
    executeAndCatchIAE(() -> r.longs(-1L));
    executeAndCatchIAE(() -> r.longs(-1L, -1L, 1L));
    executeAndCatchIAE(() -> r.doubles(-1L));
    executeAndCatchIAE(() -> r.doubles(-1L, .5, .6));
}
Also used : ThreadLocalRandom(java8.util.concurrent.ThreadLocalRandom)

Example 15 with Doubles

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

the class SplittableRandomTest method doublesDataProvider.

@DataProvider(name = "doubles")
public static Object[][] doublesDataProvider() {
    List<Object[]> data = new ArrayList<>();
    // Function to create a stream using a RandomBoxedSpliterator
    Function<Function<SplittableRandom, Double>, DoubleStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToDouble(i -> i);
    // Unbounded
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles().limit(%d)", SIZE), () -> new SplittableRandom().doubles().limit(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d)", SIZE), () -> new SplittableRandom().doubles(SIZE)), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble())", SIZE), () -> rbsf.apply(sr -> sr.nextDouble())), randomAsserter(SIZE, Double.MAX_VALUE, 0d) });
    for (int b : BOUNDS) {
        for (int o : ORIGINS) {
            final double origin = o;
            final double bound = b;
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%f, %f).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().doubles(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new SplittableRandom().doubles(%d, %f, %f)", SIZE, origin, bound), () -> new SplittableRandom().doubles(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
            if (origin == 0) {
                data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextDouble(bound))), randomAsserter(SIZE, origin, bound) });
            }
            data.add(new Object[] { TestData.Factory.ofDoubleSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextDouble(%f, %f))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextDouble(origin, bound))), randomAsserter(SIZE, origin, bound) });
        }
    }
    return data.toArray(new Object[0][]);
}
Also used : DataProvider(org.testng.annotations.DataProvider) Spliterator(java8.util.Spliterator) IntStream(java8.util.stream.IntStream) OpTestCase(java8.util.stream.OpTestCase) Set(java.util.Set) Test(org.testng.annotations.Test) Spliterators(java8.util.Spliterators) SplittableRandom(java8.util.SplittableRandom) LongStreamTestScenario(java8.util.stream.LongStreamTestScenario) LongStream(java8.util.stream.LongStream) StreamSupport(java8.util.stream.StreamSupport) TestData(java8.util.stream.TestData) Function(java8.util.function.Function) IntStreamTestScenario(java8.util.stream.IntStreamTestScenario) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) List(java.util.List) Consumer(java8.util.function.Consumer) DoubleStream(java8.util.stream.DoubleStream) Comparator(java.util.Comparator) DoubleStreamTestScenario(java8.util.stream.DoubleStreamTestScenario) Function(java8.util.function.Function) ArrayList(java.util.ArrayList) DoubleStream(java8.util.stream.DoubleStream) SplittableRandom(java8.util.SplittableRandom) DataProvider(org.testng.annotations.DataProvider)

Aggregations

SplittableRandom (java8.util.SplittableRandom)13 ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)12 LongAdder (java8.util.concurrent.atomic.LongAdder)12 Doubles (java8.lang.Doubles)8 DoubleAccumulator (java8.util.concurrent.atomic.DoubleAccumulator)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DoubleConsumer (java8.util.function.DoubleConsumer)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