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  
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   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
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              // success
39          }
40      }
41      
42      public void testSolveBadParameters() throws MathException {
43          try { // bad endpoints
44              UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0); 
45          } catch (IllegalArgumentException ex) {
46              // expected
47          }    
48          try { // bad accuracy
49              UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0); 
50          } catch (IllegalArgumentException ex) {
51              // expected
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              // success
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              // 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 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 { // null function
101             UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
102             fail("Expecting IllegalArgumentException");
103         } catch (IllegalArgumentException ex) {
104             // expected
105         }
106         try { // initial not between endpoints
107             UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
108             fail("Expecting IllegalArgumentException");
109         } catch (IllegalArgumentException ex) {
110             // expected
111         }
112         try { // endpoints not valid
113             UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
114             fail("Expecting IllegalArgumentException");
115         } catch (IllegalArgumentException ex) {
116             // expected
117         }
118         try { // bad maximum iterations
119             UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
120             fail("Expecting IllegalArgumentException");
121         } catch (IllegalArgumentException ex) {
122             // expected
123         }        
124     }
125     
126 }