mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-04-30 10:50:09 +00:00
264 lines
9.6 KiB
C#
264 lines
9.6 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Numerics;
|
|
using Xunit;
|
|
|
|
namespace SabreTools.IO.Extensions.Test
|
|
{
|
|
public class BinaryReaderExtensionsTests
|
|
{
|
|
/// <summary>
|
|
/// Test pattern from 0x00-0x0F
|
|
/// </summary>
|
|
private static readonly byte[] _bytes =
|
|
[
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
|
];
|
|
|
|
[Fact]
|
|
public void ReadTypeTest()
|
|
{
|
|
// Guid
|
|
var stream = new MemoryStream(_bytes);
|
|
var br = new BinaryReader(stream);
|
|
var expectedGuid = new Guid(_bytes);
|
|
Guid actualGuid = br.ReadType<Guid>();
|
|
Assert.Equal(expectedGuid, actualGuid);
|
|
|
|
// Half
|
|
stream = new MemoryStream(_bytes);
|
|
br = new BinaryReader(stream);
|
|
Half expectedHalf = BitConverter.Int16BitsToHalf(0x0100);
|
|
Half actualHalf = br.ReadType<Half>();
|
|
Assert.Equal(expectedHalf, actualHalf);
|
|
|
|
// Int128
|
|
stream = new MemoryStream(_bytes);
|
|
br = new BinaryReader(stream);
|
|
Int128 expectedInt128 = (Int128)new BigInteger(_bytes);
|
|
Int128 actualInt128 = br.ReadType<Int128>();
|
|
Assert.Equal(expectedInt128, actualInt128);
|
|
|
|
// UInt128
|
|
stream = new MemoryStream(_bytes);
|
|
br = new BinaryReader(stream);
|
|
UInt128 expectedUInt128 = (UInt128)new BigInteger(_bytes);
|
|
UInt128 actualUInt128 = br.ReadType<UInt128>();
|
|
Assert.Equal(expectedUInt128, actualUInt128);
|
|
|
|
// Enum
|
|
stream = new MemoryStream(_bytes);
|
|
br = new BinaryReader(stream);
|
|
TestEnum expectedTestEnum = (TestEnum)0x03020100;
|
|
TestEnum actualTestEnum = br.ReadType<TestEnum>();
|
|
Assert.Equal(expectedTestEnum, actualTestEnum);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadTypeExplicitTest()
|
|
{
|
|
byte[] bytesWithString =
|
|
[
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
0x41, 0x42, 0x43, 0x00,
|
|
];
|
|
|
|
var stream = new MemoryStream(bytesWithString);
|
|
var br = new BinaryReader(stream);
|
|
var expected = new TestStructExplicit
|
|
{
|
|
FirstValue = TestEnum.RecognizedTestValue,
|
|
SecondValue = 0x07060504,
|
|
ThirdValue = 0x0504,
|
|
FourthValue = 0x0706,
|
|
FifthValue = "ABC",
|
|
};
|
|
var read = br.ReadType<TestStructExplicit>();
|
|
Assert.Equal(expected.FirstValue, read.FirstValue);
|
|
Assert.Equal(expected.SecondValue, read.SecondValue);
|
|
Assert.Equal(expected.ThirdValue, read.ThirdValue);
|
|
Assert.Equal(expected.FourthValue, read.FourthValue);
|
|
Assert.Equal(expected.FifthValue, read.FifthValue);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadTypeSequentialTest()
|
|
{
|
|
byte[] bytesWithString =
|
|
[
|
|
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
0x08, 0x09, 0x0A, 0x0B, 0x41, 0x42, 0x43, 0x00,
|
|
];
|
|
|
|
var stream = new MemoryStream(bytesWithString);
|
|
var br = new BinaryReader(stream);
|
|
var expected = new TestStructSequential
|
|
{
|
|
FirstValue = TestEnum.RecognizedTestValue,
|
|
SecondValue = 0x07060504,
|
|
ThirdValue = 0x0908,
|
|
FourthValue = 0x0B0A,
|
|
FifthValue = "ABC",
|
|
};
|
|
var read = br.ReadType<TestStructSequential>();
|
|
Assert.Equal(expected.FirstValue, read.FirstValue);
|
|
Assert.Equal(expected.SecondValue, read.SecondValue);
|
|
Assert.Equal(expected.ThirdValue, read.ThirdValue);
|
|
Assert.Equal(expected.FourthValue, read.FourthValue);
|
|
Assert.Equal(expected.FifthValue, read.FifthValue);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadTypeStringsTest()
|
|
{
|
|
byte[] structBytes =
|
|
[
|
|
0x03, 0x41, 0x42, 0x43, // AnsiBStr
|
|
0x03, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, // BStr
|
|
0x41, 0x42, 0x43, // ByValTStr
|
|
0x41, 0x42, 0x43, 0x00, // LPStr
|
|
0x41, 0x00, 0x42, 0x00, 0x43, 0x00, 0x00, 0x00, // LPWStr
|
|
];
|
|
|
|
var stream = new MemoryStream(structBytes);
|
|
var br = new BinaryReader(stream);
|
|
var expected = new TestStructStrings
|
|
{
|
|
AnsiBStr = "ABC",
|
|
BStr = "ABC",
|
|
ByValTStr = "ABC",
|
|
LPStr = "ABC",
|
|
LPWStr = "ABC",
|
|
};
|
|
var read = br.ReadType<TestStructStrings>();
|
|
Assert.Equal(expected.AnsiBStr, read.AnsiBStr);
|
|
Assert.Equal(expected.BStr, read.BStr);
|
|
Assert.Equal(expected.ByValTStr, read.ByValTStr);
|
|
Assert.Equal(expected.LPStr, read.LPStr);
|
|
Assert.Equal(expected.LPWStr, read.LPWStr);
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadTypeArraysTest()
|
|
{
|
|
byte[] structBytes =
|
|
[
|
|
// Byte Array
|
|
0x00, 0x01, 0x02, 0x03,
|
|
|
|
// Int Array
|
|
0x03, 0x02, 0x01, 0x00,
|
|
0x04, 0x03, 0x02, 0x01,
|
|
0x05, 0x04, 0x03, 0x02,
|
|
0x06, 0x05, 0x04, 0x03,
|
|
|
|
// Enum Array
|
|
0x03, 0x02, 0x01, 0x00,
|
|
0x04, 0x03, 0x02, 0x01,
|
|
0x05, 0x04, 0x03, 0x02,
|
|
0x06, 0x05, 0x04, 0x03,
|
|
|
|
// Struct Array (X, Y)
|
|
0xFF, 0x00, 0x00, 0xFF,
|
|
0x00, 0xFF, 0xFF, 0x00,
|
|
0xAA, 0x55, 0x55, 0xAA,
|
|
0x55, 0xAA, 0xAA, 0x55,
|
|
|
|
// LPArray
|
|
0x04, 0x00,
|
|
0x00, 0x01, 0x02, 0x03,
|
|
];
|
|
|
|
var stream = new MemoryStream(structBytes);
|
|
var br = new BinaryReader(stream);
|
|
var expected = new TestStructArrays
|
|
{
|
|
ByteArray = [0x00, 0x01, 0x02, 0x03],
|
|
IntArray = [0x00010203, 0x01020304, 0x02030405, 0x03040506],
|
|
EnumArray =
|
|
[
|
|
(TestEnum)0x00010203,
|
|
(TestEnum)0x01020304,
|
|
(TestEnum)0x02030405,
|
|
(TestEnum)0x03040506,
|
|
],
|
|
StructArray =
|
|
[
|
|
new TestStructPoint { X = 0x00FF, Y = 0xFF00 },
|
|
new TestStructPoint { X = 0xFF00, Y = 0x00FF },
|
|
new TestStructPoint { X = 0x55AA, Y = 0xAA55 },
|
|
new TestStructPoint { X = 0xAA55, Y = 0x55AA },
|
|
],
|
|
LPByteArrayLength = 0x0004,
|
|
LPByteArray = [0x00, 0x01, 0x02, 0x03],
|
|
};
|
|
var read = br.ReadType<TestStructArrays>();
|
|
Assert.NotNull(read.ByteArray);
|
|
Assert.True(expected.ByteArray.SequenceEqual(read.ByteArray));
|
|
Assert.NotNull(read.IntArray);
|
|
Assert.True(expected.IntArray.SequenceEqual(read.IntArray));
|
|
Assert.NotNull(read.EnumArray);
|
|
Assert.True(expected.EnumArray.SequenceEqual(read.EnumArray));
|
|
Assert.NotNull(read.StructArray);
|
|
Assert.True(expected.StructArray.SequenceEqual(read.StructArray));
|
|
Assert.Equal(expected.LPByteArrayLength, read.LPByteArrayLength);
|
|
Assert.NotNull(read.LPByteArray);
|
|
Assert.True(expected.LPByteArray.SequenceEqual(read.LPByteArray));
|
|
}
|
|
|
|
[Fact]
|
|
public void ReadTypeInheritanceTest()
|
|
{
|
|
byte[] structBytes1 =
|
|
[
|
|
0x41, 0x42, 0x43, 0x44, // Signature
|
|
0x00, 0xFF, 0x00, 0xFF, // IdentifierType
|
|
0xAA, 0x55, 0xAA, 0x55, // FieldA
|
|
0x55, 0xAA, 0x55, 0xAA, // FieldB
|
|
];
|
|
|
|
var stream1 = new MemoryStream(structBytes1);
|
|
var br1 = new BinaryReader(stream1);
|
|
var expected1 = new TestStructInheritanceChild1
|
|
{
|
|
Signature = [0x41, 0x42, 0x43, 0x44],
|
|
IdentifierType = 0xFF00FF00,
|
|
FieldA = 0x55AA55AA,
|
|
FieldB = 0xAA55AA55,
|
|
};
|
|
var read1 = br1.ReadType<TestStructInheritanceChild1>();
|
|
Assert.NotNull(read1?.Signature);
|
|
Assert.Equal(expected1.Signature, read1.Signature);
|
|
Assert.Equal(expected1.IdentifierType, read1.IdentifierType);
|
|
Assert.Equal(expected1.FieldA, read1.FieldA);
|
|
Assert.Equal(expected1.FieldB, read1.FieldB);
|
|
|
|
byte[] structBytes2 =
|
|
[
|
|
0x41, 0x42, 0x43, 0x44, // Signature
|
|
0x00, 0xFF, 0x00, 0xFF, // IdentifierType
|
|
0xAA, 0x55, // FieldA
|
|
0x55, 0xAA, // FieldB
|
|
];
|
|
|
|
var stream2 = new MemoryStream(structBytes2);
|
|
var br2 = new BinaryReader(stream2);
|
|
var expected2 = new TestStructInheritanceChild2
|
|
{
|
|
Signature = [0x41, 0x42, 0x43, 0x44],
|
|
IdentifierType = 0xFF00FF00,
|
|
FieldA = 0x55AA,
|
|
FieldB = 0xAA55,
|
|
};
|
|
var read2 = br2.ReadType<TestStructInheritanceChild2>();
|
|
Assert.NotNull(read2?.Signature);
|
|
Assert.Equal(expected2.Signature, read2.Signature);
|
|
Assert.Equal(expected2.IdentifierType, read2.IdentifierType);
|
|
Assert.Equal(expected2.FieldA, read2.FieldA);
|
|
Assert.Equal(expected2.FieldB, read2.FieldB);
|
|
}
|
|
}
|
|
}
|