using NUnit.Framework; using X10D.IO; namespace X10D.Tests.IO; public partial class StreamTests { [Test] [CLSCompliant(false)] public void WriteUInt64_ShouldThrowArgumentException_GivenNonWriteableStream() { Stream stream = new DummyStream(); Assert.Throws(() => stream.Write(420UL, Endianness.LittleEndian)); Assert.Throws(() => stream.Write(420UL, Endianness.BigEndian)); } [Test] [CLSCompliant(false)] public void WriteUInt64_ShouldThrowArgumentNullException_GivenNullStream() { Stream stream = null!; Assert.Throws(() => stream.Write(420UL, Endianness.LittleEndian)); Assert.Throws(() => stream.Write(420UL, Endianness.BigEndian)); } [Test] [CLSCompliant(false)] public void WriteUInt64_ShouldThrowArgumentOutOfRangeException_GivenInvalidEndiannessValue() { // we don't need to enclose this stream in a using declaration, since disposing a // null stream is meaningless. NullStream.Dispose actually does nothing, anyway. // that - coupled with the fact that encapsulating the stream in a using declaration causes the // analyser to trip up and think the stream is disposed by the time the local is captured in // assertion lambda - means this line is fine as it is. please do not change. Stream stream = Stream.Null; Assert.Throws(() => stream.Write(420UL, (Endianness)(-1))); Assert.Throws(() => stream.Write(420UL, (Endianness)(-1))); } [Test] [CLSCompliant(false)] public void WriteUInt64_ShouldWriteBigEndian_GivenBigEndian() { using var stream = new MemoryStream(); stream.Write(420UL, Endianness.BigEndian); 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] [CLSCompliant(false)] public void WriteUInt64_ShouldWriteLittleEndian_GivenLittleEndian() { using var stream = new MemoryStream(); stream.Write(420UL, Endianness.LittleEndian); 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()); } }