use of java8.util.LongSummaryStatistics in project streamsupport by stefan-zobel.
the class CollectAndSummaryStatisticsTest method testLongStatistics.
public void testLongStatistics() {
List<LongSummaryStatistics> instances = new ArrayList<>();
instances.add(StreamSupport.stream(countTo(1000)).collect(Collectors.summarizingLong(i -> i)));
instances.add(StreamSupport.stream(countTo(1000)).mapToLong(i -> i).summaryStatistics());
instances.add(StreamSupport.stream(countTo(1000)).mapToLong(i -> i).collect(LongSummaryStatistics::new, LongSummaryStatistics::accept, LongSummaryStatistics::combine));
instances.add(StreamSupport.stream(countTo(1000)).mapToInt(i -> i).collect(() -> new LongSummaryStatistics(0, -1, 1001, 2), LongSummaryStatistics::accept, LongSummaryStatistics::combine));
instances.add(StreamSupport.parallelStream(countTo(1000)).collect(Collectors.summarizingLong(i -> i)));
instances.add(StreamSupport.parallelStream(countTo(1000)).mapToLong(i -> i).summaryStatistics());
instances.add(StreamSupport.parallelStream(countTo(1000)).mapToLong(i -> i).collect(LongSummaryStatistics::new, LongSummaryStatistics::accept, LongSummaryStatistics::combine));
instances.add(StreamSupport.parallelStream(countTo(1000)).mapToInt(i -> i).collect(() -> new LongSummaryStatistics(0, -1, 1001, 2), LongSummaryStatistics::accept, LongSummaryStatistics::combine));
LongSummaryStatistics original = instances.get(0);
instances.add(new LongSummaryStatistics(original.getCount(), original.getMin(), original.getMax(), original.getSum()));
for (LongSummaryStatistics stats : instances) {
assertEquals(stats.getCount(), 1000);
assertEquals(stats.getSum(), (long) StreamSupport.stream(countTo(1000)).mapToInt(i -> i).sum());
assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
assertEquals(stats.getMax(), 1000L);
assertEquals(stats.getMin(), 1L);
}
expectThrows(IllegalArgumentException.class, () -> new LongSummaryStatistics(-1, 0, 0, 0));
expectThrows(IllegalArgumentException.class, () -> new LongSummaryStatistics(1, 3, 2, 0));
}
use of java8.util.LongSummaryStatistics in project streamsupport by stefan-zobel.
the class SummaryStatisticsTest method testLongStatistics.
public void testLongStatistics() {
List<LongSummaryStatistics> instances = new ArrayList<>();
instances.add(StreamSupport.stream(countTo(1000)).collect(Collectors.summarizingLong(i -> i)));
instances.add(StreamSupport.stream(countTo(1000)).mapToLong(i -> i).summaryStatistics());
instances.add(StreamSupport.stream(countTo(1000), 0, true).collect(Collectors.summarizingLong(i -> i)));
instances.add(StreamSupport.stream(countTo(1000), 0, true).mapToLong(i -> i).summaryStatistics());
for (LongSummaryStatistics stats : instances) {
assertEquals(stats.getCount(), 1000);
assertEquals(stats.getSum(), (long) StreamSupport.stream(countTo(1000)).mapToInt(i -> i).sum());
assertEquals(stats.getMax(), 1000L);
assertEquals(stats.getMin(), 1L);
}
}
Aggregations