1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.lang3;
18
19 import static org.hamcrest.MatcherAssert.assertThat;
20 import static org.hamcrest.Matchers.allOf;
21 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
22 import static org.hamcrest.Matchers.lessThan;
23 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import static org.junit.jupiter.api.Assertions.assertNotEquals;
27 import static org.junit.jupiter.api.Assertions.assertNotNull;
28 import static org.junit.jupiter.api.Assertions.assertThrows;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
30
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.Modifier;
33
34 import org.junit.jupiter.api.Test;
35
36
37
38
39 public class RandomUtilsTest extends AbstractLangTest {
40
41
42
43
44 private static final double DELTA = 1e-5;
45
46
47
48
49 @Test
50 public void testBoolean() {
51 final boolean result = RandomUtils.nextBoolean();
52 assertTrue(result || !result);
53 }
54
55 @Test
56 public void testConstructor() {
57 assertNotNull(new RandomUtils());
58 final Constructor<?>[] cons = RandomUtils.class.getDeclaredConstructors();
59 assertEquals(1, cons.length);
60 assertTrue(Modifier.isPublic(cons[0].getModifiers()));
61 assertTrue(Modifier.isPublic(RandomUtils.class.getModifiers()));
62 assertFalse(Modifier.isFinal(RandomUtils.class.getModifiers()));
63 }
64
65
66
67
68 @Test
69 public void testExtremeRangeDouble() {
70 final double result = RandomUtils.nextDouble(0, Double.MAX_VALUE);
71 assertTrue(result >= 0 && result <= Double.MAX_VALUE);
72 }
73
74
75
76
77 @Test
78 public void testExtremeRangeFloat() {
79 final float result = RandomUtils.nextFloat(0, Float.MAX_VALUE);
80 assertTrue(result >= 0f && result <= Float.MAX_VALUE);
81 }
82
83
84
85
86 @Test
87 public void testExtremeRangeInt() {
88 final int result = RandomUtils.nextInt(0, Integer.MAX_VALUE);
89 assertThat("result >= 0 && result < Integer.MAX_VALUE", result, allOf(greaterThanOrEqualTo(0), lessThan(Integer.MAX_VALUE)));
90 }
91
92
93
94
95 @Test
96 public void testExtremeRangeLong() {
97 final long result = RandomUtils.nextLong(0, Long.MAX_VALUE);
98 assertThat("result >= 0 && result < Long.MAX_VALUE", result, allOf(greaterThanOrEqualTo(0L), lessThan(Long.MAX_VALUE)));
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112 @Test
113 public void testLargeValueRangeLong() {
114 final long startInclusive = 12900000000001L;
115 final long endExclusive = 12900000000016L;
116
117
118
119 final int n = (int) (endExclusive - startInclusive) * 1000;
120 for (int i = 0; i < n; i++) {
121 assertNotEquals(endExclusive, RandomUtils.nextLong(startInclusive, endExclusive));
122 }
123 }
124
125
126
127
128 @Test
129 public void testNextBytes() {
130 final byte[] result = RandomUtils.nextBytes(20);
131 assertEquals(20, result.length);
132 }
133
134 @Test
135 public void testNextBytesNegative() {
136 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextBytes(-1));
137 }
138
139
140
141
142 @Test
143 public void testNextDouble() {
144 final double result = RandomUtils.nextDouble(33d, 42d);
145 assertThat("result >= 33d && result < 42d", result, allOf(greaterThanOrEqualTo(33d), lessThan(42d)));
146 }
147
148 @Test
149 public void testNextDoubleLowerGreaterUpper() {
150 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextDouble(2, 1));
151 }
152
153
154
155
156 @Test
157 public void testNextDoubleMinimalRange() {
158 assertEquals(42.1, RandomUtils.nextDouble(42.1, 42.1), DELTA);
159 }
160
161 @Test
162 public void testNextDoubleNegative() {
163 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextDouble(-1, 1));
164 }
165
166
167
168
169 @Test
170 public void testNextDoubleRandomResult() {
171 final double randomResult = RandomUtils.nextDouble();
172 assertThat("randomResult >= 0 0 && randomResult < Double.MAX_VALUE", randomResult, allOf(greaterThanOrEqualTo(0d), lessThan(Double.MAX_VALUE)));
173 }
174
175
176
177
178 @Test
179 public void testNextFloat() {
180 final float result = RandomUtils.nextFloat(33f, 42f);
181 assertThat("result >= 33f && result < 42f", result, allOf(greaterThanOrEqualTo(33f), lessThan(42f)));
182 }
183
184 @Test
185 public void testNextFloatLowerGreaterUpper() {
186 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextFloat(2, 1));
187 }
188
189
190
191
192 @Test
193 public void testNextFloatMinimalRange() {
194 assertEquals(42.1f, RandomUtils.nextFloat(42.1f, 42.1f), DELTA);
195 }
196
197 @Test
198 public void testNextFloatNegative() {
199 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextFloat(-1, 1));
200 }
201
202
203
204
205 @Test
206 public void testNextFloatRandomResult() {
207 final float randomResult = RandomUtils.nextFloat();
208 assertThat("randomResult >= 0 && randomResult < Double.MAX_VALUE", randomResult, allOf(greaterThanOrEqualTo(0f), lessThan(Float.MAX_VALUE)));
209 }
210
211
212
213
214 @Test
215 public void testNextInt() {
216 final int result = RandomUtils.nextInt(33, 42);
217 assertThat("result >= 33 && result < 42", result, allOf(greaterThanOrEqualTo(33), lessThan(42)));
218 }
219
220 @Test
221 public void testNextIntLowerGreaterUpper() {
222 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextInt(2, 1));
223 }
224
225
226
227
228 @Test
229 public void testNextIntMinimalRange() {
230 assertEquals(42, RandomUtils.nextInt(42, 42));
231 }
232
233 @Test
234 public void testNextIntNegative() {
235 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextInt(-1, 1));
236 }
237
238
239
240
241 @Test
242 public void testNextIntRandomResult() {
243 final int randomResult = RandomUtils.nextInt();
244 assertTrue(randomResult > 0);
245 assertTrue(randomResult < Integer.MAX_VALUE);
246 }
247
248
249
250
251 @Test
252 public void testNextLong() {
253 final long result = RandomUtils.nextLong(33L, 42L);
254 assertThat("result >= 33L && result < 42L", result, allOf(greaterThanOrEqualTo(33L), lessThan(42L)));
255 }
256
257 @Test
258 public void testNextLongLowerGreaterUpper() {
259 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextLong(2, 1));
260 }
261
262
263
264
265 @Test
266 public void testNextLongMinimalRange() {
267 assertEquals(42L, RandomUtils.nextLong(42L, 42L));
268 }
269
270 @Test
271 public void testNextLongNegative() {
272 assertThrows(IllegalArgumentException.class, () -> RandomUtils.nextLong(-1, 1));
273 }
274
275
276
277
278 @Test
279 public void testNextLongRandomResult() {
280 final long randomResult = RandomUtils.nextLong();
281 assertThat("randomResult >= 0 && randomResult < Long.MAX_VALUE", randomResult, allOf(greaterThanOrEqualTo(0L), lessThan(Long.MAX_VALUE)));
282 }
283
284
285
286
287 @Test
288 public void testZeroLengthNextBytes() {
289 assertArrayEquals(new byte[0], RandomUtils.nextBytes(0));
290 }
291 }