--- /dev/null 2013-11-18 16:43:35.025865568 -0800 +++ new/test/java/util/DoubleSummaryStatistics/TestDoubleSummaryStatisticsSummation.java 2013-11-19 14:16:51.000000000 -0800 @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.util.*; +import java.util.stream.*; + +/* + * @test + * @bug 8006572 + */ +public class TestDoubleSummaryStatisticsSummation { + public static void main(String... args) { + double base = 1.0; + double increment = Math.ulp(base)/2.0; + int count = 1_000_001; + /* + * The exact sum of this 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. + */ + TestDoubleIterator tdi = new TestDoubleIterator(base, + increment, + count); + DoubleSummaryStatistics stats = + StreamSupport.doubleStream(Spliterators.spliteratorUnknownSize(tdi, + Spliterator.IMMUTABLE | + Spliterator.NONNULL), + false).collect(DoubleSummaryStatistics::new, + DoubleSummaryStatistics::accept, + DoubleSummaryStatistics::combine); + + double expectedSum = base + (increment * (count - 1)); + compareUlpDifference(expectedSum, stats.getSum(), 3); + + double expectedAvg = expectedSum / count; + compareUlpDifference(expectedAvg, stats.getAverage(), 3); + } + + private static void compareUlpDifference(double expected, double computed, double threshold) { + double ulpDifference = Math.abs(expected - computed) / Math.ulp(expected); + + if (ulpDifference > threshold) { + System.out.printf("Numerical summation error too large, %g ulps rather than %g.%n", + ulpDifference, threshold); + throw new RuntimeException(); + } + + } + + static class TestDoubleIterator implements PrimitiveIterator.OfDouble { + private double base; + private double subsequent; + private long count; + private long iteration = 0; + + /** + * Construct a {@code TestDoubleStream} of {@code count} + * elements whose first value is {@code base} and whose + * following values are all {@code subsequent}. + */ + TestDoubleIterator(double base, double subsequent, long count) { + this.base = base; + this.subsequent = subsequent; + this.count = count; + } + + @Override + public boolean hasNext() { + return (iteration < count); + } + + @Override + public double nextDouble() { + if (iteration >= count) + throw new NoSuchElementException(); + else { + iteration++; + return (iteration == 1) ? base : subsequent; + } + } + } +}