1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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 }