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.apache.commons.rng.sampling.SharedStateSampler;
22 import org.junit.jupiter.api.Assertions;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28 class LogNormalSamplerTest {
29
30
31
32 @Test
33 void testConstructorThrowsWithZeroShape() {
34 final UniformRandomProvider rng = RandomAssert.seededRNG();
35 final NormalizedGaussianSampler gauss = ZigguratSampler.NormalizedGaussian.of(rng);
36 final double mu = 1;
37 final double sigma = 0;
38 Assertions.assertThrows(IllegalArgumentException.class,
39 () -> LogNormalSampler.of(gauss, mu, sigma));
40 }
41
42
43
44
45 @Test
46 void testSharedStateSampler() {
47 final UniformRandomProvider rng1 = RandomAssert.seededRNG();
48 final UniformRandomProvider rng2 = RandomAssert.seededRNG();
49 final NormalizedGaussianSampler gauss = ZigguratSampler.NormalizedGaussian.of(rng1);
50
51 final double mu = -1.23;
52 final double sigma = 4.56;
53 final SharedStateContinuousSampler sampler1 =
54 LogNormalSampler.of(gauss, mu, sigma);
55 final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
56 RandomAssert.assertProduceSameSequence(sampler1, sampler2);
57 }
58
59
60
61
62
63 @Test
64 void testSharedStateSamplerThrowsIfUnderlyingSamplerDoesNotShareState() {
65 final UniformRandomProvider rng2 = RandomAssert.seededRNG();
66 final NormalizedGaussianSampler gauss = new NormalizedGaussianSampler() {
67 @Override
68 public double sample() {
69 return 0;
70 }
71 };
72 final double mu = 1.23;
73 final double sigma = 4.56;
74 final SharedStateContinuousSampler sampler1 =
75 LogNormalSampler.of(gauss, mu, sigma);
76 Assertions.assertThrows(UnsupportedOperationException.class,
77 () -> sampler1.withUniformRandomProvider(rng2));
78 }
79
80
81
82
83
84 @Test
85 void testSharedStateSamplerThrowsIfUnderlyingSamplerReturnsWrongSharedState() {
86 final UniformRandomProvider rng2 = RandomAssert.seededRNG();
87 final NormalizedGaussianSampler gauss = new BadSharedStateNormalizedGaussianSampler();
88 final double mu = 1.23;
89 final double sigma = 4.56;
90 final SharedStateContinuousSampler sampler1 =
91 LogNormalSampler.of(gauss, mu, sigma);
92 Assertions.assertThrows(UnsupportedOperationException.class,
93 () -> sampler1.withUniformRandomProvider(rng2));
94 }
95
96
97
98
99
100
101
102 private static class BadSharedStateNormalizedGaussianSampler
103 implements NormalizedGaussianSampler, SharedStateSampler<Integer> {
104 @Override
105 public double sample() {
106 return 0;
107 }
108
109 @Override
110 public Integer withUniformRandomProvider(UniformRandomProvider rng) {
111
112 return Integer.valueOf(44);
113 }
114 }
115 }