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.stream.*;
  26 
  27 /*
  28  * @test
  29  * @bug 8006572
  30  */
  31 public class TestDoubleSumAverage {
  32     public static void main(String... args) {
  33         int failures = 0;
  34 
  35         double base = 1.0;
  36         double increment = Math.ulp(base)/2.0;
  37         int count = 1_000_001;
  38         /*
  39          * The exact sum of the test stream is 1 + 1e6*ulp(1.0) but a
  40          * naive summation algorithm will return 1.0 since (1.0 +
  41          * ulp(1.0)/2) will round to 1.0 again.
  42          */
  43         DoubleSummaryStatistics stats =
  44             testStream(base, increment, count).collect(DoubleSummaryStatistics::new,
  45                                                        DoubleSummaryStatistics::accept,
  46                                                        DoubleSummaryStatistics::combine);
  47 
  48         double expectedSum = base + (increment * (count - 1));
  49         double expectedAvg = expectedSum / count;
  50 
  51         failures += compareUlpDifference(expectedSum, stats.getSum(), 3);
  52         failures += compareUlpDifference(expectedAvg, stats.getAverage(), 3);
  53 
  54         failures += compareUlpDifference(expectedSum, testStream(base, increment, count).sum(), 3);
  55         failures += compareUlpDifference(expectedAvg, testStream(base, increment, count).average().getAsDouble(), 3);
  56 
  57         double collectorSum = testBoxedStream(base, increment, count).
  58             collect(Collectors.summingDouble(d -> d));
  59         failures += compareUlpDifference(expectedSum, collectorSum, 3);
  60 
  61         double collectorAvg = testBoxedStream(base, increment, count).
  62             collect(Collectors.averagingDouble(d -> d));
  63         failures += compareUlpDifference(expectedAvg, collectorAvg, 3);
  64 
  65         if (failures > 0) {
  66             throw new RuntimeException("Found " + failures + " numerical failures.");
  67         }
  68     }
  69 
  70     private static DoubleStream testStream(double base, double increment, int count) {
  71         return StreamSupport.
  72             doubleStream(Spliterators.spliteratorUnknownSize(new TestDoubleIterator(base,
  73                                                                                     increment,
  74                                                                                     count),
  75                                                              Spliterator.IMMUTABLE |
  76                                                              Spliterator.NONNULL),
  77                          false);
  78     }
  79 
  80     private static  Stream<Double> testBoxedStream(double base, double increment, int count) {
  81         TestDoubleIterator tdi = new TestDoubleIterator(base, increment, count);
  82         Double[] tmp = new Double[count];
  83         int i = 0;
  84         while(tdi.hasNext()) {
  85             tmp[i] = tdi.next();
  86             i++;
  87         }
  88         return Stream.of(tmp);
  89     }
  90 
  91     private static int compareUlpDifference(double expected, double computed, double threshold) {
  92         double ulpDifference = Math.abs(expected - computed) / Math.ulp(expected);
  93 
  94         if (ulpDifference > threshold) {
  95             System.err.printf("Numerical summation error too large, %g ulps rather than %g.%n",
  96                               ulpDifference, threshold);
  97             return 1;
  98         } else
  99             return 0;
 100     }
 101 
 102     static class TestDoubleIterator implements PrimitiveIterator.OfDouble {
 103         private double base;
 104         private double subsequent;
 105         private long count;
 106         private long iteration = 0;
 107 
 108         /**
 109          * Construct a {@code TestDoubleStream} of {@code count}
 110          * elements whose first value is {@code base} and whose
 111          * following values are all {@code subsequent}.
 112          */
 113         TestDoubleIterator(double base, double subsequent, long count) {
 114             this.base = base;
 115             this.subsequent = subsequent;
 116             this.count = count;
 117         }
 118 
 119         @Override
 120         public boolean hasNext() {
 121             return (iteration < count);
 122         }
 123 
 124         @Override
 125         public double nextDouble() {
 126             if (iteration >= count)
 127                 throw new NoSuchElementException();
 128             else {
 129                 iteration++;
 130                 return (iteration == 1) ? base : subsequent;
 131             }
 132         }
 133     }
 134 }