use of java8.util.function.BiConsumer in project streamsupport by stefan-zobel.
the class SplittableRandomTest method testBadStreamBounds.
/**
* Invoking bounded ints, long, doubles, with illegal bounds throws
* IllegalArgumentException
*/
public void testBadStreamBounds() {
final SplittableRandom r = new SplittableRandom();
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.ints(2, 1);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.ints(10, 42, 42);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.longs(-1L, -1L);
}
});
executeAndCatchIAE(new Runnable() {
@Override
public void run() {
r.longs(10, 1L, -2L);
}
});
testDoubleBadOriginBound(new BiConsumer<Double, Double>() {
@Override
public void accept(Double o, Double b) {
r.doubles(10, o, b);
}
});
}
use of java8.util.function.BiConsumer 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.function.BiConsumer in project streamsupport by stefan-zobel.
the class TestDoubleSumAverage method testNonfiniteSum.
private static int testNonfiniteSum() {
int failures = 0;
Map<Supplier<DoubleStream>, Double> testCases = new LinkedHashMap<Supplier<DoubleStream>, Double>();
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(MAX_VALUE, MAX_VALUE);
}
}, POSITIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(-MAX_VALUE, -MAX_VALUE);
}
}, NEGATIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(1.0d, POSITIVE_INFINITY, 1.0d);
}
}, POSITIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(POSITIVE_INFINITY);
}
}, POSITIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(POSITIVE_INFINITY, POSITIVE_INFINITY);
}
}, POSITIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(POSITIVE_INFINITY, POSITIVE_INFINITY, 0.0);
}
}, POSITIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(1.0d, NEGATIVE_INFINITY, 1.0d);
}
}, NEGATIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(NEGATIVE_INFINITY);
}
}, NEGATIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(NEGATIVE_INFINITY, NEGATIVE_INFINITY);
}
}, NEGATIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(NEGATIVE_INFINITY, NEGATIVE_INFINITY, 0.0);
}
}, NEGATIVE_INFINITY);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(1.0d, NaN, 1.0d);
}
}, NaN);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(NaN);
}
}, NaN);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(1.0d, NEGATIVE_INFINITY, POSITIVE_INFINITY, 1.0d);
}
}, NaN);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(1.0d, POSITIVE_INFINITY, NEGATIVE_INFINITY, 1.0d);
}
}, NaN);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(POSITIVE_INFINITY, NaN);
}
}, NaN);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(NEGATIVE_INFINITY, NaN);
}
}, NaN);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(NaN, POSITIVE_INFINITY);
}
}, NaN);
testCases.put(new Supplier<DoubleStream>() {
@Override
public DoubleStream get() {
return DoubleStreams.of(NaN, NEGATIVE_INFINITY);
}
}, NaN);
for (Map.Entry<Supplier<DoubleStream>, Double> testCase : testCases.entrySet()) {
Supplier<DoubleStream> ds = testCase.getKey();
double expected = testCase.getValue();
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(expected, stats.getSum(), 0);
failures += compareUlpDifference(expected, stats.getAverage(), 0);
failures += compareUlpDifference(expected, ds.get().sum(), 0);
failures += compareUlpDifference(expected, ds.get().average().getAsDouble(), 0);
failures += compareUlpDifference(expected, ds.get().boxed().collect(Collectors.summingDouble(new ToDoubleFunction<Double>() {
@Override
public double applyAsDouble(Double d) {
return d;
}
})), 0);
failures += compareUlpDifference(expected, ds.get().boxed().collect(Collectors.averagingDouble(new ToDoubleFunction<Double>() {
@Override
public double applyAsDouble(Double d) {
return d;
}
})), 0);
}
return failures;
}
use of java8.util.function.BiConsumer in project streamsupport by stefan-zobel.
the class ConcurrentAssociateTest method testOnce.
private static void testOnce(final String desc, final BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
final ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<Object, Object>();
final CountDownLatch s = new CountDownLatch(1);
final Supplier<Runnable> putter = new Supplier<Runnable>() {
@Override
public Runnable get() {
return new Runnable() {
@Override
public void run() {
try {
s.await();
} catch (InterruptedException e) {
}
for (int i = 0; i < N; i++) {
Object o = new X();
associator.accept(m, o);
if (!m.containsKey(o)) {
throw new AssociationFailure(desc + " failed: entry does not exist");
}
}
}
};
}
};
Stream<CompletableFuture<Void>> putters = IntStreams.range(0, availableProcessors).mapToObj(new IntFunction<Runnable>() {
public Runnable apply(int i) {
return putter.get();
}
}).map(new Function<Runnable, CompletableFuture<Void>>() {
@Override
public CompletableFuture<Void> apply(Runnable runnable) {
return CompletableFuture.runAsync(runnable);
}
});
CompletableFuture<Void> all = CompletableFuture.allOf(putters.toArray(new IntFunction<CompletableFuture<Void>[]>() {
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<Void>[] apply(int size) {
return (CompletableFuture<Void>[]) new CompletableFuture[size];
}
}));
// Trigger the runners to start
s.countDown();
try {
all.join();
} catch (CompletionException e) {
Throwable t = e.getCause();
if (t instanceof AssociationFailure) {
throw (AssociationFailure) t;
} else {
throw e;
}
}
}
Aggregations