Skip to content

Commit

Permalink
Added a bit more test coverage for subreads.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Nov 26, 2024
1 parent c95f722 commit 5fc9512
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 9 deletions.
166 changes: 166 additions & 0 deletions Schema Tests/binary/reader/SubreadLocalSpaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
using NUnit.Framework;

namespace schema.binary;

internal class SubreadLocalSpaceTests {
[Test]
public void TestSubreadWithoutReturn() {
using var br = new SchemaBinaryReader(new byte[100]);

br.Position = 2;
br.PushLocalSpace();

Assert.AreEqual(0, br.Position);
Assert.AreEqual(98, br.Length);

br.Position = 3;
Assert.AreEqual(3, br.Position);

br.Subread(
50,
sbr => {
Assert.AreEqual(3, sbr.Position);
Assert.AreEqual(53, sbr.Length);

sbr.Position = 4;
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(53, sbr.Length);
});

Assert.AreEqual(53, br.Position);
Assert.AreEqual(98, br.Length);
}

[Test]
public void TestSubreadWithReturn() {
using var br = new SchemaBinaryReader(new byte[100]);

br.Position = 2;
br.PushLocalSpace();

Assert.AreEqual(0, br.Position);
Assert.AreEqual(98, br.Length);

br.Position = 3;
Assert.AreEqual(3, br.Position);

var returnValue = br.Subread(
50,
sbr => {
Assert.AreEqual(3, sbr.Position);
Assert.AreEqual(53, sbr.Length);

sbr.Position = 4;
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(53, sbr.Length);

return 123;
});
Assert.AreEqual(123, returnValue);

Assert.AreEqual(53, br.Position);
Assert.AreEqual(98, br.Length);
}

[Test]
public void TestSubreadAtWithoutLengthWithoutReturn() {
using var br = new SchemaBinaryReader(new byte[100]);

br.Position = 2;
br.PushLocalSpace();

Assert.AreEqual(0, br.Position);
Assert.AreEqual(98, br.Length);

br.Position = 3;
Assert.AreEqual(3, br.Position);

br.SubreadAt(
4,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(98, sbr.Length);
});

Assert.AreEqual(3, br.Position);
Assert.AreEqual(98, br.Length);
}

[Test]
public void TestSubreadAtWithLengthWithoutReturn() {
using var br = new SchemaBinaryReader(new byte[100]);

br.Position = 2;
br.PushLocalSpace();

Assert.AreEqual(0, br.Position);
Assert.AreEqual(98, br.Length);

br.Position = 3;
Assert.AreEqual(3, br.Position);

br.SubreadAt(
4,
50,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(54, sbr.Length);
});

Assert.AreEqual(3, br.Position);
Assert.AreEqual(98, br.Length);
}

[Test]
public void TestSubreadAtWithoutLengthWithReturn() {
using var br = new SchemaBinaryReader(new byte[100]);

br.Position = 2;
br.PushLocalSpace();

Assert.AreEqual(0, br.Position);
Assert.AreEqual(98, br.Length);

br.Position = 3;
Assert.AreEqual(3, br.Position);

var returnValue = br.SubreadAt(
4,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(98, sbr.Length);
return 123;
});
Assert.AreEqual(123, returnValue);

Assert.AreEqual(3, br.Position);
Assert.AreEqual(98, br.Length);
}

[Test]
public void TestSubreadAtWithLengthWithReturn() {
using var br = new SchemaBinaryReader(new byte[100]);

br.Position = 2;
br.PushLocalSpace();

Assert.AreEqual(0, br.Position);
Assert.AreEqual(98, br.Length);

br.Position = 3;
Assert.AreEqual(3, br.Position);

var returnValue = br.SubreadAt(
4,
50,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(54, sbr.Length);
return 123;
});
Assert.AreEqual(123, returnValue);

Assert.AreEqual(3, br.Position);
Assert.AreEqual(98, br.Length);
}
}
10 changes: 1 addition & 9 deletions Schema/src/binary/reader/SchemaBinaryReader_Subread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ namespace schema.binary;
public partial class SchemaBinaryReader {
public void Subread(int len, Action<IBinaryReader> subread) {
var baseOffset = this.Position;
var substream = new RangedReadableSubstream(this.BaseStream_,
baseOffset,
len);
using var sbr = new SchemaBinaryReader(substream, this.Endianness) {
AssertAlreadyAtOffset = this.AssertAlreadyAtOffset,
};
sbr.positionManagerImpl_ = new StreamPositionManager(substream);
subread(sbr);

this.SubreadAt(baseOffset, len, subread);
this.Position = baseOffset + len;
}

Expand Down

0 comments on commit 5fc9512

Please sign in to comment.