mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
56 lines
1.7 KiB
// Copyright (c) Six Labors.
|
|
// Licensed under the Six Labors Split License.
|
|
|
|
using System.Buffers;
|
|
using Microsoft.DotNet.RemoteExecutor;
|
|
using SixLabors.ImageSharp.Memory.Internals;
|
|
|
|
namespace SixLabors.ImageSharp.Tests.Memory.Allocators;
|
|
|
|
public class SharedArrayPoolBufferTests
|
|
{
|
|
[Fact]
|
|
public void AllocatesArrayPoolArray()
|
|
{
|
|
RemoteExecutor.Invoke(RunTest).Dispose();
|
|
|
|
static void RunTest()
|
|
{
|
|
using (var buffer = new SharedArrayPoolBuffer<byte>(900))
|
|
{
|
|
Assert.Equal(900, buffer.GetSpan().Length);
|
|
buffer.GetSpan().Fill(42);
|
|
}
|
|
|
|
byte[] array = ArrayPool<byte>.Shared.Rent(900);
|
|
byte[] expected = Enumerable.Repeat((byte)42, 900).ToArray();
|
|
|
|
Assert.True(expected.AsSpan().SequenceEqual(array.AsSpan(0, 900)));
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void OutstandingReferences_RetainArrays()
|
|
{
|
|
RemoteExecutor.Invoke(RunTest).Dispose();
|
|
|
|
static void RunTest()
|
|
{
|
|
var buffer = new SharedArrayPoolBuffer<byte>(900);
|
|
Span<byte> span = buffer.GetSpan();
|
|
|
|
buffer.AddRef();
|
|
((IDisposable)buffer).Dispose();
|
|
span.Fill(42);
|
|
byte[] array = ArrayPool<byte>.Shared.Rent(900);
|
|
Assert.NotEqual(42, array[0]);
|
|
ArrayPool<byte>.Shared.Return(array);
|
|
|
|
buffer.ReleaseRef();
|
|
array = ArrayPool<byte>.Shared.Rent(900);
|
|
byte[] expected = Enumerable.Repeat((byte)42, 900).ToArray();
|
|
Assert.True(expected.AsSpan().SequenceEqual(array.AsSpan(0, 900)));
|
|
ArrayPool<byte>.Shared.Return(array);
|
|
}
|
|
}
|
|
}
|
|
|