Search in sources :

Example 1 with LongSummaryStatistics

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));
}
Also used : LongSummaryStatistics(java8.util.LongSummaryStatistics) DoubleStreams(java8.util.stream.DoubleStreams) LambdaTestHelpers.countTo(java8.util.stream.LambdaTestHelpers.countTo) OpTestCase(java8.util.stream.OpTestCase) Test(org.testng.annotations.Test) Collectors(java8.util.stream.Collectors) StreamSupport(java8.util.stream.StreamSupport) ArrayList(java.util.ArrayList) IntStreams(java8.util.stream.IntStreams) LongStreams(java8.util.stream.LongStreams) ThrowableHelper.checkNPE(java8.util.stream.ThrowableHelper.checkNPE) List(java.util.List) IntSummaryStatistics(java8.util.IntSummaryStatistics) LongSummaryStatistics(java8.util.LongSummaryStatistics) DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics) Assert.expectThrows(org.testng695.Assert.expectThrows) ArrayList(java.util.ArrayList)

Example 2 with LongSummaryStatistics

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);
    }
}
Also used : LongSummaryStatistics(java8.util.LongSummaryStatistics) List(java.util.List) IntSummaryStatistics(java8.util.IntSummaryStatistics) LambdaTestHelpers.countTo(java8.util.stream.LambdaTestHelpers.countTo) OpTestCase(java8.util.stream.OpTestCase) LongSummaryStatistics(java8.util.LongSummaryStatistics) Collectors(java8.util.stream.Collectors) Test(org.testng.annotations.Test) StreamSupport(java8.util.stream.StreamSupport) ArrayList(java.util.ArrayList) DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 DoubleSummaryStatistics (java8.util.DoubleSummaryStatistics)2 IntSummaryStatistics (java8.util.IntSummaryStatistics)2 LongSummaryStatistics (java8.util.LongSummaryStatistics)2 Collectors (java8.util.stream.Collectors)2 LambdaTestHelpers.countTo (java8.util.stream.LambdaTestHelpers.countTo)2 OpTestCase (java8.util.stream.OpTestCase)2 StreamSupport (java8.util.stream.StreamSupport)2 Test (org.testng.annotations.Test)2 DoubleStreams (java8.util.stream.DoubleStreams)1 IntStreams (java8.util.stream.IntStreams)1 LongStreams (java8.util.stream.LongStreams)1 ThrowableHelper.checkNPE (java8.util.stream.ThrowableHelper.checkNPE)1 Assert.expectThrows (org.testng695.Assert.expectThrows)1