public void TestVLQUInt64() { var rnd = new Random(); for (var c = 0; c < count; c++) { var memSize = rnd.Next(1, 10_000); var mem = new byte[memSize + sizeof(UInt64) + 2]; var answer = rnd.NextUInt64(); var span1 = new Span <byte>(mem); var span2 = new ReadOnlySpan <byte>(mem); span1.WriteVLQ(answer, out var len); var back = span2.ReadVLQUInt64(out var len2); Assert.Equal(SpanUtils.MeasureVLQ(answer), len); Assert.Equal(SpanUtils.MeasureVLQ(answer), len2); Assert.Equal(answer, back); // Check overflow span1.Fill(0xFF); Assert.Throws <OverflowException>(() => { var span3 = new ReadOnlySpan <byte>(mem); span3.ReadVLQUInt64(out _); }); } }
public void TestVLQUInt16FullRange() { var rnd = new Random(); var mem = new byte[sizeof(UInt16) + 1]; for (int c = UInt16.MinValue; c < UInt16.MaxValue; c++) { var answer = (UInt16)c; var span1 = new Span <byte>(mem); var span2 = new ReadOnlySpan <byte>(mem); span1.Fill(0); span1.WriteVLQ(answer, out var len); var back = span2.ReadVLQUInt16(out var len2); Assert.Equal(answer, back); Assert.Equal(SpanUtils.MeasureVLQ(answer), len); Assert.Equal(SpanUtils.MeasureVLQ(answer), len2); } }
public void TestVLQInt32() { var rnd = new Random(); for (var c = 0; c < count; c++) { var memSize = rnd.Next(1, 10_000); var mem = new byte[memSize * SpanUtils.MeasureVLQ(UInt32.MaxValue) + 1]; var span1 = new SpanStream(mem); var span2 = new SpanStream(mem); var data = new Int32[memSize]; for (var i = 0; i < memSize; i++) { data[i] = rnd.NextInt32(); } for (var i = 0; i < memSize; i++) { span1.WriteVLQ(data[i]); } for (var i = 0; i < memSize; i++) { Assert.Equal(data[i], span2.ReadVLQInt32(out var len)); Assert.Equal(SpanUtils.MeasureVLQ(data[i]), len); } // Check overflow new Span <byte>(mem).Fill(0xFF); Assert.Throws <OverflowException>(() => { var span3 = new SpanStream(mem); span3.ReadVLQInt32(out _); }); } }