Improve VarInt: simplify, fix some minor bugs and increase test coverage.

This commit is contained in:
Mike Hearn
2011-03-13 21:29:25 +00:00
parent debd034c71
commit a504328044
2 changed files with 27 additions and 18 deletions

View File

@@ -16,6 +16,8 @@
package com.google.bitcoin.core; package com.google.bitcoin.core;
import static com.google.bitcoin.core.Utils.isLessThanUnsigned;
public class VarInt { public class VarInt {
public final long value; public final long value;
@@ -44,13 +46,12 @@ public class VarInt {
} }
public int getSizeInBytes() { public int getSizeInBytes() {
// Java doesn't have the actual value of MAX_INT, as all types in Java // Java doesn't have the actual value of MAX_INT, as all types in Java are signed.
// are signed *headsmash*. if (isLessThanUnsigned(value, 253))
if (value < 253)
return 1; return 1;
else if (value <= 65536) else if (isLessThanUnsigned(value, 65536))
return 3; // 1 marker + 2 data bytes return 3; // 1 marker + 2 data bytes
else if (value <= 4294967295L) else if (isLessThanUnsigned(value, 4294967296L))
return 5; // 1 marker + 4 data bytes return 5; // 1 marker + 4 data bytes
else else
return 9; // 1 marker + 8 data bytes return 9; // 1 marker + 8 data bytes
@@ -63,11 +64,11 @@ public class VarInt {
public byte[] encodeBE() { public byte[] encodeBE() {
if (Utils.isLessThanUnsigned(value, 253)) { if (isLessThanUnsigned(value, 253)) {
return new byte[] { (byte)value }; return new byte[] { (byte)value };
} else if (Utils.isLessThanUnsigned(value, 65536)) { } else if (isLessThanUnsigned(value, 65536)) {
return new byte[] { (byte) 253, (byte) (value), (byte) (value >> 8) }; return new byte[] { (byte) 253, (byte) (value), (byte) (value >> 8) };
} else if (Utils.isLessThanUnsigned(value, 4294967295L)) { } else if (isLessThanUnsigned(value, 4294967295L)) {
byte[] bytes = new byte[5]; byte[] bytes = new byte[5];
bytes[0] = (byte) 254; bytes[0] = (byte) 254;
Utils.uint32ToByteArrayLE(value, bytes, 1); Utils.uint32ToByteArrayLE(value, bytes, 1);
@@ -75,8 +76,8 @@ public class VarInt {
} else { } else {
byte[] bytes = new byte[9]; byte[] bytes = new byte[9];
bytes[0] = (byte) 255; bytes[0] = (byte) 255;
Utils.uint32ToByteArrayLE(value & 0xFFFFFFFF, bytes, 1); Utils.uint32ToByteArrayLE(value, bytes, 1);
Utils.uint32ToByteArrayLE(value >> 32, bytes, 5); Utils.uint32ToByteArrayLE(value >>> 32, bytes, 5);
return bytes; return bytes;
} }
} }

View File

@@ -19,25 +19,33 @@ package com.google.bitcoin.core;
import junit.framework.TestCase; import junit.framework.TestCase;
public class VarIntTest extends TestCase { public class VarIntTest extends TestCase {
public void testVarInts() throws Exception { public void testBytes() throws Exception {
VarInt a; VarInt a = new VarInt(10);
// Bytes
a = new VarInt(10);
assertEquals(1, a.getSizeInBytes()); assertEquals(1, a.getSizeInBytes());
assertEquals(1, a.encode().length); assertEquals(1, a.encode().length);
assertEquals(10, new VarInt(a.encode(), 0).value); assertEquals(10, new VarInt(a.encode(), 0).value);
}
// Shorts public void testShorts() throws Exception {
a = new VarInt(64000); VarInt a = new VarInt(64000);
assertEquals(3, a.getSizeInBytes()); assertEquals(3, a.getSizeInBytes());
assertEquals(3, a.encode().length); assertEquals(3, a.encode().length);
assertEquals(64000, new VarInt(a.encode(), 0).value); assertEquals(64000, new VarInt(a.encode(), 0).value);
}
a = new VarInt(0xAABBCCDDL); public void testInts() throws Exception {
VarInt a = new VarInt(0xAABBCCDDL);
assertEquals(5, a.getSizeInBytes()); assertEquals(5, a.getSizeInBytes());
assertEquals(5, a.encode().length); assertEquals(5, a.encode().length);
byte[] bytes = a.encode(); byte[] bytes = a.encode();
assertEquals(0xAABBCCDDL, 0xFFFFFFFFL & new VarInt(bytes, 0).value); assertEquals(0xAABBCCDDL, 0xFFFFFFFFL & new VarInt(bytes, 0).value);
} }
public void testLong() throws Exception {
VarInt a = new VarInt(0xCAFEBABEDEADBEEFL);
assertEquals(9, a.getSizeInBytes());
assertEquals(9, a.encode().length);
byte[] bytes = a.encode();
assertEquals(0xCAFEBABEDEADBEEFL, new VarInt(bytes, 0).value);
}
} }