public void Leb128Size() { Assert.That(Leb128.SizeUleb128(0), Is.EqualTo(1)); Assert.That(Leb128.SizeUleb128(0x1UL), Is.EqualTo(1)); Assert.That(Leb128.SizeUleb128(0x40UL), Is.EqualTo(1)); Assert.That(Leb128.SizeUleb128(0x7fUL), Is.EqualTo(1)); Assert.That(Leb128.SizeUleb128(0x80UL), Is.EqualTo(2)); Assert.That(Leb128.SizeUleb128(0x2000UL), Is.EqualTo(2)); Assert.That(Leb128.SizeUleb128(0x3fffUL), Is.EqualTo(2)); Assert.That(Leb128.SizeUleb128(0x4000UL), Is.EqualTo(3)); Assert.That(Leb128.SizeUleb128(0x100000UL), Is.EqualTo(3)); Assert.That(Leb128.SizeUleb128(0x1fffffUL), Is.EqualTo(3)); Assert.That(Leb128.SizeUleb128(0x200000UL), Is.EqualTo(4)); Assert.That(Leb128.SizeUleb128(0x8000000UL), Is.EqualTo(4)); Assert.That(Leb128.SizeUleb128(0xfffffffUL), Is.EqualTo(4)); Assert.That(Leb128.SizeUleb128(0x10000000UL), Is.EqualTo(5)); Assert.That(Leb128.SizeUleb128(0x40000000UL), Is.EqualTo(5)); Assert.That(Leb128.SizeUleb128(0x7fffffffUL), Is.EqualTo(5)); Assert.That(Leb128.SizeUleb128(uint.MaxValue), Is.EqualTo(5)); }
public void TestEncodeSigned() { byte[] buf = new byte[2]; BFlat.Buffer output = new BFlat.Buffer(buf, 0); Assert.AreEqual(1, Leb128.encodeSigned(output, 1)); Assert.AreEqual(0x1, buf[0]); Assert.AreEqual(1, Leb128.encodeSigned(output.rewind(), 0)); Assert.AreEqual(0x0, buf[0]); Assert.AreEqual(1, Leb128.encodeSigned(output.rewind(), -1)); Assert.AreEqual(0x7f, buf[0]); Assert.AreEqual(2, Leb128.encodeSigned(output.rewind(), 127)); Assert.AreEqual((byte)0xff, buf[0]); Assert.AreEqual(0x00, buf[1]); Assert.AreEqual(2, Leb128.encodeSigned(output.rewind(), -128)); Assert.AreEqual((byte)0x80, buf[0]); Assert.AreEqual(0x7f, buf[1]); buf = new byte[3]; output.data = buf; Assert.AreEqual(3, Leb128.encodeSigned(output.rewind(), -32767)); Assert.AreEqual((byte)0x81, buf[0]); Assert.AreEqual((byte)0x80, buf[1]); Assert.AreEqual((byte)0x7e, buf[2]); buf = new byte[4]; output.data = buf; output.position = 1; Assert.AreEqual(3, Leb128.encodeSigned(output, 32768)); Assert.AreEqual((byte)0x80, buf[1]); Assert.AreEqual((byte)0x80, buf[2]); Assert.AreEqual((byte)0x02, buf[3]); }
public void TestDecodeSigned() { BFlat.Buffer buffer = new BFlat.Buffer(new byte[10], 0); long data = 0; Leb128.encodeSigned(buffer, data); Assert.AreEqual(1, buffer.position); Assert.AreEqual(data, Leb128.decodeSigned(buffer)); data = 1; Leb128.encodeSigned(buffer.rewind(), data); Assert.AreEqual(1, buffer.position); Assert.AreEqual(data, Leb128.decodeSigned(buffer.rewind())); data = -1; Leb128.encodeSigned(buffer.rewind(), data); Assert.AreEqual(1, buffer.position); Assert.AreEqual(data, Leb128.decodeSigned(buffer.rewind())); data = 127; Leb128.encodeSigned(buffer.rewind(), data); Assert.AreEqual(2, buffer.position); Assert.AreEqual(data, Leb128.decodeSigned(buffer.rewind())); data = -128; Leb128.encodeSigned(buffer.rewind(), data); Assert.AreEqual(2, buffer.position); Assert.AreEqual(data, Leb128.decodeSigned(buffer.rewind())); data = 32767; Leb128.encodeSigned(buffer.rewind(), data); Assert.AreEqual(3, buffer.position); Assert.AreEqual(data, Leb128.decodeSigned(buffer.rewind())); data = -32768; Leb128.encodeSigned(buffer.rewind(), data); Assert.AreEqual(3, buffer.position); Assert.AreEqual(data, Leb128.decodeSigned(buffer.rewind())); }
public void ExpectLeb128Eq(ulong value, ulong pad, params byte[] expected) { List <byte> buf = new List <byte>(32); int size = Leb128.WriteUleb128(buf, value); Assert.That(size, Is.EqualTo(buf.Count)); Assert.That(expected, Is.EquivalentTo(buf)); }
public void Test_Write(uint value, byte[] expected) { var bytes = Leb128.Write(value); Assert.Equal(expected, bytes); var decoded = Leb128.Read(bytes); Assert.Equal(value, decoded); }
public void ExpectReadLeb128Eq(ulong expected, params byte[] value) { do { int count = 0; ulong val = 0; int size = Leb128.ReadUleb128(value, ref count, out val); Assert.That(size, Is.EqualTo(value.Length)); Assert.That(val, Is.EqualTo(expected)); } while (false); }
public void ReadInvalidSize() { byte[] val = new byte[] { 0, 1, 2 }; ulong ov = 0; int start = 10; var ret = Leb128.ReadUleb128(val, ref start, out ov); Assert.That(ret, Is.EqualTo(0)); Assert.That(ov, Is.EqualTo(0)); }
public void Test_Write(uint value, byte[] expected) { var bytes = new byte[5]; var length = Leb128.Write(bytes, value); Assert.Equal(expected, bytes.Take(length)); var decoded = Leb128.Read(bytes); Assert.Equal(value, decoded); }
public void ExpectReadLeb128EqStream(ulong expected, params byte[] value) { MemoryStream stream = new MemoryStream(value); do { ulong val = 0; bool valid = Leb128.ReadUleb128(stream, out val); Assert.That(valid, Is.True); Assert.That(val, Is.EqualTo(expected)); } while (false); }
// ********************************************************************** public long ReadRelative(ref long lastValue) { int offset = ReadSByte(); if (offset == sbyte.MinValue) { return(lastValue = Leb128.Read(BaseStream)); } else { return(lastValue += offset); } }
protected int WriteKey(Span <byte> buffer) { var length = 0; if (Cid.HasValue) { length += Leb128.Write(buffer, Cid.Value); } length += ByteConverter.FromString(Key, buffer.Slice(length)); return(length); }
// ********************************************************************** public long ReadGrowing(long lastValue) { uint offset = ULeb128.Read(BaseStream); if (offset == ULeb128.Max4BValue) { return(lastValue + Leb128.Read(BaseStream)); } else { return(lastValue + offset); } }
// ********************************************************************** public void WriteGrowing(long value, ref long lastValue) { long offset = value - lastValue; if (offset >= ULeb128.Max4BValue || offset < 0) { ULeb128.Write(BaseStream, ULeb128.Max4BValue); Leb128.Write(BaseStream, offset); } else { ULeb128.Write(BaseStream, (uint)offset); } lastValue = value; }
public void encodeUnsignedTest() { BFlat.Buffer buf = new BFlat.Buffer(new byte[10], 0); Assert.AreEqual(1, Leb128.encodeUnsigned(buf, 0)); Assert.AreEqual(0, buf.data[0]); Assert.AreEqual(1, Leb128.encodeUnsigned(buf, 1)); Assert.AreEqual(1, buf.data[1]); Assert.AreEqual(1, Leb128.encodeUnsigned(buf.rewind(), 127)); Assert.AreEqual((byte)0x7f, buf.data[0]); Assert.AreEqual(2, Leb128.encodeUnsigned(buf.rewind(), 128)); Assert.AreEqual((byte)0x80, buf.data[0]); Assert.AreEqual((byte)0x01, buf.data[1]); Leb128.encodeUnsigned(buf.rewind(), UInt64.MaxValue); Assert.AreEqual(UInt64.MaxValue, Leb128.decodeUnsigned(buf.rewind())); }
public virtual byte[] CreateKey() { var length = Encoding.UTF8.GetByteCount(Key); //for collections add the leb128 cid if (Cid.HasValue) { length = length + 2; } var buffer = new byte[length]; if (Cid.HasValue) { var leb128Length = Leb128.Write(buffer, Cid.Value); Converter.FromString(Key, buffer, leb128Length); } else { Converter.FromString(Key, buffer, 0); } return(buffer); }
public void Test_Read(byte[] bytes, uint expected) { var actual = Leb128.Read(bytes); Assert.Equal(expected, actual); }
public int Baseline() { Span <byte> buffer = stackalloc byte[8]; return(Leb128.Write(buffer, Value)); }
// ********************************************************************** public long ReadLeb128() { return(Leb128.Read(BaseStream)); }
// ********************************************************************** public void WriteLeb128(long value) { Leb128.Write(BaseStream, value); }