1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.analysis;
19
20 import org.apache.commons.math.ConvergenceException;
21 import org.apache.commons.math.MathException;
22
23 import junit.framework.TestCase;
24
25
26
27
28 public class UnivariateRealSolverUtilsTest extends TestCase {
29
30 protected UnivariateRealFunction sin = new SinFunction();
31
32 public void testSolveNull() throws MathException {
33 try {
34 UnivariateRealSolverUtils.solve(null, 0.0, 4.0);
35 fail();
36 } catch(IllegalArgumentException ex){
37
38 }
39 }
40
41 public void testSolveBadParameters() throws MathException {
42 try {
43 UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0);
44 } catch (IllegalArgumentException ex) {
45
46 }
47 try {
48 UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0);
49 } catch (IllegalArgumentException ex) {
50
51 }
52 }
53
54 public void testSolveSin() throws MathException {
55 double x = UnivariateRealSolverUtils.solve(sin, 1.0, 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 UnivariateRealSolverUtils.solve(sin, 1.0, 1.5);
79 fail("Expecting IllegalArgumentException ");
80 } catch (IllegalArgumentException ex) {
81
82 }
83 }
84
85 public void testBracketSin() throws MathException {
86 double[] result = UnivariateRealSolverUtils.bracket(sin,
87 0.0, -2.0, 2.0);
88 assertTrue(sin.value(result[0]) < 0);
89 assertTrue(sin.value(result[1]) > 0);
90 }
91
92 public void testBracketCornerSolution() throws MathException {
93 try {
94 UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0);
95 fail("Expecting ConvergenceException");
96 } catch (ConvergenceException ex) {
97
98 }
99 }
100
101 public void testBadParameters() throws MathException {
102 try {
103 UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
104 fail("Expecting IllegalArgumentException");
105 } catch (IllegalArgumentException ex) {
106
107 }
108 try {
109 UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
110 fail("Expecting IllegalArgumentException");
111 } catch (IllegalArgumentException ex) {
112
113 }
114 try {
115 UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
116 fail("Expecting IllegalArgumentException");
117 } catch (IllegalArgumentException ex) {
118
119 }
120 try {
121 UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
122 fail("Expecting IllegalArgumentException");
123 } catch (IllegalArgumentException ex) {
124
125 }
126 }
127
128 }