public void WriteResizeTest() { BitStreamer bs = new BitStreamer(false); Assert.IsFalse(bs.ThrowsOnExceededBuffer); Assert.IsFalse(bs.IsValid); bs.ResetWrite(8); Assert.IsTrue(bs.IsValid); bs.WriteULong(123); Assert.IsTrue(bs.IsValid); Assert.AreEqual(8, bs.ByteLength); bs.WriteInt32(321); Assert.IsTrue(bs.IsValid); Assert.AreEqual(16, bs.ByteLength); // Confirm offset has increased. Assert.AreEqual(12, bs.ByteOffset); bs.ResetRead(); Assert.AreEqual(123, bs.ReadULong()); Assert.AreEqual(321, bs.ReadInt32()); Assert.IsTrue(bs.IsValid); }
public void WriteInvalidateTest() { BitStreamer bs = new BitStreamer(false); IntPtr ptr = Memory.Alloc(8); Assert.IsFalse(bs.ThrowsOnExceededBuffer); Assert.IsFalse(bs.IsValid); bs.ResetWrite(ptr, 8); Assert.IsTrue(bs.IsValid); bs.WriteULong(123); Assert.IsTrue(bs.IsValid); bs.WriteInt32(321); Assert.IsFalse(bs.IsValid); // Confirm offset hasn't increased. Assert.AreEqual(8, bs.ByteOffset); bs.ResetWrite(); Assert.IsTrue(bs.IsValid); Memory.Free(ptr); }
public void NonAlignBufferTest() { IntPtr ptr = Memory.Alloc(12); BitStreamer bs = new BitStreamer(); bs.ResetWrite(ptr, 12); Assert.AreEqual(8, bs.ByteLength); bs.ResetRead(); Assert.AreEqual(12, bs.ByteLength); bs.ResetWrite(); Assert.AreEqual(8, bs.ByteLength); bs.WriteULong(123); // The write buffer should be rounded down to 8. So this must fail. Assert.Throws <InvalidOperationException>(() => { bs.WriteByte(1); }); Memory.Free(ptr); }
public unsafe void WriteBufferCopyTest() { ulong value = 666; IntPtr buf = Marshal.AllocHGlobal(8); BitStreamer reader = new BitStreamer(); reader.ResetWrite(buf, 8, false); reader.WriteULong(value, 64); Assert.AreEqual(64, reader.BitLength); Assert.AreEqual(64, reader.BitOffset); Assert.AreEqual(value, *(ulong *)reader.Buffer); reader.Dispose(); }