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  import org.junit.jupiter.params.ParameterizedTest;
24  import org.junit.jupiter.params.provider.ValueSource;
25  
26  /**
27   * Test for the {@link SmallMeanPoissonSampler}. The tests hit edge cases for the sampler.
28   */
29  class SmallMeanPoissonSamplerTest {
30      /**
31       * Test the constructor with a bad mean.
32       */
33      @Test
34      void testConstructorThrowsWithMeanThatSetsProbabilityP0ToZero() {
35          final UniformRandomProvider rng =
36              RandomAssert.seededRNG();
37          final double p0 = Double.MIN_VALUE;
38          // Note: p0 = Math.exp(-mean) => mean = -Math.log(p0).
39          // Add to the limit on the mean to cause p0 to be zero.
40          final double mean = -Math.log(p0) + 1;
41          Assertions.assertThrows(IllegalArgumentException.class,
42              () -> SmallMeanPoissonSampler.of(rng, mean));
43      }
44  
45      /**
46       * Test the constructor with a bad mean.
47       */
48      @Test
49      void testConstructorThrowsWithZeroMean() {
50          final UniformRandomProvider rng =
51              RandomAssert.seededRNG();
52          final double mean = 0;
53          Assertions.assertThrows(IllegalArgumentException.class,
54              () -> SmallMeanPoissonSampler.of(rng, mean));
55      }
56  
57      /**
58       * Test the sample is bounded to 1000 * mean.
59       */
60      @ParameterizedTest
61      @ValueSource(doubles = {0.5, 1, 1.5, 2.2})
62      void testSampleUpperBounds(double mean) {
63          // If the nextDouble() is always ~1 then the sample will hit the upper bounds.
64          // nextLong() returns -1; nextDouble returns Math.nextDown(1.0).
65          final UniformRandomProvider rng = () -> -1;
66          final SharedStateDiscreteSampler sampler = SmallMeanPoissonSampler.of(rng, mean);
67          final int expected = (int) Math.ceil(1000 * mean);
68          Assertions.assertEquals(expected, sampler.sample());
69      }
70  
71      /**
72       * Test the SharedStateSampler implementation.
73       */
74      @Test
75      void testSharedStateSampler() {
76          final UniformRandomProvider rng1 = RandomAssert.seededRNG();
77          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
78          final double mean = 1.23;
79          final SharedStateDiscreteSampler sampler1 =
80              SmallMeanPoissonSampler.of(rng1, mean);
81          final SharedStateDiscreteSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
82          RandomAssert.assertProduceSameSequence(sampler1, sampler2);
83      }
84  }