Apache Solr open-source search software
LUCENE-3738: Add random test for vints and vlongs, cleanup code and make more clear for longs
Uwe Schindler5ab0204216e7
diff --git a/lucene/core/src/java/org/apache/lucene/store/DataOutput.java b/lucene/core/src/java/org/apache/lucene/store/DataOutput.javaindex 3703c261251..80a17451c26 100644--- a/lucene/core/src/java/org/apache/lucene/store/DataOutput.java+++ b/lucene/core/src/java/org/apache/lucene/store/DataOutput.java@@ -76,7 +76,7 @@ public abstract class DataOutput {*/public final void writeVInt(int i) throws IOException {while ((i & ~0x7F) != 0) {- writeByte((byte)((i & 0x7f) | 0x80));+ writeByte((byte)((i & 0x7F) | 0x80));i >>>= 7;}writeByte((byte)i);@@ -97,8 +97,8 @@ public abstract class DataOutput {*/public final void writeVLong(long i) throws IOException {assert i >= 0L;- while ((i & ~0x7F) != 0) {- writeByte((byte)((i & 0x7f) | 0x80));+ while ((i & ~0x7FL) != 0L) {+ writeByte((byte)((i & 0x7FL) | 0x80L));i >>>= 7;}writeByte((byte)i);diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.javaindex 70c756f58ec..2d809f2f074 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexInput.java@@ -19,11 +19,15 @@ package org.apache.lucene.index;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.store.ByteArrayDataInput;+import org.apache.lucene.store.ByteArrayDataOutput;import org.apache.lucene.store.DataInput;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.RAMDirectory;+import org.junit.AfterClass;+import org.junit.BeforeClass;+import java.io.IOException;public class TestIndexInput extends LuceneTestCase {@@ -74,6 +78,41 @@ public class TestIndexInput extends LuceneTestCase {(byte) 0x01, // guard value};+ static final int COUNT = RANDOM_MULTIPLIER * 65536;+ static int[] INTS;+ static long[] LONGS;+ static byte[] RANDOM_TEST_BYTES;++ @BeforeClass+ public static void beforeClass() throws IOException {+ INTS = new int[COUNT];+ LONGS = new long[COUNT];+ RANDOM_TEST_BYTES = new byte[COUNT * (5 + 4 + 9 + 8)];+ final ByteArrayDataOutput bdo = new ByteArrayDataOutput(RANDOM_TEST_BYTES);+ for (int i = 0; i < COUNT; i++) {+ final int i1 = INTS[i] = random.nextInt();+ bdo.writeVInt(i1);+ bdo.writeInt(i1);++ final long l1;+ if (rarely()) {+ // a long with lots of zeroes at the end+ l1 = LONGS[i] = ((long) Math.abs(random.nextInt())) << 32;+ } else {+ l1 = LONGS[i] = Math.abs(random.nextLong());+ }+ bdo.writeVLong(l1);+ bdo.writeLong(l1);+ }+ }++ @AfterClass+ public static void afterClass() {+ INTS = null;+ LONGS = null;+ RANDOM_TEST_BYTES = null;+ }+private void checkReads(DataInput is, Class<? extends Exception> expectedEx) throws IOException {assertEquals(128,is.readVInt());assertEquals(16383,is.readVInt());@@ -116,29 +155,50 @@ public class TestIndexInput extends LuceneTestCase {}assertEquals(1L, is.readVLong()); // guard value}++ private void checkRandomReads(DataInput is) throws IOException {+ for (int i = 0; i < COUNT; i++) {+ assertEquals(INTS[i], is.readVInt());+ assertEquals(INTS[i], is.readInt());+ assertEquals(LONGS[i], is.readVLong());+ assertEquals(LONGS[i], is.readLong());+ }+ }// this test only checks BufferedIndexInput because MockIndexInput extends BufferedIndexInputpublic void testBufferedIndexInputRead() throws IOException {- final IndexInput is = new MockIndexInput(READ_TEST_BYTES);+ IndexInput is = new MockIndexInput(READ_TEST_BYTES);checkReads(is, IOException.class);is.close();+ is = new MockIndexInput(RANDOM_TEST_BYTES);+ checkRandomReads(is);+ is.close();}// this test checks the raw IndexInput methods as it uses RAMIndexInput which extends IndexInput directlypublic void testRawIndexInputRead() throws IOException {final RAMDirectory dir = new RAMDirectory();- final IndexOutput os = dir.createOutput("foo", newIOContext(random));+ IndexOutput os = dir.createOutput("foo", newIOContext(random));os.writeBytes(READ_TEST_BYTES, READ_TEST_BYTES.length);os.close();- final IndexInput is = dir.openInput("foo", newIOContext(random));+ IndexInput is = dir.openInput("foo", newIOContext(random));checkReads(is, IOException.class);is.close();++ os = dir.createOutput("bar", newIOContext(random));+ os.writeBytes(RANDOM_TEST_BYTES, RANDOM_TEST_BYTES.length);+ os.close();+ is = dir.openInput("bar", newIOContext(random));+ checkRandomReads(is);+ is.close();dir.close();}public void testByteArrayDataInput() throws IOException {- final ByteArrayDataInput is = new ByteArrayDataInput(READ_TEST_BYTES);+ ByteArrayDataInput is = new ByteArrayDataInput(READ_TEST_BYTES);checkReads(is, RuntimeException.class);+ is = new ByteArrayDataInput(RANDOM_TEST_BYTES);+ checkRandomReads(is);}}