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
018 package org.apache.commons.math.analysis.solvers;
019
020 import junit.framework.TestCase;
021
022 import org.apache.commons.math.MathException;
023 import org.apache.commons.math.analysis.SinFunction;
024 import org.apache.commons.math.analysis.UnivariateRealFunction;
025
026 /**
027 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
028 */
029 public class UnivariateRealSolverUtilsTest extends TestCase {
030
031 protected UnivariateRealFunction sin = new SinFunction();
032
033 public void testSolveNull() throws MathException {
034 try {
035 UnivariateRealSolverUtils.solve(null, 0.0, 4.0);
036 fail();
037 } catch(IllegalArgumentException ex){
038 // success
039 }
040 }
041
042 public void testSolveBadParameters() throws MathException {
043 try { // bad endpoints
044 UnivariateRealSolverUtils.solve(sin,0.0, 4.0, 4.0);
045 } catch (IllegalArgumentException ex) {
046 // expected
047 }
048 try { // bad accuracy
049 UnivariateRealSolverUtils.solve(sin, 0.0, 4.0, 0.0);
050 } catch (IllegalArgumentException ex) {
051 // expected
052 }
053 }
054
055 public void testSolveSin() throws MathException {
056 double x = UnivariateRealSolverUtils.solve(sin, 1.0, 4.0);
057 assertEquals(Math.PI, x, 1.0e-4);
058 }
059
060 public void testSolveAccuracyNull() throws MathException {
061 try {
062 double accuracy = 1.0e-6;
063 UnivariateRealSolverUtils.solve(null, 0.0, 4.0, accuracy);
064 fail();
065 } catch(IllegalArgumentException ex){
066 // success
067 }
068 }
069
070 public void testSolveAccuracySin() throws MathException {
071 double accuracy = 1.0e-6;
072 double x = UnivariateRealSolverUtils.solve(sin, 1.0,
073 4.0, accuracy);
074 assertEquals(Math.PI, x, accuracy);
075 }
076
077 public void testSolveNoRoot() throws MathException {
078 try {
079 UnivariateRealSolverUtils.solve(sin, 1.0, 1.5);
080 fail("Expecting IllegalArgumentException ");
081 } catch (IllegalArgumentException ex) {
082 // expected
083 }
084 }
085
086 public void testBracketSin() throws MathException {
087 double[] result = UnivariateRealSolverUtils.bracket(sin,
088 0.0, -2.0, 2.0);
089 assertTrue(sin.value(result[0]) < 0);
090 assertTrue(sin.value(result[1]) > 0);
091 }
092
093 public void testBracketEndpointRoot() throws MathException {
094 double[] result = UnivariateRealSolverUtils.bracket(sin, 1.5, 0, 2.0);
095 assertEquals(0.0, sin.value(result[0]), 1.0e-15);
096 assertTrue(sin.value(result[1]) > 0);
097 }
098
099 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 }