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 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 UTF16Fast() { stream.ResetWrite(m_ptrBuf, SIZE); const string str = "4Jzqh8Jb4DRlTHFjHZfAO6vRVBVLnHxEfY4Ir9lNkbRN00tn6dtRneKOKsss15PEIex"; stream.WriteString(str, BitEncoding.UTF16); stream.ResetRead(); var replica = stream.ReadString(BitEncoding.UTF16); }
public void ResetWriteTest1() { BitStreamer bs = new BitStreamer(); bs.ResetWrite(); Assert.AreEqual(BitStreamer.DefaultSize, bs.ByteLength); IntPtr ptr = bs.Buffer; bs.ResetWrite(); Assert.AreEqual(ptr, bs.Buffer); bs.Dispose(); }
public void ResetWriteInvalid() { BitStreamer bs = new BitStreamer(); IntPtr buff = Memory.Alloc(16); *(byte *)buff = 212; bs.ResetWrite(8); Assert.Throws <InvalidOperationException>(() => { bs.ResetWrite(buff, 16); }); }
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 ResetWriteTest2() { BitStreamer bs = new BitStreamer(); bs.ResetWrite(10); IntPtr ptr = bs.Buffer; Assert.AreEqual(16, bs.ByteLength); bs.ResetWrite(12); Assert.AreEqual(16, bs.ByteLength); Assert.AreEqual(ptr, bs.Buffer); bs.ResetWrite(17); Assert.AreEqual(24, bs.ByteLength); bs.Dispose(); Assert.AreEqual(IntPtr.Zero, bs.Buffer); }
public void DisposeWriterTest() { BitStreamer bs = new BitStreamer(); bs.Dispose(); bs.ResetWrite(16); bs.Dispose(); Assert.AreEqual(IntPtr.Zero, bs.Buffer); bs.ResetWrite(18); Assert.AreEqual(24, bs.ByteLength); bs.Dispose(); Assert.AreEqual(IntPtr.Zero, bs.Buffer); Assert.AreEqual(0, bs.BitLength); Assert.AreEqual(0, bs.BitOffset); Assert.AreEqual(SerializationMode.None, bs.Mode); }
public void ZeroLargeTest() { BitStreamer bs = new BitStreamer(); bs.ResetWrite(8); Assert.AreEqual(8, bs.ByteLength); bs.Skip(20 << 3); Assert.AreEqual(24, bs.ByteLength); }
public void ExpandTest() { BitStreamer bs = new BitStreamer(); bs.ResetWrite(7); Assert.AreEqual(8, bs.ByteLength); bs.WriteLong(1); bs.WriteLong(2); Assert.AreEqual(16, bs.ByteLength); }
public void ResetWriteCopy() { BitStreamer bs = new BitStreamer(); IntPtr buff = Memory.Alloc(16); *(byte *)buff = 212; bs.ResetWrite(32); Assert.AreEqual(32, bs.ByteLength); bs.ResetWrite(buff, 16, true); Assert.AreEqual(32, bs.ByteLength); Assert.AreEqual(16, bs.ByteOffset); bs.ResetRead(); Assert.AreEqual(212, bs.ReadByte()); Assert.IsTrue(bs.OwnsBuffer); Memory.Free(buff); }
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(); }
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()); }
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()); }
public void SizeTest() { BitStreamer bs = new BitStreamer(); bs.ResetWrite(60); // Rounded to next multiple of 8 = 64; Assert.AreEqual(64, bs.ByteLength); Assert.AreEqual(64 * 8, bs.BitLength); bs.WriteInt32(1, 28); Assert.AreEqual(28, bs.BitOffset); Assert.AreEqual(28 / (double)8, bs.ByteOffset); Assert.AreEqual(4, bs.BytesUsed); }
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(); }
public void ExpandFailTest() { BitStreamer bs = new BitStreamer(); IntPtr ptr = Marshal.AllocHGlobal(9); bs.ResetWrite(ptr, 9, false); Assert.AreEqual(8, bs.ByteLength); bs.WriteLong(1); Assert.Throws <InvalidOperationException>(() => { bs.WriteLong(2); }); Assert.AreEqual(8, bs.ByteLength); }
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(); } }
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); }
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 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); }
public void Init() { m_stream.ResetWrite(64); m_testStruct = new Vector3(m_random.NextSingle(), m_random.NextSingle(), m_random.NextSingle()); }
public void Init() { m_stream.ResetWrite(64); }
public void Init() { stream.ResetWrite(128); }