1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.random;
23
24 import java.util.Random;
25
26 import org.hipparchus.exception.NullArgumentException;
27 import org.junit.Assert;
28 import org.junit.Test;
29
30
31
32
33 public class RandomAdaptorTest {
34
35 @Test
36 public void testAdaptor() {
37 ConstantGenerator generator = new ConstantGenerator();
38 Random random = RandomAdaptor.of(generator);
39 checkConstant(random);
40 RandomAdaptor randomAdaptor = new RandomAdaptor(generator);
41 checkConstant(randomAdaptor);
42 }
43
44 private void checkConstant(Random random) {
45 byte[] bytes = new byte[] {0};
46 random.nextBytes(bytes);
47 Assert.assertEquals(0, bytes[0]);
48 Assert.assertFalse(random.nextBoolean());
49 Assert.assertEquals(0, random.nextDouble(), 0);
50 Assert.assertEquals(0, random.nextFloat(), 0);
51 Assert.assertEquals(0, random.nextGaussian(), 0);
52 Assert.assertEquals(0, random.nextInt());
53 Assert.assertEquals(0, random.nextInt(1));
54 Assert.assertEquals(0, random.nextLong());
55 random.setSeed(100);
56 Assert.assertEquals(0, random.nextDouble(), 0);
57 }
58
59 @SuppressWarnings("unused")
60 @Test(expected=NullArgumentException.class)
61 public void testNullGenerator(){
62 RandomGenerator nullGenerator = null;
63 Random random = new RandomAdaptor(nullGenerator);
64 }
65
66 @SuppressWarnings("unused")
67 @Test(expected=NullArgumentException.class)
68 public void testNullGenerator2(){
69 RandomGenerator nullGenerator = null;
70 Random random = RandomAdaptor.of(nullGenerator);
71 }
72
73
74
75
76
77
78 public static class ConstantGenerator implements RandomGenerator {
79
80 private final double value;
81
82 public ConstantGenerator() {
83 value = 0;
84 }
85
86 public ConstantGenerator(double value) {
87 this.value = value;
88 }
89
90 @Override
91 public boolean nextBoolean() {
92 return false;
93 }
94
95 @Override
96 public void nextBytes(byte[] bytes) {
97 }
98
99 @Override
100 public void nextBytes(byte[] bytes, int offset, int len) {
101 }
102
103 @Override
104 public double nextDouble() {
105 return value;
106 }
107
108 @Override
109 public float nextFloat() {
110 return (float) value;
111 }
112
113 @Override
114 public double nextGaussian() {
115 return value;
116 }
117
118 @Override
119 public int nextInt() {
120 return (int) value;
121 }
122
123 @Override
124 public int nextInt(int n) {
125 return (int) value;
126 }
127
128 @Override
129 public long nextLong() {
130 return (int) value;
131 }
132
133 @Override
134 public long nextLong(long n) {
135 return (int) value;
136 }
137
138 @Override
139 public void setSeed(int seed) {
140 }
141
142 @Override
143 public void setSeed(int[] seed) {
144 }
145
146 @Override
147 public void setSeed(long seed) {
148 }
149
150 }
151 }