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