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.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   * 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      /**
47       * Tests next boolean
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       * Tests extreme range.
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); // TODO: should be <max?
72      }
73  
74      /**
75       * Tests extreme range.
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); // TODO: should be <max?
81      }
82  
83      /**
84       * Tests extreme range.
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       * Tests extreme range.
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      * Test a large value for long. A previous implementation using
103      * {@link RandomUtils#nextDouble(double, double)} could generate a value equal
104      * to the upper limit.
105      *
106      * <pre>
107      * return (long) nextDouble(startInclusive, endExclusive);
108      * </pre>
109      *
110      * <p>See LANG-1592.</p>
111      */
112     @Test
113     public void testLargeValueRangeLong() {
114         final long startInclusive = 12900000000001L;
115         final long endExclusive = 12900000000016L;
116         // Note: The method using 'return (long) nextDouble(startInclusive, endExclusive)'
117         // takes thousands of calls to generate an error. This size loop fails most
118         // of the time with the previous method.
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      * Tests random byte array.
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      * Tests next double range.
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      * Test next double range with minimal range.
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      * Tests next double range, random result.
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      * Tests next float range.
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      * Test next float range with minimal range.
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      * Tests next float range, random result.
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      * Tests next int range.
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      * Test next int range with minimal range.
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      * Tests next int range, random result.
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      * Tests next long range.
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      * Test next long range with minimal range.
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      * Tests next long range, random result.
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      * Tests a zero byte array length.
286      */
287     @Test
288     public void testZeroLengthNextBytes() {
289         assertArrayEquals(new byte[0], RandomUtils.nextBytes(0));
290     }
291 }