From 5c21c86a5207cb935287657a19dd8d45b4528c34 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 23 Aug 2023 14:15:52 +0100 Subject: [PATCH] refactor!: replace Endianness enum with explicit Big/Little methods --- CHANGELOG.md | 3 + X10D.Tests/src/IO/DoubleTests.cs | 58 +- X10D.Tests/src/IO/Int16Tests.cs | 54 +- X10D.Tests/src/IO/Int32Tests.cs | 54 +- X10D.Tests/src/IO/Int64Tests.cs | 58 +- X10D.Tests/src/IO/SingleTests.cs | 58 +- X10D.Tests/src/IO/StreamTests.ReadDecimal.cs | 53 +- X10D.Tests/src/IO/StreamTests.ReadDouble.cs | 57 +- X10D.Tests/src/IO/StreamTests.ReadInt16.cs | 57 +- X10D.Tests/src/IO/StreamTests.ReadInt32.cs | 57 +- X10D.Tests/src/IO/StreamTests.ReadInt64.cs | 52 +- X10D.Tests/src/IO/StreamTests.ReadSingle.cs | 57 +- X10D.Tests/src/IO/StreamTests.ReadUInt16.cs | 57 +- X10D.Tests/src/IO/StreamTests.ReadUInt32.cs | 57 +- X10D.Tests/src/IO/StreamTests.ReadUInt64.cs | 57 +- X10D.Tests/src/IO/StreamTests.WriteDecimal.cs | 52 +- X10D.Tests/src/IO/StreamTests.WriteDouble.cs | 56 +- X10D.Tests/src/IO/StreamTests.WriteInt16.cs | 56 +- X10D.Tests/src/IO/StreamTests.WriteInt32.cs | 56 +- X10D.Tests/src/IO/StreamTests.WriteInt64.cs | 56 +- X10D.Tests/src/IO/StreamTests.WriteSingle.cs | 56 +- X10D.Tests/src/IO/StreamTests.WriteUInt16.cs | 56 +- X10D.Tests/src/IO/StreamTests.WriteUInt32.cs | 56 +- X10D.Tests/src/IO/StreamTests.WriteUInt64.cs | 56 +- X10D.Tests/src/IO/UInt16Tests.cs | 54 +- X10D.Tests/src/IO/UInt32Tests.cs | 54 +- X10D.Tests/src/IO/UInt64Tests.cs | 58 +- X10D/src/Endianness.cs | 19 - X10D/src/IO/DecimalExtensions.cs | 111 ++ X10D/src/IO/DoubleExtensions.cs | 82 +- X10D/src/IO/Int16Extensions.cs | 76 +- X10D/src/IO/Int32Extensions.cs | 60 +- X10D/src/IO/Int64Extensions.cs | 36 +- X10D/src/IO/SingleExtensions.cs | 70 +- X10D/src/IO/StreamExtensions.cs | 1585 ++++++++--------- X10D/src/IO/UInt16Extensions.cs | 40 +- X10D/src/IO/UInt32Extensions.cs | 40 +- X10D/src/IO/UInt64Extensions.cs | 36 +- 38 files changed, 1835 insertions(+), 1775 deletions(-) delete mode 100644 X10D/src/Endianness.cs create mode 100644 X10D/src/IO/DecimalExtensions.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 14b8711..1957cf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,10 +34,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - X10D: DateTime.Age(DateTime) and DateTimeOffset.Age(DateTimeOffset) parameter renamed from asOf to referenceDate. +- X10D: Methods which accepted the `Endianness` enum as an argument have been replaced with explicit +BigEndian/LittleEndian methods. ### Removed - X10D: Removed `IEnumerable.ConcatOne` - this functionality already exists with `Append`. +- X10D: Removed `Endianness` enum. - X10D.DSharpPlus: Complete sunset of library. This library will not be updated to support DSharpPlus v5.0.0. ## [3.3.1] - 2023-08-21 diff --git a/X10D.Tests/src/IO/DoubleTests.cs b/X10D.Tests/src/IO/DoubleTests.cs index f58e26c..9b8a43a 100644 --- a/X10D.Tests/src/IO/DoubleTests.cs +++ b/X10D.Tests/src/IO/DoubleTests.cs @@ -7,60 +7,60 @@ namespace X10D.Tests.IO; internal class DoubleTests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetBigEndianBytes_ReturnsCorrectValue() { const double value = 42.5; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0, 0, 0, 0, 0, 0x40, 0x45, 0x40} - : new byte[] {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + + var expected = new byte[] { 0x40, 0x45, 0x40, 0, 0, 0, 0, 0 }; + byte[] actual = value.GetBigEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetLittleEndianBytes_ReturnsCorrectValue() { const double value = 42.5; - byte[] littleEndian = {0, 0, 0, 0, 0, 0x40, 0x45, 0x40}; - byte[] bigEndian = {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + var expected = new byte[] { 0, 0, 0, 0, 0, 0x40, 0x45, 0x40 }; + byte[] actual = value.GetLittleEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly() { const double value = 42.5; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0, 0, 0, 0, 0, 0x40, 0x45, 0x40} - : new byte[] {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; - Span buffer = stackalloc byte[8]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + var expected = new byte[] { 0x40, 0x45, 0x40, 0, 0, 0, 0, 0 }; + Span actual = stackalloc byte[8]; + Assert.That(value.TryWriteBigEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly() { const double value = 42.5; - byte[] littleEndian = {0, 0, 0, 0, 0, 0x40, 0x45, 0x40}; - byte[] bigEndian = {0x40, 0x45, 0x40, 0, 0, 0, 0, 0}; - Span buffer = stackalloc byte[8]; - - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + var expected = new byte[] { 0, 0, 0, 0, 0, 0x40, 0x45, 0x40 }; + Span actual = stackalloc byte[8]; + Assert.That(value.TryWriteLittleEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() { const double value = 42.5; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteBigEndian(buffer), Is.False); + } + + [Test] + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() + { + const double value = 42.5; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); } } diff --git a/X10D.Tests/src/IO/Int16Tests.cs b/X10D.Tests/src/IO/Int16Tests.cs index 11d6bb1..8d64f97 100644 --- a/X10D.Tests/src/IO/Int16Tests.cs +++ b/X10D.Tests/src/IO/Int16Tests.cs @@ -7,56 +7,60 @@ namespace X10D.Tests.IO; internal class Int16Tests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetLittleEndianBytes_ReturnsCorrectValue() { const short value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + + byte[] expected = { 0x0F, 0 }; + byte[] actual = value.GetLittleEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetBigEndianBytes_ReturnsCorrectValue() { const short value = 0x0F; - byte[] littleEndian = {0x0F, 0}; - byte[] bigEndian = {0, 0x0F}; - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + byte[] expected = { 0, 0x0F }; + byte[] actual = value.GetBigEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly() { const short value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; - Span buffer = stackalloc byte[2]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + byte[] expected = { 0x0F, 0 }; + Span actual = stackalloc byte[2]; + Assert.That(value.TryWriteLittleEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly() { const short value = 0x0F; - byte[] littleEndian = {0x0F, 0}; - byte[] bigEndian = {0, 0x0F}; - Span buffer = stackalloc byte[2]; - - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + byte[] expected = { 0, 0x0F }; + Span actual = stackalloc byte[2]; + Assert.That(value.TryWriteBigEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() { const short value = 0x0F; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); + } + + [Test] + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() + { + const short value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteBigEndian(buffer), Is.False); } } diff --git a/X10D.Tests/src/IO/Int32Tests.cs b/X10D.Tests/src/IO/Int32Tests.cs index c123980..31898a0 100644 --- a/X10D.Tests/src/IO/Int32Tests.cs +++ b/X10D.Tests/src/IO/Int32Tests.cs @@ -7,56 +7,60 @@ namespace X10D.Tests.IO; internal class Int32Tests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetBigEndianBytes_ReturnsCorrectValue() { const int value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + + var expected = new byte[] { 0, 0, 0, 0x0F }; + byte[] actual = value.GetBigEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetLittleEndianBytes_ReturnsCorrectValue() { const int value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0x0F}; - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + var expected = new byte[] { 0x0F, 0, 0, 0 }; + byte[] actual = value.GetLittleEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly() { const int value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; - Span buffer = stackalloc byte[4]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + var expected = new byte[] { 0, 0, 0, 0x0F }; + Span actual = stackalloc byte[4]; + Assert.That(value.TryWriteBigEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly() { const int value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0x0F}; - Span buffer = stackalloc byte[4]; - - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + var expected = new byte[] { 0x0F, 0, 0, 0 }; + Span actual = stackalloc byte[4]; + Assert.That(value.TryWriteLittleEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() { const int value = 0x0F; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteBigEndian(buffer), Is.False); + } + + [Test] + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() + { + const int value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); } } diff --git a/X10D.Tests/src/IO/Int64Tests.cs b/X10D.Tests/src/IO/Int64Tests.cs index 6183d13..393b9e0 100644 --- a/X10D.Tests/src/IO/Int64Tests.cs +++ b/X10D.Tests/src/IO/Int64Tests.cs @@ -7,60 +7,60 @@ namespace X10D.Tests.IO; internal class Int64Tests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetLittleEndianBytes_ReturnsCorrectValue() { const long value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} - : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + + byte[] expected = { 0x0F, 0, 0, 0, 0, 0, 0, 0 }; + byte[] actual = value.GetLittleEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetBigEndianBytes_ReturnsCorrectValue() { const long value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 0x0F }; + byte[] actual = value.GetBigEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly() { const long value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} - : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; - Span buffer = stackalloc byte[8]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + byte[] expected = { 0x0F, 0, 0, 0, 0, 0, 0, 0 }; + Span actual = stackalloc byte[8]; + Assert.That(value.TryWriteLittleEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly() { const long value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; - Span buffer = stackalloc byte[8]; - - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 0x0F }; + Span actual = stackalloc byte[8]; + Assert.That(value.TryWriteBigEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() { const long value = 0x0F; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); + } + + [Test] + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() + { + const long value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteBigEndian(buffer), Is.False); } } diff --git a/X10D.Tests/src/IO/SingleTests.cs b/X10D.Tests/src/IO/SingleTests.cs index 5f753c4..e7de74d 100644 --- a/X10D.Tests/src/IO/SingleTests.cs +++ b/X10D.Tests/src/IO/SingleTests.cs @@ -7,60 +7,60 @@ namespace X10D.Tests.IO; internal class SingleTests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetBigEndianBytes_ReturnsCorrectValue() { const float value = 42.5f; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0, 0, 0x2A, 0x42} - : new byte[] {0x42, 0x2A, 0, 0}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + + var expected = new byte[] { 0x42, 0x2A, 0, 0 }; + byte[] actual = value.GetBigEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetLittleEndianBytes_ReturnsCorrectValue() { const float value = 42.5f; - byte[] littleEndian = {0, 0, 0x2A, 0x42}; - byte[] bigEndian = {0x42, 0x2A, 0, 0}; - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + var expected = new byte[] { 0, 0, 0x2A, 0x42 }; + byte[] actual = value.GetLittleEndianBytes(); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly() { const float value = 42.5f; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0, 0, 0x2A, 0x42} - : new byte[] {0x42, 0x2A, 0, 0}; - Span buffer = stackalloc byte[4]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + var expected = new byte[] { 0x42, 0x2A, 0, 0 }; + Span actual = stackalloc byte[4]; + Assert.That(value.TryWriteBigEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly() { const float value = 42.5f; - byte[] littleEndian = {0, 0, 0x2A, 0x42}; - byte[] bigEndian = {0x42, 0x2A, 0, 0}; - Span buffer = stackalloc byte[4]; - - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + var expected = new byte[] { 0, 0, 0x2A, 0x42 }; + Span actual = stackalloc byte[4]; + Assert.That(value.TryWriteLittleEndian(actual)); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() { const float value = 42.5f; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteBigEndian(buffer), Is.False); + } + + [Test] + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() + { + const float value = 42.5f; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); } } diff --git a/X10D.Tests/src/IO/StreamTests.ReadDecimal.cs b/X10D.Tests/src/IO/StreamTests.ReadDecimal.cs index 7f61b76..c7fcdb5 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadDecimal.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadDecimal.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,37 +7,37 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadDecimal_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadDecimal()); - Assert.Throws(() => stream.ReadDecimal(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadDecimal(Endianness.BigEndian)); - } - - [Test] - public void ReadDecimal_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadDecimalBigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadDecimal()); - Assert.Throws(() => stream.ReadDecimal(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadDecimal(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadDecimalBigEndian()); } [Test] - public void ReadDecimal_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadDecimalLittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadDecimal((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadDecimalLittleEndian()); } [Test] - public void ReadDecimal_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadDecimalBigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadDecimalBigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadDecimalLittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadDecimalLittleEndian()); + } + + [Test] + public void ReadDecimalBigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); ReadOnlySpan bytes = stackalloc byte[] @@ -47,7 +48,7 @@ internal partial class StreamTests stream.Position = 0; const decimal expected = 420.0m; - decimal actual = stream.ReadDecimal(Endianness.BigEndian); + decimal actual = stream.ReadDecimalBigEndian(); Assert.Multiple(() => { @@ -57,7 +58,7 @@ internal partial class StreamTests } [Test] - public void ReadDecimal_ShouldWriteLittleEndian_GivenLittleEndian() + public void ReadDecimalLittleEndian_ShouldWriteLittleEndian() { using var stream = new MemoryStream(); ReadOnlySpan bytes = stackalloc byte[] @@ -68,7 +69,7 @@ internal partial class StreamTests stream.Position = 0; const decimal expected = 420.0m; - decimal actual = stream.ReadDecimal(Endianness.LittleEndian); + decimal actual = stream.ReadDecimalLittleEndian(); Assert.Multiple(() => { diff --git a/X10D.Tests/src/IO/StreamTests.ReadDouble.cs b/X10D.Tests/src/IO/StreamTests.ReadDouble.cs index b400ac1..02f7991 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadDouble.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadDouble.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,60 +7,60 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadDouble_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadDouble()); - Assert.Throws(() => stream.ReadDouble(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadDouble(Endianness.BigEndian)); - } - - [Test] - public void ReadDouble_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadDoubleBigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadDouble()); - Assert.Throws(() => stream.ReadDouble(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadDouble(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadDoubleBigEndian()); } [Test] - public void ReadDouble_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadDoubleLittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadDouble((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadDoubleLittleEndian()); } [Test] - public void ReadDouble_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadDoubleBigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadDoubleBigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadDoubleLittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadDoubleLittleEndian()); + } + + [Test] + public void ReadDoubleBigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x40, 0x7A, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}; + ReadOnlySpan bytes = stackalloc byte[] { 0x40, 0x7A, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00 }; stream.Write(bytes); stream.Position = 0; const double expected = 420.0; - double actual = stream.ReadDouble(Endianness.BigEndian); + double actual = stream.ReadDoubleBigEndian(); Assert.That(stream.Position, Is.EqualTo(8)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadDouble_ShouldWriteLittleEndian_GivenLittleEndian() + public void ReadDoubleLittleEndian_ShouldWriteLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x40}; + ReadOnlySpan bytes = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x40 }; stream.Write(bytes); stream.Position = 0; const double expected = 420.0; - double actual = stream.ReadDouble(Endianness.LittleEndian); + double actual = stream.ReadDoubleLittleEndian(); Assert.That(stream.Position, Is.EqualTo(8)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.ReadInt16.cs b/X10D.Tests/src/IO/StreamTests.ReadInt16.cs index 950f368..a71d4bd 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadInt16.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadInt16.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,60 +7,60 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadInt16_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadInt16()); - Assert.Throws(() => stream.ReadInt16(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadInt16(Endianness.BigEndian)); - } - - [Test] - public void ReadInt16_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadInt16BigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadInt16()); - Assert.Throws(() => stream.ReadInt16(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadInt16(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadInt16BigEndian()); } [Test] - public void ReadInt16_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadInt16LittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadInt16((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadInt16LittleEndian()); } [Test] - public void ReadInt16_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadInt16BigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadInt16BigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadInt16LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadInt16LittleEndian()); + } + + [Test] + public void ReadInt16BigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x01, 0xA4}; + ReadOnlySpan bytes = stackalloc byte[] { 0x01, 0xA4 }; stream.Write(bytes); stream.Position = 0; const short expected = 420; - short actual = stream.ReadInt16(Endianness.BigEndian); + short actual = stream.ReadInt16BigEndian(); Assert.That(stream.Position, Is.EqualTo(2)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadInt16_ShouldReadLittleEndian_GivenLittleEndian() + public void ReadInt16LittleEndian_ShouldReadLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0xA4, 0x01}; + ReadOnlySpan bytes = stackalloc byte[] { 0xA4, 0x01 }; stream.Write(bytes); stream.Position = 0; const short expected = 420; - short actual = stream.ReadInt16(Endianness.LittleEndian); + short actual = stream.ReadInt16LittleEndian(); Assert.That(stream.Position, Is.EqualTo(2)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.ReadInt32.cs b/X10D.Tests/src/IO/StreamTests.ReadInt32.cs index 8b45594..e51ef95 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadInt32.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadInt32.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,60 +7,60 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadInt32_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadInt32()); - Assert.Throws(() => stream.ReadInt32(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadInt32(Endianness.BigEndian)); - } - - [Test] - public void ReadInt32_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadInt32BigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadInt32()); - Assert.Throws(() => stream.ReadInt32(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadInt32(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadInt32BigEndian()); } [Test] - public void ReadInt32_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadInt32LittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadInt32((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadInt32LittleEndian()); } [Test] - public void ReadInt32_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadInt32BigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadInt32BigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadInt32LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadInt32LittleEndian()); + } + + [Test] + public void ReadInt32BigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan bytes = stackalloc byte[] { 0x00, 0x00, 0x01, 0xA4 }; stream.Write(bytes); stream.Position = 0; const int expected = 420; - int actual = stream.ReadInt32(Endianness.BigEndian); + int actual = stream.ReadInt32BigEndian(); Assert.That(stream.Position, Is.EqualTo(4)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadInt32_ShouldReadLittleEndian_GivenLittleEndian() + public void ReadInt32LittleEndian_ShouldReadLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00}; + ReadOnlySpan bytes = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00 }; stream.Write(bytes); stream.Position = 0; const int expected = 420; - int actual = stream.ReadInt32(Endianness.LittleEndian); + int actual = stream.ReadInt32LittleEndian(); Assert.That(stream.Position, Is.EqualTo(4)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.ReadInt64.cs b/X10D.Tests/src/IO/StreamTests.ReadInt64.cs index d094a56..faebfda 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadInt64.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadInt64.cs @@ -6,60 +6,58 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadInt64_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadInt64()); - Assert.Throws(() => stream.ReadInt64(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadInt64(Endianness.BigEndian)); - } - - [Test] - public void ReadInt64_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadInt64BigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadInt64()); - Assert.Throws(() => stream.ReadInt64(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadInt64(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadInt64BigEndian()); } [Test] - public void ReadInt64_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadInt64LittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadInt64((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadInt64LittleEndian()); } [Test] - public void ReadInt64_ShouldReadBigEndian_GivenBigEndian() + public void ReadInt64BigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadInt64BigEndian()); + } + + [Test] + public void ReadInt64LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadInt64LittleEndian()); + } + + [Test] + public void ReadInt64BigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan bytes = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4 }; stream.Write(bytes); stream.Position = 0; const long expected = 420; - long actual = stream.ReadInt64(Endianness.BigEndian); + long actual = stream.ReadInt64BigEndian(); Assert.That(stream.Position, Is.EqualTo(8)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadInt64_ShouldWriteLittleEndian_GivenLittleEndian() + public void ReadInt64LittleEndian_ShouldWriteLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + ReadOnlySpan bytes = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; stream.Write(bytes); stream.Position = 0; const long expected = 420; - long actual = stream.ReadInt64(Endianness.LittleEndian); + long actual = stream.ReadInt64LittleEndian(); Assert.That(stream.Position, Is.EqualTo(8)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.ReadSingle.cs b/X10D.Tests/src/IO/StreamTests.ReadSingle.cs index 7d5a396..ff2336b 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadSingle.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadSingle.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,60 +7,60 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadSingle_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadSingle()); - Assert.Throws(() => stream.ReadSingle(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadSingle(Endianness.BigEndian)); - } - - [Test] - public void ReadSingle_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadSingleBigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadSingle()); - Assert.Throws(() => stream.ReadSingle(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadSingle(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadSingleBigEndian()); } [Test] - public void ReadSingle_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadSingleLittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadSingle((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadSingleLittleEndian()); } [Test] - public void ReadSingle_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadSingleBigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadSingleBigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadSingleLittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadSingleLittleEndian()); + } + + [Test] + public void ReadSingleBigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x43, 0xD2, 0x00, 0x00}; + ReadOnlySpan bytes = stackalloc byte[] { 0x43, 0xD2, 0x00, 0x00 }; stream.Write(bytes); stream.Position = 0; const float expected = 420.0f; - float actual = stream.ReadSingle(Endianness.BigEndian); + float actual = stream.ReadSingleBigEndian(); Assert.That(stream.Position, Is.EqualTo(4)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadSingle_ShouldReadLittleEndian_GivenLittleEndian() + public void ReadSingleLittleEndian_ShouldReadLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x00, 0x00, 0xD2, 0x43}; + ReadOnlySpan bytes = stackalloc byte[] { 0x00, 0x00, 0xD2, 0x43 }; stream.Write(bytes); stream.Position = 0; const float expected = 420.0f; - float actual = stream.ReadSingle(Endianness.LittleEndian); + float actual = stream.ReadSingleLittleEndian(); Assert.That(stream.Position, Is.EqualTo(4)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.ReadUInt16.cs b/X10D.Tests/src/IO/StreamTests.ReadUInt16.cs index cd8d726..0cd249d 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadUInt16.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadUInt16.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,60 +7,60 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadUInt16_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadUInt16()); - Assert.Throws(() => stream.ReadUInt16(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadUInt16(Endianness.BigEndian)); - } - - [Test] - public void ReadUInt16_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadUInt16BigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadUInt16()); - Assert.Throws(() => stream.ReadUInt16(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadUInt16(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadUInt16BigEndian()); } [Test] - public void ReadUInt16_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadUInt16LittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadUInt16((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadUInt16LittleEndian()); } [Test] - public void ReadUInt16_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadUInt16BigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadUInt16BigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadUInt16LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadUInt16LittleEndian()); + } + + [Test] + public void ReadUInt16BigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x01, 0xA4}; + ReadOnlySpan bytes = stackalloc byte[] { 0x01, 0xA4 }; stream.Write(bytes); stream.Position = 0; const ushort expected = 420; - ushort actual = stream.ReadUInt16(Endianness.BigEndian); + ushort actual = stream.ReadUInt16BigEndian(); Assert.That(stream.Position, Is.EqualTo(2)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadUInt16_ShouldReadLittleEndian_GivenLittleEndian() + public void ReadUInt16LittleEndian_ShouldReadLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0xA4, 0x01}; + ReadOnlySpan bytes = stackalloc byte[] { 0xA4, 0x01 }; stream.Write(bytes); stream.Position = 0; const ushort expected = 420; - ushort actual = stream.ReadUInt16(Endianness.LittleEndian); + ushort actual = stream.ReadUInt16LittleEndian(); Assert.That(stream.Position, Is.EqualTo(2)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.ReadUInt32.cs b/X10D.Tests/src/IO/StreamTests.ReadUInt32.cs index cc5c67d..3164afe 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadUInt32.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadUInt32.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,60 +7,60 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadUInt32_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadUInt32()); - Assert.Throws(() => stream.ReadUInt32(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadUInt32(Endianness.BigEndian)); - } - - [Test] - public void ReadUInt32_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadUInt32BigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadUInt32()); - Assert.Throws(() => stream.ReadUInt32(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadUInt32(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadUInt32BigEndian()); } [Test] - public void ReadUInt32_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadUInt32LittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadUInt32((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadUInt32LittleEndian()); } [Test] - public void ReadUInt32_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadUInt32BigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadUInt32BigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadUInt32LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadUInt32LittleEndian()); + } + + [Test] + public void ReadUInt32BigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan bytes = stackalloc byte[] { 0x00, 0x00, 0x01, 0xA4 }; stream.Write(bytes); stream.Position = 0; const uint expected = 420; - uint actual = stream.ReadUInt32(Endianness.BigEndian); + uint actual = stream.ReadUInt32BigEndian(); Assert.That(stream.Position, Is.EqualTo(4)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadUInt32_ShouldReadLittleEndian_GivenLittleEndian() + public void ReadUInt32LittleEndian_ShouldReadLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00}; + ReadOnlySpan bytes = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00 }; stream.Write(bytes); stream.Position = 0; const uint expected = 420; - uint actual = stream.ReadUInt32(Endianness.LittleEndian); + uint actual = stream.ReadUInt32LittleEndian(); Assert.That(stream.Position, Is.EqualTo(4)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.ReadUInt64.cs b/X10D.Tests/src/IO/StreamTests.ReadUInt64.cs index 0d916a7..ca33b01 100644 --- a/X10D.Tests/src/IO/StreamTests.ReadUInt64.cs +++ b/X10D.Tests/src/IO/StreamTests.ReadUInt64.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,60 +7,60 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void ReadUInt64_ShouldThrowArgumentException_GivenNonReadableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.ReadUInt64()); - Assert.Throws(() => stream.ReadUInt64(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadUInt64(Endianness.BigEndian)); - } - - [Test] - public void ReadUInt64_ShouldThrowArgumentNullException_GivenNullStream() + public void ReadUInt64BigEndian_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; - Assert.Throws(() => stream.ReadUInt64()); - Assert.Throws(() => stream.ReadUInt64(Endianness.LittleEndian)); - Assert.Throws(() => stream.ReadUInt64(Endianness.BigEndian)); + Assert.Throws(() => stream.ReadUInt64BigEndian()); } [Test] - public void ReadUInt64_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + public void ReadUInt64LittleEndian_ShouldThrowArgumentNullException_GivenNullStream() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.ReadUInt64((Endianness)(-1))); + Stream stream = null!; + Assert.Throws(() => stream.ReadUInt64LittleEndian()); } [Test] - public void ReadUInt64_ShouldReadBigEndian_GivenBigEndian() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadUInt64BigEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadUInt64BigEndian()); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void ReadUInt64LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.ReadUInt64LittleEndian()); + } + + [Test] + public void ReadUInt64BigEndian_ShouldReadBigEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan bytes = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4 }; stream.Write(bytes); stream.Position = 0; const ulong expected = 420; - ulong actual = stream.ReadUInt64(Endianness.BigEndian); + ulong actual = stream.ReadUInt64BigEndian(); Assert.That(stream.Position, Is.EqualTo(8)); Assert.That(actual, Is.EqualTo(expected)); } [Test] - public void ReadUInt64_ShouldWriteLittleEndian_GivenLittleEndian() + public void ReadUInt64LittleEndian_ShouldWriteLittleEndian() { using var stream = new MemoryStream(); - ReadOnlySpan bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + ReadOnlySpan bytes = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; stream.Write(bytes); stream.Position = 0; const ulong expected = 420; - ulong actual = stream.ReadUInt64(Endianness.LittleEndian); + ulong actual = stream.ReadUInt64LittleEndian(); Assert.That(stream.Position, Is.EqualTo(8)); Assert.That(actual, Is.EqualTo(expected)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteDecimal.cs b/X10D.Tests/src/IO/StreamTests.WriteDecimal.cs index 3aaa113..911dd5d 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteDecimal.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteDecimal.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using NUnit.Framework; using X10D.IO; @@ -7,46 +8,47 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteDecimal_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write(420.0m, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420.0m, Endianness.BigEndian)); - } - - [Test] - public void WriteDecimal_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDecimalArgument() { Stream stream = null!; - Assert.Throws(() => stream.Write(420.0m, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420.0m, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian(420.0m)); } [Test] - public void WriteDecimal_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndDecimalArgument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write(420.0m, (Endianness)(-1))); - Assert.Throws(() => stream.Write(420.0m, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian(420.0m)); } [Test] - public void WriteDecimal_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDecimalArgument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian(420.0m)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndDecimalArgument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian(420.0m)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenDecimalArgument() { using var stream = new MemoryStream(); - stream.Write(420.0m, Endianness.BigEndian); + stream.WriteBigEndian(420.0m); Assert.That(stream.Position, Is.EqualTo(16)); stream.Position = 0; Span actual = stackalloc byte[16]; ReadOnlySpan expected = stackalloc byte[] { - 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x68 + 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x68, 0x10, 0x00, 0x00 }; int read = stream.Read(actual); @@ -55,10 +57,10 @@ internal partial class StreamTests } [Test] - public void WriteDecimal_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenDecimalArgument() { using var stream = new MemoryStream(); - stream.Write(420.0m, Endianness.LittleEndian); + stream.WriteLittleEndian(420.0m); Assert.That(stream.Position, Is.EqualTo(16)); stream.Position = 0; diff --git a/X10D.Tests/src/IO/StreamTests.WriteDouble.cs b/X10D.Tests/src/IO/StreamTests.WriteDouble.cs index e933858..8828325 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteDouble.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteDouble.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteDouble_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write(420.0, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420.0, Endianness.BigEndian)); - } - - [Test] - public void WriteDouble_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDoubleArgument() { Stream stream = null!; - Assert.Throws(() => stream.Write(420.0, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420.0, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian(420.0)); } [Test] - public void WriteDouble_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndDoubleArgument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write(420.0, (Endianness)(-1))); - Assert.Throws(() => stream.Write(420.0, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian(420.0)); } [Test] - public void WriteDouble_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDoubleArgument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian(420.0)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndDoubleArgument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian(420.0)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenDoubleArgument() { using var stream = new MemoryStream(); - stream.Write(420.0, Endianness.BigEndian); + stream.WriteBigEndian(420.0); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; - ReadOnlySpan expected = stackalloc byte[] {0x40, 0x7A, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}; + ReadOnlySpan expected = stackalloc byte[] { 0x40, 0x7A, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteDouble_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenDoubleArgument() { using var stream = new MemoryStream(); - stream.Write(420.0, Endianness.LittleEndian); + stream.WriteLittleEndian(420.0); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; - ReadOnlySpan expected = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x40}; + ReadOnlySpan expected = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x40 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteInt16.cs b/X10D.Tests/src/IO/StreamTests.WriteInt16.cs index cc17549..bc38dd8 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteInt16.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteInt16.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteInt16_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write((short)420, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write((short)420, Endianness.BigEndian)); - } - - [Test] - public void WriteInt16_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt16Argument() { Stream stream = null!; - Assert.Throws(() => stream.Write((short)420, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write((short)420, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian((short)420)); } [Test] - public void WriteInt16_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt16Argument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write((short)420, (Endianness)(-1))); - Assert.Throws(() => stream.Write((short)420, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian((short)420)); } [Test] - public void WriteInt16_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt16Argument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian((short)420)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt16Argument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian((short)420)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenInt16Argument() { using var stream = new MemoryStream(); - stream.Write((short)420, Endianness.BigEndian); + stream.WriteBigEndian((short)420); Assert.That(stream.Position, Is.EqualTo(2)); stream.Position = 0; Span actual = stackalloc byte[2]; - ReadOnlySpan expected = stackalloc byte[] {0x01, 0xA4}; + ReadOnlySpan expected = stackalloc byte[] { 0x01, 0xA4 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(2)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteInt16_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenInt16Argument() { using var stream = new MemoryStream(); - stream.Write((short)420, Endianness.LittleEndian); + stream.WriteLittleEndian((short)420); Assert.That(stream.Position, Is.EqualTo(2)); stream.Position = 0; Span actual = stackalloc byte[2]; - ReadOnlySpan expected = stackalloc byte[] {0xA4, 0x01}; + ReadOnlySpan expected = stackalloc byte[] { 0xA4, 0x01 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(2)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteInt32.cs b/X10D.Tests/src/IO/StreamTests.WriteInt32.cs index 95f748e..75688f9 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteInt32.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteInt32.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteInt32_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write(420, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420, Endianness.BigEndian)); - } - - [Test] - public void WriteInt32_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt32Argument() { Stream stream = null!; - Assert.Throws(() => stream.Write(420, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian(420)); } [Test] - public void WriteInt32_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt32Argument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write(420, (Endianness)(-1))); - Assert.Throws(() => stream.Write(420, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian(420)); } [Test] - public void WriteInt32_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt32Argument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian(420)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt32Argument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian(420)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenInt32Argument() { using var stream = new MemoryStream(); - stream.Write(420, Endianness.BigEndian); + stream.WriteBigEndian(420); Assert.That(stream.Position, Is.EqualTo(4)); stream.Position = 0; Span actual = stackalloc byte[4]; - ReadOnlySpan expected = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan expected = stackalloc byte[] { 0x00, 0x00, 0x01, 0xA4 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(4)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteInt32_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenInt32Argument() { using var stream = new MemoryStream(); - stream.Write(420, Endianness.LittleEndian); + stream.WriteLittleEndian(420); Assert.That(stream.Position, Is.EqualTo(4)); stream.Position = 0; Span actual = stackalloc byte[4]; - ReadOnlySpan expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00}; + ReadOnlySpan expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(4)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteInt64.cs b/X10D.Tests/src/IO/StreamTests.WriteInt64.cs index adb8ce2..feeb677 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteInt64.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteInt64.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteInt64_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write(420L, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420L, Endianness.BigEndian)); - } - - [Test] - public void WriteInt64_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt64Argument() { Stream stream = null!; - Assert.Throws(() => stream.Write(420L, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420L, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian(420L)); } [Test] - public void WriteInt64_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt64Argument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write(420L, (Endianness)(-1))); - Assert.Throws(() => stream.Write(420L, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian(420L)); } [Test] - public void WriteInt64_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt64Argument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian(420L)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt64Argument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian(420L)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenInt64Argument() { using var stream = new MemoryStream(); - stream.Write(420L, Endianness.BigEndian); + stream.WriteBigEndian(420L); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; - ReadOnlySpan expected = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan expected = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteInt64_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenLittleEndian() { using var stream = new MemoryStream(); - stream.Write(420L, Endianness.LittleEndian); + stream.WriteLittleEndian(420L); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; - ReadOnlySpan expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + ReadOnlySpan expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteSingle.cs b/X10D.Tests/src/IO/StreamTests.WriteSingle.cs index 4a7376e..7df8eb8 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteSingle.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteSingle.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteSingle_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write(420.0f, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420.0f, Endianness.BigEndian)); - } - - [Test] - public void WriteSingle_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndSingleArgument() { Stream stream = null!; - Assert.Throws(() => stream.Write(420.0f, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420.0f, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian(420.0f)); } [Test] - public void WriteSingle_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndSingleArgument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write(420.0f, (Endianness)(-1))); - Assert.Throws(() => stream.Write(420.0f, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian(420.0f)); } [Test] - public void WriteSingle_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndSingleArgument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian(420.0f)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndSingleArgument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian(420.0f)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenSingleArgument() { using var stream = new MemoryStream(); - stream.Write(420.0f, Endianness.BigEndian); + stream.WriteBigEndian(420.0f); Assert.That(stream.Position, Is.EqualTo(4)); stream.Position = 0; Span actual = stackalloc byte[4]; - ReadOnlySpan expected = stackalloc byte[] {0x43, 0xD2, 0x00, 0x00}; + ReadOnlySpan expected = stackalloc byte[] { 0x43, 0xD2, 0x00, 0x00 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(4)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteSingle_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenSingleArgument() { using var stream = new MemoryStream(); - stream.Write(420.0f, Endianness.LittleEndian); + stream.WriteLittleEndian(420.0f); Assert.That(stream.Position, Is.EqualTo(4)); stream.Position = 0; Span actual = stackalloc byte[4]; - ReadOnlySpan expected = stackalloc byte[] {0x00, 0x00, 0xD2, 0x43}; + ReadOnlySpan expected = stackalloc byte[] { 0x00, 0x00, 0xD2, 0x43 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(4)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteUInt16.cs b/X10D.Tests/src/IO/StreamTests.WriteUInt16.cs index 89a620f..7fde9f2 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteUInt16.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteUInt16.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteUInt16_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write((ushort)420, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write((ushort)420, Endianness.BigEndian)); - } - - [Test] - public void WriteUInt16_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt16Argument() { Stream stream = null!; - Assert.Throws(() => stream.Write((ushort)420, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write((ushort)420, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian((ushort)420)); } [Test] - public void WriteUInt16_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt16Argument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write((ushort)420, (Endianness)(-1))); - Assert.Throws(() => stream.Write((ushort)420, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian((ushort)420)); } [Test] - public void WriteUInt16_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt16Argument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian((ushort)420)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt16Argument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian((ushort)420)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenUInt16Endian() { using var stream = new MemoryStream(); - stream.Write((ushort)420, Endianness.BigEndian); + stream.WriteBigEndian((ushort)420); Assert.That(stream.Position, Is.EqualTo(2)); stream.Position = 0; Span actual = stackalloc byte[2]; - ReadOnlySpan expected = stackalloc byte[] {0x01, 0xA4}; + ReadOnlySpan expected = stackalloc byte[] { 0x01, 0xA4 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(2)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteUInt16_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenUInt16tleEndian() { using var stream = new MemoryStream(); - stream.Write((ushort)420, Endianness.LittleEndian); + stream.WriteLittleEndian((ushort)420); Assert.That(stream.Position, Is.EqualTo(2)); stream.Position = 0; Span actual = stackalloc byte[2]; - ReadOnlySpan expected = stackalloc byte[] {0xA4, 0x01}; + ReadOnlySpan expected = stackalloc byte[] { 0xA4, 0x01 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(2)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteUInt32.cs b/X10D.Tests/src/IO/StreamTests.WriteUInt32.cs index a9c3bb9..e5eafb7 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteUInt32.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteUInt32.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteUInt32_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write(420U, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420U, Endianness.BigEndian)); - } - - [Test] - public void WriteUInt32_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt32Argument() { Stream stream = null!; - Assert.Throws(() => stream.Write(420U, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420U, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian(420U)); } [Test] - public void WriteUInt32_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt32Argument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write(420U, (Endianness)(-1))); - Assert.Throws(() => stream.Write(420U, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian(420U)); } [Test] - public void WriteUInt32_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt32Argument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian(420U)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt32Argument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian(420U)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenUInt32Argument() { using var stream = new MemoryStream(); - stream.Write(420U, Endianness.BigEndian); + stream.WriteBigEndian(420U); Assert.That(stream.Position, Is.EqualTo(4)); stream.Position = 0; Span actual = stackalloc byte[4]; - ReadOnlySpan expected = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan expected = stackalloc byte[] { 0x00, 0x00, 0x01, 0xA4 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(4)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteUInt32_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenUInt32Argument() { using var stream = new MemoryStream(); - stream.Write(420U, Endianness.LittleEndian); + stream.WriteLittleEndian(420U); Assert.That(stream.Position, Is.EqualTo(4)); stream.Position = 0; Span actual = stackalloc byte[4]; - ReadOnlySpan expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00}; + ReadOnlySpan expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(4)); diff --git a/X10D.Tests/src/IO/StreamTests.WriteUInt64.cs b/X10D.Tests/src/IO/StreamTests.WriteUInt64.cs index dad8cc4..1bf8464 100644 --- a/X10D.Tests/src/IO/StreamTests.WriteUInt64.cs +++ b/X10D.Tests/src/IO/StreamTests.WriteUInt64.cs @@ -1,4 +1,5 @@ -using NUnit.Framework; +using System.Diagnostics.CodeAnalysis; +using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; @@ -6,44 +7,45 @@ namespace X10D.Tests.IO; internal partial class StreamTests { [Test] - public void WriteUInt64_ShouldThrowArgumentException_GivenNonWriteableStream() - { - Stream stream = new DummyStream(); - Assert.Throws(() => stream.Write(420UL, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420UL, Endianness.BigEndian)); - } - - [Test] - public void WriteUInt64_ShouldThrowArgumentNullException_GivenNullStream() + public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument() { Stream stream = null!; - Assert.Throws(() => stream.Write(420UL, Endianness.LittleEndian)); - Assert.Throws(() => stream.Write(420UL, Endianness.BigEndian)); + Assert.Throws(() => stream.WriteBigEndian(420UL)); } [Test] - public void WriteUInt64_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt64Argument() { - // we don't need to enclose this stream in a using declaration, since disposing a - // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. - // that - coupled with the fact that encapsulating the stream in a using declaration causes the - // analyser to trip up and think the stream is disposed by the time the local is captured in - // assertion lambda - means this line is fine as it is. please do not change. - Stream stream = Stream.Null; - Assert.Throws(() => stream.Write(420UL, (Endianness)(-1))); - Assert.Throws(() => stream.Write(420UL, (Endianness)(-1))); + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteBigEndian(420UL)); } [Test] - public void WriteUInt64_ShouldWriteBigEndian_GivenBigEndian() + public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument() + { + Stream stream = null!; + Assert.Throws(() => stream.WriteLittleEndian(420UL)); + } + + [Test] + [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] + public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt64Argument() + { + Stream stream = new DummyStream(); + Assert.Throws(() => stream.WriteLittleEndian(420UL)); + } + + [Test] + public void WriteBigEndian_ShouldWriteBigEndian_GivenUInt64Argument() { using var stream = new MemoryStream(); - stream.Write(420UL, Endianness.BigEndian); + stream.WriteBigEndian(420UL); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; - ReadOnlySpan expected = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4}; + ReadOnlySpan expected = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); @@ -51,15 +53,15 @@ internal partial class StreamTests } [Test] - public void WriteUInt64_ShouldWriteLittleEndian_GivenLittleEndian() + public void WriteLittleEndian_ShouldWriteLittleEndian_GivenUInt64Argument() { using var stream = new MemoryStream(); - stream.Write(420UL, Endianness.LittleEndian); + stream.WriteLittleEndian(420UL); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; - ReadOnlySpan expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; + ReadOnlySpan expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); diff --git a/X10D.Tests/src/IO/UInt16Tests.cs b/X10D.Tests/src/IO/UInt16Tests.cs index 6085358..4ce858d 100644 --- a/X10D.Tests/src/IO/UInt16Tests.cs +++ b/X10D.Tests/src/IO/UInt16Tests.cs @@ -7,56 +7,62 @@ namespace X10D.Tests.IO; internal class UInt16Tests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetLittleEndianBytes_ReturnsCorrectValue_WithEndianness() { const ushort value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + byte[] expected = { 0x0F, 0 }; + byte[] actual = value.GetLittleEndianBytes(); + + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetBigEndianBytes_ReturnsCorrectValue_WithEndianness() { const ushort value = 0x0F; - byte[] littleEndian = {0x0F, 0}; - byte[] bigEndian = {0, 0x0F}; + byte[] expected = { 0, 0x0F }; + byte[] actual = value.GetBigEndianBytes(); - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() { const ushort value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0} : new byte[] {0, 0x0F}; + byte[] expected = { 0x0F, 0 }; - Span buffer = stackalloc byte[2]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + Span actual = stackalloc byte[2]; + Assert.That(value.TryWriteLittleEndian(actual)); + + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() { const ushort value = 0x0F; - byte[] littleEndian = {0x0F, 0}; - byte[] bigEndian = {0, 0x0F}; + byte[] expected = { 0, 0x0F }; - Span buffer = stackalloc byte[2]; + Span actual = stackalloc byte[2]; + Assert.That(value.TryWriteBigEndian(actual)); - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() { const ushort value = 0x0F; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); + } + + [Test] + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() + { + const ushort value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteBigEndian(buffer), Is.False); } } diff --git a/X10D.Tests/src/IO/UInt32Tests.cs b/X10D.Tests/src/IO/UInt32Tests.cs index a04ea4b..eada9aa 100644 --- a/X10D.Tests/src/IO/UInt32Tests.cs +++ b/X10D.Tests/src/IO/UInt32Tests.cs @@ -7,56 +7,62 @@ namespace X10D.Tests.IO; internal class UInt32Tests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetLittleEndianBytes_ReturnsCorrectValue_WithEndianness() { const uint value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + byte[] expected = { 0x0F, 0, 0, 0 }; + byte[] actual = value.GetLittleEndianBytes(); + + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetBigEndianBytes_ReturnsCorrectValue_WithEndianness() { const uint value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0x0F}; + byte[] expected = { 0, 0, 0, 0x0F }; + byte[] actual = value.GetBigEndianBytes(); - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() { const uint value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian ? new byte[] {0x0F, 0, 0, 0} : new byte[] {0, 0, 0, 0x0F}; + byte[] expected = { 0x0F, 0, 0, 0 }; - Span buffer = stackalloc byte[4]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + Span actual = stackalloc byte[4]; + Assert.That(value.TryWriteLittleEndian(actual)); + + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() { const uint value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0x0F}; + byte[] expected = { 0, 0, 0, 0x0F }; - Span buffer = stackalloc byte[4]; + Span actual = stackalloc byte[4]; + Assert.That(value.TryWriteBigEndian(actual)); - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() { const uint value = 0x0F; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); + } + + [Test] + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() + { + const uint value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteBigEndian(buffer), Is.False); } } diff --git a/X10D.Tests/src/IO/UInt64Tests.cs b/X10D.Tests/src/IO/UInt64Tests.cs index af5727d..6ed1be8 100644 --- a/X10D.Tests/src/IO/UInt64Tests.cs +++ b/X10D.Tests/src/IO/UInt64Tests.cs @@ -7,60 +7,62 @@ namespace X10D.Tests.IO; internal class UInt64Tests { [Test] - public void GetBytes_ReturnsCorrectValue() + public void GetLittleEndianBytes_ReturnsCorrectValue_WithEndianness() { const ulong value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} - : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; - CollectionAssert.AreEqual(bytes, value.GetBytes()); + byte[] expected = { 0x0F, 0, 0, 0, 0, 0, 0, 0 }; + byte[] actual = value.GetLittleEndianBytes(); + + CollectionAssert.AreEqual(expected, actual); } [Test] - public void GetBytes_ReturnsCorrectValue_WithEndianness() + public void GetBigEndianBytes_ReturnsCorrectValue_WithEndianness() { const ulong value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; + byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 0x0F }; + byte[] actual = value.GetBigEndianBytes(); - CollectionAssert.AreEqual(littleEndian, value.GetBytes(Endianness.LittleEndian)); - CollectionAssert.AreEqual(bigEndian, value.GetBytes(Endianness.BigEndian)); + CollectionAssert.AreEqual(expected, actual); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() + public void TryWriteLittleEndian_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() { const ulong value = 0x0F; - byte[] bytes = BitConverter.IsLittleEndian - ? new byte[] {0x0F, 0, 0, 0, 0, 0, 0, 0} - : new byte[] {0, 0, 0, 0, 0, 0, 0, 0x0F}; + byte[] expected = { 0x0F, 0, 0, 0, 0, 0, 0, 0 }; - Span buffer = stackalloc byte[8]; - Assert.That(value.TryWriteBytes(buffer)); - CollectionAssert.AreEqual(bytes, buffer.ToArray()); + Span actual = stackalloc byte[8]; + Assert.That(value.TryWriteLittleEndian(actual)); + + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan_WithEndianness() + public void TryWriteBigEndian_ReturnsTrue_FillsSpanCorrectly_GivenLargeEnoughSpan() { const ulong value = 0x0F; - byte[] littleEndian = {0x0F, 0, 0, 0, 0, 0, 0, 0}; - byte[] bigEndian = {0, 0, 0, 0, 0, 0, 0, 0x0F}; + byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 0x0F }; - Span buffer = stackalloc byte[8]; + Span actual = stackalloc byte[8]; + Assert.That(value.TryWriteBigEndian(actual)); - Assert.That(value.TryWriteBytes(buffer, Endianness.LittleEndian)); - CollectionAssert.AreEqual(littleEndian, buffer.ToArray()); - - Assert.That(value.TryWriteBytes(buffer, Endianness.BigEndian)); - CollectionAssert.AreEqual(bigEndian, buffer.ToArray()); + CollectionAssert.AreEqual(expected, actual.ToArray()); } [Test] - public void TryWriteBytes_ReturnsFalse_GivenSmallSpan() + public void TryWriteLittleEndian_RReturnsFalse_GivenSmallSpan() { const ulong value = 0x0F; Span buffer = stackalloc byte[0]; - Assert.That(value.TryWriteBytes(buffer), Is.False); + Assert.That(value.TryWriteLittleEndian(buffer), Is.False); + } + + [Test] + public void TryWriteBigEndian_ReturnsFalse_GivenSmallSpan() + { + const ulong value = 0x0F; + Span buffer = stackalloc byte[0]; + Assert.That(value.TryWriteBigEndian(buffer), Is.False); } } diff --git a/X10D/src/Endianness.cs b/X10D/src/Endianness.cs deleted file mode 100644 index 3b23c07..0000000 --- a/X10D/src/Endianness.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.ComponentModel; - -namespace X10D; - -/// -/// Represents an enumeration of endianness values. -/// -public enum Endianness -{ - /// - /// The value should be read as though it uses little endian encoding. - /// - [Description("The value should be read as though it uses little endian encoding.")] LittleEndian, - - /// - /// The value should be read as though it uses big endian encoding. - /// - [Description("The value should be read as though it uses big endian encoding.")] BigEndian -} diff --git a/X10D/src/IO/DecimalExtensions.cs b/X10D/src/IO/DecimalExtensions.cs new file mode 100644 index 0000000..8058f70 --- /dev/null +++ b/X10D/src/IO/DecimalExtensions.cs @@ -0,0 +1,111 @@ +using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; + +namespace X10D.IO; + +/// +/// IO-related extension methods for . +/// +public static class DecimalExtensions +{ + /// + /// Converts the current decimal number into an array of bytes, as little endian. + /// + /// The value. + /// An array of bytes with length 4. + [Pure] + public static byte[] GetBigEndianBytes(this decimal value) + { + Span buffer = stackalloc byte[4]; + value.TryWriteBigEndian(buffer); + return buffer.ToArray(); + } + + /// + /// Converts the current decimal number into an array of bytes, as little endian. + /// + /// The value. + /// An array of bytes with length 4. + [Pure] + public static byte[] GetLittleEndianBytes(this decimal value) + { + Span buffer = stackalloc byte[4]; + value.TryWriteLittleEndian(buffer); + return buffer.ToArray(); + } + + /// + /// Converts the current decimal number into a span of bytes, as big endian. + /// + /// The value. + /// The span of bytes where the value is to be written, as big endian. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBigEndian(this decimal value, Span destination) + { + Span buffer = stackalloc int[4]; + GetBits(value, buffer); + + if (buffer[0].TryWriteBigEndian(destination[..4]) && + buffer[1].TryWriteBigEndian(destination[4..8]) && + buffer[2].TryWriteBigEndian(destination[8..12]) && + buffer[3].TryWriteBigEndian(destination[12..])) + { + if (BitConverter.IsLittleEndian) + { + destination.Reverse(); + } + + return true; + } + + destination.Clear(); + return false; + } + + /// + /// Converts the current decimal number into a span of bytes, as little endian. + /// + /// The value. + /// The span of bytes where the value is to be written, as little endian. + /// if the conversion was successful; otherwise, . + public static bool TryWriteLittleEndian(this decimal value, Span destination) + { + Span buffer = stackalloc int[4]; + GetBits(value, buffer); + + if (buffer[0].TryWriteLittleEndian(destination[..4]) && + buffer[1].TryWriteLittleEndian(destination[4..8]) && + buffer[2].TryWriteLittleEndian(destination[8..12]) && + buffer[3].TryWriteLittleEndian(destination[12..])) + { + if (!BitConverter.IsLittleEndian) + { + destination.Reverse(); + } + + return true; + } + + destination.Clear(); + return false; + } + + private static void GetBits(decimal value, Span destination) + { +#if NET5_0_OR_GREATER + decimal.GetBits(value, destination); +#else + Span buffer = stackalloc byte[16]; + MemoryMarshal.Write(buffer, ref value); + + var flags = MemoryMarshal.Read(buffer[..4]); + var hi = MemoryMarshal.Read(buffer[4..8]); + var lo = MemoryMarshal.Read(buffer[8..]); + + destination[0] = flags; + destination[1] = hi; + destination[2] = (int)(lo & 0xFFFFFFFF); + destination[3] = (int)(lo >> 32); +#endif + } +} diff --git a/X10D/src/IO/DoubleExtensions.cs b/X10D/src/IO/DoubleExtensions.cs index f71e4be..728cada 100644 --- a/X10D/src/IO/DoubleExtensions.cs +++ b/X10D/src/IO/DoubleExtensions.cs @@ -10,58 +10,70 @@ namespace X10D.IO; public static class DoubleExtensions { /// - /// Returns the current double-precision floating-point value as an array of bytes. + /// Converts the current double-precision floating-point number into an array of bytes, as little endian. /// - /// The number to convert. - /// An array of bytes with length 8. + /// The value. + /// An array of bytes with length 4. [Pure] - public static byte[] GetBytes(this double value) + public static byte[] GetBigEndianBytes(this double value) { - byte[] buffer = new byte[8]; - value.TryWriteBytes(buffer); - return buffer; + Span buffer = stackalloc byte[8]; + value.TryWriteBigEndian(buffer); + return buffer.ToArray(); } /// - /// Returns the current double-precision floating-point value as an array of bytes. + /// Converts the current double-precision floating-point number into an array of bytes, as little endian. /// - /// The number to convert. - /// The endianness with which to write the bytes. - /// An array of bytes with length 8. + /// The value. + /// An array of bytes with length 4. [Pure] - public static byte[] GetBytes(this double value, Endianness endianness) + public static byte[] GetLittleEndianBytes(this double value) { - byte[] buffer = new byte[8]; - value.TryWriteBytes(buffer, endianness); - return buffer; + Span buffer = stackalloc byte[8]; + value.TryWriteLittleEndian(buffer); + return buffer.ToArray(); } /// - /// Converts the current double-precision floating-point into a span of bytes. + /// Converts the current double-precision floating-point number into a span of bytes, as big endian. /// - /// The value. - /// When this method returns, the bytes representing the converted . + /// The value. + /// The span of bytes where the value is to be written, as big endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this double value, Span destination) + public static bool TryWriteBigEndian(this double value, Span destination) { - return BitConverter.TryWriteBytes(destination, value); - } - - /// - /// Converts the current double-precision floating-point into a span of bytes. - /// - /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. - /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this double value, Span destination, Endianness endianness) - { - if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian)) +#if NET5_0_OR_GREATER + return BinaryPrimitives.TryWriteDoubleBigEndian(destination, value); +#else + if (BitConverter.IsLittleEndian) { - long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); - value = BitConverter.Int64BitsToDouble(tmp); + return MemoryMarshal.TryWrite(destination, ref value); } - return MemoryMarshal.TryWrite(destination, ref value); + long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); + return MemoryMarshal.TryWrite(destination, ref tmp); +#endif + } + + /// + /// Converts the current double-precision floating-point number into a span of bytes, as little endian. + /// + /// The value. + /// The span of bytes where the value is to be written, as little endian. + /// if the conversion was successful; otherwise, . + public static bool TryWriteLittleEndian(this double value, Span destination) + { +#if NET5_0_OR_GREATER + return BinaryPrimitives.TryWriteDoubleLittleEndian(destination, value); +#else + if (!BitConverter.IsLittleEndian) + { + return MemoryMarshal.TryWrite(destination, ref value); + } + + long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); + return MemoryMarshal.TryWrite(destination, ref tmp); +#endif } } diff --git a/X10D/src/IO/Int16Extensions.cs b/X10D/src/IO/Int16Extensions.cs index 60b0b87..6c0dcc4 100644 --- a/X10D/src/IO/Int16Extensions.cs +++ b/X10D/src/IO/Int16Extensions.cs @@ -9,54 +9,50 @@ namespace X10D.IO; public static class Int16Extensions { /// - /// Returns the current 16-bit signed integer value as an array of bytes. - /// - /// The number to convert. - /// An array of bytes with length 2. - [Pure] - public static byte[] GetBytes(this short value) - { - byte[] buffer = new byte[2]; - value.TryWriteBytes(buffer); - return buffer; - } - - /// - /// Returns the current 16-bit signed integer value as an array of bytes. - /// - /// The number to convert. - /// The endianness with which to write the bytes. - /// An array of bytes with length 2. - [Pure] - public static byte[] GetBytes(this short value, Endianness endianness) - { - byte[] buffer = new byte[2]; - value.TryWriteBytes(buffer, endianness); - return buffer; - } - - /// - /// Converts the current 16-bit signed integer into a span of bytes. + /// Converts the current 16-bit signed integer into an array of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this short value, Span destination) + /// An array of bytes with length 8. + [Pure] + public static byte[] GetBigEndianBytes(this short value) { - return BitConverter.TryWriteBytes(destination, value); + Span buffer = stackalloc byte[2]; + value.TryWriteBigEndian(buffer); + return buffer.ToArray(); } /// - /// Converts the current 16-bit signed integer into a span of bytes. + /// Converts the current 16-bit signed integer into an array of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. - /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this short value, Span destination, Endianness endianness) + /// An array of bytes with length 8. + [Pure] + public static byte[] GetLittleEndianBytes(this short value) { - return endianness == Endianness.BigEndian - ? BinaryPrimitives.TryWriteInt16BigEndian(destination, value) - : BinaryPrimitives.TryWriteInt16LittleEndian(destination, value); + Span buffer = stackalloc byte[2]; + value.TryWriteLittleEndian(buffer); + return buffer.ToArray(); + } + + /// + /// Writes the current 16-bit signed integer into a span of bytes, as big endian. + /// + /// The value. + /// The span of bytes where the value is to be written, as big endian. + /// if the conversion was successful; otherwise, . + public static bool TryWriteBigEndian(this short value, Span destination) + { + return BinaryPrimitives.TryWriteInt16BigEndian(destination, value); + } + + /// + /// Writes the current 16-bit signed integer into a span of bytes, as little endian. + /// + /// The value. + /// The span of bytes where the value is to be written, as little endian. + /// if the conversion was successful; otherwise, . + public static bool TryWriteLittleEndian(this short value, Span destination) + { + return BinaryPrimitives.TryWriteInt16LittleEndian(destination, value); } } diff --git a/X10D/src/IO/Int32Extensions.cs b/X10D/src/IO/Int32Extensions.cs index a349dec..7f9d6cb 100644 --- a/X10D/src/IO/Int32Extensions.cs +++ b/X10D/src/IO/Int32Extensions.cs @@ -9,54 +9,50 @@ namespace X10D.IO; public static class Int32Extensions { /// - /// Returns the current 32-bit signed integer value as an array of bytes. + /// Converts the current 32-bit signed integer into an array of bytes, as big endian. /// - /// The number to convert. - /// An array of bytes with length 4. + /// The value. + /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this int value) + public static byte[] GetBigEndianBytes(this int value) { - byte[] buffer = new byte[4]; - value.TryWriteBytes(buffer); - return buffer; - } - - /// - /// Returns the current 32-bit signed integer value as an array of bytes. - /// - /// The number to convert. - /// The endianness with which to write the bytes. - /// An array of bytes with length 4. - [Pure] - public static byte[] GetBytes(this int value, Endianness endianness) - { - byte[] buffer = new byte[4]; - value.TryWriteBytes(buffer, endianness); + Span buffer = stackalloc byte[4]; + value.TryWriteBigEndian(buffer); return buffer.ToArray(); } /// - /// Converts the current 32-bit signed integer into a span of bytes. + /// Converts the current 32-bit signed integer into an array of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this int value, Span destination) + /// An array of bytes with length 8. + [Pure] + public static byte[] GetLittleEndianBytes(this int value) { - return BitConverter.TryWriteBytes(destination, value); + Span buffer = stackalloc byte[4]; + value.TryWriteLittleEndian(buffer); + return buffer.ToArray(); } /// - /// Converts the current 32-bit signed integer into a span of bytes. + /// Writes the current 32-bit signed integer into a span of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. + /// The span of bytes where the value is to be written, as big endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this int value, Span destination, Endianness endianness) + public static bool TryWriteBigEndian(this int value, Span destination) { - return endianness == Endianness.BigEndian - ? BinaryPrimitives.TryWriteInt32BigEndian(destination, value) - : BinaryPrimitives.TryWriteInt32LittleEndian(destination, value); + return BinaryPrimitives.TryWriteInt32BigEndian(destination, value); + } + + /// + /// Writes the current 32-bit signed integer into a span of bytes, as little endian. + /// + /// The value. + /// The span of bytes where the value is to be written, as little endian. + /// if the conversion was successful; otherwise, . + public static bool TryWriteLittleEndian(this int value, Span destination) + { + return BinaryPrimitives.TryWriteInt32LittleEndian(destination, value); } } diff --git a/X10D/src/IO/Int64Extensions.cs b/X10D/src/IO/Int64Extensions.cs index 2fd7f05..118fc49 100644 --- a/X10D/src/IO/Int64Extensions.cs +++ b/X10D/src/IO/Int64Extensions.cs @@ -9,54 +9,50 @@ namespace X10D.IO; public static class Int64Extensions { /// - /// Returns the current 64-bit signed integer value as an array of bytes. + /// Converts the current 64-bit signed integer into an array of bytes, as big endian. /// - /// The number to convert. + /// The value. /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this long value) + public static byte[] GetBigEndianBytes(this long value) { Span buffer = stackalloc byte[8]; - value.TryWriteBytes(buffer); + value.TryWriteBigEndian(buffer); return buffer.ToArray(); } /// - /// Returns the current 64-bit signed integer value as an array of bytes. + /// Converts the current 64-bit signed integer into an array of bytes, as big endian. /// - /// The number to convert. - /// The endianness with which to write the bytes. + /// The value. /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this long value, Endianness endianness) + public static byte[] GetLittleEndianBytes(this long value) { Span buffer = stackalloc byte[8]; - value.TryWriteBytes(buffer, endianness); + value.TryWriteLittleEndian(buffer); return buffer.ToArray(); } /// - /// Converts the current 64-bit signed integer a span of bytes. + /// Writes the current 64-bit signed integer into a span of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . + /// The span of bytes where the value is to be written, as big endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this long value, Span destination) + public static bool TryWriteBigEndian(this long value, Span destination) { - return BitConverter.TryWriteBytes(destination, value); + return BinaryPrimitives.TryWriteInt64BigEndian(destination, value); } /// - /// Converts the current 64-bit signed integer a span of bytes. + /// Writes the current 64-bit signed integer into a span of bytes, as little endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. + /// The span of bytes where the value is to be written, as little endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this long value, Span destination, Endianness endianness) + public static bool TryWriteLittleEndian(this long value, Span destination) { - return endianness == Endianness.BigEndian - ? BinaryPrimitives.TryWriteInt64BigEndian(destination, value) - : BinaryPrimitives.TryWriteInt64LittleEndian(destination, value); + return BinaryPrimitives.TryWriteInt64LittleEndian(destination, value); } } diff --git a/X10D/src/IO/SingleExtensions.cs b/X10D/src/IO/SingleExtensions.cs index 5db19f5..d799261 100644 --- a/X10D/src/IO/SingleExtensions.cs +++ b/X10D/src/IO/SingleExtensions.cs @@ -1,6 +1,8 @@ using System.Buffers.Binary; using System.Diagnostics.Contracts; +#if !NET5_0_OR_GREATER using System.Runtime.InteropServices; +#endif namespace X10D.IO; @@ -10,58 +12,70 @@ namespace X10D.IO; public static class SingleExtensions { /// - /// Returns the current single-precision floating-point value as an array of bytes. + /// Converts the current single-precision floating-point number into an array of bytes, as little endian. /// - /// The number to convert. + /// The value. /// An array of bytes with length 4. [Pure] - public static byte[] GetBytes(this float value) + public static byte[] GetBigEndianBytes(this float value) { Span buffer = stackalloc byte[4]; - value.TryWriteBytes(buffer); + value.TryWriteBigEndian(buffer); return buffer.ToArray(); } /// - /// Returns the current single-precision floating-point value as an array of bytes. + /// Converts the current single-precision floating-point number into an array of bytes, as little endian. /// - /// The number to convert. - /// The endianness with which to write the bytes. + /// The value. /// An array of bytes with length 4. [Pure] - public static byte[] GetBytes(this float value, Endianness endianness) + public static byte[] GetLittleEndianBytes(this float value) { Span buffer = stackalloc byte[4]; - value.TryWriteBytes(buffer, endianness); + value.TryWriteLittleEndian(buffer); return buffer.ToArray(); } /// - /// Converts the current single-precision floating-point into a span of bytes. + /// Converts the current single-precision floating-point number into a span of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . + /// The span of bytes where the value is to be written, as big endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this float value, Span destination) + public static bool TryWriteBigEndian(this float value, Span destination) { - return BitConverter.TryWriteBytes(destination, value); - } - - /// - /// Converts the current single-precision floating-point into a span of bytes. - /// - /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. - /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this float value, Span destination, Endianness endianness) - { - if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian)) +#if NET5_0_OR_GREATER + return BinaryPrimitives.TryWriteSingleBigEndian(destination, value); +#else + if (BitConverter.IsLittleEndian) { - int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); - value = BitConverter.Int32BitsToSingle(tmp); + return MemoryMarshal.TryWrite(destination, ref value); } - return MemoryMarshal.TryWrite(destination, ref value); + int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); + return MemoryMarshal.TryWrite(destination, ref tmp); +#endif + } + + /// + /// Converts the current single-precision floating-point number into a span of bytes, as little endian. + /// + /// The value. + /// The span of bytes where the value is to be written, as little endian. + /// if the conversion was successful; otherwise, . + public static bool TryWriteLittleEndian(this float value, Span destination) + { +#if NET5_0_OR_GREATER + return BinaryPrimitives.TryWriteSingleLittleEndian(destination, value); +#else + if (!BitConverter.IsLittleEndian) + { + return MemoryMarshal.TryWrite(destination, ref value); + } + + int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); + return MemoryMarshal.TryWrite(destination, ref tmp); +#endif } } diff --git a/X10D/src/IO/StreamExtensions.cs b/X10D/src/IO/StreamExtensions.cs index d1d44db..9bc7243 100644 --- a/X10D/src/IO/StreamExtensions.cs +++ b/X10D/src/IO/StreamExtensions.cs @@ -1,5 +1,4 @@ -using System.Buffers.Binary; -using System.Diagnostics.CodeAnalysis; +using System.Buffers.Binary; using System.Reflection; using System.Runtime.InteropServices; using System.Security.Cryptography; @@ -11,9 +10,6 @@ namespace X10D.IO; /// public static class StreamExtensions { - private static readonly Endianness DefaultEndianness = - BitConverter.IsLittleEndian ? Endianness.LittleEndian : Endianness.BigEndian; - /// /// Returns the hash of the current stream as an array of bytes using the specified hash algorithm. /// @@ -64,42 +60,20 @@ public static class StreamExtensions } /// - /// Reads a decimal value from the current stream using the system's default endian encoding, and advances the stream - /// position by sixteen bytes. - /// - /// The stream to read. - /// A sixteen-byte decimal value read from the stream. - public static decimal ReadDecimal(this Stream stream) - { - return stream.ReadDecimal(DefaultEndianness); - } - - /// - /// Reads a decimal value from the current stream using a specified endian encoding, and advances the stream position - /// by sixteen bytes. + /// Reads an from the current stream as big endian, and advances the stream position by sixteen + /// bytes. /// /// The stream from which the value should be read. - /// The endian encoding to use. - /// A decimal value read from the stream. - public static decimal ReadDecimal(this Stream stream, Endianness endianness) + /// The big endian value. + /// is + /// does not support reading. + public static decimal ReadDecimalBigEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); @@ -109,436 +83,496 @@ public static class StreamExtensions const int int32Size = sizeof(int); const int partitionSize = decimalSize / int32Size; - var bits = new int[partitionSize]; + Span buffer = stackalloc int[partitionSize]; for (var index = 0; index < partitionSize; index++) { - bits[index] = stream.ReadInt32(endianness); + buffer[index] = stream.ReadInt32BigEndian(); } - if (endianness != DefaultEndianness) + if (BitConverter.IsLittleEndian) { - Array.Reverse(bits); + buffer.Reverse(); } - return new decimal(bits); +#if NET5_0_OR_GREATER + return new decimal(buffer); +#else + return new decimal(buffer.ToArray()); +#endif } /// - /// Reads a double-precision floating point value from the current stream using the system's default endian encoding, - /// and advances the stream position by eight bytes. + /// Reads an from the current stream as big endian, and advances the stream position by sixteen + /// bytes. /// /// The stream from which the value should be read. - /// A double-precision floating point value read from the stream. - public static double ReadDouble(this Stream stream) - { - return stream.ReadDouble(DefaultEndianness); - } - - /// - /// Reads a double-precision floating point value from the current stream using a specified endian encoding, and - /// advances the stream position by eight bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// A double-precision floating point value read from the stream. - public static double ReadDouble(this Stream stream, Endianness endianness) + /// The big endian value. + /// is + /// does not support reading. + public static decimal ReadDecimalLittleEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(double)]; - stream.Read(buffer); + const int decimalSize = sizeof(decimal); + const int int32Size = sizeof(int); + const int partitionSize = decimalSize / int32Size; - var value = MemoryMarshal.Read(buffer); - - if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian)) + Span buffer = stackalloc int[partitionSize]; + for (var index = 0; index < partitionSize; index++) { - long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); - value = BitConverter.Int64BitsToDouble(tmp); + buffer[index] = stream.ReadInt32LittleEndian(); } - return value; + if (!BitConverter.IsLittleEndian) + { + buffer.Reverse(); + } + +#if NET5_0_OR_GREATER + return new decimal(buffer); +#else + return new decimal(buffer.ToArray()); +#endif } /// - /// Reads a two-byte signed integer from the current stream using the system's default endian encoding, and advances - /// the stream position by two bytes. + /// Reads an from the current stream as big endian, and advances the stream position by eight bytes. /// /// The stream from which the value should be read. - /// An two-byte signed integer read from the stream. - public static short ReadInt16(this Stream stream) - { - return stream.ReadInt16(DefaultEndianness); - } - - /// - /// Reads a two-byte signed integer from the current stream using the specified endian encoding, and advances the - /// stream position by two bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// An two-byte unsigned integer read from the stream. - public static short ReadInt16(this Stream stream, Endianness endianness) + /// The big endian value. + /// is + /// does not support reading. + public static double ReadDoubleBigEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(short)]; - stream.Read(buffer); + Span buffer = stackalloc byte[8]; + _ = stream.Read(buffer); +#if NET5_0_OR_GREATER + return BinaryPrimitives.ReadDoubleBigEndian(buffer); +#else + if (BitConverter.IsLittleEndian) + { + buffer.Reverse(); + } - return endianness == Endianness.LittleEndian - ? BinaryPrimitives.ReadInt16LittleEndian(buffer) - : BinaryPrimitives.ReadInt16BigEndian(buffer); + return MemoryMarshal.Read(buffer); +#endif } /// - /// Reads a four-byte signed integer from the current stream using the system's default endian encoding, and advances - /// the stream position by four bytes. + /// Reads an from the current stream as little endian, and advances the stream position by eight + /// bytes. /// /// The stream from which the value should be read. - /// An four-byte signed integer read from the stream. - public static int ReadInt32(this Stream stream) - { - return stream.ReadInt32(DefaultEndianness); - } - - /// - /// Reads a four-byte signed integer from the current stream using the specified endian encoding, and advances the - /// stream position by four bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// An four-byte unsigned integer read from the stream. - public static int ReadInt32(this Stream stream, Endianness endianness) + /// The little endian value. + /// is + /// does not support reading. + public static double ReadDoubleLittleEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(int)]; - stream.Read(buffer); + Span buffer = stackalloc byte[8]; + _ = stream.Read(buffer); +#if NET5_0_OR_GREATER + return BinaryPrimitives.ReadDoubleLittleEndian(buffer); +#else + if (!BitConverter.IsLittleEndian) + { + buffer.Reverse(); + } - return endianness == Endianness.LittleEndian - ? BinaryPrimitives.ReadInt32LittleEndian(buffer) - : BinaryPrimitives.ReadInt32BigEndian(buffer); + return MemoryMarshal.Read(buffer); +#endif } /// - /// Reads an eight-byte signed integer from the current stream using the system's default endian encoding, and - /// advances the stream position by eight bytes. + /// Reads an from the current stream as big endian, and advances the stream position by two bytes. /// /// The stream from which the value should be read. - /// An eight-byte signed integer read from the stream. - public static long ReadInt64(this Stream stream) - { - return stream.ReadInt64(DefaultEndianness); - } - - /// - /// Reads an eight-byte signed integer from the current stream using the specified endian encoding, and advances the - /// stream position by eight bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// An eight-byte unsigned integer read from the stream. - public static long ReadInt64(this Stream stream, Endianness endianness) + /// The big endian value. + /// is + /// does not support reading. + public static short ReadInt16BigEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(long)]; - stream.Read(buffer); - - return endianness == Endianness.LittleEndian - ? BinaryPrimitives.ReadInt64LittleEndian(buffer) - : BinaryPrimitives.ReadInt64BigEndian(buffer); + Span buffer = stackalloc byte[2]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadInt16BigEndian(buffer); } /// - /// Reads a single-precision floating point value from the current stream using the system's default endian encoding, - /// and advances the stream position by four bytes. + /// Reads an from the current stream as little endian, and advances the stream position by two bytes. /// /// The stream from which the value should be read. - /// A single-precision floating point value read from the stream. - public static double ReadSingle(this Stream stream) - { - return stream.ReadSingle(DefaultEndianness); - } - - /// - /// Reads a double-precision floating point value from the current stream using a specified endian encoding, and - /// advances the stream position by four bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// A single-precision floating point value read from the stream. - public static float ReadSingle(this Stream stream, Endianness endianness) + /// The little endian value. + /// is + /// does not support reading. + public static short ReadInt16LittleEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) + if (!stream.CanRead) { - throw new ArgumentOutOfRangeException(nameof(endianness)); + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) + + Span buffer = stackalloc byte[2]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadInt16LittleEndian(buffer); + } + + /// + /// Reads an from the current stream as big endian, and advances the stream position by four bytes. + /// + /// The stream from which the value should be read. + /// The big endian value. + /// is + /// does not support reading. + public static int ReadInt32BigEndian(this Stream stream) + { + if (stream is null) { - throw new ArgumentOutOfRangeException(nameof(endianness)); + throw new ArgumentNullException(nameof(stream)); } -#endif if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(float)]; - stream.Read(buffer); - - var value = MemoryMarshal.Read(buffer); - - if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian)) - { - int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); - value = BitConverter.Int32BitsToSingle(tmp); - } - - return value; + Span buffer = stackalloc byte[4]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadInt32BigEndian(buffer); } /// - /// Reads a two-byte unsigned integer from the current stream using the system's default endian encoding, and advances - /// the stream position by two bytes. + /// Reads an from the current stream as little endian, and advances the stream position by four bytes. /// /// The stream from which the value should be read. - /// An two-byte unsigned integer read from the stream. + /// The little endian value. + /// is + /// does not support reading. + public static int ReadInt32LittleEndian(this Stream stream) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanRead) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); + } + + Span buffer = stackalloc byte[4]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadInt32LittleEndian(buffer); + } + + /// + /// Reads an from the current stream as big endian, and advances the stream position by eight bytes. + /// + /// The stream from which the value should be read. + /// The big endian value. + /// is + /// does not support reading. + public static long ReadInt64BigEndian(this Stream stream) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanRead) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); + } + + Span buffer = stackalloc byte[8]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadInt64BigEndian(buffer); + } + + /// + /// Reads an from the current stream as little endian, and advances the stream position by eight + /// bytes. + /// + /// The stream from which the value should be read. + /// The little endian value. + /// is + /// does not support reading. + public static long ReadInt64LittleEndian(this Stream stream) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanRead) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); + } + + Span buffer = stackalloc byte[8]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadInt64LittleEndian(buffer); + } + + /// + /// Reads an from the current stream as big endian, and advances the stream position by four bytes. + /// + /// The stream from which the value should be read. + /// The big endian value. + /// is + /// does not support reading. [CLSCompliant(false)] - public static ushort ReadUInt16(this Stream stream) - { - return stream.ReadUInt16(DefaultEndianness); - } - - /// - /// Reads a two-byte unsigned integer from the current stream using the specified endian encoding, and advances the - /// stream position by two bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// An two-byte unsigned integer read from the stream. - [CLSCompliant(false)] - public static ushort ReadUInt16(this Stream stream, Endianness endianness) + public static float ReadSingleBigEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(ushort)]; - stream.Read(buffer); + Span buffer = stackalloc byte[4]; + _ = stream.Read(buffer); +#if NET5_0_OR_GREATER + return BinaryPrimitives.ReadSingleBigEndian(buffer); +#else + if (BitConverter.IsLittleEndian) + { + buffer.Reverse(); + } - return endianness == Endianness.LittleEndian - ? BinaryPrimitives.ReadUInt16LittleEndian(buffer) - : BinaryPrimitives.ReadUInt16BigEndian(buffer); + return MemoryMarshal.Read(buffer); +#endif } /// - /// Reads a four-byte unsigned integer from the current stream using the system's default endian encoding, and - /// advances the stream position by four bytes. + /// Reads an from the current stream as little endian, and advances the stream position by four + /// bytes. /// /// The stream from which the value should be read. - /// An four-byte unsigned integer read from the stream. + /// The little endian value. + /// is + /// does not support reading. [CLSCompliant(false)] - public static uint ReadUInt32(this Stream stream) - { - return stream.ReadUInt32(DefaultEndianness); - } - - /// - /// Reads a four-byte unsigned integer from the current stream using the specified endian encoding, and advances the - /// stream position by four bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// An four-byte unsigned integer read from the stream. - [CLSCompliant(false)] - public static uint ReadUInt32(this Stream stream, Endianness endianness) + public static float ReadSingleLittleEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(uint)]; - stream.Read(buffer); + Span buffer = stackalloc byte[4]; + _ = stream.Read(buffer); +#if NET5_0_OR_GREATER + return BinaryPrimitives.ReadSingleLittleEndian(buffer); +#else + if (!BitConverter.IsLittleEndian) + { + buffer.Reverse(); + } - return endianness == Endianness.LittleEndian - ? BinaryPrimitives.ReadUInt32LittleEndian(buffer) - : BinaryPrimitives.ReadUInt32BigEndian(buffer); + return MemoryMarshal.Read(buffer); +#endif } /// - /// Reads an eight-byte unsigned integer from the current stream using the system's default endian encoding, and - /// advances the stream position by eight bytes. + /// Reads an from the current stream as big endian, and advances the stream position by two bytes. /// /// The stream from which the value should be read. - /// An eight-byte unsigned integer read from the stream. + /// The big endian value. + /// is + /// does not support reading. [CLSCompliant(false)] - public static ulong ReadUInt64(this Stream stream) - { - return stream.ReadUInt64(DefaultEndianness); - } - - /// - /// Reads an eight-byte unsigned integer from the current stream using the specified endian encoding, and advances the - /// stream position by eight bytes. - /// - /// The stream from which the value should be read. - /// The endian encoding to use. - /// An eight-byte unsigned integer read from the stream. - [CLSCompliant(false)] - public static ulong ReadUInt64(this Stream stream, Endianness endianness) + public static ushort ReadUInt16BigEndian(this Stream stream) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) + if (!stream.CanRead) { - throw new ArgumentOutOfRangeException(nameof(endianness)); + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) + + Span buffer = stackalloc byte[2]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadUInt16BigEndian(buffer); + } + + /// + /// Reads an from the current stream as little endian, and advances the stream position by two bytes. + /// + /// The stream from which the value should be read. + /// The little endian value. + /// is + /// does not support reading. + [CLSCompliant(false)] + public static ushort ReadUInt16LittleEndian(this Stream stream) + { + if (stream is null) { - throw new ArgumentOutOfRangeException(nameof(endianness)); + throw new ArgumentNullException(nameof(stream)); } -#endif if (!stream.CanRead) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); } - Span buffer = stackalloc byte[sizeof(ulong)]; - stream.Read(buffer); + Span buffer = stackalloc byte[2]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadUInt16LittleEndian(buffer); + } - return endianness == Endianness.LittleEndian - ? BinaryPrimitives.ReadUInt64LittleEndian(buffer) - : BinaryPrimitives.ReadUInt64BigEndian(buffer); + /// + /// Reads an from the current stream as big endian, and advances the stream position by four bytes. + /// + /// The stream from which the value should be read. + /// The big endian value. + /// is + /// does not support reading. + [CLSCompliant(false)] + public static uint ReadUInt32BigEndian(this Stream stream) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanRead) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); + } + + Span buffer = stackalloc byte[4]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadUInt32BigEndian(buffer); + } + + /// + /// Reads an from the current stream as little endian, and advances the stream position by four bytes. + /// + /// The stream from which the value should be read. + /// The little endian value. + /// is + /// does not support reading. + [CLSCompliant(false)] + public static uint ReadUInt32LittleEndian(this Stream stream) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanRead) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); + } + + Span buffer = stackalloc byte[4]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadUInt32LittleEndian(buffer); + } + + /// + /// Reads an from the current stream as big endian, and advances the stream position by eight bytes. + /// + /// The stream from which the value should be read. + /// The big endian value. + /// is + /// does not support reading. + [CLSCompliant(false)] + public static ulong ReadUInt64BigEndian(this Stream stream) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanRead) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); + } + + Span buffer = stackalloc byte[8]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadUInt64BigEndian(buffer); + } + + /// + /// Reads an from the current stream as little endian, and advances the stream position by eight + /// bytes. + /// + /// The stream from which the value should be read. + /// The little endian value. + /// is + /// does not support reading. + [CLSCompliant(false)] + public static ulong ReadUInt64LittleEndian(this Stream stream) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanRead) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportReading); + } + + Span buffer = stackalloc byte[8]; + _ = stream.Read(buffer); + return BinaryPrimitives.ReadUInt64LittleEndian(buffer); } /// @@ -605,582 +639,465 @@ public static class StreamExtensions } /// - /// Writes a two-byte signed integer to the current stream using the system's default endian encoding, and advances - /// the stream position by two bytes. + /// Writes a to the current stream as big endian, and advances the stream position by two bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteBigEndian(this Stream stream, short value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[2]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as big endian, and advances the stream position by four bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteBigEndian(this Stream stream, int value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[4]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as big endian, and advances the stream position by eight bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteBigEndian(this Stream stream, long value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[8]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as big endian, and advances the stream position by two bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + [CLSCompliant(false)] + public static int WriteBigEndian(this Stream stream, ushort value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[2]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as big endian, and advances the stream position by four bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + [CLSCompliant(false)] + public static int WriteBigEndian(this Stream stream, uint value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[4]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as big endian, and advances the stream position by eight bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + [CLSCompliant(false)] + public static int WriteBigEndian(this Stream stream, ulong value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[8]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by four bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteBigEndian(this Stream stream, float value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[4]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by eight + /// bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteBigEndian(this Stream stream, double value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[8]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by sixteen + /// bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteBigEndian(this Stream stream, decimal value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[16]; + value.TryWriteBigEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by two bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteLittleEndian(this Stream stream, short value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[2]; + value.TryWriteLittleEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes an to the current stream as little endian, and advances the stream position by four bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteLittleEndian(this Stream stream, int value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[4]; + value.TryWriteLittleEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by eight bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteLittleEndian(this Stream stream, long value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[8]; + value.TryWriteLittleEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by four bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteLittleEndian(this Stream stream, float value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[4]; + value.TryWriteLittleEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by eight + /// bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteLittleEndian(this Stream stream, double value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[8]; + value.TryWriteLittleEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by sixteen + /// bytes. + /// + /// The stream to which the value should be written. + /// The to write. + /// The number of bytes written to the stream. + /// is . + /// does not support writing. + public static int WriteLittleEndian(this Stream stream, decimal value) + { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[16]; + value.TryWriteLittleEndian(buffer); + return stream.WriteInternal(buffer); + } + + /// + /// Writes a to the current stream as little endian, and advances the stream position by two bytes. /// /// The stream to which the value should be written. /// The two-byte signed integer to write. /// The number of bytes written to the stream. - [ExcludeFromCodeCoverage] - public static int Write(this Stream stream, short value) + /// is . + /// does not support writing. + [CLSCompliant(false)] + public static int WriteLittleEndian(this Stream stream, ushort value) { - return stream.Write(value, DefaultEndianness); + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + + if (!stream.CanWrite) + { + throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); + } + + Span buffer = stackalloc byte[2]; + BinaryPrimitives.WriteUInt16LittleEndian(buffer, value); + return stream.WriteInternal(buffer); } /// - /// Writes a two-byte signed integer to the current stream using the specified endian encoding, and advances the - /// stream position by two bytes. + /// Writes a to the current stream as little endian, and advances the stream position by four bytes. /// /// The stream to which the value should be written. /// The two-byte signed integer to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - public static int Write(this Stream stream, short value, Endianness endianness) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - - if (!stream.CanWrite) - { - throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); - } - - Span buffer = stackalloc byte[sizeof(short)]; - - if (endianness == Endianness.LittleEndian) - { - BinaryPrimitives.WriteInt16LittleEndian(buffer, value); - } - else - { - BinaryPrimitives.WriteInt16BigEndian(buffer, value); - } - - return stream.WriteInternal(buffer); - } - - /// - /// Writes a four-byte signed integer to the current stream using the system's default endian encoding, and advances - /// the stream position by four bytes. - /// - /// The stream to which the value should be written. - /// The four-byte signed integer to write. - /// The number of bytes written to the stream. - [ExcludeFromCodeCoverage] - public static int Write(this Stream stream, int value) - { - return stream.Write(value, DefaultEndianness); - } - - /// - /// Writes a four-byte signed integer to the current stream using the specified endian encoding, and advances the - /// stream position by four bytes. - /// - /// The stream to which the value should be written. - /// The four-byte signed integer to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - public static int Write(this Stream stream, int value, Endianness endianness) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - - if (!stream.CanWrite) - { - throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); - } - - Span buffer = stackalloc byte[sizeof(int)]; - - if (endianness == Endianness.LittleEndian) - { - BinaryPrimitives.WriteInt32LittleEndian(buffer, value); - } - else - { - BinaryPrimitives.WriteInt32BigEndian(buffer, value); - } - - return stream.WriteInternal(buffer); - } - - /// - /// Writes an eight-byte signed integer to the current stream using the system's default endian encoding, and advances - /// the stream position by eight bytes. - /// - /// The stream to which the value should be written. - /// The eight-byte signed integer to write. - /// The number of bytes written to the stream. - /// is . - [ExcludeFromCodeCoverage] - public static int Write(this Stream stream, long value) - { - return stream.Write(value, DefaultEndianness); - } - - /// - /// Writes an eight-byte signed integer to the current stream using the specified endian encoding, and advances the - /// stream position by eight bytes. - /// - /// The stream to which the value should be written. - /// The eight-byte signed integer to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - public static int Write(this Stream stream, long value, Endianness endianness) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - - if (!stream.CanWrite) - { - throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); - } - - Span buffer = stackalloc byte[sizeof(long)]; - - if (endianness == Endianness.LittleEndian) - { - BinaryPrimitives.WriteInt64LittleEndian(buffer, value); - } - else - { - BinaryPrimitives.WriteInt64BigEndian(buffer, value); - } - - return stream.WriteInternal(buffer); - } - - /// - /// Writes a two-byte unsigned integer to the current stream using the system's default endian encoding, and advances - /// the stream position by two bytes. - /// - /// The stream to which the value should be written. - /// The two-byte unsigned integer to write. /// The number of bytes written to the stream. /// is . + /// does not support writing. [CLSCompliant(false)] - [ExcludeFromCodeCoverage] - public static int Write(this Stream stream, ushort value) - { - return stream.Write(value, DefaultEndianness); - } - - /// - /// Writes a two-byte unsigned integer to the current stream using the specified endian encoding, and advances the - /// stream position by two bytes. - /// - /// The stream to which the value should be written. - /// The two-byte unsigned integer to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - [CLSCompliant(false)] - public static int Write(this Stream stream, ushort value, Endianness endianness) + public static int WriteLittleEndian(this Stream stream, uint value) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanWrite) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); } - Span buffer = stackalloc byte[sizeof(ushort)]; - - if (endianness == Endianness.LittleEndian) - { - BinaryPrimitives.WriteUInt16LittleEndian(buffer, value); - } - else - { - BinaryPrimitives.WriteUInt16BigEndian(buffer, value); - } - + Span buffer = stackalloc byte[4]; + BinaryPrimitives.WriteUInt32LittleEndian(buffer, value); return stream.WriteInternal(buffer); } /// - /// Writes a four-byte unsigned integer to the current stream using the system's default endian encoding, and advances - /// the stream position by four bytes. + /// Writes a to the current stream as little endian, and advances the stream position by eight bytes. /// /// The stream to which the value should be written. - /// The four-byte unsigned integer to write. + /// The two-byte signed integer to write. /// The number of bytes written to the stream. /// is . + /// does not support writing. [CLSCompliant(false)] - [ExcludeFromCodeCoverage] - public static int Write(this Stream stream, uint value) - { - return stream.Write(value, DefaultEndianness); - } - - /// - /// Writes a four-byte unsigned integer to the current stream using the specified endian encoding, and advances the - /// stream position by four bytes. - /// - /// The stream to which the value should be written. - /// The four-byte unsigned integer to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - [CLSCompliant(false)] - public static int Write(this Stream stream, uint value, Endianness endianness) + public static int WriteLittleEndian(this Stream stream, ulong value) { if (stream is null) { throw new ArgumentNullException(nameof(stream)); } -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - if (!stream.CanWrite) { throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); } - Span buffer = stackalloc byte[sizeof(uint)]; - - if (endianness == Endianness.LittleEndian) - { - BinaryPrimitives.WriteUInt32LittleEndian(buffer, value); - } - else - { - BinaryPrimitives.WriteUInt32BigEndian(buffer, value); - } - + Span buffer = stackalloc byte[8]; + BinaryPrimitives.WriteUInt64LittleEndian(buffer, value); return stream.WriteInternal(buffer); } - /// - /// Writes an eight-byte unsigned integer to the current stream using the system's default endian encoding, and - /// advances the stream position by eight bytes. - /// - /// The stream to which the value should be written. - /// The eight-byte unsigned integer to write. - /// The number of bytes written to the stream. - /// is . - [CLSCompliant(false)] - [ExcludeFromCodeCoverage] - public static int Write(this Stream stream, ulong value) - { - return stream.Write(value, DefaultEndianness); - } - - /// - /// Writes an eight-byte signed integer to the current stream using the specified endian encoding, and advances the - /// stream position by eight bytes. - /// - /// The stream to which the value should be written. - /// The eight-byte signed integer to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - [CLSCompliant(false)] - public static int Write(this Stream stream, ulong value, Endianness endianness) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - - if (!stream.CanWrite) - { - throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); - } - - Span buffer = stackalloc byte[sizeof(ulong)]; - - if (endianness == Endianness.LittleEndian) - { - BinaryPrimitives.WriteUInt64LittleEndian(buffer, value); - } - else - { - BinaryPrimitives.WriteUInt64BigEndian(buffer, value); - } - - return stream.WriteInternal(buffer); - } - - /// - /// Writes a single-precision floating point value to the current stream using the specified endian encoding, and - /// advances the stream position by four bytes. - /// - /// The stream to which the value should be written. - /// The single-precision floating point value to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - public static int Write(this Stream stream, float value, Endianness endianness) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - - if (!stream.CanWrite) - { - throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); - } - - Span buffer = stackalloc byte[sizeof(float)]; - - if (endianness == Endianness.LittleEndian) - { -#if NET5_0_OR_GREATER - BinaryPrimitives.WriteSingleLittleEndian(buffer, value); -#else - if (BitConverter.IsLittleEndian) - { - MemoryMarshal.Write(buffer, ref value); - } - else - { - // dotcover disable - //NOSONAR - int temp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); - MemoryMarshal.Write(buffer, ref temp); - //NOSONAR - // dotcover enable - } -#endif - } - else - { -#if NET5_0_OR_GREATER - BinaryPrimitives.WriteSingleBigEndian(buffer, value); -#else - if (BitConverter.IsLittleEndian) - { - int temp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); - MemoryMarshal.Write(buffer, ref temp); - } - else - { - // dotcover disable - //NOSONAR - MemoryMarshal.Write(buffer, ref value); - //NOSONAR - // dotcover enable - } -#endif - } - - return stream.WriteInternal(buffer); - } - - /// - /// Writes a double-precision floating point value to the current stream using the specified endian encoding, and - /// advances the stream position by eight bytes. - /// - /// The stream to which the value should be written. - /// The double-precision floating point value to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - public static int Write(this Stream stream, double value, Endianness endianness) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - - if (!stream.CanWrite) - { - throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); - } - - Span buffer = stackalloc byte[sizeof(double)]; - - if (endianness == Endianness.LittleEndian) - { -#if NET5_0_OR_GREATER - BinaryPrimitives.WriteDoubleLittleEndian(buffer, value); -#else - if (BitConverter.IsLittleEndian) - { - MemoryMarshal.Write(buffer, ref value); - } - else - { - // dotcover disable - //NOSONAR - long temp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); - MemoryMarshal.Write(buffer, ref temp); - //NOSONAR - // dotcover enable - } -#endif - } - else - { -#if NET5_0_OR_GREATER - BinaryPrimitives.WriteDoubleBigEndian(buffer, value); -#else - if (BitConverter.IsLittleEndian) - { - long temp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); - MemoryMarshal.Write(buffer, ref temp); - } - else - { - // dotcover disable - //NOSONAR - MemoryMarshal.Write(buffer, ref value); - //NOSONAR - // dotcover enable - } -#endif - } - - return stream.WriteInternal(buffer); - } - - /// - /// Writes a decimal value to the current stream using the specified endian encoding, and advances the stream position - /// by sixteen bytes. - /// - /// The stream to which the value should be written. - /// The decimal value to write. - /// The endian encoding to use. - /// The number of bytes written to the stream. - /// is . - [ExcludeFromCodeCoverage] - public static int Write(this Stream stream, decimal value, Endianness endianness) - { - if (stream is null) - { - throw new ArgumentNullException(nameof(stream)); - } - -#if NET5_0_OR_GREATER - if (!Enum.IsDefined(endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#else - if (!Enum.IsDefined(typeof(Endianness), endianness)) - { - throw new ArgumentOutOfRangeException(nameof(endianness)); - } -#endif - - if (!stream.CanWrite) - { - throw new ArgumentException(ExceptionMessages.StreamDoesNotSupportWriting); - } - - int[] bits = decimal.GetBits(value); - long preWritePosition = stream.Position; - - if (endianness != DefaultEndianness) - { - Array.Reverse(bits); - } - - foreach (int section in bits) - { - stream.Write(section, endianness); - } - - return (int)(stream.Position - preWritePosition); - } - private static int WriteInternal(this Stream stream, ReadOnlySpan value) { long preWritePosition = stream.Position; diff --git a/X10D/src/IO/UInt16Extensions.cs b/X10D/src/IO/UInt16Extensions.cs index 5eb12ae..2cbc777 100644 --- a/X10D/src/IO/UInt16Extensions.cs +++ b/X10D/src/IO/UInt16Extensions.cs @@ -10,54 +10,50 @@ namespace X10D.IO; public static class UInt16Extensions { /// - /// Returns the current 16-bit unsigned integer value as an array of bytes. + /// Converts the current 16-bit unsigned integer into an array of bytes, as big endian. /// - /// The number to convert. - /// An array of bytes with length 2. + /// The value. + /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this ushort value) + public static byte[] GetBigEndianBytes(this ushort value) { Span buffer = stackalloc byte[2]; - value.TryWriteBytes(buffer); + value.TryWriteBigEndian(buffer); return buffer.ToArray(); } /// - /// Returns the current 16-bit unsigned integer value as an array of bytes. + /// Converts the current 16-bit unsigned integer into an array of bytes, as big endian. /// - /// The number to convert. - /// The endianness with which to write the bytes. - /// An array of bytes with length 2. + /// The value. + /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this ushort value, Endianness endianness) + public static byte[] GetLittleEndianBytes(this ushort value) { Span buffer = stackalloc byte[2]; - value.TryWriteBytes(buffer, endianness); + value.TryWriteLittleEndian(buffer); return buffer.ToArray(); } /// - /// Converts the current 16-bit unsigned integer into a span of bytes. + /// Writes the current 16-bit unsigned integer into a span of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . + /// The span of bytes where the value is to be written, as big endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this ushort value, Span destination) + public static bool TryWriteBigEndian(this ushort value, Span destination) { - return BitConverter.TryWriteBytes(destination, value); + return BinaryPrimitives.TryWriteUInt16BigEndian(destination, value); } /// - /// Converts the current 16-bit unsigned integer into a span of bytes. + /// Writes the current 16-bit unsigned integer into a span of bytes, as little endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. + /// The span of bytes where the value is to be written, as little endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this ushort value, Span destination, Endianness endianness) + public static bool TryWriteLittleEndian(this ushort value, Span destination) { - return endianness == Endianness.BigEndian - ? BinaryPrimitives.TryWriteUInt16BigEndian(destination, value) - : BinaryPrimitives.TryWriteUInt16LittleEndian(destination, value); + return BinaryPrimitives.TryWriteUInt16LittleEndian(destination, value); } } diff --git a/X10D/src/IO/UInt32Extensions.cs b/X10D/src/IO/UInt32Extensions.cs index c4f3c30..12845ea 100644 --- a/X10D/src/IO/UInt32Extensions.cs +++ b/X10D/src/IO/UInt32Extensions.cs @@ -10,54 +10,50 @@ namespace X10D.IO; public static class UInt32Extensions { /// - /// Returns the current 32-bit unsigned integer value as an array of bytes. + /// Converts the current 32-bit unsigned integer into an array of bytes, as big endian. /// - /// The number to convert. - /// An array of bytes with length 4. + /// The value. + /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this uint value) + public static byte[] GetBigEndianBytes(this uint value) { Span buffer = stackalloc byte[4]; - value.TryWriteBytes(buffer); + value.TryWriteBigEndian(buffer); return buffer.ToArray(); } /// - /// Returns the current 32-bit unsigned integer value as an array of bytes. + /// Converts the current 32-bit unsigned integer into an array of bytes, as big endian. /// - /// The number to convert. - /// The endianness with which to write the bytes. - /// An array of bytes with length 4. + /// The value. + /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this uint value, Endianness endianness) + public static byte[] GetLittleEndianBytes(this uint value) { Span buffer = stackalloc byte[4]; - value.TryWriteBytes(buffer, endianness); + value.TryWriteLittleEndian(buffer); return buffer.ToArray(); } /// - /// Converts the current 32-bit unsigned integer into a span of bytes. + /// Writes the current 32-bit unsigned integer into a span of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . + /// The span of bytes where the value is to be written, as big endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this uint value, Span destination) + public static bool TryWriteBigEndian(this uint value, Span destination) { - return BitConverter.TryWriteBytes(destination, value); + return BinaryPrimitives.TryWriteUInt32BigEndian(destination, value); } /// - /// Converts the current 32-bit unsigned integer into a span of bytes. + /// Writes the current 32-bit unsigned integer into a span of bytes, as little endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. + /// The span of bytes where the value is to be written, as little endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this uint value, Span destination, Endianness endianness) + public static bool TryWriteLittleEndian(this uint value, Span destination) { - return endianness == Endianness.BigEndian - ? BinaryPrimitives.TryWriteUInt32BigEndian(destination, value) - : BinaryPrimitives.TryWriteUInt32LittleEndian(destination, value); + return BinaryPrimitives.TryWriteUInt32LittleEndian(destination, value); } } diff --git a/X10D/src/IO/UInt64Extensions.cs b/X10D/src/IO/UInt64Extensions.cs index 1f48869..96f78c1 100644 --- a/X10D/src/IO/UInt64Extensions.cs +++ b/X10D/src/IO/UInt64Extensions.cs @@ -10,54 +10,50 @@ namespace X10D.IO; public static class UInt64Extensions { /// - /// Returns the current 64-bit unsigned integer value as an array of bytes. + /// Converts the current 64-bit unsigned integer into an array of bytes, as big endian. /// - /// The number to convert. + /// The value. /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this ulong value) + public static byte[] GetBigEndianBytes(this ulong value) { Span buffer = stackalloc byte[8]; - value.TryWriteBytes(buffer); + value.TryWriteBigEndian(buffer); return buffer.ToArray(); } /// - /// Returns the current 64-bit unsigned integer value as an array of bytes. + /// Converts the current 64-bit unsigned integer into an array of bytes, as big endian. /// - /// The number to convert. - /// The endianness with which to write the bytes. + /// The value. /// An array of bytes with length 8. [Pure] - public static byte[] GetBytes(this ulong value, Endianness endianness) + public static byte[] GetLittleEndianBytes(this ulong value) { Span buffer = stackalloc byte[8]; - value.TryWriteBytes(buffer, endianness); + value.TryWriteLittleEndian(buffer); return buffer.ToArray(); } /// - /// Converts the current 64-bit unsigned integer into a span of bytes. + /// Writes the current 64-bit unsigned integer into a span of bytes, as big endian. /// /// The value. - /// When this method returns, the bytes representing the converted . + /// The span of bytes where the value is to be written, as big endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this ulong value, Span destination) + public static bool TryWriteBigEndian(this ulong value, Span destination) { - return BitConverter.TryWriteBytes(destination, value); + return BinaryPrimitives.TryWriteUInt64BigEndian(destination, value); } /// - /// Converts the current 64-bit unsigned integer into a span of bytes. + /// Writes the current 64-bit unsigned integer into a span of bytes, as little endian. /// /// The value. - /// When this method returns, the bytes representing the converted . - /// The endianness with which to write the bytes. + /// The span of bytes where the value is to be written, as little endian. /// if the conversion was successful; otherwise, . - public static bool TryWriteBytes(this ulong value, Span destination, Endianness endianness) + public static bool TryWriteLittleEndian(this ulong value, Span destination) { - return endianness == Endianness.BigEndian - ? BinaryPrimitives.TryWriteUInt64BigEndian(destination, value) - : BinaryPrimitives.TryWriteUInt64LittleEndian(destination, value); + return BinaryPrimitives.TryWriteUInt64LittleEndian(destination, value); } }