View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Tests for {@link RandomUtils}
34   */
35  class RandomUtilsTest extends AbstractLangTest {
36  
37      /**
38       * For comparing doubles and floats
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       * Tests next boolean
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       * Tests extreme range.
69       */
70      @Test
71      void testExtremeRangeDouble() {
72          final double result = RandomUtils.nextDouble(0, Double.MAX_VALUE);
73          assertTrue(result >= 0 && result <= Double.MAX_VALUE); // TODO: should be <max?
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); // TODO: should be <max?
81      }
82  
83      /**
84       * Tests extreme range.
85       */
86      @Test
87      void testExtremeRangeFloat() {
88          final float result = RandomUtils.nextFloat(0, Float.MAX_VALUE);
89          assertTrue(result >= 0f && result <= Float.MAX_VALUE); // TODO: should be <max?
90      }
91  
92      /**
93       * Tests extreme range.
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); // TODO: should be <max?
100     }
101 
102     /**
103      * Tests extreme range.
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      * Tests extreme range.
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      * Tests extreme range.
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      * Tests extreme range.
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      * Test a large value for long. A previous implementation using
146      * {@link RandomUtils#nextDouble(double, double)} could generate a value equal
147      * to the upper limit.
148      *
149      * <pre>
150      * return (long) nextDouble(startInclusive, endExclusive);
151      * </pre>
152      *
153      * <p>See LANG-1592.</p>
154      */
155     @Test
156     void testLargeValueRangeLong() {
157         final long startInclusive = 12900000000001L;
158         final long endExclusive = 12900000000016L;
159         // Note: The method using 'return (long) nextDouble(startInclusive, endExclusive)'
160         // takes thousands of calls to generate an error. This size loop fails most
161         // of the time with the previous method.
162         final int n = (int) (endExclusive - startInclusive) * 1000;
163         for (int i = 0; i < n; i++) {
164             assertNotEquals(endExclusive, RandomUtils.nextLong(startInclusive, endExclusive));
165         }
166     }
167 
168     /**
169      * Test a large value for long. A previous implementation using
170      * {@link RandomUtils#nextDouble(double, double)} could generate a value equal
171      * to the upper limit.
172      *
173      * <pre>
174      * return (long) nextDouble(startInclusive, endExclusive);
175      * </pre>
176      *
177      * <p>See LANG-1592.</p>
178      */
179     @ParameterizedTest
180     @MethodSource("randomProvider")
181     void testLargeValueRangeLong(final RandomUtils ru) {
182         final long startInclusive = 12900000000001L;
183         final long endExclusive = 12900000000016L;
184         // Note: The method using 'return (long) nextDouble(startInclusive, endExclusive)'
185         // takes thousands of calls to generate an error. This size loop fails most
186         // of the time with the previous method.
187         final int n = (int) (endExclusive - startInclusive) * 1000;
188         for (int i = 0; i < n; i++) {
189             assertNotEquals(endExclusive, ru.randomLong(startInclusive, endExclusive));
190         }
191     }
192 
193     /**
194      * Tests random byte array.
195      */
196     @Test
197     void testNextBytes() {
198         final byte[] result = RandomUtils.nextBytes(20);
199         assertEquals(20, result.length);
200     }
201 
202     /**
203      * Tests random byte array.
204      */
205     @ParameterizedTest
206     @MethodSource("randomProvider")
207     void testNextBytes(final RandomUtils ru) {
208         final byte[] result = ru.randomBytes(20);
209         assertEquals(20, result.length);
210     }
211 
212     @Test
213     void testNextBytesNegative() {
214         assertIllegalArgumentException(() -> RandomUtils.nextBytes(-1));
215     }
216 
217     @ParameterizedTest
218     @MethodSource("randomProvider")
219     void testNextBytesNegative(final RandomUtils ru) {
220         assertIllegalArgumentException(() -> ru.randomBytes(-1));
221     }
222 
223     /**
224      * Tests next double range.
225      */
226     @Test
227     void testNextDouble() {
228         final double result = RandomUtils.nextDouble(33d, 42d);
229         assertTrue(result >= 33d);
230         assertTrue(result < 42d);
231     }
232 
233     /**
234      * Tests next double range.
235      */
236     @ParameterizedTest
237     @MethodSource("randomProvider")
238     void testNextDouble(final RandomUtils ru) {
239         final double result = ru.randomDouble(33d, 42d);
240         assertTrue(result >= 33d);
241         assertTrue(result < 42d);
242     }
243 
244     @Test
245     void testNextDoubleLowerGreaterUpper() {
246         assertIllegalArgumentException(() -> RandomUtils.nextDouble(2, 1));
247     }
248 
249     @ParameterizedTest
250     @MethodSource("randomProvider")
251     void testNextDoubleLowerGreaterUpper(final RandomUtils ru) {
252         assertIllegalArgumentException(() -> ru.randomDouble(2, 1));
253     }
254 
255     /**
256      * Test next double range with minimal range.
257      */
258     @Test
259     void testNextDoubleMinimalRange() {
260         assertEquals(42.1, RandomUtils.nextDouble(42.1, 42.1), DELTA);
261     }
262 
263     /**
264      * Test next double range with minimal range.
265      */
266     @ParameterizedTest
267     @MethodSource("randomProvider")
268     void testNextDoubleMinimalRange(final RandomUtils ru) {
269         assertEquals(42.1, ru.randomDouble(42.1, 42.1), DELTA);
270     }
271 
272     @Test
273     void testNextDoubleNegative() {
274         assertIllegalArgumentException(() -> RandomUtils.nextDouble(-1, 1));
275     }
276 
277     @ParameterizedTest
278     @MethodSource("randomProvider")
279     void testNextDoubleNegative(final RandomUtils ru) {
280         assertIllegalArgumentException(() -> ru.randomDouble(-1, 1));
281     }
282 
283     /**
284      * Tests next double range, random result.
285      */
286     @Test
287     void testNextDoubleRandomResult() {
288         final double result = RandomUtils.nextDouble();
289         assertTrue(result >= 0d);
290         assertTrue(result < Double.MAX_VALUE);
291     }
292 
293     /**
294      * Tests next double range, random result.
295      */
296     @ParameterizedTest
297     @MethodSource("randomProvider")
298     void testNextDoubleRandomResult(final RandomUtils ru) {
299         final double result = ru.randomDouble();
300         assertTrue(result >= 0d);
301         assertTrue(result < Double.MAX_VALUE);
302     }
303 
304     /**
305      * Tests next float range.
306      */
307     @Test
308     void testNextFloat() {
309         final float result = RandomUtils.nextFloat(33f, 42f);
310         assertTrue(result >= 33f);
311         assertTrue(result < 42f);
312     }
313 
314     /**
315      * Tests next float range.
316      */
317     @ParameterizedTest
318     @MethodSource("randomProvider")
319     void testNextFloat(final RandomUtils ru) {
320         final float result = ru.randomFloat(33f, 42f);
321         assertTrue(result >= 33f);
322         assertTrue(result < 42f);
323     }
324 
325     @Test
326     void testNextFloatLowerGreaterUpper() {
327         assertIllegalArgumentException(() -> RandomUtils.nextFloat(2, 1));
328     }
329 
330     @ParameterizedTest
331     @MethodSource("randomProvider")
332     void testNextFloatLowerGreaterUpper(final RandomUtils ru) {
333         assertIllegalArgumentException(() -> ru.randomFloat(2, 1));
334     }
335 
336     /**
337      * Test next float range with minimal range.
338      */
339     @Test
340     void testNextFloatMinimalRange() {
341         assertEquals(42.1f, RandomUtils.nextFloat(42.1f, 42.1f), DELTA);
342     }
343 
344     /**
345      * Test next float range with minimal range.
346      */
347     @ParameterizedTest
348     @MethodSource("randomProvider")
349     void testNextFloatMinimalRange(final RandomUtils ru) {
350         assertEquals(42.1f, ru.randomFloat(42.1f, 42.1f), DELTA);
351     }
352 
353     @Test
354     void testNextFloatNegative() {
355         assertIllegalArgumentException(() -> RandomUtils.nextFloat(-1, 1));
356     }
357 
358     @ParameterizedTest
359     @MethodSource("randomProvider")
360     void testNextFloatNegative(final RandomUtils ru) {
361         assertIllegalArgumentException(() -> ru.randomFloat(-1, 1));
362     }
363 
364     /**
365      * Tests next float range, random result.
366      */
367     @Test
368     void testNextFloatRandomResult() {
369         final float result = RandomUtils.nextFloat();
370         assertTrue(result >= 0f);
371         assertTrue(result < Float.MAX_VALUE);
372     }
373 
374     /**
375      * Tests next float range, random result.
376      */
377     @ParameterizedTest
378     @MethodSource("randomProvider")
379     void testNextFloatRandomResult(final RandomUtils ru) {
380         final float result = ru.randomFloat();
381         assertTrue(result >= 0f);
382         assertTrue(result < Float.MAX_VALUE);
383     }
384 
385     /**
386      * Tests next int range.
387      */
388     @Test
389     void testNextInt() {
390         final int result = RandomUtils.nextInt(33, 42);
391         assertTrue(result >= 33);
392         assertTrue(result < 42);
393     }
394 
395     /**
396      * Tests next int range.
397      */
398     @ParameterizedTest
399     @MethodSource("randomProvider")
400     void testNextInt(final RandomUtils ru) {
401         final int result = ru.randomInt(33, 42);
402         assertTrue(result >= 33);
403         assertTrue(result < 42);
404     }
405 
406     @Test
407     void testNextIntLowerGreaterUpper() {
408         assertIllegalArgumentException(() -> RandomUtils.nextInt(2, 1));
409     }
410 
411     @ParameterizedTest
412     @MethodSource("randomProvider")
413     void testNextIntLowerGreaterUpper(final RandomUtils ru) {
414         assertIllegalArgumentException(() -> ru.randomInt(2, 1));
415     }
416 
417     /**
418      * Test next int range with minimal range.
419      */
420     @Test
421     void testNextIntMinimalRange() {
422         assertEquals(42, RandomUtils.nextInt(42, 42));
423     }
424 
425     /**
426      * Test next int range with minimal range.
427      */
428     @ParameterizedTest
429     @MethodSource("randomProvider")
430     void testNextIntMinimalRange(final RandomUtils ru) {
431         assertEquals(42, ru.randomInt(42, 42));
432     }
433 
434     @Test
435     void testNextIntNegative() {
436         assertIllegalArgumentException(() -> RandomUtils.nextInt(-1, 1));
437     }
438 
439     @ParameterizedTest
440     @MethodSource("randomProvider")
441     void testNextIntNegative(final RandomUtils ru) {
442         assertIllegalArgumentException(() -> ru.randomInt(-1, 1));
443     }
444 
445     /**
446      * Tests next int range, random result.
447      */
448     @Test
449     void testNextIntRandomResult() {
450         final int randomResult = RandomUtils.nextInt();
451         assertTrue(randomResult > 0);
452         assertTrue(randomResult < Integer.MAX_VALUE);
453     }
454 
455     /**
456      * Tests next int range, random result.
457      */
458     @ParameterizedTest
459     @MethodSource("randomProvider")
460     void testNextIntRandomResult(final RandomUtils ru) {
461         final int randomResult = ru.randomInt();
462         assertTrue(randomResult > 0);
463         assertTrue(randomResult < Integer.MAX_VALUE);
464     }
465 
466     /**
467      * Tests next long range.
468      */
469     @Test
470     void testNextLong() {
471         final long result = RandomUtils.nextLong(33L, 42L);
472         assertTrue(result >= 33L);
473         assertTrue(result < 42L);
474     }
475 
476     /**
477      * Tests next long range.
478      */
479     @ParameterizedTest
480     @MethodSource("randomProvider")
481     void testNextLong(final RandomUtils ru) {
482         final long result = ru.randomLong(33L, 42L);
483         assertTrue(result >= 33L);
484         assertTrue(result < 42L);
485     }
486 
487     @Test
488     void testNextLongLowerGreaterUpper() {
489         assertIllegalArgumentException(() -> RandomUtils.nextLong(2, 1));
490     }
491 
492     @ParameterizedTest
493     @MethodSource("randomProvider")
494     void testNextLongLowerGreaterUpper(final RandomUtils ru) {
495         assertIllegalArgumentException(() -> ru.randomLong(2, 1));
496     }
497 
498     /**
499      * Test next long range with minimal range.
500      */
501     @Test
502     void testNextLongMinimalRange() {
503         assertEquals(42L, RandomUtils.nextLong(42L, 42L));
504     }
505 
506     /**
507      * Test next long range with minimal range.
508      */
509     @ParameterizedTest
510     @MethodSource("randomProvider")
511     void testNextLongMinimalRange(final RandomUtils ru) {
512         assertEquals(42L, ru.randomLong(42L, 42L));
513     }
514 
515     @Test
516     void testNextLongNegative() {
517         assertIllegalArgumentException(() -> RandomUtils.nextLong(-1, 1));
518     }
519 
520     @ParameterizedTest
521     @MethodSource("randomProvider")
522     void testNextLongNegative(final RandomUtils ru) {
523         assertIllegalArgumentException(() -> ru.randomLong(-1, 1));
524     }
525 
526     /**
527      * Tests next long range, random result.
528      */
529     @Test
530     void testNextLongRandomResult() {
531         final long result = RandomUtils.nextLong();
532         assertTrue(result >= 0L);
533         assertTrue(result < Long.MAX_VALUE);
534     }
535 
536     /**
537      * Tests next long range, random result.
538      */
539     @ParameterizedTest
540     @MethodSource("randomProvider")
541     void testNextLongRandomResult(final RandomUtils ru) {
542         final long result = ru.randomLong();
543         assertTrue(result >= 0L);
544         assertTrue(result < Long.MAX_VALUE);
545     }
546 
547     /**
548      * Tests a zero byte array length.
549      */
550     @Test
551     void testZeroLengthNextBytes() {
552         assertArrayEquals(new byte[0], RandomUtils.nextBytes(0));
553     }
554 
555     /**
556      * Tests a zero byte array length.
557      */
558     @ParameterizedTest
559     @MethodSource("randomProvider")
560     void testZeroLengthNextBytes(final RandomUtils ru) {
561         assertArrayEquals(new byte[0], ru.randomBytes(0));
562     }
563 }