Skip to content

Commit ae589c7

Browse files
committed
Added unit tests for custom array pool overloads
1 parent 1f6aed0 commit ae589c7

File tree

5 files changed

+133
-0
lines changed

5 files changed

+133
-0
lines changed

UnitTests/UnitTests.HighPerformance.Shared/Buffers/Test_ArrayPoolBufferWriter{T}.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Toolkit.HighPerformance.Buffers;
1010
using Microsoft.Toolkit.HighPerformance.Extensions;
1111
using Microsoft.VisualStudio.TestTools.UnitTesting;
12+
using UnitTests.HighPerformance.Shared.Buffers;
1213

1314
namespace UnitTests.HighPerformance.Buffers
1415
{
@@ -54,6 +55,51 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan()
5455
Assert.ThrowsException<ObjectDisposedException>(() => writer.Advance(1));
5556
}
5657

58+
[TestCategory("ArrayPoolBufferWriterOfT")]
59+
[TestMethod]
60+
public void Test_ArrayPoolBufferWriterOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
61+
{
62+
var pool = new TrackingArrayPool<byte>();
63+
64+
using (var writer = new ArrayPoolBufferWriter<byte>())
65+
{
66+
Assert.AreEqual(pool.RentedArrays.Count, 1);
67+
68+
Assert.AreEqual(writer.Capacity, 256);
69+
Assert.AreEqual(writer.FreeCapacity, 256);
70+
Assert.AreEqual(writer.WrittenCount, 0);
71+
Assert.IsTrue(writer.WrittenMemory.IsEmpty);
72+
Assert.IsTrue(writer.WrittenSpan.IsEmpty);
73+
74+
Span<byte> span = writer.GetSpan(43);
75+
76+
Assert.IsTrue(span.Length >= 43);
77+
78+
writer.Advance(43);
79+
80+
Assert.AreEqual(writer.Capacity, 256);
81+
Assert.AreEqual(writer.FreeCapacity, 256 - 43);
82+
Assert.AreEqual(writer.WrittenCount, 43);
83+
Assert.AreEqual(writer.WrittenMemory.Length, 43);
84+
Assert.AreEqual(writer.WrittenSpan.Length, 43);
85+
86+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.Advance(-1));
87+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => writer.GetMemory(-1));
88+
Assert.ThrowsException<ArgumentException>(() => writer.Advance(1024));
89+
90+
writer.Dispose();
91+
92+
Assert.ThrowsException<ObjectDisposedException>(() => writer.WrittenMemory);
93+
Assert.ThrowsException<ObjectDisposedException>(() => writer.WrittenSpan.Length);
94+
Assert.ThrowsException<ObjectDisposedException>(() => writer.Capacity);
95+
Assert.ThrowsException<ObjectDisposedException>(() => writer.FreeCapacity);
96+
Assert.ThrowsException<ObjectDisposedException>(() => writer.Clear());
97+
Assert.ThrowsException<ObjectDisposedException>(() => writer.Advance(1));
98+
}
99+
100+
Assert.AreEqual(pool.RentedArrays.Count, 0);
101+
}
102+
57103
[TestCategory("ArrayPoolBufferWriterOfT")]
58104
[TestMethod]
59105
[ExpectedException(typeof(ArgumentOutOfRangeException))]

UnitTests/UnitTests.HighPerformance.Shared/Buffers/Test_MemoryOwner{T}.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Buffers;
67
using System.Diagnostics.CodeAnalysis;
78
using System.Linq;
89
using Microsoft.Toolkit.HighPerformance.Buffers;
910
using Microsoft.VisualStudio.TestTools.UnitTesting;
11+
using UnitTests.HighPerformance.Shared.Buffers;
1012

1113
namespace UnitTests.HighPerformance.Buffers
1214
{
@@ -30,6 +32,29 @@ public void Test_MemoryOwnerOfT_AllocateAndGetMemoryAndSpan()
3032
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
3133
}
3234

35+
[TestCategory("MemoryOwnerOfT")]
36+
[TestMethod]
37+
public void Test_MemoryOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
38+
{
39+
var pool = new TrackingArrayPool<int>();
40+
41+
using (var buffer = MemoryOwner<int>.Allocate(127, pool))
42+
{
43+
Assert.AreEqual(pool.RentedArrays.Count, 1);
44+
45+
Assert.IsTrue(buffer.Length == 127);
46+
Assert.IsTrue(buffer.Memory.Length == 127);
47+
Assert.IsTrue(buffer.Span.Length == 127);
48+
49+
buffer.Span.Fill(42);
50+
51+
Assert.IsTrue(buffer.Memory.Span.ToArray().All(i => i == 42));
52+
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
53+
}
54+
55+
Assert.AreEqual(pool.RentedArrays.Count, 0);
56+
}
57+
3358
[TestCategory("MemoryOwnerOfT")]
3459
[TestMethod]
3560
[ExpectedException(typeof(ArgumentOutOfRangeException))]

UnitTests/UnitTests.HighPerformance.Shared/Buffers/Test_SpanOwner{T}.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using Microsoft.Toolkit.HighPerformance.Buffers;
99
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
using UnitTests.HighPerformance.Shared.Buffers;
1011

1112
namespace UnitTests.HighPerformance.Buffers
1213
{
@@ -28,6 +29,27 @@ public void Test_SpanOwnerOfT_AllocateAndGetMemoryAndSpan()
2829
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
2930
}
3031

32+
[TestCategory("SpanOwnerOfT")]
33+
[TestMethod]
34+
public void Test_SpanOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
35+
{
36+
var pool = new TrackingArrayPool<int>();
37+
38+
using (var buffer = SpanOwner<int>.Allocate(127))
39+
{
40+
Assert.AreEqual(pool.RentedArrays.Count, 1);
41+
42+
Assert.IsTrue(buffer.Length == 127);
43+
Assert.IsTrue(buffer.Span.Length == 127);
44+
45+
buffer.Span.Fill(42);
46+
47+
Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42));
48+
}
49+
50+
Assert.AreEqual(pool.RentedArrays.Count, 0);
51+
}
52+
3153
[TestCategory("SpanOwnerOfT")]
3254
[TestMethod]
3355
[ExpectedException(typeof(ArgumentOutOfRangeException))]
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Buffers;
6+
using System.Collections.Generic;
7+
8+
namespace UnitTests.HighPerformance.Shared.Buffers
9+
{
10+
public sealed class TrackingArrayPool<T> : ArrayPool<T>
11+
{
12+
private readonly ArrayPool<T> pool = ArrayPool<T>.Create();
13+
14+
private readonly HashSet<T[]> arrays = new HashSet<T[]>();
15+
16+
/// <summary>
17+
/// Gets the collection of currently rented out arrays
18+
/// </summary>
19+
public IReadOnlyCollection<T[]> RentedArrays => this.arrays;
20+
21+
/// <inheritdoc/>
22+
public override T[] Rent(int minimumLength)
23+
{
24+
T[] array = this.pool.Rent(minimumLength);
25+
26+
this.arrays.Add(array);
27+
28+
return array;
29+
}
30+
31+
/// <inheritdoc/>
32+
public override void Return(T[] array, bool clearArray = false)
33+
{
34+
this.arrays.Remove(array);
35+
36+
this.pool.Return(array, clearArray);
37+
}
38+
}
39+
}

UnitTests/UnitTests.HighPerformance.Shared/UnitTests.HighPerformance.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<Compile Include="$(MSBuildThisFileDirectory)Buffers\Test_ArrayPoolBufferWriter{T}.cs" />
1414
<Compile Include="$(MSBuildThisFileDirectory)Buffers\Test_MemoryOwner{T}.cs" />
1515
<Compile Include="$(MSBuildThisFileDirectory)Buffers\Test_SpanOwner{T}.cs" />
16+
<Compile Include="$(MSBuildThisFileDirectory)Buffers\TrackingArrayPool{T}.cs" />
1617
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_ArrayExtensions.2D.cs" />
1718
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_ArrayExtensions.cs" />
1819
<Compile Include="$(MSBuildThisFileDirectory)Extensions\Test_ArrayPoolExtensions.cs" />

0 commit comments

Comments
 (0)