Skip to content

Commit 46b52b4

Browse files
committed
Add unit tests for slicing a 2D memory from manager
1 parent 76da689 commit 46b52b4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Memory2D{T}.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,29 @@ public void Test_Memory2DT_ToString()
516516

517517
Assert.AreEqual(text, expected);
518518
}
519+
520+
#if NET6_0_OR_GREATER
521+
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3536
522+
[TestMethod]
523+
[DataRow(720, 1280)]
524+
public void Test_Memory2DT_CastAndSlice_WorksCorrectly(int height, int width)
525+
{
526+
Memory2D<int> data =
527+
new byte[width * height * sizeof(int)]
528+
.AsMemory()
529+
.Cast<byte, int>()
530+
.AsMemory2D(height: height, width: width);
531+
532+
Memory2D<int> slice = data.Slice(
533+
row: height / 2,
534+
column: 0,
535+
height: height / 2,
536+
width: width);
537+
538+
Assert.IsTrue(Unsafe.AreSame(ref data.Span[height / 2, 0], ref slice.Span[0, 0]));
539+
Assert.IsTrue(Unsafe.AreSame(ref data.Span[height / 2, width - 1], ref slice.Span[0, width - 1]));
540+
Assert.IsTrue(Unsafe.AreSame(ref data.Span[height - 1, 0], ref slice.Span[(height / 2) - 1, 0]));
541+
Assert.IsTrue(Unsafe.AreSame(ref data.Span[height - 1, width - 1], ref slice.Span[(height / 2) - 1, width - 1]));
542+
}
543+
#endif
519544
}

tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlyMemory2D{T}.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,4 +464,29 @@ public void Test_ReadOnlyMemory2DT_ToString()
464464

465465
Assert.AreEqual(text, expected);
466466
}
467+
468+
#if NET6_0_OR_GREATER
469+
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3536
470+
[TestMethod]
471+
[DataRow(720, 1280)]
472+
public void Test_ReadOnlyMemory2DT_CastAndSlice_WorksCorrectly(int height, int width)
473+
{
474+
ReadOnlyMemory2D<int> data =
475+
new byte[width * height * sizeof(int)]
476+
.AsMemory()
477+
.Cast<byte, int>()
478+
.AsMemory2D(height: height, width: width);
479+
480+
ReadOnlyMemory2D<int> slice = data.Slice(
481+
row: height / 2,
482+
column: 0,
483+
height: height / 2,
484+
width: width);
485+
486+
Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height / 2, 0]), ref Unsafe.AsRef(in slice.Span[0, 0])));
487+
Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height / 2, width - 1]), ref Unsafe.AsRef(in slice.Span[0, width - 1])));
488+
Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height - 1, 0]), ref Unsafe.AsRef(in slice.Span[(height / 2) - 1, 0])));
489+
Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height - 1, width - 1]), ref Unsafe.AsRef(in slice.Span[(height / 2) - 1, width - 1])));
490+
}
491+
#endif
467492
}

0 commit comments

Comments
 (0)