Skip to content

Commit

Permalink
Fixed some bugs around generating logic for reading/writing lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Aug 1, 2023
1 parent 69095e4 commit 01a61a9
Show file tree
Hide file tree
Showing 26 changed files with 467 additions and 291 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;

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

namespace build.attributes.sequence {
public partial class RSequenceLengthSourceAttributeTests {
[BinarySchema]
public partial class ReadonlyListClass : IBinaryConvertible {
[WLengthOfSequence(nameof(Values))]
private uint count_;

[RSequenceLengthSource(nameof(count_))]
public readonly List<int> Values = new();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using NUnit.Framework;


namespace schema.binary.attributes {
internal class RSequenceUntilEndOfStreamAttributeTests {
[Test]
public void TestByteArrayUntilEndOfStream() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public byte[] Field { get; set; }
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadBytes(er.Length - er.Position);
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteBytes(this.Field);
}
}
}
");
}

[Test]
public void TestByteListUntilEndOfStream() {
BinarySchemaTestUtil.AssertGenerated(@"
using System.Collections.Generic;
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public List<byte> Field { get; } = new();
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
{
this.Field.Clear();
while (!er.Eof) {
this.Field.Add(er.ReadByte());
}
}
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
for (var i = 0; i < this.Field.Count; ++i) {
ew.WriteByte(this.Field[i]);
}
}
}
}
");
}

[Test]
public void TestIntArrayUntilEndOfStream() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public int[] Field { get; set; }
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadInt32s((er.Length - er.Position) / 4);
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteInt32s(this.Field);
}
}
}
");
}

[Test]
public void TestIntListUntilEndOfStream() {
BinarySchemaTestUtil.AssertGenerated(@"
using System.Collections.Generic;
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public List<int> Field { get; } = new();
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
{
this.Field.Clear();
while (!er.Eof) {
this.Field.Add(er.ReadInt32());
}
}
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
for (var i = 0; i < this.Field.Count; ++i) {
ew.WriteInt32(this.Field[i]);
}
}
}
}
");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,155 +2,35 @@


namespace schema.binary.attributes {
internal class RSequenceUntilEndOfStreamAttributeTests {
internal class RSequenceLengthSourceAttributeTests {
[Test]
public void TestByteArrayUntilEndOfStream() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public byte[] Field { get; set; }
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadBytes(er.Length - er.Position);
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteBytes(this.Field);
}
}
}
");
}

[Test]
public void TestByteListUntilEndOfStream() {
public void TestReadonlyList() {
BinarySchemaTestUtil.AssertGenerated(@"
using System.Collections.Generic;
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public List<byte> Field { get; } = new();
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
public partial class ReadonlyListClass : IBinaryConvertible {
[WLengthOfSequence(nameof(Values))]
private uint count_;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
{
this.Field.Clear();
while (!er.Eof) {
this.Field.Add(er.ReadByte());
}
}
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteBytes(this.Field);
}
}
}
");
}

[Test]
public void TestIntArrayUntilEndOfStream() {
BinarySchemaTestUtil.AssertGenerated(@"
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public int[] Field { get; set; }
[RSequenceLengthSource(nameof(count_))]
public readonly List<int> Values = new();
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
using schema.util.sequences;
namespace foo.bar {
public partial class Wrapper {
public partial class ReadonlyListClass {
public void Read(IEndianBinaryReader er) {
this.Field = er.ReadInt32s((er.Length - er.Position) / 4);
}
}
}
",
@"using System;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteInt32s(this.Field);
}
}
}
");
}

[Test]
public void TestIntListUntilEndOfStream() {
BinarySchemaTestUtil.AssertGenerated(@"
using System.Collections.Generic;
using schema.binary;
using schema.binary.attributes;
namespace foo.bar {
[BinarySchema]
public partial class Wrapper : IBinaryConvertible {
[RSequenceUntilEndOfStream]
public List<int> Field { get; } = new();
}
}",
@"using System;
using System.Collections.Generic;
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public void Read(IEndianBinaryReader er) {
{
this.Field.Clear();
while (!er.Eof) {
this.Field.Add(er.ReadInt32());
}
this.count_ = er.ReadUInt32();
SequencesUtil.ResizeSequenceInPlace(this.Values, (int) this.count_);
for (var i = 0; i < this.Values.Count; ++i) {
this.Values[i] = er.ReadInt32();
}
}
}
Expand All @@ -160,9 +40,12 @@ public void Read(IEndianBinaryReader er) {
using System.IO;
namespace foo.bar {
public partial class Wrapper {
public partial class ReadonlyListClass {
public void Write(ISubEndianBinaryWriter ew) {
ew.WriteInt32s(this.Field);
ew.WriteUInt32((uint) Values.Count);
for (var i = 0; i < this.Values.Count; ++i) {
ew.WriteInt32(this.Values[i]);
}
}
}
}
Expand Down
Loading

0 comments on commit 01a61a9

Please sign in to comment.