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.apache.commons.rng.sampling.SharedStateSampler;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Test for the {@link LogNormalSampler}. The tests hit edge cases for the sampler.
27   */
28  class LogNormalSamplerTest {
29      /**
30       * Test the constructor with a bad standard deviation.
31       */
32      @Test
33      void testConstructorThrowsWithZeroShape() {
34          final UniformRandomProvider rng = RandomAssert.seededRNG();
35          final NormalizedGaussianSampler gauss = ZigguratSampler.NormalizedGaussian.of(rng);
36          final double mu = 1;
37          final double sigma = 0;
38          Assertions.assertThrows(IllegalArgumentException.class,
39              () -> LogNormalSampler.of(gauss, mu, sigma));
40      }
41  
42      /**
43       * Test the SharedStateSampler implementation.
44       */
45      @Test
46      void testSharedStateSampler() {
47          final UniformRandomProvider rng1 = RandomAssert.seededRNG();
48          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
49          final NormalizedGaussianSampler gauss = ZigguratSampler.NormalizedGaussian.of(rng1);
50          // Test a negative mean is allowed (see RNG-166)
51          final double mu = -1.23;
52          final double sigma = 4.56;
53          final SharedStateContinuousSampler sampler1 =
54              LogNormalSampler.of(gauss, mu, sigma);
55          final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
56          RandomAssert.assertProduceSameSequence(sampler1, sampler2);
57      }
58  
59      /**
60       * Test the SharedStateSampler implementation throws if the underlying sampler is
61       * not a SharedStateSampler.
62       */
63      @Test
64      void testSharedStateSamplerThrowsIfUnderlyingSamplerDoesNotShareState() {
65          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
66          final NormalizedGaussianSampler gauss = new NormalizedGaussianSampler() {
67              @Override
68              public double sample() {
69                  return 0;
70              }
71          };
72          final double mu = 1.23;
73          final double sigma = 4.56;
74          final SharedStateContinuousSampler sampler1 =
75              LogNormalSampler.of(gauss, mu, sigma);
76          Assertions.assertThrows(UnsupportedOperationException.class,
77              () -> sampler1.withUniformRandomProvider(rng2));
78      }
79  
80      /**
81       * Test the SharedStateSampler implementation throws if the underlying sampler is
82       * a SharedStateSampler that returns an incorrect type.
83       */
84      @Test
85      void testSharedStateSamplerThrowsIfUnderlyingSamplerReturnsWrongSharedState() {
86          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
87          final NormalizedGaussianSampler gauss = new BadSharedStateNormalizedGaussianSampler();
88          final double mu = 1.23;
89          final double sigma = 4.56;
90          final SharedStateContinuousSampler sampler1 =
91              LogNormalSampler.of(gauss, mu, sigma);
92          Assertions.assertThrows(UnsupportedOperationException.class,
93              () -> sampler1.withUniformRandomProvider(rng2));
94      }
95  
96      /**
97       * Test class to return an incorrect sampler from the SharedStateSampler method.
98       *
99       * <p>Note that due to type erasure the type returned by the SharedStateSampler is not
100      * available at run-time and the LogNormalSampler has to assume it is the correct type.</p>
101      */
102     private static class BadSharedStateNormalizedGaussianSampler
103             implements NormalizedGaussianSampler, SharedStateSampler<Integer> {
104         @Override
105         public double sample() {
106             return 0;
107         }
108 
109         @Override
110         public Integer withUniformRandomProvider(UniformRandomProvider rng) {
111             // Something that is not a NormalizedGaussianSampler
112             return Integer.valueOf(44);
113         }
114     }
115 }