public static void ByteOffsetStackByte4() { var byte4 = new Byte4(); Assert.Equal(new IntPtr(0), UnsafeIn.ByteOffset(byte4.B0, byte4.B0)); Assert.Equal(new IntPtr(1), UnsafeIn.ByteOffset(byte4.B0, byte4.B1)); Assert.Equal(new IntPtr(-1), UnsafeIn.ByteOffset(byte4.B1, byte4.B0)); Assert.Equal(new IntPtr(2), UnsafeIn.ByteOffset(byte4.B0, byte4.B2)); Assert.Equal(new IntPtr(-2), UnsafeIn.ByteOffset(byte4.B2, byte4.B0)); Assert.Equal(new IntPtr(3), UnsafeIn.ByteOffset(byte4.B0, byte4.B3)); Assert.Equal(new IntPtr(-3), UnsafeIn.ByteOffset(byte4.B3, byte4.B0)); }
public static void ByteOffsetArray() { var a = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }; Assert.Equal(new IntPtr(0), UnsafeIn.ByteOffset(a[0], a[0])); Assert.Equal(new IntPtr(1), UnsafeIn.ByteOffset(a[0], a[1])); Assert.Equal(new IntPtr(-1), UnsafeIn.ByteOffset(a[1], a[0])); Assert.Equal(new IntPtr(2), UnsafeIn.ByteOffset(a[0], a[2])); Assert.Equal(new IntPtr(-2), UnsafeIn.ByteOffset(a[2], a[0])); Assert.Equal(new IntPtr(3), UnsafeIn.ByteOffset(a[0], a[3])); Assert.Equal(new IntPtr(4), UnsafeIn.ByteOffset(a[0], a[4])); Assert.Equal(new IntPtr(5), UnsafeIn.ByteOffset(a[0], a[5])); Assert.Equal(new IntPtr(6), UnsafeIn.ByteOffset(a[0], a[6])); Assert.Equal(new IntPtr(7), UnsafeIn.ByteOffset(a[0], a[7])); }
public static unsafe void CopyToVoidPtr() { int value = 10; int destination = -1; UnsafeIn.Copy(UnsafeIn.AsPointer(in destination), value); Assert.Equal(10, destination); Assert.Equal(10, value); int destination2 = -1; UnsafeIn.Copy(&destination2, value); Assert.Equal(10, destination2); Assert.Equal(10, value); }
public static unsafe void CopyBlockRef(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; } UnsafeIn.CopyBlock(ref destination[0], source[0], (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]); } }
public static unsafe void CopyBlockUnalignedRef(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; } UnsafeIn.CopyBlockUnaligned(ref destination[0], source[0], (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]); } }