Ejemplo n.º 1
0
        public void ReadInvalidateTest()
        {
            BitStreamer bs  = new BitStreamer(false);
            IntPtr      ptr = Memory.Alloc(4);

            Assert.IsFalse(bs.ThrowsOnExceededBuffer);
            Assert.IsFalse(bs.IsValid);

            bs.ResetRead(ptr, 4);

            Assert.IsTrue(bs.IsValid);

            bs.ReadInt32();
            Assert.IsTrue(bs.IsValid);

            bs.ReadInt32();
            Assert.IsFalse(bs.IsValid);

            // Confirm offset hasn't increased.
            Assert.AreEqual(4, bs.ByteOffset);

            bs.ResetRead();
            Assert.IsTrue(bs.IsValid);

            Memory.Free(ptr);
        }
Ejemplo n.º 2
0
        public void PeekIntTest(int value)
        {
            const int min = -2000, max = 0;

            m_stream.WriteInt32(value, min, max);
            m_stream.ResetRead();
            Assert.AreEqual(value, m_stream.PeekInt32(min, max));
            Assert.AreEqual(0, m_stream.BitOffset);
        }
Ejemplo n.º 3
0
        public void VectorReadTest()
        {
            m_stream.WriteBlit(m_testStruct);
            m_stream.ResetRead();

            Vector3 result = m_stream.ReadBlit <Vector3>();

            Assert.AreEqual(m_testStruct, result);
        }
Ejemplo n.º 4
0
        public void UTF16Fast()
        {
            stream.ResetWrite(m_ptrBuf, SIZE);

            const string str = "4Jzqh8Jb4DRlTHFjHZfAO6vRVBVLnHxEfY4Ir9lNkbRN00tn6dtRneKOKsss15PEIex";

            stream.WriteString(str, BitEncoding.UTF16);

            stream.ResetRead();

            var replica = stream.ReadString(BitEncoding.UTF16);
        }
Ejemplo n.º 5
0
        public void ResetReadTest2()
        {
            BitStreamer bs = new BitStreamer();

            bs.ResetRead(new byte[22], 2, 20);

            Assert.Throws <ArgumentOutOfRangeException>(() => bs.ResetRead(new byte[22], 2, 21));

            Assert.AreEqual(24, bs.ByteLength);
            Assert.AreEqual(24 << 3, bs.BitLength);
            Assert.AreEqual(0, bs.BitOffset);
            bs.Dispose();
        }
Ejemplo n.º 6
0
        public void ResetReadTest3()
        {
            BitStreamer bs = new BitStreamer();

            Assert.Throws <ArgumentNullException>(() => bs.ResetRead((IntPtr)null, 10));

            IntPtr ptr = Marshal.AllocHGlobal(30);

            bs.ResetRead(ptr, 30, true);

            Assert.AreEqual(32 << 3, bs.BitLength);
            Assert.AreEqual(0, bs.BitOffset);
            bs.Dispose();
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public void ReadwriteTest(int value)
        {
            m_stream.WriteInt32(value);
            Assert.AreEqual(32, m_stream.BitOffset);

            m_stream.ResetRead();
            Assert.AreEqual(value, m_stream.ReadInt32());
            Assert.AreEqual(32, m_stream.BitOffset);
        }
Ejemplo n.º 10
0
        public void ResetWithoutBufferTest()
        {
            BitStreamer bs = new BitStreamer();

            Assert.Throws <InvalidOperationException>(() =>
            {
                bs.ResetRead();
            });
        }
Ejemplo n.º 11
0
        public void ResetReadTest1()
        {
            BitStreamer bs = new BitStreamer();

            bs.ResetRead(new byte[10]);

            Assert.AreEqual(16, bs.ByteLength);
            Assert.AreEqual(16 << 3, bs.BitLength);
            Assert.AreEqual(0, bs.BitOffset);
            Assert.IsTrue(bs.OwnsBuffer);
            bs.Dispose();
        }
Ejemplo n.º 12
0
        public void CopyWithResize()
        {
            BitStreamer bs = new BitStreamer();

            bs.ResetWrite(8);

            Assert.AreEqual(8, bs.ByteLength);

            bs.ResetRead(new byte[16]);
            Assert.AreEqual(16, bs.ByteLength);
            Assert.IsTrue(bs.OwnsBuffer);

            bs.Dispose();
        }
Ejemplo n.º 13
0
        public void ResetWriteOwnsBufferTest()
        {
            BitStreamer bs = new BitStreamer();

            bs.ResetWrite(16);
            bs.WriteByte(1);
            bs.WriteByte(2);

            Assert.AreEqual(16, bs.BitOffset);
            Assert.AreEqual(16, bs.ByteLength);

            // Confirm values are there.
            bs.ResetRead();
            Assert.AreEqual(1, bs.ReadByte());
            Assert.AreEqual(2, bs.ReadByte());

            bs.ResetWrite();
            Assert.AreEqual(0, bs.BitOffset);
            Assert.AreEqual(16, bs.ByteLength);

            // Confirm values have been zeroed.
            bs.ResetRead();
            Assert.AreEqual(0, bs.ReadByte());
        }
Ejemplo n.º 14
0
        public void ResetReadOffsetTest()
        {
            var arr = new byte[16];

            arr[5] = 123;

            BitStreamer bs = new BitStreamer();

            bs.ResetRead(arr, 5, 10);

            Assert.AreEqual(16, bs.ByteLength);
            Assert.AreEqual(0, bs.ByteOffset);

            Assert.AreEqual(123, bs.ReadByte());
        }
Ejemplo n.º 15
0
        public void ReadWriteIntTest()
        {
            int         val = 123456789;
            BitStreamer bs  = new BitStreamer();

            bs.ResetWrite(64);

            bs.WriteInt32(val);

            Assert.AreEqual(32, bs.BitOffset);

            bs.ResetRead();

            Assert.AreEqual(val, bs.ReadInt32());
        }
Ejemplo n.º 16
0
        public unsafe void ReadBufferCopyTest()
        {
            ulong  value = 85830981411525;
            IntPtr buf   = Marshal.AllocHGlobal(8);

            *(ulong *)buf = value;

            BitStreamer reader = new BitStreamer();

            reader.ResetRead(buf, 8, false);

            Assert.AreEqual(64, reader.BitLength);
            Assert.AreEqual(0, reader.BitOffset);

            Assert.AreEqual(value, reader.ReadULong());
        }
Ejemplo n.º 17
0
        public void ResetWriteCopyBufferNull()
        {
            BitStreamer bs = new BitStreamer();

            IntPtr buff = Memory.Alloc(16);

            *(byte *)buff = 212;

            bs.ResetWrite(buff, 16, true);
            Assert.AreEqual(16, bs.ByteLength);
            Assert.AreEqual(16, bs.ByteOffset);

            bs.ResetRead();
            Assert.AreEqual(212, bs.ReadByte());
            Assert.IsTrue(bs.OwnsBuffer);

            Memory.Free(buff);
        }
Ejemplo n.º 18
0
        public void BitStream()
        {
            BitStreamer stream = new BitStreamer();

            stream.ResetWrite(m_ptrBuf, SIZE, false);

            for (int i = 0; i < AMOUNT / 2; i++)
            {
                stream.WriteBool(i % 2 == 0);
                stream.WriteInt32(i);
            }

            stream.ResetRead();
            for (int i = 0; i < AMOUNT / 2; i++)
            {
                bool b   = stream.ReadBool();
                int  num = stream.ReadInt32();
            }
        }
Ejemplo n.º 19
0
        public void NonAlignBufferSet()
        {
            IntPtr ptr = Memory.Alloc(12);

            BitStreamer bs = new BitStreamer();

            bs.ResetRead(ptr, 4);

            Assert.AreEqual(4, bs.ByteLength);
            bs.ReadFloat();

            // Should fail because we are exceeding the allowed size of 4.
            Assert.Throws <InvalidOperationException>(() =>
            {
                bs.ReadByte(1);
            });

            bs.ResetWrite();
            Assert.AreEqual(0, bs.ByteLength);
        }
Ejemplo n.º 20
0
        public void HalfTest(float value)
        {
            m_stream.WriteHalf(value);
            Assert.AreEqual(16, m_stream.BitOffset);

            m_stream.ResetRead();
            float peek = m_stream.PeekHalf();

            Assert.AreEqual(0, m_stream.BitOffset);

            float replica = m_stream.ReadHalf();

            Assert.AreEqual(16, m_stream.BitOffset);
            Assert.AreEqual(value, replica, 0.001f);
        }
Ejemplo n.º 21
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);
        }
Ejemplo n.º 22
0
        public void ReadWriteMultipleTest()
        {
            const bool   bVal   = true;
            const double dVal   = double.MaxValue / 3 * 2;
            const float  fVal   = float.MinValue / 5;
            const short  sVal   = -12345;
            const int    offset = 113;

            m_stream = new BitStreamer();
            m_stream.ResetWrite(64);
            m_stream.WriteBool(bVal);
            m_stream.WriteDouble(dVal);
            m_stream.WriteFloat(fVal);
            m_stream.WriteShort(sVal);
            Assert.AreEqual(offset, m_stream.BitOffset);

            m_stream.ResetRead();
            Assert.AreEqual(bVal, m_stream.ReadBool());
            Assert.AreEqual(dVal, m_stream.ReadDouble());
            Assert.AreEqual(fVal, m_stream.ReadFloat());
            Assert.AreEqual(sVal, m_stream.ReadShort());
            Assert.AreEqual(offset, m_stream.BitOffset);
        }
Ejemplo n.º 23
0
        public void ASCIITest(string value)
        {
            m_stream.WriteString(value, Encoding.ASCII);
            Assert.AreEqual(sizeof(ushort) + value.Length, m_stream.ByteOffset);

            m_stream.ResetRead();
            Assert.AreEqual(value, m_stream.ReadString(Encoding.ASCII));
        }
Ejemplo n.º 24
0
        public void Vector2Test()
        {
            Vector2 value = new Vector2(rnd.NextFloat(), rnd.NextFloat());

            stream.Skip(1);
            stream.WriteVector2(value);

            Assert.AreEqual(65, stream.BitOffset);
            stream.ResetRead();
            stream.Skip(1);

            Assert.AreEqual(value, stream.ReadVector2());
            Assert.AreEqual(65, stream.BitOffset);
        }