@@ -130,6 +130,12 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()
130
130
{
131
131
var buffer = MemoryOwner < int > . Allocate ( 127 ) ;
132
132
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).
133
139
var segment = buffer . DangerousGetArray ( ) ;
134
140
135
141
Assert . IsNotNull ( segment . Array ) ;
@@ -139,10 +145,14 @@ public void Test_MemoryOwnerOfT_AllocateAndGetArray()
139
145
140
146
var second = buffer . Slice ( 10 , 80 ) ;
141
147
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).
142
151
Assert . ThrowsException < ObjectDisposedException > ( ( ) => buffer . DangerousGetArray ( ) ) ;
143
152
144
153
segment = second . DangerousGetArray ( ) ;
145
154
155
+ // Same as before, but we now also verify the initial offset != 0, as we used Slice
146
156
Assert . IsNotNull ( segment . Array ) ;
147
157
Assert . IsTrue ( segment . Array . Length >= second . Length ) ;
148
158
Assert . AreEqual ( segment . Offset , 10 ) ;
0 commit comments