// 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; using System.Linq; using CommunityToolkit.HighPerformance.Buffers; using Microsoft.VisualStudio.TestTools.UnitTesting; using CommunityToolkit.HighPerformance.UnitTests.Buffers.Internals; namespace CommunityToolkit.HighPerformance.UnitTests.Buffers; [TestClass] public class Test_SpanOwnerOfT { [TestMethod] public void Test_SpanOwnerOfT_AllocateAndGetMemoryAndSpan() { using SpanOwner buffer = SpanOwner.Allocate(127); Assert.IsTrue(buffer.Length == 127); Assert.IsTrue(buffer.Span.Length == 127); buffer.Span.Fill(42); Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42)); } [TestMethod] public void Test_SpanOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan() { TrackingArrayPool? pool = new(); using (SpanOwner buffer = SpanOwner.Allocate(127, pool)) { Assert.AreEqual(pool.RentedArrays.Count, 1); Assert.IsTrue(buffer.Length == 127); Assert.IsTrue(buffer.Span.Length == 127); buffer.Span.Fill(42); Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42)); } Assert.AreEqual(pool.RentedArrays.Count, 0); } [TestMethod] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Test_SpanOwnerOfT_InvalidRequestedSize() { using SpanOwner buffer = SpanOwner.Allocate(-1); Assert.Fail("You shouldn't be here"); } [TestMethod] public void Test_SpanOwnerOfT_PooledBuffersAndClear() { using (SpanOwner buffer = SpanOwner.Allocate(127)) { buffer.Span.Fill(42); } using (SpanOwner buffer = SpanOwner.Allocate(127)) { Assert.IsTrue(buffer.Span.ToArray().All(i => i == 42)); } using (SpanOwner buffer = SpanOwner.Allocate(127, AllocationMode.Clear)) { Assert.IsTrue(buffer.Span.ToArray().All(i => i == 0)); } } [TestMethod] public void Test_SpanOwnerOfT_AllocateAndGetArray() { using SpanOwner buffer = SpanOwner.Allocate(127); ArraySegment segment = buffer.DangerousGetArray(); // See comments in the MemoryOwner tests about this. The main difference // here is that we don't do the disposed checks, as SpanOwner is optimized // with the assumption that usages after dispose are undefined behavior. This // is all documented in the XML docs for the SpanOwner type. Assert.IsNotNull(segment.Array); Assert.IsTrue(segment.Array.Length >= buffer.Length); Assert.AreEqual(segment.Offset, 0); Assert.AreEqual(segment.Count, buffer.Length); } }