1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package libcore.java.util;
18
19import java.util.DoubleSummaryStatistics;
20
21public class DoubleSummaryStatisticsTest extends junit.framework.TestCase {
22
23    private static final double data1[] = {22.4, -53.4, 74.8, -12.4, 17, 0, 100};
24    private static final double data2[] = {1.2, 3.5, 2.7, 1, 7.6};
25
26    public void test_empty() {
27        DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
28        assertEquals(0, dss.getCount());
29        assertEquals(0.0d, dss.getSum());
30        assertEquals(0.0d, dss.getAverage());
31        assertEquals(Double.POSITIVE_INFINITY, dss.getMin());
32        assertEquals(Double.NEGATIVE_INFINITY, dss.getMax());
33    }
34
35    public void test_accept() {
36        DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
37        dss.accept(100.5d);
38        assertEquals(1, dss.getCount());
39        assertEquals(100.5d, dss.getSum());
40        dss.accept(45.0d);
41        assertEquals(2, dss.getCount());
42        assertEquals(145.5d, dss.getSum());
43    }
44
45    public void test_combine() {
46        DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData2();
47        DoubleSummaryStatistics dssCombined = getDoubleSummaryStatisticsData1();
48        dssCombined.combine(dss1);
49        assertEquals(12, dssCombined.getCount());
50        assertEquals(164.4d, dssCombined.getSum());
51        assertEquals(100.0d, dssCombined.getMax());
52        assertEquals(-53.4d, dssCombined.getMin());
53        assertEquals(13.7, dssCombined.getAverage(), 1E-6);
54    }
55
56    public void test_getCount() {
57        DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
58        assertEquals(data1.length, dss1.getCount());
59    }
60
61    public void test_getSum() {
62        DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
63        assertEquals(148.4, dss1.getSum());
64
65        dss1.accept(Double.NaN);
66        assertEquals(Double.NaN, dss1.getSum());
67    }
68
69    public void test_getMin() {
70        DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
71        assertEquals(-53.4d, dss1.getMin());
72
73        dss1.accept(Double.NaN);
74        assertEquals(Double.NaN, dss1.getMin());
75    }
76
77    public void test_getMax() {
78        DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
79        assertEquals(100.0d, dss1.getMax());
80
81        dss1.accept(Double.NaN);
82        assertEquals(Double.NaN, dss1.getMax());
83    }
84
85    public void test_getAverage() {
86        DoubleSummaryStatistics dss1 = getDoubleSummaryStatisticsData1();
87        assertEquals(21.2, dss1.getAverage(), 1E-6);
88
89        dss1.accept(Double.NaN);
90        assertEquals(Double.NaN, dss1.getAverage());
91    }
92
93    private static DoubleSummaryStatistics getDoubleSummaryStatisticsData1() {
94        DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
95        for (double value : data1) {
96            dss.accept(value);
97        }
98        return dss;
99    }
100
101    private static DoubleSummaryStatistics getDoubleSummaryStatisticsData2() {
102        DoubleSummaryStatistics dss = new DoubleSummaryStatistics();
103        for (double value : data2) {
104            dss.accept(value);
105        }
106        return dss;
107    }
108}
109