コード例 #1
0
        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);
        }
コード例 #2
0
        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);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        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();
        }