1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math.stat.descriptive;
18
19 import org.apache.commons.math.TestUtils;
20 import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
21
22
23
24
25
26 public abstract class StorelessUnivariateStatisticAbstractTest
27 extends UnivariateStatisticAbstractTest {
28
29 public StorelessUnivariateStatisticAbstractTest(String name) {
30 super(name);
31 }
32
33
34 protected double[][] smallSamples = {{}, {1}, {1,2}, {1,2,3}, {1,2,3,4}};
35
36
37 @Override
38 public abstract UnivariateStatistic getUnivariateStatistic();
39
40
41 @Override
42 public abstract double expectedValue();
43
44
45
46
47 public void testIncrementation() throws Exception {
48
49 StorelessUnivariateStatistic statistic =
50 (StorelessUnivariateStatistic) getUnivariateStatistic();
51
52
53 for (int i = 0; i < testArray.length; i++) {
54 statistic.increment(testArray[i]);
55 }
56
57 assertEquals(expectedValue(), statistic.getResult(), getTolerance());
58 assertEquals(testArray.length, statistic.getN());
59
60 statistic.clear();
61
62
63 statistic.incrementAll(testArray);
64 assertEquals(expectedValue(), statistic.getResult(), getTolerance());
65 assertEquals(testArray.length, statistic.getN());
66
67 statistic.clear();
68
69
70 assertTrue(Double.isNaN(statistic.getResult()));
71 assertEquals(0, statistic.getN());
72
73 }
74
75 public void testSerialization() throws Exception {
76
77 StorelessUnivariateStatistic statistic =
78 (StorelessUnivariateStatistic) getUnivariateStatistic();
79
80 TestUtils.checkSerializedEquality(statistic);
81
82 statistic.clear();
83
84 for (int i = 0; i < testArray.length; i++) {
85 statistic.increment(testArray[i]);
86 if(i % 5 == 0)
87 statistic = (StorelessUnivariateStatistic)TestUtils.serializeAndRecover(statistic);
88 }
89
90 TestUtils.checkSerializedEquality(statistic);
91
92 assertEquals(expectedValue(), statistic.getResult(), getTolerance());
93
94 statistic.clear();
95
96 assertTrue(Double.isNaN(statistic.getResult()));
97
98 }
99
100 public void testEqualsAndHashCode() {
101 StorelessUnivariateStatistic statistic =
102 (StorelessUnivariateStatistic) getUnivariateStatistic();
103 StorelessUnivariateStatistic statistic2 = null;
104
105 assertTrue("non-null, compared to null", !statistic.equals(statistic2));
106 assertTrue("reflexive, non-null", statistic.equals(statistic));
107
108 int emptyHash = statistic.hashCode();
109 statistic2 = (StorelessUnivariateStatistic) getUnivariateStatistic();
110 assertTrue("empty stats should be equal", statistic.equals(statistic2));
111 assertEquals("empty stats should have the same hashcode",
112 emptyHash, statistic2.hashCode());
113
114 statistic.increment(1d);
115 assertTrue("reflexive, non-empty", statistic.equals(statistic));
116 assertTrue("non-empty, compared to empty", !statistic.equals(statistic2));
117 assertTrue("non-empty, compared to empty", !statistic2.equals(statistic));
118 assertTrue("non-empty stat should have different hashcode from empty stat",
119 statistic.hashCode() != emptyHash);
120
121 statistic2.increment(1d);
122 assertTrue("stats with same data should be equal", statistic.equals(statistic2));
123 assertEquals("stats with same data should have the same hashcode",
124 statistic.hashCode(), statistic2.hashCode());
125
126 statistic.increment(Double.POSITIVE_INFINITY);
127 assertTrue("stats with different n's should not be equal", !statistic2.equals(statistic));
128 assertTrue("stats with different n's should have different hashcodes",
129 statistic.hashCode() != statistic2.hashCode());
130
131 statistic2.increment(Double.POSITIVE_INFINITY);
132 assertTrue("stats with same data should be equal", statistic.equals(statistic2));
133 assertEquals("stats with same data should have the same hashcode",
134 statistic.hashCode(), statistic2.hashCode());
135
136 statistic.clear();
137 statistic2.clear();
138 assertTrue("cleared stats should be equal", statistic.equals(statistic2));
139 assertEquals("cleared stats should have thashcode of empty stat",
140 emptyHash, statistic2.hashCode());
141 assertEquals("cleared stats should have thashcode of empty stat",
142 emptyHash, statistic.hashCode());
143
144 }
145
146 public void testMomentSmallSamples() {
147 UnivariateStatistic stat = getUnivariateStatistic();
148 if (stat instanceof SecondMoment) {
149 SecondMoment moment = (SecondMoment) getUnivariateStatistic();
150 assertTrue(Double.isNaN(moment.getResult()));
151 moment.increment(1d);
152 assertEquals(0d, moment.getResult(), 0);
153 }
154 }
155
156
157
158
159
160 public void testConsistency() {
161 StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();
162 stat.incrementAll(testArray);
163 assertEquals(stat.getResult(), stat.evaluate(testArray), getTolerance());
164 for (int i = 0; i < smallSamples.length; i++) {
165 stat.clear();
166 for (int j =0; j < smallSamples[i].length; j++) {
167 stat.increment(smallSamples[i][j]);
168 }
169 TestUtils.assertEquals(stat.getResult(), stat.evaluate(smallSamples[i]), getTolerance());
170 }
171 }
172
173
174
175
176
177
178 public void testCopyConsistency() {
179
180 StorelessUnivariateStatistic master =
181 (StorelessUnivariateStatistic) getUnivariateStatistic();
182
183 StorelessUnivariateStatistic replica = null;
184
185
186 long index = Math.round((Math.random()) * testArray.length);
187
188
189 master.incrementAll(testArray, 0, (int) index);
190 replica = master.copy();
191
192
193 assertTrue(replica.equals(master));
194 assertTrue(master.equals(replica));
195
196
197 master.incrementAll(testArray,
198 (int) index, (int) (testArray.length - index));
199 replica.incrementAll(testArray,
200 (int) index, (int) (testArray.length - index));
201 assertTrue(replica.equals(master));
202 assertTrue(master.equals(replica));
203 }
204
205 public void testSerial() {
206 StorelessUnivariateStatistic s =
207 (StorelessUnivariateStatistic) getUnivariateStatistic();
208 assertEquals(s, TestUtils.serializeAndRecover(s));
209 }
210 }