Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 064a638

Browse files
benaadamsstephentoub
authored andcommitted
Add TryGetOwnedMemory tests and ref (#27288)
1 parent 1e3faea commit 064a638

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/System.Memory/ref/System.Memory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,5 +507,9 @@ public static partial class MemoryMarshal
507507
public static ref T GetReference<T>(System.Span<T> span) { throw null; }
508508
public static System.Collections.Generic.IEnumerable<T> ToEnumerable<T>(System.ReadOnlyMemory<T> memory) { throw null; }
509509
public static bool TryGetArray<T>(System.ReadOnlyMemory<T> readOnlyMemory, out System.ArraySegment<T> arraySegment) { throw null; }
510+
public static bool TryGetOwnedMemory<T, TOwner>(ReadOnlyMemory<T> readOnlyMemory, out TOwner ownedMemory)
511+
where TOwner : System.Buffers.OwnedMemory<T> { throw null; }
512+
public static bool TryGetOwnedMemory<T, TOwner>(ReadOnlyMemory<T> readOnlyMemory, out TOwner ownedMemory, out int index, out int length)
513+
where TOwner : System.Buffers.OwnedMemory<T> { throw null; }
510514
}
511515
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.Runtime.InteropServices;
7+
using Xunit;
8+
9+
namespace System.MemoryTests
10+
{
11+
public static partial class MemoryMarshalTests
12+
{
13+
[Fact]
14+
public static void TryGetOwnedMemoryFromDefaultMemory()
15+
{
16+
ReadOnlyMemory<int> memory = default;
17+
Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory<int> owner));
18+
Assert.Null(owner);
19+
}
20+
21+
[Fact]
22+
public static void TryGetOwnedMemory()
23+
{
24+
int[] array = new int[10];
25+
OwnedMemory<int> orignalOwner = new CustomMemoryForTest<int>(array);
26+
ReadOnlyMemory<int> memory = orignalOwner.Memory;
27+
28+
Assert.True(MemoryMarshal.TryGetOwnedMemory(memory, out CustomMemoryForTest<int> customOwner));
29+
Assert.Same(orignalOwner, customOwner);
30+
31+
Assert.True(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory<int> owner));
32+
Assert.Same(orignalOwner, owner);
33+
34+
Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OtherMemoryForTest<int> notOwner));
35+
Assert.Null(notOwner);
36+
}
37+
38+
[Fact]
39+
public static void TryGetOwnedMemoryFromDefaultMemory_IndexLength()
40+
{
41+
ReadOnlyMemory<int> memory = default;
42+
Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory<int> owner, out int index, out int length));
43+
Assert.Equal(0, index);
44+
Assert.Equal(0, length);
45+
Assert.Null(owner);
46+
}
47+
48+
[Fact]
49+
public static void TryGetOwnedMemory_IndexLength()
50+
{
51+
int[] array = new int[10];
52+
OwnedMemory<int> orignalOwner = new CustomMemoryForTest<int>(array);
53+
ReadOnlyMemory<int> memory = orignalOwner.Memory;
54+
55+
for (int i = 0; i < array.Length; i++)
56+
{
57+
Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out CustomMemoryForTest<int> customOwner, out int index, out int length));
58+
Assert.Same(orignalOwner, customOwner);
59+
Assert.Equal(i, index);
60+
Assert.Equal(array.Length - i, length);
61+
}
62+
63+
for (int i = 0; i < array.Length; i++)
64+
{
65+
Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OwnedMemory<int> owner, out int index, out int length));
66+
Assert.Same(orignalOwner, owner);
67+
Assert.Equal(i, index);
68+
Assert.Equal(array.Length - i, length);
69+
}
70+
71+
for (int i = 0; i < array.Length; i++)
72+
{
73+
Assert.False(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OtherMemoryForTest<int> notOwner, out int index, out int length));
74+
Assert.Null(notOwner);
75+
}
76+
}
77+
78+
internal class OtherMemoryForTest<T> : OwnedMemory<T>
79+
{
80+
public OtherMemoryForTest() { }
81+
82+
public override int Length => 0;
83+
public override bool IsDisposed => false;
84+
protected override bool IsRetained => false;
85+
public override Span<T> Span => throw new NotImplementedException();
86+
public override MemoryHandle Pin(int byteOffset = 0) => throw new NotImplementedException();
87+
protected override bool TryGetArray(out ArraySegment<T> arraySegment) => throw new NotImplementedException();
88+
protected override void Dispose(bool disposing) => throw new NotImplementedException();
89+
public override void Retain() => throw new NotImplementedException();
90+
public override bool Release() => throw new NotImplementedException();
91+
}
92+
}
93+
}

src/System.Memory/tests/System.Memory.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<Compile Include="MemoryMarshal\CastSpan.cs" />
149149
<Compile Include="MemoryMarshal\GetReference.cs" />
150150
<Compile Include="MemoryMarshal\TryGetArray.cs" />
151+
<Compile Include="MemoryMarshal\TryGetOwnedMemory.cs" />
151152
<Compile Include="MemoryMarshal\ToEnumerable.cs" />
152153
</ItemGroup>
153154
<ItemGroup>

0 commit comments

Comments
 (0)