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);
}
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);
}
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);
}
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;
}
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);
}
Aggregations