1 16 17 package org.apache.commons.math.analysis; 18 19 import org.apache.commons.math.ConvergenceException; 20 import org.apache.commons.math.MathException; 21 22 import junit.framework.TestCase; 23 24 27 public class UnivariateRealSolverUtilsTest extends TestCase { 28 29 protected UnivariateRealFunction sin = new SinFunction(); 30 31 public void testSolveNull() throws MathException { 32 try { 33 UnivariateRealSolverUtils.solve(null, 0.0, 4.0); 34 fail(); 35 } catch(IllegalArgumentException ex){ 36 } 38 } 39 40 public void testSolveBadParameters() throws MathException { 41 try { double x = UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0); 43 } catch (IllegalArgumentException ex) { 44 } 46 try { double x = UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0); 48 } catch (IllegalArgumentException ex) { 49 } 51 } 52 53 public void testSolveSin() throws MathException { 54 double x = UnivariateRealSolverUtils.solve(sin, 1.0, 55 4.0); 56 assertEquals(Math.PI, x, 1.0e-4); 57 } 58 59 public void testSolveAccuracyNull() throws MathException { 60 try { 61 double accuracy = 1.0e-6; 62 UnivariateRealSolverUtils.solve(null, 0.0, 4.0, accuracy); 63 fail(); 64 } catch(IllegalArgumentException ex){ 65 } 67 } 68 69 public void testSolveAccuracySin() throws MathException { 70 double accuracy = 1.0e-6; 71 double x = UnivariateRealSolverUtils.solve(sin, 1.0, 72 4.0, accuracy); 73 assertEquals(Math.PI, x, accuracy); 74 } 75 76 public void testSolveNoRoot() throws MathException { 77 try { 78 double x = UnivariateRealSolverUtils.solve(sin, 1.0, 79 1.5); 80 fail("Expecting IllegalArgumentException "); 81 } catch (IllegalArgumentException ex) { 82 } 84 } 85 86 public void testBracketSin() throws MathException { 87 double[] result = UnivariateRealSolverUtils.bracket(sin, 88 0.0, -2.0, 2.0); 89 assertTrue(sin.value(result[0]) < 0); 90 assertTrue(sin.value(result[1]) > 0); 91 } 92 93 public void testBracketCornerSolution() throws MathException { 94 try { 95 double[] result = UnivariateRealSolverUtils.bracket(sin, 96 1.5, 0, 2.0); 97 fail("Expecting ConvergenceException"); 98 } catch (ConvergenceException ex) { 99 } 101 } 102 103 public void testBadParameters() throws MathException { 104 try { double[] result = UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0); 106 fail("Expecting IllegalArgumentException"); 107 } catch (IllegalArgumentException ex) { 108 } 110 try { double[] result = UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0); 112 fail("Expecting IllegalArgumentException"); 113 } catch (IllegalArgumentException ex) { 114 } 116 try { double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0); 118 fail("Expecting IllegalArgumentException"); 119 } catch (IllegalArgumentException ex) { 120 } 122 try { double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0); 124 fail("Expecting IllegalArgumentException"); 125 } catch (IllegalArgumentException ex) { 126 } 128 } 129 130 } 131 | Popular Tags |