Skip to content

Apache Lucene open-source search software

LUCENE-10343: Remove MyRandom in favor of test framework random (#573)

gf2121238119224ad5
diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/packed/TestLegacyDirectPacked.java b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/packed/TestLegacyDirectPacked.java
index 0cbceb92685..154e529f681 100644
--- a/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/packed/TestLegacyDirectPacked.java
+++ b/lucene/backward-codecs/src/test/org/apache/lucene/backward_codecs/packed/TestLegacyDirectPacked.java
@@ -16,9 +16,9 @@
*/
package org.apache.lucene.backward_codecs.packed;
+import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
import java.util.Random;
import org.apache.lucene.backward_codecs.store.EndiannessReverserUtil;
-import org.apache.lucene.store.ByteArrayDataInput;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
@@ -27,6 +27,7 @@ import org.apache.lucene.tests.util.LuceneTestCase;
import org.apache.lucene.tests.util.LuceneTestCase.Nightly;
import org.apache.lucene.tests.util.TestUtil;
import org.apache.lucene.util.LongValues;
+import org.apache.lucene.util.packed.PackedInts;
@Nightly // N-2 formats are only tested on nightly runs
public class TestLegacyDirectPacked extends LuceneTestCase {
@@ -96,7 +97,7 @@ public class TestLegacyDirectPacked extends LuceneTestCase {
}
private void doTestBpv(Directory directory, int bpv, long offset) throws Exception {
- MyRandom random = new MyRandom(random().nextLong());
+ Random random = random();
int numIters = TEST_NIGHTLY ? 100 : 10;
for (int i = 0; i < numIters; i++) {
long[] original = randomLongs(random, bpv);
@@ -124,29 +125,13 @@ public class TestLegacyDirectPacked extends LuceneTestCase {
}
}
- private long[] randomLongs(MyRandom random, int bpv) {
+ private long[] randomLongs(Random random, int bpv) {
int amount = random.nextInt(5000);
long[] longs = new long[amount];
+ long max = PackedInts.maxValue(bpv);
for (int i = 0; i < longs.length; i++) {
- longs[i] = random.nextLong(bpv);
+ longs[i] = RandomNumbers.randomLongBetween(random, 0, max);
}
return longs;
}
-
- // java.util.Random only returns 48bits of randomness in nextLong...
- static class MyRandom extends Random {
- byte[] buffer = new byte[8];
- ByteArrayDataInput input = new ByteArrayDataInput();
-
- MyRandom(long seed) {
- super(seed);
- }
-
- public synchronized long nextLong(int bpv) {
- nextBytes(buffer);
- input.reset(buffer);
- long bits = input.readLong();
- return bits >>> (64 - bpv);
- }
- }
}
diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestDirectPacked.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestDirectPacked.java
index b4e04e8d862..3ed18de0fef 100644
--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestDirectPacked.java
+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestDirectPacked.java
@@ -16,8 +16,8 @@
*/
package org.apache.lucene.util.packed;
+import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
import java.util.Random;
-import org.apache.lucene.store.ByteArrayDataInput;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
@@ -111,7 +111,7 @@ public class TestDirectPacked extends LuceneTestCase {
private void doTestBpv(Directory directory, int bpv, long offset, boolean merge)
throws Exception {
- MyRandom random = new MyRandom(random().nextLong());
+ Random random = random();
int numIters = TEST_NIGHTLY ? 100 : 10;
for (int i = 0; i < numIters; i++) {
long[] original = randomLongs(random, bpv);
@@ -145,29 +145,13 @@ public class TestDirectPacked extends LuceneTestCase {
}
}
- private long[] randomLongs(MyRandom random, int bpv) {
+ private long[] randomLongs(Random random, int bpv) {
int amount = random.nextInt(5000);
long[] longs = new long[amount];
+ long max = PackedInts.maxValue(bpv);
for (int i = 0; i < longs.length; i++) {
- longs[i] = random.nextLong(bpv);
+ longs[i] = RandomNumbers.randomLongBetween(random, 0, max);
}
return longs;
}
-
- // java.util.Random only returns 48bits of randomness in nextLong...
- static class MyRandom extends Random {
- byte[] buffer = new byte[8];
- ByteArrayDataInput input = new ByteArrayDataInput();
-
- MyRandom(long seed) {
- super(seed);
- }
-
- public synchronized long nextLong(int bpv) {
- nextBytes(buffer);
- input.reset(buffer);
- long bits = input.readLong();
- return bits >>> (64 - bpv);
- }
- }
}