예제 #1
0
        public void Test_Memory2DT_TryGetMemory_1()
        {
            int[,] array =
            {
                { 1, 2, 3 },
                { 4, 5, 6 }
            };

            Memory2D <int> memory2d = new Memory2D <int>(array);

            // Here we test that we can get a Memory<T> from a 2D one when the underlying
            // data is contiguous. Note that in this case this can only work on runtimes
            // with fast Span<T> support, because otherwise it's not possible to get a
            // Memory<T> (or a Span<T> too, for that matter) from a 2D array.
            bool success = memory2d.TryGetMemory(out Memory <int> memory);

#if WINDOWS_UWP
            Assert.IsFalse(success);
            Assert.IsTrue(memory.IsEmpty);
#else
            Assert.IsTrue(success);
            Assert.AreEqual(memory.Length, array.Length);
            Assert.IsTrue(Unsafe.AreSame(ref array[0, 0], ref memory.Span[0]));
#endif
        }
예제 #2
0
        public void Test_Memory2DT_TryGetMemory_2()
        {
            int[] array = { 1, 2, 3, 4 };

            Memory2D <int> memory2d = new Memory2D <int>(array, 2, 2);

            // Same test as above, but this will always succeed on all runtimes,
            // as creating a Memory<T> from a 1D array is always supported.
            bool success = memory2d.TryGetMemory(out Memory <int> memory);

            Assert.IsTrue(success);
            Assert.AreEqual(memory.Length, array.Length);
            Assert.AreEqual(memory.Span[2], 3);
        }
예제 #3
0
        public void Test_Memory2DT_TryGetMemory_3()
        {
            Memory <int> data = new[] { 1, 2, 3, 4 };

            Memory2D <int> memory2d = data.AsMemory2D(2, 2);

            // Same as above, just with the extra Memory<T> indirection. Same as above,
            // this test is only supported on runtimes with fast Span<T> support.
            // On others, we just don't expose the Memory<T>.AsMemory2D extension.
            bool success = memory2d.TryGetMemory(out Memory <int> memory);

            Assert.IsTrue(success);
            Assert.AreEqual(memory.Length, data.Length);
            Assert.AreEqual(memory.Span[2], 3);
        }