    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.special;
    17
    18  import org.apache.commons.math.MathException;
    19  import org.apache.commons.math.TestUtils;
    20
    21  import junit.framework.TestCase;
    22
    23  /***
    24   * @version $Revision: 155427 $ $Date: 2005-02-26 06:11:52 -0700 (Sat, 26 Feb 2005) $
    25   */
    26  public class BetaTest extends TestCase {
    27      /***
    28       * Constructor for BetaTest.
    29       * @param name
    30       */
    31      public BetaTest(String name) {
    32          super(name);
    33      }
    34
    35      private void testRegularizedBeta(double expected, double x, double a,
    36          double b)
    37      {
    38          try {
    39              double actual = Beta.regularizedBeta(x, a, b);
    40              TestUtils.assertEquals(expected, actual, 10e-5);
    41          } catch(MathException ex){
    42              fail(ex.getMessage());
    43          }
    44      }
    45
    46      private void testLogBeta(double expected, double a, double b) {
    47          double actual = Beta.logBeta(a, b);
    48          TestUtils.assertEquals(expected, actual, 10e-5);
    49      }
    50
    51      public void testRegularizedBetaNanPositivePositive() {
    52          testRegularizedBeta(Double.NaN, Double.NaN, 1.0, 1.0);
    53      }
    54
    55      public void testRegularizedBetaPositiveNanPositive() {
    56          testRegularizedBeta(Double.NaN, 0.5, Double.NaN, 1.0);
    57      }
    58
    59      public void testRegularizedBetaPositivePositiveNan() {
    60          testRegularizedBeta(Double.NaN, 0.5, 1.0, Double.NaN);
    61      }
    62
    63      public void testRegularizedBetaNegativePositivePositive() {
    64          testRegularizedBeta(Double.NaN, -0.5, 1.0, 2.0);
    65      }
    66
    67      public void testRegularizedBetaPositiveNegativePositive() {
    68          testRegularizedBeta(Double.NaN, 0.5, -1.0, 2.0);
    69      }
    70
    71      public void testRegularizedBetaPositivePositiveNegative() {
    72          testRegularizedBeta(Double.NaN, 0.5, 1.0, -2.0);
    73      }
    74
    75      public void testRegularizedBetaZeroPositivePositive() {
    76          testRegularizedBeta(0.0, 0.0, 1.0, 2.0);
    77      }
    78
    79      public void testRegularizedBetaPositiveZeroPositive() {
    80          testRegularizedBeta(Double.NaN, 0.5, 0.0, 2.0);
    81      }
    82
    83      public void testRegularizedBetaPositivePositiveZero() {
    84          testRegularizedBeta(Double.NaN, 0.5, 1.0, 0.0);
    85      }
    86
    87      public void testRegularizedBetaPositivePositivePositive() {
    88          testRegularizedBeta(0.75, 0.5, 1.0, 2.0);
    89      }
    90
    91      public void testLogBetaNanPositive() {
    92          testLogBeta(Double.NaN, Double.NaN, 2.0);
    93      }
    94
    95      public void testLogBetaPositiveNan() {
    96          testLogBeta(Double.NaN, 1.0, Double.NaN);
    97      }
    98
    99      public void testLogBetaNegativePositive() {
    100         testLogBeta(Double.NaN, -1.0, 2.0);
    101     }
    102
    103     public void testLogBetaPositiveNegative() {
    104         testLogBeta(Double.NaN, 1.0, -2.0);
    105     }
    106
    107     public void testLogBetaZeroPositive() {
    108         testLogBeta(Double.NaN, 0.0, 2.0);
    109     }
    110
    111     public void testLogBetaPositiveZero() {
    112         testLogBeta(Double.NaN, 1.0, 0.0);
    113     }
    114
    115     public void testLogBetaPositivePositive() {
    116         testLogBeta(-0.693147, 1.0, 2.0);
    117     }
    118 }