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

118 lines
3.6 KiB
C#
Raw Permalink Normal View History

2024-04-25 14:05:06 -04:00
using System;
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
{
2024-11-25 15:03:07 -05:00
public class ByteArrayWriterExtensionsTests
2024-04-25 14:05:06 -04:00
{
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
int offset = 0;
byte[] buffer = new byte[16];
2026-01-27 08:51:29 -05:00
bool actual = buffer.WriteType(ref offset, new Guid(_bytes));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes(_bytes, buffer);
// Half
offset = 0;
buffer = new byte[2];
2026-01-27 08:51:29 -05:00
actual = buffer.WriteType(ref offset, BitConverter.Int16BitsToHalf(0x0100));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes([.. _bytes.Take(2)], buffer);
// Int128
offset = 0;
buffer = new byte[16];
2026-01-27 08:51:29 -05:00
actual = buffer.WriteType(ref offset, (Int128)new BigInteger(_bytes));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes(_bytes, buffer);
// UInt128
offset = 0;
buffer = new byte[16];
2026-01-27 08:51:29 -05:00
actual = buffer.WriteType(ref offset, (UInt128)new BigInteger(_bytes));
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes(_bytes, buffer);
// Enum
offset = 0;
buffer = new byte[4];
2026-01-27 08:51:29 -05:00
actual = buffer.WriteType(ref offset, (TestEnum)0x03020100);
2024-11-25 16:15:12 -05:00
Assert.True(actual);
ValidateBytes([.. _bytes.Take(4)], buffer);
}
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
byte[] buffer = new byte[16];
int offset = 0;
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 = buffer.WriteType(ref offset, obj);
Assert.True(write);
ValidateBytes(expected, buffer);
}
[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
byte[] buffer = new byte[24];
2024-04-25 20:36:44 -04:00
int offset = 0;
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 = buffer.WriteType(ref offset, obj);
Assert.True(write);
ValidateBytes(expected, buffer);
}
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]);
}
}
}
}