Skip to content

Commit 937a50d

Browse files
committed
Added comments to unit tests
1 parent 537203b commit 937a50d

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()
130130
{
131131
var buffer = MemoryOwner<int>.Allocate(127);
132132

133+
// Here we allocate a MemoryOwner<T> instance with a requested size of 127, which means it
134+
// internally requests an array of size 127 from ArrayPool<T>.Shared. We then get the array
135+
// segment, so we need to verify that (since buffer is not disposed) the returned array is
136+
// not null, is of size >= the requested one (since ArrayPool<T> by definition returns an
137+
// array that is at least of the requested size), and that the offset and count properties
138+
// match our input values (same length, and offset at 0 since the buffer was not sliced).
133139
var segment = buffer.DangerousGetArray();
134140

135141
Assert.IsNotNull(segment.Array);
@@ -139,10 +145,14 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()
139145

140146
var second = buffer.Slice(10, 80);
141147

148+
// The original buffer instance is disposed here, because calling Slice transfers
149+
// the ownership of the internal buffer to the new instance (this is documented in
150+
// XML docs for the MemoryOwner<T>.Slice method).
142151
Assert.ThrowsException<ObjectDisposedException>(() => buffer.DangerousGetArray());
143152

144153
segment = second.DangerousGetArray();
145154

155+
// Same as before, but we now also verify the initial offset != 0, as we used Slice
146156
Assert.IsNotNull(segment.Array);
147157
Assert.IsTrue(segment.Array.Length >= second.Length);
148158
Assert.AreEqual(segment.Offset, 10);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ public void Test_SpanOwnerOfT_AllocateAndGetArray()
8888

8989
var segment = buffer.DangerousGetArray();
9090

91+
// See comments in the MemoryOwner<T> tests about this. The main difference
92+
// here is that we don't do the disposed checks, as SpanOwner<T> is optimized
93+
// with the assumption that usages after dispose are undefined behavior. This
94+
// is all documented in the XML docs for the SpanOwner<T> type.
9195
Assert.IsNotNull(segment.Array);
9296
Assert.IsTrue(segment.Array.Length >= buffer.Length);
9397
Assert.AreEqual(segment.Offset, 0);

0 commit comments

Comments
 (0)