Skip to content

Commit 0c9a3c5

Browse files
committed
Added missing tests for Span2D<T>.TryGetSpan
1 parent 68888d2 commit 0c9a3c5

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

UnitTests/UnitTests.HighPerformance.Shared/Memory/Test_ReadOnlySpan2D{T}.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,51 @@ ref Unsafe.AsRef(span[2]),
586586

587587
[TestCategory("ReadOnlySpan2DT")]
588588
[TestMethod]
589-
public void Test_ReadOnlySpan2DT_TryGetReadOnlySpan_1()
589+
public void Test_ReadOnlySpan2DT_TryGetSpan_From1DArray_1()
590+
{
591+
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
592+
593+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array, 3, 3);
594+
595+
bool success = span2d.TryGetSpan(out ReadOnlySpan<int> span);
596+
597+
Assert.IsTrue(success);
598+
Assert.AreEqual(span.Length, span2d.Length);
599+
Assert.IsTrue(Unsafe.AreSame(ref array[0], ref Unsafe.AsRef(in span[0])));
600+
}
601+
602+
[TestCategory("ReadOnlySpan2DT")]
603+
[TestMethod]
604+
public void Test_ReadOnlySpan2DT_TryGetSpan_From1DArray_2()
605+
{
606+
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
607+
608+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array, 3, 3).Slice(1, 0, 2, 3);
609+
610+
bool success = span2d.TryGetSpan(out ReadOnlySpan<int> span);
611+
612+
Assert.IsTrue(success);
613+
Assert.AreEqual(span.Length, span2d.Length);
614+
Assert.IsTrue(Unsafe.AreSame(ref array[3], ref Unsafe.AsRef(in span[0])));
615+
}
616+
617+
[TestCategory("ReadOnlySpan2DT")]
618+
[TestMethod]
619+
public void Test_ReadOnlySpan2DT_TryGetSpan_From1DArray_3()
620+
{
621+
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
622+
623+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array, 3, 3).Slice(0, 1, 3, 2);
624+
625+
bool success = span2d.TryGetSpan(out ReadOnlySpan<int> span);
626+
627+
Assert.IsFalse(success);
628+
Assert.AreEqual(span.Length, 0);
629+
}
630+
631+
[TestCategory("ReadOnlySpan2DT")]
632+
[TestMethod]
633+
public void Test_ReadOnlySpan2DT_TryGetReadOnlySpan_From2DArray_1()
590634
{
591635
int[,] array =
592636
{
@@ -610,7 +654,7 @@ public void Test_ReadOnlySpan2DT_TryGetReadOnlySpan_1()
610654

611655
[TestCategory("ReadOnlySpan2DT")]
612656
[TestMethod]
613-
public void Test_ReadOnlySpan2DT_TryGetReadOnlySpan_2()
657+
public void Test_ReadOnlySpan2DT_TryGetReadOnlySpan_From2DArray_2()
614658
{
615659
int[,] array =
616660
{

UnitTests/UnitTests.HighPerformance.Shared/Memory/Test_Span2D{T}.cs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,51 @@ public void Test_Span2DT_GetRowSpan()
761761

762762
[TestCategory("Span2DT")]
763763
[TestMethod]
764-
public void Test_Span2DT_TryGetSpan_1()
764+
public void Test_Span2DT_TryGetSpan_From1DArray_1()
765+
{
766+
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
767+
768+
Span2D<int> span2d = new Span2D<int>(array, 3, 3);
769+
770+
bool success = span2d.TryGetSpan(out Span<int> span);
771+
772+
Assert.IsTrue(success);
773+
Assert.AreEqual(span.Length, span2d.Length);
774+
Assert.IsTrue(Unsafe.AreSame(ref array[0], ref span[0]));
775+
}
776+
777+
[TestCategory("Span2DT")]
778+
[TestMethod]
779+
public void Test_Span2DT_TryGetSpan_From1DArray_2()
780+
{
781+
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
782+
783+
Span2D<int> span2d = new Span2D<int>(array, 3, 3).Slice(1, 0, 2, 3);
784+
785+
bool success = span2d.TryGetSpan(out Span<int> span);
786+
787+
Assert.IsTrue(success);
788+
Assert.AreEqual(span.Length, span2d.Length);
789+
Assert.IsTrue(Unsafe.AreSame(ref array[3], ref span[0]));
790+
}
791+
792+
[TestCategory("Span2DT")]
793+
[TestMethod]
794+
public void Test_Span2DT_TryGetSpan_From1DArray_3()
795+
{
796+
int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
797+
798+
Span2D<int> span2d = new Span2D<int>(array, 3, 3).Slice(0, 1, 3, 2);
799+
800+
bool success = span2d.TryGetSpan(out Span<int> span);
801+
802+
Assert.IsFalse(success);
803+
Assert.AreEqual(span.Length, 0);
804+
}
805+
806+
[TestCategory("Span2DT")]
807+
[TestMethod]
808+
public void Test_Span2DT_TryGetSpan_From2DArray_1()
765809
{
766810
int[,] array =
767811
{
@@ -790,7 +834,7 @@ public void Test_Span2DT_TryGetSpan_1()
790834

791835
[TestCategory("Span2DT")]
792836
[TestMethod]
793-
public void Test_Span2DT_TryGetSpan_2()
837+
public void Test_Span2DT_TryGetSpan_From2DArray_2()
794838
{
795839
int[,] array =
796840
{

0 commit comments

Comments
 (0)