public static void TestThrift() { TestStruct1 test1Message = new TestStruct1(); test1Message.Id = 1487; test1Message.UserName = "******"; byte[] buffer = new byte[1024]; Serialize(test1Message, ref buffer); TestStruct1 outMessage1 = new TestStruct1(); Deserialize(buffer, outMessage1); Debug.Log(outMessage1.ToString()); // refactor: use the assert of the 'unit test' }
public void TestStruct() { var s1 = new TestStruct1(); Assert.AreEqual( BuildResult( "!YamlSharpTest.YamlRepresenterTest%2BTestStruct1", "c: null", "b: 0", "a: 0" ), YamlSerializer.Serialize(s1) ); s1.a = 2; s1.b = 1.2; s1.c = "1"; Assert.AreEqual( BuildResult( "!YamlSharpTest.YamlRepresenterTest%2BTestStruct1", "c: \"1\"", "b: 1.2", "a: 2" ), YamlSerializer.Serialize(s1) ); s1.e = 1; Assert.AreEqual( BuildResult( "!YamlSharpTest.YamlRepresenterTest%2BTestStruct1", "e: 1", "c: \"1\"", "b: 1.2", "a: 2" ), YamlSerializer.Serialize(s1) ); }
public void Deserialize_FromOracleData_WithTestStruct2() { const int intProp = 42; var testStruct1 = new TestStruct1() { IntProp = 100, ByteProp = 50, ShortProp = 200 }; var oracle = new SerializationTestOracle(); var oracleResult = oracle.GenerateTestStruct2(intProp, testStruct1); var serializer = new FlatBuffersSerializer(); var o = serializer.Deserialize <TestStruct2>(oracleResult, 0, oracleResult.Length); Assert.AreEqual(intProp, o.IntProp); Assert.AreEqual(testStruct1.IntProp, o.TestStructProp.IntProp); Assert.AreEqual(testStruct1.ByteProp, o.TestStructProp.ByteProp); Assert.AreEqual(testStruct1.ShortProp, o.TestStructProp.ShortProp); }
public void Serialize_WithTestStruct1_CanBeReadByOracle() { const int intProp = 42; const byte byteProp = 22; const short shortProp = 62; var serializer = new FlatBuffersSerializer(); var obj = new TestStruct1() { IntProp = intProp, ByteProp = byteProp, ShortProp = shortProp }; var buffer = new byte[32]; serializer.Serialize(obj, buffer, 0, buffer.Length); var oracle = new SerializationTestOracle(); var oracleResult = oracle.ReadTestStruct1(buffer); Assert.AreEqual(obj.IntProp, oracleResult.IntProp); Assert.AreEqual(obj.ByteProp, oracleResult.ByteProp); Assert.AreEqual(obj.ShortProp, oracleResult.ShortProp); }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest3: Equals should return false when comparing with instance of different classes"); try { TestEmptyClass classInstance1 = new TestEmptyClass(); TestClass classInstance2 = new TestClass(); RuntimeTypeHandle classInstanceHandle1 = classInstance1.GetType().TypeHandle; RuntimeTypeHandle classInstanceHandle2 = classInstance2.GetType().TypeHandle; if (classInstanceHandle1.Equals(classInstanceHandle2)) { TestLibrary.TestFramework.LogError("003.1", "Equals returns true when comparing with instance of different classe"); retVal = false; } TestStruct1 ts1 = new TestStruct1(); TestStruct2 ts2 = new TestStruct2(); RuntimeTypeHandle structHandle1 = ts1.GetType().TypeHandle; RuntimeTypeHandle structHandle2 = ts2.GetType().TypeHandle; if (structHandle1.Equals(structHandle2)) { TestLibrary.TestFramework.LogError("003.2", "Equals returns false when comparing with instance of different structs"); retVal = false; } TestEnum1 te1 = TestEnum1.DEFAULT; TestEnum2 te2 = TestEnum2.DEFAULT; RuntimeTypeHandle enumHandle1 = te1.GetType().TypeHandle; RuntimeTypeHandle enumHandle2 = te2.GetType().TypeHandle; if (enumHandle1.Equals(enumHandle2)) { TestLibrary.TestFramework.LogError("003.3", "Equals returns false when comparing with instance of different enums"); retVal = false; } if (classInstanceHandle1.Equals(structHandle1)) { TestLibrary.TestFramework.LogError("003.4", "Equals returns false when comparing a instance of struct with a instance of class"); retVal = false; } if (classInstanceHandle1.Equals(enumHandle1)) { TestLibrary.TestFramework.LogError("003.5", "Equals returns false when comparing a instance of enum with a instance of class"); retVal = false; } if (structHandle1.Equals(enumHandle1)) { TestLibrary.TestFramework.LogError("003.6", "Equals returns false when comparing a instance of struct with a instance of enum"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("003.7", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2: Equals should return true when comparing with instance of same classes"); try { TestEmptyClass ec1 = new TestEmptyClass(); TestEmptyClass ec2 = new TestEmptyClass(); RuntimeTypeHandle handle1 = ec1.GetType().TypeHandle; RuntimeTypeHandle handle2 = ec2.GetType().TypeHandle; if (!handle1.Equals(handle2)) { TestLibrary.TestFramework.LogError("002.1", "Equals returns false when comparing with instance of same classe"); retVal = false; } TestStruct1 ts1 = new TestStruct1(); TestStruct1 ts2 = new TestStruct1(); handle1 = ts1.GetType().TypeHandle; handle2 = ts2.GetType().TypeHandle; if (!handle1.Equals(handle2)) { TestLibrary.TestFramework.LogError("002.2", "Equals returns false when comparing with instance of same structs"); retVal = false; } TestEnum1 te1 = TestEnum1.DEFAULT; TestEnum1 te2 = TestEnum1.DEFAULT; handle1 = te1.GetType().TypeHandle; handle2 = te2.GetType().TypeHandle; if (!handle1.Equals(handle2)) { TestLibrary.TestFramework.LogError("002.3", "Equals returns false when comparing with instance of same enums"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002.4", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return retVal; }
public unsafe static int Main() { /// /// Testing simple struct size /// if (Marshal.SizeOf(typeof(TestStruct1)) != 32) { return(1); } TestStruct1 myStruct = new TestStruct1(); myStruct.a = 0x12345678; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1))); Marshal.StructureToPtr(myStruct, p, false); Type testType = typeof(TestStruct1); if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a) { return(2); } Marshal.FreeHGlobal(p); /// /// Testing struct size with ByValTStr string /// if (Marshal.SizeOf(typeof(TestStruct2)) != 32) { return(3); } TestStruct2 myStruct2 = new TestStruct2(); myStruct2.b = 0x12345678; p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2))); Marshal.StructureToPtr(myStruct2, p, false); Type testType2 = typeof(TestStruct2); if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b) { return(4); } Marshal.FreeHGlobal(p); /// /// Test structure size and struct with inheritance /// if (Marshal.SizeOf(typeof(TestStruct3)) != 64) { return(5); } TestStruct3 myStruct3 = new TestStruct3(); myStruct3.b = 0x12345678; myStruct3.c = 0x76543210; p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3))); Marshal.StructureToPtr(myStruct3, p, false); Type testType3 = typeof(TestStruct3); if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b) { return(6); } if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c) { return(7); } Marshal.FreeHGlobal(p); /// /// Also make sure OffsetOf returns the correct Exception. /// try { Marshal.OffsetOf(testType3, "blah"); return(8); } catch (ArgumentException e) { /// Way to go :) } catch (Exception e) { return(9); } // test size of structs with objects if (Marshal.SizeOf(typeof(TestStruct4)) != IntPtr.Size) { return(10); } if (Marshal.SizeOf(typeof(TestStruct5)) != IntPtr.Size) { return(11); } if (Marshal.SizeOf(typeof(TestStruct6)) != IntPtr.Size) { return(12); } // a VARIANT is if (Marshal.SizeOf(typeof(TestStruct7)) != 16) { return(13); } if (IsOSX() && IntPtr.Size == 4) { if (Marshal.SizeOf(typeof(TestStruct8)) != 12) { return(14); } if (Marshal.SizeOf(typeof(TestStruct10)) != 12) { return(16); } } else { if (Marshal.SizeOf(typeof(TestStruct8)) != 16 && Marshal.SizeOf(typeof(TestStruct8)) != 12) { return(14); } if (Marshal.SizeOf(typeof(TestStruct10)) != 16 && Marshal.SizeOf(typeof(TestStruct10)) != 12) { return(16); } } if (Marshal.SizeOf(typeof(TestStruct9)) != 12) { return(15); } if (Marshal.SizeOf(typeof(TestStruct11)) != 11) { return(17); } if (Marshal.SizeOf(typeof(TestStruct12)) != 6) { return(18); } if (Marshal.SizeOf(typeof(TestStruct13)) != 12) { return(19); } if (Marshal.SizeOf(typeof(TestStruct14)) != 12) { return(20); } return(0); }
static void reg_struct(TestStruct1 regStruct) { regStruct.a = 1; }
public unsafe static int Main () { /// /// Testing simple struct size /// if(Marshal.SizeOf(typeof(TestStruct1)) != 32) { return 1; } TestStruct1 myStruct = new TestStruct1(); myStruct.a = 0x12345678; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct1))); Marshal.StructureToPtr(myStruct, p, false); Type testType = typeof(TestStruct1); if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType, "a")) != myStruct.a) return 2; Marshal.FreeHGlobal(p); /// /// Testing struct size with ByValTStr string /// if(Marshal.SizeOf(typeof(TestStruct2)) != 32) return 3; TestStruct2 myStruct2 = new TestStruct2(); myStruct2.b = 0x12345678; p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct2))); Marshal.StructureToPtr(myStruct2, p, false); Type testType2 = typeof(TestStruct2); if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType2, "b")) != myStruct2.b) return 4; Marshal.FreeHGlobal(p); /// /// Test structure size and struct with inheritance /// if(Marshal.SizeOf(typeof(TestStruct3)) != 64) return 5; TestStruct3 myStruct3 = new TestStruct3(); myStruct3.b = 0x12345678; myStruct3.c = 0x76543210; p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(TestStruct3))); Marshal.StructureToPtr(myStruct3, p, false); Type testType3 = typeof(TestStruct3); if(Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "b")) != myStruct3.b) return 6; if (Marshal.ReadInt32(p, (int)Marshal.OffsetOf(testType3, "c")) != myStruct3.c) return 7; Marshal.FreeHGlobal(p); /// /// Also make sure OffsetOf returns the correct Exception. /// try { Marshal.OffsetOf(testType3, "blah"); return 8; } catch(ArgumentException e) { /// Way to go :) } catch(Exception e) { return 9; } // test size of structs with objects if (Marshal.SizeOf (typeof (TestStruct4)) != IntPtr.Size) return 10; if (Marshal.SizeOf (typeof (TestStruct5)) != IntPtr.Size) return 11; if (Marshal.SizeOf (typeof (TestStruct6)) != IntPtr.Size) return 12; // a VARIANT is if (Marshal.SizeOf (typeof (TestStruct7)) != 16) return 13; if (IsOSX () && IntPtr.Size == 4) { if (Marshal.SizeOf (typeof (TestStruct8)) != 12) return 14; if (Marshal.SizeOf (typeof (TestStruct10)) != 12) return 16; } else { if (Marshal.SizeOf (typeof (TestStruct8)) != 16) return 14; if (Marshal.SizeOf (typeof (TestStruct10)) != 16) return 16; } if (Marshal.SizeOf (typeof (TestStruct9)) != 12) return 15; if (Marshal.SizeOf (typeof (TestStruct11)) != 11) return 17; if (Marshal.SizeOf (typeof (TestStruct12)) != 6) return 18; if (Marshal.SizeOf (typeof (TestStruct13)) != 12) return 19; if (Marshal.SizeOf (typeof (TestStruct14)) != 12) return 20; return 0; }
public void testEnumInStruct1() { var tmp = new TestStruct1(MyFlagsEnum.V2); }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest3: Equals should return false when comparing with instance of different classes"); try { TestEmptyClass classInstance1 = new TestEmptyClass(); TestClass classInstance2 = new TestClass(); RuntimeTypeHandle classInstanceHandle1 = classInstance1.GetType().TypeHandle; RuntimeTypeHandle classInstanceHandle2 = classInstance2.GetType().TypeHandle; if (classInstanceHandle1.Equals(classInstanceHandle2)) { TestLibrary.TestFramework.LogError("003.1", "Equals returns true when comparing with instance of different classe"); retVal = false; } TestStruct1 ts1 = new TestStruct1(); TestStruct2 ts2 = new TestStruct2(); RuntimeTypeHandle structHandle1 = ts1.GetType().TypeHandle; RuntimeTypeHandle structHandle2 = ts2.GetType().TypeHandle; if (structHandle1.Equals(structHandle2)) { TestLibrary.TestFramework.LogError("003.2", "Equals returns false when comparing with instance of different structs"); retVal = false; } TestEnum1 te1 = TestEnum1.DEFAULT; TestEnum2 te2 = TestEnum2.DEFAULT; RuntimeTypeHandle enumHandle1 = te1.GetType().TypeHandle; RuntimeTypeHandle enumHandle2 = te2.GetType().TypeHandle; if (enumHandle1.Equals(enumHandle2)) { TestLibrary.TestFramework.LogError("003.3", "Equals returns false when comparing with instance of different enums"); retVal = false; } if (classInstanceHandle1.Equals(structHandle1)) { TestLibrary.TestFramework.LogError("003.4", "Equals returns false when comparing a instance of struct with a instance of class"); retVal = false; } if (classInstanceHandle1.Equals(enumHandle1)) { TestLibrary.TestFramework.LogError("003.5", "Equals returns false when comparing a instance of enum with a instance of class"); retVal = false; } if (structHandle1.Equals(enumHandle1)) { TestLibrary.TestFramework.LogError("003.6", "Equals returns false when comparing a instance of struct with a instance of enum"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("003.7", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public void StructWithNullReferenceToSealedType() { var x = new TestStruct1(); RoundTrip(x); }
public void run(string projectDir, bool openGlTestEnabled) { try { // Create a C++ object and invoke some functions. { NTestClass obj = NTestClass.create("test1234ۆ"); checkEqual(obj.isNull(), false, "isNull() returned wrong value"); checkEqual(obj.getName(), "test1234ۆ", "Wrong name returned by getter"); checkEqual(obj.concatenateStrings("abcۆ", "defۆ"), "abcۆdefۆ", "Strings not concatenated correctly"); checkEqual(obj.concatenateStringsUtf16("ghiۆ", "jklۆ"), "ghiۆjklۆ", "UTF-16 strings not concatenated correctly"); // An exception can be propagated from C++ to C#. try { obj.throwException(); } catch (Exception e) { if (!e.Message.Contains("test_exception")) { throw new Exception("Got wrong exception from C++ function", e); } } // Vector math. checkEqual(obj.addFloatVectors(new Vector4(1, 2, 3, 4), new Vector4(5, 6, 7, 8)), new Vector4(6, 8, 10, 12), "Float vectors summed incorrectly"); checkEqual(obj.addFloatVectorsNoexcept(new Vector4(2, 3, 4, 5), new Vector4(6, 7, 8, 9)), new Vector4(8, 10, 12, 14), "Float vectors summed incorrectly (noexcept version)"); checkEqual(obj.getColor(new Vector2(1, 2)), new Vector4(2, 4, 4, 6), "getColor() returned wrong value"); // Destroy the object. obj.release(); checkEqual(NGlobal.partition2Test(), 22, "Partition 2 test function returned wrong value"); } // Subclasses. { NDerivedClass obj = NGlobal.createDerivedClassInstance(); checkEqual(obj.test1(), "derived1", "Overridden function returned wrong value"); checkEqual(obj.test3(), "base3", "Non-overridden function returned wrong value"); checkEqual(typeof(NDerivedClass).GetMethod("test2"), null, "test2() function should not be accessible because the base class was derived as private"); // A derived-class pointer can be converted to base-class pointer. NBaseClass1 basePtr = obj; checkEqual(basePtr.test1(), "derived1", "Overridden function returned wrong value when invoked with a base class pointer"); checkEqual(typeof(NBaseClass1).GetMethod("test3"), null, "Base class should not have functions that only exist in other base classes of the derived class"); obj.release(); } // Create a base class instance directly. { NBaseClass1 obj = NGlobal.createBaseClass1Instance(); checkEqual(obj.test1(), "base1", "Function from base class instance returned wrong value"); obj.release(); } // Create a struct and access its fields by a pointer from both C++ and C#. { TestStruct1 s = new TestStruct1(); string name = nameof(TestStruct1); checkEqual(s.i1, 0, $"{name} field i1 has wrong value before initialization"); NGlobal.setStruct1Values(&s); checkEqual(s.i1, 100, $"{name} field i1 has wrong value"); checkEqual(s.v1, new Vector4(1, 2, 3, 4), $"{name} field v1 has wrong value"); checkEqual(s.array1[1], 200, $"{name} field array1[1] has wrong value"); } { TestStruct2 s = new TestStruct2(); string name = nameof(TestStruct2); checkEqual(s.i, 0, $"{name} field i has wrong value before initialization"); NGlobal.setStruct2Values(&s); checkEqual(s.i, 100, $"{name} field s.i has wrong value"); } { CustomSharedStruct s = new CustomSharedStruct(); string name = nameof(CustomSharedStruct); checkEqual(s.i1, 0, $"{name} field i1 has wrong value before initialization"); NGlobal.setCustomSharedStructValues(&s); checkEqual(s.i1, 700, $"{name} field i1 has wrong value"); checkEqual(s.v1, new Vector4(10, 20, 30, 40), "Struct field v1 has wrong value"); } // Callback functions { NCallbackTest cbTest = NGlobal.createCallbackTestInstance(); checkEqual(cbTest.invokeGivenCallback("string5ۆ", "string6ۆ", (s1, s2) => s1 + s2), "string5ۆstring6ۆ", "C++ -> C# callback (given as parameter) should have concatenated strings"); checkEqual(cbTest.invokeGivenCallbackUtf16("string7ۆ", "string8ۆ", (s1, s2) => s1 + s2), "string7ۆstring8ۆ", "C++ -> C# callback (given as parameter) should have concatenated UTF-16 strings"); cbTest.setCallback((s1, s2) => s1 + s2); checkEqual(cbTest.invokeStoredCallback("string1ۆ", "string2ۆ"), "string1ۆstring2ۆ", "C++ -> C# callback (stored) should have concatenated strings"); } // Namespaces. { CsNamespace.CppOuterNamespace.NTestClass2 obj = NGlobal.createTestClass2Instance("test5678", 123); checkEqual(obj.getName(), "test5678", "Wrong name returned by getter"); checkEqual(obj.getIndex(), 123, "Wrong index returned by getter"); obj.release(); checkEqual(CsNamespace.CppOuterNamespace.NGlobal.calculateSum(1, CsNamespace.CppOuterNamespace.EnumInsideNamespace.TEST2), 3, "Wrong result from calculateSum()"); } { CsNamespace.CppOuterNamespace.CppInnerNamespace.NTestClass3 obj = NGlobal.createTestClass3Instance("test_abc"); checkEqual(obj.getName(), "test_abc", "Wrong name returned by getter"); obj.release(); CsNamespace.CppOuterNamespace.CppInnerNamespace.StructInsideNamespace s = new CsNamespace.CppOuterNamespace.CppInnerNamespace.StructInsideNamespace(); s.v = 7; checkEqual(CsNamespace.CppOuterNamespace.CppInnerNamespace.NGlobal.calculateProduct(5, s), 35, "Wrong result from calculateProduct()"); } // File set. { NGlobal.handleBicycle(new IncludedBicycleStruct()); NGlobal.handleVehicle(new IncludedVehicleStruct()); checkEqual(Type.GetType("CsNamespace.IncludedBicycleStruct") == null, false, "Struct from included file should be available"); checkEqual(Type.GetType("CsNamespace.ExcludedCarStruct") == null, true, "Struct from excluded file not exist"); checkEqual(Type.GetType("CsNamespace.ExcludedFileStruct") == null, true, "Struct from excluded file not exist"); } // Enum reflection in C++. { string result = NGlobal.testEnumReflection(); if (result.Length > 0) { throw new Exception($"Error in enum reflection test: {result}"); } } // OpenGL. if (openGlTestEnabled) { NGlobal.testOpenGl(projectDir); } else { Log.write("Skipping OpenGL test", null, ConsoleColor.Yellow); } Log.write("*** FUNCTIONAL TEST SUCCESS ***", null, ConsoleColor.Green); } catch (Exception e) { Log.write("*** FUNCTIONAL TEST FAILED ***", e, ConsoleColor.Red); } }
public void TestStruct() { var s1 = new TestStruct1(); Assert.AreEqual( BuildResult( "!YamlSerializerTest.YamlRepresenterTest%2BTestStruct1", "a: 0", "b: 0", "c: null" ), YamlSerializer.Serialize(s1) ); s1.a = 2; s1.b = 1.2; s1.c = "1"; Assert.AreEqual( BuildResult( "!YamlSerializerTest.YamlRepresenterTest%2BTestStruct1", "a: 2", "b: 1.2", "c: \"1\"" ), YamlSerializer.Serialize(s1) ); s1.e = 1; Assert.AreEqual( BuildResult( "!YamlSerializerTest.YamlRepresenterTest%2BTestStruct1", "a: 2", "b: 1.2", "c: \"1\"", "e: 1" ), YamlSerializer.Serialize(s1) ); }