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.junit.Test;
20 import org.apache.commons.rng.UniformRandomProvider;
21 import org.apache.commons.rng.sampling.RandomAssert;
22 import org.apache.commons.rng.simple.RandomSource;
23
24
25
26
27 public class ZigguratNormalizedGaussianSamplerTest {
28
29 @Test(expected = StackOverflowError.class)
30 public void testInfiniteLoop() {
31
32
33 final UniformRandomProvider bad = new UniformRandomProvider() {
34
35 public long nextLong(long n) { return 0; }
36 public long nextLong() { return Long.MAX_VALUE; }
37 public int nextInt(int n) { return 0; }
38 public int nextInt() { return Integer.MAX_VALUE; }
39 public float nextFloat() { return 1; }
40 public double nextDouble() { return 1;}
41 public void nextBytes(byte[] bytes, int start, int len) {}
42 public void nextBytes(byte[] bytes) {}
43 public boolean nextBoolean() { return false; }
44
45 };
46
47
48 new ZigguratNormalizedGaussianSampler(bad).sample();
49 }
50
51
52
53
54 @Test
55 public void testSharedStateSampler() {
56 final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
57 final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
58 final SharedStateContinuousSampler sampler1 =
59 ZigguratNormalizedGaussianSampler.<ZigguratNormalizedGaussianSampler>of(rng1);
60 final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
61 RandomAssert.assertProduceSameSequence(sampler1, sampler2);
62 }
63 }