This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-rng.git
The following commit(s) were added to refs/heads/master by this push: new c7779b1 Add edge case where the generator outputs all bits set. c7779b1 is described below commit c7779b1e32ce3533d2f9f440b46106bb1907be46 Author: aherbert <aherb...@apache.org> AuthorDate: Fri Jun 11 13:05:02 2021 +0100 Add edge case where the generator outputs all bits set. --- .../AhrensDieterExponentialSamplerTest.java | 23 +++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSamplerTest.java index d7b81a8..8ce972d 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/AhrensDieterExponentialSamplerTest.java @@ -22,6 +22,7 @@ import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.core.source64.SplitMix64; import org.apache.commons.rng.sampling.RandomAssert; import org.apache.commons.rng.simple.RandomSource; +import org.junit.Assert; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; @@ -75,6 +76,26 @@ public class AhrensDieterExponentialSamplerTest { }; final SharedStateContinuousSampler sampler = AhrensDieterExponentialSampler.of(rng, 1); // This should not infinite loop - sampler.sample(); + final double x = sampler.sample(); + Assert.assertTrue(x >= 0); + } + + /** + * Test the sampler is robust to a generator that outputs full bits. The uniform random + * double will be at the top of the range {@code [0, 1]}. + */ + @Test + public void testSamplerWithOneFromRandomGenerator() { + // A broken generator that returns zero. + final UniformRandomProvider rng = new SplitMix64(0) { + @Override + public long nextLong() { + // All the bits set + return -1; + } + }; + final SharedStateContinuousSampler sampler = AhrensDieterExponentialSampler.of(rng, 1); + final double x = sampler.sample(); + Assert.assertTrue(x >= 0); } }