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