Skip to content

Commit

Permalink
Fixed some bugs around nullness and children and added more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Nov 4, 2023
1 parent 77ab7e9 commit 9fdebe7
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 48 deletions.
75 changes: 75 additions & 0 deletions Schema Build Tests/build/PointerOrNullTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.IO;
using System.Threading.Tasks;

using NUnit.Framework;

using schema.binary;
using schema.binary.attributes;
using schema.binary.testing;


namespace build {
internal partial class PointerToOrNullTests {
[BinarySchema]
public partial class ParentImpl : IBinaryConvertible {
public Child Child { get; } = new();

[RAtPositionOrNull(nameof(Child.FieldPointer), 123)]
public int? Field { get; set; }
}

[BinarySchema]
public partial class Child : IChildOf<ParentImpl>, IBinaryConvertible {
public ParentImpl Parent { get; set; }

[WPointerToOrNull(nameof(Parent.Field), 123)]
public byte FieldPointer { get; set; }
}


[Test]
public async Task TestReadNonnull() {
var ms = new MemoryStream(new byte[] { 1, 12, 0, 0, 0 });
using var br = new SchemaBinaryReader(ms);

var parent = br.ReadNew<ParentImpl>();

Assert.AreEqual(12, parent.Field.Value);
}

[Test]
public async Task TestReadNull() {
var ms = new MemoryStream(new byte[] { 123 });
using var br = new SchemaBinaryReader(ms);

var parent = br.ReadNew<ParentImpl>();

Assert.IsNull(parent.Field);
}


[Test]
public async Task TestWriteNonnull() {
var parent = new ParentImpl();
parent.Field = 12;

var bw = new SchemaBinaryWriter();
parent.Write(bw);

var bytes = await BinarySchemaAssert.GetEndianBinaryWriterBytes(bw);
CollectionAssert.AreEqual(new byte[] { 1, 12, 0, 0, 0 }, bytes);
}

[Test]
public async Task TestWriteNull() {
var parent = new ParentImpl();
parent.Field = null;

var bw = new SchemaBinaryWriter();
parent.Write(bw);

var bytes = await BinarySchemaAssert.GetEndianBinaryWriterBytes(bw);
CollectionAssert.AreEqual(new byte[] { 123 }, bytes);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void Read(IBinaryReader br) {
namespace foo.bar {
public partial class Parent {
public void Write(IBinaryWriter bw) {
this.Child.Parent = this;
this.Child.Write(bw);
}
}
Expand Down Expand Up @@ -173,6 +174,7 @@ public partial class Parent {
public void Write(IBinaryWriter bw) {
bw.WriteUInt32(this.Length);
foreach (var e in this.Child) {
e.Parent = this;
e.Write(bw);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace foo.bar {
[BinarySchema]
public partial class SizeWrapper : IBinaryConvertible {
[WPointerToOrNull(nameof(Foo), 123)]
public uint? FooOffset { get; set; }
public uint FooOffset { get; set; }
public byte Foo;
public byte? Foo;
}
}",
@"using System;
Expand All @@ -31,15 +31,18 @@ public void Read(IBinaryReader br) {
}
",
@"using System;
using System.Threading.Tasks;
using schema.binary;
namespace foo.bar {
public partial class SizeWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteUInt32Delayed((this.Foo == null ? Task.FromResult(123) : bw.GetPointerToMemberRelativeToScope(""Foo"")).ContinueWith(task => (uint) task.Result));
bw.MarkStartOfMember(""Foo"");
bw.WriteByte(this.Foo);
bw.MarkEndOfMember();
bw.WriteUInt32Delayed((this.Foo == null ? Task.FromResult(123L) : bw.GetPointerToMemberRelativeToScope(""Foo"")).ContinueWith(task => (uint) task.Result));
if (this.Foo != null) {
bw.MarkStartOfMember(""Foo"");
bw.WriteByte(this.Foo.Value);
bw.MarkEndOfMember();
}
}
}
}
Expand Down Expand Up @@ -79,12 +82,13 @@ public void Read(IBinaryReader br) {
}
",
@"using System;
using System.Threading.Tasks;
using schema.binary;
namespace foo.bar {
public partial class SizeWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteUInt32Delayed((this.Foo.Bar == null ? Task.FromResult(0) : bw.GetPointerToMemberRelativeToScope(""Foo.Bar"")).ContinueWith(task => (uint) task.Result));
bw.WriteUInt32Delayed((this.Foo.Bar == null ? Task.FromResult(0L) : bw.GetPointerToMemberRelativeToScope(""Foo.Bar"")).ContinueWith(task => (uint) task.Result));
bw.MarkStartOfMember(""Foo"");
this.Foo.Write(bw);
bw.MarkEndOfMember();
Expand Down Expand Up @@ -129,12 +133,13 @@ public void Read(IBinaryReader br) {
}
",
@"using System;
using System.Threading.Tasks;
using schema.binary;
namespace foo.bar {
public partial class SizeWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteUInt32Delayed((this.Parent.Foo == null ? Task.FromResult(0) : bw.GetPointerToMemberRelativeToScope(""Foo"")).ContinueWith(task => (uint) task.Result));
bw.WriteUInt32Delayed((this.Parent.Foo == null ? Task.FromResult(0L) : bw.GetPointerToMemberRelativeToScope(""Foo"")).ContinueWith(task => (uint) task.Result));
}
}
}
Expand All @@ -159,10 +164,13 @@ public void Read(IBinaryReader br) {
namespace foo.bar {
public partial class ParentImpl {
public void Write(IBinaryWriter bw) {
this.Child.Parent = this;
this.Child.Write(bw);
bw.MarkStartOfMember(""Foo"");
bw.WriteByte(this.Foo.Value);
bw.MarkEndOfMember();
if (this.Foo != null) {
bw.MarkStartOfMember(""Foo"");
bw.WriteByte(this.Foo.Value);
bw.MarkEndOfMember();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void Read(IBinaryReader br) {
namespace foo.bar {
public partial class ParentImpl {
public void Write(IBinaryWriter bw) {
this.Child.Parent = this;
this.Child.Write(bw);
bw.MarkStartOfMember(""Foo"");
bw.WriteByte(this.Foo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public void Read(IBinaryReader br) {
namespace foo.bar {
public partial class ParentImpl {
public void Write(IBinaryWriter bw) {
this.Child.Parent = this;
this.Child.Write(bw);
bw.MarkStartOfMember(""Foo"");
bw.WriteByte(this.Foo);
Expand Down
6 changes: 3 additions & 3 deletions Schema Tests/binary/generator/IfBooleanGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void Write(IBinaryWriter bw) {
this.ImmediateValue.Write(bw);
}
bw.WriteByte((byte) (this.Field ? 1 : 0));
if (this.Field) {
if (this.OtherValue != null) {
bw.WriteInt32(this.OtherValue.Value);
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ namespace foo.bar {
public partial class ByteWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteByte((byte) (this.Field ? 1 : 0));
if (this.Field) {
if (this.OtherValue != null) {
bw.WriteInt32(this.OtherValue.Value);
}
}
Expand Down Expand Up @@ -165,7 +165,7 @@ namespace foo.bar {
public partial class ByteWrapper {
public void Write(IBinaryWriter bw) {
this.Field.Write(bw);
if (this.Field.Bool) {
if (this.OtherValue != null) {
bw.WriteInt32(this.OtherValue.Value);
}
}
Expand Down
8 changes: 6 additions & 2 deletions Schema Tests/binary/generator/NullableGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ public void Read(IBinaryReader br) {
namespace foo.bar {
public partial class NullableWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteByte((byte) (this.Field1.Value ? 1 : 0));
bw.WriteInt32(this.Field2.Value);
if (this.Field1 != null) {
bw.WriteByte((byte) (this.Field1.Value ? 1 : 0));
}
if (this.Field2 != null) {
bw.WriteInt32(this.Field2.Value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ namespace foo.bar {
public partial class OffsetWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteUInt32(this.Offset);
bw.WriteByte(this.Field.Value);
if (this.Field != null) {
bw.WriteByte(this.Field.Value);
}
}
}
}
Expand Down Expand Up @@ -98,7 +100,9 @@ public void Read(IBinaryReader br) {
namespace foo.bar {
public partial class OffsetWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteByte(this.Field.Value);
if (this.Field != null) {
bw.WriteByte(this.Field.Value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ namespace foo.bar {
public partial class ConstLengthWrapper {
public void Write(IBinaryWriter bw) {
bw.WriteInt32s(this.Field);
bw.WriteInt32s(this.NullableField);
if (this.Toggle) {
if (this.NullableField != null) {
bw.WriteInt32s(this.NullableField);
}
if (this.IfBooleanArray != null) {
bw.WriteInt32s(this.IfBooleanArray);
}
if (this.Toggle) {
if (this.IfBooleanList != null) {
for (var i = 0; i < this.IfBooleanList.Count; ++i) {
bw.WriteInt32(this.IfBooleanList[i]);
}
Expand Down
4 changes: 2 additions & 2 deletions Schema/src/binary/BinarySchemaStructureParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public interface IGenericMemberType : IMemberType {

public interface IOffset {
ISchemaValueMember OffsetName { get; }
int? NullValue { get; }
long? NullValue { get; }
}

public interface IStringType : IMemberType {
Expand Down Expand Up @@ -639,7 +639,7 @@ public class GenericMemberType : IGenericMemberType {

public class Offset : IOffset {
public ISchemaValueMember OffsetName { get; set; }
public int? NullValue { get; set; }
public long? NullValue { get; set; }
}

public class StringType : IStringType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace schema.binary.attributes {
public interface IAtPositionAttribute {
string OffsetName { get; }
int? NullValue { get; }
long? NullValue { get; }
}
}
2 changes: 1 addition & 1 deletion Schema/src/binary/attributes/memory/IPointerToAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace schema.binary.attributes {
public interface IPointerToAttribute {
IChain<IAccessChainNode> AccessChainToOtherMember { get; }
int? NullValue { get; }
long? NullValue { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public RAtPositionAttribute(string offsetName) {
}

public string OffsetName { get; }
public int? NullValue => null;
public long? NullValue => null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace schema.binary.attributes {
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class RAtPositionOrNullAttribute(string offsetName, int nullValue = 0)
public class RAtPositionOrNullAttribute(string offsetName, long nullValue = 0)
: Attribute, IAtPositionAttribute {
public string OffsetName { get; } = offsetName;
public int? NullValue { get; } = nullValue;
public long? NullValue { get; } = nullValue;
}
}
2 changes: 1 addition & 1 deletion Schema/src/binary/attributes/memory/WPointerToAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public IChain<IAccessChainNode> AccessChainToOtherMember {
private set;
}

public int? NullValue => null;
public long? NullValue => null;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;


namespace schema.binary.attributes {
Expand All @@ -12,7 +13,7 @@ public class WPointerToOrNullAttribute : BMemberAttribute,
private readonly string otherMemberName_;

public WPointerToOrNullAttribute(string otherMemberName,
int nullValue = 0) {
long nullValue = 0) {
this.otherMemberName_ = otherMemberName;
this.NullValue = nullValue;
}
Expand All @@ -29,6 +30,6 @@ public IChain<IAccessChainNode> AccessChainToOtherMember {
private set;
}

public int? NullValue { get; }
public long? NullValue { get; }
}
}
10 changes: 10 additions & 0 deletions Schema/src/binary/dependencies/DependencyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public static bool DependsOnSchemaUtilAsserts(
LengthOfSequenceMembers.Length: > 1
});


public static bool DependsOnSystemThreadingTasks(
this IBinarySchemaContainer container)
=> container
.Members
.OfType<ISchemaValueMember>()
.Any(member => member.MemberType is IPrimitiveMemberType {
PointerToAttribute: { NullValue: { } }
});

public static bool DependsOnCollectionsImports(
this IBinarySchemaContainer container)
=> container
Expand Down
Loading

0 comments on commit 9fdebe7

Please sign in to comment.