From a504328044768af1ed498f9c24bda2c4bf107502 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Sun, 13 Mar 2011 21:29:25 +0000 Subject: [PATCH] Improve VarInt: simplify, fix some minor bugs and increase test coverage. --- src/com/google/bitcoin/core/VarInt.java | 21 ++++++++-------- tests/com/google/bitcoin/core/VarIntTest.java | 24 ++++++++++++------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/com/google/bitcoin/core/VarInt.java b/src/com/google/bitcoin/core/VarInt.java index df47c4c0..cc56e3ef 100644 --- a/src/com/google/bitcoin/core/VarInt.java +++ b/src/com/google/bitcoin/core/VarInt.java @@ -16,6 +16,8 @@ package com.google.bitcoin.core; +import static com.google.bitcoin.core.Utils.isLessThanUnsigned; + public class VarInt { public final long value; @@ -44,13 +46,12 @@ public class VarInt { } public int getSizeInBytes() { - // Java doesn't have the actual value of MAX_INT, as all types in Java - // are signed *headsmash*. - if (value < 253) + // Java doesn't have the actual value of MAX_INT, as all types in Java are signed. + if (isLessThanUnsigned(value, 253)) return 1; - else if (value <= 65536) + else if (isLessThanUnsigned(value, 65536)) return 3; // 1 marker + 2 data bytes - else if (value <= 4294967295L) + else if (isLessThanUnsigned(value, 4294967296L)) return 5; // 1 marker + 4 data bytes else return 9; // 1 marker + 8 data bytes @@ -63,11 +64,11 @@ public class VarInt { public byte[] encodeBE() { - if (Utils.isLessThanUnsigned(value, 253)) { + if (isLessThanUnsigned(value, 253)) { 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) }; - } else if (Utils.isLessThanUnsigned(value, 4294967295L)) { + } else if (isLessThanUnsigned(value, 4294967295L)) { byte[] bytes = new byte[5]; bytes[0] = (byte) 254; Utils.uint32ToByteArrayLE(value, bytes, 1); @@ -75,8 +76,8 @@ public class VarInt { } else { byte[] bytes = new byte[9]; bytes[0] = (byte) 255; - Utils.uint32ToByteArrayLE(value & 0xFFFFFFFF, bytes, 1); - Utils.uint32ToByteArrayLE(value >> 32, bytes, 5); + Utils.uint32ToByteArrayLE(value, bytes, 1); + Utils.uint32ToByteArrayLE(value >>> 32, bytes, 5); return bytes; } } diff --git a/tests/com/google/bitcoin/core/VarIntTest.java b/tests/com/google/bitcoin/core/VarIntTest.java index 05b2e70a..c17b4d6e 100644 --- a/tests/com/google/bitcoin/core/VarIntTest.java +++ b/tests/com/google/bitcoin/core/VarIntTest.java @@ -19,25 +19,33 @@ package com.google.bitcoin.core; import junit.framework.TestCase; public class VarIntTest extends TestCase { - public void testVarInts() throws Exception { - VarInt a; - - // Bytes - a = new VarInt(10); + public void testBytes() throws Exception { + VarInt a = new VarInt(10); assertEquals(1, a.getSizeInBytes()); assertEquals(1, a.encode().length); assertEquals(10, new VarInt(a.encode(), 0).value); + } - // Shorts - a = new VarInt(64000); + public void testShorts() throws Exception { + VarInt a = new VarInt(64000); assertEquals(3, a.getSizeInBytes()); assertEquals(3, a.encode().length); 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.encode().length); byte[] bytes = a.encode(); 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); + } }