Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-237: Rename RandomInts to RandomNumbers and add methods operating …
…on longs.
- Loading branch information
Showing
9 changed files
with
195 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 0 additions & 33 deletions
33
...omized-runner/src/main/java/com/carrotsearch/randomizedtesting/generators/RandomInts.java
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
...zed-runner/src/main/java/com/carrotsearch/randomizedtesting/generators/RandomNumbers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.carrotsearch.randomizedtesting.generators; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* Utility classes for selecting random numbers from within a range or the | ||
* numeric domain for a given type. | ||
*/ | ||
public final class RandomNumbers { | ||
/** | ||
* A random integer between <code>min</code> (inclusive) and <code>max</code> (inclusive). | ||
*/ | ||
public static int randomIntBetween(Random r, int min, int max) { | ||
assert max >= min : "max must be >= min: " + min + ", " + max; | ||
long range = (long) max - (long) min; | ||
if (range < Integer.MAX_VALUE) { | ||
return min + r.nextInt(1 + (int) range); | ||
} else { | ||
return toIntExact(min + nextLong(r, 1 + range)); | ||
} | ||
} | ||
|
||
/** | ||
* A random long between <code>min</code> (inclusive) and <code>max</code> (inclusive). | ||
*/ | ||
public static long randomLongBetween(Random r, long min, long max) { | ||
assert max >= min : "max must be >= min: " + min + ", " + max; | ||
long range = max - min; | ||
if (range < 0) { | ||
range -= Long.MAX_VALUE; | ||
if (range == Long.MIN_VALUE) { | ||
// Full spectrum. | ||
return r.nextLong(); | ||
} else { | ||
long first = r.nextLong() & Long.MAX_VALUE; | ||
long second = range == Long.MAX_VALUE ? (r.nextLong() & Long.MAX_VALUE) : nextLong(r, range + 1); | ||
return min + first + second; | ||
} | ||
} else { | ||
long second = range == Long.MAX_VALUE ? (r.nextLong() & Long.MAX_VALUE) : nextLong(r, range + 1); | ||
return min + second; | ||
} | ||
} | ||
|
||
/** | ||
* Similar to {@link Random#nextInt(int)}, but returns a long between | ||
* 0 (inclusive) and <code>n</code> (exclusive). | ||
* | ||
* @param rnd Random generator. | ||
* @param n the bound on the random number to be returned. Must be | ||
* positive. | ||
* @return Returns a random number between 0 and n-1. | ||
*/ | ||
public static long nextLong(Random rnd, long n) { | ||
if (n <= 0) { | ||
throw new IllegalArgumentException("n <= 0: " + n); | ||
} | ||
|
||
long value = rnd.nextLong(); | ||
long range = n - 1; | ||
if ((n & range) == 0L) { | ||
value &= range; | ||
} else { | ||
for (long u = value >>> 1; u + range - (value = u % n) < 0L;) { | ||
u = rnd.nextLong() >>> 1; | ||
} | ||
} | ||
return value; | ||
} | ||
|
||
private static int toIntExact(long value) { | ||
if (value > Integer.MAX_VALUE) { | ||
throw new ArithmeticException("Overflow: " + value); | ||
} else { | ||
return (int) value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
randomized-runner/src/test/java/com/carrotsearch/randomizedtesting/TestRandomNumbers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.carrotsearch.randomizedtesting; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Random; | ||
|
||
import org.junit.Test; | ||
|
||
import com.carrotsearch.randomizedtesting.annotations.Repeat; | ||
import com.carrotsearch.randomizedtesting.generators.RandomNumbers; | ||
|
||
@Repeat(iterations = 100) | ||
public class TestRandomNumbers extends RandomizedTest { | ||
@Test | ||
public void testRandomIntBetween() { | ||
int max = Integer.MAX_VALUE; | ||
int min = Integer.MIN_VALUE; | ||
|
||
Random rnd = getRandom(); | ||
checkRandomInt(rnd, 1, 1); | ||
checkRandomInt(rnd, 0, 100); | ||
checkRandomInt(rnd, 0, max); | ||
checkRandomInt(rnd, min, 0); | ||
checkRandomInt(rnd, -1, max); | ||
checkRandomInt(rnd, min, 1); | ||
checkRandomInt(rnd, min, max); | ||
} | ||
|
||
@Test | ||
public void testRandomLongBetween() { | ||
long max = Long.MAX_VALUE; | ||
long min = Long.MIN_VALUE; | ||
|
||
Random rnd = getRandom(); | ||
checkRandomLong(rnd, 1, 1); | ||
checkRandomLong(rnd, 0, 100); | ||
checkRandomLong(rnd, 0, max); | ||
checkRandomLong(rnd, min, 0); | ||
checkRandomLong(rnd, -1, max); | ||
checkRandomLong(rnd, min, 1); | ||
checkRandomLong(rnd, min, max); | ||
} | ||
|
||
private void checkRandomInt(Random rnd, int min, int max) { | ||
for (int i = 0; i < 100000; i++) { | ||
int v = RandomNumbers.randomIntBetween(rnd, min, max); | ||
if (v < min || v > max) { | ||
fail(min + " " + max + ": " + v); | ||
} | ||
} | ||
} | ||
|
||
private void checkRandomLong(Random rnd, long min, long max) { | ||
for (int i = 0; i < 100000; i++) { | ||
long v = RandomNumbers.randomLongBetween(rnd, min, max); | ||
if (v < min || v > max) { | ||
fail(min + " " + max + ": " + v); | ||
} | ||
} | ||
} | ||
} |