Addressed exception "swallowing" in test coverage. From Phil's recent

email: "If the test case throws an unexpected exception, the test 
should fail.  ....[fix] these by changing the test method signature to 
throws Exception.  This also eliminates the need to import 
MathException everywhere."


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141242 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Tim O'Brien 2004-05-23 21:34:19 +00:00
parent 2ad68ba544
commit 6c07bd13a8
6 changed files with 85 additions and 163 deletions

View File

@ -16,12 +16,10 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.13 $ $Date: 2004/02/21 21:35:17 $
* @version $Revision: 1.14 $ $Date: 2004/05/23 21:34:19 $
*/
public class ChiSquareDistributionTest extends TestCase {
private ChiSquaredDistribution chiSquare;
@ -50,7 +48,7 @@ public class ChiSquareDistributionTest extends TestCase {
super.tearDown();
}
public void testLowerTailProbability(){
public void testLowerTailProbability() throws Exception {
testProbability( .210, .001);
testProbability( .554, .010);
testProbability( .831, .025);
@ -58,7 +56,7 @@ public class ChiSquareDistributionTest extends TestCase {
testProbability(1.610, .100);
}
public void testUpperTailProbability(){
public void testUpperTailProbability() throws Exception {
testProbability(20.515, .999);
testProbability(15.086, .990);
testProbability(12.833, .975);
@ -66,7 +64,7 @@ public class ChiSquareDistributionTest extends TestCase {
testProbability( 9.236, .900);
}
public void testLowerTailValues(){
public void testLowerTailValues() throws Exception {
testValue(.001, .210);
testValue(.010, .554);
testValue(.025, .831);
@ -74,7 +72,7 @@ public class ChiSquareDistributionTest extends TestCase {
testValue(.100, 1.610);
}
public void testUpperTailValues(){
public void testUpperTailValues() throws Exception {
testValue(.999, 20.515);
testValue(.990, 15.086);
testValue(.975, 12.833);
@ -82,23 +80,13 @@ public class ChiSquareDistributionTest extends TestCase {
testValue(.900, 9.236);
}
private void testProbability(double x, double expected){
try {
double actual = chiSquare.cumulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
private void testProbability(double x, double expected) throws Exception {
double actual = chiSquare.cumulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
}
private void testValue(double p, double expected){
try {
double actual = chiSquare.inverseCumulativeProbability(p);
assertEquals("value for " + p, expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
private void testValue(double p, double expected) throws Exception {
double actual = chiSquare.inverseCumulativeProbability(p);
assertEquals("value for " + p, expected, actual, 10e-4);
}
}

View File

@ -15,13 +15,12 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import org.apache.commons.math.TestUtils;
import junit.framework.TestCase;
/**
* @version $Revision: 1.13 $ $Date: 2004/05/23 00:33:40 $
* @version $Revision: 1.14 $ $Date: 2004/05/23 21:34:19 $
*/
public class ExponentialDistributionTest extends TestCase {
private ExponentialDistribution exp;
@ -52,135 +51,122 @@ public class ExponentialDistributionTest extends TestCase {
super.tearDown();
}
public void testInverseCumulativeProbability001() {
public void testInverseCumulativeProbability001() throws Exception {
testValue(.005003, .001);
}
public void testInverseCumulativeProbability010() {
public void testInverseCumulativeProbability010() throws Exception {
testValue(0.050252, .010);
}
public void testInverseCumulativeProbability025() {
public void testInverseCumulativeProbability025() throws Exception {
testValue(0.126589, .025);
}
public void testInverseCumulativeProbability050() {
public void testInverseCumulativeProbability050() throws Exception {
testValue(0.256566, .050);
}
public void testInverseCumulativeProbability100() {
public void testInverseCumulativeProbability100() throws Exception {
testValue(0.526803, .100);
}
public void testInverseCumulativeProbability999() {
public void testInverseCumulativeProbability999() throws Exception {
testValue(34.5388, .999);
}
public void testInverseCumulativeProbability990() {
public void testInverseCumulativeProbability990() throws Exception {
testValue(23.0259, .990);
}
public void testInverseCumulativeProbability975() {
public void testInverseCumulativeProbability975() throws Exception {
testValue(18.4444, .975);
}
public void testInverseCumulativeProbability950() {
public void testInverseCumulativeProbability950() throws Exception {
testValue(14.9787, .950);
}
public void testInverseCumulativeProbability900() {
public void testInverseCumulativeProbability900() throws Exception {
testValue(11.5129, .900);
}
public void testCumulativeProbability001() {
public void testCumulativeProbability001() throws Exception {
testProbability(0.005003, .001);
}
public void testCumulativeProbability010() {
public void testCumulativeProbability010() throws Exception {
testProbability(0.050252, .010);
}
public void testCumulativeProbability025() {
public void testCumulativeProbability025() throws Exception {
testProbability(0.126589, .025);
}
public void testCumulativeProbability050() {
public void testCumulativeProbability050() throws Exception {
testProbability(0.256566, .050);
}
public void testCumulativeProbability100() {
public void testCumulativeProbability100() throws Exception {
testProbability(0.526803, .100);
}
public void testCumulativeProbability999() {
public void testCumulativeProbability999() throws Exception {
testProbability(34.5388, .999);
}
public void testCumulativeProbability990() {
public void testCumulativeProbability990() throws Exception {
testProbability(23.0259, .990);
}
public void testCumulativeProbability975() {
public void testCumulativeProbability975() throws Exception {
testProbability(18.4444, .975);
}
public void testCumulativeProbability950() {
public void testCumulativeProbability950() throws Exception {
testProbability(14.9787, .950);
}
public void testCumulativeProbability900() {
public void testCumulativeProbability900() throws Exception {
testProbability(11.5129, .900);
}
public void testCumulativeProbabilityNegative() {
public void testCumulativeProbabilityNegative() throws Exception {
testProbability(-1.0, 0.0);
}
public void testCumulativeProbabilityZero() {
public void testCumulativeProbabilityZero() throws Exception {
testProbability(0.0, 0.0);
}
public void testInverseCumulativeProbabilityNegative() {
public void testInverseCumulativeProbabilityNegative() throws Exception {
testValue(Double.NaN, -1.0);
}
public void testInverseCumulativeProbabilityZero() {
public void testInverseCumulativeProbabilityZero() throws Exception {
testValue(0.0, 0.0);
}
public void testInverseCumulativeProbabilityOne() {
public void testInverseCumulativeProbabilityOne() throws Exception {
testValue(Double.POSITIVE_INFINITY, 1.0);
}
public void testInverseCumulativeProbabilityPositive() {
public void testInverseCumulativeProbabilityPositive() throws Exception {
testValue(Double.NaN, 2.0);
}
public void testCumulativeProbability2() {
try {
double actual = exp.cumulativeProbability(0.25, 0.75);
assertEquals(0.0905214, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
public void testCumulativeProbability2() throws Exception {
double actual = exp.cumulativeProbability(0.25, 0.75);
assertEquals(0.0905214, actual, 10e-4);
}
private void testProbability(double x, double expected) {
try {
double actual = exp.cumulativeProbability(x);
TestUtils.assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
private void testProbability(double x, double expected) throws Exception {
double actual = exp.cumulativeProbability(x);
TestUtils.assertEquals(expected, actual, 10e-4);
}
private void testValue(double expected, double p) {
try {
double actual = exp.inverseCumulativeProbability(p);
TestUtils.assertEquals(expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
private void testValue(double expected, double p) throws Exception {
double actual = exp.inverseCumulativeProbability(p);
TestUtils.assertEquals(expected, actual, 10e-4);
}
}

View File

@ -15,12 +15,10 @@
*/
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.12 $ $Date: 2004/05/23 00:33:40 $
* @version $Revision: 1.13 $ $Date: 2004/05/23 21:34:19 $
*/
public class FDistributionTest extends TestCase {
private FDistribution f;
@ -49,49 +47,41 @@ public class FDistributionTest extends TestCase {
super.tearDown();
}
public void testLowerTailProbability() {
public void testLowerTailProbability() throws Exception {
testProbability(1.0 / 10.67, .010);
testProbability(1.0 / 6.98, .025);
testProbability(1.0 / 4.95, .050);
testProbability(1.0 / 3.40, .100);
}
public void testUpperTailProbability() {
public void testUpperTailProbability() throws Exception {
testProbability(8.75, .990);
testProbability(5.99, .975);
testProbability(4.39, .950);
testProbability(3.11, .900);
}
public void testLowerTailValues() {
public void testLowerTailValues() throws Exception {
testValue(1.0 / 10.67, .010);
testValue(1.0 / 6.98, .025);
testValue(1.0 / 4.95, .050);
testValue(1.0 / 3.40, .100);
}
public void testUpperTailValues() {
public void testUpperTailValues() throws Exception {
testValue(8.75, .990);
testValue(5.99, .975);
testValue(4.39, .950);
testValue(3.11, .900);
}
private void testProbability(double x, double expected) {
try {
double actual = f.cumulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 1e-3);
} catch (MathException e) {
e.printStackTrace();
}
private void testProbability(double x, double expected) throws Exception {
double actual = f.cumulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 1e-3);
}
private void testValue(double expected, double p) {
try {
double actual = f.inverseCumulativeProbability(p);
assertEquals("value for " + p, expected, actual, 1e-2);
} catch (MathException e) {
e.printStackTrace();
}
private void testValue(double expected, double p) throws Exception {
double actual = f.inverseCumulativeProbability(p);
assertEquals("value for " + p, expected, actual, 1e-2);
}
}

View File

@ -16,15 +16,13 @@
package org.apache.commons.math.distribution;
import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.15 $ $Date: 2004/05/23 00:33:40 $
* @version $Revision: 1.16 $ $Date: 2004/05/23 21:34:19 $
*/
public class GammaDistributionTest extends TestCase {
public void testProbabilities() {
public void testProbabilities() throws Exception {
testProbability(-1.000, 4.0, 2.0, .0000);
testProbability(15.501, 4.0, 2.0, .9499);
testProbability(0.504, 4.0, 1.0, .0018);
@ -32,40 +30,24 @@ public class GammaDistributionTest extends TestCase {
testProbability(5.000, 2.0, 2.0, .7127);
}
public void testValues() {
public void testValues() throws Exception {
testValue(15.501, 4.0, 2.0, .9499);
testValue(0.504, 4.0, 1.0, .0018);
testValue(10.011, 1.0, 2.0, .9933);
testValue(5.000, 2.0, 2.0, .7127);
}
private void testProbability(
double x,
double a,
double b,
double expected) {
try {
double actual =
DistributionFactory
.newInstance()
.createGammaDistribution(a, b)
.cumulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
private void testProbability(double x, double a, double b, double expected) throws Exception {
DistributionFactory factory = DistributionFactory.newInstance();
GammaDistribution distribution = factory.createGammaDistribution( a, b );
double actual = distribution.cumulativeProbability(x);
assertEquals("probability for " + x, expected, actual, 10e-4);
}
private void testValue(double expected, double a, double b, double p) {
try {
double actual =
DistributionFactory
.newInstance()
.createGammaDistribution(a, b)
.inverseCumulativeProbability(p);
assertEquals("critical value for " + p, expected, actual, 10e-4);
} catch (MathException e) {
e.printStackTrace();
}
private void testValue(double expected, double a, double b, double p) throws Exception {
DistributionFactory factory = DistributionFactory.newInstance();
GammaDistribution distribution = factory.createGammaDistribution( a, b );
double actual = distribution.inverseCumulativeProbability(p);
assertEquals("critical value for " + p, expected, actual, 10e-4);
}
}

View File

@ -22,7 +22,7 @@ import org.apache.commons.math.TestUtils;
import junit.framework.TestCase;
/**
* @version $Revision: 1.10 $ $Date: 2004/05/23 00:33:41 $
* @version $Revision: 1.11 $ $Date: 2004/05/23 21:34:19 $
*/
public class BeanTransformerTest extends TestCase {
@ -70,30 +70,22 @@ public class BeanTransformerTest extends TestCase {
/**
*
*/
public void testTransform() {
public void testTransform() throws Exception {
BeanTransformer b = new BeanTransformer("x");
TestBean target = new TestBean();
double value = Double.NaN;
try {
value = b.transform(target);
} catch (MathException e) {
e.printStackTrace();
}
value = b.transform(target);
TestUtils.assertEquals(1.0, value, 1.0e-2);
}
/**
*
*/
public void testTransformInvalidType(){
public void testTransformInvalidType() throws Exception {
BeanTransformer b = new BeanTransformer("y");
TestBean target = new TestBean();
try {
try {
b.transform(target);
} catch (MathException e) {
e.printStackTrace();
}
b.transform(target);
fail("Expecting ClassCastException");
} catch(ClassCastException ex){
// success

View File

@ -22,21 +22,17 @@ import org.apache.commons.math.MathException;
import junit.framework.TestCase;
/**
* @version $Revision: 1.11 $ $Date: 2004/05/23 00:33:41 $
* @version $Revision: 1.12 $ $Date: 2004/05/23 21:34:19 $
*/
public class DefaultTransformerTest extends TestCase {
/**
*
*/
public void testTransformDouble(){
public void testTransformDouble() throws Exception {
double expected = 1.0;
Double input = new Double(expected);
DefaultTransformer t = new DefaultTransformer();
try {
assertEquals(expected, t.transform(input), 1.0e-4);
} catch (MathException e) {
e.printStackTrace();
}
assertEquals(expected, t.transform(input), 1.0e-4);
}
/**
@ -55,43 +51,31 @@ public class DefaultTransformerTest extends TestCase {
/**
*
*/
public void testTransformInteger(){
public void testTransformInteger() throws Exception {
double expected = 1.0;
Integer input = new Integer(1);
DefaultTransformer t = new DefaultTransformer();
try {
assertEquals(expected, t.transform(input), 1.0e-4);
} catch (MathException e) {
e.printStackTrace();
}
assertEquals(expected, t.transform(input), 1.0e-4);
}
/**
*
*/
public void testTransformBigDecimal(){
public void testTransformBigDecimal() throws Exception {
double expected = 1.0;
BigDecimal input = new BigDecimal("1.0");
DefaultTransformer t = new DefaultTransformer();
try {
assertEquals(expected, t.transform(input), 1.0e-4);
} catch (MathException e) {
e.printStackTrace();
}
assertEquals(expected, t.transform(input), 1.0e-4);
}
/**
*
*/
public void testTransformString(){
public void testTransformString() throws Exception {
double expected = 1.0;
String input = "1.0";
DefaultTransformer t = new DefaultTransformer();
try {
assertEquals(expected, t.transform(input), 1.0e-4);
} catch (MathException e) {
e.printStackTrace();
}
assertEquals(expected, t.transform(input), 1.0e-4);
}
/**