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.apache.commons.lang3.LangAssertions.assertIllegalArgumentException;
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNotEquals;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.util.stream.Stream;
27
28 import org.junit.jupiter.api.Test;
29 import org.junit.jupiter.params.ParameterizedTest;
30 import org.junit.jupiter.params.provider.MethodSource;
31
32
33
34
35 class RandomUtilsTest extends AbstractLangTest {
36
37
38
39
40 private static final double DELTA = 1e-5;
41
42 static Stream<RandomUtils> randomProvider() {
43 return Stream.of(RandomUtils.secure(), RandomUtils.secureStrong(), RandomUtils.insecure());
44 }
45
46
47
48
49 @Test
50 void testBoolean() {
51 final boolean result = RandomUtils.nextBoolean();
52 assertTrue(result || !result);
53 }
54
55 @ParameterizedTest
56 @MethodSource("randomProvider")
57 void testBoolean(final RandomUtils ru) {
58 final boolean result = ru.randomBoolean();
59 assertTrue(result || !result);
60 }
61
62 @Test
63 void testConstructor() {
64 assertNotNull(new RandomUtils());
65 }
66
67
68
69
70 @Test
71 void testExtremeRangeDouble() {
72 final double result = RandomUtils.nextDouble(0, Double.MAX_VALUE);
73 assertTrue(result >= 0 && result <= Double.MAX_VALUE);
74 }
75
76 @ParameterizedTest
77 @MethodSource("randomProvider")
78 void testExtremeRangeDouble(final RandomUtils ru) {
79 final double result = ru.randomDouble(0, Double.MAX_VALUE);
80 assertTrue(result >= 0 && result <= Double.MAX_VALUE);
81 }
82
83
84
85
86 @Test
87 void testExtremeRangeFloat() {
88 final float result = RandomUtils.nextFloat(0, Float.MAX_VALUE);
89 assertTrue(result >= 0f && result <= Float.MAX_VALUE);
90 }
91
92
93
94
95 @ParameterizedTest
96 @MethodSource("randomProvider")
97 void testExtremeRangeFloat(final RandomUtils ru) {
98 final float result = ru.randomFloat(0, Float.MAX_VALUE);
99 assertTrue(result >= 0f && result <= Float.MAX_VALUE);
100 }
101
102
103
104
105 @Test
106 void testExtremeRangeInt() {
107 final int result = RandomUtils.nextInt(0, Integer.MAX_VALUE);
108 assertTrue(result >= 0);
109 assertTrue(result < Integer.MAX_VALUE);
110 }
111
112
113
114
115 @ParameterizedTest
116 @MethodSource("randomProvider")
117 void testExtremeRangeInt(final RandomUtils ru) {
118 final int result = ru.randomInt(0, Integer.MAX_VALUE);
119 assertTrue(result >= 0);
120 assertTrue(result < Integer.MAX_VALUE);
121 }
122
123
124
125
126 @Test
127 void testExtremeRangeLong() {
128 final long result = RandomUtils.nextLong(0, Long.MAX_VALUE);
129 assertTrue(result >= 0);
130 assertTrue(result < Long.MAX_VALUE);
131 }
132
133
134
135
136 @ParameterizedTest
137 @MethodSource("randomProvider")
138 void testExtremeRangeLong(final RandomUtils ru) {
139 final long result = ru.randomLong(0, Long.MAX_VALUE);
140 assertTrue(result >= 0);
141 assertTrue(result < Long.MAX_VALUE);
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 @Test
158 void testLargeValueRangeLong() {
159 final long startInclusive = 12900000000001L;
160 final long endExclusive = 12900000000016L;
161
162
163
164 final int n = (int) (endExclusive - startInclusive) * 1000;
165 for (int i = 0; i < n; i++) {
166 assertNotEquals(endExclusive, RandomUtils.nextLong(startInclusive, endExclusive));
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 @ParameterizedTest
184 @MethodSource("randomProvider")
185 void testLargeValueRangeLong(final RandomUtils ru) {
186 final long startInclusive = 12900000000001L;
187 final long endExclusive = 12900000000016L;
188
189
190
191 final int n = (int) (endExclusive - startInclusive) * 1000;
192 for (int i = 0; i < n; i++) {
193 assertNotEquals(endExclusive, ru.randomLong(startInclusive, endExclusive));
194 }
195 }
196
197
198
199
200 @Test
201 void testNextBytes() {
202 final byte[] result = RandomUtils.nextBytes(20);
203 assertEquals(20, result.length);
204 }
205
206
207
208
209 @ParameterizedTest
210 @MethodSource("randomProvider")
211 void testNextBytes(final RandomUtils ru) {
212 final byte[] result = ru.randomBytes(20);
213 assertEquals(20, result.length);
214 }
215
216 @Test
217 void testNextBytesNegative() {
218 assertIllegalArgumentException(() -> RandomUtils.nextBytes(-1));
219 }
220
221 @ParameterizedTest
222 @MethodSource("randomProvider")
223 void testNextBytesNegative(final RandomUtils ru) {
224 assertIllegalArgumentException(() -> ru.randomBytes(-1));
225 }
226
227
228
229
230 @Test
231 void testNextDouble() {
232 final double result = RandomUtils.nextDouble(33d, 42d);
233 assertTrue(result >= 33d);
234 assertTrue(result < 42d);
235 }
236
237
238
239
240 @ParameterizedTest
241 @MethodSource("randomProvider")
242 void testNextDouble(final RandomUtils ru) {
243 final double result = ru.randomDouble(33d, 42d);
244 assertTrue(result >= 33d);
245 assertTrue(result < 42d);
246 }
247
248 @Test
249 void testNextDoubleLowerGreaterUpper() {
250 assertIllegalArgumentException(() -> RandomUtils.nextDouble(2, 1));
251 }
252
253 @ParameterizedTest
254 @MethodSource("randomProvider")
255 void testNextDoubleLowerGreaterUpper(final RandomUtils ru) {
256 assertIllegalArgumentException(() -> ru.randomDouble(2, 1));
257 }
258
259
260
261
262 @Test
263 void testNextDoubleMinimalRange() {
264 assertEquals(42.1, RandomUtils.nextDouble(42.1, 42.1), DELTA);
265 }
266
267
268
269
270 @ParameterizedTest
271 @MethodSource("randomProvider")
272 void testNextDoubleMinimalRange(final RandomUtils ru) {
273 assertEquals(42.1, ru.randomDouble(42.1, 42.1), DELTA);
274 }
275
276 @Test
277 void testNextDoubleNegative() {
278 assertIllegalArgumentException(() -> RandomUtils.nextDouble(-1, 1));
279 }
280
281 @ParameterizedTest
282 @MethodSource("randomProvider")
283 void testNextDoubleNegative(final RandomUtils ru) {
284 assertIllegalArgumentException(() -> ru.randomDouble(-1, 1));
285 }
286
287
288
289
290 @Test
291 void testNextDoubleRandomResult() {
292 final double result = RandomUtils.nextDouble();
293 assertTrue(result >= 0d);
294 assertTrue(result < Double.MAX_VALUE);
295 }
296
297
298
299
300 @ParameterizedTest
301 @MethodSource("randomProvider")
302 void testNextDoubleRandomResult(final RandomUtils ru) {
303 final double result = ru.randomDouble();
304 assertTrue(result >= 0d);
305 assertTrue(result < Double.MAX_VALUE);
306 }
307
308
309
310
311 @Test
312 void testNextFloat() {
313 final float result = RandomUtils.nextFloat(33f, 42f);
314 assertTrue(result >= 33f);
315 assertTrue(result < 42f);
316 }
317
318
319
320
321 @ParameterizedTest
322 @MethodSource("randomProvider")
323 void testNextFloat(final RandomUtils ru) {
324 final float result = ru.randomFloat(33f, 42f);
325 assertTrue(result >= 33f);
326 assertTrue(result < 42f);
327 }
328
329 @Test
330 void testNextFloatLowerGreaterUpper() {
331 assertIllegalArgumentException(() -> RandomUtils.nextFloat(2, 1));
332 }
333
334 @ParameterizedTest
335 @MethodSource("randomProvider")
336 void testNextFloatLowerGreaterUpper(final RandomUtils ru) {
337 assertIllegalArgumentException(() -> ru.randomFloat(2, 1));
338 }
339
340
341
342
343 @Test
344 void testNextFloatMinimalRange() {
345 assertEquals(42.1f, RandomUtils.nextFloat(42.1f, 42.1f), DELTA);
346 }
347
348
349
350
351 @ParameterizedTest
352 @MethodSource("randomProvider")
353 void testNextFloatMinimalRange(final RandomUtils ru) {
354 assertEquals(42.1f, ru.randomFloat(42.1f, 42.1f), DELTA);
355 }
356
357 @Test
358 void testNextFloatNegative() {
359 assertIllegalArgumentException(() -> RandomUtils.nextFloat(-1, 1));
360 }
361
362 @ParameterizedTest
363 @MethodSource("randomProvider")
364 void testNextFloatNegative(final RandomUtils ru) {
365 assertIllegalArgumentException(() -> ru.randomFloat(-1, 1));
366 }
367
368
369
370
371 @Test
372 void testNextFloatRandomResult() {
373 final float result = RandomUtils.nextFloat();
374 assertTrue(result >= 0f);
375 assertTrue(result < Float.MAX_VALUE);
376 }
377
378
379
380
381 @ParameterizedTest
382 @MethodSource("randomProvider")
383 void testNextFloatRandomResult(final RandomUtils ru) {
384 final float result = ru.randomFloat();
385 assertTrue(result >= 0f);
386 assertTrue(result < Float.MAX_VALUE);
387 }
388
389
390
391
392 @Test
393 void testNextInt() {
394 final int result = RandomUtils.nextInt(33, 42);
395 assertTrue(result >= 33);
396 assertTrue(result < 42);
397 }
398
399
400
401
402 @ParameterizedTest
403 @MethodSource("randomProvider")
404 void testNextInt(final RandomUtils ru) {
405 final int result = ru.randomInt(33, 42);
406 assertTrue(result >= 33);
407 assertTrue(result < 42);
408 }
409
410 @Test
411 void testNextIntLowerGreaterUpper() {
412 assertIllegalArgumentException(() -> RandomUtils.nextInt(2, 1));
413 }
414
415 @ParameterizedTest
416 @MethodSource("randomProvider")
417 void testNextIntLowerGreaterUpper(final RandomUtils ru) {
418 assertIllegalArgumentException(() -> ru.randomInt(2, 1));
419 }
420
421
422
423
424 @Test
425 void testNextIntMinimalRange() {
426 assertEquals(42, RandomUtils.nextInt(42, 42));
427 }
428
429
430
431
432 @ParameterizedTest
433 @MethodSource("randomProvider")
434 void testNextIntMinimalRange(final RandomUtils ru) {
435 assertEquals(42, ru.randomInt(42, 42));
436 }
437
438 @Test
439 void testNextIntNegative() {
440 assertIllegalArgumentException(() -> RandomUtils.nextInt(-1, 1));
441 }
442
443 @ParameterizedTest
444 @MethodSource("randomProvider")
445 void testNextIntNegative(final RandomUtils ru) {
446 assertIllegalArgumentException(() -> ru.randomInt(-1, 1));
447 }
448
449
450
451
452 @Test
453 void testNextIntRandomResult() {
454 final int randomResult = RandomUtils.nextInt();
455 assertTrue(randomResult > 0);
456 assertTrue(randomResult < Integer.MAX_VALUE);
457 }
458
459
460
461
462 @ParameterizedTest
463 @MethodSource("randomProvider")
464 void testNextIntRandomResult(final RandomUtils ru) {
465 final int randomResult = ru.randomInt();
466 assertTrue(randomResult > 0);
467 assertTrue(randomResult < Integer.MAX_VALUE);
468 }
469
470
471
472
473 @Test
474 void testNextLong() {
475 final long result = RandomUtils.nextLong(33L, 42L);
476 assertTrue(result >= 33L);
477 assertTrue(result < 42L);
478 }
479
480
481
482
483 @ParameterizedTest
484 @MethodSource("randomProvider")
485 void testNextLong(final RandomUtils ru) {
486 final long result = ru.randomLong(33L, 42L);
487 assertTrue(result >= 33L);
488 assertTrue(result < 42L);
489 }
490
491 @Test
492 void testNextLongLowerGreaterUpper() {
493 assertIllegalArgumentException(() -> RandomUtils.nextLong(2, 1));
494 }
495
496 @ParameterizedTest
497 @MethodSource("randomProvider")
498 void testNextLongLowerGreaterUpper(final RandomUtils ru) {
499 assertIllegalArgumentException(() -> ru.randomLong(2, 1));
500 }
501
502
503
504
505 @Test
506 void testNextLongMinimalRange() {
507 assertEquals(42L, RandomUtils.nextLong(42L, 42L));
508 }
509
510
511
512
513 @ParameterizedTest
514 @MethodSource("randomProvider")
515 void testNextLongMinimalRange(final RandomUtils ru) {
516 assertEquals(42L, ru.randomLong(42L, 42L));
517 }
518
519 @Test
520 void testNextLongNegative() {
521 assertIllegalArgumentException(() -> RandomUtils.nextLong(-1, 1));
522 }
523
524 @ParameterizedTest
525 @MethodSource("randomProvider")
526 void testNextLongNegative(final RandomUtils ru) {
527 assertIllegalArgumentException(() -> ru.randomLong(-1, 1));
528 }
529
530
531
532
533 @Test
534 void testNextLongRandomResult() {
535 final long result = RandomUtils.nextLong();
536 assertTrue(result >= 0L);
537 assertTrue(result < Long.MAX_VALUE);
538 }
539
540
541
542
543 @ParameterizedTest
544 @MethodSource("randomProvider")
545 void testNextLongRandomResult(final RandomUtils ru) {
546 final long result = ru.randomLong();
547 assertTrue(result >= 0L);
548 assertTrue(result < Long.MAX_VALUE);
549 }
550
551
552
553
554 @Test
555 void testZeroLengthNextBytes() {
556 assertArrayEquals(new byte[0], RandomUtils.nextBytes(0));
557 }
558
559
560
561
562 @ParameterizedTest
563 @MethodSource("randomProvider")
564 void testZeroLengthNextBytes(final RandomUtils ru) {
565 assertArrayEquals(new byte[0], ru.randomBytes(0));
566 }
567 }