improved test coverage

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@797790 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-07-25 16:21:52 +00:00
parent 1a7da0d08a
commit afb42aaf58
1 changed files with 21 additions and 19 deletions

View File

@ -16,9 +16,9 @@
*/
package org.apache.commons.math.optimization.univariate;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.apache.commons.math.MathException;
import org.apache.commons.math.analysis.QuinticFunction;
@ -26,32 +26,33 @@ import org.apache.commons.math.analysis.SinFunction;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.UnivariateRealOptimizer;
import org.junit.Test;
/**
* @version $Revision$ $Date$
*/
public final class BrentMinimizerTest extends TestCase {
public BrentMinimizerTest(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(BrentMinimizerTest.class);
suite.setName("BrentOptimizer Tests");
return suite;
}
public final class BrentMinimizerTest {
@Test
public void testSinMin() throws MathException {
UnivariateRealFunction f = new SinFunction();
UnivariateRealOptimizer minimizer = new BrentOptimizer();
try {
minimizer.getResult();
fail("an exception should have been thrown");
} catch (IllegalStateException ise) {
// expected
} catch (Exception e) {
fail("wrong exception caught");
}
assertEquals(3 * Math.PI / 2, minimizer.optimize(f, GoalType.MINIMIZE, 4, 5), 70 * minimizer.getAbsoluteAccuracy());
assertTrue(minimizer.getIterationCount() <= 50);
assertEquals(3 * Math.PI / 2, minimizer.optimize(f, GoalType.MINIMIZE, 1, 5), 70 * minimizer.getAbsoluteAccuracy());
assertTrue(minimizer.getIterationCount() <= 50);
}
public void testQuinticMin() throws MathException {
@Test
public void testQuinticMin() throws MathException {
// The quintic function has zeros at 0, +-0.5 and +-1.
// The function has extrema (first derivative is zero) at 0.27195613 and 0.82221643,
UnivariateRealFunction f = new QuinticFunction();
@ -64,12 +65,13 @@ public final class BrentMinimizerTest extends TestCase {
assertEquals(-0.27195613, minimizer.optimize(f, GoalType.MINIMIZE, -1.0, 0.2), 1.0e-8);
assertTrue(minimizer.getIterationCount() <= 50);
}
}
@Test
public void testMinEndpoints() throws Exception {
UnivariateRealFunction f = new SinFunction();
UnivariateRealOptimizer solver = new BrentOptimizer();
// endpoint is minimum
double result = solver.optimize(f, GoalType.MINIMIZE, 3 * Math.PI / 2, 5);
assertEquals(3 * Math.PI / 2, result, 70 * solver.getAbsoluteAccuracy());
@ -78,5 +80,5 @@ public final class BrentMinimizerTest extends TestCase {
assertEquals(3 * Math.PI / 2, result, 70 * solver.getAbsoluteAccuracy());
}
}