test/java/util/stream/TestDoubleSumAverage.java

Print this page




  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 }


  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 8030212
  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 += testZeroAverageOfNonEmptyStream();
  38         failures += testForCompenstation();
  39         failures += testInfiniteSum();
  40 
  41         if (failures > 0) {
  42             throw new RuntimeException("Found " + failures + " numerical failure(s).");
  43         }
  44     }
  45 
  46     /**
  47      * Test to verify that a non-empty stream with a zero average is non-empty.
  48      */
  49     private static int testZeroAverageOfNonEmptyStream() {
  50         Supplier<DoubleStream> ds = () -> DoubleStream.iterate(0.0, e -> 0.0).limit(10);
  51 
  52         return  compareUlpDifference(0.0, ds.get().average().getAsDouble(), 0);
  53     }
  54 
  55     /**
  56      * Compute the sum and average of a sequence of double values in
  57      * various ways and report an error if naive summation is used.
  58      */
  59     private static int testForCompenstation() {
  60         int failures = 0;
  61 
  62         /*
  63          * The exact sum of the test stream is 1 + 1e6*ulp(1.0) but a
  64          * naive summation algorithm will return 1.0 since (1.0 +
  65          * ulp(1.0)/2) will round to 1.0 again.
  66          */
  67         double base = 1.0;
  68         double increment = Math.ulp(base)/2.0;
  69         int count = 1_000_001;
  70 
  71         double expectedSum = base + (increment * (count - 1));
  72         double expectedAvg = expectedSum / count;
  73 
  74         // Factory for double a stream of [base, increment, ..., increment] limited to a size of count
  75         Supplier<DoubleStream> ds = () -> DoubleStream.iterate(base, e -> increment).limit(count);
  76 
  77         DoubleSummaryStatistics stats = ds.get().collect(DoubleSummaryStatistics::new,
  78                                                          DoubleSummaryStatistics::accept,
  79                                                          DoubleSummaryStatistics::combine);
  80 
  81         failures += compareUlpDifference(expectedSum, stats.getSum(), 3);
  82         failures += compareUlpDifference(expectedAvg, stats.getAverage(), 3);
  83 
  84         failures += compareUlpDifference(expectedSum,
  85                                          ds.get().sum(), 3);
  86         failures += compareUlpDifference(expectedAvg,
  87                                          ds.get().average().getAsDouble(), 3);
  88 
  89         failures += compareUlpDifference(expectedSum,
  90                                          ds.get().boxed().collect(Collectors.summingDouble(d -> d)), 3);
  91         failures += compareUlpDifference(expectedAvg,
  92                                          ds.get().boxed().collect(Collectors.averagingDouble(d -> d)),3);
  93         return failures;
  94     }
  95 
  96     private static int testInfiniteSum() {
  97         int failures = 0;



  98 
  99         List<Supplier<DoubleStream>> dss = new ArrayList<>();
 100         dss.add(() -> DoubleStream.of(1.0d, Double.POSITIVE_INFINITY, 1.0d));
 101         dss.add(() -> DoubleStream.of(1.0d, Double.NEGATIVE_INFINITY, 1.0d));
 102         dss.add(() -> DoubleStream.of(1.0d, Double.NaN, 1.0d));
 103         dss.add(() -> DoubleStream.of(1.0d, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0d));
 104                           
 105         double[] expected = {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN, Double.NaN};
 106 
 107         int i = 0;
 108         for(Supplier<DoubleStream> ds : dss) {
 109             
 110             DoubleSummaryStatistics stats = ds.get().collect(DoubleSummaryStatistics::new,
 111                                                              DoubleSummaryStatistics::accept,
 112                                                              DoubleSummaryStatistics::combine);
 113             System.err.println(i);
 114             System.err.println("\ta");
 115             failures += compareUlpDifference(expected[i], stats.getSum(), 0);
 116             System.err.println("\tb");
 117             failures += compareUlpDifference(expected[i], stats.getAverage(), 0);
 118 
 119             System.err.println("\tc");
 120             failures += compareUlpDifference(expected[i], ds.get().sum(), 0);
 121             System.err.println("\td");
 122             failures += compareUlpDifference(expected[i], ds.get().average().getAsDouble(), 0);
 123 
 124             System.err.println("\te");
 125             failures += compareUlpDifference(expected[i], ds.get().boxed().collect(Collectors.summingDouble(d -> d)), 0);
 126             System.err.println("\tf");
 127             failures += compareUlpDifference(expected[i], ds.get().boxed().collect(Collectors.averagingDouble(d -> d)), 0);
 128 
 129             i++;
 130         }
 131 
 132         return failures;
 133     }
 134 
 135     /**
 136      * Compute the ulp difference of two double values and compare against an error threshold.
 137      */
 138     private static int compareUlpDifference(double expected, double computed, double threshold) {
 139         if (!Double.isFinite(expected)) {
 140             // Handle NaN and infinity cases
 141             if (Double.compare(expected, computed) == 0)
 142                 return 0;
 143             else {
 144             System.err.printf("Unexpected sum, %g rather than %g.%n",
 145                               computed, expected);
 146                 return 1;
 147             }
 148         }
 149 
 150         double ulpDifference = Math.abs(expected - computed) / Math.ulp(expected);
 151 
 152         if (ulpDifference > threshold) {
 153             System.err.printf("Numerical summation error too large, %g ulps rather than %g.%n",
 154                               ulpDifference, threshold);
 155             return 1;
 156         } else
 157             return 0;
 158     }
 159 }