-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed some bugs around reading lists to the end of a stream.
- Loading branch information
1 parent
16e91ab
commit c595cbe
Showing
7 changed files
with
452 additions
and
100 deletions.
There are no files selected for viewing
267 changes: 267 additions & 0 deletions
267
Schema Build Tests/attributes/sequence/RSequenceUntilEndOfStreamAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.ConstrainedExecution; | ||
|
||
using schema.binary; | ||
using schema.binary.attributes; | ||
|
||
namespace build { | ||
public partial class RSequenceUntilEndOfStreamAttributeTests { | ||
[BinarySchema] | ||
public partial class ByteSequenceWrapper : IBinaryConvertible { | ||
[RSequenceUntilEndOfStream] | ||
public byte[] Values { get; set; } | ||
|
||
public override bool Equals(object other) { | ||
if (other is ByteSequenceWrapper otherSequenceWrapper) { | ||
return this.Values.SequenceEqual(otherSequenceWrapper.Values); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString() => string.Join(", ", Values); | ||
} | ||
|
||
[Test] | ||
public void TestWriteAndReadBytes() { | ||
var expectedSw = new ByteSequenceWrapper { | ||
Values = new byte[] { 1, 2, 3, 4, 5, 9, 8, 7, 6 } | ||
}; | ||
|
||
var ms = new MemoryStream(); | ||
|
||
var endianness = Endianness.BigEndian; | ||
|
||
var ew = new EndianBinaryWriter(endianness); | ||
expectedSw.Write(ew); | ||
ew.CompleteAndCopyToDelayed(ms).Wait(); | ||
|
||
ms.Position = 0; | ||
var er = new EndianBinaryReader(ms, endianness); | ||
var actualSw = er.ReadNew<ByteSequenceWrapper>(); | ||
Assert.AreEqual(expectedSw, actualSw); | ||
} | ||
|
||
[Test] | ||
public void TestWriteAndReadBytesInSubstream() { | ||
var bytes = new byte[] { 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, }; | ||
var expectedSw = new ByteSequenceWrapper { | ||
Values = new byte[] { 1, 2, 3, 4, 5, 6 } | ||
}; | ||
|
||
var ms = new MemoryStream(bytes); | ||
var er = new EndianBinaryReader(ms); | ||
|
||
ByteSequenceWrapper actualSw = default; | ||
er.Subread(3, | ||
expectedSw.Values.Length, | ||
ser => { | ||
Assert.AreEqual(3, ser.Position); | ||
Assert.AreEqual(9, ser.Length); | ||
actualSw = ser.ReadNew<ByteSequenceWrapper>(); | ||
Assert.AreEqual(9, ser.Position); | ||
}); | ||
|
||
Assert.AreEqual(expectedSw, actualSw); | ||
} | ||
|
||
[Test] | ||
public void TestWriteAndReadBytesInLocalSubstream() { | ||
var bytes = new byte[] { 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, }; | ||
var expectedSw = new ByteSequenceWrapper { | ||
Values = new byte[] { 1, 2, 3, 4, 5, 6 } | ||
}; | ||
|
||
var ms = new MemoryStream(bytes); | ||
var er = new EndianBinaryReader(ms); | ||
|
||
er.Position = 1; | ||
er.PushLocalSpace(); | ||
Assert.AreEqual(0, er.Position); | ||
|
||
ByteSequenceWrapper actualSw = default; | ||
er.Subread(2, | ||
expectedSw.Values.Length, | ||
ser => { | ||
Assert.AreEqual(2, ser.Position); | ||
Assert.AreEqual(8, ser.Length); | ||
actualSw = ser.ReadNew<ByteSequenceWrapper>(); | ||
Assert.AreEqual(8, ser.Position); | ||
}); | ||
|
||
Assert.AreEqual(expectedSw, actualSw); | ||
} | ||
|
||
[BinarySchema] | ||
public partial class IntSequenceWrapper : IBinaryConvertible { | ||
[RSequenceUntilEndOfStream] | ||
public int[] Values { get; set; } | ||
|
||
public override bool Equals(object other) { | ||
if (other is IntSequenceWrapper otherSequenceWrapper) { | ||
return this.Values.SequenceEqual(otherSequenceWrapper.Values); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString() => string.Join(", ", Values); | ||
} | ||
|
||
[Test] | ||
public void TestWriteAndReadInts() { | ||
var expectedSw = new IntSequenceWrapper { | ||
Values = new[] { 1, 2, 3, 4, 5, 9, 8, 7, 6 } | ||
}; | ||
|
||
var ms = new MemoryStream(); | ||
|
||
var endianness = Endianness.BigEndian; | ||
|
||
var ew = new EndianBinaryWriter(endianness); | ||
expectedSw.Write(ew); | ||
ew.CompleteAndCopyToDelayed(ms).Wait(); | ||
|
||
ms.Position = 0; | ||
var er = new EndianBinaryReader(ms, endianness); | ||
var actualSw = er.ReadNew<IntSequenceWrapper>(); | ||
Assert.AreEqual(expectedSw, actualSw); | ||
} | ||
|
||
|
||
[BinarySchema] | ||
public partial class Vector3f : IBinaryConvertible { | ||
public float X { get; set; } | ||
public float Y { get; set; } | ||
public float Z { get; set; } | ||
|
||
public override bool Equals(object other) { | ||
if (other is Vector3f otherVector) { | ||
return X == otherVector.X && | ||
Y == otherVector.Y && | ||
Z == otherVector.Z; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString() => $"({X}, {Y}, {Z})"; | ||
} | ||
|
||
[BinarySchema] | ||
public partial class FloatClassSequenceWrapper : IBinaryConvertible { | ||
[RSequenceUntilEndOfStream] | ||
public Vector3f[] Values { get; set; } | ||
|
||
public override bool Equals(object other) { | ||
if (other is FloatClassSequenceWrapper otherSequenceWrapper) { | ||
return this.Values.SequenceEqual(otherSequenceWrapper.Values); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString() | ||
=> string.Join(", ", Values.Select(value => value.ToString())); | ||
} | ||
|
||
[Test] | ||
public void TestWriteAndReadClasses() { | ||
var expectedSw = new FloatClassSequenceWrapper { | ||
Values = new Vector3f[] { | ||
new() { X = 1, Y = 2, Z = 3 }, | ||
new() { X = 2, Y = 3, Z = 4 }, | ||
new() { X = 3, Y = 4, Z = 5 }, | ||
}, | ||
}; | ||
|
||
var ms = new MemoryStream(); | ||
|
||
var endianness = Endianness.BigEndian; | ||
|
||
var ew = new EndianBinaryWriter(endianness); | ||
expectedSw.Write(ew); | ||
ew.CompleteAndCopyToDelayed(ms).Wait(); | ||
|
||
ms.Position = 0; | ||
var er = new EndianBinaryReader(ms, endianness); | ||
var actualSw = er.ReadNew<FloatClassSequenceWrapper>(); | ||
Assert.AreEqual(expectedSw, actualSw); | ||
} | ||
|
||
|
||
[BinarySchema] | ||
public partial class Vector3b : IBinaryConvertible { | ||
public byte X { get; set; } | ||
public byte Y { get; set; } | ||
public byte Z { get; set; } | ||
|
||
public override bool Equals(object other) { | ||
if (other is Vector3b otherVector) { | ||
return X == otherVector.X && | ||
Y == otherVector.Y && | ||
Z == otherVector.Z; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString() => $"({X}, {Y}, {Z})"; | ||
} | ||
|
||
[BinarySchema] | ||
public partial class ByteClassSequenceWrapper : IBinaryConvertible { | ||
[RSequenceUntilEndOfStream] | ||
public Vector3b[] Values { get; set; } | ||
|
||
public override bool Equals(object other) { | ||
if (other is ByteClassSequenceWrapper otherSequenceWrapper) { | ||
return this.Values.SequenceEqual(otherSequenceWrapper.Values); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public override string ToString() | ||
=> string.Join(", ", Values.Select(value => value.ToString())); | ||
} | ||
|
||
[Test] | ||
public void TestWriteAndReadClassesInLocalSubstream() { | ||
var bytes = new byte[] { 0, 0, 0, 1, 2, 3, 2, 3, 4, 3, 4, 5, 0, 0, 0, }; | ||
var expectedSw = new ByteClassSequenceWrapper { | ||
Values = new Vector3b[] { | ||
new() { X = 1, Y = 2, Z = 3 }, | ||
new() { X = 2, Y = 3, Z = 4 }, | ||
new() { X = 3, Y = 4, Z = 5 }, | ||
}, | ||
}; | ||
|
||
var ms = new MemoryStream(bytes); | ||
var er = new EndianBinaryReader(ms); | ||
|
||
er.Position = 1; | ||
er.PushLocalSpace(); | ||
Assert.AreEqual(0, er.Position); | ||
|
||
ByteClassSequenceWrapper actualSw = default; | ||
er.Subread(2, | ||
3 * expectedSw.Values.Length, | ||
ser => { | ||
Assert.AreEqual(2, ser.Position); | ||
Assert.AreEqual(11, ser.Length); | ||
actualSw = ser.ReadNew<ByteClassSequenceWrapper>(); | ||
Assert.AreEqual(11, ser.Position); | ||
}); | ||
|
||
Assert.AreEqual(expectedSw, actualSw); | ||
} | ||
} | ||
} |
Oops, something went wrong.