KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > math > analysis > UnivariateRealSolverUtilsTest


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
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$ $Date: 2005-02-26 05:11:52 -0800 (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 JavaDoc ex){
36             // success
37
}
38     }
39     
40     public void testSolveBadParameters() throws MathException {
41         try { // bad endpoints
42
double x = UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0);
43         } catch (IllegalArgumentException JavaDoc ex) {
44             // expected
45
}
46         try { // bad accuracy
47
double x = UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0);
48         } catch (IllegalArgumentException JavaDoc ex) {
49             // expected
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 JavaDoc ex){
65             // success
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 JavaDoc ex) {
82             // expected
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             // expected
100
}
101     }
102     
103     public void testBadParameters() throws MathException {
104         try { // null function
105
double[] result = UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
106             fail("Expecting IllegalArgumentException");
107         } catch (IllegalArgumentException JavaDoc ex) {
108             // expected
109
}
110         try { // initial not between endpoints
111
double[] result = UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
112             fail("Expecting IllegalArgumentException");
113         } catch (IllegalArgumentException JavaDoc ex) {
114             // expected
115
}
116         try { // endpoints not valid
117
double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
118             fail("Expecting IllegalArgumentException");
119         } catch (IllegalArgumentException JavaDoc ex) {
120             // expected
121
}
122         try { // bad maximum iterations
123
double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
124             fail("Expecting IllegalArgumentException");
125         } catch (IllegalArgumentException JavaDoc ex) {
126             // expected
127
}
128     }
129     
130 }
131
Popular Tags