LUCENE-3738: Add random test for vints and vlongs, cleanup code and make more clear for longs

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1308117 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-04-01 13:52:16 +00:00
parent b70152d24c
commit 5ab0204216
2 changed files with 67 additions and 7 deletions

View File

@ -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);

View File

@ -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 BufferedIndexInput
public 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 directly
public 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);
}
}