diff options
author | nietras <nietras@users.noreply.github.com> | 2016-10-07 16:54:11 +0300 |
---|---|---|
committer | Jan Kotas <jkotas@microsoft.com> | 2016-10-07 16:54:11 +0300 |
commit | 563673ab41b02d66ca981cffbb80d5172a944338 (patch) | |
tree | 31c9939f134f2fae37adcc021fd5284fbe46006a /src/System.Runtime.CompilerServices.Unsafe/tests | |
parent | 36c90ac693ff812b168266e6d4e6b2ab1b677615 (diff) |
S.R.CS.Unsafe add CopyBlockUnaligned and InitBlockAligned. Add UIntPtr overload to all copy and init-block methods. (#12443)
Diffstat (limited to 'src/System.Runtime.CompilerServices.Unsafe/tests')
-rw-r--r-- | src/System.Runtime.CompilerServices.Unsafe/tests/UnsafeTests.cs | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/System.Runtime.CompilerServices.Unsafe/tests/UnsafeTests.cs b/src/System.Runtime.CompilerServices.Unsafe/tests/UnsafeTests.cs index 2cca63b8d6..ab8067c1d0 100644 --- a/src/System.Runtime.CompilerServices.Unsafe/tests/UnsafeTests.cs +++ b/src/System.Runtime.CompilerServices.Unsafe/tests/UnsafeTests.cs @@ -179,6 +179,83 @@ namespace System.Runtime.CompilerServices } } + [Theory] + [MemberData(nameof(InitBlockData))] + public static unsafe void InitBlockUIntPtrStack(int numBytes, byte value) + { + byte* stackPtr = stackalloc byte[numBytes]; + Unsafe.InitBlock(stackPtr, value, (UIntPtr)numBytes); + for (int i = 0; i < numBytes; i++) + { + Assert.Equal(stackPtr[i], value); + } + } + + [Theory] + [MemberData(nameof(InitBlockData))] + public static unsafe void InitBlockUIntPtrUnmanaged(int numBytes, byte value) + { + IntPtr allocatedMemory = Marshal.AllocCoTaskMem(numBytes); + byte* bytePtr = (byte*)allocatedMemory.ToPointer(); + Unsafe.InitBlock(bytePtr, value, (UIntPtr)numBytes); + for (int i = 0; i < numBytes; i++) + { + Assert.Equal(bytePtr[i], value); + } + } + + [Theory] + [MemberData(nameof(InitBlockData))] + public static unsafe void InitBlockUnalignedStack(int numBytes, byte value) + { + byte* stackPtr = stackalloc byte[numBytes + 1]; + stackPtr += 1; // +1 = make unaligned + Unsafe.InitBlockUnaligned(stackPtr, value, (uint)numBytes); + for (int i = 0; i < numBytes; i++) + { + Assert.Equal(stackPtr[i], value); + } + } + + [Theory] + [MemberData(nameof(InitBlockData))] + public static unsafe void InitBlockUnalignedUnmanaged(int numBytes, byte value) + { + IntPtr allocatedMemory = Marshal.AllocCoTaskMem(numBytes + 1); + byte* bytePtr = (byte*)allocatedMemory.ToPointer() + 1; // +1 = make unaligned + Unsafe.InitBlockUnaligned(bytePtr, value, (uint)numBytes); + for (int i = 0; i < numBytes; i++) + { + Assert.Equal(bytePtr[i], value); + } + } + + [Theory] + [MemberData(nameof(InitBlockData))] + public static unsafe void InitBlockUnalignedUIntPtrStack(int numBytes, byte value) + { + byte* stackPtr = stackalloc byte[numBytes + 1]; + stackPtr += 1; // +1 = make unaligned + Unsafe.InitBlockUnaligned(stackPtr, value, (UIntPtr)numBytes); + for (int i = 0; i < numBytes; i++) + { + Assert.Equal(stackPtr[i], value); + } + } + + [Theory] + [MemberData(nameof(InitBlockData))] + public static unsafe void InitBlockUnalignedUIntPtrUnmanaged(int numBytes, byte value) + { + IntPtr allocatedMemory = Marshal.AllocCoTaskMem(numBytes + 1); + byte* bytePtr = (byte*)allocatedMemory.ToPointer() + 1; // +1 = make unaligned + Unsafe.InitBlockUnaligned(bytePtr, value, (UIntPtr)numBytes); + for (int i = 0; i < numBytes; i++) + { + Assert.Equal(bytePtr[i], value); + } + } + public static IEnumerable<object[]> InitBlockData() { yield return new object[] { 0, 1 }; @@ -212,6 +289,80 @@ namespace System.Runtime.CompilerServices } } + [Theory] + [MemberData(nameof(CopyBlockData))] + public static unsafe void CopyBlockUIntPtr(int numBytes) + { + byte* source = stackalloc byte[numBytes]; + byte* destination = stackalloc byte[numBytes]; + + for (int i = 0; i < numBytes; i++) + { + byte value = (byte)(i % 255); + source[i] = value; + } + + Unsafe.CopyBlock(destination, source, (UIntPtr)numBytes); + + for (int i = 0; i < numBytes; i++) + { + byte value = (byte)(i % 255); + Assert.Equal(value, destination[i]); + Assert.Equal(source[i], destination[i]); + } + } + + [Theory] + [MemberData(nameof(CopyBlockData))] + public static unsafe void CopyBlockUnaligned(int numBytes) + { + byte* source = stackalloc byte[numBytes + 1]; + byte* destination = stackalloc byte[numBytes + 1]; + source += 1; // +1 = make unaligned + destination += 1; // +1 = make unaligned + + for (int i = 0; i < numBytes; i++) + { + byte value = (byte)(i % 255); + source[i] = value; + } + + Unsafe.CopyBlockUnaligned(destination, source, (uint)numBytes); + + for (int i = 0; i < numBytes; i++) + { + byte value = (byte)(i % 255); + Assert.Equal(value, destination[i]); + Assert.Equal(source[i], destination[i]); + } + } + + + [Theory] + [MemberData(nameof(CopyBlockData))] + public static unsafe void CopyBlockUnalignedUIntPtr(int numBytes) + { + byte* source = stackalloc byte[numBytes + 1]; + byte* destination = stackalloc byte[numBytes + 1]; + source += 1; // +1 = make unaligned + destination += 1; // +1 = make unaligned + + for (int i = 0; i < numBytes; i++) + { + byte value = (byte)(i % 255); + source[i] = value; + } + + Unsafe.CopyBlockUnaligned(destination, source, (UIntPtr)numBytes); + + for (int i = 0; i < numBytes; i++) + { + byte value = (byte)(i % 255); + Assert.Equal(value, destination[i]); + Assert.Equal(source[i], destination[i]); + } + } + public static IEnumerable<object[]> CopyBlockData() { yield return new object[] { 0 }; |