Skip to content

Commit

Permalink
Fixed the generated C# for fixed arrays of Booleans.
Browse files Browse the repository at this point in the history
fixes #1004

* mend

Fixed the generated C# for fixed arrays of Booleans
  • Loading branch information
nem0 authored and ddobrev committed Nov 19, 2017
1 parent 481bbc0 commit 04a1591
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Generator/Generators/CSharp/CSharpMarshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
}
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]);");
Expand Down Expand Up @@ -514,7 +517,11 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
}
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({
Expand Down
18 changes: 18 additions & 0 deletions tests/CSharp/CSharp.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,24 @@ public void TestCopyCtor()
}
}

[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()
{
Expand Down
2 changes: 2 additions & 0 deletions tests/CSharp/CSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class DLL_API Foo
int operator ++();
int operator --();

bool btest[5];

protected:
int P;
TemplateInAnotherUnit<int> templateInAnotherUnit;
Expand Down

0 comments on commit 04a1591

Please sign in to comment.