Ejemplo n.º 1
0
        public void ValidateIgnoreCharSet(char value, uint expectedUInt)
        {
            char expected = (char)expectedUInt;

            Assert.Equal(expected, NativeExportsNE.ReturnU2AsU2IgnoreCharSet(value));
            Assert.Equal(expected, NativeExportsNE.ReturnI2AsI2IgnoreCharSet(value));
        }
Ejemplo n.º 2
0
        public void NonBlittableStructPinnableMarshalerPassByRef()
        {
            string str      = "Hello world!";
            string expected = ReverseChars(str);

            NativeExportsNE.ReverseReplaceString(ref str);
            Assert.Equal(expected, str);
        }
Ejemplo n.º 3
0
        public void ValidateRefCharAsBuffer()
        {
            char[] chars    = CharacterMappings().Select(o => (char)o[0]).ToArray();
            char[] expected = new char[chars.Length];
            Array.Copy(chars, expected, chars.Length);
            Array.Reverse(expected);

            NativeExportsNE.ReverseBuffer(ref MemoryMarshal.GetArrayDataReference(chars), chars.Length);
            Assert.Equal(expected, chars);
        }
Ejemplo n.º 4
0
        public void NonBlittableStructIn(bool b1, bool b2, bool b3)
        {
            var container = new BoolStruct
            {
                b1 = b1,
                b2 = b2,
                b3 = b3
            };

            Assert.Equal(b1 && b2 && b3, NativeExportsNE.AndBoolsRef(container));
        }
Ejemplo n.º 5
0
        public void GetPinnableReferenceMarshalling()
        {
            int originalValue = 42;
            var wrapper       = new IntWrapper {
                i = originalValue
            };

            var retVal = NativeExportsNE.DoubleIntRef(wrapper);

            Assert.Equal(originalValue * 2, wrapper.i);
            Assert.Equal(originalValue * 2, retVal.i);
        }
Ejemplo n.º 6
0
        public void NonBlittableStructWithFree()
        {
            var stringContainer = new StringContainer
            {
                str1 = "Foo",
                str2 = "Bar"
            };

            NativeExportsNE.DeepDuplicateStrings(stringContainer, out var stringContainer2);

            Assert.Equal(stringContainer, stringContainer2);
        }
Ejemplo n.º 7
0
        public void NonBlittableStructWithoutAllocation()
        {
            var boolStruct = new BoolStruct
            {
                b1 = true,
                b2 = false,
                b3 = true
            };

            NativeExportsNE.NegateBools(boolStruct, out BoolStruct boolStructNegated);

            Assert.Equal(!boolStruct.b1, boolStructNegated.b1);
            Assert.Equal(!boolStruct.b2, boolStructNegated.b2);
            Assert.Equal(!boolStruct.b3, boolStructNegated.b3);
        }
Ejemplo n.º 8
0
        public void ValidateIntFields()
        {
            const int A = 24, B = 37, C = 59;
            var       initial = new IntFields()
            {
                a = A,
                b = B,
                c = C,
            };
            var expected = new IntFields()
            {
                a = initial.a * 2,
                b = initial.b * 2,
                c = initial.c * 2,
            };

            var input = initial;
            {
                var result = NativeExportsNE.DoubleIntFields(input);
                Assert.Equal(initial, input);
                Assert.Equal(expected, result);
            }
            {
                var result = new IntFields();
                NativeExportsNE.DoubleIntFieldsRefReturn(input, ref result);
                Assert.Equal(initial, input);
                Assert.Equal(expected, result);
            }

            {
                IntFields result;
                NativeExportsNE.DoubleIntFieldsOutReturn(input, out result);
                Assert.Equal(initial, input);
                Assert.Equal(expected, result);
            }

            {
                input = initial;
                NativeExportsNE.DoubleIntFieldsByRef(ref input);
                Assert.Equal(expected, input);
            }

            {
                input = initial;
                NativeExportsNE.DoubleIntFieldsByRefIn(in input);
                Assert.Equal(expected, input); // Updated even when passed with in keyword (matches built-in system)
            }
        }
Ejemplo n.º 9
0
        public void ValidateBoolIsMarshalledAsExpected()
        {
            Assert.Equal((uint)1, NativeExportsNE.ReturnByteBoolAsUInt(true));

            Assert.Equal((uint)0, NativeExportsNE.ReturnByteBoolAsUInt(false));
            Assert.Equal((uint)1, NativeExportsNE.ReturnSByteBoolAsUInt(true));
            Assert.Equal((uint)0, NativeExportsNE.ReturnSByteBoolAsUInt(false));
            Assert.Equal(VARIANT_TRUE, NativeExportsNE.ReturnVariantBoolAsUInt(true));
            Assert.Equal(VARIANT_FALSE, NativeExportsNE.ReturnVariantBoolAsUInt(false));
            Assert.Equal((uint)1, NativeExportsNE.ReturnIntBoolAsUInt(true));
            Assert.Equal((uint)0, NativeExportsNE.ReturnIntBoolAsUInt(false));
            Assert.Equal((uint)1, NativeExportsNE.ReturnUIntBoolAsUInt(true));
            Assert.Equal((uint)0, NativeExportsNE.ReturnUIntBoolAsUInt(false));
            Assert.Equal((uint)1, NativeExportsNE.ReturnWinBoolAsUInt(true));
            Assert.Equal((uint)0, NativeExportsNE.ReturnWinBoolAsUInt(false));
        }
Ejemplo n.º 10
0
        public void ValidateDefaultBoolReturns(uint value, bool expected)
        {
            Assert.Equal(expected, NativeExportsNE.ReturnUIntAsDefaultBool(value));

            bool result = !expected;

            NativeExportsNE.ReturnUIntAsDefaultBool_Ref(value, ref result);
            Assert.Equal(expected, result);

            result = !expected;
            NativeExportsNE.ReturnUIntAsDefaultBool_Out(value, out result);
            Assert.Equal(expected, result);

            result = !expected;
            NativeExportsNE.ReturnUIntAsDefaultBool_In(value, in result);
            Assert.Equal(!expected, result); // Should not be updated when using 'in'
        }
Ejemplo n.º 11
0
        public void ValidateUnicodeReturns(char expected, uint value)
        {
            Assert.Equal(expected, NativeExportsNE.ReturnUIntAsUnicode(value));

            char initial = '\u0000';
            char result  = initial;

            NativeExportsNE.ReturnUIntAsUnicode_Ref(value, ref result);
            Assert.Equal(expected, result);

            result = initial;
            NativeExportsNE.ReturnUIntAsUnicode_Out(value, out result);
            Assert.Equal(expected, result);

            result = initial;
            NativeExportsNE.ReturnUIntAsUnicode_In(value, in result);
            Assert.Equal(expected, result); // Value is updated even when passed with 'in' keyword (matches built-in system)
        }
Ejemplo n.º 12
0
        public void NonBlittableStructRef()
        {
            var stringContainer = new StringContainer
            {
                str1 = "Foo",
                str2 = "Bar"
            };

            var expected = new StringContainer
            {
                str1 = ReverseUTF8Bytes(stringContainer.str1),
                str2 = ReverseUTF8Bytes(stringContainer.str2)
            };

            var stringContainerCopy = stringContainer;

            NativeExportsNE.ReverseStrings(ref stringContainerCopy);

            Assert.Equal(expected, stringContainerCopy);
        }
Ejemplo n.º 13
0
 public void ValidateUnicodeCharIsMarshalledAsExpected(char value, uint expected)
 {
     Assert.Equal(expected, NativeExportsNE.ReturnUnicodeAsUInt(value));
 }
Ejemplo n.º 14
0
        public unsafe void ValidatePointerFields()
        {
            int  iInitial = 31;
            bool bInitial = false;
            char cInitial = 'A';

            int  iExpected = iInitial + 1;
            bool bExpected = !bInitial;
            char cExpected = (char)(cInitial + 1);

            int  i       = iInitial;
            bool b       = bInitial;
            char c       = cInitial;
            var  initial = new PointerFields()
            {
                i = &i,
                b = &b,
                c = &c,
            };

            PointerFields input = initial;

            {
                int  iResult;
                bool bResult;
                char cResult;
                var  result = new PointerFields()
                {
                    i = &iResult,
                    b = &bResult,
                    c = &cResult
                };
                NativeExportsNE.IncrementInvertPointerFieldsRefReturn(input, ref result);
                Assert.Equal(initial, input);
                ValidateFieldValues(result);
            }

            {
                ResetFieldValues(input);
                NativeExportsNE.IncrementInvertPointerFieldsByRef(ref input);
                Assert.Equal(initial, input);
                ValidateFieldValues(input);
            }

            {
                ResetFieldValues(input);
                NativeExportsNE.IncrementInvertPointerFieldsByRefIn(in input);
                Assert.Equal(initial, input);
                ValidateFieldValues(input);
            }

            void ResetFieldValues(PointerFields input)
            {
                *(input.i) = iInitial;
                *(input.b) = bInitial;
                *(input.c) = cInitial;
            }

            void ValidateFieldValues(PointerFields result)
            {
                Assert.Equal(iExpected, *result.i);
                Assert.Equal(bExpected, *result.b);
                Assert.Equal(cExpected, *result.c);
            }
        }
Ejemplo n.º 15
0
        public void MarshalUsing()
        {
            double d = 1234.56789;

            Assert.Equal(d, NativeExportsNE.GetLongBytesAsDouble(d));
        }
Ejemplo n.º 16
0
        public void NonBlittableStructStackallocPinnableNativeMarshalling()
        {
            string str = "Hello world!";

            Assert.Equal(str.Length, NativeExportsNE.ReturnStringLength(str));
        }