Low-level InputStream test w/mock

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150003 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2003-08-12 10:41:56 +00:00
parent 624995b302
commit 7eb95be705
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package org.apache.lucene.index;
import org.apache.lucene.store.InputStream;
import java.io.IOException;
public class MockInputStream extends InputStream {
private byte[] buffer;
private int pointer = 0;
public MockInputStream(byte[] bytes) {
buffer = bytes;
length = bytes.length;
}
protected void readInternal(byte[] dest, int destOffset, int len)
throws IOException {
int remainder = len;
int start = pointer;
while (remainder != 0) {
// int bufferNumber = start / buffer.length;
int bufferOffset = start % buffer.length;
int bytesInBuffer = buffer.length - bufferOffset;
int bytesToCopy = bytesInBuffer >= remainder ? remainder : bytesInBuffer;
System.arraycopy(buffer, bufferOffset, dest, destOffset, bytesToCopy);
destOffset += bytesToCopy;
start += bytesToCopy;
remainder -= bytesToCopy;
}
pointer += len;
}
public void close() throws IOException {
// ignore
}
protected void seekInternal(long pos) throws IOException {
pointer = (int) pos;
}
}

View File

@ -0,0 +1,21 @@
package org.apache.lucene.index;
import junit.framework.TestCase;
import org.apache.lucene.store.InputStream;
import java.io.IOException;
public class TestInputStream extends TestCase {
public void testRead() throws IOException {
InputStream is = new MockInputStream(new byte[] { (byte) 0x80, 0x01,
(byte) 0xFF, 0x7F,
(byte) 0x80, (byte) 0x80, 0x01,
(byte) 0x81, (byte) 0x80, 0x01,
0x06, 'L', 'u', 'c', 'e', 'n', 'e'});
assertEquals(128,is.readVInt());
assertEquals(16383,is.readVInt());
assertEquals(16384,is.readVInt());
assertEquals(16385,is.readVInt());
assertEquals("Lucene",is.readString());
}
}