Skip to content

Commit

Permalink
Upgrade to NUnit4.x - Moved assertions to Constraint model where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
monoman committed Feb 13, 2024
1 parent 31b59dc commit 2233a5f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ namespace InterlockLedger.Tags;
[TestFixture]
public class ByteArrayExtensionsTests
{
[TestCase(new byte[] { 0 }, ExpectedResult = 0, TestName = "ILIntDecodeFromByteArray 0")]
[TestCase(new byte[] { 1 }, ExpectedResult = 1, TestName = "ILIntDecodeFromByteArray 1")]
[TestCase(new byte[] { 0x80 }, ExpectedResult = 128, TestName = "ILIntDecodeFromByteArray 128")]
[TestCase(new byte[] { 0xF7 }, ExpectedResult = 247, TestName = "ILIntDecodeFromByteArray 247")]
[TestCase(new byte[] { 0xF8, 0 }, ExpectedResult = 248, TestName = "ILIntDecodeFromByteArray 248")]
[TestCase(new byte[] { 0xF8, 1 }, ExpectedResult = 249, TestName = "ILIntDecodeFromByteArray 249")]
[TestCase(new byte[] { 0xF8, 0xFF }, ExpectedResult = 503, TestName = "ILIntDecodeFromByteArray 503")]
[TestCase(new byte[] { 0xF9, 1, 1 }, ExpectedResult = 505, TestName = "ILIntDecodeFromByteArray 505")]
[TestCase(new byte[] { 0xFA, 1, 1, 1 }, ExpectedResult = 66041, TestName = "ILIntDecodeFromByteArray 66041")]
[TestCase(new byte[] { 0xFB, 1, 1, 1, 1 }, ExpectedResult = 16843257, TestName = "ILIntDecodeFromByteArray 16843257")]
[TestCase(new byte[] { 0xFC, 1, 1, 1, 1, 1 }, ExpectedResult = 4311810553, TestName = "ILIntDecodeFromByteArray 4311810553")]
[TestCase(new byte[] { 0xFD, 1, 1, 1, 1, 1, 1 }, ExpectedResult = 1103823438329, TestName = "ILIntDecodeFromByteArray 1103823438329")]
[TestCase(new byte[] { 0xFE, 1, 1, 1, 1, 1, 1, 1 }, ExpectedResult = 282578800148985, TestName = "ILIntDecodeFromByteArray 282578800148985")]
[TestCase(new byte[] { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, ExpectedResult = 72057594037928183, TestName = "ILIntDecodeFromByteArray 72057594037928183")]
[TestCase(new byte[] { 0xFF, 1, 1, 1, 1, 1, 1, 1, 1 }, ExpectedResult = 72340172838076921, TestName = "ILIntDecodeFromByteArray 72340172838076921")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 7 }, ExpectedResult = 18446744073709551615, TestName = "ILIntDecodeFromByteArray 18446744073709551615")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 8 }, ExpectedResult = 0, TestName = "ILIntDecodeFromByteArray too large by 1 => zero")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80 }, ExpectedResult = 0, TestName = "ILIntDecodeFromByteArray too large by 120 => zero")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, ExpectedResult = 0, TestName = "ILIntDecodeFromByteArray too large by all => zero")]
[TestCase(new byte[] { 0 }, ExpectedResult = 0ul, TestName = "ILIntDecodeFromByteArray 0")]
[TestCase(new byte[] { 1 }, ExpectedResult = 1ul, TestName = "ILIntDecodeFromByteArray 1")]
[TestCase(new byte[] { 0x80 }, ExpectedResult = 128ul, TestName = "ILIntDecodeFromByteArray 128")]
[TestCase(new byte[] { 0xF7 }, ExpectedResult = 247ul, TestName = "ILIntDecodeFromByteArray 247")]
[TestCase(new byte[] { 0xF8, 0 }, ExpectedResult = 248ul, TestName = "ILIntDecodeFromByteArray 248")]
[TestCase(new byte[] { 0xF8, 1 }, ExpectedResult = 249ul, TestName = "ILIntDecodeFromByteArray 249")]
[TestCase(new byte[] { 0xF8, 0xFF }, ExpectedResult = 503ul, TestName = "ILIntDecodeFromByteArray 503")]
[TestCase(new byte[] { 0xF9, 1, 1 }, ExpectedResult = 505ul, TestName = "ILIntDecodeFromByteArray 505")]
[TestCase(new byte[] { 0xFA, 1, 1, 1 }, ExpectedResult = 66041ul, TestName = "ILIntDecodeFromByteArray 66041")]
[TestCase(new byte[] { 0xFB, 1, 1, 1, 1 }, ExpectedResult = 16843257ul, TestName = "ILIntDecodeFromByteArray 16843257")]
[TestCase(new byte[] { 0xFC, 1, 1, 1, 1, 1 }, ExpectedResult = 4311810553ul, TestName = "ILIntDecodeFromByteArray 4311810553")]
[TestCase(new byte[] { 0xFD, 1, 1, 1, 1, 1, 1 }, ExpectedResult = 1103823438329ul, TestName = "ILIntDecodeFromByteArray 1103823438329")]
[TestCase(new byte[] { 0xFE, 1, 1, 1, 1, 1, 1, 1 }, ExpectedResult = 282578800148985ul, TestName = "ILIntDecodeFromByteArray 282578800148985")]
[TestCase(new byte[] { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, ExpectedResult = 72057594037928183ul, TestName = "ILIntDecodeFromByteArray 72057594037928183")]
[TestCase(new byte[] { 0xFF, 1, 1, 1, 1, 1, 1, 1, 1 }, ExpectedResult = 72340172838076921ul, TestName = "ILIntDecodeFromByteArray 72340172838076921")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 7 }, ExpectedResult = 18446744073709551615ul, TestName = "ILIntDecodeFromByteArray 18446744073709551615")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 8 }, ExpectedResult = 0ul, TestName = "ILIntDecodeFromByteArray too large by 1 => zero")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80 }, ExpectedResult = 0ul, TestName = "ILIntDecodeFromByteArray too large by 120 => zero")]
[TestCase(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, ExpectedResult = 0ul, TestName = "ILIntDecodeFromByteArray too large by all => zero")]
public ulong ILIntDecodeFromByteArray(byte[] bytes) => bytes.ILIntDecode();

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class IBufferWriterOfByteExtensionsTests
public byte[] ILIntEncodeToIBufferWriter(ulong value) {
var result = new MockBufferWriter(new byte[value.ILIntSize()]);
result.ILIntEncode(value);
Assert.AreEqual(result.Memory.Length, result.Position);
Assert.That(result.Position, Is.EqualTo(result.Memory.Length));
return result.Memory;
}
}
72 changes: 39 additions & 33 deletions InterlockLedger.Tags.ILInt.UnitTests/Features/ILIntReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,52 +40,52 @@ namespace InterlockLedger.Tags;
public class ILIntReaderTests
{
[Test]
public void Test0() => Assert.AreEqual(0ul, ILD([0]), "ILIntDecodeFromByteArray 0");
public void Test0() => Assert.That(ILD([0]), Is.EqualTo(0ul), "ILIntDecodeFromByteArray 0");

[Test]
public void Test1() => Assert.AreEqual(1ul, ILD([1]), "ILIntDecodeFromByteArray 1");
public void Test1() => Assert.That(ILD([1]), Is.EqualTo(1ul), "ILIntDecodeFromByteArray 1");

[Test]
public void Test1103823438329() => Assert.AreEqual(1103823438329ul, ILD([0xFD, 1, 1, 1, 1, 1, 1]), "ILIntDecodeFromByteArray 1103823438329");
public void Test1103823438329() => Assert.That(ILD([0xFD, 1, 1, 1, 1, 1, 1]), Is.EqualTo(1103823438329ul), "ILIntDecodeFromByteArray 1103823438329");

[Test]
public void Test128() => Assert.AreEqual(128ul, ILD([0x80]), "ILIntDecodeFromByteArray 128");
public void Test128() => Assert.That(ILD([0x80]), Is.EqualTo(128ul), "ILIntDecodeFromByteArray 128");

[Test]
public void Test16843257() => Assert.AreEqual(16843257ul, ILD([0xFB, 1, 1, 1, 1]), "ILIntDecodeFromByteArray 16843257");
public void Test16843257() => Assert.That(ILD([0xFB, 1, 1, 1, 1]), Is.EqualTo(16843257ul), "ILIntDecodeFromByteArray 16843257");

[Test]
public void Test18446744073709551615() => Assert.AreEqual(18446744073709551615ul, ILD([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 7]), "ILIntDecodeFromByteArray 18446744073709551615");
public void Test18446744073709551615() => Assert.That(ILD([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 7]), Is.EqualTo(18446744073709551615ul), "ILIntDecodeFromByteArray 18446744073709551615");

[Test]
public void Test247() => Assert.AreEqual(247ul, ILD([0xF7]), "ILIntDecodeFromByteArray 247");
public void Test247() => Assert.That(ILD([0xF7]), Is.EqualTo(247ul), "ILIntDecodeFromByteArray 247");

[Test]
public void Test248() => Assert.AreEqual(248ul, ILD([0xF8, 0]), "ILIntDecodeFromByteArray 248");
public void Test248() => Assert.That(ILD([0xF8, 0]), Is.EqualTo(248ul), "ILIntDecodeFromByteArray 248");

[Test]
public void Test249() => Assert.AreEqual(249ul, ILD([0xF8, 1]), "ILIntDecodeFromByteArray 249");
public void Test249() => Assert.That(ILD([0xF8, 1]), Is.EqualTo(249ul), "ILIntDecodeFromByteArray 249");

[Test]
public void Test282578800148985() => Assert.AreEqual(282578800148985ul, ILD([0xFE, 1, 1, 1, 1, 1, 1, 1]), "ILIntDecodeFromByteArray 282578800148985");
public void Test282578800148985() => Assert.That(ILD([0xFE, 1, 1, 1, 1, 1, 1, 1]), Is.EqualTo(282578800148985ul), "ILIntDecodeFromByteArray 282578800148985");

[Test]
public void Test4311810553() => Assert.AreEqual(4311810553ul, ILD([0xFC, 1, 1, 1, 1, 1]), "ILIntDecodeFromByteArray 4311810553");
public void Test4311810553() => Assert.That(ILD([0xFC, 1, 1, 1, 1, 1]), Is.EqualTo(4311810553ul), "ILIntDecodeFromByteArray 4311810553");

[Test]
public void Test503() => Assert.AreEqual(503ul, ILD([0xF8, 0xFF]), "ILIntDecodeFromByteArray 503");
public void Test503() => Assert.That(ILD([0xF8, 0xFF]), Is.EqualTo(503ul), "ILIntDecodeFromByteArray 503");

[Test]
public void Test505() => Assert.AreEqual(505ul, ILD([0xF9, 1, 1]), "ILIntDecodeFromByteArray 505");
public void Test505() => Assert.That(ILD([0xF9, 1, 1]), Is.EqualTo(505ul), "ILIntDecodeFromByteArray 505");

[Test]
public void Test66041() => Assert.AreEqual(66041ul, ILD([0xFA, 1, 1, 1]), "ILIntDecodeFromByteArray 66041");
public void Test66041() => Assert.That(ILD([0xFA, 1, 1, 1]), Is.EqualTo(66041ul), "ILIntDecodeFromByteArray 66041");

[Test]
public void Test72057594037928183() => Assert.AreEqual(72057594037928183ul, ILD([0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), "ILIntDecodeFromByteArray 72057594037928183");
public void Test72057594037928183() => Assert.That(ILD([0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), Is.EqualTo(72057594037928183ul), "ILIntDecodeFromByteArray 72057594037928183");

[Test]
public void Test72340172838076921() => Assert.AreEqual(72340172838076921ul, ILD([0xFF, 1, 1, 1, 1, 1, 1, 1, 1]), "ILIntDecodeFromByteArray 72340172838076921");
public void Test72340172838076921() => Assert.That(ILD([0xFF, 1, 1, 1, 1, 1, 1, 1, 1]), Is.EqualTo(72340172838076921ul), "ILIntDecodeFromByteArray 72340172838076921");

[Test]
public void TestTooLargeBy1() => Assert.Throws<InvalidOperationException>(() => ILD([0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 8]), "ILIntDecodeFromByteArray too large by 1 => zero");
Expand All @@ -100,16 +100,18 @@ public class ILIntReaderTests
public void TryDecodeIEnum() {
static (long consumed, bool ok, ulong? value) DoTry(params byte[] bytes) {
var reader = new ILIntReader();
Assert.IsFalse(reader.Ready, "Should not be read before being supplied bytes");
Assert.That(reader.Ready, Is.False, "Should not be read before being supplied bytes");
var ok = reader.TryDecode((IEnumerable<byte>)bytes, out var consumed);
Assert.AreEqual(ok, reader.Ready, "Should be ready when done consuming bytes");
Assert.That(reader.Ready, Is.EqualTo(ok), "Should be ready when done consuming bytes");
return (consumed, ok, ok ? reader.Value : (ulong?)null);
}
static void DoTryExpecting(bool okExpected, long consumedExpected, ulong? valueExpected, params byte[] bytes) {
var (consumed, ok, value) = DoTry(bytes);
Assert.AreEqual(okExpected, ok);
Assert.AreEqual(consumedExpected, consumed);
Assert.AreEqual(valueExpected, value);
Assert.Multiple(() => {
Assert.That(ok, Is.EqualTo(okExpected));
Assert.That(consumed, Is.EqualTo(consumedExpected));
Assert.That(value, Is.EqualTo(valueExpected));
});
}
DoTryExpecting(false, 0L, null);
DoTryExpecting(true, 1L, 0UL, 0);
Expand All @@ -122,19 +124,21 @@ static void DoTryExpecting(bool okExpected, long consumedExpected, ulong? valueE
public void TryDecodeReadOnlySequence() {
static (long consumed, bool ok, ulong? value) DoTry(int offset, int tail, params byte[] bytes) {
var reader = new ILIntReader();
Assert.IsFalse(reader.Ready, "Should not be read before being supplied bytes");
Assert.That(reader.Ready, Is.False, "Should not be read before being supplied bytes");
var newBytes = new byte[bytes.Length + offset + tail];
Array.Copy(bytes, 0, newBytes, offset, bytes.Length);
var sequence = new ReadOnlySequence<byte>(newBytes, offset, bytes.Length);
var ok = reader.TryDecode(sequence, out var consumed);
Assert.AreEqual(ok, reader.Ready, "Should be ready when done consuming bytes");
Assert.That(reader.Ready, Is.EqualTo(ok), "Should be ready when done consuming bytes");
return (consumed, ok, ok ? reader.Value : (ulong?)null);
}
static void DoTryExpecting(bool okExpected, long consumedExpected, ulong? valueExpected, int offset, int tail, params byte[] bytes) {
var (consumed, ok, value) = DoTry(offset, tail, bytes);
Assert.AreEqual(okExpected, ok);
Assert.AreEqual(valueExpected, value);
Assert.AreEqual(consumedExpected, consumed);
Assert.Multiple(() => {
Assert.That(ok, Is.EqualTo(okExpected));
Assert.That(value, Is.EqualTo(valueExpected));
Assert.That(consumed, Is.EqualTo(consumedExpected));
});
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
Expand All @@ -151,19 +155,21 @@ static void DoTryExpecting(bool okExpected, long consumedExpected, ulong? valueE
public void TryDecodeReadOnlySpan() {
static (long consumed, bool ok, ulong? value) DoTry(int offset, int tail, params byte[] bytes) {
var reader = new ILIntReader();
Assert.IsFalse(reader.Ready, "Should not be read before being supplied bytes");
Assert.That(reader.Ready, Is.False, "Should not be read before being supplied bytes");
var newBytes = new byte[bytes.Length + offset + tail];
Array.Copy(bytes, 0, newBytes, offset, bytes.Length);
var span = new ReadOnlySpan<byte>(newBytes, offset, bytes.Length);
var ok = reader.TryDecode(span, out var consumed);
Assert.AreEqual(ok, reader.Ready, "Should be ready when done consuming bytes");
Assert.That(reader.Ready, Is.EqualTo(ok), "Should be ready when done consuming bytes");
return (consumed, ok, ok ? reader.Value : (ulong?)null);
}
static void DoTryExpecting(bool okExpected, long consumedExpected, ulong? valueExpected, int offset, int tail, params byte[] bytes) {
var (consumed, ok, value) = DoTry(offset, tail, bytes);
Assert.AreEqual(okExpected, ok);
Assert.AreEqual(consumedExpected, consumed);
Assert.AreEqual(valueExpected, value);
Assert.Multiple(() => {
Assert.That(ok, Is.EqualTo(okExpected));
Assert.That(consumed, Is.EqualTo(consumedExpected));
Assert.That(value, Is.EqualTo(valueExpected));
});
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 32; j++) {
Expand All @@ -180,15 +186,15 @@ private static ulong ILD(byte[] bytes) {
var reader = new ILIntReader();
for (int k = 0; k < 2; k++) {
if (k > 0) reader.Reset();
Assert.IsFalse(reader.Ready, "Should not be read before being supplied bytes");
Assert.That(reader.Ready, Is.False, "Should not be read before being supplied bytes");
for (int i = 0; i < bytes.Length; i++) {
var done = reader.Done(bytes[i]);
if (i + 1 < bytes.Length && done)
Assert.Fail("Value decoded without all bytes");
if (i + 1 == bytes.Length && !done)
Assert.Fail("Value not decoded with supplied bytes");
}
Assert.IsTrue(reader.Ready, "Should be ready when done consuming bytes");
Assert.That(reader.Ready, "Should be ready when done consuming bytes");
}
return reader.Value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ public class ILIntTests
[TestCase((ulong)72057594037928183)]
[TestCase((ulong)72340172838076921)]
[TestCase(18446744073709551615)]
public void AsILIntILIntDecode(ulong value) => Assert.AreEqual(value, value.AsILInt().ILIntDecode());
public void AsILIntILIntDecode(ulong value) => Assert.That(value.AsILInt().ILIntDecode(), Is.EqualTo(value));
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.0" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit.Analyzers" Version="4.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 2233a5f

Please sign in to comment.