public void TestToBytes() { var data0 = RandomBytes(7101, -0x02F0B4EF); using (var bb = new TestByteBuffer(data0)) { var dst = bb.ToBytes(); Assert.AreEqual(7101, dst.Length, "ToBytes() - length"); Assert.AreEqual(0, bb.StreamPosition, "ToBytes() - StreamPosition"); AssertAreEqual(data0, dst, "ToBytes()"); bb.InitializeStream(); dst = bb.ToBytes(); Assert.AreEqual(7101, dst.Length, "ToBytes() - length - after Stream initialized"); Assert.AreEqual(0, bb.StreamPosition, "ToBytes() - StreamPosition - after Stream initialized"); AssertAreEqual(data0, dst, "ToBytes() - after Stream initialized"); } using (var bb = new TestByteBuffer(data0)) { var dst = bb.ToBytes(389, 5803); Assert.AreEqual(5803, dst.Length, "ToBytes(int,int) - length"); Assert.AreEqual(0, bb.StreamPosition, "ToBytes(int,int) - StreamPosition"); AssertAreEqual(data0, 389, dst, 0, 5803, "ToBytes(int,int)"); bb.InitializeStream(); dst = bb.ToBytes(389, 5803); Assert.AreEqual(5803, dst.Length, "ToBytes(int,int) - length - after Stream initialized"); Assert.AreEqual(0, bb.StreamPosition, "ToBytes(int,int) - StreamPosition - after Stream initialized"); AssertAreEqual(data0, 389, dst, 0, 5803, "ToBytes(int,int) - after Stream initialized"); } }
public void TestCopyToBinaryWriterLargeMemoryStream() { var data0 = RandomBytes(6001001, -0x02F0B4EF); using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bb.CopyTo(bw); Assert.AreEqual(6001001, ms.Length, "CopyTo(BinaryWriter(MemoryStream)) - length"); Assert.AreEqual(0, bb.StreamPosition, "CopyTo(BinaryWriter(MemoryStream)) - StreamPosition - after Stream initialized"); AssertAreEqual(data0, ToArray(ms), "CopyTo(BinaryWriter(MemoryStream)) - destination data"); AssertAreEqual(data0, bb.DumpTestData(), "CopyTo(BinaryWriter(MemoryStream)) - source data"); } using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bb.InitializeStream(position: 42); bb.CopyTo(bw); Assert.AreEqual(6001001, ms.Length, "CopyTo(BinaryWriter(MemoryStream)) - length - after Stream initialized"); Assert.AreEqual(42, bb.StreamPosition, "CopyTo(BinaryWriter(MemoryStream)) - StreamPosition - after Stream initialized"); AssertAreEqual(data0, ToArray(ms), "CopyTo(BinaryWriter(MemoryStream)) - destination data after Stream initialized"); AssertAreEqual(data0, bb.DumpTestData(), "CopyTo(BinaryWriter(MemoryStream)) - source data after Stream initialized"); } }
public void TestCopyFrom() { var data0 = RandomBytes(9432, -0x02F0B4EF); var data1 = RandomBytes(8472, -0x7BFC1702); using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream(TestByteBuffer.CopyBytes(data1))) { var result = bb.CopyFrom(ms, 4195); Assert.AreEqual(4195, bb.Length, "CopyFrom - length"); Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition"); Assert.AreEqual(4195, result, "CopyFrom - first batch return value"); AssertAreEqual(data1, 0, bb.DumpTestData(), 0, 4195, "CopyFrom - first batch data"); result = bb.CopyFrom(ms, 9000); Assert.AreEqual(9000, bb.Length, "CopyFrom - length"); // apparently the length of the buffer after CopyFrom is the bytes requested, and not actual bytes read Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition"); Assert.AreEqual(4277, result, "CopyFrom - second batch return value"); AssertAreEqual(data1, 4195, bb.DumpTestData(), 0, 4277, "CopyFrom - second batch data"); } using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream(TestByteBuffer.CopyBytes(data1))) { bb.InitializeStream(position: 42); var result = bb.CopyFrom(ms, 4195); Assert.AreEqual(4195, bb.Length, "CopyFrom - length - after Stream initialized"); Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition - after Stream initialized"); Assert.AreEqual(4195, result, "CopyFrom - first batch return value - after Stream initialized"); AssertAreEqual(data1, 0, bb.DumpTestData(), 0, 4195, "CopyFrom - first batch data - after Stream initialized"); bb.InitializeStream(position: 42); result = bb.CopyFrom(ms, 9000); Assert.AreEqual(9000, bb.Length, "CopyFrom - length - after Stream initialized"); // apparently the length of the buffer after CopyFrom is the bytes requested, and not actual bytes read Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition - after Stream initialized"); Assert.AreEqual(4277, result, "CopyFrom - second batch return value - after Stream initialized"); AssertAreEqual(data1, 4195, bb.DumpTestData(), 0, 4277, "CopyFrom - second batch data - after Stream initialized"); } }
public void TestCopyToBytes() { var data0 = RandomBytes(10000000, 0); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) { var dst = new byte[10000000]; bb.CopyTo(dst, 0, 750000); stats += bb.LastStatistics; bb.CopyTo(dst, 750000, 750000 + 1); stats += bb.LastStatistics; bb.CopyTo(dst, 1500000 + 1, 500000); stats += bb.LastStatistics; bb.CopyTo(dst, 2000000 + 1, 1000000 - 1); stats += bb.LastStatistics; bb.CopyTo(dst, 3000000, 1001101); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) { var dst = new byte[10000000]; bb.InitializeStream(); bb.CopyTo(dst, 0, 750000); stats += bb.LastStatistics; bb.CopyTo(dst, 750000, 750000 + 1); stats += bb.LastStatistics; bb.CopyTo(dst, 1500000 + 1, 500000); stats += bb.LastStatistics; bb.CopyTo(dst, 2000000 + 1, 1000000 - 1); stats += bb.LastStatistics; bb.CopyTo(dst, 3000000, 1001101); stats += bb.LastStatistics; } Console.WriteLine(stats); }
public void TestCopyToBinaryWriterLargeMemoryStream() { var data0 = RandomBytes(10000000, 0); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream()) using (var bw = new BinaryWriter(ms)) { bb.InitializeStream(); bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; bb.CopyTo(bw); stats += bb.LastStatistics; } Console.WriteLine(stats); }
public void TestCopyFrom() { var data0 = RandomBytes(10000000, 0); var data1 = RandomBytes(10000000, 1); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream(TestByteBuffer.CopyBytes(data1))) { bb.CopyFrom(ms, 750000); stats += bb.LastStatistics; bb.CopyFrom(ms, 750000 + 1); stats += bb.LastStatistics; bb.CopyFrom(ms, 500000); stats += bb.LastStatistics; bb.CopyFrom(ms, 1000000 - 1); stats += bb.LastStatistics; bb.CopyFrom(ms, 1001101); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream(TestByteBuffer.CopyBytes(data1))) { bb.InitializeStream(); bb.CopyFrom(ms, 750000); stats += bb.LastStatistics; bb.CopyFrom(ms, 750000 + 1); stats += bb.LastStatistics; bb.CopyFrom(ms, 500000); stats += bb.LastStatistics; bb.CopyFrom(ms, 1000000 - 1); stats += bb.LastStatistics; bb.CopyFrom(ms, 1001101); stats += bb.LastStatistics; } Console.WriteLine(stats); }
public void TestAppend() { var data0 = RandomBytes(10000000, 0); var data1 = RandomBytes(10000000, 1); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) { bb.Append(data1, 0, 750000); stats += bb.LastStatistics; bb.Append(data1, 750000, 750000 + 1); stats += bb.LastStatistics; bb.Append(data1, 1500000 + 1, 500000); stats += bb.LastStatistics; bb.Append(data1, 2000000 + 1, 1000000 - 1); stats += bb.LastStatistics; bb.Append(data1, 3000000, 1001101); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(); bb.Append(data1, 0, 750000); stats += bb.LastStatistics; bb.Append(data1, 750000, 750000 + 1); stats += bb.LastStatistics; bb.Append(data1, 1500000 + 1, 500000); stats += bb.LastStatistics; bb.Append(data1, 2000000 + 1, 1000000 - 1); stats += bb.LastStatistics; bb.Append(data1, 3000000, 1001101); stats += bb.LastStatistics; } Console.WriteLine(stats); }
public void TestFromBytes() { var data0 = RandomBytes(10000000, 0); var data1 = RandomBytes(10000000, 1); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) { bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(); bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; bb.FromBytes(data1); stats += bb.LastStatistics; } Console.WriteLine(stats); }
public void TestGetChunk() { var data0 = RandomBytes(7101, -0x02F0B4EF); using (var bb = new TestByteBuffer(data0)) { var dst = bb.GetChunk(1007, 4361); Assert.AreEqual(4361, dst.Length, "GetChunk(int,int) - length"); Assert.AreEqual(0, bb.StreamPosition, "GetChunk(int,int) - StreamPosition"); AssertAreEqual(data0, 1007, dst, 0, 4361, "GetChunk(int,int) - actual data"); bb.InitializeStream(position: 42); dst = bb.GetChunk(1007, 4361); Assert.AreEqual(4361, dst.Length, "GetChunk(int,int) - length - after Stream initialized"); Assert.AreEqual(42, bb.StreamPosition, "GetChunk(int,int) - StreamPosition - after Stream initialized"); AssertAreEqual(data0, 1007, dst, 0, 4361, "GetChunk(int,int) - actual data after Stream initialized"); } }
public void TestSwap1() { var data0 = RandomBytes(8472, -0x02F0B4EF); using (var bb = new TestByteBuffer(data0)) { bb.Swap(1); Assert.AreEqual(0, bb.StreamPosition, "Swap(1) - StreamPosition"); AssertAreEqual(data0, bb.DumpTestData(), "Swap(1)"); } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(position: 42); bb.Swap(1); Assert.AreEqual(42, bb.StreamPosition, "Swap(1) - StreamPosition - after Stream initialized"); AssertAreEqual(data0, bb.DumpTestData(), "Swap(1) - after Stream initialized"); } }
public void TestCopyToStream() { var data0 = RandomBytes(7101, -0x02F0B4EF); using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream()) { bb.CopyTo(ms, 1237, 4193); Assert.AreEqual(4193, ms.Length, "CopyTo(Stream,int,int) - length"); Assert.AreEqual(0, bb.StreamPosition, "CopyTo(Stream,int,int) - StreamPosition"); AssertAreEqual(data0, 1237, ToArray(ms), 0, 4193, "CopyTo(Stream,int,int)"); bb.CopyTo(ms, 642, 942); Assert.AreEqual(4193 + 942, ms.Length, "CopyTo(Stream,int,int) - length after append"); Assert.AreEqual(0, bb.StreamPosition, "CopyTo(Stream,int,int) - StreamPosition after append"); AssertAreEqual(data0, 1237, ToArray(ms), 0, 4193, "CopyTo(Stream,int,int) - existing data"); AssertAreEqual(data0, 642, ToArray(ms), 4193, 942, "CopyTo(Stream,int,int) - new appended data"); } using (var bb = new TestByteBuffer(data0)) using (var ms = new MemoryStream()) { bb.InitializeStream(position: 42); bb.CopyTo(ms, 1237, 4193); Assert.AreEqual(4193, ms.Length, "CopyTo(Stream,int,int) - length - after Stream initialized"); Assert.AreEqual(42, bb.StreamPosition, "CopyTo(Stream,int,int) - StreamPosition - after Stream initialized"); AssertAreEqual(data0, 1237, ToArray(ms), 0, 4193, "CopyTo(Stream,int,int) - after Stream initialized"); bb.CopyTo(ms, 642, 942); Assert.AreEqual(4193 + 942, ms.Length, "CopyTo(Stream,int,int) - length after append - after Stream initialized"); Assert.AreEqual(42, bb.StreamPosition, "CopyTo(Stream,int,int) - StreamPosition after append - after Stream initialized"); AssertAreEqual(data0, 1237, ToArray(ms), 0, 4193, "CopyTo(Stream,int,int) - existing data - after Stream initialized"); AssertAreEqual(data0, 642, ToArray(ms), 4193, 942, "CopyTo(Stream,int,int) - new appended data - after Stream initialized"); } }
public void TestChop() { var data0 = RandomBytes(6000, -0x02F0B4EF); using (var bb = new TestByteBuffer(data0)) { bb.Chop(6000); Assert.AreEqual(0, bb.Length, "Chop(6000) - Length"); Assert.AreEqual(0, bb.StreamPosition, "Chop(6000) - StreamPosition"); AssertAreEqual(new byte[0], bb.DumpTestData(), "Chop(6000)"); } using (var bb = new TestByteBuffer(data0)) { bb.Chop(401); Assert.AreEqual(6000 - 401, bb.Length, "Chop(401) - Length"); Assert.AreEqual(0, bb.StreamPosition, "Chop(401) - StreamPosition"); AssertAreEqual(data0, 401, bb.DumpTestData(), 0, 5599, "Chop(401)"); } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(position: 42); bb.Chop(6000); Assert.AreEqual(0, bb.Length, "Chop(6000) - Length - after Stream initialized"); Assert.AreEqual(0, bb.StreamPosition, "Chop(6000) - StreamPosition - after Stream initialized"); AssertAreEqual(new byte[0], bb.DumpTestData(), "Chop(6000) - after Stream initialized"); } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(position: 42); bb.Chop(401); Assert.AreEqual(6000 - 401, bb.Length, "Chop(401) - Length - after Stream initialized"); Assert.AreEqual(0, bb.StreamPosition, "Chop(401) - StreamPosition - after Stream initialized"); AssertAreEqual(data0, 401, bb.DumpTestData(), 0, 5599, "Chop(401) - after Stream initialized"); } }
public void TestCopyToBytes() { var data0 = RandomBytes(7101, -0x02F0B4EF); using (var bb = new TestByteBuffer(data0)) { var dst = new byte[6731]; bb.CopyTo(dst, 1007, 4361); Assert.AreEqual(0, bb.StreamPosition, "CopyTo(byte[],int,int) - StreamPosition"); AssertAreEqual(data0, 1007, dst, 0, 4361, "CopyTo(byte[],int,int) - actual data"); AssertAreEqual(new byte[6731 - 4361], 0, dst, 4361, 6731 - 4361, "CopyTo(byte[],int,int) - zero data"); dst = new byte[6731]; bb.InitializeStream(position: 42); bb.CopyTo(dst, 1007, 4361); Assert.AreEqual(42, bb.StreamPosition, "CopyTo(byte[],int,int) - StreamPosition after Stream initialized"); AssertAreEqual(data0, 1007, dst, 0, 4361, "CopyTo(byte[],int,int) - actual data after Stream initialized"); AssertAreEqual(new byte[6731 - 4361], 0, dst, 4361, 6731 - 4361, "CopyTo(byte[],int,int) - zero data after Stream initialized"); } using (var bb = new TestByteBuffer(data0)) { var dst = new byte[6731]; bb.CopyTo(dst, 1007, 723, 4361); Assert.AreEqual(0, bb.StreamPosition, "CopyTo(byte[],int,int,int) - StreamPosition"); AssertAreEqual(data0, 1007, dst, 723, 4361, "CopyTo(byte[],int,int,int) - actual data"); AssertAreEqual(new byte[723], 0, dst, 0, 723, "CopyTo(byte[],int,int,int) - zero data before actual"); AssertAreEqual(new byte[6731 - (4361 + 723)], 0, dst, 723 + 4361, 6731 - (4361 + 723), "CopyTo(byte[],int,int,int) - zero data after actual"); dst = new byte[6731]; bb.InitializeStream(position: 42); bb.CopyTo(dst, 1007, 723, 4361); Assert.AreEqual(42, bb.StreamPosition, "CopyTo(byte[],int,int,int) - StreamPosition after Stream initialized"); AssertAreEqual(data0, 1007, dst, 723, 4361, "CopyTo(byte[],int,int,int) - actual data after Stream initialized"); AssertAreEqual(new byte[723], 0, dst, 0, 723, "CopyTo(byte[],int,int,int) - zero data before actual after Stream initialized"); AssertAreEqual(new byte[6731 - (4361 + 723)], 0, dst, 723 + 4361, 6731 - (4361 + 723), "CopyTo(byte[],int,int,int) - zero data after actual after Stream initialized"); } }
public void TestFromBytes() { var data0 = RandomBytes(7101, -0x02F0B4EF); var data1 = RandomBytes(9431, -0x7BFC1702); using (var bb = new TestByteBuffer(data0)) { bb.FromBytes(data1); Assert.AreEqual(9431, bb.Length, "FromBytes - length"); Assert.AreEqual(0, bb.StreamPosition, "FromBytes - StreamPosition"); AssertAreEqual(data1, bb.DumpTestData(), "FromBytes"); } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(position: 42); bb.FromBytes(data1); Assert.AreEqual(9431, bb.Length, "FromBytes - length - after Stream initialized"); Assert.AreEqual(0, bb.StreamPosition, "FromBytes - StreamPosition - after Stream initialized"); AssertAreEqual(data1, bb.DumpTestData(), "FromBytes - after Stream initialized"); } }
public void TestAppend() { var data0 = RandomBytes(8472, -0x02F0B4EF); var data1 = RandomBytes(8472, -0x7BFC1702); using (var bb = new TestByteBuffer(data0)) { bb.Append(data1, 4195, 4194); Assert.AreEqual(8472 + 4194, bb.Length, "Append - length"); Assert.AreEqual(0, bb.StreamPosition, "Append - StreamPosition"); AssertAreEqual(data0, 0, bb.DumpTestData(), 0, 8472, "Append - original data"); AssertAreEqual(data1, 4195, bb.DumpTestData(), 8472, 4194, "Append - appended data"); } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(position: 42); bb.Append(data1, 4195, 4194); Assert.AreEqual(8472 + 4194, bb.Length, "Append - length - after Stream initialized"); Assert.AreEqual(42, bb.StreamPosition, "Append - StreamPosition - after Stream initialized"); AssertAreEqual(data0, 0, bb.DumpTestData(), 0, 8472, "Append - original data - after Stream initialized"); AssertAreEqual(data1, 4195, bb.DumpTestData(), 8472, 4194, "Append - appended data - after Stream initialized"); } }
public void TestGetChunk() { var data0 = RandomBytes(10000000, 0); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) { bb.GetChunk(0, 750000); stats += bb.LastStatistics; bb.GetChunk(750000, 750000 + 1); stats += bb.LastStatistics; bb.GetChunk(1500000 + 1, 500000); stats += bb.LastStatistics; bb.GetChunk(2000000 + 1, 1000000 - 1); stats += bb.LastStatistics; bb.GetChunk(3000000, 1001101); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(); bb.GetChunk(0, 750000); stats += bb.LastStatistics; bb.GetChunk(750000, 750000 + 1); stats += bb.LastStatistics; bb.GetChunk(1500000 + 1, 500000); stats += bb.LastStatistics; bb.GetChunk(2000000 + 1, 1000000 - 1); stats += bb.LastStatistics; bb.GetChunk(3000000, 1001101); stats += bb.LastStatistics; } Console.WriteLine(stats); }
public void TestSwapCustom() { var data0 = RandomBytes(10000004, 0); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) { bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(); bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; bb.Swap(7); stats += bb.LastStatistics; } Console.WriteLine(stats); }
public void TestChop() { var data0 = RandomBytes(10000000, 0); var stats = TestByteBufferStatistics.Empty; using (var bb = new TestByteBuffer(data0)) { bb.Chop(1000000); stats += bb.LastStatistics; bb.Chop(1000000 - 1); stats += bb.LastStatistics; bb.Chop(1000000 + 1); stats += bb.LastStatistics; bb.Chop(1000000 - 2); stats += bb.LastStatistics; bb.Chop(1000000 + 2); stats += bb.LastStatistics; } using (var bb = new TestByteBuffer(data0)) { bb.InitializeStream(); bb.Chop(1000000); stats += bb.LastStatistics; bb.Chop(1000000 - 1); stats += bb.LastStatistics; bb.Chop(1000000 + 1); stats += bb.LastStatistics; bb.Chop(1000000 - 2); stats += bb.LastStatistics; bb.Chop(1000000 + 2); stats += bb.LastStatistics; } Console.WriteLine(stats); }