VarIntTest: add test for reading and writing

This commit is contained in:
Andreas Schildbach 2023-03-27 16:27:50 +02:00
parent 21fae58bc8
commit 140912b080

View file

@ -21,7 +21,11 @@ import junitparams.Parameters;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@RunWith(JUnitParamsRunner.class) @RunWith(JUnitParamsRunner.class)
public class VarIntTest { public class VarIntTest {
@ -62,6 +66,19 @@ public class VarIntTest {
assertEquals(value, VarInt.ofBytes(a.encode(), 0).longValue()); assertEquals(value, VarInt.ofBytes(a.encode(), 0).longValue());
} }
@Test
@Parameters(method = "longTestVectors")
public void writeThenRead(long value, int size) {
VarInt varInt = VarInt.of(value);
ByteBuffer buf = ByteBuffer.allocate(varInt.getSizeInBytes());
varInt.write(buf);
assertFalse(buf.hasRemaining());
((Buffer) buf).rewind();
VarInt varIntCopy = VarInt.read(buf);
assertFalse(buf.hasRemaining());
assertEquals(varInt, varIntCopy);
}
private Object[] integerTestVectors() { private Object[] integerTestVectors() {
return new Object[]{ return new Object[]{
new Object[]{ 0, 1}, new Object[]{ 0, 1},