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