View Javadoc
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.rng.sampling.distribution;
18  
19  import org.apache.commons.rng.RestorableUniformRandomProvider;
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.apache.commons.rng.sampling.RandomAssert;
22  import org.apache.commons.rng.simple.RandomSource;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  /**
27   * Test for the {@link AhrensDieterMarsagliaTsangGammaSampler}. The tests hit edge cases for the sampler.
28   */
29  public class AhrensDieterMarsagliaTsangGammaSamplerTest {
30      /**
31       * Test the constructor with a bad alpha.
32       */
33      @Test(expected = IllegalArgumentException.class)
34      public void testConstructorThrowsWithZeroAlpha() {
35          final RestorableUniformRandomProvider rng =
36              RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
37          final double alpha = 0;
38          final double theta = 1;
39          AhrensDieterMarsagliaTsangGammaSampler.of(rng, alpha, theta);
40      }
41  
42      /**
43       * Test the constructor with a bad theta.
44       */
45      @Test(expected = IllegalArgumentException.class)
46      public void testConstructorThrowsWithZeroTheta() {
47          final RestorableUniformRandomProvider rng =
48              RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
49          final double alpha = 1;
50          final double theta = 0;
51          AhrensDieterMarsagliaTsangGammaSampler.of(rng, alpha, theta);
52      }
53  
54      /**
55       * Test the SharedStateSampler implementation.
56       */
57      @Test
58      public void testSharedStateSamplerWithAlphaBelowOne() {
59          testSharedStateSampler(0.5, 3.456);
60      }
61  
62      /**
63       * Test the SharedStateSampler implementation.
64       */
65      @Test
66      public void testSharedStateSamplerWithAlphaAboveOne() {
67          testSharedStateSampler(3.5, 3.456);
68      }
69  
70      /**
71       * Test the SharedStateSampler implementation.
72       *
73       * @param alpha Alpha.
74       * @param theta Theta.
75       */
76      private static void testSharedStateSampler(double alpha, double theta) {
77          final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
78          final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
79          // Use instance constructor not factory constructor to exercise 1.X public API
80          final AhrensDieterMarsagliaTsangGammaSampler sampler1 =
81              new AhrensDieterMarsagliaTsangGammaSampler(rng1, alpha, theta);
82          final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
83          RandomAssert.assertProduceSameSequence(sampler1, sampler2);
84      }
85  
86      /**
87       * Test the toString method. This is added to ensure coverage as the factory constructor
88       * used in other tests does not create an instance of the wrapper class.
89       */
90      @Test
91      public void testToString() {
92          final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
93          Assert.assertTrue(new AhrensDieterMarsagliaTsangGammaSampler(rng, 1.0, 2.0).toString()
94                  .toLowerCase().contains("gamma"));
95      }
96  }