public void ReadWriteAsCompactVarInt(ref ulong val) { var value = new CompactVarInt(val, sizeof(ulong)); ReadWrite(ref value); if (!Serializing) { val = value.ToLong(); } }
public void CanUseCompactVarInt() { var tests = new[] { new object[] { 0UL, new byte[] { 0 } }, new object[] { 1UL, new byte[] { 1 } }, new object[] { 127UL, new byte[] { 0x7F } }, new object[] { 128UL, new byte[] { 0x80, 0x00 } }, new object[] { 255UL, new byte[] { 0x80, 0x7F } }, new object[] { 256UL, new byte[] { 0x81, 0x00 } }, new object[] { 16383UL, new byte[] { 0xFE, 0x7F } }, //new object[]{16384UL, new byte[]{0xFF, 0x00}}, //new object[]{16511UL, new byte[]{0x80, 0xFF, 0x7F}}, //new object[]{65535UL, new byte[]{0x82, 0xFD, 0x7F}}, new object[] { (ulong)1 << 32, new byte[] { 0x8E, 0xFE, 0xFE, 0xFF, 0x00 } }, }; foreach (var test in tests) { ulong val = (ulong)test[0]; byte[] expectedBytes = (byte[])test[1]; AssertEx.CollectionEquals(new CompactVarInt(val, sizeof(ulong)).ToBytes(), expectedBytes); AssertEx.CollectionEquals(new CompactVarInt(val, sizeof(uint)).ToBytes(), expectedBytes); var compact = new CompactVarInt(sizeof(ulong)); compact.ReadWrite(expectedBytes); Assert.Equal(val, compact.ToLong()); compact = new CompactVarInt(sizeof(uint)); compact.ReadWrite(expectedBytes); Assert.Equal(val, compact.ToLong()); } foreach (var i in Enumerable.Range(0, 65535 * 4)) { var compact = new CompactVarInt((ulong)i, sizeof(ulong)); var bytes = compact.ToBytes(); compact = new CompactVarInt(sizeof(ulong)); compact.ReadWrite(bytes); Assert.Equal((ulong)i, compact.ToLong()); } }
public void CanUseCompactVarInt() { var tests = new[]{ new object[]{0UL, new byte[]{0}}, new object[]{1UL, new byte[]{1}}, new object[]{127UL, new byte[]{0x7F}}, new object[]{128UL, new byte[]{0x80, 0x00}}, new object[]{255UL, new byte[]{0x80, 0x7F}}, new object[]{256UL, new byte[]{0x81, 0x00}}, new object[]{16383UL, new byte[]{0xFE, 0x7F}}, //new object[]{16384UL, new byte[]{0xFF, 0x00}}, //new object[]{16511UL, new byte[]{0x80, 0xFF, 0x7F}}, //new object[]{65535UL, new byte[]{0x82, 0xFD, 0x7F}}, new object[]{(ulong)1 << 32, new byte[]{0x8E, 0xFE, 0xFE, 0xFF, 0x00}}, }; foreach(var test in tests) { ulong val = (ulong)test[0]; byte[] expectedBytes = (byte[])test[1]; AssertEx.CollectionEquals(new CompactVarInt(val, sizeof(ulong)).ToBytes(), expectedBytes); AssertEx.CollectionEquals(new CompactVarInt(val, sizeof(uint)).ToBytes(), expectedBytes); var compact = new CompactVarInt(sizeof(ulong)); compact.ReadWrite(expectedBytes); Assert.Equal(val, compact.ToLong()); compact = new CompactVarInt(sizeof(uint)); compact.ReadWrite(expectedBytes); Assert.Equal(val, compact.ToLong()); } foreach(var i in Enumerable.Range(0, 65535 * 4)) { var compact = new CompactVarInt((ulong)i, sizeof(ulong)); var bytes = compact.ToBytes(); compact = new CompactVarInt(sizeof(ulong)); compact.ReadWrite(bytes); Assert.Equal((ulong)i, compact.ToLong()); } }