示例#1
0
        public void PinnedArrayTest()
        {
            using (PinnedArray <int> array = new PinnedArray <int>(10))
            {
                array.Pinned = true;
                Assert.AreEqual(10, array.Buffer.Length);
                for (int i = 0; i < 10; i++)
                {
                    array.Buffer[i] = i;
                }

                array.Resize(20);
                Assert.AreEqual(20, array.Buffer.Length);
                Assert.IsTrue(array.Buffer.Take(10).SequenceEqual(Enumerable.Range(0, 10)));

                array.Resize(5);
                Assert.AreEqual(5, array.Buffer.Length);
                Assert.IsTrue(array.Buffer.SequenceEqual(Enumerable.Range(0, 5)));

                int[] array2 = new int[5];
                Marshal.Copy(array.GetElementAddress(0), array2, 0, 5);
                Assert.IsTrue(array.Buffer.SequenceEqual(array2));

                int[] array3 = Enumerable.Range(5, 5).ToArray();
                Marshal.Copy(array3, 0, array.GetElementAddress(0), 5);
                Assert.IsTrue(array.Buffer.SequenceEqual(array3));
            }
        }