001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.analysis;
018:
019: import org.apache.commons.math.ConvergenceException;
020: import org.apache.commons.math.MathException;
021:
022: import junit.framework.TestCase;
023:
024: /**
025: * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
026: */
027: public class UnivariateRealSolverUtilsTest extends TestCase {
028:
029: protected UnivariateRealFunction sin = new SinFunction();
030:
031: public void testSolveNull() throws MathException {
032: try {
033: UnivariateRealSolverUtils.solve(null, 0.0, 4.0);
034: fail();
035: } catch (IllegalArgumentException ex) {
036: // success
037: }
038: }
039:
040: public void testSolveBadParameters() throws MathException {
041: try { // bad endpoints
042: double x = UnivariateRealSolverUtils.solve(sin, 0.0, 4.0,
043: 4.0);
044: } catch (IllegalArgumentException ex) {
045: // expected
046: }
047: try { // bad accuracy
048: double x = UnivariateRealSolverUtils.solve(sin, 0.0, 4.0,
049: 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, 4.0,
073: accuracy);
074: assertEquals(Math.PI, x, accuracy);
075: }
076:
077: public void testSolveNoRoot() throws MathException {
078: try {
079: double x = 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, 0.0,
088: -2.0, 2.0);
089: assertTrue(sin.value(result[0]) < 0);
090: assertTrue(sin.value(result[1]) > 0);
091: }
092:
093: public void testBracketCornerSolution() throws MathException {
094: try {
095: double[] result = UnivariateRealSolverUtils.bracket(sin,
096: 1.5, 0, 2.0);
097: fail("Expecting ConvergenceException");
098: } catch (ConvergenceException ex) {
099: // expected
100: }
101: }
102:
103: public void testBadParameters() throws MathException {
104: try { // null function
105: double[] result = UnivariateRealSolverUtils.bracket(null,
106: 1.5, 0, 2.0);
107: fail("Expecting IllegalArgumentException");
108: } catch (IllegalArgumentException ex) {
109: // expected
110: }
111: try { // initial not between endpoints
112: double[] result = UnivariateRealSolverUtils.bracket(sin,
113: 2.5, 0, 2.0);
114: fail("Expecting IllegalArgumentException");
115: } catch (IllegalArgumentException ex) {
116: // expected
117: }
118: try { // endpoints not valid
119: double[] result = UnivariateRealSolverUtils.bracket(sin,
120: 1.5, 2.0, 1.0);
121: fail("Expecting IllegalArgumentException");
122: } catch (IllegalArgumentException ex) {
123: // expected
124: }
125: try { // bad maximum iterations
126: double[] result = UnivariateRealSolverUtils.bracket(sin,
127: 1.5, 0, 2.0, 0);
128: fail("Expecting IllegalArgumentException");
129: } catch (IllegalArgumentException ex) {
130: // expected
131: }
132: }
133:
134: }
|