Apache Lucene open-source search software
LUCENE-4148: Make _TestUtil able to generate random longs.
Adrien Granda94e7cd49e5c
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.javaindex 6eb3346867e..c8fcd9eda8c 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java@@ -360,7 +360,7 @@ public class TestDocValues extends LuceneTestCase {final Counter trackBytes = Counter.newCounter();DocValuesConsumer w = Ints.getWriter(dir, "test", trackBytes, type, newIOContext(random()));for (int i = 0; i < NUM_VALUES; i++) {- final long v = random().nextLong() % (1 + maxV);+ final long v = _TestUtil.nextLong(random(), -maxV, maxV);valueHolder.numberValue = values[i] = v;w.add(i, valueHolder);}diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.javaindex d1807f417f8..23a129b7c8b 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java@@ -18,6 +18,7 @@ package org.apache.lucene.index;*/import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util._TestUtil;import org.apache.lucene.store.ByteArrayDataInput;import org.apache.lucene.store.ByteArrayDataOutput;import org.apache.lucene.store.DataInput;@@ -99,9 +100,9 @@ public class TestIndexInput extends LuceneTestCase {final long l1;if (rarely()) {// a long with lots of zeroes at the end- l1 = LONGS[i] = ((long) Math.abs(random.nextInt())) << 32;+ l1 = LONGS[i] = _TestUtil.nextLong(random, 0, Integer.MAX_VALUE) << 32;} else {- l1 = LONGS[i] = Math.abs(random.nextLong());+ l1 = LONGS[i] = _TestUtil.nextLong(random, 0, Long.MAX_VALUE);}bdo.writeVLong(l1);bdo.writeLong(l1);diff --git a/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java b/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.javaindex c526e84786e..6417ebe8359 100644--- a/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java+++ b/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java@@ -261,7 +261,7 @@ public class TestFSTs extends LuceneTestCase {final PositiveIntOutputs outputs = PositiveIntOutputs.getSingleton(random().nextBoolean());final List<FSTTester.InputOutput<Long>> pairs = new ArrayList<FSTTester.InputOutput<Long>>(terms.length);for(int idx=0;idx<terms.length;idx++) {- pairs.add(new FSTTester.InputOutput<Long>(terms[idx], random().nextLong() & Long.MAX_VALUE));+ pairs.add(new FSTTester.InputOutput<Long>(terms[idx], _TestUtil.nextLong(random(), 0, Long.MAX_VALUE)));}new FSTTester<Long>(random(), dir, inputMode, pairs, outputs, false).doTest();}@@ -630,7 +630,7 @@ public class TestFSTs extends LuceneTestCase {final int num = atLeast(100);for(int iter=0;iter<num;iter++) {- Long v = minLong + random.nextLong() % (maxLong - minLong);+ Long v = _TestUtil.nextLong(random, minLong, maxLong);IntsRef input = Util.getByOutput(fstLong, v);assertTrue(validOutputs.contains(v) || input == null);}diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.javaindex e608c2c243b..726492f72ec 100644--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java@@ -51,8 +51,8 @@ public class TestPackedInts extends LuceneTestCase {public void testPackedInts() throws IOException {int num = atLeast(5);for (int iter = 0; iter < num; iter++) {- long ceil = 2;for(int nbits=1;nbits<63;nbits++) {+ final long maxValue = PackedInts.maxValue(nbits);final int valueCount = 100+random().nextInt(500);final Directory d = newDirectory();@@ -62,11 +62,7 @@ public class TestPackedInts extends LuceneTestCase {final long[] values = new long[valueCount];for(int i=0;i<valueCount;i++) {- long v = random().nextLong() % ceil;- if (v < 0) {- v = -v;- }- values[i] = v;+ values[i] = _TestUtil.nextLong(random(), 0, maxValue);w.add(values[i]);}w.finish();@@ -77,7 +73,7 @@ public class TestPackedInts extends LuceneTestCase {PackedInts.Reader r = PackedInts.getReader(in);assertEquals(fp, in.getFilePointer());for(int i=0;i<valueCount;i++) {- assertEquals("index=" + i + " ceil=" + ceil + " valueCount="+ assertEquals("index=" + i + " valueCount="+ valueCount + " nbits=" + nbits + " for "+ r.getClass().getSimpleName(), values[i], r.get(i));}@@ -87,7 +83,7 @@ public class TestPackedInts extends LuceneTestCase {IndexInput in = d.openInput("out.bin", newIOContext(random()));PackedInts.ReaderIterator r = PackedInts.getReaderIterator(in);for(int i=0;i<valueCount;i++) {- assertEquals("index=" + i + " ceil=" + ceil + " valueCount="+ assertEquals("index=" + i + " valueCount="+ valueCount + " nbits=" + nbits + " for "+ r.getClass().getSimpleName(), values[i], r.next());}@@ -100,7 +96,7 @@ public class TestPackedInts extends LuceneTestCase {for (int i = 0; i < valueCount; i +=1 + ((valueCount - i) <= 20 ? random().nextInt(valueCount - i): random().nextInt(20))) {- final String msg = "index=" + i + " ceil=" + ceil + " valueCount="+ final String msg = "index=" + i + " valueCount="+ valueCount + " nbits=" + nbits + " for "+ intsEnum.getClass().getSimpleName();if (i - intsEnum.ord() == 1 && random().nextBoolean()) {@@ -122,7 +118,7 @@ public class TestPackedInts extends LuceneTestCase {IndexInput in = d.openInput("out.bin", newIOContext(random()));PackedInts.Reader intsEnum = PackedInts.getDirectReader(in);for (int i = 0; i < valueCount; i++) {- final String msg = "index=" + i + " ceil=" + ceil + " valueCount="+ final String msg = "index=" + i + " valueCount="+ valueCount + " nbits=" + nbits + " for "+ intsEnum.getClass().getSimpleName();final int index = random().nextInt(valueCount);@@ -131,7 +127,6 @@ public class TestPackedInts extends LuceneTestCase {}in.close();}- ceil *= 2;d.close();}}@@ -174,7 +169,7 @@ public class TestPackedInts extends LuceneTestCase {final long maxValue = PackedInts.maxValue(bits1);for(int i=0;i<valueCount;i++) {- final long val = random().nextLong() & maxValue;+ final long val = _TestUtil.nextLong(random(), 0, maxValue);packed1.set(i, val);packed2.set(i, val);}@@ -234,7 +229,7 @@ public class TestPackedInts extends LuceneTestCase {List<PackedInts.Mutable> packedInts = createPackedInts(valueCount, bitsPerValue);for (PackedInts.Mutable packedInt: packedInts) {try {- fill(packedInt, (long)(Math.pow(2, bitsPerValue)-1), randomSeed);+ fill(packedInt, PackedInts.maxValue(bitsPerValue), randomSeed);} catch (Exception e) {e.printStackTrace(System.err);fail(String.format(@@ -278,9 +273,8 @@ public class TestPackedInts extends LuceneTestCase {private void fill(PackedInts.Mutable packedInt, long maxValue, long randomSeed) {Random rnd2 = new Random(randomSeed);- maxValue++;for (int i = 0 ; i < packedInt.size() ; i++) {- long value = Math.abs(rnd2.nextLong() % maxValue);+ long value = _TestUtil.nextLong(rnd2, 0, maxValue);packedInt.set(i, value);assertEquals(String.format("The set/get of the value at index %d should match for %s",@@ -425,7 +419,7 @@ public class TestPackedInts extends LuceneTestCase {final int from = random().nextInt(valueCount + 1);final int to = from + random().nextInt(valueCount + 1 - from);for (int bpv = 1; bpv <= 64; ++bpv) {- final long val = random().nextInt((int) Math.min(Integer.MAX_VALUE, PackedInts.maxValue(bpv)));+ final long val = _TestUtil.nextLong(random(), 0, PackedInts.maxValue(bpv));List<PackedInts.Mutable> packedInts = createPackedInts(valueCount, bpv);for (PackedInts.Mutable ints : packedInts) {String msg = ints.getClass().getSimpleName() + " bpv=" + bpv + ", from=" + from + ", to=" + to + ", val=" + val;diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.javaindex 35ca64be2b4..946ddaea20b 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java@@ -26,6 +26,8 @@ import java.io.InputStream;import java.io.OutputStream;import java.io.PrintStream;import java.lang.reflect.Method;+import java.math.BigDecimal;+import java.math.BigInteger;import java.nio.CharBuffer;import java.util.*;import java.util.concurrent.ExecutorService;@@ -210,7 +212,23 @@ public class _TestUtil {/** start and end are BOTH inclusive */public static int nextInt(Random r, int start, int end) {- return start + r.nextInt(end-start+1);+ return RandomInts.randomIntBetween(r, start, end);+ }++ /** start and end are BOTH inclusive */+ public static long nextLong(Random r, long start, long end) {+ assert end >= start;+ final BigInteger range = BigInteger.valueOf(end).add(BigInteger.valueOf(1)).subtract(BigInteger.valueOf(start));+ if (range.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0) {+ return start + r.nextInt(range.intValue());+ } else {+ // probably not evenly distributed when range is large, but OK for tests+ final BigInteger augend = new BigDecimal(range).multiply(new BigDecimal(r.nextDouble())).toBigInteger();+ final long result = BigInteger.valueOf(start).add(augend).longValue();+ assert result >= start;+ assert result <= end;+ return result;+ }}public static String randomSimpleString(Random r, int maxLength) {