Skip to content

Commit

Permalink
Simplified subread logic so it doesn't require creating new instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
MeltyPlayer committed Nov 26, 2024
1 parent 5fc9512 commit 2dd1923
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ public void TestWriteAndReadBytesInSubstream() {
};

var ms = new MemoryStream(bytes);
var er = new SchemaBinaryReader(ms);
var br = new SchemaBinaryReader(ms);

ByteSequenceWrapper actualSw = default;
er.SubreadAt(3,
br.SubreadAt(3,
expectedSw.Values.Length,
ser => {
Assert.AreEqual(3, ser.Position);
Assert.AreEqual(9, ser.Length);
() => {
Assert.AreEqual(3, br.Position);
Assert.AreEqual(9, br.Length);

actualSw = ser.ReadNew<ByteSequenceWrapper>();
actualSw = br.ReadNew<ByteSequenceWrapper>();

Assert.AreEqual(9, ser.Position);
Assert.AreEqual(9, br.Position);
});

Assert.AreEqual(expectedSw, actualSw);
Expand All @@ -79,22 +79,22 @@ public void TestWriteAndReadBytesInLocalSubstream() {
};

var ms = new MemoryStream(bytes);
var er = new SchemaBinaryReader(ms);
var br = new SchemaBinaryReader(ms);

er.Position = 1;
er.PushLocalSpace();
Assert.AreEqual(0, er.Position);
br.Position = 1;
br.PushLocalSpace();
Assert.AreEqual(0, br.Position);

ByteSequenceWrapper actualSw = default;
er.SubreadAt(2,
br.SubreadAt(2,
expectedSw.Values.Length,
ser => {
Assert.AreEqual(2, ser.Position);
Assert.AreEqual(8, ser.Length);
() => {
Assert.AreEqual(2, br.Position);
Assert.AreEqual(8, br.Length);

actualSw = ser.ReadNew<ByteSequenceWrapper>();
actualSw = br.ReadNew<ByteSequenceWrapper>();

Assert.AreEqual(8, ser.Position);
Assert.AreEqual(8, br.Position);
});

Assert.AreEqual(expectedSw, actualSw);
Expand Down Expand Up @@ -246,22 +246,22 @@ public void TestWriteAndReadClassesInLocalSubstream() {
};

var ms = new MemoryStream(bytes);
var er = new SchemaBinaryReader(ms);
var br = new SchemaBinaryReader(ms);

er.Position = 1;
er.PushLocalSpace();
Assert.AreEqual(0, er.Position);
br.Position = 1;
br.PushLocalSpace();
Assert.AreEqual(0, br.Position);

ByteClassSequenceWrapper actualSw = default;
er.SubreadAt(2,
br.SubreadAt(2,
3 * expectedSw.Values.Length,
ser => {
Assert.AreEqual(2, ser.Position);
Assert.AreEqual(11, ser.Length);
() => {
Assert.AreEqual(2, br.Position);
Assert.AreEqual(11, br.Length);

actualSw = ser.ReadNew<ByteClassSequenceWrapper>();
actualSw = br.ReadNew<ByteClassSequenceWrapper>();

Assert.AreEqual(11, ser.Position);
Assert.AreEqual(11, br.Position);
});

Assert.AreEqual(expectedSw, actualSw);
Expand Down
16 changes: 8 additions & 8 deletions Schema Tests/binary/reader/AssertOffsetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void TestNestedSpaces_NotThrowing() {
br.PushLocalSpace();
br.Position = 5;

Assert.DoesNotThrow(() => br.SubreadAt(5, 5, (_) => { }));
Assert.DoesNotThrow(() => br.SubreadAt(5, 5, () => { }));
}

[Test]
Expand All @@ -41,7 +41,7 @@ public void TestNestedSpaces_Throwing() {
br.Position = 5;

Assert.Throws<Asserts.AssertionException>(
() => br.SubreadAt(8, 5, (_) => { }));
() => br.SubreadAt(8, 5, () => { }));
}

[Test]
Expand All @@ -53,9 +53,9 @@ public void TestNestedSubreads_NotThrowing() {
br.Position = 5;
br.SubreadAt(
0,
sbr => {
sbr.Position = 10;
Assert.DoesNotThrow(() => sbr.SubreadAt(10, 5, (_) => { }));
() => {
br.Position = 10;
Assert.DoesNotThrow(() => br.SubreadAt(10, 5, () => { }));
});
}

Expand All @@ -68,10 +68,10 @@ public void TestNestedSubreads_Throwing() {
br.Position = 5;
br.SubreadAt(
0,
sbr => {
sbr.Position = 10;
() => {
br.Position = 10;
Assert.Throws<Asserts.AssertionException>(
() => sbr.SubreadAt(8, 5, (_) => { }));
() => br.SubreadAt(8, 5, () => { }));
});
}
}
6 changes: 3 additions & 3 deletions Schema Tests/binary/reader/PositionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public void TestNestedPointer() {
br.SubreadAt(
4,
50,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(54, sbr.Length);
() => {
Assert.AreEqual(4, br.Position);
Assert.AreEqual(54, br.Length);
});
Assert.AreEqual(3, br.Position);
}
Expand Down
48 changes: 24 additions & 24 deletions Schema Tests/binary/reader/SubreadLocalSpaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public void TestSubreadWithoutReturn() {

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

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

Assert.AreEqual(53, br.Position);
Expand All @@ -46,13 +46,13 @@ public void TestSubreadWithReturn() {

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

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

return 123;
});
Expand All @@ -77,9 +77,9 @@ public void TestSubreadAtWithoutLengthWithoutReturn() {

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

Assert.AreEqual(3, br.Position);
Expand All @@ -102,9 +102,9 @@ public void TestSubreadAtWithLengthWithoutReturn() {
br.SubreadAt(
4,
50,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(54, sbr.Length);
() => {
Assert.AreEqual(4, br.Position);
Assert.AreEqual(54, br.Length);
});

Assert.AreEqual(3, br.Position);
Expand All @@ -126,9 +126,9 @@ public void TestSubreadAtWithoutLengthWithReturn() {

var returnValue = br.SubreadAt(
4,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(98, sbr.Length);
() => {
Assert.AreEqual(4, br.Position);
Assert.AreEqual(98, br.Length);
return 123;
});
Assert.AreEqual(123, returnValue);
Expand All @@ -153,9 +153,9 @@ public void TestSubreadAtWithLengthWithReturn() {
var returnValue = br.SubreadAt(
4,
50,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(54, sbr.Length);
() => {
Assert.AreEqual(4, br.Position);
Assert.AreEqual(54, br.Length);
return 123;
});
Assert.AreEqual(123, returnValue);
Expand Down
48 changes: 24 additions & 24 deletions Schema Tests/binary/reader/SubreadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public void TestSubreadWithoutReturn() {

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

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

Assert.AreEqual(53, br.Position);
Expand All @@ -36,13 +36,13 @@ public void TestSubreadWithReturn() {

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

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

return 123;
});
Expand All @@ -62,9 +62,9 @@ public void TestSubreadAtWithoutLengthWithoutReturn() {

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

Assert.AreEqual(3, br.Position);
Expand All @@ -82,9 +82,9 @@ public void TestSubreadAtWithLengthWithoutReturn() {
br.SubreadAt(
4,
50,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(54, sbr.Length);
() => {
Assert.AreEqual(4, br.Position);
Assert.AreEqual(54, br.Length);
});

Assert.AreEqual(3, br.Position);
Expand All @@ -101,9 +101,9 @@ public void TestSubreadAtWithoutLengthWithReturn() {

var returnValue = br.SubreadAt(
4,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(100, sbr.Length);
() => {
Assert.AreEqual(4, br.Position);
Assert.AreEqual(100, br.Length);
return 123;
});
Assert.AreEqual(123, returnValue);
Expand All @@ -123,9 +123,9 @@ public void TestSubreadAtWithLengthWithReturn() {
var returnValue = br.SubreadAt(
4,
50,
sbr => {
Assert.AreEqual(4, sbr.Position);
Assert.AreEqual(54, sbr.Length);
() => {
Assert.AreEqual(4, br.Position);
Assert.AreEqual(54, br.Length);
return 123;
});
Assert.AreEqual(123, returnValue);
Expand Down
Loading

0 comments on commit 2dd1923

Please sign in to comment.