001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive;
017:
018: import org.apache.commons.math.TestUtils;
019: import org.apache.commons.math.stat.descriptive.moment.SecondMoment;
020:
021: /**
022: * Test cases for {@link StorelessUnivariateStatistic} classes.
023: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
024: */
025: public abstract class StorelessUnivariateStatisticAbstractTest extends
026: UnivariateStatisticAbstractTest {
027:
028: public StorelessUnivariateStatisticAbstractTest(String name) {
029: super (name);
030: }
031:
032: /** Small sample arrays */
033: protected double[][] smallSamples = { {}, { 1 }, { 1, 2 },
034: { 1, 2, 3 }, { 1, 2, 3, 4 } };
035:
036: /** Return a new instance of the statistic */
037: public abstract UnivariateStatistic getUnivariateStatistic();
038:
039: /**Expected value for the testArray defined in UnivariateStatisticAbstractTest */
040: public abstract double expectedValue();
041:
042: /** Verify that calling increment() in a loop over testArray results in correct state */
043: public void testIncrementation() throws Exception {
044:
045: StorelessUnivariateStatistic statistic = (StorelessUnivariateStatistic) getUnivariateStatistic();
046:
047: statistic.clear();
048:
049: for (int i = 0; i < testArray.length; i++) {
050: statistic.increment(testArray[i]);
051: }
052:
053: assertEquals(expectedValue(), statistic.getResult(),
054: getTolerance());
055: assertEquals(testArray.length, statistic.getN());
056:
057: statistic.clear();
058:
059: assertTrue(Double.isNaN(statistic.getResult()));
060: assertEquals(0, statistic.getN());
061:
062: }
063:
064: public void testSerialization() throws Exception {
065:
066: StorelessUnivariateStatistic statistic = (StorelessUnivariateStatistic) getUnivariateStatistic();
067:
068: TestUtils.checkSerializedEquality(statistic);
069:
070: statistic.clear();
071:
072: for (int i = 0; i < testArray.length; i++) {
073: statistic.increment(testArray[i]);
074: if (i % 5 == 0)
075: statistic = (StorelessUnivariateStatistic) TestUtils
076: .serializeAndRecover(statistic);
077: }
078:
079: TestUtils.checkSerializedEquality(statistic);
080:
081: assertEquals(expectedValue(), statistic.getResult(),
082: getTolerance());
083:
084: statistic.clear();
085:
086: assertTrue(Double.isNaN(statistic.getResult()));
087:
088: }
089:
090: public void testEqualsAndHashCode() {
091: StorelessUnivariateStatistic statistic = (StorelessUnivariateStatistic) getUnivariateStatistic();
092: StorelessUnivariateStatistic statistic2 = null;
093:
094: assertTrue("non-null, compared to null", !statistic
095: .equals(statistic2));
096: assertTrue("reflexive, non-null", statistic.equals(statistic));
097:
098: int emptyHash = statistic.hashCode();
099: statistic2 = (StorelessUnivariateStatistic) getUnivariateStatistic();
100: assertTrue("empty stats should be equal", statistic
101: .equals(statistic2));
102: assertEquals("empty stats should have the same hashcode",
103: emptyHash, statistic2.hashCode());
104:
105: statistic.increment(1d);
106: assertTrue("reflexive, non-empty", statistic.equals(statistic));
107: assertTrue("non-empty, compared to empty", !statistic
108: .equals(statistic2));
109: assertTrue("non-empty, compared to empty", !statistic2
110: .equals(statistic));
111: assertTrue(
112: "non-empty stat should have different hashcode from empty stat",
113: statistic.hashCode() != emptyHash);
114:
115: statistic2.increment(1d);
116: assertTrue("stats with same data should be equal", statistic
117: .equals(statistic2));
118: assertEquals(
119: "stats with same data should have the same hashcode",
120: statistic.hashCode(), statistic2.hashCode());
121:
122: statistic.increment(Double.POSITIVE_INFINITY);
123: assertTrue("stats with different n's should not be equal",
124: !statistic2.equals(statistic));
125: assertTrue(
126: "stats with different n's should have different hashcodes",
127: statistic.hashCode() != statistic2.hashCode());
128:
129: statistic2.increment(Double.POSITIVE_INFINITY);
130: assertTrue("stats with same data should be equal", statistic
131: .equals(statistic2));
132: assertEquals(
133: "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
139: .equals(statistic2));
140: assertEquals(
141: "cleared stats should have thashcode of empty stat",
142: emptyHash, statistic2.hashCode());
143: assertEquals(
144: "cleared stats should have thashcode of empty stat",
145: emptyHash, statistic.hashCode());
146:
147: }
148:
149: public void testMomentSmallSamples() {
150: UnivariateStatistic stat = getUnivariateStatistic();
151: if (stat instanceof SecondMoment) {
152: SecondMoment moment = (SecondMoment) getUnivariateStatistic();
153: assertTrue(Double.isNaN(moment.getResult()));
154: moment.increment(1d);
155: assertEquals(0d, moment.getResult(), 0);
156: }
157: }
158:
159: /**
160: * Make sure that evaluate(double[]) and inrementAll(double[]),
161: * getResult() give same results.
162: */
163: public void testConsistency() {
164: StorelessUnivariateStatistic stat = (StorelessUnivariateStatistic) getUnivariateStatistic();
165: stat.incrementAll(testArray);
166: assertEquals(stat.getResult(), stat.evaluate(testArray),
167: getTolerance());
168: for (int i = 0; i < smallSamples.length; i++) {
169: stat.clear();
170: for (int j = 0; j < smallSamples[i].length; j++) {
171: stat.increment(smallSamples[i][j]);
172: }
173: TestUtils.assertEquals(stat.getResult(), stat
174: .evaluate(smallSamples[i]), getTolerance());
175: }
176: }
177:
178: }
|