1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.analysis.solvers;
19
20 import junit.framework.TestCase;
21
22 import org.apache.commons.math.MathException;
23 import org.apache.commons.math.analysis.SinFunction;
24 import org.apache.commons.math.analysis.UnivariateRealFunction;
25
26
27
28
29 public class UnivariateRealSolverUtilsTest extends TestCase {
30
31 protected UnivariateRealFunction sin = new SinFunction();
32
33 public void testSolveNull() throws MathException {
34 try {
35 UnivariateRealSolverUtils.solve(null, 0.0, 4.0);
36 fail();
37 } catch(IllegalArgumentException ex){
38
39 }
40 }
41
42 public void testSolveBadParameters() throws MathException {
43 try {
44 UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0);
45 } catch (IllegalArgumentException ex) {
46
47 }
48 try {
49 UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0);
50 } catch (IllegalArgumentException ex) {
51
52 }
53 }
54
55 public void testSolveSin() throws MathException {
56 double x = UnivariateRealSolverUtils.solve(sin, 1.0, 4.0);
57 assertEquals(Math.PI, x, 1.0e-4);
58 }
59
60 public void testSolveAccuracyNull() throws MathException {
61 try {
62 double accuracy = 1.0e-6;
63 UnivariateRealSolverUtils.solve(null, 0.0, 4.0, accuracy);
64 fail();
65 } catch(IllegalArgumentException ex){
66
67 }
68 }
69
70 public void testSolveAccuracySin() throws MathException {
71 double accuracy = 1.0e-6;
72 double x = UnivariateRealSolverUtils.solve(sin, 1.0,
73 4.0, accuracy);
74 assertEquals(Math.PI, x, accuracy);
75 }
76
77 public void testSolveNoRoot() throws MathException {
78 try {
79 UnivariateRealSolverUtils.solve(sin, 1.0, 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 testBracketEndpointRoot() throws MathException {
94 double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0);
95 assertEquals(0.0, sin.value(result[0]), 1.0e-15);
96 assertTrue(sin.value(result[1]) > 0);
97 }
98
99 public void testBadParameters() throws MathException {
100 try {
101 UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
102 fail("Expecting IllegalArgumentException");
103 } catch (IllegalArgumentException ex) {
104
105 }
106 try {
107 UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
108 fail("Expecting IllegalArgumentException");
109 } catch (IllegalArgumentException ex) {
110
111 }
112 try {
113 UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
114 fail("Expecting IllegalArgumentException");
115 } catch (IllegalArgumentException ex) {
116
117 }
118 try {
119 UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
120 fail("Expecting IllegalArgumentException");
121 } catch (IllegalArgumentException ex) {
122
123 }
124 }
125
126 }