Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public static void EnsureIsInNativeIntRange(int height, int width, int pitch)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ComputeInt32Area(int height, int width, int pitch)
{
return checked(((width + pitch) * Max(unchecked(height - 1), 0)) + width);
if (height <= 0 || width <= 0)
return 0;

return checked(((width + pitch) * (height - 1)) + width);
}
}
19 changes: 2 additions & 17 deletions src/CommunityToolkit.HighPerformance/Memory/Memory2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,6 @@ public unsafe Memory2D(MemoryManager<T> memoryManager, int offset, int height, i
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}

if (width == 0 || height == 0)
{
this = default;

return;
}

int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = length - offset;

Expand Down Expand Up @@ -435,16 +428,8 @@ internal unsafe Memory2D(Memory<T> memory, int offset, int height, int width, in
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}

if (width == 0 || height == 0)
{
this = default;

return;
}

int
area = OverflowHelper.ComputeInt32Area(height, width, pitch),
remaining = memory.Length - offset;
int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = memory.Length - offset;

if (area > remaining)
{
Expand Down
14 changes: 0 additions & 14 deletions src/CommunityToolkit.HighPerformance/Memory/ReadOnlyMemory2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,6 @@ public unsafe ReadOnlyMemory2D(MemoryManager<T> memoryManager, int offset, int h
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}

if (width == 0 || height == 0)
{
this = default;

return;
}

int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = length - offset;

Expand Down Expand Up @@ -455,13 +448,6 @@ internal unsafe ReadOnlyMemory2D(ReadOnlyMemory<T> memory, int offset, int heigh
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}

if (width == 0 || height == 0)
{
this = default;

return;
}

int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = memory.Length - offset;

Expand Down
14 changes: 0 additions & 14 deletions src/CommunityToolkit.HighPerformance/Memory/ReadOnlySpan2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,6 @@ public ReadOnlySpan2D(T[] array, int offset, int height, int width, int pitch)
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}

if (width == 0 || height == 0)
{
this = default;

return;
}

int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = array.Length - offset;

Expand Down Expand Up @@ -459,13 +452,6 @@ internal ReadOnlySpan2D(ReadOnlySpan<T> span, int offset, int height, int width,
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}

if (width == 0 || height == 0)
{
this = default;

return;
}

int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = span.Length - offset;

Expand Down
7 changes: 0 additions & 7 deletions src/CommunityToolkit.HighPerformance/Memory/Span2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,6 @@ internal Span2D(Span<T> span, int offset, int height, int width, int pitch)
ThrowHelper.ThrowArgumentOutOfRangeExceptionForPitch();
}

if (width == 0 || height == 0)
{
this = default;

return;
}

int area = OverflowHelper.ComputeInt32Area(height, width, pitch);
int remaining = span.Length - offset;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,30 @@ public void Test_MemoryExtensions_MemoryStream()
Assert.IsTrue(stream.CanWrite);
}

#if NET6_0_OR_GREATER
[TestMethod]
public void Test_MemoryExtensions_AsMemory2d_Empty()
{
Memory2D<int> empty1 = Array.Empty<int>().AsMemory().AsMemory2D(0, 0);
Assert.IsTrue(empty1.IsEmpty);
Assert.AreEqual(empty1.Length, 0);
Assert.AreEqual(empty1.Width, 0);
Assert.AreEqual(empty1.Height, 0);

Memory2D<int> empty2 = Array.Empty<int>().AsMemory().AsMemory2D(4, 0);
Assert.IsTrue(empty2.IsEmpty);
Assert.AreEqual(empty2.Length, 0);
Assert.AreEqual(empty2.Width, 0);
Assert.AreEqual(empty2.Height, 4);

Memory2D<int> empty3 = Array.Empty<int>().AsMemory().AsMemory2D(0, 7);
Assert.IsTrue(empty3.IsEmpty);
Assert.AreEqual(empty3.Length, 0);
Assert.AreEqual(empty3.Width, 7);
Assert.AreEqual(empty3.Height, 0);
}
#endif

private sealed class ArrayMemoryManager<T> : MemoryManager<T>
where T : unmanaged
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -34,4 +35,28 @@ public void Test_ReadOnlyMemoryExtensions_MemoryStream()
Assert.AreEqual(stream.Length, memory.Length);
Assert.IsFalse(stream.CanWrite);
}

#if NET6_0_OR_GREATER
[TestMethod]
public void Test_ReadOnlyMemoryExtensions_AsMemory2d_Empty()
{
ReadOnlyMemory2D<int> empty1 = ((ReadOnlyMemory<int>)Array.Empty<int>().AsMemory()).AsMemory2D(0, 0);
Assert.IsTrue(empty1.IsEmpty);
Assert.AreEqual(empty1.Length, 0);
Assert.AreEqual(empty1.Width, 0);
Assert.AreEqual(empty1.Height, 0);

ReadOnlyMemory2D<int> empty2 = ((ReadOnlyMemory<int>)Array.Empty<int>().AsMemory()).AsMemory2D(4, 0);
Assert.IsTrue(empty2.IsEmpty);
Assert.AreEqual(empty2.Length, 0);
Assert.AreEqual(empty2.Width, 0);
Assert.AreEqual(empty2.Height, 4);

ReadOnlyMemory2D<int> empty3 = ((ReadOnlyMemory<int>)Array.Empty<int>().AsMemory()).AsMemory2D(0, 7);
Assert.IsTrue(empty3.IsEmpty);
Assert.AreEqual(empty3.Length, 0);
Assert.AreEqual(empty3.Width, 7);
Assert.AreEqual(empty3.Height, 0);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,28 @@ public void Test_ReadOnlySpanExtensions_CopyTo_RefEnumerable()

CollectionAssert.AreEqual(array, result);
}

#if NET6_0_OR_GREATER
[TestMethod]
public void Test_ReadOnlySpanExtensions_AsSpan2d_Empty()
{
ReadOnlySpan2D<int> empty1 = ReadOnlySpan<int>.Empty.AsSpan2D(0, 0);
Assert.IsTrue(empty1.IsEmpty);
Assert.AreEqual(empty1.Length, 0);
Assert.AreEqual(empty1.Width, 0);
Assert.AreEqual(empty1.Height, 0);

ReadOnlySpan2D<int> empty2 = ReadOnlySpan<int>.Empty.AsSpan2D(4, 0);
Assert.IsTrue(empty2.IsEmpty);
Assert.AreEqual(empty2.Length, 0);
Assert.AreEqual(empty2.Width, 0);
Assert.AreEqual(empty2.Height, 4);

ReadOnlySpan2D<int> empty3 = ReadOnlySpan<int>.Empty.AsSpan2D(0, 7);
Assert.IsTrue(empty3.IsEmpty);
Assert.AreEqual(empty3.Length, 0);
Assert.AreEqual(empty3.Width, 7);
Assert.AreEqual(empty3.Height, 0);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,28 @@ public void Test_SpanExtensions_CopyTo_RefEnumerable()

CollectionAssert.AreEqual(array, result);
}

#if NET6_0_OR_GREATER
[TestMethod]
public void Test_SpanExtensions_AsSpan2d_Empty()
{
Span2D<int> empty1 = Span<int>.Empty.AsSpan2D(0, 0);
Assert.IsTrue(empty1.IsEmpty);
Assert.AreEqual(empty1.Length, 0);
Assert.AreEqual(empty1.Width, 0);
Assert.AreEqual(empty1.Height, 0);

Span2D<int> empty2 = Span<int>.Empty.AsSpan2D(4, 0);
Assert.IsTrue(empty2.IsEmpty);
Assert.AreEqual(empty2.Length, 0);
Assert.AreEqual(empty2.Width, 0);
Assert.AreEqual(empty2.Height, 4);

Span2D<int> empty3 = Span<int>.Empty.AsSpan2D(0, 7);
Assert.IsTrue(empty3.IsEmpty);
Assert.AreEqual(empty3.Length, 0);
Assert.AreEqual(empty3.Width, 7);
Assert.AreEqual(empty3.Height, 0);
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.UnitTests.Buffers.Internals;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CommunityToolkit.HighPerformance.UnitTests;
Expand Down Expand Up @@ -43,6 +45,27 @@ public void Test_Memory2DT_Empty()
Assert.AreEqual(empty4.Length, 0);
Assert.AreEqual(empty4.Width, 7);
Assert.AreEqual(empty4.Height, 0);

#if NET6_0_OR_GREATER
MemoryManager<int> memoryManager = new UnmanagedSpanOwner<int>(1);
Memory2D<int> empty5 = new (memoryManager, 0, 0);
Assert.IsTrue(empty5.IsEmpty);
Assert.AreEqual(empty5.Length, 0);
Assert.AreEqual(empty5.Width, 0);
Assert.AreEqual(empty5.Height, 0);

Memory2D<int> empty6 = new (memoryManager, 4, 0);
Assert.IsTrue(empty6.IsEmpty);
Assert.AreEqual(empty6.Length, 0);
Assert.AreEqual(empty6.Width, 0);
Assert.AreEqual(empty6.Height, 4);

Memory2D<int> empty7 = new (memoryManager, 0, 7);
Assert.IsTrue(empty7.IsEmpty);
Assert.AreEqual(empty7.Length, 0);
Assert.AreEqual(empty7.Width, 7);
Assert.AreEqual(empty7.Height, 0);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using CommunityToolkit.HighPerformance.UnitTests.Buffers.Internals;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CommunityToolkit.HighPerformance.UnitTests;
Expand Down Expand Up @@ -34,6 +36,27 @@ public void Test_ReadOnlyMemory2DT_Empty()
Assert.AreEqual(empty2.Length, 0);
Assert.AreEqual(empty2.Width, 0);
Assert.AreEqual(empty2.Height, 0);

#if NET6_0_OR_GREATER
MemoryManager<int> memoryManager = new UnmanagedSpanOwner<int>(1);
ReadOnlyMemory2D<int> empty5 = new (memoryManager, 0, 0);
Assert.IsTrue(empty5.IsEmpty);
Assert.AreEqual(empty5.Length, 0);
Assert.AreEqual(empty5.Width, 0);
Assert.AreEqual(empty5.Height, 0);

ReadOnlyMemory2D<int> empty6 = new (memoryManager, 4, 0);
Assert.IsTrue(empty6.IsEmpty);
Assert.AreEqual(empty6.Length, 0);
Assert.AreEqual(empty6.Width, 0);
Assert.AreEqual(empty6.Height, 4);

ReadOnlyMemory2D<int> empty7 = new (memoryManager, 0, 7);
Assert.IsTrue(empty7.IsEmpty);
Assert.AreEqual(empty7.Length, 0);
Assert.AreEqual(empty7.Width, 7);
Assert.AreEqual(empty7.Height, 0);
#endif
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ public void Test_ReadOnlySpan2DT_Empty()
Assert.AreEqual(empty2.Length, 0);
Assert.AreEqual(empty2.Width, 0);
Assert.AreEqual(empty2.Height, 0);

ReadOnlySpan2D<string> empty3 = new([], 0, 0);

Assert.IsTrue(empty3.IsEmpty);
Assert.AreEqual(empty3.Length, 0);
Assert.AreEqual(empty3.Width, 0);
Assert.AreEqual(empty3.Height, 0);

ReadOnlySpan2D<string> empty4 = new([], 4, 0);

Assert.IsTrue(empty4.IsEmpty);
Assert.AreEqual(empty4.Length, 0);
Assert.AreEqual(empty4.Width, 0);
Assert.AreEqual(empty4.Height, 4);

ReadOnlySpan2D<string> empty5 = new([], 0, 7);

Assert.IsTrue(empty5.IsEmpty);
Assert.AreEqual(empty5.Length, 0);
Assert.AreEqual(empty5.Width, 7);
Assert.AreEqual(empty5.Height, 0);
}

#if NET6_0_OR_GREATER
Expand Down