Search in sources :

Example 1 with DoubleSummaryStatistics

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

the class SummaryStatisticsTest method testDoubleStatistics.

public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(StreamSupport.stream(countTo(1000)).collect(Collectors.summarizingDouble(i -> i)));
    instances.add(StreamSupport.stream(countTo(1000)).mapToDouble(i -> i).summaryStatistics());
    instances.add(StreamSupport.stream(countTo(1000), 0, true).collect(Collectors.summarizingDouble(i -> i)));
    instances.add(StreamSupport.stream(countTo(1000), 0, true).mapToDouble(i -> i).summaryStatistics());
    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) StreamSupport.stream(countTo(1000)).mapToInt(i -> i).sum());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
}
Also used : 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) DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics)

Example 2 with DoubleSummaryStatistics

use of java8.util.DoubleSummaryStatistics 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 3 with DoubleSummaryStatistics

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

the class CollectAndSummaryStatisticsTest method testDoubleStatistics.

public void testDoubleStatistics() {
    List<DoubleSummaryStatistics> instances = new ArrayList<>();
    instances.add(StreamSupport.stream(countTo(1000)).collect(Collectors.summarizingDouble(i -> i)));
    instances.add(StreamSupport.stream(countTo(1000)).mapToDouble(i -> i).summaryStatistics());
    instances.add(StreamSupport.stream(countTo(1000)).mapToDouble(i -> i).collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept, DoubleSummaryStatistics::combine));
    instances.add(StreamSupport.stream(countTo(1000)).mapToInt(i -> i).collect(() -> new DoubleSummaryStatistics(0, -1, 1001, 2), DoubleSummaryStatistics::accept, DoubleSummaryStatistics::combine));
    instances.add(StreamSupport.parallelStream(countTo(1000)).collect(Collectors.summarizingDouble(i -> i)));
    instances.add(StreamSupport.parallelStream(countTo(1000)).mapToDouble(i -> i).summaryStatistics());
    instances.add(StreamSupport.parallelStream(countTo(1000)).mapToDouble(i -> i).collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept, DoubleSummaryStatistics::combine));
    instances.add(StreamSupport.parallelStream(countTo(1000)).mapToInt(i -> i).collect(() -> new DoubleSummaryStatistics(0, -1, 1001, 2), DoubleSummaryStatistics::accept, DoubleSummaryStatistics::combine));
    DoubleSummaryStatistics original = instances.get(0);
    instances.add(new DoubleSummaryStatistics(original.getCount(), original.getMin(), original.getMax(), original.getSum()));
    for (DoubleSummaryStatistics stats : instances) {
        assertEquals(stats.getCount(), 1000);
        assertEquals(stats.getSum(), (double) StreamSupport.stream(countTo(1000)).mapToInt(i -> i).sum());
        assertEquals(stats.getAverage(), stats.getSum() / stats.getCount());
        assertEquals(stats.getMax(), 1000.0);
        assertEquals(stats.getMin(), 1.0);
    }
    expectThrows(IllegalArgumentException.class, () -> new DoubleSummaryStatistics(-1, 0, 0, 0));
    expectThrows(IllegalArgumentException.class, () -> new DoubleSummaryStatistics(1, 3, 2, 0));
    double[] values = { 1.0, Double.NaN };
    for (double min : values) {
        for (double max : values) {
            for (double sum : values) {
                if (Double.isNaN(min) && Double.isNaN(max) && Double.isNaN(sum))
                    continue;
                if (!Double.isNaN(min) && !Double.isNaN(max) && !Double.isNaN(sum))
                    continue;
                expectThrows(IllegalArgumentException.class, () -> new DoubleSummaryStatistics(1, min, max, sum));
            }
        }
    }
}
Also used : 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) DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics)

Example 4 with DoubleSummaryStatistics

use of java8.util.DoubleSummaryStatistics 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;
}
Also used : DoubleSummaryStatistics(java8.util.DoubleSummaryStatistics) Double(java.lang.Double)

Aggregations

DoubleSummaryStatistics (java8.util.DoubleSummaryStatistics)4 Double (java.lang.Double)2 ArrayList (java.util.ArrayList)2 List (java.util.List)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