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 java.time.Duration;
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.apache.commons.rng.core.source64.SplitMix64;
22  import org.apache.commons.rng.sampling.RandomAssert;
23  import org.junit.jupiter.api.Assertions;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Test for the {@link AhrensDieterExponentialSampler}. The tests hit edge cases for the sampler.
28   */
29  class AhrensDieterExponentialSamplerTest {
30      /**
31       * Test the constructor with a bad mean.
32       */
33      @Test
34      void testConstructorThrowsWithZeroMean() {
35          final UniformRandomProvider rng = RandomAssert.seededRNG();
36          final double mean = 0;
37          Assertions.assertThrows(IllegalArgumentException.class, () -> AhrensDieterExponentialSampler.of(rng, mean));
38      }
39  
40      /**
41       * Test the SharedStateSampler implementation.
42       */
43      @Test
44      void testSharedStateSampler() {
45          final UniformRandomProvider rng1 = RandomAssert.seededRNG();
46          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
47          final double mean = 1.23;
48          final SharedStateContinuousSampler sampler1 =
49              AhrensDieterExponentialSampler.of(rng1, mean);
50          final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
51          RandomAssert.assertProduceSameSequence(sampler1, sampler2);
52      }
53  
54      /**
55       * Test the sampler is robust to a generator that outputs zeros.
56       * See RNG-144.
57       */
58      @Test
59      void testSamplerWithZeroFromRandomGenerator() {
60          // A broken generator that returns zero.
61          final UniformRandomProvider rng = new SplitMix64(0) {
62              @Override
63              public long nextLong() {
64                  return 0L;
65              }
66          };
67          final SharedStateContinuousSampler sampler = AhrensDieterExponentialSampler.of(rng, 1);
68          // This should not infinite loop
69          final double[] x = {-1};
70          Assertions.assertTimeout(Duration.ofMillis(50), () -> {
71              x[0] = sampler.sample();
72          });
73          Assertions.assertTrue(x[0] >= 0);
74      }
75  
76      /**
77       * Test the sampler is robust to a generator that outputs full bits. The uniform random
78       * double will be at the top of the range {@code [0, 1]}.
79       */
80      @Test
81      void testSamplerWithOneFromRandomGenerator() {
82          // A broken generator that returns all the bits set.
83          final UniformRandomProvider rng = new SplitMix64(0) {
84              @Override
85              public long nextLong() {
86                  // All the bits set
87                  return -1;
88              }
89          };
90          final SharedStateContinuousSampler sampler = AhrensDieterExponentialSampler.of(rng, 1);
91          final double x = sampler.sample();
92          Assertions.assertTrue(x >= 0);
93      }
94  }