From 245ca9010a09f8b9f831b8df476b7019b28286f5 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Thu, 25 Apr 2024 20:24:46 -0400 Subject: [PATCH] Add decimal write tests --- .../Extensions/BinaryWriterExtensionsTests.cs | 34 +++++++++++++++++- .../ByteArrayExtensionsWriteTests.cs | 35 ++++++++++++++++++- .../Extensions/StreamExtensionsWriteTests.cs | 33 ++++++++++++++++- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/SabreTools.IO.Test/Extensions/BinaryWriterExtensionsTests.cs b/SabreTools.IO.Test/Extensions/BinaryWriterExtensionsTests.cs index b3dce25..bb0bd22 100644 --- a/SabreTools.IO.Test/Extensions/BinaryWriterExtensionsTests.cs +++ b/SabreTools.IO.Test/Extensions/BinaryWriterExtensionsTests.cs @@ -11,16 +11,27 @@ using Xunit; namespace SabreTools.IO.Test.Extensions { // TODO: Add byte[], char[] tests - // TODO: Add decimal tests // TODO: Add string writing tests public class BinaryWriterExtensionsTests { + /// + /// 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, ]; + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + [Fact] public void WriteByteValueTest() { @@ -346,6 +357,27 @@ namespace SabreTools.IO.Test.Extensions ValidateBytes(expected, stream.GetBuffer()); } + [Fact] + public void WriteDecimalTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = _decimalBytes.Take(16).ToArray(); + bw.Write(0.0123456789M); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + var bw = new BinaryWriter(stream); + byte[] expected = _decimalBytes.Take(16).Reverse().ToArray(); + bool write = bw.WriteBigEndian(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + [Fact] public void WriteGuidTest() { diff --git a/SabreTools.IO.Test/Extensions/ByteArrayExtensionsWriteTests.cs b/SabreTools.IO.Test/Extensions/ByteArrayExtensionsWriteTests.cs index 01004fe..f5d4512 100644 --- a/SabreTools.IO.Test/Extensions/ByteArrayExtensionsWriteTests.cs +++ b/SabreTools.IO.Test/Extensions/ByteArrayExtensionsWriteTests.cs @@ -8,16 +8,27 @@ using Xunit; namespace SabreTools.IO.Test.Extensions { - // TODO: Add decimal tests // TODO: Add string writing tests public class ByteArrayExtensionsWriteTests { + /// + /// 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, ]; + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + [Fact] public void WriteByteTest() { @@ -328,6 +339,28 @@ namespace SabreTools.IO.Test.Extensions ValidateBytes(expected, buffer); } + [Fact] + public void WriteDecimalTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = _decimalBytes.Take(16).ToArray(); + bool write = buffer.Write(ref offset, 0.0123456789M); + Assert.True(write); + ValidateBytes(expected, buffer); + } + + [Fact] + public void WriteDecimalBigEndianTest() + { + byte[] buffer = new byte[16]; + int offset = 0; + byte[] expected = _decimalBytes.Take(16).Reverse().ToArray(); + bool write = buffer.WriteBigEndian(ref offset, 0.0123456789M); + Assert.True(write); + ValidateBytes(expected, buffer); + } + [Fact] public void WriteGuidTest() { diff --git a/SabreTools.IO.Test/Extensions/StreamExtensionsWriteTests.cs b/SabreTools.IO.Test/Extensions/StreamExtensionsWriteTests.cs index a8da03f..af932cf 100644 --- a/SabreTools.IO.Test/Extensions/StreamExtensionsWriteTests.cs +++ b/SabreTools.IO.Test/Extensions/StreamExtensionsWriteTests.cs @@ -9,16 +9,27 @@ using Xunit; namespace SabreTools.IO.Test.Extensions { - // TODO: Add decimal tests // TODO: Add string writing tests public class StreamExtensionsWriteTests { + /// + /// 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, ]; + /// + /// Represents the decimal value 0.0123456789 + /// + private static readonly byte[] _decimalBytes = + [ + 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0A, 0x00, + ]; + [Fact] public void WriteByteValueTest() { @@ -301,6 +312,26 @@ namespace SabreTools.IO.Test.Extensions ValidateBytes(expected, stream.GetBuffer()); } + [Fact] + public void WriteDecimalTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = _decimalBytes.Take(16).ToArray(); + bool write = stream.Write(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + + [Fact] + public void WriteDecimalBigEndianTest() + { + var stream = new MemoryStream(new byte[16], 0, 16, true, true); + byte[] expected = _decimalBytes.Take(16).Reverse().ToArray(); + bool write = stream.WriteBigEndian(0.0123456789M); + Assert.True(write); + ValidateBytes(expected, stream.GetBuffer()); + } + [Fact] public void WriteGuidTest() {