Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,5 +507,9 @@ public static partial class MemoryMarshal
public static ref T GetReference<T>(System.Span<T> span) { throw null; }
public static System.Collections.Generic.IEnumerable<T> ToEnumerable<T>(System.ReadOnlyMemory<T> memory) { throw null; }
public static bool TryGetArray<T>(System.ReadOnlyMemory<T> readOnlyMemory, out System.ArraySegment<T> arraySegment) { throw null; }
public static bool TryGetOwnedMemory<T, TOwner>(ReadOnlyMemory<T> readOnlyMemory, out TOwner ownedMemory)
where TOwner : System.Buffers.OwnedMemory<T> { throw null; }
public static bool TryGetOwnedMemory<T, TOwner>(ReadOnlyMemory<T> readOnlyMemory, out TOwner ownedMemory, out int index, out int length)
where TOwner : System.Buffers.OwnedMemory<T> { throw null; }
}
}
93 changes: 93 additions & 0 deletions src/System.Memory/tests/MemoryMarshal/TryGetOwnedMemory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Runtime.InteropServices;
using Xunit;

namespace System.MemoryTests
{
public static partial class MemoryMarshalTests
{
[Fact]
public static void TryGetOwnedMemoryFromDefaultMemory()
{
ReadOnlyMemory<int> memory = default;
Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory<int> owner));
Assert.Null(owner);
}

[Fact]
public static void TryGetOwnedMemory()
{
int[] array = new int[10];
OwnedMemory<int> orignalOwner = new CustomMemoryForTest<int>(array);
ReadOnlyMemory<int> memory = orignalOwner.Memory;

Assert.True(MemoryMarshal.TryGetOwnedMemory(memory, out CustomMemoryForTest<int> customOwner));
Assert.Same(orignalOwner, customOwner);

Assert.True(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory<int> owner));
Assert.Same(orignalOwner, owner);

Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OtherMemoryForTest<int> notOwner));
Assert.Null(notOwner);
}

[Fact]
public static void TryGetOwnedMemoryFromDefaultMemory_IndexLength()
{
ReadOnlyMemory<int> memory = default;
Assert.False(MemoryMarshal.TryGetOwnedMemory(memory, out OwnedMemory<int> owner, out int index, out int length));
Assert.Equal(0, index);
Assert.Equal(0, length);
Assert.Null(owner);
}

[Fact]
public static void TryGetOwnedMemory_IndexLength()
{
int[] array = new int[10];
OwnedMemory<int> orignalOwner = new CustomMemoryForTest<int>(array);
ReadOnlyMemory<int> memory = orignalOwner.Memory;

for (int i = 0; i < array.Length; i++)
{
Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out CustomMemoryForTest<int> customOwner, out int index, out int length));
Assert.Same(orignalOwner, customOwner);
Assert.Equal(i, index);
Assert.Equal(array.Length - i, length);
}

for (int i = 0; i < array.Length; i++)
{
Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OwnedMemory<int> owner, out int index, out int length));
Assert.Same(orignalOwner, owner);
Assert.Equal(i, index);
Assert.Equal(array.Length - i, length);
}

for (int i = 0; i < array.Length; i++)
{
Assert.False(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OtherMemoryForTest<int> notOwner, out int index, out int length));
Assert.Null(notOwner);
}
}

internal class OtherMemoryForTest<T> : OwnedMemory<T>
{
public OtherMemoryForTest() { }

public override int Length => 0;
public override bool IsDisposed => false;
protected override bool IsRetained => false;
public override Span<T> Span => throw new NotImplementedException();
public override MemoryHandle Pin(int byteOffset = 0) => throw new NotImplementedException();
protected override bool TryGetArray(out ArraySegment<T> arraySegment) => throw new NotImplementedException();
protected override void Dispose(bool disposing) => throw new NotImplementedException();
public override void Retain() => throw new NotImplementedException();
public override bool Release() => throw new NotImplementedException();
}
}
}
1 change: 1 addition & 0 deletions src/System.Memory/tests/System.Memory.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
<Compile Include="MemoryMarshal\CastSpan.cs" />
<Compile Include="MemoryMarshal\GetReference.cs" />
<Compile Include="MemoryMarshal\TryGetArray.cs" />
<Compile Include="MemoryMarshal\TryGetOwnedMemory.cs" />
<Compile Include="MemoryMarshal\ToEnumerable.cs" />
</ItemGroup>
<ItemGroup>
Expand Down