2023-08-23 13:15:52 +00:00
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using NUnit.Framework;
|
2023-04-01 17:17:50 +00:00
|
|
|
|
using X10D.IO;
|
|
|
|
|
|
|
|
|
|
namespace X10D.Tests.IO;
|
|
|
|
|
|
2023-08-22 16:32:47 +00:00
|
|
|
|
internal partial class StreamTests
|
2023-04-01 17:17:50 +00:00
|
|
|
|
{
|
2023-04-05 21:26:31 +00:00
|
|
|
|
[Test]
|
2023-08-23 13:15:52 +00:00
|
|
|
|
public void WriteBigEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument()
|
|
|
|
|
{
|
|
|
|
|
Stream stream = null!;
|
|
|
|
|
Assert.Throws<ArgumentNullException>(() => stream.WriteBigEndian(420UL));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
|
|
|
|
|
public void WriteBigEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt64Argument()
|
2023-04-01 17:17:50 +00:00
|
|
|
|
{
|
|
|
|
|
Stream stream = new DummyStream();
|
2023-08-23 13:15:52 +00:00
|
|
|
|
Assert.Throws<ArgumentException>(() => stream.WriteBigEndian(420UL));
|
2023-04-01 17:17:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:26:31 +00:00
|
|
|
|
[Test]
|
2023-08-23 13:15:52 +00:00
|
|
|
|
public void WriteLittleEndian_ShouldThrowArgumentNullException_GivenNullStream_AndUInt64Argument()
|
2023-04-01 17:17:50 +00:00
|
|
|
|
{
|
|
|
|
|
Stream stream = null!;
|
2023-08-23 13:15:52 +00:00
|
|
|
|
Assert.Throws<ArgumentNullException>(() => stream.WriteLittleEndian(420UL));
|
2023-04-01 17:17:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:26:31 +00:00
|
|
|
|
[Test]
|
2023-08-23 13:15:52 +00:00
|
|
|
|
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope")]
|
|
|
|
|
public void WriteLittleEndian_ShouldThrowArgumentException_GivenNonWritableStream_AndUInt64Argument()
|
2023-04-01 17:17:50 +00:00
|
|
|
|
{
|
2023-08-23 13:15:52 +00:00
|
|
|
|
Stream stream = new DummyStream();
|
|
|
|
|
Assert.Throws<ArgumentException>(() => stream.WriteLittleEndian(420UL));
|
2023-04-01 17:17:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:26:31 +00:00
|
|
|
|
[Test]
|
2023-08-23 13:15:52 +00:00
|
|
|
|
public void WriteBigEndian_ShouldWriteBigEndian_GivenUInt64Argument()
|
2023-04-01 17:17:50 +00:00
|
|
|
|
{
|
|
|
|
|
using var stream = new MemoryStream();
|
2023-08-23 13:15:52 +00:00
|
|
|
|
stream.WriteBigEndian(420UL);
|
2023-04-05 21:26:31 +00:00
|
|
|
|
Assert.That(stream.Position, Is.EqualTo(8));
|
2023-04-01 17:17:50 +00:00
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
Span<byte> actual = stackalloc byte[8];
|
2023-08-23 13:15:52 +00:00
|
|
|
|
ReadOnlySpan<byte> expected = stackalloc byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xA4 };
|
2023-04-01 17:17:50 +00:00
|
|
|
|
int read = stream.Read(actual);
|
|
|
|
|
|
2023-04-05 21:26:31 +00:00
|
|
|
|
Assert.That(read, Is.EqualTo(8));
|
2023-04-01 17:17:50 +00:00
|
|
|
|
CollectionAssert.AreEqual(expected.ToArray(), actual.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 21:26:31 +00:00
|
|
|
|
[Test]
|
2023-08-23 13:15:52 +00:00
|
|
|
|
public void WriteLittleEndian_ShouldWriteLittleEndian_GivenUInt64Argument()
|
2023-04-01 17:17:50 +00:00
|
|
|
|
{
|
|
|
|
|
using var stream = new MemoryStream();
|
2023-08-23 13:15:52 +00:00
|
|
|
|
stream.WriteLittleEndian(420UL);
|
2023-04-05 21:26:31 +00:00
|
|
|
|
Assert.That(stream.Position, Is.EqualTo(8));
|
2023-04-01 17:17:50 +00:00
|
|
|
|
stream.Position = 0;
|
|
|
|
|
|
|
|
|
|
Span<byte> actual = stackalloc byte[8];
|
2023-08-23 13:15:52 +00:00
|
|
|
|
ReadOnlySpan<byte> expected = stackalloc byte[] { 0xA4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
|
2023-04-01 17:17:50 +00:00
|
|
|
|
int read = stream.Read(actual);
|
|
|
|
|
|
2023-04-05 21:26:31 +00:00
|
|
|
|
Assert.That(read, Is.EqualTo(8));
|
2023-04-01 17:17:50 +00:00
|
|
|
|
CollectionAssert.AreEqual(expected.ToArray(), actual.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|