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