001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.math.analysis.solvers;
018    
019    import org.apache.commons.math.MathException;
020    import org.apache.commons.math.analysis.QuinticFunction;
021    import org.apache.commons.math.analysis.SinFunction;
022    import org.apache.commons.math.analysis.UnivariateRealFunction;
023    
024    import junit.framework.TestCase;
025    
026    /**
027     * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
028     */
029    public final class BisectionSolverTest extends TestCase {
030    
031        @Deprecated
032        public void testDeprecated() throws MathException {
033            UnivariateRealFunction f = new SinFunction();
034            double result;
035            
036            UnivariateRealSolver solver = new BisectionSolver(f);
037            result = solver.solve(3, 4);
038            assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
039    
040            result = solver.solve(1, 4);
041            assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
042        }
043    
044        public void testSinZero() throws MathException {
045            UnivariateRealFunction f = new SinFunction();
046            double result;
047            
048            UnivariateRealSolver solver = new BisectionSolver();
049            result = solver.solve(f, 3, 4);
050            assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
051    
052            result = solver.solve(f, 1, 4);
053            assertEquals(result, Math.PI, solver.getAbsoluteAccuracy());
054        }
055    
056       public void testQuinticZero() throws MathException {
057            UnivariateRealFunction f = new QuinticFunction();
058            double result;
059    
060            UnivariateRealSolver solver = new BisectionSolver();
061            result = solver.solve(f, -0.2, 0.2);
062            assertEquals(result, 0, solver.getAbsoluteAccuracy());
063    
064            result = solver.solve(f, -0.1, 0.3);
065            assertEquals(result, 0, solver.getAbsoluteAccuracy());
066    
067            result = solver.solve(f, -0.3, 0.45);
068            assertEquals(result, 0, solver.getAbsoluteAccuracy());
069    
070            result = solver.solve(f, 0.3, 0.7);
071            assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
072    
073            result = solver.solve(f, 0.2, 0.6);
074            assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
075    
076            result = solver.solve(f, 0.05, 0.95);
077            assertEquals(result, 0.5, solver.getAbsoluteAccuracy());
078    
079            result = solver.solve(f, 0.85, 1.25);
080            assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
081    
082            result = solver.solve(f, 0.8, 1.2);
083            assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
084    
085            result = solver.solve(f, 0.85, 1.75);
086            assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
087    
088            result = solver.solve(f, 0.55, 1.45);
089            assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
090    
091            result = solver.solve(f, 0.85, 5);
092            assertEquals(result, 1.0, solver.getAbsoluteAccuracy());
093            
094            assertEquals(result, solver.getResult(), 0);
095            assertTrue(solver.getIterationCount() > 0);
096        }
097        
098        /**
099         * 
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    }