예제 #1
0
        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);
        }