diff --git a/XUnitTest.Core/Buffers/SpanReaderTests.cs b/XUnitTest.Core/Buffers/SpanReaderTests.cs index 2d3e7cd07..b54a2fde5 100644 --- a/XUnitTest.Core/Buffers/SpanReaderTests.cs +++ b/XUnitTest.Core/Buffers/SpanReaderTests.cs @@ -115,4 +115,73 @@ public void Test2(Boolean isLittle) writer.Write(str, -1, Encoding.Default); Assert.Equal(str, reader.ReadString(-1, Encoding.Default)[..str.Length]); } + + [Fact] + public void ReadByteTest() + { + var data = new Byte[] { 1, 2, 3 }; + var reader = new SpanReader(data); + + Assert.Equal(1, reader.ReadByte()); + Assert.Equal(2, reader.ReadByte()); + Assert.Equal(3, reader.ReadByte()); + } + + [Fact] + public void ReadInt16Test() + { + var data = new Byte[] { 1, 0, 2, 0 }; + var reader = new SpanReader(data); + + Assert.Equal(1, reader.ReadInt16()); + Assert.Equal(2, reader.ReadInt16()); + } + + [Fact] + public void ReadInt32Test() + { + var data = new Byte[] { 1, 0, 0, 0, 2, 0, 0, 0 }; + var reader = new SpanReader(data); + + Assert.Equal(1, reader.ReadInt32()); + Assert.Equal(2, reader.ReadInt32()); + } + + [Fact] + public void ReadStringTest() + { + var data = Encoding.UTF8.GetBytes("Hello, World!"); + var reader = new SpanReader(data); + + Assert.Equal("Hello, World!", reader.ReadString(data.Length)); + } + + [Fact] + public void ReadBytesTest() + { + var data = new Byte[] { 1, 2, 3, 4, 5 }; + var reader = new SpanReader(data); + + var result = reader.ReadBytes(3); + Assert.Equal(new Byte[] { 1, 2, 3 }, result.ToArray()); + } + + [Fact] + public void GCAllocationTest() + { + var data = new Byte[100]; + var reader = new SpanReader(data); + + var initialGen0Collections = GC.CollectionCount(0); + var initialGen1Collections = GC.CollectionCount(1); + var initialGen2Collections = GC.CollectionCount(2); + var memory = GC.GetAllocatedBytesForCurrentThread(); + + reader.ReadBytes(50); + + Assert.Equal(memory, GC.GetAllocatedBytesForCurrentThread()); + Assert.Equal(initialGen0Collections, GC.CollectionCount(0)); + Assert.Equal(initialGen1Collections, GC.CollectionCount(1)); + Assert.Equal(initialGen2Collections, GC.CollectionCount(2)); + } } diff --git a/XUnitTest.Core/Buffers/SpanWriterTests.cs b/XUnitTest.Core/Buffers/SpanWriterTests.cs index c1f651e3d..7f23fb9bd 100644 --- a/XUnitTest.Core/Buffers/SpanWriterTests.cs +++ b/XUnitTest.Core/Buffers/SpanWriterTests.cs @@ -1,4 +1,5 @@ using System; +using System.Text; using NewLife.Buffers; using Xunit; @@ -26,4 +27,194 @@ public void CtorTest() //Assert.Throws(() => writer.GetSpan(100)); } + + [Fact] + public void TestAdvance() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Advance(5); + Assert.Equal(5, writer.Position); + } + + [Fact] + public void TestGetSpan() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + var span = writer.GetSpan(5); + Assert.Equal(10, span.Length); + } + + [Fact] + public void TestWriteByte() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.WriteByte(1); + Assert.Equal(1, writer.Position); + Assert.Equal(1, buffer[0]); + } + + [Fact] + public void TestWriteInt16() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write((Int16)1); + Assert.Equal(2, writer.Position); + Assert.Equal(1, BitConverter.ToInt16(buffer, 0)); + } + + [Fact] + public void TestWriteUInt16() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write((UInt16)1); + Assert.Equal(2, writer.Position); + Assert.Equal(1, BitConverter.ToUInt16(buffer, 0)); + } + + [Fact] + public void TestWriteInt32() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write(1); + Assert.Equal(4, writer.Position); + Assert.Equal(1, BitConverter.ToInt32(buffer, 0)); + } + + [Fact] + public void TestWriteUInt32() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write((UInt32)1); + Assert.Equal(4, writer.Position); + Assert.Equal(1u, BitConverter.ToUInt32(buffer, 0)); + } + + [Fact] + public void TestWriteInt64() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write((Int64)1); + Assert.Equal(8, writer.Position); + Assert.Equal(1, BitConverter.ToInt64(buffer, 0)); + } + + [Fact] + public void TestWriteUInt64() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write((UInt64)1); + Assert.Equal(8, writer.Position); + Assert.Equal(1u, BitConverter.ToUInt64(buffer, 0)); + } + + [Fact] + public void TestWriteSingle() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write((Single)1.0); + Assert.Equal(4, writer.Position); + Assert.Equal(1.0f, BitConverter.ToSingle(buffer, 0)); + } + + [Fact] + public void TestWriteDouble() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write((Double)1.0); + Assert.Equal(8, writer.Position); + Assert.Equal(1.0, BitConverter.ToDouble(buffer, 0)); + } + + [Fact] + public void TestWriteString() + { + var buffer = new Byte[50]; + var writer = new SpanWriter(buffer); + writer.Write("Hello", -1, Encoding.UTF8); + Assert.Equal(5, writer.Position); + Assert.Equal("Hello", Encoding.UTF8.GetString(buffer, 0, 5)); + } + + [Fact] + public void TestWriteByteArray() + { + var buffer = new Byte[10]; + + var memory = GC.GetAllocatedBytesForCurrentThread(); + var writer = new SpanWriter(buffer); + writer.Write([1, 2, 3]); + + Assert.Equal(memory, GC.GetAllocatedBytesForCurrentThread()); + Assert.Equal(3, writer.Position); + Assert.Equal(new Byte[] { 1, 2, 3 }, buffer[..3]); + } + + [Fact] + public void TestWriteReadOnlySpan() + { + var buffer = new Byte[10]; + var writer = new SpanWriter(buffer); + writer.Write(new ReadOnlySpan([1, 2, 3])); + Assert.Equal(3, writer.Position); + Assert.Equal(new Byte[] { 1, 2, 3 }, buffer[..3]); + } + + [Fact] + public void TestWriteSpan() + { + var buffer = new Byte[10]; + var buf = new Byte[] { 1, 2, 3 }; + + var memory = GC.GetAllocatedBytesForCurrentThread(); + var writer = new SpanWriter(buffer); + writer.Write(new Span(buf)); + + Assert.Equal(memory, GC.GetAllocatedBytesForCurrentThread()); + Assert.Equal(3, writer.Position); + Assert.Equal(buf, buffer[..3]); + } + + [Fact] + public void TestWriteStruct() + { + var buffer = new Byte[10]; + + var memory = GC.GetAllocatedBytesForCurrentThread(); + var writer = new SpanWriter(buffer); + writer.Write(new TestStruct { Value = 1 }); + + Assert.Equal(memory, GC.GetAllocatedBytesForCurrentThread()); + Assert.Equal(4, writer.Position); + Assert.Equal(1, BitConverter.ToInt32(buffer, 0)); + } + + [Fact] + public void TestWriteEncodedInt() + { + var buffer = new Byte[10]; + + var memory = GC.GetAllocatedBytesForCurrentThread(); + var writer = new SpanWriter(buffer); + writer.WriteEncodedInt(128); + + Assert.Equal(memory, GC.GetAllocatedBytesForCurrentThread()); + Assert.Equal(2, writer.Position); + Assert.Equal(new Byte[] { 0x80, 0x01 }, buffer[..2]); + } + + private struct TestStruct + { + public Int32 Value; + } } diff --git a/XUnitTest.Core/Data/IPacketTests.cs b/XUnitTest.Core/Data/IPacketTests.cs index 524834663..36ac46a79 100644 --- a/XUnitTest.Core/Data/IPacketTests.cs +++ b/XUnitTest.Core/Data/IPacketTests.cs @@ -31,9 +31,11 @@ public void OwnerPacketTest() Assert.Equal(123, memory.Length); Assert.Equal('A', (Char)memory.Span[77]); + var gcmemory = GC.GetAllocatedBytesForCurrentThread(); pk.Resize(127); var pk2 = pk.Slice(7, 70, false) as OwnerPacket; + Assert.Equal(gcmemory + 48, GC.GetAllocatedBytesForCurrentThread()); Assert.NotNull(pk2); Assert.Equal(70, pk2.Length); Assert.Equal(7, pk2.Offset); @@ -62,7 +64,9 @@ public void OwnerPacketTest() public void MemoryPacketTest() { var buf = Rand.NextBytes(125); + var gcmemory = GC.GetAllocatedBytesForCurrentThread(); var pk = new MemoryPacket(buf, 123); + Assert.Equal(gcmemory, GC.GetAllocatedBytesForCurrentThread()); Assert.Equal(125, pk.Memory.Length); Assert.Equal(123, pk.Length);