1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
28
29 class AhrensDieterExponentialSamplerTest {
30
31
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
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
56
57
58 @Test
59 void testSamplerWithZeroFromRandomGenerator() {
60
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
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
78
79
80 @Test
81 void testSamplerWithOneFromRandomGenerator() {
82
83 final UniformRandomProvider rng = new SplitMix64(0) {
84 @Override
85 public long nextLong() {
86
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 }