1 /*
2  * Copyright (c) 2021, 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 /*
25  * @test
26  * @bug 8214761
27  * @run testng NegativeCompensation
28  * @summary When combining two DoubleSummaryStatistics, the compensation
29  *          has to be subtracted.
30  */
31 
32 package test.java.util.DoubleSummaryStatistics;
33 
34 import java.util.DoubleSummaryStatistics;
35 import org.testng.annotations.Test;
36 import static org.testng.Assert.assertEquals;
37 import static org.testng.Assert.assertTrue;
38 
39 import android.platform.test.annotations.LargeTest;
40 
41 public class NegativeCompensation {
42     static final double VAL = 1.000000001;
43     static final int LOG_ITER = 21;
44 
45     @LargeTest
46     @Test
testErrorComparision()47     public static void testErrorComparision() {
48         DoubleSummaryStatistics stat0 = new DoubleSummaryStatistics();
49         DoubleSummaryStatistics stat1 = new DoubleSummaryStatistics();
50         DoubleSummaryStatistics stat2 = new DoubleSummaryStatistics();
51 
52         stat1.accept(VAL);
53         stat1.accept(VAL);
54         stat2.accept(VAL);
55         stat2.accept(VAL);
56         stat2.accept(VAL);
57 
58         for (int i = 0; i < LOG_ITER; ++i) {
59             stat1.combine(stat2);
60             stat2.combine(stat1);
61         }
62 
63         for (long i = 0, iend = stat2.getCount(); i < iend; ++i) {
64             stat0.accept(VAL);
65         }
66 
67         double res = 0;
68         for(long i = 0, iend = stat2.getCount(); i < iend; ++i) {
69             res += VAL;
70         }
71 
72         double absErrN = Math.abs(res - stat2.getSum());
73         double absErr = Math.abs(stat0.getSum() - stat2.getSum());
74         assertTrue(absErrN >= absErr,
75                 "Naive sum error is not greater than or equal to Summary sum");
76         assertEquals(absErr, 0.0, "Absolute error is not zero");
77     }
78 }