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 2012/06/19 16:27:43 UTC

svn commit: r1351728 - in /lucene/dev/branches/branch_4x/lucene: core/src/test/org/apache/lucene/codecs/lucene40/values/ core/src/test/org/apache/lucene/index/ core/src/test/org/apache/lucene/util/fst/ core/src/test/org/apache/lucene/util/packed/ test-...

Author: jpountz
Date: Tue Jun 19 14:27:43 2012
New Revision: 1351728

URL: http://svn.apache.org/viewvc?rev=1351728&view=rev
Log:
LUCENE-4148: Make _TestUtil able to generate random longs. (merged from r1351718)

Modified:
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
    lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java?rev=1351728&r1=1351727&r2=1351728&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/codecs/lucene40/values/TestDocValues.java Tue Jun 19 14:27:43 2012
@@ -360,7 +360,7 @@ public class TestDocValues extends Lucen
       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);
       }

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java?rev=1351728&r1=1351727&r2=1351728&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java Tue Jun 19 14:27:43 2012
@@ -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 Luce
       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);

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java?rev=1351728&r1=1351727&r2=1351728&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/fst/TestFSTs.java Tue Jun 19 14:27:43 2012
@@ -261,7 +261,7 @@ public class TestFSTs extends LuceneTest
       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 LuceneTest
 
         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);
         }

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java?rev=1351728&r1=1351727&r2=1351728&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java Tue Jun 19 14:27:43 2012
@@ -51,8 +51,8 @@ public class TestPackedInts extends Luce
   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 Luce
 
         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 Luce
           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 Luce
           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 Luce
           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 Luce
           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 Luce
           }
           in.close();
         }
-        ceil *= 2;
         d.close();
       }
     }
@@ -174,7 +169,7 @@ public class TestPackedInts extends Luce
 
       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 Luce
     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 Luce
 
   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 Luce
     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;

Modified: lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java?rev=1351728&r1=1351727&r2=1351728&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java (original)
+++ lucene/dev/branches/branch_4x/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java Tue Jun 19 14:27:43 2012
@@ -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) {