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 ChengBetaSampler}. The tests hit edge cases for the sampler.
26   */
27  class ChengBetaSamplerTest {
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 beta = 1;
36          Assertions.assertThrows(IllegalArgumentException.class,
37              () -> ChengBetaSampler.of(rng, alpha, beta));
38      }
39  
40      /**
41       * Test the constructor with a bad beta.
42       */
43      @Test
44      void testConstructorThrowsWithZeroBeta() {
45          final UniformRandomProvider rng = RandomAssert.seededRNG();
46          final double alpha = 1;
47          final double beta = 0;
48          Assertions.assertThrows(IllegalArgumentException.class,
49              () -> ChengBetaSampler.of(rng, alpha, beta));
50      }
51  
52      /**
53       * Test the SharedStateSampler implementation.
54       */
55      @Test
56      void testSharedStateSamplerWithAlphaAndBetaAbove1AndAlphaBelowBeta() {
57          testSharedStateSampler(1.23, 4.56);
58      }
59  
60      /**
61       * Test the SharedStateSampler implementation.
62       */
63      @Test
64      void testSharedStateSamplerWithAlphaAndBetaAbove1AndAlphaAboveBeta() {
65          testSharedStateSampler(4.56, 1.23);
66      }
67  
68      /**
69       * Test the SharedStateSampler implementation.
70       */
71      @Test
72      void testSharedStateSamplerWithAlphaOrBetaBelow1AndAlphaBelowBeta() {
73          testSharedStateSampler(0.23, 4.56);
74      }
75  
76      /**
77       * Test the SharedStateSampler implementation.
78       */
79      @Test
80      void testSharedStateSamplerWithAlphaOrBetaBelow1AndAlphaAboveBeta() {
81          testSharedStateSampler(4.56, 0.23);
82      }
83  
84      /**
85       * Test the SharedStateSampler implementation.
86       *
87       * @param alpha Alpha.
88       * @param beta Beta.
89       */
90      private static void testSharedStateSampler(double alpha, double beta) {
91          final UniformRandomProvider rng1 = RandomAssert.seededRNG();
92          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
93          // Use instance constructor not factory constructor to exercise 1.X public API
94          final SharedStateContinuousSampler sampler1 =
95              new ChengBetaSampler(rng1, alpha, beta);
96          final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
97          RandomAssert.assertProduceSameSequence(sampler1, sampler2);
98      }
99  
100     /**
101      * Test the toString method. This is added to ensure coverage as the factory constructor
102      * used in other tests does not create an instance of the wrapper class.
103      */
104     @Test
105     void testToString() {
106         final UniformRandomProvider rng = RandomAssert.seededRNG();
107         Assertions.assertTrue(new ChengBetaSampler(rng, 1.0, 2.0).toString()
108                 .toLowerCase().contains("beta"));
109     }
110 }