1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
26
27 public class GammaTest extends TestCase {
28
29
30
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 }