From 563673ab41b02d66ca981cffbb80d5172a944338 Mon Sep 17 00:00:00 2001 From: nietras Date: Fri, 7 Oct 2016 15:54:11 +0200 Subject: S.R.CS.Unsafe add CopyBlockUnaligned and InitBlockAligned. Add UIntPtr overload to all copy and init-block methods. (#12443) --- .../tests/UnsafeTests.cs | 151 +++++++++++++++++++++ 1 file changed, 151 insertions(+) (limited to 'src/System.Runtime.CompilerServices.Unsafe/tests') 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 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 CopyBlockData() { yield return new object[] { 0 }; -- cgit v1.2.3