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 GammaTest extends TestCase {
27      /**
28       * Constructor for BetaTest.
29       * @param name
30       */
31      public GammaTest(String name) {
32          super(name);
33      }
34  
35      private void testRegularizedGamma(double expected, double a, double x) {
36          try {
37              double actualP = Gamma.regularizedGammaP(a, x);
38              double actualQ = Gamma.regularizedGammaQ(a, x);
39              TestUtils.assertEquals(expected, actualP, 10e-5);
40              TestUtils.assertEquals(actualP, 1.0 - actualQ, 10e-5);
41          } catch(MathException ex){
42              fail(ex.getMessage());
43          }
44      }
45  
46      private void testLogGamma(double expected, double x) {
47          double actual = Gamma.logGamma(x);
48          TestUtils.assertEquals(expected, actual, 10e-5);
49      }
50  
51      public void testRegularizedGammaNanPositive() {
52          testRegularizedGamma(Double.NaN, Double.NaN, 1.0);
53      }
54  
55      public void testRegularizedGammaPositiveNan() {
56          testRegularizedGamma(Double.NaN, 1.0, Double.NaN);
57      }
58      
59      public void testRegularizedGammaNegativePositive() {
60          testRegularizedGamma(Double.NaN, -1.5, 1.0);
61      }
62      
63      public void testRegularizedGammaPositiveNegative() {
64          testRegularizedGamma(Double.NaN, 1.0, -1.0);
65      }
66      
67      public void testRegularizedGammaZeroPositive() {
68          testRegularizedGamma(Double.NaN, 0.0, 1.0);
69      }
70      
71      public void testRegularizedGammaPositiveZero() {
72          testRegularizedGamma(0.0, 1.0, 0.0);
73      }
74      
75      public void testRegularizedGammaPositivePositive() {
76          testRegularizedGamma(0.632121, 1.0, 1.0);
77      }
78      
79      public void testLogGammaNan() {
80          testLogGamma(Double.NaN, Double.NaN);
81      }
82      
83      public void testLogGammaNegative() {
84          testLogGamma(Double.NaN, -1.0);
85      }
86      
87      public void testLogGammaZero() {
88          testLogGamma(Double.NaN, 0.0);
89      }
90      
91      public void testLogGammaPositive() {
92          testLogGamma(0.693147, 3.0);
93      }
94  }