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