Skip to content

Commit

Permalink
在单元测试中,借助GetAllocatedBytesForCurrentThread分析是否分配了内存
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 17, 2024
1 parent 9eb82d6 commit 11ef2a6
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 0 deletions.
69 changes: 69 additions & 0 deletions XUnitTest.Core/Buffers/SpanReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
191 changes: 191 additions & 0 deletions XUnitTest.Core/Buffers/SpanWriterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using NewLife.Buffers;
using Xunit;

Expand Down Expand Up @@ -26,4 +27,194 @@ public void CtorTest()

//Assert.Throws<ArgumentOutOfRangeException>(() => 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<Byte>([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<Byte>(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;
}
}
4 changes: 4 additions & 0 deletions XUnitTest.Core/Data/IPacketTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 11ef2a6

Please sign in to comment.