public unsafe void RawArraySlice() { const int memLen = 5; var mem = stackalloc int[memLen] { 0, 1, 2, 3, 4 }; var array = new RawArray <int>((IntPtr)mem, memLen); var array03 = new RawArray <int>((IntPtr)mem, 3); var array22 = new RawArray <int>((IntPtr)(mem + 2), 2); var array32 = new RawArray <int>((IntPtr)(mem + 3), 2); AssertEqual(array.Slice(0, array.Length), array); AssertEqual(array.Slice(0, 3), array03); AssertEqual(array.Slice(2, 2), array22); AssertEqual(array.Slice(3, 2), array32); AssertEqual(array.Slice(3), array32); AssertEqual(array.Slice(0), array); Assert.True(array.Slice(5).IsEmpty); Assert.True(array.Slice(5, 0).IsEmpty); AssertEqual(array.AsSpan(0, array.Length), array); AssertEqual(array.AsSpan(0, 3), array03); AssertEqual(array.AsSpan(2, 2), array22); AssertEqual(array.AsSpan(3, 2), array32); AssertEqual(array.AsSpan(3), array32); AssertEqual(array.AsSpan(0), array); Assert.True(array.AsSpan(5).IsEmpty); Assert.True(array.AsSpan(5, 0).IsEmpty); }
private static void AssertEqual <T>(ReadOnlySpan <T> array1, RawArray <T> array2) where T : unmanaged, IEquatable <T> { Assert.True(array1.SequenceEqual(array2.AsSpan())); }