1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.analysis.solvers;
18  
19  import org.apache.commons.math.MathException;
20  import org.apache.commons.math.analysis.QuinticFunction;
21  import org.apache.commons.math.analysis.SinFunction;
22  import org.apache.commons.math.analysis.UnivariateRealFunction;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
28   */
29  public final class BisectionSolverTest extends TestCase {
30  
31      @Deprecated
32      public void testDeprecated() throws MathException {
33          UnivariateRealFunction f = new SinFunction();
34          double result;
35          
36          UnivariateRealSolver solver = new BisectionSolver(f);
37          result = solver.solve(3, 4);
38          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
39  
40          result = solver.solve(1, 4);
41          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
42      }
43  
44      public void testSinZero() throws MathException {
45          UnivariateRealFunction f = new SinFunction();
46          double result;
47          
48          UnivariateRealSolver solver = new BisectionSolver();
49          result = solver.solve(f, 3, 4);
50          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
51  
52          result = solver.solve(f, 1, 4);
53          assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
54      }
55  
56     public void testQuinticZero() throws MathException {
57          UnivariateRealFunction f = new QuinticFunction();
58          double result;
59  
60          UnivariateRealSolver solver = new BisectionSolver();
61          result = solver.solve(f, -0.2, 0.2);
62          assertEquals(result, 0, solver.getAbsoluteAccuracy());
63  
64          result = solver.solve(f, -0.1, 0.3);
65          assertEquals(result, 0, solver.getAbsoluteAccuracy());
66  
67          result = solver.solve(f, -0.3, 0.45);
68          assertEquals(result, 0, solver.getAbsoluteAccuracy());
69  
70          result = solver.solve(f, 0.3, 0.7);
71          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
72  
73          result = solver.solve(f, 0.2, 0.6);
74          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
75  
76          result = solver.solve(f, 0.05, 0.95);
77          assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
78  
79          result = solver.solve(f, 0.85, 1.25);
80          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
81  
82          result = solver.solve(f, 0.8, 1.2);
83          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
84  
85          result = solver.solve(f, 0.85, 1.75);
86          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
87  
88          result = solver.solve(f, 0.55, 1.45);
89          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
90  
91          result = solver.solve(f, 0.85, 5);
92          assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
93          
94          assertEquals(result, solver.getResult(), 0);
95          assertTrue(solver.getIterationCount() > 0);
96      }
97      
98      /**
99       * 
100      */
101     public void testSetFunctionValueAccuracy(){
102         double expected = 1.0e-2;    
103         UnivariateRealSolver solver = new BisectionSolver();
104         solver.setFunctionValueAccuracy(expected);
105         assertEquals(expected, solver.getFunctionValueAccuracy(), 1.0e-2);
106     }        
107     
108     /**
109      * 
110      */
111     public void testResetFunctionValueAccuracy(){
112         double newValue = 1.0e-2;    
113         UnivariateRealSolver solver = new BisectionSolver();
114         double oldValue = solver.getFunctionValueAccuracy();
115         solver.setFunctionValueAccuracy(newValue);
116         solver.resetFunctionValueAccuracy();
117         assertEquals(oldValue, solver.getFunctionValueAccuracy(), 1.0e-2);
118     }        
119     
120     /**
121      * 
122      */
123     public void testSetAbsoluteAccuracy(){
124         double expected = 1.0e-2; 
125         UnivariateRealSolver solver = new BisectionSolver();
126         solver.setAbsoluteAccuracy(expected);
127         assertEquals(expected, solver.getAbsoluteAccuracy(), 1.0e-2); 
128     }        
129     
130     /**
131      * 
132      */
133     public void testResetAbsoluteAccuracy(){
134         double newValue = 1.0e-2;       
135         UnivariateRealSolver solver = new BisectionSolver();
136         double oldValue = solver.getAbsoluteAccuracy();
137         solver.setAbsoluteAccuracy(newValue);
138         solver.resetAbsoluteAccuracy();
139         assertEquals(oldValue, solver.getAbsoluteAccuracy(), 1.0e-2);
140     }        
141     
142     /**
143      * 
144      */
145     public void testSetMaximalIterationCount(){
146         int expected = 100;
147         UnivariateRealSolver solver = new BisectionSolver();
148         solver.setMaximalIterationCount(expected);
149         assertEquals(expected, solver.getMaximalIterationCount());
150     }        
151     
152     /**
153      * 
154      */
155     public void testResetMaximalIterationCount(){
156         int newValue = 10000;
157         UnivariateRealSolver solver = new BisectionSolver();
158         int oldValue = solver.getMaximalIterationCount();
159         solver.setMaximalIterationCount(newValue);
160         solver.resetMaximalIterationCount();
161         assertEquals(oldValue, solver.getMaximalIterationCount());
162     }        
163     
164     /**
165      * 
166      */
167     public void testSetRelativeAccuracy(){
168         double expected = 1.0e-2;
169         UnivariateRealSolver solver = new BisectionSolver();
170         solver.setRelativeAccuracy(expected);
171         assertEquals(expected, solver.getRelativeAccuracy(), 1.0e-2);
172     }        
173     
174     /**
175      * 
176      */
177     public void testResetRelativeAccuracy(){
178         double newValue = 1.0e-2;        
179         UnivariateRealSolver solver = new BisectionSolver();
180         double oldValue = solver.getRelativeAccuracy();
181         solver.setRelativeAccuracy(newValue);
182         solver.resetRelativeAccuracy();
183         assertEquals(oldValue, solver.getRelativeAccuracy(), 1.0e-2);
184     }        
185     
186    
187 }