Example #1
0
        public void CopyPointerToPointer_Unsafe()
        {
            var size = _container.Size;

            unsafe
            {
                using var destContainer0 = new UnmanagedMemoryContainer(size);
                void *srcPointer  = _container.IntPtr.ToPointer();
                void *destPointer = destContainer0.IntPtr.ToPointer();

                // void* srcPointer / void* destPointer
                Unsafe.CopyBlock(srcPointer, destPointer, (uint)size);

                Assert.Equal(_container.GetSum(), destContainer0.GetSum());
            }

            unsafe
            {
                using var destContainer1 = new UnmanagedMemoryContainer(size);
                void *srcPointer  = _container.IntPtr.ToPointer();
                void *destPointer = destContainer1.IntPtr.ToPointer();

                // Unalignedにする必要ない(使いたいだけ)
                Unsafe.CopyBlockUnaligned(srcPointer, destPointer, (uint)size);

                Assert.Equal(_container.GetSum(), destContainer1.GetSum());
            }
        }
        public static ulong GetSum(this UnmanagedMemoryContainer container)
        {
            ulong sum = 0;

            for (var i = 0; i < container.Size; ++i)
            {
                sum += Marshal.ReadByte(container.IntPtr, i);
            }
            return(sum);
        }
        public static UnmanagedMemoryContainer GetInstanceBurst256Byte()
        {
            var container = new UnmanagedMemoryContainer(256);

            for (var i = 0; i < container.Size; ++i)
            {
                Marshal.WriteByte(container.IntPtr, i, (byte)i);
            }

            return(container);
        }
Example #4
0
        public void CopyPointerToPointer_memcpy()
        {
            var size = _container.Size;

            using var destContainer = new UnmanagedMemoryContainer(size);
            var srcIntPtr  = _container.IntPtr;
            var destIntPtr = destContainer.IntPtr;

            _ = memcpy(destIntPtr, srcIntPtr, (UIntPtr)size);

            Assert.Equal(_container.GetSum(), destContainer.GetSum());
        }
Example #5
0
        public void CopyPointerToPointer_Buffer()
        {
            unsafe
            {
                var size = _container.Size;
                using var destContainer = new UnmanagedMemoryContainer(size);
                void *srcPointer  = _container.IntPtr.ToPointer();
                void *destPointer = destContainer.IntPtr.ToPointer();

                // void* srcPointer / void* destPointer
                Buffer.MemoryCopy(srcPointer, destPointer, destinationSizeInBytes: size, sourceBytesToCopy: size);

                Assert.Equal(_container.GetSum(), destContainer.GetSum());
            }
        }
Example #6
0
        public void CopyArrayToPointer()
        {
            var size     = 256;
            var srcArray = Enumerable.Range(0, size).Select(x => (byte)x).ToArray();

            using var destContainer = new UnmanagedMemoryContainer(size);
            var destIntPtr = destContainer.IntPtr;

            Marshal.Copy(srcArray, startIndex: 0, destIntPtr, srcArray.Length);

            ulong sum = 0;

            foreach (var b in srcArray)
            {
                sum += b;
            }
            Assert.Equal(sum, destContainer.GetSum());
        }
Example #7
0
 public ConvertTests()
 {
     // テスト開始の度にコールされる
     _container = UnmanagedMemoryContainerExtension.GetInstanceBurst256Byte();
 }