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