diff --git a/src/Neo.Extensions/UnsafeData.cs b/src/Neo.Extensions/UnsafeData.cs index 6d4c9a6f8e..50126c25f4 100644 --- a/src/Neo.Extensions/UnsafeData.cs +++ b/src/Neo.Extensions/UnsafeData.cs @@ -18,14 +18,16 @@ public static class UnsafeData /// /// The length of the data. /// The size of variable-length of the data. - public static int GetVarSize(int value) + public static byte GetVarSize(long value) { if (value < 0xFD) return sizeof(byte); - else if (value <= 0xFFFF) + else if (value <= ushort.MaxValue) return sizeof(byte) + sizeof(ushort); - else + else if (value <= uint.MaxValue) return sizeof(byte) + sizeof(uint); + else + return sizeof(byte) + sizeof(ulong); } } } diff --git a/tests/Neo.Extensions.Tests/UT_UnsafeData.cs b/tests/Neo.Extensions.Tests/UT_UnsafeData.cs index 5c8bb310c3..98562fdfa2 100644 --- a/tests/Neo.Extensions.Tests/UT_UnsafeData.cs +++ b/tests/Neo.Extensions.Tests/UT_UnsafeData.cs @@ -12,22 +12,33 @@ public class UT_UnsafeData [TestMethod] public void TestGetVarSizeInt() { - for (int i = 0; i < 3; i++) + for (int i = 0; i < 4; i++) { if (i == 0) { int result = UnsafeData.GetVarSize(1); + int old = OldGetVarSize(1); Assert.AreEqual(1, result); + Assert.AreEqual(1, old); } else if (i == 1) { - int result = UnsafeData.GetVarSize(0xFFFF); + int result = UnsafeData.GetVarSize(ushort.MaxValue); + int old = OldGetVarSize(ushort.MaxValue); Assert.AreEqual(3, result); + Assert.AreEqual(3, old); } - else + else if (i == 2) { - int result = UnsafeData.GetVarSize(0xFFFFFF); + int result = UnsafeData.GetVarSize(uint.MaxValue); + int old = OldGetVarSize(int.MaxValue); Assert.AreEqual(5, result); + Assert.AreEqual(5, old); + } + else + { + int result = UnsafeData.GetVarSize(long.MaxValue); + Assert.AreEqual(9, result); } } } @@ -159,5 +170,15 @@ enum TestEnum6 : long { case1 = 1, case2 = 2 } + + public static int OldGetVarSize(int value) + { + if (value < 0xFD) + return sizeof(byte); + else if (value <= ushort.MaxValue) + return sizeof(byte) + sizeof(ushort); + else + return sizeof(byte) + sizeof(uint); + } } }