You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2022/01/05 14:54:22 UTC

[lucene] 01/02: LUCENE-10343: Remove MyRandom in favor of test framework random (#573)

This is an automated email from the ASF dual-hosted git repository.

jpountz pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit 76d83507beddcc421fc1906e0be4562e16531819
Author: gf2121 <52...@users.noreply.github.com>
AuthorDate: Wed Jan 5 22:31:00 2022 +0800

    LUCENE-10343: Remove MyRandom in favor of test framework random (#573)
---
 .../packed/TestLegacyDirectPacked.java             | 27 +++++-----------------
 .../lucene/util/packed/TestDirectPacked.java       | 26 ++++-----------------
 2 files changed, 11 insertions(+), 42 deletions(-)

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 63c6952..d571e5e 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;
@@ -26,6 +26,7 @@ import org.apache.lucene.store.IndexOutput;
 import org.apache.lucene.tests.util.LuceneTestCase;
 import org.apache.lucene.tests.util.TestUtil;
 import org.apache.lucene.util.LongValues;
+import org.apache.lucene.util.packed.PackedInts;
 
 public class TestLegacyDirectPacked extends LuceneTestCase {
 
@@ -94,7 +95,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);
@@ -122,29 +123,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 b4e04e8..3ed18de 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);
-    }
-  }
 }