public void TestPutPrefixedStringWithPrefixLength() { Encoding encoding = Encoding.GetEncoding("ISO-8859-1"); IoBuffer buf = IoBuffer.Allocate(16).Sweep(); buf.AutoExpand = true; buf.PutPrefixedString("A", 1, encoding); Assert.AreEqual(2, buf.Position); Assert.AreEqual(1, buf.Get(0)); Assert.AreEqual((byte)'A', buf.Get(1)); buf.Sweep(); buf.PutPrefixedString("A", 2, encoding); Assert.AreEqual(3, buf.Position); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual(1, buf.Get(1)); Assert.AreEqual((byte)'A', buf.Get(2)); buf.Sweep(); buf.PutPrefixedString("A", 4, encoding); Assert.AreEqual(5, buf.Position); Assert.AreEqual(0, buf.Get(0)); Assert.AreEqual(0, buf.Get(1)); Assert.AreEqual(0, buf.Get(2)); Assert.AreEqual(1, buf.Get(3)); Assert.AreEqual((byte)'A', buf.Get(4)); }
public void TestSweepNonZeros() { IoBuffer buf = ByteBufferAllocator.Instance.Allocate(4); Int32 i; unchecked { i = (Int32)0xdeadbeef; } buf.PutInt32(i); buf.Clear(); Assert.AreEqual(i, buf.GetInt32()); Assert.AreEqual(4, buf.Position); Assert.AreEqual(4, buf.Limit); buf.Sweep((byte)0x45); Assert.AreEqual(0, buf.Position); Assert.AreEqual(4, buf.Limit); Assert.AreEqual(0x45454545, buf.GetInt32()); }
public void TestSweepWithZeros() { IoBuffer buf = allocator.Allocate(4); Int32 i; unchecked { i = (Int32)0xdeadbeef; } buf.PutInt32(i); buf.Clear(); Assert.AreEqual(i, buf.GetInt32()); Assert.AreEqual(4, buf.Position); Assert.AreEqual(4, buf.Limit); buf.Sweep(); Assert.AreEqual(0, buf.Position); Assert.AreEqual(4, buf.Limit); Assert.AreEqual(0x0, buf.GetInt32()); }
public void TestAutoShrink() { IoBuffer buf = ByteBufferAllocator.Instance.Allocate(8); buf.AutoShrink = true; // Make sure the buffer doesn't shrink too much (less than the initial // capacity.) buf.Sweep((byte)1); buf.Fill(7); buf.Compact(); Assert.AreEqual(8, buf.Capacity); Assert.AreEqual(1, buf.Position); Assert.AreEqual(8, buf.Limit); buf.Clear(); Assert.AreEqual(1, buf.Get()); // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer shrinks when only 1/4 is being used. buf.Sweep((byte)1); buf.Fill(24); buf.Compact(); Assert.AreEqual(16, buf.Capacity); Assert.AreEqual(8, buf.Position); Assert.AreEqual(16, buf.Limit); buf.Clear(); for (int i = 0; i < 8; i++) { Assert.AreEqual(1, buf.Get()); } // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer shrinks when only 1/8 is being used. buf.Sweep((byte)1); buf.Fill(28); buf.Compact(); Assert.AreEqual(8, buf.Capacity); Assert.AreEqual(4, buf.Position); Assert.AreEqual(8, buf.Limit); buf.Clear(); for (int i = 0; i < 4; i++) { Assert.AreEqual(1, buf.Get()); } // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer shrinks when 0 byte is being used. buf.Fill(32); buf.Compact(); Assert.AreEqual(8, buf.Capacity); Assert.AreEqual(0, buf.Position); Assert.AreEqual(8, buf.Limit); // Expand the buffer. buf.Capacity = 32; buf.Clear(); Assert.AreEqual(32, buf.Capacity); // Make sure the buffer doesn't shrink when more than 1/4 is being used. buf.Sweep((byte)1); buf.Fill(23); buf.Compact(); Assert.AreEqual(32, buf.Capacity); Assert.AreEqual(9, buf.Position); Assert.AreEqual(32, buf.Limit); buf.Clear(); for (int i = 0; i < 9; i++) { Assert.AreEqual(1, buf.Get()); } }
/// <inheritdoc/> public override IoBuffer Sweep() { _buf.Sweep(); return(this); }