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;
19  
20  import org.apache.commons.math.ConvergenceException;
21  import org.apache.commons.math.MathException;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * @version $Revision: 566833 $ $Date: 2007-08-16 13:36:33 -0700 (Thu, 16 Aug 2007) $
27   */
28  public class UnivariateRealSolverUtilsTest extends TestCase {
29      
30      protected UnivariateRealFunction sin = new SinFunction();
31      
32      public void testSolveNull() throws MathException {
33          try {
34              UnivariateRealSolverUtils.solve(null, 0.0, 4.0);
35              fail();
36          } catch(IllegalArgumentException ex){
37              // success
38          }
39      }
40      
41      public void testSolveBadParameters() throws MathException {
42          try { // bad endpoints
43              UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0); 
44          } catch (IllegalArgumentException ex) {
45              // expected
46          }    
47          try { // bad accuracy
48              UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0); 
49          } catch (IllegalArgumentException ex) {
50              // expected
51          }        
52      }
53      
54      public void testSolveSin() throws MathException {     
55          double x = UnivariateRealSolverUtils.solve(sin, 1.0, 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 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              UnivariateRealSolverUtils.solve(sin, 1.0, 1.5);  
79              fail("Expecting IllegalArgumentException ");  
80          } catch (IllegalArgumentException ex) {
81              // expected
82          }
83      }
84      
85      public void testBracketSin() throws MathException {
86          double[] result = UnivariateRealSolverUtils.bracket(sin, 
87                  0.0, -2.0, 2.0);
88          assertTrue(sin.value(result[0]) < 0);
89          assertTrue(sin.value(result[1]) > 0);
90      }
91      
92      public void testBracketCornerSolution() throws MathException {
93          try {
94              UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0); 
95              fail("Expecting ConvergenceException");
96          } catch (ConvergenceException ex) {
97              // expected
98          }
99      }
100     
101     public void testBadParameters() throws MathException {
102         try { // null function
103             UnivariateRealSolverUtils.bracket(null, 1.5, 0, 2.0);
104             fail("Expecting IllegalArgumentException");
105         } catch (IllegalArgumentException ex) {
106             // expected
107         }
108         try { // initial not between endpoints
109             UnivariateRealSolverUtils.bracket(sin, 2.5, 0, 2.0);
110             fail("Expecting IllegalArgumentException");
111         } catch (IllegalArgumentException ex) {
112             // expected
113         }
114         try { // endpoints not valid
115             UnivariateRealSolverUtils.bracket(sin, 1.5, 2.0, 1.0);
116             fail("Expecting IllegalArgumentException");
117         } catch (IllegalArgumentException ex) {
118             // expected
119         }
120         try { // bad maximum iterations
121             UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0, 0);
122             fail("Expecting IllegalArgumentException");
123         } catch (IllegalArgumentException ex) {
124             // expected
125         }        
126     }
127     
128 }