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 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
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 }