public unsafe void SizePrefixTest() { BitStreamer bs = new BitStreamer(); bs.ResetWrite(64); bs.ReserveSizePrefix(); Assert.AreEqual(4, bs.ByteOffset); // Write some random data. var bits = 32; for (int i = 0; i < 8; i++) { bs.WriteInt32(i + 1, 7); bits += 7; } Assert.AreEqual(bits, bs.BitOffset); int bytesUsed = bs.BytesUsed; // Prefix the size and make sure the offset remains unchanged. Assert.AreEqual(bytesUsed, bs.PrefixSize()); Assert.AreEqual(bits, bs.BitOffset); var newbs = new BitStreamer(); newbs.ResetRead(bs.Buffer, bs.ByteLength, false); // Read the length of the buffer. // Must be read as uint due to Zig/Zagging of int value. Assert.AreEqual(bytesUsed, newbs.ReadUInt32()); for (int i = 0; i < 8; i++) { Assert.AreEqual(i + 1, newbs.ReadInt32(7)); } Assert.AreEqual(bs.BitOffset, newbs.BitOffset); }
public void BuilderTest() { const uint uintVal = 598135039; const long longVal = 1234567890; const char charVal = '@'; const decimal decVal = (decimal)(System.Math.PI * System.Math.E); m_stream .WriteUInt32(uintVal) .WriteLong(longVal) .WriteChar(charVal) .WriteDecimal(decVal) .ResetRead(); Assert.AreEqual(SerializationMode.Reading, m_stream.Mode); Assert.AreEqual(uintVal, m_stream.ReadUInt32()); Assert.AreEqual(longVal, m_stream.ReadLong()); Assert.AreEqual(charVal, m_stream.ReadChar()); Assert.AreEqual(decVal, m_stream.ReadDecimal()); }