public unsafe void Test_ReadOnlySpan2DT_GetPinnableReference() { Assert.IsTrue(Unsafe.AreSame( ref Unsafe.AsRef <int>(null), ref ReadOnlySpan2D <int> .Empty.GetPinnableReference())); int[,] array = { { 1, 2, 3 }, { 4, 5, 6 } }; ReadOnlySpan2D <int> span2d = new ReadOnlySpan2D <int>(array); ref int r0 = ref span2d.GetPinnableReference();
public void Test_ReadOnlySpan2DT_TryCopyTo2D() { int[,] array = { { 1, 2, 3 }, { 4, 5, 6 } }; ReadOnlySpan2D <int> span2d = new ReadOnlySpan2D <int>(array); int[,] target = new int[2, 3]; Assert.IsTrue(span2d.TryCopyTo(target)); Assert.IsFalse(span2d.TryCopyTo(Span2D <int> .Empty)); }
public void Test_ReadOnlySpan2DT_Empty() { ReadOnlySpan2D <int> empty1 = default; Assert.IsTrue(empty1.IsEmpty); Assert.AreEqual(empty1.Length, 0); Assert.AreEqual(empty1.Width, 0); Assert.AreEqual(empty1.Height, 0); ReadOnlySpan2D <string> empty2 = ReadOnlySpan2D <string> .Empty; Assert.IsTrue(empty2.IsEmpty); Assert.AreEqual(empty2.Length, 0); Assert.AreEqual(empty2.Width, 0); Assert.AreEqual(empty2.Height, 0); }
public void Test_ReadOnlySpan2DT_CopyTo2D_1() { int[,] array = { { 1, 2, 3 }, { 4, 5, 6 } }; ReadOnlySpan2D <int> span2d = new ReadOnlySpan2D <int>(array); int[,] target = new int[2, 3]; span2d.CopyTo(target); CollectionAssert.AreEqual(array, target); Assert.ThrowsException <ArgumentException>(() => new ReadOnlySpan2D <int>(array).CopyTo(Span2D <int> .Empty)); }
public void Test_ReadOnlySpan2DT_Array2DConstructor_1() { int[,] array = { { 1, 2, 3 }, { 4, 5, 6 } }; ReadOnlySpan2D <int> span2d = new ReadOnlySpan2D <int>(array); Assert.IsFalse(span2d.IsEmpty); Assert.AreEqual(span2d.Length, 6); Assert.AreEqual(span2d.Width, 3); Assert.AreEqual(span2d.Height, 2); Assert.AreEqual(span2d[0, 1], 2); Assert.AreEqual(span2d[1, 2], 6); _ = new ReadOnlySpan2D <object>(new string[1, 2]); }
public void Test_2DArray_SpanVSpan2D_ReadOnly() { var unused = new byte[1024]; var array = new[, ] { { 1, 2, 3 }, { 4, 5, 6 } }; var height = array.GetLength(0); var width = array.GetLength(1); ReadOnlySpan <int> span = array.ToReadOnlySpan(); ReadOnlySpan2D <int> span2D = array; for (var i = 0; i < height; i++) { for (var j = 0; j < width; j++) { Assert.AreEqual(span2D[i, j], span[i * width + j]); } } array[1, 2] = 42; Assert.AreEqual(42, span[5]); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true, true); array[1, 2] = 6; for (var i = 0; i < height; i++) { for (var j = 0; j < width; j++) { Assert.AreEqual(span2D[i, j], span[i * width + j]); } } array[1, 2] = 100500; Assert.AreEqual(100500, span[5]); }
public void Test_HeapAlloc() { const int h = 17; const int w = 31; Span <int> buff = new int[h * w]; for (var i = 0; i < 17; i++) { for (var j = 0; j < w; j++) { buff[i * w + j] = i * 1000 + j; } } ReadOnlySpan2D <int> span2d = ((ReadOnlySpan <int>)buff).ToReadOnlySpan2D(h, w); Assert.AreEqual(14022, span2d[14, 22]); Assert.AreEqual(05013, span2d[05, 13]); }
public void Test_ReadOnlySpan2D_Stack() { Span <long> span1 = stackalloc long[] { 1, 2, 3, 4, 5, 6 }; ReadOnlySpan2D <long> span2D = ((ReadOnlySpan <long>)span1).ToReadOnlySpan2D(2, 3); ReadOnlySpan <long> span2 = SpanExtensions.GetRow(span2D, 0); ReadOnlySpan <long> span3 = SpanExtensions.GetRow(span2D, 1); Assert.IsTrue(span1.Slice(0, 3) == span2); Assert.IsTrue(span1.Slice(3, 3) == span3); span1[1] = 20; span1[4] = 50; Assert.AreEqual(20, span2[1]); Assert.AreEqual(50, span3[1]); Assert.IsTrue(span1.Slice(0, 3) == span2); Assert.IsTrue(span1.Slice(3, 3) == span3); } }
public unsafe void Test_ReadOnlySpan2DT_RefConstructor() { ReadOnlySpan <int> span = stackalloc[] { 1, 2, 3, 4, 5, 6 }; ReadOnlySpan2D <int> span2d = ReadOnlySpan2D <int> .DangerousCreate(span[0], 2, 3, 0); Assert.IsFalse(span2d.IsEmpty); Assert.AreEqual(span2d.Length, 6); Assert.AreEqual(span2d.Width, 3); Assert.AreEqual(span2d.Height, 2); Assert.AreEqual(span2d[0, 0], 1); Assert.AreEqual(span2d[1, 2], 6); Assert.ThrowsException <ArgumentOutOfRangeException>(() => ReadOnlySpan2D <int> .DangerousCreate(Unsafe.AsRef <int>(null), -1, 0, 0)); Assert.ThrowsException <ArgumentOutOfRangeException>(() => ReadOnlySpan2D <int> .DangerousCreate(Unsafe.AsRef <int>(null), 1, -2, 0)); Assert.ThrowsException <ArgumentOutOfRangeException>(() => ReadOnlySpan2D <int> .DangerousCreate(Unsafe.AsRef <int>(null), 1, 0, -5)); }
public void Test_ReadOnlySpan2DT_CopyTo_2() { int[,] array = { { 1, 2, 3 }, { 4, 5, 6 } }; ReadOnlySpan2D <int> span2d = new ReadOnlySpan2D <int>(array, 0, 1, 2, 2); int[] target = new int[4]; span2d.CopyTo(target); int[] expected = { 2, 3, 5, 6 }; CollectionAssert.AreEqual(target, expected); Assert.ThrowsException <ArgumentException>(() => new ReadOnlySpan2D <int>(array).CopyTo(Span <int> .Empty)); }
public void Test_ReadOnlySpan2DT_Array2DConstructor_2() { int[,] array = { { 1, 2, 3 }, { 4, 5, 6 } }; ReadOnlySpan2D <int> span2d = new ReadOnlySpan2D <int>(array, 0, 1, 2, 2); Assert.IsFalse(span2d.IsEmpty); Assert.AreEqual(span2d.Length, 4); Assert.AreEqual(span2d.Width, 2); Assert.AreEqual(span2d.Height, 2); Assert.AreEqual(span2d[0, 0], 2); Assert.AreEqual(span2d[1, 1], 6); _ = new ReadOnlySpan2D <object>(new string[1, 2]); Assert.ThrowsException <ArgumentOutOfRangeException>(() => new ReadOnlySpan2D <object>(new string[1, 2], 0, 0, 2, 2)); }
internal SegmentedSpan(ReadOnlySpan2D <byte> source) { Source = source; }
internal static ulong HashXx3(this ReadOnlySpan2D <byte> data) => XxHash3.Hash64(data);
/// <summary> /// Initializes a new instance of the <see cref="MemoryDebugView2D{T}"/> class with the specified parameters. /// </summary> /// <param name="span">The input <see cref="ReadOnlySpan2D{T}"/> instance with the items to display.</param> public MemoryDebugView2D(ReadOnlySpan2D <T> span) { this.Items = span.ToArray(); }