1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.util.*;
  25 import java.util.function.*;
  26 import java.util.stream.*;
  27 
  28 /*
  29  * @test
  30  * @bug 8006572
  31  * @summary Test for use of non-naive summation in stream-related sum and average operations.
  32  */
  33 public class TestDoubleSumAverage {
  34     public static void main(String... args) {
  35         int failures = 0;
  36 
  37         failures += testForCompenstation();
  38         failures += testZeroAverageOfNonEmptyStream();
  39 
  40         if (failures > 0) {
  41             throw new RuntimeException("Found " + failures + " numerical failure(s).");
  42         }
  43     }
  44 
  45     /**
  46      * Compute the sum and average of a sequence of double values in
  47      * various ways and report an error if naive summation is used.
  48      */
  49     private static int testForCompenstation() {
  50         int failures = 0;
  51 
  52         /*
  53          * The exact sum of the test stream is 1 + 1e6*ulp(1.0) but a
  54          * naive summation algorithm will return 1.0 since (1.0 +
  55          * ulp(1.0)/2) will round to 1.0 again.
  56          */
  57         double base = 1.0;
  58         double increment = Math.ulp(base)/2.0;
  59         int count = 1_000_001;
  60 
  61         double expectedSum = base + (increment * (count - 1));
  62         double expectedAvg = expectedSum / count;
  63 
  64         // Factory for double a stream of [base, increment, ..., increment] limited to a size of count
  65         Supplier<DoubleStream> ds = () -> DoubleStream.iterate(base, e -> increment).limit(count);
  66 
  67         DoubleSummaryStatistics stats = ds.get().collect(DoubleSummaryStatistics::new,
  68                                                          DoubleSummaryStatistics::accept,
  69                                                          DoubleSummaryStatistics::combine);
  70 
  71         failures += compareUlpDifference(expectedSum, stats.getSum(), 3);
  72         failures += compareUlpDifference(expectedAvg, stats.getAverage(), 3);
  73 
  74         failures += compareUlpDifference(expectedSum,
  75                                          ds.get().sum(), 3);
  76         failures += compareUlpDifference(expectedAvg,
  77                                          ds.get().average().getAsDouble(), 3);
  78 
  79         failures += compareUlpDifference(expectedSum,
  80                                          ds.get().boxed().collect(Collectors.summingDouble(d -> d)), 3);
  81         failures += compareUlpDifference(expectedAvg,
  82                                          ds.get().boxed().collect(Collectors.averagingDouble(d -> d)),3);
  83         return failures;
  84     }
  85 
  86     /**
  87      * Test to verify that a non-empty stream with a zero average is non-empty.
  88      */
  89     private static int testZeroAverageOfNonEmptyStream() {
  90         Supplier<DoubleStream> ds = () -> DoubleStream.iterate(0.0, e -> 0.0).limit(10);
  91 
  92         return  compareUlpDifference(0.0, ds.get().average().getAsDouble(), 0);
  93     }
  94 
  95     /**
  96      * Compute the ulp difference of two double values and compare against an error threshold.
  97      */
  98     private static int compareUlpDifference(double expected, double computed, double threshold) {
  99         double ulpDifference = Math.abs(expected - computed) / Math.ulp(expected);
 100 
 101         if (ulpDifference > threshold) {
 102             System.err.printf("Numerical summation error too large, %g ulps rather than %g.%n",
 103                               ulpDifference, threshold);
 104             return 1;
 105         } else
 106             return 0;
 107     }
 108 }