Compare commits

...

3 Commits

Author SHA1 Message Date
Oliver Booth 28d7bee262
fix(tests): add support for trace logging during tests 2023-08-23 14:18:04 +01:00
Oliver Booth 0bf89bb82a
refactor!: change exception thrown by GetHash and TryWriteHash
The methods no longer throw TypeInitializationException, and instead now throw ArgumentException.
2023-08-23 14:17:42 +01:00
Oliver Booth 5c21c86a52
refactor!: replace Endianness enum with explicit Big/Little methods 2023-08-23 14:15:52 +01:00
40 changed files with 1864 additions and 1787 deletions

View File

@ -34,10 +34,15 @@ 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.
- X10D: `Stream.GetHash<>` and `Stream.TryWriteHash<>` now throw ArgumentException in lieu of
TypeInitializationException.
### Removed
- X10D: Removed `IEnumerable<T>.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

View File

@ -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<byte> 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<byte> 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<byte> 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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteLittleEndian(buffer), Is.False);
}
}

View File

@ -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<byte> buffer = stackalloc byte[2];
Assert.That(value.TryWriteBytes(buffer));
CollectionAssert.AreEqual(bytes, buffer.ToArray());
byte[] expected = { 0x0F, 0 };
Span<byte> 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<byte> 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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteBigEndian(buffer), Is.False);
}
}

View File

@ -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<byte> buffer = stackalloc byte[4];
Assert.That(value.TryWriteBytes(buffer));
CollectionAssert.AreEqual(bytes, buffer.ToArray());
var expected = new byte[] { 0, 0, 0, 0x0F };
Span<byte> 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<byte> 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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteLittleEndian(buffer), Is.False);
}
}

View File

@ -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<byte> 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<byte> 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<byte> 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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteBigEndian(buffer), Is.False);
}
}

View File

@ -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<byte> buffer = stackalloc byte[4];
Assert.That(value.TryWriteBytes(buffer));
CollectionAssert.AreEqual(bytes, buffer.ToArray());
var expected = new byte[] { 0x42, 0x2A, 0, 0 };
Span<byte> 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<byte> 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<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteLittleEndian(buffer), Is.False);
}
}

View File

@ -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<ArgumentException>(() => stream.ReadDecimal());
Assert.Throws<ArgumentException>(() => stream.ReadDecimal(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadDecimal(Endianness.BigEndian));
}
[Test]
public void ReadDecimal_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadDecimalBigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadDecimal());
Assert.Throws<ArgumentNullException>(() => stream.ReadDecimal(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadDecimal(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadDecimal((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadDecimalBigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadDecimalLittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadDecimalLittleEndian());
}
[Test]
public void ReadDecimalBigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> 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<byte> 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(() =>
{

View File

@ -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<ArgumentException>(() => stream.ReadDouble());
Assert.Throws<ArgumentException>(() => stream.ReadDouble(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadDouble(Endianness.BigEndian));
}
[Test]
public void ReadDouble_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadDoubleBigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadDouble());
Assert.Throws<ArgumentNullException>(() => stream.ReadDouble(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadDouble(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadDouble((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadDoubleBigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadDoubleLittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadDoubleLittleEndian());
}
[Test]
public void ReadDoubleBigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x40, 0x7A, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x40};
ReadOnlySpan<byte> 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));

View File

@ -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<ArgumentException>(() => stream.ReadInt16());
Assert.Throws<ArgumentException>(() => stream.ReadInt16(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadInt16(Endianness.BigEndian));
}
[Test]
public void ReadInt16_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadInt16BigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadInt16());
Assert.Throws<ArgumentNullException>(() => stream.ReadInt16(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadInt16(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadInt16((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadInt16BigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadInt16LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadInt16LittleEndian());
}
[Test]
public void ReadInt16BigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0xA4, 0x01};
ReadOnlySpan<byte> 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));

View File

@ -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<ArgumentException>(() => stream.ReadInt32());
Assert.Throws<ArgumentException>(() => stream.ReadInt32(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadInt32(Endianness.BigEndian));
}
[Test]
public void ReadInt32_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadInt32BigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadInt32());
Assert.Throws<ArgumentNullException>(() => stream.ReadInt32(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadInt32(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadInt32((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadInt32BigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadInt32LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadInt32LittleEndian());
}
[Test]
public void ReadInt32BigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00};
ReadOnlySpan<byte> 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));

View File

@ -6,60 +6,58 @@ namespace X10D.Tests.IO;
internal partial class StreamTests
{
[Test]
public void ReadInt64_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadInt64());
Assert.Throws<ArgumentException>(() => stream.ReadInt64(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadInt64(Endianness.BigEndian));
}
[Test]
public void ReadInt64_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadInt64BigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadInt64());
Assert.Throws<ArgumentNullException>(() => stream.ReadInt64(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadInt64(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadInt64((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadInt64LittleEndian());
}
[Test]
public void ReadInt64_ShouldReadBigEndian_GivenBigEndian()
public void ReadInt64BigEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadInt64BigEndian());
}
[Test]
public void ReadInt64LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadInt64LittleEndian());
}
[Test]
public void ReadInt64BigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
ReadOnlySpan<byte> 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));

View File

@ -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<ArgumentException>(() => stream.ReadSingle());
Assert.Throws<ArgumentException>(() => stream.ReadSingle(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadSingle(Endianness.BigEndian));
}
[Test]
public void ReadSingle_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadSingleBigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadSingle());
Assert.Throws<ArgumentNullException>(() => stream.ReadSingle(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadSingle(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadSingle((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadSingleBigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadSingleLittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadSingleLittleEndian());
}
[Test]
public void ReadSingleBigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x43, 0xD2, 0x00, 0x00};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0x00, 0x00, 0xD2, 0x43};
ReadOnlySpan<byte> 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));

View File

@ -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<ArgumentException>(() => stream.ReadUInt16());
Assert.Throws<ArgumentException>(() => stream.ReadUInt16(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadUInt16(Endianness.BigEndian));
}
[Test]
public void ReadUInt16_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadUInt16BigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt16());
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt16(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt16(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadUInt16((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadUInt16BigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadUInt16LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadUInt16LittleEndian());
}
[Test]
public void ReadUInt16BigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0xA4, 0x01};
ReadOnlySpan<byte> 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));

View File

@ -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<ArgumentException>(() => stream.ReadUInt32());
Assert.Throws<ArgumentException>(() => stream.ReadUInt32(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadUInt32(Endianness.BigEndian));
}
[Test]
public void ReadUInt32_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadUInt32BigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt32());
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt32(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt32(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadUInt32((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadUInt32BigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadUInt32LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadUInt32LittleEndian());
}
[Test]
public void ReadUInt32BigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00};
ReadOnlySpan<byte> 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));

View File

@ -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<ArgumentException>(() => stream.ReadUInt64());
Assert.Throws<ArgumentException>(() => stream.ReadUInt64(Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.ReadUInt64(Endianness.BigEndian));
}
[Test]
public void ReadUInt64_ShouldThrowArgumentNullException_GivenNullStream()
public void ReadUInt64BigEndian_ShouldThrowArgumentNullException_GivenNullStream()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt64());
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt64(Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.ReadUInt64(Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.ReadUInt64((Endianness)(-1)));
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => stream.ReadUInt64BigEndian());
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void ReadUInt64LittleEndian_ShouldThrowArgumentException_GivenNonReadableStream()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.ReadUInt64LittleEndian());
}
[Test]
public void ReadUInt64BigEndian_ShouldReadBigEndian()
{
using var stream = new MemoryStream();
ReadOnlySpan<byte> bytes = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> bytes = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
ReadOnlySpan<byte> 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));

View File

@ -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<ArgumentException>(() => stream.Write(420.0m, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write(420.0m, Endianness.BigEndian));
}
[Test]
public void WriteDecimal_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDecimalArgument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write(420.0m, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write(420.0m, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write(420.0m, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(420.0m, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420.0m));
}
[Test]
public void WriteDecimal_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDecimalArgument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => 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<byte> actual = stackalloc byte[16];
ReadOnlySpan<byte> 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;

View File

@ -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<ArgumentException>(() => stream.Write(420.0, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write(420.0, Endianness.BigEndian));
}
[Test]
public void WriteDouble_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDoubleArgument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write(420.0, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write(420.0, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write(420.0, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(420.0, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420.0));
}
[Test]
public void WriteDouble_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndDoubleArgument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => 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<byte> actual = stackalloc byte[8];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x40, 0x7A, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[8];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x40};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x7A, 0x40 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(8));

View File

@ -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<ArgumentException>(() => stream.Write((short)420, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write((short)420, Endianness.BigEndian));
}
[Test]
public void WriteInt16_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt16Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write((short)420, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write((short)420, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write((short)420, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write((short)420, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian((short)420));
}
[Test]
public void WriteInt16_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt16Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => 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<byte> actual = stackalloc byte[2];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[2];
ReadOnlySpan<byte> expected = stackalloc byte[] {0xA4, 0x01};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0xA4, 0x01 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(2));

View File

@ -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<ArgumentException>(() => stream.Write(420, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write(420, Endianness.BigEndian));
}
[Test]
public void WriteInt32_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt32Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write(420, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write(420, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write(420, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(420, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420));
}
[Test]
public void WriteInt32_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt32Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.WriteLittleEndian(420));
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt32Argument()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => 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<byte> actual = stackalloc byte[4];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[4];
ReadOnlySpan<byte> expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(4));

View File

@ -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<ArgumentException>(() => stream.Write(420L, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write(420L, Endianness.BigEndian));
}
[Test]
public void WriteInt64_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt64Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write(420L, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write(420L, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write(420L, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(420L, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420L));
}
[Test]
public void WriteInt64_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndInt64Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.WriteLittleEndian(420L));
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndInt64Argument()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => 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<byte> actual = stackalloc byte[8];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[8];
ReadOnlySpan<byte> expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(8));

View File

@ -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<ArgumentException>(() => stream.Write(420.0f, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write(420.0f, Endianness.BigEndian));
}
[Test]
public void WriteSingle_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndSingleArgument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write(420.0f, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write(420.0f, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write(420.0f, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(420.0f, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420.0f));
}
[Test]
public void WriteSingle_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndSingleArgument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => 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<byte> actual = stackalloc byte[4];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x43, 0xD2, 0x00, 0x00};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[4];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x00, 0x00, 0xD2, 0x43};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0x00, 0x00, 0xD2, 0x43 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(4));

View File

@ -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<ArgumentException>(() => stream.Write((ushort)420, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write((ushort)420, Endianness.BigEndian));
}
[Test]
public void WriteUInt16_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt16Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write((ushort)420, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write((ushort)420, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write((ushort)420, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write((ushort)420, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian((ushort)420));
}
[Test]
public void WriteUInt16_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt16Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => 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<ArgumentException>(() => 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<byte> actual = stackalloc byte[2];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[2];
ReadOnlySpan<byte> expected = stackalloc byte[] {0xA4, 0x01};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0xA4, 0x01 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(2));

View File

@ -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<ArgumentException>(() => stream.Write(420U, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write(420U, Endianness.BigEndian));
}
[Test]
public void WriteUInt32_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt32Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write(420U, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write(420U, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write(420U, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(420U, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420U));
}
[Test]
public void WriteUInt32_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt32Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.WriteLittleEndian(420U));
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt32Argument()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => 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<byte> actual = stackalloc byte[4];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[4];
ReadOnlySpan<byte> expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(4));

View File

@ -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<ArgumentException>(() => stream.Write(420UL, Endianness.LittleEndian));
Assert.Throws<ArgumentException>(() => stream.Write(420UL, Endianness.BigEndian));
}
[Test]
public void WriteUInt64_ShouldThrowArgumentNullException_GivenNullStream()
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.Write(420UL, Endianness.LittleEndian));
Assert.Throws<ArgumentNullException>(() => stream.Write(420UL, Endianness.BigEndian));
Assert.Throws<ArgumentNullException>(() => 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<ArgumentOutOfRangeException>(() => stream.Write(420UL, (Endianness)(-1)));
Assert.Throws<ArgumentOutOfRangeException>(() => stream.Write(420UL, (Endianness)(-1)));
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420UL));
}
[Test]
public void WriteUInt64_ShouldWriteBigEndian_GivenBigEndian()
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument()
{
Stream stream = null!;
Assert.Throws<ArgumentNullException>(() => stream.WriteLittleEndian(420UL));
}
[Test]
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt64Argument()
{
Stream stream = new DummyStream();
Assert.Throws<ArgumentException>(() => 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<byte> actual = stackalloc byte[8];
ReadOnlySpan<byte> expected = stackalloc byte[] {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4};
ReadOnlySpan<byte> 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<byte> actual = stackalloc byte[8];
ReadOnlySpan<byte> expected = stackalloc byte[] {0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
ReadOnlySpan<byte> expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
int read = stream.Read(actual);
Assert.That(read, Is.EqualTo(8));

View File

@ -86,16 +86,15 @@ internal partial class StreamTests
[Test]
public void NullCreateMethodShouldThrow()
{
Assert.Throws<TypeInitializationException>(() => Stream.Null.GetHash<HashAlgorithmTestClass>());
Assert.Throws<TypeInitializationException>(() =>
Stream.Null.TryWriteHash<HashAlgorithmTestClass>(Span<byte>.Empty, out _));
Assert.Throws<ArgumentException>(() => Stream.Null.GetHash<HashAlgorithmTestClass>());
Assert.Throws<ArgumentException>(() => Stream.Null.TryWriteHash<HashAlgorithmTestClass>(Span<byte>.Empty, out _));
}
[Test]
public void NoCreateMethodShouldThrow()
{
Assert.Throws<TypeInitializationException>(() => Stream.Null.GetHash<HashAlgorithmTestClassNoCreateMethod>());
Assert.Throws<TypeInitializationException>(() =>
Assert.Throws<ArgumentException>(() => Stream.Null.GetHash<HashAlgorithmTestClassNoCreateMethod>());
Assert.Throws<ArgumentException>(() =>
Stream.Null.TryWriteHash<HashAlgorithmTestClassNoCreateMethod>(Span<byte>.Empty, out _));
}

View File

@ -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<byte> buffer = stackalloc byte[2];
Assert.That(value.TryWriteBytes(buffer));
CollectionAssert.AreEqual(bytes, buffer.ToArray());
Span<byte> 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<byte> buffer = stackalloc byte[2];
Span<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteBigEndian(buffer), Is.False);
}
}

View File

@ -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<byte> buffer = stackalloc byte[4];
Assert.That(value.TryWriteBytes(buffer));
CollectionAssert.AreEqual(bytes, buffer.ToArray());
Span<byte> 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<byte> buffer = stackalloc byte[4];
Span<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteBigEndian(buffer), Is.False);
}
}

View File

@ -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<byte> buffer = stackalloc byte[8];
Assert.That(value.TryWriteBytes(buffer));
CollectionAssert.AreEqual(bytes, buffer.ToArray());
Span<byte> 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<byte> buffer = stackalloc byte[8];
Span<byte> 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<byte> 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<byte> buffer = stackalloc byte[0];
Assert.That(value.TryWriteBigEndian(buffer), Is.False);
}
}

View File

@ -0,0 +1,20 @@
using System.Diagnostics;
using NUnit.Framework;
namespace X10D.Tests;
[SetUpFixture]
internal sealed class SetupTrace
{
[OneTimeSetUp]
public void StartTest()
{
Trace.Listeners.Add(new ConsoleTraceListener());
}
[OneTimeTearDown]
public void EndTest()
{
Trace.Flush();
}
}

View File

@ -1,19 +0,0 @@
using System.ComponentModel;
namespace X10D;
/// <summary>
/// Represents an enumeration of endianness values.
/// </summary>
public enum Endianness
{
/// <summary>
/// The value should be read as though it uses little endian encoding.
/// </summary>
[Description("The value should be read as though it uses little endian encoding.")] LittleEndian,
/// <summary>
/// The value should be read as though it uses big endian encoding.
/// </summary>
[Description("The value should be read as though it uses big endian encoding.")] BigEndian
}

View File

@ -0,0 +1,111 @@
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="decimal" />.
/// </summary>
public static class DecimalExtensions
{
/// <summary>
/// Converts the current decimal number into an array of bytes, as little endian.
/// </summary>
/// <param name="value">The <see cref="int" /> value.</param>
/// <returns>An array of bytes with length 4.</returns>
[Pure]
public static byte[] GetBigEndianBytes(this decimal value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// Converts the current decimal number into an array of bytes, as little endian.
/// </summary>
/// <param name="value">The <see cref="int" /> value.</param>
/// <returns>An array of bytes with length 4.</returns>
[Pure]
public static byte[] GetLittleEndianBytes(this decimal value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// Converts the current decimal number into a span of bytes, as big endian.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBigEndian(this decimal value, Span<byte> destination)
{
Span<int> 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;
}
/// <summary>
/// Converts the current decimal number into a span of bytes, as little endian.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteLittleEndian(this decimal value, Span<byte> destination)
{
Span<int> 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<int> destination)
{
#if NET5_0_OR_GREATER
decimal.GetBits(value, destination);
#else
Span<byte> buffer = stackalloc byte[16];
MemoryMarshal.Write(buffer, ref value);
var flags = MemoryMarshal.Read<int>(buffer[..4]);
var hi = MemoryMarshal.Read<int>(buffer[4..8]);
var lo = MemoryMarshal.Read<long>(buffer[8..]);
destination[0] = flags;
destination[1] = hi;
destination[2] = (int)(lo & 0xFFFFFFFF);
destination[3] = (int)(lo >> 32);
#endif
}
}

View File

@ -10,58 +10,70 @@ namespace X10D.IO;
public static class DoubleExtensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 8.</returns>
/// <param name="value">The <see cref="int" /> value.</param>
/// <returns>An array of bytes with length 4.</returns>
[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<byte> buffer = stackalloc byte[8];
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 8.</returns>
/// <param name="value">The <see cref="int" /> value.</param>
/// <returns>An array of bytes with length 4.</returns>
[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<byte> buffer = stackalloc byte[8];
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="double" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="double" />.</param>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this double value, Span<byte> destination)
public static bool TryWriteBigEndian(this double value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current double-precision floating-point into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="double" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="double" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this double value, Span<byte> destination, Endianness endianness)
#if NET5_0_OR_GREATER
return BinaryPrimitives.TryWriteDoubleBigEndian(destination, value);
#else
if (BitConverter.IsLittleEndian)
{
if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian))
{
long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
value = BitConverter.Int64BitsToDouble(tmp);
}
return MemoryMarshal.TryWrite(destination, ref value);
}
long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
return MemoryMarshal.TryWrite(destination, ref tmp);
#endif
}
/// <summary>
/// Converts the current double-precision floating-point number into a span of bytes, as little endian.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteLittleEndian(this double value, Span<byte> 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
}
}

View File

@ -9,54 +9,50 @@ namespace X10D.IO;
public static class Int16Extensions
{
/// <summary>
/// Returns the current 16-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
[Pure]
public static byte[] GetBytes(this short value)
{
byte[] buffer = new byte[2];
value.TryWriteBytes(buffer);
return buffer;
}
/// <summary>
/// Returns the current 16-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 2.</returns>
[Pure]
public static byte[] GetBytes(this short value, Endianness endianness)
{
byte[] buffer = new byte[2];
value.TryWriteBytes(buffer, endianness);
return buffer;
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="short" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="short" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this short value, Span<byte> destination)
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBigEndianBytes(this short value)
{
return BitConverter.TryWriteBytes(destination, value);
Span<byte> buffer = stackalloc byte[2];
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="short" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="short" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this short value, Span<byte> destination, Endianness endianness)
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetLittleEndianBytes(this short value)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteInt16BigEndian(destination, value)
: BinaryPrimitives.TryWriteInt16LittleEndian(destination, value);
Span<byte> buffer = stackalloc byte[2];
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// Writes the current 16-bit signed integer into a span of bytes, as big endian.
/// </summary>
/// <param name="value">The <see cref="short" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBigEndian(this short value, Span<byte> destination)
{
return BinaryPrimitives.TryWriteInt16BigEndian(destination, value);
}
/// <summary>
/// Writes the current 16-bit signed integer into a span of bytes, as little endian.
/// </summary>
/// <param name="value">The <see cref="short" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteLittleEndian(this short value, Span<byte> destination)
{
return BinaryPrimitives.TryWriteInt16LittleEndian(destination, value);
}
}

View File

@ -9,54 +9,50 @@ namespace X10D.IO;
public static class Int32Extensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
/// <param name="value">The <see cref="int" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[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;
}
/// <summary>
/// Returns the current 32-bit signed integer value as an array of bytes.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 4.</returns>
[Pure]
public static byte[] GetBytes(this int value, Endianness endianness)
{
byte[] buffer = new byte[4];
value.TryWriteBytes(buffer, endianness);
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="int" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="int" />.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this int value, Span<byte> destination)
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetLittleEndianBytes(this int value)
{
return BitConverter.TryWriteBytes(destination, value);
Span<byte> buffer = stackalloc byte[4];
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="int" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="int" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this int value, Span<byte> destination, Endianness endianness)
public static bool TryWriteBigEndian(this int value, Span<byte> destination)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteInt32BigEndian(destination, value)
: BinaryPrimitives.TryWriteInt32LittleEndian(destination, value);
return BinaryPrimitives.TryWriteInt32BigEndian(destination, value);
}
/// <summary>
/// Writes the current 32-bit signed integer into a span of bytes, as little endian.
/// </summary>
/// <param name="value">The <see cref="int" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteLittleEndian(this int value, Span<byte> destination)
{
return BinaryPrimitives.TryWriteInt32LittleEndian(destination, value);
}
}

View File

@ -9,54 +9,50 @@ namespace X10D.IO;
public static class Int64Extensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="value">The <see cref="long" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this long value)
public static byte[] GetBigEndianBytes(this long value)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer);
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="value">The <see cref="long" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this long value, Endianness endianness)
public static byte[] GetLittleEndianBytes(this long value)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer, endianness);
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="long" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="long" />.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this long value, Span<byte> destination)
public static bool TryWriteBigEndian(this long value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
return BinaryPrimitives.TryWriteInt64BigEndian(destination, value);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="long" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="long" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this long value, Span<byte> destination, Endianness endianness)
public static bool TryWriteLittleEndian(this long value, Span<byte> destination)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteInt64BigEndian(destination, value)
: BinaryPrimitives.TryWriteInt64LittleEndian(destination, value);
return BinaryPrimitives.TryWriteInt64LittleEndian(destination, value);
}
}

View File

@ -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
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="value">The <see cref="int" /> value.</param>
/// <returns>An array of bytes with length 4.</returns>
[Pure]
public static byte[] GetBytes(this float value)
public static byte[] GetBigEndianBytes(this float value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer);
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="value">The <see cref="int" /> value.</param>
/// <returns>An array of bytes with length 4.</returns>
[Pure]
public static byte[] GetBytes(this float value, Endianness endianness)
public static byte[] GetLittleEndianBytes(this float value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer, endianness);
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="float" />.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this float value, Span<byte> destination)
public static bool TryWriteBigEndian(this float value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
}
/// <summary>
/// Converts the current single-precision floating-point into a span of bytes.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="float" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this float value, Span<byte> destination, Endianness endianness)
#if NET5_0_OR_GREATER
return BinaryPrimitives.TryWriteSingleBigEndian(destination, value);
#else
if (BitConverter.IsLittleEndian)
{
if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian))
{
int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value));
value = BitConverter.Int32BitsToSingle(tmp);
}
return MemoryMarshal.TryWrite(destination, ref value);
}
int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value));
return MemoryMarshal.TryWrite(destination, ref tmp);
#endif
}
/// <summary>
/// Converts the current single-precision floating-point number into a span of bytes, as little endian.
/// </summary>
/// <param name="value">The <see cref="float" /> value.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteLittleEndian(this float value, Span<byte> 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
}
}

File diff suppressed because it is too large Load Diff

View File

@ -10,54 +10,50 @@ namespace X10D.IO;
public static class UInt16Extensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 2.</returns>
/// <param name="value">The <see cref="ushort" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this ushort value)
public static byte[] GetBigEndianBytes(this ushort value)
{
Span<byte> buffer = stackalloc byte[2];
value.TryWriteBytes(buffer);
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 2.</returns>
/// <param name="value">The <see cref="ushort" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this ushort value, Endianness endianness)
public static byte[] GetLittleEndianBytes(this ushort value)
{
Span<byte> buffer = stackalloc byte[2];
value.TryWriteBytes(buffer, endianness);
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="ushort" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ushort" />.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ushort value, Span<byte> destination)
public static bool TryWriteBigEndian(this ushort value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
return BinaryPrimitives.TryWriteUInt16BigEndian(destination, value);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="ushort" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ushort" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ushort value, Span<byte> destination, Endianness endianness)
public static bool TryWriteLittleEndian(this ushort value, Span<byte> destination)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteUInt16BigEndian(destination, value)
: BinaryPrimitives.TryWriteUInt16LittleEndian(destination, value);
return BinaryPrimitives.TryWriteUInt16LittleEndian(destination, value);
}
}

View File

@ -10,54 +10,50 @@ namespace X10D.IO;
public static class UInt32Extensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <returns>An array of bytes with length 4.</returns>
/// <param name="value">The <see cref="uint" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this uint value)
public static byte[] GetBigEndianBytes(this uint value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer);
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <returns>An array of bytes with length 4.</returns>
/// <param name="value">The <see cref="uint" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this uint value, Endianness endianness)
public static byte[] GetLittleEndianBytes(this uint value)
{
Span<byte> buffer = stackalloc byte[4];
value.TryWriteBytes(buffer, endianness);
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="uint" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="uint" />.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this uint value, Span<byte> destination)
public static bool TryWriteBigEndian(this uint value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
return BinaryPrimitives.TryWriteUInt32BigEndian(destination, value);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="uint" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="uint" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this uint value, Span<byte> destination, Endianness endianness)
public static bool TryWriteLittleEndian(this uint value, Span<byte> destination)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteUInt32BigEndian(destination, value)
: BinaryPrimitives.TryWriteUInt32LittleEndian(destination, value);
return BinaryPrimitives.TryWriteUInt32LittleEndian(destination, value);
}
}

View File

@ -10,54 +10,50 @@ namespace X10D.IO;
public static class UInt64Extensions
{
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="value">The <see cref="ulong" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this ulong value)
public static byte[] GetBigEndianBytes(this ulong value)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer);
value.TryWriteBigEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The number to convert.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="value">The <see cref="ulong" /> value.</param>
/// <returns>An array of bytes with length 8.</returns>
[Pure]
public static byte[] GetBytes(this ulong value, Endianness endianness)
public static byte[] GetLittleEndianBytes(this ulong value)
{
Span<byte> buffer = stackalloc byte[8];
value.TryWriteBytes(buffer, endianness);
value.TryWriteLittleEndian(buffer);
return buffer.ToArray();
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="ulong" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ulong" />.</param>
/// <param name="destination">The span of bytes where the value is to be written, as big endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ulong value, Span<byte> destination)
public static bool TryWriteBigEndian(this ulong value, Span<byte> destination)
{
return BitConverter.TryWriteBytes(destination, value);
return BinaryPrimitives.TryWriteUInt64BigEndian(destination, value);
}
/// <summary>
/// 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.
/// </summary>
/// <param name="value">The <see cref="ulong" /> value.</param>
/// <param name="destination">When this method returns, the bytes representing the converted <see cref="ulong" />.</param>
/// <param name="endianness">The endianness with which to write the bytes.</param>
/// <param name="destination">The span of bytes where the value is to be written, as little endian.</param>
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this ulong value, Span<byte> destination, Endianness endianness)
public static bool TryWriteLittleEndian(this ulong value, Span<byte> destination)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteUInt64BigEndian(destination, value)
: BinaryPrimitives.TryWriteUInt64LittleEndian(destination, value);
return BinaryPrimitives.TryWriteUInt64LittleEndian(destination, value);
}
}