예제 #1
0
        public static void OwnedMemoryRetainWithPinningAndSlice()
        {
            int[]                array  = { 1, 2, 3, 4, 5 };
            OwnedMemory <int>    owner  = new CustomMemoryForTest <int>(array);
            ReadOnlyMemory <int> memory = owner.Memory.Slice(1);
            MemoryHandle         handle = memory.Retain(pin: true);
            ReadOnlySpan <int>   span   = memory.Span;

            Assert.True(handle.HasPointer);
            unsafe
            {
                int *pointer = (int *)handle.Pointer;

                GC.Collect();

                for (int i = 0; i < memory.Length; i++)
                {
                    Assert.Equal(array[i + 1], pointer[i]);
                }

                for (int i = 0; i < memory.Length; i++)
                {
                    Assert.Equal(array[i + 1], span[i]);
                }
            }
            handle.Dispose();
        }
예제 #2
0
        public static void ToStringFromMemoryFromOwnedMemory()
        {
            int[]             a        = { 91, 92, -93, 94 };
            OwnedMemory <int> intOwner = new CustomMemoryForTest <int>(a);

            Assert.Equal("System.ReadOnlyMemory<Int32>[4]", ((ReadOnlyMemory <int>)intOwner.Memory).ToString());

            intOwner = new CustomMemoryForTest <int>(Array.Empty <int>());
            Assert.Equal("System.ReadOnlyMemory<Int32>[0]", ((ReadOnlyMemory <int>)intOwner.Memory).ToString());

            char[]             charArray = { '1', '2', '-', '4' };
            OwnedMemory <char> charOwner = new CustomMemoryForTest <char>(charArray);

            Assert.Equal("12-4", ((ReadOnlyMemory <char>)charOwner.Memory).ToString());

            charOwner = new CustomMemoryForTest <char>(Array.Empty <char>());
            Assert.Equal("", ((ReadOnlyMemory <char>)charOwner.Memory).ToString());

            string[]             strArray = { "91", "92", "-93", "94" };
            OwnedMemory <string> strOwner = new CustomMemoryForTest <string>(strArray);

            Assert.Equal("System.ReadOnlyMemory<String>[4]", ((ReadOnlyMemory <string>)strOwner.Memory).ToString());

            strOwner = new CustomMemoryForTest <string>(Array.Empty <string>());
            Assert.Equal("System.ReadOnlyMemory<String>[0]", ((ReadOnlyMemory <string>)strOwner.Memory).ToString());
        }
예제 #3
0
        public static void ToStringFromMemoryFromMemoryManager()
        {
            int[] a = { 91, 92, -93, 94 };
            MemoryManager <int> intManager = new CustomMemoryForTest <int>(a);

            Assert.Equal("System.Memory<Int32>[4]", intManager.Memory.ToString());

            intManager = new CustomMemoryForTest <int>(Array.Empty <int>());
            Assert.Equal("System.Memory<Int32>[0]", intManager.Memory.ToString());

            char[] charArray = { '1', '2', '-', '4' };
            MemoryManager <char> charManager = new CustomMemoryForTest <char>(charArray);

            Assert.Equal("12-4", charManager.Memory.ToString());

            charManager = new CustomMemoryForTest <char>(Array.Empty <char>());
            Assert.Equal("", charManager.Memory.ToString());

            string[] strArray = { "91", "92", "-93", "94" };
            MemoryManager <string> strManager = new CustomMemoryForTest <string>(strArray);

            Assert.Equal("System.Memory<String>[4]", strManager.Memory.ToString());

            strManager = new CustomMemoryForTest <string>(Array.Empty <string>());
            Assert.Equal("System.Memory<String>[0]", strManager.Memory.ToString());
        }
예제 #4
0
        public static void TryGetOwnedMemory_IndexLength()
        {
            int[]                array        = new int[10];
            OwnedMemory <int>    orignalOwner = new CustomMemoryForTest <int>(array);
            ReadOnlyMemory <int> memory       = orignalOwner.Memory;

            for (int i = 0; i < array.Length; i++)
            {
                Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out CustomMemoryForTest <int> customOwner, out int index, out int length));
                Assert.Same(orignalOwner, customOwner);
                Assert.Equal(i, index);
                Assert.Equal(array.Length - i, length);
            }

            for (int i = 0; i < array.Length; i++)
            {
                Assert.True(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OwnedMemory <int> owner, out int index, out int length));
                Assert.Same(orignalOwner, owner);
                Assert.Equal(i, index);
                Assert.Equal(array.Length - i, length);
            }

            for (int i = 0; i < array.Length; i++)
            {
                Assert.False(MemoryMarshal.TryGetOwnedMemory(memory.Slice(i), out OtherMemoryForTest <int> notOwner, out int index, out int length));
                Assert.Null(notOwner);
            }
        }
예제 #5
0
        public static void MemoryManagerPinAndSlice()
        {
            int[] array = { 1, 2, 3, 4, 5 };
            MemoryManager <int>  manager = new CustomMemoryForTest <int>(array);
            ReadOnlyMemory <int> memory  = manager.Memory.Slice(1);
            MemoryHandle         handle  = memory.Pin();
            ReadOnlySpan <int>   span    = memory.Span;

            unsafe
            {
                int *pointer = (int *)handle.Pointer;
                Assert.True(pointer != null);

                GC.Collect();

                for (int i = 0; i < memory.Length; i++)
                {
                    Assert.Equal(array[i + 1], pointer[i]);
                }

                for (int i = 0; i < memory.Length; i++)
                {
                    Assert.Equal(array[i + 1], span[i]);
                }
            }
            handle.Dispose();
        }
예제 #6
0
        public static void MemoryFromOwnedMemoryAfterDispose()
        {
            int[]             a     = { 91, 92, -93, 94 };
            OwnedMemory <int> owner = new CustomMemoryForTest <int>(a);

            owner.Dispose();
            Assert.Throws <ObjectDisposedException>(() => owner.AsMemory);
        }
예제 #7
0
        public static void MemoryFromOwnedMemoryLong()
        {
            long[]             a      = { 91, -92, 93, 94, -95 };
            OwnedMemory <long> owner  = new CustomMemoryForTest <long>(a);
            Memory <long>      memory = owner.AsMemory;

            memory.Validate(91, -92, 93, 94, -95);
        }
예제 #8
0
        public static void MemoryFromOwnedMemoryInt()
        {
            int[]             a      = { 91, 92, -93, 94 };
            OwnedMemory <int> owner  = new CustomMemoryForTest <int>(a);
            Memory <int>      memory = owner.AsMemory;

            memory.Validate(91, 92, -93, 94);
        }
예제 #9
0
        public static void ImplicitReadOnlyMemoryFromMemoryManager()
        {
            long[] a = { 91, -92, 93, 94, -95 };
            MemoryManager <long> manager = new CustomMemoryForTest <long>(a);
            Memory <long>        memory  = manager.Memory;

            CastReadOnly <long>(memory, 91, -92, 93, 94, -95);
        }
예제 #10
0
파일: OwnedMemory.cs 프로젝트: wli3/corefx
        public static void ImplicitReadOnlyMemoryFromOwnedMemory()
        {
            long[]             a      = { 91, -92, 93, 94, -95 };
            OwnedMemory <long> owner  = new CustomMemoryForTest <long>(a);
            Memory <long>      memory = owner.Memory;

            CastReadOnly <long>(memory, 91, -92, 93, 94, -95);
        }
예제 #11
0
파일: OwnedMemory.cs 프로젝트: wli3/corefx
        public static void OwnedMemoryPinEmptyArray()
        {
            int[]             a      = {};
            OwnedMemory <int> owner  = new CustomMemoryForTest <int>(a);
            MemoryHandle      handle = owner.Pin();

            Assert.True(handle.HasPointer);
        }
예제 #12
0
        public static void DisposeOwnedMemoryAfterRetain()
        {
            int[]             a     = { 91, 92, -93, 94 };
            OwnedMemory <int> owner = new CustomMemoryForTest <int>(a);

            owner.Retain();
            Assert.Throws <InvalidOperationException>(() => owner.Dispose());
            owner.Release();
        }
예제 #13
0
        public static void TryGetArrayFromEmptyOwnedMemory()
        {
            int[]             array = new int[0];
            OwnedMemory <int> owner = new CustomMemoryForTest <int>(array);

            Assert.True(owner.Memory.TryGetArray(out ArraySegment <int> segment));
            Assert.Same(array, segment.Array);
            Assert.Equal(0, segment.Array.Length);
        }
예제 #14
0
        public static void TryGetArrayFromEmptyMemoryManager()
        {
            int[] array = new int[0];
            MemoryManager <int> manager = new CustomMemoryForTest <int>(array);

            Assert.True(MemoryMarshal.TryGetArray(manager.Memory, out ArraySegment <int> segment));
            Assert.Same(array, segment.Array);
            Assert.Equal(0, segment.Array.Length);
        }
예제 #15
0
파일: OwnedMemory.cs 프로젝트: wli3/corefx
        public static void OwnedMemoryDispose()
        {
            int[]             a     = { 91, 92, -93, 94 };
            OwnedMemory <int> owner = new CustomMemoryForTest <int>(a);

            Assert.False(owner.IsDisposed);
            owner.Dispose();
            Assert.True(owner.IsDisposed);
        }
예제 #16
0
        public static void DisposeOwnedMemoryAfterRetainAndRelease()
        {
            int[]             a     = { 91, 92, -93, 94 };
            OwnedMemory <int> owner = new CustomMemoryForTest <int>(a);

            owner.Retain();
            owner.Release();
            owner.Dispose();
            Assert.True(owner.IsDisposed);
        }
예제 #17
0
        public static void SpanFromMemoryManagerAfterDispose()
        {
            int[] a = { 91, 92, -93, 94 };
            MemoryManager <int> manager;

            using (manager = new CustomMemoryForTest <int>(a))
            {
            }
            Assert.Throws <ObjectDisposedException>(() => manager.GetSpan());
        }
예제 #18
0
        public static void MemoryManagerDispose()
        {
            int[] a = { 91, 92, -93, 94 };
            CustomMemoryForTest <int> manager;

            using (manager = new CustomMemoryForTest <int>(a))
            {
                Assert.False(manager.IsDisposed);
            }
            Assert.True(manager.IsDisposed);
        }
예제 #19
0
        public static void MemoryManagerPinEmptyArray()
        {
            int[] a = { };
            MemoryManager <int> manager = new CustomMemoryForTest <int>(a);
            MemoryHandle        handle  = manager.Pin();

            unsafe
            {
                Assert.True(handle.Pointer != null);
            }
        }
예제 #20
0
파일: OwnedMemory.cs 프로젝트: wli3/corefx
        public static void MemoryFromOwnedMemoryObject()
        {
            object o1 = new object();
            object o2 = new object();

            object[]             a      = { o1, o2 };
            OwnedMemory <object> owner  = new CustomMemoryForTest <object>(a);
            Memory <object>      memory = owner.Memory;

            memory.ValidateReferenceType(o1, o2);
        }
예제 #21
0
        public static void MemoryFromMemoryManagerObject()
        {
            object o1 = new object();
            object o2 = new object();

            object[] a = { o1, o2 };
            MemoryManager <object> manager = new CustomMemoryForTest <object>(a);
            Memory <object>        memory  = manager.Memory;

            memory.ValidateReferenceType(o1, o2);
        }
예제 #22
0
        public static void MemoryManagerCtorInvalid()
        {
            int[] a = { 91, 92, -93, 94 };
            MemoryManager <int> manager = new CustomMemoryForTest <int>(a);

            Assert.Throws <ArgumentOutOfRangeException>(() => new Memory <int>(manager, 0, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new Memory <int>(manager, -1, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => new Memory <int>(manager, -1, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new Memory <int>(manager, -1, -1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new Memory <int>(manager, 0, a.Length + 1));
            Assert.Throws <ArgumentOutOfRangeException>(() => new Memory <int>(manager, a.Length + 1, 0));
            Assert.Throws <ArgumentOutOfRangeException>(() => new Memory <int>(manager, 1, a.Length));
        }
예제 #23
0
        public static void MemoryManagerTryGetArray()
        {
            int[] array = new int[10];
            MemoryManager <int>  manager = new CustomMemoryForTest <int>(array);
            ReadOnlyMemory <int> memory  = manager.Memory;

            Assert.True(MemoryMarshal.TryGetArray(memory, out ArraySegment <int> segment));
            Assert.Equal(array.Length, segment.Count);

            for (int i = segment.Offset; i < segment.Count + segment.Offset; i++)
            {
                Assert.Equal(array[i], segment.Array[i]);
            }
        }
예제 #24
0
파일: OwnedMemory.cs 프로젝트: wli3/corefx
        public static void MemoryFromOwnedMemoryLong()
        {
            long[]             a      = { 91, -92, 93, 94, -95 };
            OwnedMemory <long> owner  = new CustomMemoryForTest <long>(a);
            Memory <long>      memory = owner.Memory;

            memory.Validate(91, -92, 93, 94, -95);
            memory.Slice(0, 5).Validate(91, -92, 93, 94, -95);
            memory.Slice(1, 0).Validate();
            memory.Slice(1, 1).Validate(-92);
            memory.Slice(1, 2).Validate(-92, 93);
            memory.Slice(2, 3).Validate(93, 94, -95);
            memory.Slice(5, 0).Validate();
        }
예제 #25
0
파일: OwnedMemory.cs 프로젝트: wli3/corefx
        public static void ReadOnlyMemoryFromMemoryFromOwnedMemoryInt()
        {
            int[]                a              = { 91, 92, -93, 94 };
            OwnedMemory <int>    owner          = new CustomMemoryForTest <int>(a);
            ReadOnlyMemory <int> readOnlyMemory = owner.Memory;

            readOnlyMemory.Validate(91, 92, -93, 94);
            readOnlyMemory.Slice(0, 4).Validate(91, 92, -93, 94);
            readOnlyMemory.Slice(1, 0).Validate();
            readOnlyMemory.Slice(1, 1).Validate(92);
            readOnlyMemory.Slice(1, 2).Validate(92, -93);
            readOnlyMemory.Slice(2, 2).Validate(-93, 94);
            readOnlyMemory.Slice(4, 0).Validate();
        }
예제 #26
0
        public static void SliceWithStartAndLengthPastEnd()
        {
            int[]        a      = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
            Memory <int> memory = new Memory <int>(a).Slice(a.Length, 0);

            Assert.Equal(0, memory.Length);
            Assert.True(Unsafe.AreSame(ref a[a.Length - 1], ref Unsafe.Subtract(ref MemoryMarshal.GetReference(memory.Span), 1)));

            MemoryManager <int> manager           = new CustomMemoryForTest <int>(a);
            Memory <int>        memoryFromManager = manager.Memory.Slice(a.Length, 0);

            Assert.Equal(0, memoryFromManager.Length);
            Assert.True(Unsafe.AreSame(ref a[a.Length - 1], ref Unsafe.Subtract(ref MemoryMarshal.GetReference(memoryFromManager.Span), 1)));
        }
예제 #27
0
        public static void SliceWithStart()
        {
            int[]        a      = { 90, 91, 92, 93, 94, 95, 96, 97, 98, 99 };
            Memory <int> memory = new Memory <int>(a).Slice(6);

            Assert.Equal(4, memory.Length);
            Assert.True(Unsafe.AreSame(ref a[6], ref MemoryMarshal.GetReference(memory.Span)));

            MemoryManager <int> manager           = new CustomMemoryForTest <int>(a);
            Memory <int>        memoryFromManager = manager.Memory.Slice(6);

            Assert.Equal(4, memoryFromManager.Length);
            Assert.True(Unsafe.AreSame(ref a[6], ref MemoryMarshal.GetReference(memoryFromManager.Span)));
        }
예제 #28
0
        public static void OwnedMemoryRetainWithoutPinning()
        {
            int[]                array  = { 1, 2, 3, 4, 5 };
            OwnedMemory <int>    owner  = new CustomMemoryForTest <int>(array);
            ReadOnlyMemory <int> memory = owner.Memory;
            MemoryHandle         handle = memory.Retain();

            Assert.False(handle.HasPointer);
            unsafe
            {
                Assert.True(handle.Pointer == null);
            }
            handle.Dispose();
        }
예제 #29
0
파일: Retain.cs 프로젝트: samerames/corefx
        public static void OwnedMemoryRetainWithoutPinning()
        {
            int[]             array  = { 1, 2, 3, 4, 5 };
            OwnedMemory <int> owner  = new CustomMemoryForTest <int>(array);
            Memory <int>      memory = owner.Memory;
            MemoryHandle      handle = memory.Retain();

            unsafe
            {
                int *pointer = (int *)handle.PinnedPointer;
                Assert.True(pointer == null);
            }
            handle.Dispose();
        }
예제 #30
0
        public static void OwnedMemoryTryGetArray()
        {
            int[]             array  = new int[10];
            OwnedMemory <int> owner  = new CustomMemoryForTest <int>(array);
            Memory <int>      memory = owner.Memory;

            Assert.True(memory.TryGetArray(out ArraySegment <int> segment));
            Assert.Equal(array.Length, segment.Count);

            for (int i = segment.Offset; i < segment.Count + segment.Offset; i++)
            {
                Assert.Equal(array[i], segment.Array[i]);
            }
        }