Skip to content

Commit

Permalink
Fixed some bugs with parsing booleans.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Jul 30, 2023
1 parent 966cf3b commit 16e91ab
Show file tree
Hide file tree
Showing 11 changed files with 519 additions and 282 deletions.
61 changes: 60 additions & 1 deletion Schema Build Tests/build/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,67 @@

namespace build {
[BinarySchema]
public partial class ClassWith1Bool : IBinaryConvertible {
public partial class ClassWithInt16Bools : IBinaryConvertible {
[IntegerFormat(SchemaIntegerType.INT16)]
public bool Bool { get; private set; }

[IntegerFormat(SchemaIntegerType.INT16)]
public bool ReadonlyBool { get; }

[SequenceLengthSource(4)]
[IntegerFormat(SchemaIntegerType.INT16)]
public bool[] Bools { get; set; }

[SequenceLengthSource(4)]
[IntegerFormat(SchemaIntegerType.INT16)]
public bool[] ReadonlyBools { get; set; }
}

[BinarySchema]
public partial class ClassWithInt32Bools : IBinaryConvertible {
[IntegerFormat(SchemaIntegerType.INT32)]
public bool Bool { get; private set; }

[IntegerFormat(SchemaIntegerType.INT32)]
public bool ReadonlyBool { get; }

[SequenceLengthSource(4)]
[IntegerFormat(SchemaIntegerType.INT32)]
public bool[] Bools { get; set; }

[SequenceLengthSource(4)]
[IntegerFormat(SchemaIntegerType.INT32)]
public bool[] ReadonlyBools { get; set; }
}

[BinarySchema]
public partial class ClassWithInt64Bools : IBinaryConvertible {
[IntegerFormat(SchemaIntegerType.INT64)]
public bool Bool { get; private set; }

[IntegerFormat(SchemaIntegerType.INT64)]
public bool ReadonlyBool { get; }

[SequenceLengthSource(4)]
[IntegerFormat(SchemaIntegerType.INT64)]
public bool[] Bools { get; set; }

[SequenceLengthSource(4)]
[IntegerFormat(SchemaIntegerType.INT64)]
public bool[] ReadonlyBools { get; set; }
}


[BinarySchema]
public partial class ClassWithChars : IBinaryConvertible {
public char Char { get; private set; }

public char ReadonlyChar { get; }

[SequenceLengthSource(4)]
public char[] Chars { get; set; }

[SequenceLengthSource(4)]
public char[] ReadonlyChars { get; set; }
}
}
2 changes: 1 addition & 1 deletion Schema Tests/SchemaWriterGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void Write(ISubEndianBinaryWriter ew) {" +
ew.WriteInt32s(this.intValues);" +
@"
this.other.Write(ew);
ew.WriteInt32((int) this.others.Length);
ew.WriteInt32(this.others.Length);
foreach (var e in this.others) {
e.Write(ew);
}
Expand Down
140 changes: 136 additions & 4 deletions Schema Tests/binary/generator/BooleanGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace schema.binary.text {
internal class BooleanGeneratorTests {
[Test] public void TestByte() {
[Test]
public void TestByte() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
Expand All @@ -18,19 +19,19 @@ public partial class ByteWrapper {
public bool ReadonlyField { get; }
}
}",
@"using System;
@"using System;
using System.IO;
namespace foo.bar {
public partial class ByteWrapper {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadByte() != 0;
er.AssertByte(this.ReadonlyField ? 1 : 0);
er.AssertByte((byte) (this.ReadonlyField ? 1 : 0));
}
}
}
",
@"using System;
@"using System;
using System.IO;
namespace foo.bar {
Expand All @@ -41,6 +42,137 @@ public void Write(ISubEndianBinaryWriter ew) {
}
}
}
");
}


[Test]
public void TestInt16() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class ByteWrapper {
[IntegerFormat(SchemaIntegerType.INT16)]
public bool Field { get; set; }
[IntegerFormat(SchemaIntegerType.INT16)]
public bool ReadonlyField { get; }
}
}",
@"using System;
using System.IO;
namespace foo.bar {
public partial class ByteWrapper {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadInt16() != 0;
er.AssertInt16((short) (this.ReadonlyField ? 1 : 0));
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class ByteWrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteInt16((short) (this.Field ? 1 : 0));
ew.WriteInt16((short) (this.ReadonlyField ? 1 : 0));
}
}
}
");
}


[Test]
public void TestInt32() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class ByteWrapper {
[IntegerFormat(SchemaIntegerType.INT32)]
public bool Field { get; set; }
[IntegerFormat(SchemaIntegerType.INT32)]
public bool ReadonlyField { get; }
}
}",
@"using System;
using System.IO;
namespace foo.bar {
public partial class ByteWrapper {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadInt32() != 0;
er.AssertInt32(this.ReadonlyField ? 1 : 0);
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class ByteWrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteInt32(this.Field ? 1 : 0);
ew.WriteInt32(this.ReadonlyField ? 1 : 0);
}
}
}
");
}


[Test]
public void TestByteArray() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class ByteWrapper {
[SequenceLengthSource(4)]
[IntegerFormat(SchemaIntegerType.BYTE)]
public bool[] Field { get; set; }
}
}",
@"using System;
using System.IO;
using schema.util.sequences;
namespace foo.bar {
public partial class ByteWrapper {
public void Read(IEndianBinaryReader er) {
this.Field = SequencesUtil.CloneAndResizeSequence(this.Field, 4);
for (var i = 0; i < this.Field.Length; ++i) {
this.Field[i] = er.ReadByte() != 0;
}
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class ByteWrapper {
public void Write(ISubEndianBinaryWriter ew) {
for (var i = 0; i < this.Field.Length; ++i) {
ew.WriteByte((byte) (this.Field[i] ? 1 : 0));
}
}
}
}
");
}
}
Expand Down
86 changes: 86 additions & 0 deletions Schema Tests/binary/generator/CharGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using NUnit.Framework;


namespace schema.binary.text {
internal class CharGeneratorTests {
[Test]
public void TestChar() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper {
public char Field { get; set; }
public char ReadonlyField { get; }
}
}",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadChar();
er.AssertChar(this.ReadonlyField);
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteChar(this.Field);
ew.WriteChar(this.ReadonlyField);
}
}
}
");
}


[Test]
public void TestCharArray() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper {
[SequenceLengthSource(4)]
public char[] Field { get; set; }
}
}",
@"using System;
using System.IO;
using schema.util.sequences;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
this.Field = SequencesUtil.CloneAndResizeSequence(this.Field, 4);
er.ReadChars(this.Field);
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteChars(this.Field);
}
}
}
");
}
}
}
4 changes: 2 additions & 2 deletions Schema Tests/binary/generator/HalfGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public partial class HalfWrapper {
namespace foo.bar {
public partial class HalfWrapper {
public void Read(IEndianBinaryReader er) {
this.field1 = (Single) er.ReadHalf();
this.field1 = (float) er.ReadHalf();
er.AssertHalf((float) this.field2);
for (var i = 0; i < this.field3.Length; ++i) {
this.field3[i] = (Single) er.ReadHalf();
this.field3[i] = (float) er.ReadHalf();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Schema/Schema.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Description>Library for converting classes to and from binary. Provides a C# Roslyn generator that automatically implements conversion logic for simple classes.</Description>
<PackageId>schema</PackageId>
<Title>schema</Title>
<Version>0.1.8</Version>
<Version>0.1.9</Version>
<Authors>MeltyPlayer</Authors>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/SchemaIntegerType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static SchemaNumberType AsNumberType(this SchemaIntegerType type)
SchemaIntegerType.UINT32 => SchemaNumberType.UINT32,
SchemaIntegerType.INT64 => SchemaNumberType.INT64,
SchemaIntegerType.UINT64 => SchemaNumberType.UINT64,
SchemaIntegerType.UNDEFINED => SchemaNumberType.UNDEFINED,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};

Expand Down
2 changes: 2 additions & 0 deletions Schema/src/binary/SchemaNumberType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static SchemaIntegerType AsIntegerType(this SchemaNumberType type)
SchemaNumberType.UINT32 => SchemaIntegerType.UINT32,
SchemaNumberType.INT64 => SchemaIntegerType.INT64,
SchemaNumberType.UINT64 => SchemaIntegerType.UINT64,
SchemaNumberType.UNDEFINED => SchemaIntegerType.UNDEFINED,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};

Expand All @@ -62,6 +63,7 @@ public static SchemaPrimitiveType AsPrimitiveType(
SchemaNumberType.UN8 => SchemaPrimitiveType.UN8,
SchemaNumberType.SN16 => SchemaPrimitiveType.SN16,
SchemaNumberType.UN16 => SchemaPrimitiveType.UN16,
SchemaNumberType.UNDEFINED => SchemaPrimitiveType.UNDEFINED,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
}
Expand Down
1 change: 1 addition & 0 deletions Schema/src/binary/SchemaPrimitiveType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static SchemaNumberType AsNumberType(this SchemaPrimitiveType type)
SchemaPrimitiveType.UN8 => SchemaNumberType.UN8,
SchemaPrimitiveType.SN16 => SchemaNumberType.SN16,
SchemaPrimitiveType.UN16 => SchemaNumberType.UN16,
SchemaPrimitiveType.UNDEFINED => SchemaNumberType.UNDEFINED,
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};

Expand Down
Loading

0 comments on commit 16e91ab

Please sign in to comment.