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 java.util.Locale;
25  
26  import org.hipparchus.UnitTestUtils;
27  import org.junit.Assert;
28  import org.junit.Test;
29  
30  /**
31   * Test cases for the {@link StatisticalSummaryValues} class.
32   */
33  public final class StatisticalSummaryValuesTest {
34  
35      @Test
36      public void testSerialization() {
37          StatisticalSummaryValues u = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
38          UnitTestUtils.checkSerializedEquality(u);
39          StatisticalSummaryValues t = (StatisticalSummaryValues) UnitTestUtils.serializeAndRecover(u);
40          verifyEquality(u, t);
41      }
42  
43      @SuppressWarnings("unlikely-arg-type")
44      @Test
45      public void testEqualsAndHashCode() {
46          StatisticalSummaryValues u  = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
47          StatisticalSummaryValues t = null;
48          Assert.assertTrue("reflexive", u.equals(u));
49          Assert.assertFalse("non-null compared to null", u.equals(t));
50          Assert.assertFalse("wrong type", u.equals(Double.valueOf(0)));
51          t = new StatisticalSummaryValues(1, 2, 3, 4, 5, 6);
52          Assert.assertTrue("instances with same data should be equal", t.equals(u));
53          Assert.assertEquals("hash code", u.hashCode(), t.hashCode());
54  
55          u = new StatisticalSummaryValues(Double.NaN, 2, 3, 4, 5, 6);
56          t = new StatisticalSummaryValues(1, Double.NaN, 3, 4, 5, 6);
57          Assert.assertFalse("instances based on different data should be different",
58                  (u.equals(t) ||t.equals(u)));
59      }
60  
61      private void verifyEquality(StatisticalSummaryValues s, StatisticalSummaryValues u) {
62          Assert.assertEquals("N",s.getN(),u.getN());
63          UnitTestUtils.assertEquals("sum",s.getSum(),u.getSum(), 0);
64          UnitTestUtils.assertEquals("var",s.getVariance(),u.getVariance(), 0);
65          UnitTestUtils.assertEquals("std",s.getStandardDeviation(),u.getStandardDeviation(), 0);
66          UnitTestUtils.assertEquals("mean",s.getMean(),u.getMean(), 0);
67          UnitTestUtils.assertEquals("min",s.getMin(),u.getMin(), 0);
68          UnitTestUtils.assertEquals("max",s.getMax(),u.getMax(), 0);
69      }
70  
71      @Test
72      public void testToString() {
73          StatisticalSummaryValues u  = new StatisticalSummaryValues(4.5, 16, 10, 5, 4, 45);
74          Locale d = Locale.getDefault();
75          Locale.setDefault(Locale.US);
76          Assert.assertEquals("StatisticalSummaryValues:\n" +
77                       "n: 10\n" +
78                       "min: 4.0\n" +
79                       "max: 5.0\n" +
80                       "mean: 4.5\n" +
81                       "std dev: 4.0\n" +
82                       "variance: 16.0\n" +
83                       "sum: 45.0\n",  u.toString());
84          Locale.setDefault(d);
85      }
86  }