Search in sources :

Example 11 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testBoundedDoubles.

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

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

Example 12 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class SplittableRandomTest method testUnsizedIntsCountSeq.

/**
 * A sequential unsized stream of ints generates at least 100 values
 */
public void testUnsizedIntsCountSeq() {
    final LongAdder counter = new LongAdder();
    SplittableRandom r = new SplittableRandom();
    long size = 100;
    r.ints().limit(size).forEach(new IntConsumer() {

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

Example 13 with Stream

use of java8.util.stream.Stream 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)

Example 14 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class TestDoubleSumAverage method testForCompensation.

/**
 * Compute the sum and average of a sequence of double values in
 * various ways and report an error if naive summation is used.
 */
private static int testForCompensation() {
    int failures = 0;
    /*
         * The exact sum of the test stream is 1 + 1e6*ulp(1.0) but a
         * naive summation algorithm will return 1.0 since (1.0 +
         * ulp(1.0)/2) will round to 1.0 again.
         */
    final double base = 1.0;
    final double increment = Math.ulp(base) / 2.0;
    final int count = 1000001;
    double expectedSum = base + (increment * (count - 1));
    double expectedAvg = expectedSum / count;
    // Factory for double a stream of [base, increment, ..., increment] limited to a size of count
    Supplier<DoubleStream> ds = new Supplier<DoubleStream>() {

        @Override
        public DoubleStream get() {
            return DoubleStreams.iterate(base, new DoubleUnaryOperator() {

                @Override
                public double applyAsDouble(double e) {
                    return increment;
                }
            }).limit(count);
        }
    };
    DoubleSummaryStatistics stats = ds.get().collect(new Supplier<DoubleSummaryStatistics>() {

        @Override
        public DoubleSummaryStatistics get() {
            return new DoubleSummaryStatistics();
        }
    }, (ObjDoubleConsumer<DoubleSummaryStatistics>) new ObjDoubleConsumer<DoubleSummaryStatistics>() {

        @Override
        public void accept(DoubleSummaryStatistics doubleSummaryStatistics, double v) {
            doubleSummaryStatistics.accept(v);
        }
    }, (BiConsumer<DoubleSummaryStatistics, DoubleSummaryStatistics>) new BiConsumer<DoubleSummaryStatistics, DoubleSummaryStatistics>() {

        @Override
        public void accept(DoubleSummaryStatistics doubleSummaryStatistics, DoubleSummaryStatistics doubleSummaryStatistics2) {
            doubleSummaryStatistics.combine(doubleSummaryStatistics2);
        }
    });
    failures += compareUlpDifference(expectedSum, stats.getSum(), 3);
    failures += compareUlpDifference(expectedAvg, stats.getAverage(), 3);
    failures += compareUlpDifference(expectedSum, ds.get().sum(), 3);
    failures += compareUlpDifference(expectedAvg, ds.get().average().getAsDouble(), 3);
    failures += compareUlpDifference(expectedSum, ds.get().boxed().collect(Collectors.summingDouble(new ToDoubleFunction<Double>() {

        @Override
        public double applyAsDouble(Double d) {
            return d;
        }
    })), 3);
    failures += compareUlpDifference(expectedAvg, ds.get().boxed().collect(Collectors.averagingDouble(new ToDoubleFunction<Double>() {

        @Override
        public double applyAsDouble(Double d) {
            return d;
        }
    })), 3);
    return failures;
}
Also used : DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics) Double(java.lang.Double)

Example 15 with Stream

use of java8.util.stream.Stream in project streamsupport by stefan-zobel.

the class StringJoinerTest method addAddAll.

public void addAddAll() {
    final StringJoiner sj = new StringJoiner(DASH, "{", "}");
    sj.add(ONE);
    ArrayList<String> nextOne = new ArrayList<String>();
    nextOne.add(TWO);
    nextOne.add(THREE);
    Stream<String> stream = StreamSupport.stream(nextOne);
    stream.forEachOrdered(new java8.util.function.Consumer<String>() {

        @Override
        public void accept(String t) {
            sj.add(t);
        }
    });
    String expected = "{" + ONE + DASH + TWO + DASH + THREE + "}";
    assertEquals(sj.toString(), expected);
}
Also used : ArrayList(java.util.ArrayList) StringJoiner(java8.util.StringJoiner)

Aggregations

LongAdder (java8.util.concurrent.atomic.LongAdder)36 SplittableRandom (java8.util.SplittableRandom)27 ThreadLocalRandom (java8.util.concurrent.ThreadLocalRandom)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 ArrayList (java.util.ArrayList)9 Test (org.testng.annotations.Test)9 Function (java8.util.function.Function)8 OpTestCase (java8.util.stream.OpTestCase)8 List (java.util.List)7 StreamSupport (java8.util.stream.StreamSupport)7 TestData (java8.util.stream.TestData)7 Set (java.util.Set)6 Supplier (java8.util.function.Supplier)6 DoubleStream (java8.util.stream.DoubleStream)6 IntStream (java8.util.stream.IntStream)6 LongStream (java8.util.stream.LongStream)6 HashSet (java.util.HashSet)5 Spliterator (java8.util.Spliterator)5 Consumer (java8.util.function.Consumer)5 Collection (java.util.Collection)4