improved test coverage
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@797742 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
648a02f0b4
commit
00589c9975
|
@ -13,6 +13,8 @@
|
|||
*/
|
||||
package org.apache.commons.math.stat.descriptive;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
@ -42,7 +44,7 @@ public class DescriptiveStatisticsTest extends TestCase {
|
|||
return new DescriptiveStatistics();
|
||||
}
|
||||
|
||||
public void testSetterInjection() throws Exception {
|
||||
public void testSetterInjection() {
|
||||
DescriptiveStatistics stats = createDescriptiveStatistics();
|
||||
stats.addValue(1);
|
||||
stats.addValue(3);
|
||||
|
@ -52,6 +54,114 @@ public class DescriptiveStatisticsTest extends TestCase {
|
|||
assertEquals(42, stats.getMean(), 1E-10);
|
||||
}
|
||||
|
||||
public void testCopy() {
|
||||
DescriptiveStatistics stats = createDescriptiveStatistics();
|
||||
stats.addValue(1);
|
||||
stats.addValue(3);
|
||||
DescriptiveStatistics copy = new DescriptiveStatistics(stats);
|
||||
assertEquals(2, copy.getMean(), 1E-10);
|
||||
// Now lets try some new math
|
||||
stats.setMeanImpl(new deepMean());
|
||||
copy = stats.copy();
|
||||
assertEquals(42, copy.getMean(), 1E-10);
|
||||
}
|
||||
|
||||
public void testWindowSize() {
|
||||
DescriptiveStatistics stats = createDescriptiveStatistics();
|
||||
stats.setWindowSize(300);
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
stats.addValue(i + 1);
|
||||
}
|
||||
int refSum = (100 * 101) / 2;
|
||||
assertEquals(refSum / 100.0, stats.getMean(), 1E-10);
|
||||
assertEquals(300, stats.getWindowSize());
|
||||
try {
|
||||
stats.setWindowSize(-3);
|
||||
fail("an exception should have been thrown");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
// expected
|
||||
} catch (Exception e) {
|
||||
fail("wrong exception caught: " + e.getMessage());
|
||||
}
|
||||
assertEquals(300, stats.getWindowSize());
|
||||
stats.setWindowSize(50);
|
||||
assertEquals(50, stats.getWindowSize());
|
||||
int refSum2 = refSum - (50 * 51) / 2;
|
||||
assertEquals(refSum2 / 50.0, stats.getMean(), 1E-10);
|
||||
}
|
||||
|
||||
public void testGetValues() {
|
||||
DescriptiveStatistics stats = createDescriptiveStatistics();
|
||||
for (int i = 100; i > 0; --i) {
|
||||
stats.addValue(i);
|
||||
}
|
||||
int refSum = (100 * 101) / 2;
|
||||
assertEquals(refSum / 100.0, stats.getMean(), 1E-10);
|
||||
double[] v = stats.getValues();
|
||||
for (int i = 0; i < v.length; ++i) {
|
||||
assertEquals(100.0 - i, v[i], 1.0e-10);
|
||||
}
|
||||
double[] s = stats.getSortedValues();
|
||||
for (int i = 0; i < s.length; ++i) {
|
||||
assertEquals(i + 1.0, s[i], 1.0e-10);
|
||||
}
|
||||
assertEquals(12.0, stats.getElement(88), 1.0e-10);
|
||||
}
|
||||
|
||||
public void testToString() {
|
||||
DescriptiveStatistics stats = createDescriptiveStatistics();
|
||||
stats.addValue(1);
|
||||
stats.addValue(2);
|
||||
stats.addValue(3);
|
||||
Locale d = Locale.getDefault();
|
||||
Locale.setDefault(Locale.US);
|
||||
assertEquals("DescriptiveStatistics:\n" +
|
||||
"n: 3\n" +
|
||||
"min: 1.0\n" +
|
||||
"max: 3.0\n" +
|
||||
"mean: 2.0\n" +
|
||||
"std dev: 1.0\n" +
|
||||
"median: 2.0\n" +
|
||||
"skewness: 0.0\n" +
|
||||
"kurtosis: NaN\n", stats.toString());
|
||||
Locale.setDefault(d);
|
||||
}
|
||||
|
||||
public void testShuffledStatistics() {
|
||||
// the purpose of this test is only to check the get/set methods
|
||||
// we are aware shuffling statistics like this is really not
|
||||
// something sensible to do in production ...
|
||||
DescriptiveStatistics reference = createDescriptiveStatistics();
|
||||
DescriptiveStatistics shuffled = createDescriptiveStatistics();
|
||||
|
||||
UnivariateStatistic tmp = shuffled.getGeometricMeanImpl();
|
||||
shuffled.setGeometricMeanImpl(shuffled.getMeanImpl());
|
||||
shuffled.setMeanImpl(shuffled.getKurtosisImpl());
|
||||
shuffled.setKurtosisImpl(shuffled.getSkewnessImpl());
|
||||
shuffled.setSkewnessImpl(shuffled.getVarianceImpl());
|
||||
shuffled.setVarianceImpl(shuffled.getMaxImpl());
|
||||
shuffled.setMaxImpl(shuffled.getMinImpl());
|
||||
shuffled.setMinImpl(shuffled.getSumImpl());
|
||||
shuffled.setSumImpl(shuffled.getSumsqImpl());
|
||||
shuffled.setSumsqImpl(tmp);
|
||||
|
||||
for (int i = 100; i > 0; --i) {
|
||||
reference.addValue(i);
|
||||
shuffled.addValue(i);
|
||||
}
|
||||
|
||||
assertEquals(reference.getMean(), shuffled.getGeometricMean(), 1.0e-10);
|
||||
assertEquals(reference.getKurtosis(), shuffled.getMean(), 1.0e-10);
|
||||
assertEquals(reference.getSkewness(), shuffled.getKurtosis(), 1.0e-10);
|
||||
assertEquals(reference.getVariance(), shuffled.getSkewness(), 1.0e-10);
|
||||
assertEquals(reference.getMax(), shuffled.getVariance(), 1.0e-10);
|
||||
assertEquals(reference.getMin(), shuffled.getMax(), 1.0e-10);
|
||||
assertEquals(reference.getSum(), shuffled.getMin(), 1.0e-10);
|
||||
assertEquals(reference.getSumsq(), shuffled.getSum(), 1.0e-10);
|
||||
assertEquals(reference.getGeometricMean(), shuffled.getSumsq(), 1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
public void testPercentileSetter() throws Exception {
|
||||
DescriptiveStatistics stats = createDescriptiveStatistics();
|
||||
stats.addValue(1);
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
package org.apache.commons.math.stat.descriptive;
|
||||
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
@ -69,6 +71,7 @@ public class MultivariateSummaryStatisticsTest extends TestCase {
|
|||
u.addValue(new double[] { 3, 4 });
|
||||
assertEquals(2, u.getMean()[0], 1E-14);
|
||||
assertEquals(3, u.getMean()[1], 1E-14);
|
||||
assertEquals(2, u.getDimension());
|
||||
}
|
||||
|
||||
public void testSetterIllegalState() throws Exception {
|
||||
|
@ -84,6 +87,58 @@ public class MultivariateSummaryStatisticsTest extends TestCase {
|
|||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public void testToString() throws DimensionMismatchException {
|
||||
MultivariateSummaryStatistics stats = createMultivariateSummaryStatistics(2, true);
|
||||
stats.addValue(new double[] {1, 3});
|
||||
stats.addValue(new double[] {2, 2});
|
||||
stats.addValue(new double[] {3, 1});
|
||||
Locale d = Locale.getDefault();
|
||||
Locale.setDefault(Locale.US);
|
||||
assertEquals("MultivariateSummaryStatistics:\n" +
|
||||
"n: 3\n" +
|
||||
"min: 1.0, 1.0\n" +
|
||||
"max: 3.0, 3.0\n" +
|
||||
"mean: 2.0, 2.0\n" +
|
||||
"geometric mean: 1.8171205928321394, 1.8171205928321394\n" +
|
||||
"sum of squares: 14.0, 14.0\n" +
|
||||
"sum of logarithms: 1.791759469228055, 1.791759469228055\n" +
|
||||
"standard deviation: 1.0, 1.0\n" +
|
||||
"covariance: Array2DRowRealMatrix{{1.0,-1.0},{-1.0,1.0}}\n",
|
||||
stats.toString());
|
||||
Locale.setDefault(d);
|
||||
}
|
||||
|
||||
public void testShuffledStatistics() throws DimensionMismatchException {
|
||||
// the purpose of this test is only to check the get/set methods
|
||||
// we are aware shuffling statistics like this is really not
|
||||
// something sensible to do in production ...
|
||||
MultivariateSummaryStatistics reference = createMultivariateSummaryStatistics(2, true);
|
||||
MultivariateSummaryStatistics shuffled = createMultivariateSummaryStatistics(2, true);
|
||||
|
||||
StorelessUnivariateStatistic[] tmp = shuffled.getGeoMeanImpl();
|
||||
shuffled.setGeoMeanImpl(shuffled.getMeanImpl());
|
||||
shuffled.setMeanImpl(shuffled.getMaxImpl());
|
||||
shuffled.setMaxImpl(shuffled.getMinImpl());
|
||||
shuffled.setMinImpl(shuffled.getSumImpl());
|
||||
shuffled.setSumImpl(shuffled.getSumsqImpl());
|
||||
shuffled.setSumsqImpl(shuffled.getSumLogImpl());
|
||||
shuffled.setSumLogImpl(tmp);
|
||||
|
||||
for (int i = 100; i > 0; --i) {
|
||||
reference.addValue(new double[] {i, i});
|
||||
shuffled.addValue(new double[] {i, i});
|
||||
}
|
||||
|
||||
TestUtils.assertEquals(reference.getMean(), shuffled.getGeometricMean(), 1.0e-10);
|
||||
TestUtils.assertEquals(reference.getMax(), shuffled.getMean(), 1.0e-10);
|
||||
TestUtils.assertEquals(reference.getMin(), shuffled.getMax(), 1.0e-10);
|
||||
TestUtils.assertEquals(reference.getSum(), shuffled.getMin(), 1.0e-10);
|
||||
TestUtils.assertEquals(reference.getSumSq(), shuffled.getSum(), 1.0e-10);
|
||||
TestUtils.assertEquals(reference.getSumLog(), shuffled.getSumSq(), 1.0e-10);
|
||||
TestUtils.assertEquals(reference.getGeometricMean(), shuffled.getSumLog(), 1.0e-10);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Bogus mean implementation to test setter injection.
|
||||
|
|
Loading…
Reference in New Issue