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  package org.apache.commons.math.special;
18  
19  import org.apache.commons.math.MathException;
20  import org.apache.commons.math.TestUtils;
21  
22  import junit.framework.TestCase;
23  
24  /**
25   * @version $Revision: 549278 $ $Date: 2007-06-20 15:24:04 -0700 (Wed, 20 Jun 2007) $
26   */
27  public class GammaTest extends TestCase {
28      /**
29       * Constructor for BetaTest.
30       * @param name
31       */
32      public GammaTest(String name) {
33          super(name);
34      }
35  
36      private void testRegularizedGamma(double expected, double a, double x) {
37          try {
38              double actualP = Gamma.regularizedGammaP(a, x);
39              double actualQ = Gamma.regularizedGammaQ(a, x);
40              TestUtils.assertEquals(expected, actualP, 10e-15);
41              TestUtils.assertEquals(actualP, 1.0 - actualQ, 10e-15);
42          } catch(MathException ex){
43              fail(ex.getMessage());
44          }
45      }
46  
47      private void testLogGamma(double expected, double x) {
48          double actual = Gamma.logGamma(x);
49          TestUtils.assertEquals(expected, actual, 10e-15);
50      }
51  
52      public void testRegularizedGammaNanPositive() {
53          testRegularizedGamma(Double.NaN, Double.NaN, 1.0);
54      }
55  
56      public void testRegularizedGammaPositiveNan() {
57          testRegularizedGamma(Double.NaN, 1.0, Double.NaN);
58      }
59      
60      public void testRegularizedGammaNegativePositive() {
61          testRegularizedGamma(Double.NaN, -1.5, 1.0);
62      }
63      
64      public void testRegularizedGammaPositiveNegative() {
65          testRegularizedGamma(Double.NaN, 1.0, -1.0);
66      }
67      
68      public void testRegularizedGammaZeroPositive() {
69          testRegularizedGamma(Double.NaN, 0.0, 1.0);
70      }
71      
72      public void testRegularizedGammaPositiveZero() {
73          testRegularizedGamma(0.0, 1.0, 0.0);
74      }
75      
76      public void testRegularizedGammaPositivePositive() {
77          testRegularizedGamma(0.632120558828558, 1.0, 1.0);
78      }
79      
80      public void testLogGammaNan() {
81          testLogGamma(Double.NaN, Double.NaN);
82      }
83      
84      public void testLogGammaNegative() {
85          testLogGamma(Double.NaN, -1.0);
86      }
87      
88      public void testLogGammaZero() {
89          testLogGamma(Double.NaN, 0.0);
90      }
91      
92      public void testLogGammaPositive() {
93          testLogGamma(0.6931471805599457, 3.0);
94      }
95  }