Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="8.0.1" />
<PackageReference Include="coverlet.collector" Version="6.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
10 changes: 5 additions & 5 deletions tests/Neo.Extensions.Tests/Net/UT_IpAddressExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.Extensions;
using System.Net;

Expand All @@ -22,22 +22,22 @@ public class UT_IpAddressExtensions
public void TestUnmapForIPAddress()
{
var addr = new IPAddress(new byte[] { 127, 0, 0, 1 });
addr.UnMap().Should().Be(addr);
Assert.AreEqual(addr, addr.UnMap());

var addr2 = addr.MapToIPv6();
addr2.UnMap().Should().Be(addr);
Assert.AreEqual(addr, addr2.UnMap());
}

[TestMethod]
public void TestUnmapForIPEndPoin()
{
var addr = new IPAddress(new byte[] { 127, 0, 0, 1 });
var endPoint = new IPEndPoint(addr, 8888);
endPoint.UnMap().Should().Be(endPoint);
Assert.AreEqual(endPoint, endPoint.UnMap());

var addr2 = addr.MapToIPv6();
var endPoint2 = new IPEndPoint(addr2, 8888);
endPoint2.UnMap().Should().Be(endPoint);
Assert.AreEqual(endPoint, endPoint2.UnMap());
}
}
}
4 changes: 2 additions & 2 deletions tests/Neo.Extensions.Tests/UT_AssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;

Expand All @@ -27,7 +27,7 @@ public void TestGetVersion()
.Where(u => u.FullName == "Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null")
.FirstOrDefault();
string version = asm?.GetVersion() ?? "";
version.Should().Be("0.0.0");
Assert.AreEqual("0.0.0", version);
}
}
}
97 changes: 45 additions & 52 deletions tests/Neo.Extensions.Tests/UT_BigIntegerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using FluentAssertions;
using Neo.Extensions;
using Neo.Json;
using System;
Expand All @@ -25,53 +24,53 @@ public class UT_BigIntegerExtensions
public void TestGetLowestSetBit()
{
var big1 = new BigInteger(0);
big1.GetLowestSetBit().Should().Be(-1);
Assert.AreEqual(-1, big1.GetLowestSetBit());

var big2 = new BigInteger(512);
big2.GetLowestSetBit().Should().Be(9);
Assert.AreEqual(9, big2.GetLowestSetBit());

var big3 = new BigInteger(int.MinValue);
big3.GetLowestSetBit().Should().Be(31);
Assert.AreEqual(31, big3.GetLowestSetBit());

var big4 = new BigInteger(long.MinValue);
big4.GetLowestSetBit().Should().Be(63);
Assert.AreEqual(63, big4.GetLowestSetBit());

var big5 = new BigInteger(-18);
big5.GetLowestSetBit().Should().Be(1);
Assert.AreEqual(1, big5.GetLowestSetBit());

var big6 = BigInteger.Pow(2, 1000);
big6.GetLowestSetBit().Should().Be(1000);
Assert.AreEqual(1000, big6.GetLowestSetBit());
}

[TestMethod]
public void TestGetLowestSetBit_EdgeCases()
{
BigInteger.MinusOne.GetLowestSetBit().Should().Be(0);
BigInteger.One.GetLowestSetBit().Should().Be(0);
new BigInteger(ulong.MaxValue).GetLowestSetBit().Should().Be(0);
(BigInteger.One << 1000).GetLowestSetBit().Should().Be(1000);
Assert.AreEqual(0, BigInteger.MinusOne.GetLowestSetBit());
Assert.AreEqual(0, BigInteger.One.GetLowestSetBit());
Assert.AreEqual(0, new BigInteger(ulong.MaxValue).GetLowestSetBit());
Assert.AreEqual(1000, (BigInteger.One << 1000).GetLowestSetBit());
}

[TestMethod]
public void TestToByteArrayStandard()
{
BigInteger number = BigInteger.Zero;
number.ToByteArrayStandard().Should().BeEmpty();
CollectionAssert.AreEqual(Array.Empty<byte>(), number.ToByteArrayStandard());

number = BigInteger.One;
number.ToByteArrayStandard().Should().Equal(new byte[] { 0x01 });
CollectionAssert.AreEqual(new byte[] { 0x01 }, number.ToByteArrayStandard());

number = new BigInteger(256); // Binary: 100000000
number.ToByteArrayStandard().Should().Equal(new byte[] { 0x00, 0x01 });
CollectionAssert.AreEqual(new byte[] { 0x00, 0x01 }, number.ToByteArrayStandard());
}

[TestMethod]
public void TestToByteArrayStandard_EdgeCases()
{
BigInteger.MinusOne.ToByteArrayStandard().Should().Equal(new byte[] { 0xFF });
new BigInteger(byte.MaxValue).ToByteArrayStandard().Should().Equal(new byte[] { 0xFF, 0x00 });
new BigInteger(ushort.MaxValue).ToByteArrayStandard().Should().Equal(new byte[] { 0xFF, 0xFF, 0x00 });
new BigInteger(JNumber.MIN_SAFE_INTEGER).ToByteArrayStandard().Should().Equal(new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0 });
CollectionAssert.AreEqual(new byte[] { 0xFF }, BigInteger.MinusOne.ToByteArrayStandard());
CollectionAssert.AreEqual(new byte[] { 0xFF, 0x00 }, new BigInteger(byte.MaxValue).ToByteArrayStandard());
CollectionAssert.AreEqual(new byte[] { 0xFF, 0xFF, 0x00 }, new BigInteger(ushort.MaxValue).ToByteArrayStandard());
CollectionAssert.AreEqual(new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0 }, new BigInteger(JNumber.MIN_SAFE_INTEGER).ToByteArrayStandard());
}

[TestMethod]
Expand All @@ -80,35 +79,35 @@ public void TestMod()
var x = new BigInteger(-13);
var y = new BigInteger(5);
var result = x.Mod(y);
result.Should().Be(2); // -13 % 5 is -3, but Mod method should return 2
Assert.AreEqual(2, result); // -13 % 5 is -3, but Mod method should return 2
}

[TestMethod]
public void TestMod_EdgeCases()
{
// Test case 1: Mod of zero
BigInteger.Zero.Mod(5).Should().Be(0, "Mod of zero should always be zero");
Assert.AreEqual(0, BigInteger.Zero.Mod(5), "Mod of zero should always be zero");

// Test case 2: Mod of -1
BigInteger.MinusOne.Mod(5).Should().Be(4, "Mod of -1 should return the modulus minus 1");
Assert.AreEqual(4, BigInteger.MinusOne.Mod(5), "Mod of -1 should return the modulus minus 1");

// Test case 3: Mod with large numbers
BigInteger minValue = new BigInteger(long.MinValue);
BigInteger maxValue = new BigInteger(long.MaxValue);
minValue.Mod(maxValue).Should().Be(9223372036854775806, "Mod with large numbers should be calculated correctly");
Assert.AreEqual(9223372036854775806, minValue.Mod(maxValue), "Mod with large numbers should be calculated correctly");

// Test case 4: Comparing Mod with % operator
BigInteger result = minValue.Mod(maxValue);
result.Should().NotBe(long.MinValue % long.MaxValue, "Mod should always return non-negative values, unlike % operator");
Assert.AreNotEqual(long.MinValue % long.MaxValue, result, "Mod should always return non-negative values, unlike % operator");

// Test case 5: Verifying % operator behavior
(long.MinValue % long.MaxValue).Should().Be((long)(minValue % maxValue), "% operator should behave consistently for BigInteger and long");
Assert.AreEqual((long)(minValue % maxValue), long.MinValue % long.MaxValue, "% operator should behave consistently for BigInteger and long");

// Test case 6: Mod with prime numbers
new BigInteger(17).Mod(19).Should().Be(17, "Mod with a larger prime should return the original number");
Assert.AreEqual(17, new BigInteger(17).Mod(19), "Mod with a larger prime should return the original number");

// Test case 7: Mod with powers of 2
new BigInteger(1024).Mod(16).Should().Be(0, "Mod with powers of 2 should utilize bitwise operations efficiently");
Assert.AreEqual(0, new BigInteger(1024).Mod(16), "Mod with powers of 2 should utilize bitwise operations efficiently");
}

[TestMethod]
Expand All @@ -117,76 +116,70 @@ public void TestModInverse()
var a = new BigInteger(3);
var n = new BigInteger(11);
var result = a.ModInverse(n);
result.Should().Be(4); // 3 * 4 % 11 == 1
Assert.AreEqual(4, result); // 3 * 4 % 11 == 1

a = new BigInteger(1);
n = new BigInteger(11);
result = a.ModInverse(n);
result.Should().Be(1); // 1 * 1 % 11 == 1
Assert.AreEqual(1, result); // 1 * 1 % 11 == 1

a = new BigInteger(13);
n = new BigInteger(11);
result = a.ModInverse(n);
result.Should().Be(6); // 13 % 11 = 2, and 2 * 6 % 11 == 1
Assert.AreEqual(6, result); // 13 % 11 = 2, and 2 * 6 % 11 == 1

a = new BigInteger(6);
n = new BigInteger(12); // 6 and 12 are not coprime
Action act = () => a.ModInverse(n);
act.Should().Throw<ArithmeticException>()
.WithMessage("No modular inverse exists for the given inputs.");
Assert.ThrowsException<ArithmeticException>(() => a.ModInverse(n));
}

[TestMethod]
public void TestModInverse_EdgeCases()
{
Action act = () => BigInteger.Zero.ModInverse(11);
act.Should().Throw<ArithmeticException>();
Assert.ThrowsException<ArithmeticException>(() => BigInteger.Zero.ModInverse(11));

BigInteger.One.ModInverse(2).Should().Be(1);
Assert.AreEqual(1, BigInteger.One.ModInverse(2));

act = () => new BigInteger(2).ModInverse(4);
act.Should().Throw<ArithmeticException>();
Assert.ThrowsException<ArithmeticException>(() => new BigInteger(2).ModInverse(4));

new BigInteger(long.MaxValue - 1).ModInverse(long.MaxValue).Should().Be(long.MaxValue - 1);
Assert.AreEqual(long.MaxValue - 1, new BigInteger(long.MaxValue - 1).ModInverse(long.MaxValue));
}

[TestMethod]
public void TestBit()
{
var bigInteger = new BigInteger(5); // Binary: 101
var result = bigInteger.TestBit(2);
result.Should().BeTrue(); // Bit at index 2 is set (1)
Assert.IsTrue(bigInteger.TestBit(2)); // Bit at index 2 is set (1)

bigInteger = new BigInteger(5); // Binary: 101
result = bigInteger.TestBit(1);
result.Should().BeFalse(); // Bit at index 1 is not set (0)
Assert.IsFalse(bigInteger.TestBit(1)); // Bit at index 1 is not set (0)
}

[TestMethod]
public void TestBit_EdgeCases()
{
BigInteger.Zero.TestBit(0).Should().BeFalse();
BigInteger.Zero.TestBit(100).Should().BeFalse();
BigInteger.MinusOne.TestBit(0).Should().BeTrue();
BigInteger.MinusOne.TestBit(1000).Should().BeTrue();
(BigInteger.One << 1000).TestBit(1000).Should().BeTrue();
(BigInteger.One << 1000).TestBit(999).Should().BeFalse();
Assert.IsFalse(BigInteger.Zero.TestBit(0));
Assert.IsFalse(BigInteger.Zero.TestBit(100));
Assert.IsTrue(BigInteger.MinusOne.TestBit(0));
Assert.IsTrue(BigInteger.MinusOne.TestBit(1000));
Assert.IsTrue((BigInteger.One << 1000).TestBit(1000));
Assert.IsFalse((BigInteger.One << 1000).TestBit(999));
}

[TestMethod]
public void TestSum()
{
var bigIntegers = new List<BigInteger> { 1, 2, 3, 4 };
var result = bigIntegers.Sum();
result.Should().Be(10);
Assert.AreEqual(10, result);
}

[TestMethod]
public void TestSum_EdgeCases()
{
new List<BigInteger>().Sum().Should().Be(0);
new List<BigInteger> { JNumber.MIN_SAFE_INTEGER, JNumber.MAX_SAFE_INTEGER }.Sum().Should().Be(0);
new List<BigInteger> { JNumber.MAX_SAFE_INTEGER, JNumber.MAX_SAFE_INTEGER }.Sum().Should().Be(JNumber.MAX_SAFE_INTEGER * 2);
Assert.AreEqual(0, new List<BigInteger>().Sum());
Assert.AreEqual(0, new List<BigInteger> { JNumber.MIN_SAFE_INTEGER, JNumber.MAX_SAFE_INTEGER }.Sum());
Assert.AreEqual(JNumber.MAX_SAFE_INTEGER * 2, new List<BigInteger> { JNumber.MAX_SAFE_INTEGER, JNumber.MAX_SAFE_INTEGER }.Sum());
}
}
}
39 changes: 19 additions & 20 deletions tests/Neo.Extensions.Tests/UT_ByteArrayComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using FluentAssertions;
using System;

namespace Neo.Extensions.Tests
Expand All @@ -22,64 +21,64 @@ public void TestCompare()
{
ByteArrayComparer comparer = ByteArrayComparer.Default;
byte[]? x = null, y = null;
comparer.Compare(x, y).Should().Be(0);
Assert.AreEqual(0, comparer.Compare(x, y));

x = new byte[] { 1, 2, 3, 4, 5 };
y = x;
comparer.Compare(x, y).Should().Be(0);
comparer.Compare(x, x).Should().Be(0);
Assert.AreEqual(0, comparer.Compare(x, y));
Assert.AreEqual(0, comparer.Compare(x, x));

y = null;
comparer.Compare(x, y).Should().BeGreaterThan(0);
Assert.IsTrue(comparer.Compare(x, y) > 0);

y = x;
x = null;
comparer.Compare(x, y).Should().BeLessThan(0);
Assert.IsTrue(comparer.Compare(x, y) < 0);

x = new byte[] { 1 };
y = Array.Empty<byte>();
comparer.Compare(x, y).Should().BeGreaterThan(0);
Assert.IsTrue(comparer.Compare(x, y) > 0);
y = x;
comparer.Compare(x, y).Should().Be(0);
Assert.AreEqual(0, comparer.Compare(x, y));

x = new byte[] { 1 };
y = new byte[] { 2 };
comparer.Compare(x, y).Should().BeLessThan(0);
Assert.IsTrue(comparer.Compare(x, y) < 0);

comparer.Compare(null, Array.Empty<byte>()).Should().Be(0);
comparer.Compare(Array.Empty<byte>(), null).Should().Be(0);
Assert.AreEqual(0, comparer.Compare(null, Array.Empty<byte>()));
Assert.AreEqual(0, comparer.Compare(Array.Empty<byte>(), null));

x = new byte[] { 1, 2, 3, 4, 5 };
y = new byte[] { 1, 2, 3 };
comparer.Compare(x, y).Should().BeGreaterThan(0);
Assert.IsTrue(comparer.Compare(x, y) > 0);

x = new byte[] { 1, 2, 3, 4, 5 };
y = new byte[] { 1, 2, 3, 4, 5, 6 };
comparer.Compare(x, y).Should().BeLessThan(0);
Assert.IsTrue(comparer.Compare(x, y) < 0);

// cases for reverse comparer
comparer = ByteArrayComparer.Reverse;

x = new byte[] { 3 };
comparer.Compare(x, y).Should().BeLessThan(0);
Assert.IsTrue(comparer.Compare(x, y) < 0);

y = x;
comparer.Compare(x, y).Should().Be(0);
Assert.AreEqual(0, comparer.Compare(x, y));

x = new byte[] { 1 };
y = new byte[] { 2 };
comparer.Compare(x, y).Should().BeGreaterThan(0);
Assert.IsTrue(comparer.Compare(x, y) > 0);

comparer.Compare(null, Array.Empty<byte>()).Should().Be(0);
comparer.Compare(Array.Empty<byte>(), null).Should().Be(0);
Assert.AreEqual(0, comparer.Compare(null, Array.Empty<byte>()));
Assert.AreEqual(0, comparer.Compare(Array.Empty<byte>(), null));

x = new byte[] { 1, 2, 3, 4, 5 };
y = new byte[] { 1, 2, 3 };
comparer.Compare(x, y).Should().BeLessThan(0);
Assert.IsTrue(comparer.Compare(x, y) < 0);

x = new byte[] { 1, 2, 3, 4, 5 };
y = new byte[] { 1, 2, 3, 4, 5, 6 };
comparer.Compare(x, y).Should().BeGreaterThan(0);
Assert.IsTrue(comparer.Compare(x, y) > 0);
}
}
}
Loading