Browse Source

Added a test for fixed arrays which showed that setting actually worked incorrectly.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/75/head
Dimitar Dobrev 12 years ago
parent
commit
acbdf7d82a
  1. 59
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  2. 1
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 10
      tests/CSharpTemp/CSharpTemp.Tests.cs

59
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -115,23 +115,21 @@ namespace CppSharp.Generators.CSharp @@ -115,23 +115,21 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType)
{
case ArrayType.ArraySize.Constant:
StringBuilder arrayBuilder = new StringBuilder();
arrayBuilder.AppendFormat("{0}[] value = new {0}[{1}];", array.Type, array.Size);
arrayBuilder.AppendLine();
arrayBuilder.AppendFormat("fixed ({0}* v = value)", array.Type);
arrayBuilder.AppendLine();
arrayBuilder.AppendLine("{");
arrayBuilder.AppendFormat(" {0}* to = v;", array.Type);
arrayBuilder.AppendLine();
arrayBuilder.AppendFormat(" {0}* from = {1}->{2};",
array.Type, Generator.GeneratedIdentifier("ptr"), Context.ArgName);
arrayBuilder.AppendLine();
arrayBuilder.AppendFormat(" for (int i = 0; i < {0}; i++)", array.Size);
arrayBuilder.AppendLine();
arrayBuilder.AppendLine(" *to++ = *from++;");
arrayBuilder.Append("}");
Context.SupportBefore.WriteLine(arrayBuilder.ToString());
Context.Return.Write("value");
var supportBefore = Context.SupportBefore;
string value = Generator.GeneratedIdentifier("value");
supportBefore.WriteLine("{0}[] {1} = new {0}[{2}];", array.Type, value, array.Size);
string v = Generator.GeneratedIdentifier("v");
supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, value);
supportBefore.WriteStartBraceIndent();
string to = Generator.GeneratedIdentifier("to");
supportBefore.WriteLine("{0}* {1} = {2};", array.Type, to, v);
string from = Generator.GeneratedIdentifier("from");
supportBefore.WriteLine("{0}* {1} = {2}->{3};",
array.Type, from, Generator.GeneratedIdentifier("ptr"), Context.ArgName);
supportBefore.WriteLine("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.WriteLineIndent("*{0}++ = *{1}++;", to, from);
supportBefore.WriteCloseBraceIndent();
Context.Return.Write(value);
break;
case ArrayType.ArraySize.Variable:
Context.Return.Write("null");
@ -377,18 +375,23 @@ namespace CppSharp.Generators.CSharp @@ -377,18 +375,23 @@ namespace CppSharp.Generators.CSharp
switch (array.SizeType)
{
case ArrayType.ArraySize.Constant:
StringBuilder arrayBuilder = new StringBuilder();
arrayBuilder.AppendFormat("if (value.Length != {0})", array.Size);
arrayBuilder.AppendLine();
arrayBuilder.AppendFormat(
" throw new ArgumentException(\"The value must be an array of size {0}.\");",
var supportBefore = Context.SupportBefore;
supportBefore.WriteLine("if ({0}.Length != {1})",
Context.ArgName, array.Size);
supportBefore.WriteLineIndent(
"throw new ArgumentException(\"The value must be an array of size {0}.\");",
array.Size);
arrayBuilder.AppendLine();
arrayBuilder.AppendFormat("fixed ({0}* v = value)", array.Type);
arrayBuilder.AppendLine();
arrayBuilder.Append(" *");
Context.SupportBefore.Write(arrayBuilder.ToString());
Context.Return.Write("*v");
string v = Generator.GeneratedIdentifier("v");
supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, Context.ArgName);
supportBefore.WriteLineIndent("for (int i = 0; i < {0}; i++)", array.Size);
supportBefore.PushIndent();
supportBefore.WriteLineIndent("*({0}->{1} + i) = *({2} + i);",
Generator.GeneratedIdentifier("ptr"), Context.ReturnVarName, v);
supportBefore.PopIndent();
supportBefore.WriteLine("fixed ({0}* {1} = {2})", array.Type, v, Context.ArgName);
supportBefore.PushIndent();
supportBefore.Write("*");
Context.Return.Write("*{0}", v);
break;
default:
Context.Return.Write("null");

1
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -826,6 +826,7 @@ namespace CppSharp.Generators.CSharp @@ -826,6 +826,7 @@ namespace CppSharp.Generators.CSharp
Generator.GeneratedIdentifier("ptr"), Helpers.InstanceIdentifier);
var marshal = new CSharpMarshalManagedToNativePrinter(ctx);
ctx.ReturnVarName = field.OriginalName;
param.Visit(marshal);
if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))

10
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -31,6 +31,16 @@ public class CSharpTempTests @@ -31,6 +31,16 @@ public class CSharpTempTests
BindingFlags.Instance | BindingFlags.NonPublic), Is.Not.Null);
}
[Test]
public void TestFixedArrays()
{
Qux qux = new Qux();
var array = new[] { 1, 2, 3 };
qux.array = array;
for (int i = 0; i < qux.array.Length; i++)
Assert.That(array[i], Is.EqualTo(qux.array[i]));
}
[Test]
public void TestMultipleInheritance()
{

Loading…
Cancel
Save