Skip to content

Commit 6e40cf8

Browse files
committed
Added new unit tests for [ReadOnly]Span2D<T> indexers
1 parent 8f74184 commit 6e40cf8

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,89 @@ ref Unsafe.AsRef<int>(null),
400400
Assert.IsTrue(Unsafe.AreSame(ref r0, ref array[0, 0]));
401401
}
402402

403+
#if NETCOREAPP3_1_OR_GREATER
404+
[TestCategory("Span2DT")]
405+
[TestMethod]
406+
public unsafe void Test_ReadOnlySpan2DT_Index_Indexer_1()
407+
{
408+
int[,] array = new int[4, 4];
409+
410+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array);
411+
412+
ref int arrayRef = ref array[1, 3];
413+
ref readonly int span2dRef = ref span2d[1, ^1];
414+
415+
Assert.IsTrue(Unsafe.AreSame(ref arrayRef, ref Unsafe.AsRef(in span2dRef)));
416+
}
417+
418+
[TestCategory("Span2DT")]
419+
[TestMethod]
420+
public unsafe void Test_ReadOnlySpan2DT_Index_Indexer_2()
421+
{
422+
int[,] array = new int[4, 4];
423+
424+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array);
425+
426+
ref int arrayRef = ref array[2, 1];
427+
ref readonly int span2dRef = ref span2d[^2, ^3];
428+
429+
Assert.IsTrue(Unsafe.AreSame(ref arrayRef, ref Unsafe.AsRef(in span2dRef)));
430+
}
431+
432+
[TestCategory("Span2DT")]
433+
[TestMethod]
434+
[ExpectedException(typeof(IndexOutOfRangeException))]
435+
public unsafe void Test_ReadOnlySpan2DT_Index_Indexer_Fail()
436+
{
437+
int[,] array = new int[4, 4];
438+
439+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array);
440+
441+
ref readonly int span2dRef = ref span2d[^6, 2];
442+
}
443+
444+
[TestCategory("Span2DT")]
445+
[TestMethod]
446+
public unsafe void Test_ReadOnlySpan2DT_Range_Indexer_1()
447+
{
448+
int[,] array = new int[4, 4];
449+
450+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array);
451+
ReadOnlySpan2D<int> slice = span2d[1.., 1..];
452+
453+
Assert.AreEqual(slice.Length, 9);
454+
Assert.IsTrue(Unsafe.AreSame(ref array[1, 1], ref Unsafe.AsRef(in slice[0, 0])));
455+
Assert.IsTrue(Unsafe.AreSame(ref array[3, 3], ref Unsafe.AsRef(in slice[2, 2])));
456+
}
457+
458+
[TestCategory("Span2DT")]
459+
[TestMethod]
460+
public unsafe void Test_ReadOnlySpan2DT_Range_Indexer_2()
461+
{
462+
int[,] array = new int[4, 4];
463+
464+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array);
465+
ReadOnlySpan2D<int> slice = span2d[0..^2, 1..^1];
466+
467+
Assert.AreEqual(slice.Length, 4);
468+
Assert.IsTrue(Unsafe.AreSame(ref array[0, 1], ref Unsafe.AsRef(in slice[0, 0])));
469+
Assert.IsTrue(Unsafe.AreSame(ref array[1, 2], ref Unsafe.AsRef(in slice[1, 1])));
470+
}
471+
472+
[TestCategory("Span2DT")]
473+
[TestMethod]
474+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
475+
public unsafe void Test_ReadOnlySpan2DT_Range_Indexer_Fail()
476+
{
477+
int[,] array = new int[4, 4];
478+
479+
ReadOnlySpan2D<int> span2d = new ReadOnlySpan2D<int>(array);
480+
_ = span2d[0..6, 2..^1];
481+
482+
Assert.Fail();
483+
}
484+
#endif
485+
403486
[TestCategory("ReadOnlySpan2DT")]
404487
[TestMethod]
405488
public void Test_ReadOnlySpan2DT_Slice_1()

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

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,89 @@ ref Unsafe.AsRef<int>(null),
564564
Assert.IsTrue(Unsafe.AreSame(ref r0, ref array[0, 0]));
565565
}
566566

567+
#if NETCOREAPP3_1_OR_GREATER
568+
[TestCategory("Span2DT")]
569+
[TestMethod]
570+
public unsafe void Test_Span2DT_Index_Indexer_1()
571+
{
572+
int[,] array = new int[4, 4];
573+
574+
Span2D<int> span2d = new Span2D<int>(array);
575+
576+
ref int arrayRef = ref array[1, 3];
577+
ref int span2dRef = ref span2d[1, ^1];
578+
579+
Assert.IsTrue(Unsafe.AreSame(ref arrayRef, ref span2dRef));
580+
}
581+
582+
[TestCategory("Span2DT")]
583+
[TestMethod]
584+
public unsafe void Test_Span2DT_Index_Indexer_2()
585+
{
586+
int[,] array = new int[4, 4];
587+
588+
Span2D<int> span2d = new Span2D<int>(array);
589+
590+
ref int arrayRef = ref array[2, 1];
591+
ref int span2dRef = ref span2d[^2, ^3];
592+
593+
Assert.IsTrue(Unsafe.AreSame(ref arrayRef, ref span2dRef));
594+
}
595+
596+
[TestCategory("Span2DT")]
597+
[TestMethod]
598+
[ExpectedException(typeof(IndexOutOfRangeException))]
599+
public unsafe void Test_Span2DT_Index_Indexer_Fail()
600+
{
601+
int[,] array = new int[4, 4];
602+
603+
Span2D<int> span2d = new Span2D<int>(array);
604+
605+
ref int span2dRef = ref span2d[^6, 2];
606+
}
607+
608+
[TestCategory("Span2DT")]
609+
[TestMethod]
610+
public unsafe void Test_Span2DT_Range_Indexer_1()
611+
{
612+
int[,] array = new int[4, 4];
613+
614+
Span2D<int> span2d = new Span2D<int>(array);
615+
Span2D<int> slice = span2d[1.., 1..];
616+
617+
Assert.AreEqual(slice.Length, 9);
618+
Assert.IsTrue(Unsafe.AreSame(ref array[1, 1], ref slice[0, 0]));
619+
Assert.IsTrue(Unsafe.AreSame(ref array[3, 3], ref slice[2, 2]));
620+
}
621+
622+
[TestCategory("Span2DT")]
623+
[TestMethod]
624+
public unsafe void Test_Span2DT_Range_Indexer_2()
625+
{
626+
int[,] array = new int[4, 4];
627+
628+
Span2D<int> span2d = new Span2D<int>(array);
629+
Span2D<int> slice = span2d[0..^2, 1..^1];
630+
631+
Assert.AreEqual(slice.Length, 4);
632+
Assert.IsTrue(Unsafe.AreSame(ref array[0, 1], ref slice[0, 0]));
633+
Assert.IsTrue(Unsafe.AreSame(ref array[1, 2], ref slice[1, 1]));
634+
}
635+
636+
[TestCategory("Span2DT")]
637+
[TestMethod]
638+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
639+
public unsafe void Test_Span2DT_Range_Indexer_Fail()
640+
{
641+
int[,] array = new int[4, 4];
642+
643+
Span2D<int> span2d = new Span2D<int>(array);
644+
_ = span2d[0..6, 2..^1];
645+
646+
Assert.Fail();
647+
}
648+
#endif
649+
567650
[TestCategory("Span2DT")]
568651
[TestMethod]
569652
public void Test_Span2DT_Slice_1()

0 commit comments

Comments
 (0)