From 04a1591e8404f0dcd6c880fd93a0f04762ea31c9 Mon Sep 17 00:00:00 2001 From: Mikulas Florek Date: Sun, 19 Nov 2017 23:27:20 +0100 Subject: [PATCH] Fixed the generated C# for fixed arrays of Booleans. fixes #1004 * mend Fixed the generated C# for fixed arrays of Booleans --- .../Generators/CSharp/CSharpMarshal.cs | 11 +++++++++-- tests/CSharp/CSharp.Tests.cs | 18 ++++++++++++++++++ tests/CSharp/CSharp.h | 2 ++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpMarshal.cs b/src/Generator/Generators/CSharp/CSharpMarshal.cs index 13dff12b..63f353c3 100644 --- a/src/Generator/Generators/CSharp/CSharpMarshal.cs +++ b/src/Generator/Generators/CSharp/CSharpMarshal.cs @@ -115,7 +115,10 @@ namespace CppSharp.Generators.CSharp } else { - if (arrayType.IsPrimitiveType(PrimitiveType.Char) && + if (arrayType.IsPrimitiveType(PrimitiveType.Bool)) + supportBefore.WriteLineIndent($@"{value}[i] = { + Context.ReturnVarName}[i] != 0;"); + else if (arrayType.IsPrimitiveType(PrimitiveType.Char) && Context.Context.Options.MarshalCharAsManagedChar) supportBefore.WriteLineIndent($@"{value}[i] = global::System.Convert.ToChar({ Context.ReturnVarName}[i]);"); @@ -514,7 +517,11 @@ namespace CppSharp.Generators.CSharp } else { - if (arrayType.IsPrimitiveType(PrimitiveType.Char) && + if (arrayType.IsPrimitiveType(PrimitiveType.Bool)) + supportBefore.WriteLineIndent($@"{ + Context.ReturnVarName}[i] = (byte)({ + Context.ArgName}[i] ? 1 : 0);"); + else if (arrayType.IsPrimitiveType(PrimitiveType.Char) && Context.Context.Options.MarshalCharAsManagedChar) supportBefore.WriteLineIndent($@"{ Context.ReturnVarName}[i] = global::System.Convert.ToSByte({ diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index 4242f695..04eb044d 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -281,6 +281,24 @@ public unsafe class CSharpTests : GeneratorTestFixture } } + [Test] + public void TestBooleanArray() + { + Foo foo = new Foo { A = 10 }; + var new_values = new bool[5]; + for(int i = 0; i < new_values.Length; ++i) + { + new_values[i] = i % 2 == 0; + } + foo.Btest = new_values; + Assert.AreEqual(true, foo.Btest[0]); + Assert.AreEqual(false, foo.Btest[1]); + Assert.AreEqual(true, foo.Btest[2]); + Assert.AreEqual(false, foo.Btest[3]); + Assert.AreEqual(true, foo.Btest[4]); + } + + [Test] public void TestImplicitCtor() { diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 0523b176..37756251 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -32,6 +32,8 @@ public: int operator ++(); int operator --(); + bool btest[5]; + protected: int P; TemplateInAnotherUnit templateInAnotherUnit;