View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.stat.descriptive;
23  
24  import org.hipparchus.UnitTestUtils;
25  import org.junit.jupiter.api.Test;
26  
27  import java.util.Locale;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertNotEquals;
32  
33  /**
34   * Test cases for the {@link StatisticalSummaryValues} class.
35   */
36  final class StatisticalSummaryValuesTest {
37  
38      @Test
39      void testSerialization() {
40          StatisticalSummaryValues u = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
41          UnitTestUtils.checkSerializedEquality(u);
42          StatisticalSummaryValues t = (StatisticalSummaryValues) UnitTestUtils.serializeAndRecover(u);
43          verifyEquality(u, t);
44      }
45  
46      @SuppressWarnings("unlikely-arg-type")
47      @Test
48      void testEqualsAndHashCode() {
49          StatisticalSummaryValues u  = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
50          StatisticalSummaryValues t = null;
51          assertEquals(u, u, "reflexive");
52          assertNotEquals(u, t, "non-null compared to null");
53          assertNotEquals(u, Double.valueOf(0), "wrong type");
54          t = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
55          assertEquals(t, u, "instances with same data should be equal");
56          assertEquals(u.hashCode(), t.hashCode(), "hash code");
57  
58          u = new StatisticalSummaryValues(Double.NaN, 2, 3, 4, 5, 6);
59          t = new StatisticalSummaryValues(1, Double.NaN, 3, 4, 5, 6);
60          assertFalse((u.equals(t) ||t.equals(u)),
61                  "instances based on different data should be different");
62      }
63  
64      private void verifyEquality(StatisticalSummaryValues s, StatisticalSummaryValues u) {
65          assertEquals(s.getN(),u.getN(),"N");
66          UnitTestUtils.customAssertEquals("sum", s.getSum(), u.getSum(), 0);
67          UnitTestUtils.customAssertEquals("var", s.getVariance(), u.getVariance(), 0);
68          UnitTestUtils.customAssertEquals("std", s.getStandardDeviation(), u.getStandardDeviation(), 0);
69          UnitTestUtils.customAssertEquals("mean", s.getMean(), u.getMean(), 0);
70          UnitTestUtils.customAssertEquals("min", s.getMin(), u.getMin(), 0);
71          UnitTestUtils.customAssertEquals("max", s.getMax(), u.getMax(), 0);
72      }
73  
74      @Test
75      void testToString() {
76          StatisticalSummaryValues u  = new StatisticalSummaryValues(4.5, 16, 10, 5, 4, 45);
77          Locale d = Locale.getDefault();
78          Locale.setDefault(Locale.US);
79          assertEquals("StatisticalSummaryValues:\n" +
80                       "n: 10\n" +
81                       "min: 4.0\n" +
82                       "max: 5.0\n" +
83                       "mean: 4.5\n" +
84                       "std dev: 4.0\n" +
85                       "variance: 16.0\n" +
86                       "sum: 45.0\n",  u.toString());
87          Locale.setDefault(d);
88      }
89  }