improved test coverage

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@592093 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2007-11-05 17:11:29 +00:00
parent 8d7e160b5a
commit e21865e806
2 changed files with 80 additions and 30 deletions

View File

@ -445,21 +445,39 @@ public class GaussNewtonEstimatorTest
} }
public void testMaxIterations() {
Circle circle = new Circle(98.680, 47.345);
circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0);
circle.addPoint(110.0, -20.0);
circle.addPoint( 35.0, 15.0);
circle.addPoint( 45.0, 97.0);
try {
GaussNewtonEstimator estimator = new GaussNewtonEstimator(4, 1.0e-14, 1.0e-14);
estimator.estimate(circle);
fail("an exception should have been caught");
} catch (EstimationException ee) {
// expected behavior
} catch (Exception e) {
fail("wrong exception type caught");
}
}
public void testCircleFitting() throws EstimationException { public void testCircleFitting() throws EstimationException {
Circle circle = new Circle(98.680, 47.345); Circle circle = new Circle(98.680, 47.345);
circle.addPoint( 30.0, 68.0); circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0); circle.addPoint( 50.0, -6.0);
circle.addPoint(110.0, -20.0); circle.addPoint(110.0, -20.0);
circle.addPoint( 35.0, 15.0); circle.addPoint( 35.0, 15.0);
circle.addPoint( 45.0, 97.0); circle.addPoint( 45.0, 97.0);
GaussNewtonEstimator estimator = new GaussNewtonEstimator(100, 1.0e-10, 1.0e-10); GaussNewtonEstimator estimator = new GaussNewtonEstimator(100, 1.0e-10, 1.0e-10);
estimator.estimate(circle); estimator.estimate(circle);
double rms = estimator.getRMS(circle); double rms = estimator.getRMS(circle);
assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10); assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10);
assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10); assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10);
assertEquals(96.07590211815305, circle.getX(), 1.0e-10); assertEquals(96.07590211815305, circle.getX(), 1.0e-10);
assertEquals(48.13516790438953, circle.getY(), 1.0e-10); assertEquals(48.13516790438953, circle.getY(), 1.0e-10);
} }
public void testCircleFittingBadInit() throws EstimationException { public void testCircleFittingBadInit() throws EstimationException {
Circle circle = new Circle(-12, -12); Circle circle = new Circle(-12, -12);

View File

@ -446,24 +446,56 @@ public class LevenbergMarquardtEstimatorTest
} }
public void testCircleFitting() throws EstimationException { public void testControlParameters() throws EstimationException {
Circle circle = new Circle(98.680, 47.345); Circle circle = new Circle(98.680, 47.345);
circle.addPoint( 30.0, 68.0); circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0); circle.addPoint( 50.0, -6.0);
circle.addPoint(110.0, -20.0); circle.addPoint(110.0, -20.0);
circle.addPoint( 35.0, 15.0); circle.addPoint( 35.0, 15.0);
circle.addPoint( 45.0, 97.0); circle.addPoint( 45.0, 97.0);
LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator(); checkEstimate(circle, 100.0, 1000, 1.0e-10, 1.0e-10, 1.0e-10, false);
estimator.estimate(circle); checkEstimate(circle, 1.0e-12, 10, 1.0e-20, 1.0e-20, 1.0e-20, true);
assertTrue(estimator.getCostEvaluations() < 10);
assertTrue(estimator.getJacobianEvaluations() < 10);
double rms = estimator.getRMS(circle);
assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10);
assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10);
assertEquals(96.07590211815305, circle.getX(), 1.0e-10);
assertEquals(48.13516790438953, circle.getY(), 1.0e-10);
} }
private void checkEstimate(EstimationProblem problem,
double initialStepBoundFactor, int maxCostEval,
double costRelativeTolerance, double parRelativeTolerance,
double orthoTolerance, boolean shouldFail) {
try {
LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator();
estimator.setInitialStepBoundFactor(initialStepBoundFactor);
estimator.setMaxCostEval(maxCostEval);
estimator.setCostRelativeTolerance(costRelativeTolerance);
estimator.setParRelativeTolerance(parRelativeTolerance);
estimator.setOrthoTolerance(orthoTolerance);
estimator.estimate(problem);
assertTrue(! shouldFail);
} catch (EstimationException ee) {
System.out.println(ee.getClass().getName() + " " + ee.getMessage());
assertTrue(shouldFail);
} catch (Exception e) {
fail("wrong exception type caught");
}
}
public void testCircleFitting() throws EstimationException {
Circle circle = new Circle(98.680, 47.345);
circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0);
circle.addPoint(110.0, -20.0);
circle.addPoint( 35.0, 15.0);
circle.addPoint( 45.0, 97.0);
LevenbergMarquardtEstimator estimator = new LevenbergMarquardtEstimator();
estimator.estimate(circle);
assertTrue(estimator.getCostEvaluations() < 10);
assertTrue(estimator.getJacobianEvaluations() < 10);
double rms = estimator.getRMS(circle);
assertEquals(1.768262623567235, Math.sqrt(circle.getM()) * rms, 1.0e-10);
assertEquals(69.96016176931406, circle.getRadius(), 1.0e-10);
assertEquals(96.07590211815305, circle.getX(), 1.0e-10);
assertEquals(48.13516790438953, circle.getY(), 1.0e-10);
}
public void testCircleFittingBadInit() throws EstimationException { public void testCircleFittingBadInit() throws EstimationException {
Circle circle = new Circle(-12, -12); Circle circle = new Circle(-12, -12);
double[][] points = new double[][] { double[][] points = new double[][] {