using System; using System.IO; using System.Linq; using System.Numerics; using Xunit; namespace SabreTools.IO.Extensions.Test { public class StreamWriterExtensionsTests { /// /// Test pattern from 0x00-0x0F /// private static readonly byte[] _bytes = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, ]; [Fact] public void WriteTypeTest() { // Guid var stream = new MemoryStream(new byte[16], 0, 16, true, true); bool actual = stream.WriteType(new Guid(_bytes)); Assert.True(actual); ValidateBytes(_bytes, stream.GetBuffer()); // Half stream = new MemoryStream(new byte[2], 0, 2, true, true); actual = stream.WriteType(BitConverter.Int16BitsToHalf(0x0100)); Assert.True(actual); ValidateBytes([.. _bytes.Take(2)], stream.GetBuffer()); // Int128 stream = new MemoryStream(new byte[16], 0, 16, true, true); actual = stream.WriteType((Int128)new BigInteger(_bytes)); Assert.True(actual); ValidateBytes(_bytes, stream.GetBuffer()); // UInt128 stream = new MemoryStream(new byte[16], 0, 16, true, true); actual = stream.WriteType((UInt128)new BigInteger(_bytes)); Assert.True(actual); ValidateBytes(_bytes, stream.GetBuffer()); // Enum stream = new MemoryStream(new byte[4], 0, 4, true, true); actual = stream.WriteType((TestEnum)0x03020100); Assert.True(actual); ValidateBytes([.. _bytes.Take(4)], stream.GetBuffer()); } [Fact] public void WriteTypeExplicitTest() { byte[] bytesWithString = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x41, 0x42, 0x43, 0x00, ]; var stream = new MemoryStream(new byte[16], 0, 16, true, true); var obj = new TestStructExplicit { FirstValue = TestEnum.RecognizedTestValue, SecondValue = 0x07060504, FifthValue = "ABC", }; byte[] expected = [.. bytesWithString.Take(12)]; bool write = stream.WriteType(obj); Assert.True(write); ValidateBytes(expected, stream.GetBuffer()); } [Fact] public void WriteTypeSequentialTest() { byte[] bytesWithString = [ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x41, 0x42, 0x43, 0x00, ]; var stream = new MemoryStream(new byte[24], 0, 24, true, true); var obj = new TestStructSequential { FirstValue = TestEnum.RecognizedTestValue, SecondValue = 0x07060504, ThirdValue = 0x0908, FourthValue = 0x0B0A, FifthValue = "ABC", }; byte[] expected = [.. bytesWithString.Take(16)]; bool write = stream.WriteType(obj); Assert.True(write); ValidateBytes(expected, stream.GetBuffer()); } /// /// Validate that a set of actual bytes matches the expected bytes /// private static void ValidateBytes(byte[] expected, byte[] actual) { for (int i = 0; i < expected.Length; i++) { Assert.Equal(expected[i], actual[i]); } } } }