예제 #1
0
파일: PInvoke.cs 프로젝트: ezverev/corert
        private static void TestStruct()
        {
            Console.WriteLine("Testing Structs");
            SequentialStruct ss = new SequentialStruct();

            ss.f0 = 100;
            ss.f1 = 1;
            ss.f2 = 10.0f;
            ss.f3 = "Hello";

            ThrowIfNotEquals(true, StructTest(ss), "Struct marshalling scenario1 failed.");

            StructTest_ByRef(ref ss);
            ThrowIfNotEquals(true, ss.f1 == 2 && ss.f2 == 11.0 && ss.f3.Equals("Ifmmp"), "Struct marshalling scenario2 failed.");

            SequentialStruct ss2 = new SequentialStruct();

            StructTest_ByOut(out ss2);
            ThrowIfNotEquals(true, ss2.f0 == 1 && ss2.f1 == 1.0 && ss2.f2 == 1.0 && ss2.f3.Equals("0123456"), "Struct marshalling scenario3 failed.");

            ExplicitStruct es = new ExplicitStruct();

            es.f1 = 100;
            es.f2 = 100.0f;
            es.f3 = "Hello";
            ThrowIfNotEquals(true, StructTest_Explicit(es), "Struct marshalling scenario4 failed.");

            NestedStruct ns = new NestedStruct();

            ns.f1 = 100;
            ns.f2 = es;
            ThrowIfNotEquals(true, StructTest_Nested(ns), "Struct marshalling scenario5 failed.");

// RhpThrowEx is not implemented in CPPCodeGen
#if !CODEGEN_CPP
            bool       pass       = false;
            AutoStruct autoStruct = new AutoStruct();
            try
            {
                // passing struct with Auto layout should throw exception.
                StructTest_Auto(autoStruct);
            }
            catch (Exception)
            {
                pass = true;
            }
            ThrowIfNotEquals(true, pass, "Struct marshalling scenario6 failed.");
#endif
        }
예제 #2
0
        public void TestStructMember()
        {
            MockRepo     repo = new MockRepo();
            NestedStruct obj  = new NestedStruct {
                nested = new NestedStruct.Nested {
                    Value = 0
                }
            };

            repo.AddDef("a", obj);
            ModDefinition mod = new ModDefinition {
                guid = "a", field = "nested.Value", value = 10
            };

            new Mod("NestedStruct", mod, repo).Apply();
            Assert.AreEqual(10, obj.nested.Value);
        }
예제 #3
0
        public void TestStructInStruct()
        {
            MockRepo     repo = new MockRepo();
            NestedStruct obj  = new NestedStruct {
                nested = new NestedStruct.Nested {
                    further = new NestedStruct.Nested.Further {
                        Value2 = 7
                    }
                }
            };

            repo.AddDef("a", obj);
            ModDefinition mod = new ModDefinition {
                guid = "a", field = "nested.further.Value2", value = 10
            };

            new Mod("StructInStruct", mod, repo).Apply();
            Assert.AreEqual(10, obj.nested.further.Value2);
        }
        protected override object CreateArrays()
        {
            var itemArrays = new ItemArrays();
            itemArrays._nestedStructArray = new NestedStruct[data.Length];
            Array.Copy(data, 0, itemArrays._nestedStructArray, 0, data.Length);

            itemArrays._untypedArray = new object[data.Length + 1];
            Array.Copy(data, 0, itemArrays._untypedArray, 0, data.Length);

            var nestedStructArray = new NestedStruct[data.Length];
            Array.Copy(data, 0, nestedStructArray, 0, data.Length);
            itemArrays._arrayInObject = nestedStructArray;
            itemArrays._nullableNestedStructArray = new NestedStruct?[data.Length + 1];
            for (var i = 0; i < data.Length; i++)
            {
                itemArrays._nullableNestedStructArray[i] = data[i];
            }
            return itemArrays;
        }
        protected override object CreateArrays()
        {
            ItemArrays itemArrays = new ItemArrays();

            itemArrays._nestedStructArray = new NestedStruct[data.Length];
            System.Array.Copy(data, 0, itemArrays._nestedStructArray, 0, data.Length);

            itemArrays._untypedArray = new object[data.Length + 1];
            System.Array.Copy(data, 0, itemArrays._untypedArray, 0, data.Length);

            NestedStruct[] nestedStructArray = new NestedStruct[data.Length];
            System.Array.Copy(data, 0, nestedStructArray, 0, data.Length);
            itemArrays._arrayInObject             = nestedStructArray;
            itemArrays._nullableNestedStructArray = new NestedStruct?[data.Length + 1];
            for (int i = 0; i < data.Length; i++)
            {
                itemArrays._nullableNestedStructArray[i] = data[i];
            }
            return(itemArrays);
        }
예제 #6
0
        public void TestStructList()
        {
            MockRepo     repo = new MockRepo();
            NestedStruct obj  = new NestedStruct {
                nestedList = new List <NestedStruct.Nested> {
                    new NestedStruct.Nested {
                        Value = 7
                    }, new NestedStruct.Nested {
                        Value = 8
                    }
                }
            };

            repo.AddDef("a", obj);
            ModDefinition mod = new ModDefinition {
                guid = "a", field = "nestedList[1].Value", value = 10
            };

            new Mod("NestedStructList", mod, repo).Apply();
            Assert.AreEqual(7, obj.nestedList[0].Value);
            Assert.AreEqual(10, obj.nestedList[1].Value);
        }
예제 #7
0
 public SimpleClassWithNestedStruct(double structFirstValue, string structSecondValue, double innerStructFirstValue, string innerStructSecondValue)
 {
     Struct = new NestedStruct(structFirstValue, structSecondValue, innerStructFirstValue, innerStructSecondValue);
 }
예제 #8
0
 public static bool Compare(NestedStruct?val, NestedStruct val1)
 {
     return(Compare(val.Value, val1));
 }
예제 #9
0
 public static bool Compare(NestedStruct val, NestedStruct val1)
 {
     return(val.Equals(val1));
 }
예제 #10
0
 public static NestedStruct Create(NestedStruct val)
 {
     NestedStruct ns = new NestedStruct(); return(ns);
 }
예제 #11
0
        private static void TestStruct()
        {
            Console.WriteLine("Testing Structs");
            SequentialStruct ss = new SequentialStruct();

            ss.f0 = 100;
            ss.f1 = 1;
            ss.f2 = 10.0f;
            ss.f3 = "Hello";

            ThrowIfNotEquals(true, StructTest(ss), "Struct marshalling scenario1 failed.");

            StructTest_ByRef(ref ss);
            ThrowIfNotEquals(true, ss.f1 == 2 && ss.f2 == 11.0 && ss.f3.Equals("Ifmmp"), "Struct marshalling scenario2 failed.");

            SequentialStruct ss2 = new SequentialStruct();

            StructTest_ByOut(out ss2);
            ThrowIfNotEquals(true, ss2.f0 == 1 && ss2.f1 == 1.0 && ss2.f2 == 1.0 && ss2.f3.Equals("0123456"), "Struct marshalling scenario3 failed.");

            NesterOfSequentialStruct.SequentialStruct ss3 = new NesterOfSequentialStruct.SequentialStruct();
            ss3.f1 = 10.0f;
            ss3.f2 = 123;

            ThrowIfNotEquals(true, StructTest_Sequential2(ss3), "Struct marshalling scenario1 failed.");

            ExplicitStruct es = new ExplicitStruct();

            es.f1 = 100;
            es.f2 = 100.0f;
            es.f3 = "Hello";
            ThrowIfNotEquals(true, StructTest_Explicit(es), "Struct marshalling scenario4 failed.");

            NestedStruct ns = new NestedStruct();

            ns.f1 = 100;
            ns.f2 = es;
            ThrowIfNotEquals(true, StructTest_Nested(ns), "Struct marshalling scenario5 failed.");

            SequentialStruct[] ssa = null;
            ThrowIfNotEquals(true, IsNULL(ssa), "Non-blittable array null check failed");

            ssa = new SequentialStruct[3];
            for (int i = 0; i < 3; i++)
            {
                ssa[i].f1 = 0;
                ssa[i].f1 = i;
                ssa[i].f2 = i * i;
                ssa[i].f3 = i.LowLevelToString();
            }
            ThrowIfNotEquals(true, StructTest_Array(ssa, ssa.Length), "Array of struct marshalling failed");

            InlineString ils = new InlineString();

            InlineStringTest(ref ils);
            ThrowIfNotEquals("Hello World!", ils.name, "Inline string marshalling failed");

            InlineArrayStruct ias = new InlineArrayStruct();

            ias.inlineArray = new short[128];

            for (short i = 0; i < 128; i++)
            {
                ias.inlineArray[i] = i;
            }

            ias.inlineString = "Hello";

            InlineUnicodeStruct ius = new InlineUnicodeStruct();

            ius.inlineString = "Hello World";

            ThrowIfNotEquals(true, InlineArrayTest(ref ias, ref ius), "inline array marshalling failed");
            bool pass = true;

            for (short i = 0; i < 128; i++)
            {
                if (ias.inlineArray[i] != i + 1)
                {
                    pass = false;
                }
            }
            ThrowIfNotEquals(true, pass, "inline array marshalling failed");

            ThrowIfNotEquals("Hello World", ias.inlineString, "Inline ByValTStr Ansi marshalling failed");

            ThrowIfNotEquals("Hello World", ius.inlineString, "Inline ByValTStr Unicode marshalling failed");

            pass = false;
            AutoStruct autoStruct = new AutoStruct();

            try
            {
                // passing struct with Auto layout should throw exception.
                StructTest_Auto(autoStruct);
            }
            catch (Exception)
            {
                pass = true;
            }
            ThrowIfNotEquals(true, pass, "Struct marshalling scenario6 failed.");

            Callbacks callbacks = new Callbacks();

            callbacks.callback0 = new Callback0(callbackFunc0);
            callbacks.callback1 = new Callback1(callbackFunc1);
            callbacks.callback2 = new Callback2(callbackFunc2);
            ThrowIfNotEquals(true, RegisterCallbacks(ref callbacks), "Scenario 7: Struct with delegate marshalling failed");
        }
예제 #12
0
 static extern bool StructTest_Nested(NestedStruct ns);
예제 #13
0
 public static bool Compare(NestedStruct?val, NestedStruct val1)
 {
     return(val == null ? false : Compare(val.Value, val1));
 }
 private void AssertAreEqual(NestedStruct expected, NestedStruct actual)
 {
     Assert.AreEqual(expected, actual);
 }
예제 #15
0
 public SerializeMe()
 {
     m_Struct = new NestedStruct();
     m_Name   = "";
 }
 private void AssertAreEqual(NestedStruct expected, NestedStruct actual)
 {
     Assert.AreEqual(expected, actual);
 }
예제 #17
0
        private static void TestStruct()
        {
#if !CODEGEN_CPP
            Console.WriteLine("Testing Structs");
            SequentialStruct ss = new SequentialStruct();
            ss.f0 = 100;
            ss.f1 = 1;
            ss.f2 = 10.0f;
            ss.f3 = "Hello";

            ThrowIfNotEquals(true, StructTest(ss), "Struct marshalling scenario1 failed.");

            StructTest_ByRef(ref ss);
            ThrowIfNotEquals(true, ss.f1 == 2 && ss.f2 == 11.0 && ss.f3.Equals("Ifmmp"), "Struct marshalling scenario2 failed.");

            SequentialStruct ss2 = new SequentialStruct();
            StructTest_ByOut(out ss2);
            ThrowIfNotEquals(true, ss2.f0 == 1 && ss2.f1 == 1.0 && ss2.f2 == 1.0 && ss2.f3.Equals("0123456"), "Struct marshalling scenario3 failed.");

            NesterOfSequentialStruct.SequentialStruct ss3 = new NesterOfSequentialStruct.SequentialStruct();
            ss3.f1 = 10.0f;
            ss3.f2 = 123;

            ThrowIfNotEquals(true, StructTest_Sequential2(ss3), "Struct marshalling scenario1 failed.");

            ExplicitStruct es = new ExplicitStruct();
            es.f1 = 100;
            es.f2 = 100.0f;
            es.f3 = "Hello";
            ThrowIfNotEquals(true, StructTest_Explicit(es), "Struct marshalling scenario4 failed.");

            NestedStruct ns = new NestedStruct();
            ns.f1 = 100;
            ns.f2 = es;
            ThrowIfNotEquals(true, StructTest_Nested(ns), "Struct marshalling scenario5 failed.");

            SequentialStruct[] ssa = null;
            ThrowIfNotEquals(true, IsNULL(ssa), "Non-blittable array null check failed");

            ssa = new SequentialStruct[3];
            for (int i = 0; i < 3; i++)
            {
                ssa[i].f1 = 0;
                ssa[i].f1 = i;
                ssa[i].f2 = i * i;
                ssa[i].f3 = i.LowLevelToString();
            }
            ThrowIfNotEquals(true, StructTest_Array(ssa, ssa.Length), "Array of struct marshalling failed");

            InlineString ils = new InlineString();
            InlineStringTest(ref ils);
            ThrowIfNotEquals("Hello World!", ils.name, "Inline string marshalling failed");

            InlineArrayStruct ias = new InlineArrayStruct();
            ias.inlineArray = new short[128];

            for (short i = 0; i < 128; i++)
            {
                ias.inlineArray[i] = i;
            }

            ias.inlineString = "Hello";

            InlineUnicodeStruct ius = new InlineUnicodeStruct();
            ius.inlineString = "Hello World";


            TestStruct2 ts = new TestStruct2()
            {
                f1 = 100, f2 = true
            };
            int    size   = Marshal.SizeOf <TestStruct2>(ts);
            IntPtr memory = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.StructureToPtr <TestStruct2>(ts, memory, false);
                TestStruct2 ts2 = Marshal.PtrToStructure <TestStruct2>(memory);
                ThrowIfNotEquals(true, ts2.f1 == 100 && ts2.f2 == true, "Struct marshalling Marshal API failed");

                IntPtr offset = Marshal.OffsetOf <TestStruct2>("f2");
                ThrowIfNotEquals(new IntPtr(8), offset, "Struct marshalling OffsetOf failed.");
            }
            finally
            {
                Marshal.FreeHGlobal(memory);
            }

            ThrowIfNotEquals(true, InlineArrayTest(ref ias, ref ius), "inline array marshalling failed");
            bool pass = true;
            for (short i = 0; i < 128; i++)
            {
                if (ias.inlineArray[i] != i + 1)
                {
                    pass = false;
                }
            }
            ThrowIfNotEquals(true, pass, "inline array marshalling failed");

            ThrowIfNotEquals("Hello World", ias.inlineString, "Inline ByValTStr Ansi marshalling failed");

            ThrowIfNotEquals("Hello World", ius.inlineString, "Inline ByValTStr Unicode marshalling failed");

            // RhpThrowEx is not implemented in CPPCodeGen
            pass = false;
            AutoStruct autoStruct = new AutoStruct();
            try
            {
                // passing struct with Auto layout should throw exception.
                StructTest_Auto(autoStruct);
            }
            catch (Exception)
            {
                pass = true;
            }
            ThrowIfNotEquals(true, pass, "Struct marshalling scenario6 failed.");

            Callbacks callbacks = new Callbacks();
            callbacks.callback0 = new Callback0(callbackFunc0);
            callbacks.callback1 = new Callback1(callbackFunc1);
            callbacks.callback2 = new Callback2(callbackFunc2);
            ThrowIfNotEquals(true, RegisterCallbacks(ref callbacks), "Scenario 7: Struct with delegate marshalling failed");
#endif
        }
예제 #18
0
        private static void TestStruct()
        {
            Console.WriteLine("Testing Structs");
            SequentialStruct ss = new SequentialStruct();

            ss.f0 = 100;
            ss.f1 = 1;
            ss.f2 = 10.0f;
            ss.f3 = "Hello";

            ThrowIfNotEquals(true, StructTest(ss), "Struct marshalling scenario1 failed.");

            StructTest_ByRef(ref ss);
            ThrowIfNotEquals(true, ss.f1 == 2 && ss.f2 == 11.0 && ss.f3.Equals("Ifmmp"), "Struct marshalling scenario2 failed.");

            SequentialStruct ss2 = new SequentialStruct();

            StructTest_ByOut(out ss2);
            ThrowIfNotEquals(true, ss2.f0 == 1 && ss2.f1 == 1.0 && ss2.f2 == 1.0 && ss2.f3.Equals("0123456"), "Struct marshalling scenario3 failed.");

            ExplicitStruct es = new ExplicitStruct();

            es.f1 = 100;
            es.f2 = 100.0f;
            es.f3 = "Hello";
            ThrowIfNotEquals(true, StructTest_Explicit(es), "Struct marshalling scenario4 failed.");

            NestedStruct ns = new NestedStruct();

            ns.f1 = 100;
            ns.f2 = es;
            ThrowIfNotEquals(true, StructTest_Nested(ns), "Struct marshalling scenario5 failed.");

            SequentialStruct[] ssa = new SequentialStruct[3];
            for (int i = 0; i < 3; i++)
            {
                ssa[i].f1 = 0;
                ssa[i].f1 = i;
                ssa[i].f2 = i * i;
                ssa[i].f3 = i.ToString();
            }
            ThrowIfNotEquals(true, StructTest_Array(ssa, ssa.Length), "Array of struct marshalling failed");

            InlineArrayStruct ias = new InlineArrayStruct();

            ias.inlineArray = new short[128];

            for (short i = 0; i < 128; i++)
            {
                ias.inlineArray[i] = i;
            }

            ias.inlineString = "Hello";

            InlineUnicodeStruct ius = new InlineUnicodeStruct();

            ius.inlineString = "Hello World";

#if !CODEGEN_CPP
            ThrowIfNotEquals(true, InlineArrayTest(ref ias, ref ius), "inline array marshalling failed");
            bool pass = true;
            for (short i = 0; i < 128; i++)
            {
                if (ias.inlineArray[i] != i + 1)
                {
                    pass = false;
                }
            }
            ThrowIfNotEquals(true, pass, "inline array marshalling failed");

            ThrowIfNotEquals("Hello World", ias.inlineString, "Inline ByValTStr Ansi marshalling failed");

            ThrowIfNotEquals("Hello World", ius.inlineString, "Inline ByValTStr Unicode marshalling failed");

            // RhpThrowEx is not implemented in CPPCodeGen
            pass = false;
            AutoStruct autoStruct = new AutoStruct();
            try
            {
                // passing struct with Auto layout should throw exception.
                StructTest_Auto(autoStruct);
            }
            catch (Exception)
            {
                pass = true;
            }
            ThrowIfNotEquals(true, pass, "Struct marshalling scenario6 failed.");
#endif
        }
예제 #19
0
 public SimpleClassWithNestedStruct(double structFirstValue, string structSecondValue, double innerStructFirstValue, string innerStructSecondValue)
 {
     Struct = new NestedStruct(structFirstValue, structSecondValue, innerStructFirstValue, innerStructSecondValue);
 }