Exemplo n.º 1
0
        public void ReturnsNullWhenThePtrIsZero()
        {
            var target = new MarshallingService();
            var result = target.PtrToUnicodeString(IntPtr.Zero, 1);

            Assert.IsNull(result);
        }
Exemplo n.º 2
0
        public void MarshalTheValueToAPointer()
        {
            var lpStubStructure = IntPtr.Zero;
            var target          = new MarshallingService();

            try
            {
                var sizeOfStubStructure = Marshal.SizeOf <StubStructure>();
                var value = new StubStructure
                {
                    Field1 = 1,
                    Field2 = 2,
                    Field3 = 3,
                    Field4 = "4"
                };

                lpStubStructure = Marshal.AllocHGlobal(sizeOfStubStructure);
                target.StructureToPtr(value, lpStubStructure);

                var result = Marshal.PtrToStructure <StubStructure>(lpStubStructure);

                Assert.AreEqual(1, result.Field1);
                Assert.AreEqual(2, result.Field2);
                Assert.AreEqual(3, result.Field3);
                Assert.AreEqual("4", result.Field4);
            }
            finally
            {
                if (lpStubStructure != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpStubStructure);
                }
            }
        }
Exemplo n.º 3
0
        public void IdentifyTheCorrectSizeOfAStructure()
        {
            var target = new MarshallingService();
            var result = target.SizeOf <StubStructure>();

            Assert.AreEqual(32, result);
        }
Exemplo n.º 4
0
        public void ReturnsEmptyStringWhenTheLengthIsZero()
        {
            var target = new MarshallingService();
            var result = target.PtrToUnicodeString(new IntPtr(1), 0);

            Assert.AreEqual(string.Empty, result);
        }
Exemplo n.º 5
0
        public void IdentifyTheCorrectSizeOfAnInteger()
        {
            var target = new MarshallingService();
            var result = target.SizeOf <int>();

            Assert.AreEqual(4, result);
        }
Exemplo n.º 6
0
        public void ReturnsNullPtrWhenNullBytes()
        {
            var target = new MarshallingService();
            var result = target.ByteArrayToPtr(null);

            Assert.AreEqual(IntPtr.Zero, result);
        }
Exemplo n.º 7
0
        public void ReturnsEmptyArrayWhenLengthIsZero()
        {
            var target = new MarshallingService();
            var result = target.PtrToByteArray(new IntPtr(1), 0);

            CollectionAssert.IsEmpty(result);
        }
Exemplo n.º 8
0
        public void ReturnsNullArrayWhenZero()
        {
            var target = new MarshallingService();
            var result = target.PtrToByteArray(IntPtr.Zero, 0);

            Assert.Null(result);
        }
Exemplo n.º 9
0
        public void ReturnsNullWhenTheSizeIsZero()
        {
            var target = new MarshallingService();
            var result = target.AllocHGlobal(0);

            Assert.AreEqual(IntPtr.Zero, result);
        }
Exemplo n.º 10
0
        public void AllocateThePointerSpecified()
        {
            var result = IntPtr.Zero;

            try
            {
                var target = new MarshallingService();
                result = target.AllocHGlobal(4);

                Assert.AreNotEqual(IntPtr.Zero, result);
            }
            finally
            {
                if (result != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(result);
                }
            }
        }
Exemplo n.º 11
0
        public void MarshalsTheStringFromThePtrAsExpected()
        {
            var ptr      = IntPtr.Zero;
            var expected = "Hello";

            try
            {
                ptr = Marshal.StringToHGlobalUni(expected);

                var target = new MarshallingService();
                var result = target.PtrToUnicodeString(ptr, expected.Length);

                Assert.AreEqual(expected, result);
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
Exemplo n.º 12
0
        public void MarshalsTheByteArrayToAPtrAsExpected()
        {
            IntPtr ptr = IntPtr.Zero;

            try
            {
                var bytes = new byte[] { 1, 2, 3, 4, 5 };

                var target = new MarshallingService();
                var result = target.ByteArrayToPtr(bytes);

                Assert.True(result != IntPtr.Zero);
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }
            }
        }
Exemplo n.º 13
0
        public void MarshalsThePtrToByteArrayAsExpected()
        {
            IntPtr ptr = IntPtr.Zero;

            try
            {
                var bytes = new byte[] { 1, 2, 3, 4 };

                var target = new MarshallingService();
                ptr = target.ByteArrayToPtr(bytes);

                var result = target.PtrToByteArray(ptr, bytes.Length);

                CollectionAssert.AreEqual(bytes, result);
            }
            finally
            {
                if (ptr != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(ptr);
                }
            }
        }
Exemplo n.º 14
0
        public void ThrowsAnExceptionWhenTheSizeIsLessThanZero()
        {
            var target = new MarshallingService();

            Assert.Throws <ArgumentException>(() => target.AllocHGlobal(0));
        }
Exemplo n.º 15
0
        public void ThrowAnExceptionWhenThePtrIsZero()
        {
            var target = new MarshallingService();

            Assert.Throws <ArgumentNullException>(() => target.StructureToPtr(new StubStructure(), IntPtr.Zero));
        }