1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 /**
25 * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
26 */
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
37 }
38 }
39
40 public void testSolveBadParameters() throws MathException {
41 try {
42 double x = UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0);
43 } catch (IllegalArgumentException ex) {
44
45 }
46 try {
47 double x = UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0);
48 } catch (IllegalArgumentException ex) {
49
50 }
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
66 }
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
83 }
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
100 }
101 }
102
103 public void testBadParameters() throws MathException {
104 try {
105 double[] result = UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
106 fail("Expecting IllegalArgumentException");
107 } catch (IllegalArgumentException ex) {
108
109 }
110 try {
111 double[] result = UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
112 fail("Expecting IllegalArgumentException");
113 } catch (IllegalArgumentException ex) {
114
115 }
116 try {
117 double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
118 fail("Expecting IllegalArgumentException");
119 } catch (IllegalArgumentException ex) {
120
121 }
122 try {
123 double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
124 fail("Expecting IllegalArgumentException");
125 } catch (IllegalArgumentException ex) {
126
127 }
128 }
129
130 }