Files
SabreTools.IO/SabreTools.IO.Extensions.Test/BinaryWriterExtensionsTests.cs

119 lines
3.9 KiB
C#
Raw Permalink Normal View History

2024-04-25 14:05:06 -04:00
using System;
using System.IO;
using System.Linq;
using System.Numerics;
using Xunit;
2026-03-18 15:47:44 -04:00
namespace SabreTools.IO.Extensions.Test
2024-04-25 14:05:06 -04:00
{
public class BinaryWriterExtensionsTests
{
2024-04-25 20:24:46 -04:00
/// <summary>
/// Test pattern from 0x00-0x0F
/// </summary>
2024-04-25 14:05:06 -04:00
private static readonly byte[] _bytes =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
];
2024-11-25 16:15:12 -05:00
[Fact]
public void WriteTypeTest()
{
// Guid
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
2026-01-27 08:51:29 -05:00
bool actual = bw.WriteType(new Guid(_bytes));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes(_bytes, stream.GetBuffer());
// Half
stream = new MemoryStream(new byte[2], 0, 2, true, true);
bw = new BinaryWriter(stream);
2026-01-27 08:51:29 -05:00
actual = bw.WriteType(BitConverter.Int16BitsToHalf(0x0100));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes([.. _bytes.Take(2)], stream.GetBuffer());
// Int128
stream = new MemoryStream(new byte[16], 0, 16, true, true);
bw = new BinaryWriter(stream);
2026-01-27 08:51:29 -05:00
actual = bw.WriteType((Int128)new BigInteger(_bytes));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes(_bytes, stream.GetBuffer());
// UInt128
stream = new MemoryStream(new byte[16], 0, 16, true, true);
bw = new BinaryWriter(stream);
2026-01-27 08:51:29 -05:00
actual = bw.WriteType((UInt128)new BigInteger(_bytes));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes(_bytes, stream.GetBuffer());
// Enum
stream = new MemoryStream(new byte[4], 0, 4, true, true);
bw = new BinaryWriter(stream);
2026-01-27 08:51:29 -05:00
actual = bw.WriteType((TestEnum)0x03020100);
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes([.. _bytes.Take(4)], stream.GetBuffer());
}
2024-04-25 20:36:44 -04:00
[Fact]
public void WriteTypeExplicitTest()
{
2024-04-29 15:02:51 -04:00
byte[] bytesWithString =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x41, 0x42, 0x43, 0x00,
];
2024-04-25 20:36:44 -04:00
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
var bw = new BinaryWriter(stream);
var obj = new TestStructExplicit
{
FirstValue = TestEnum.RecognizedTestValue,
2024-04-25 20:36:44 -04:00
SecondValue = 0x07060504,
2024-04-29 15:02:51 -04:00
FifthValue = "ABC",
2024-04-25 20:36:44 -04:00
};
2025-11-13 08:56:30 -05:00
byte[] expected = [.. bytesWithString.Take(12)];
2024-04-25 20:36:44 -04:00
bool write = bw.WriteType(obj);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
[Fact]
public void WriteTypeSequentialTest()
{
2024-04-29 15:02:51 -04:00
byte[] bytesWithString =
[
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x41, 0x42, 0x43, 0x00,
];
2024-04-28 16:55:16 -04:00
var stream = new MemoryStream(new byte[24], 0, count: 24, true, true);
2024-04-25 20:36:44 -04:00
var bw = new BinaryWriter(stream);
var obj = new TestStructSequential
{
FirstValue = TestEnum.RecognizedTestValue,
2024-04-25 20:36:44 -04:00
SecondValue = 0x07060504,
ThirdValue = 0x0908,
FourthValue = 0x0B0A,
2024-04-29 15:02:51 -04:00
FifthValue = "ABC",
2024-04-25 20:36:44 -04:00
};
2025-11-13 08:56:30 -05:00
byte[] expected = [.. bytesWithString.Take(16)];
2024-04-25 20:36:44 -04:00
bool write = bw.WriteType(obj);
Assert.True(write);
ValidateBytes(expected, stream.GetBuffer());
}
2024-04-25 14:05:06 -04:00
/// <summary>
/// Validate that a set of actual bytes matches the expected bytes
/// </summary>
private static void ValidateBytes(byte[] expected, byte[] actual)
2024-04-25 14:05:06 -04:00
{
for (int i = 0; i < expected.Length; i++)
{
Assert.Equal(expected[i], actual[i]);
}
}
}
}