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 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
28
29 class SmallMeanPoissonSamplerTest {
30
31
32
33 @Test
34 void testConstructorThrowsWithMeanThatSetsProbabilityP0ToZero() {
35 final UniformRandomProvider rng =
36 RandomAssert.seededRNG();
37 final double p0 = Double.MIN_VALUE;
38
39
40 final double mean = -Math.log(p0) + 1;
41 Assertions.assertThrows(IllegalArgumentException.class,
42 () -> SmallMeanPoissonSampler.of(rng, mean));
43 }
44
45
46
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
59
60 @ParameterizedTest
61 @ValueSource(doubles = {0.5, 1, 1.5, 2.2})
62 void testSampleUpperBounds(double mean) {
63
64
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
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 }