Skip to content

Commit bb9d0e0

Browse files
committed
Add MemoryPool Test
1 parent 3626ff7 commit bb9d0e0

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

src/Tests/MemoryModelTest/MemoryOwnerTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using CommunityToolkit.HighPerformance.Buffers;
2+
using System;
3+
using System.Threading.Tasks;
24

35
namespace MemoryModelTest;
46

@@ -7,6 +9,19 @@ public static class MemoryOwnerTest
79

810
#region Constants & Statics
911

12+
public static async Task AllocateLengthTestAsync()
13+
{
14+
await Task.Yield();
15+
16+
using var buffer = MemoryOwner<int>.Allocate(1);
17+
18+
var memory = buffer.Memory;
19+
memory.Span[0] = 84;
20+
21+
Console.WriteLine(string.Join(' ', memory.ToArray()));
22+
//output: 84
23+
}
24+
1025
public static void SliceTest()
1126
{
1227
using var buffer = MemoryOwner<int>.Allocate(42);

src/Tests/MemoryModelTest/MemoryTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Buffers;
23
using System.Threading.Tasks;
34

45
namespace MemoryModelTest;
@@ -8,6 +9,21 @@ public static class MemoryTest
89

910
#region Constants & Statics
1011

12+
public static async Task MemoryPoolTestAsync()
13+
{
14+
await Task.Yield();
15+
16+
var owner = MemoryPool<byte>.Shared.Rent(1);
17+
18+
var memory = owner.Memory;
19+
memory.Span[0] = 84;
20+
21+
Console.WriteLine(string.Join(' ', memory.ToArray()));
22+
//output:84 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
23+
24+
owner.Dispose();
25+
}
26+
1127
public static async Task MemoryToSpanTestAsync()
1228
{
1329
await Task.Yield();

src/Tests/MemoryModelTest/Program.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ public static async Task Main(string[] args)
3434

3535
//ArrayPoolBufferWriterTest.Test();
3636

37-
//MemoryOwnerTest.SliceTest();
37+
//await MemoryTest.MemoryToSpanTestAsync();
38+
//await MemoryTest.MemoryPoolTestAsync();
3839

39-
//SpanOwnerTest.Test();
40+
//MemoryOwnerTest.SliceTest();
41+
//await MemoryOwnerTest.AllocateLengthTestAsync();
4042

41-
await MemoryTest.MemoryToSpanTestAsync();
43+
await SpanOwnerTest.Test();
4244
}
4345

4446
#endregion

src/Tests/MemoryModelTest/SpanOwnerTest.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using CommunityToolkit.HighPerformance.Buffers;
2+
using System;
3+
using System.Threading.Tasks;
24

35
namespace MemoryModelTest;
46

@@ -7,11 +9,17 @@ public static class SpanOwnerTest
79

810
#region Constants & Statics
911

10-
public static void Test()
12+
public static async Task Test()
1113
{
12-
using var buffer = SpanOwner<int>.Allocate(100);
14+
await Task.Yield();
15+
16+
using var buffer = SpanOwner<int>.Allocate(1);
1317

1418
var span = buffer.Span;
19+
span[0] = 84;
20+
21+
Console.WriteLine(string.Join(' ', span.ToArray()));
22+
//output: 84
1523
}
1624

1725
#endregion

0 commit comments

Comments
 (0)