using System.Diagnostics.CodeAnalysis; using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; internal partial class StreamTests { [Test] public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument() { Stream stream = null!; Assert.Throws(() => stream.WriteBigEndian(420UL)); } [Test] [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt64Argument() { Stream stream = new DummyStream(); Assert.Throws(() => stream.WriteBigEndian(420UL)); } [Test] public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument() { Stream stream = null!; Assert.Throws(() => stream.WriteLittleEndian(420UL)); } [Test] [SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")] public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt64Argument() { Stream stream = new DummyStream(); Assert.Throws(() => stream.WriteLittleEndian(420UL)); } [Test] public void WriteBigEndian_ShouldWriteBigEndian_GivenUInt64Argument() { using var stream = new MemoryStream(); stream.WriteBigEndian(420UL); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; ReadOnlySpan expected = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); CollectionAssert.AreEqual(expected.ToArray(), actual.ToArray()); } [Test] public void WriteLittleEndian_ShouldWriteLittleEndian_GivenUInt64Argument() { using var stream = new MemoryStream(); stream.WriteLittleEndian(420UL); Assert.That(stream.Position, Is.EqualTo(8)); stream.Position = 0; Span actual = stackalloc byte[8]; ReadOnlySpan expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; int read = stream.Read(actual); Assert.That(read, Is.EqualTo(8)); CollectionAssert.AreEqual(expected.ToArray(), actual.ToArray()); } }