Skip to content

Commit

Permalink
Merge pull request #17 from crookseta/2.1
Browse files Browse the repository at this point in the history
2.1
  • Loading branch information
crookseta authored Nov 15, 2024
2 parents 841a112 + 0a2dcf2 commit 6465fbf
Show file tree
Hide file tree
Showing 35 changed files with 2,289 additions and 169 deletions.
4 changes: 2 additions & 2 deletions src/MissingValues.Benchmarks/MissingValues.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
Expand Down
63 changes: 63 additions & 0 deletions src/MissingValues.Tests/Core/Int256Test.GenericMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -842,13 +843,21 @@ public static void CreateCheckedToInt256Test()
NumberBaseHelper<Int>.CreateChecked(int.MaxValue).Should().Be(Int32MaxValue);
NumberBaseHelper<Int>.CreateChecked(long.MaxValue).Should().Be(Int64MaxValue);
NumberBaseHelper<Int>.CreateChecked(Int128.MaxValue).Should().Be(Int128MaxValue);
NumberBaseHelper<Int>
.CreateChecked(BigInteger.Parse(
"57896044618658097711785492504343953926634992332820282019728792003956564819967"))
.Should().Be(MaxValue);
NumberBaseHelper<Int>.CreateChecked(Int256MaxValueAsDouble).Should().Be(MaxValue);

NumberBaseHelper<Int>.CreateChecked(byte.MinValue).Should().Be(Zero);
NumberBaseHelper<Int>.CreateChecked(short.MinValue).Should().Be(Int16MinValue);
NumberBaseHelper<Int>.CreateChecked(int.MinValue).Should().Be(Int32MinValue);
NumberBaseHelper<Int>.CreateChecked(long.MinValue).Should().Be(Int64MinValue);
NumberBaseHelper<Int>.CreateChecked(Int128.MinValue).Should().Be(Int128MinValue);
NumberBaseHelper<Int>
.CreateChecked(BigInteger.Parse(
"-57896044618658097711785492504343953926634992332820282019728792003956564819968"))
.Should().Be(MinValue);
NumberBaseHelper<Int>.CreateChecked(Int256MinValueAsDouble).Should().Be(MinValue);
}
[Fact]
Expand All @@ -859,13 +868,21 @@ public static void CreateSaturatingToInt256Test()
NumberBaseHelper<Int>.CreateSaturating(int.MaxValue).Should().Be(Int32MaxValue);
NumberBaseHelper<Int>.CreateSaturating(long.MaxValue).Should().Be(Int64MaxValue);
NumberBaseHelper<Int>.CreateSaturating(Int128.MaxValue).Should().Be(Int128MaxValue);
NumberBaseHelper<Int>
.CreateSaturating(BigInteger.Parse(
"57896044618658097711785492504343953926634992332820282019728792003956564819967"))
.Should().Be(MaxValue);
NumberBaseHelper<Int>.CreateSaturating(Int256MaxValueAsDouble).Should().Be(MaxValue);

NumberBaseHelper<Int>.CreateSaturating(byte.MinValue).Should().Be(Zero);
NumberBaseHelper<Int>.CreateSaturating(short.MinValue).Should().Be(Int16MinValue);
NumberBaseHelper<Int>.CreateSaturating(int.MinValue).Should().Be(Int32MinValue);
NumberBaseHelper<Int>.CreateSaturating(long.MinValue).Should().Be(Int64MinValue);
NumberBaseHelper<Int>.CreateSaturating(Int128.MinValue).Should().Be(Int128MinValue);
NumberBaseHelper<Int>
.CreateSaturating(BigInteger.Parse(
"-57896044618658097711785492504343953926634992332820282019728792003956564819968"))
.Should().Be(MinValue);
NumberBaseHelper<Int>.CreateSaturating(Int256MinValueAsDouble).Should().Be(MinValue);
}
[Fact]
Expand All @@ -876,13 +893,21 @@ public static void CreateTruncatingToInt256Test()
NumberBaseHelper<Int>.CreateTruncating(int.MaxValue).Should().Be(Int32MaxValue);
NumberBaseHelper<Int>.CreateTruncating(long.MaxValue).Should().Be(Int64MaxValue);
NumberBaseHelper<Int>.CreateTruncating(Int128.MaxValue).Should().Be(Int128MaxValue);
NumberBaseHelper<Int>
.CreateTruncating(BigInteger.Parse(
"57896044618658097711785492504343953926634992332820282019728792003956564819967"))
.Should().Be(MaxValue);
NumberBaseHelper<Int>.CreateTruncating(Int256MaxValueAsDouble).Should().Be(MaxValue);

NumberBaseHelper<Int>.CreateTruncating(byte.MinValue).Should().Be(Zero);
NumberBaseHelper<Int>.CreateTruncating(short.MinValue).Should().Be(Int16MinValue);
NumberBaseHelper<Int>.CreateTruncating(int.MinValue).Should().Be(Int32MinValue);
NumberBaseHelper<Int>.CreateTruncating(long.MinValue).Should().Be(Int64MinValue);
NumberBaseHelper<Int>.CreateTruncating(Int128.MinValue).Should().Be(Int128MinValue);
NumberBaseHelper<Int>
.CreateTruncating(BigInteger.Parse(
"-57896044618658097711785492504343953926634992332820282019728792003956564819968"))
.Should().Be(MinValue);
NumberBaseHelper<Int>.CreateTruncating(Int256MinValueAsDouble).Should().Be(MinValue);
}

Expand All @@ -894,13 +919,19 @@ public static void CreateCheckedFromInt256Test()
NumberBaseHelper<int>.CreateChecked(Int32MaxValue).Should().Be(int.MaxValue);
NumberBaseHelper<long>.CreateChecked(Int64MaxValue).Should().Be(long.MaxValue);
NumberBaseHelper<Int128>.CreateChecked(Int128MaxValue).Should().Be(Int128.MaxValue);
NumberBaseHelper<BigInteger>.CreateChecked(MaxValue).Should()
.Be(BigInteger.Parse(
"57896044618658097711785492504343953926634992332820282019728792003956564819967"));
NumberBaseHelper<double>.CreateChecked(MaxValue).Should().Be(Int256MaxValueAsDouble);

NumberBaseHelper<byte>.CreateChecked(Zero).Should().Be(byte.MinValue);
NumberBaseHelper<short>.CreateChecked(Int16MinValue).Should().Be(short.MinValue);
NumberBaseHelper<int>.CreateChecked(Int32MinValue).Should().Be(int.MinValue);
NumberBaseHelper<long>.CreateChecked(Int64MinValue).Should().Be(long.MinValue);
NumberBaseHelper<Int128>.CreateChecked(Int128MinValue).Should().Be(Int128.MinValue);
NumberBaseHelper<BigInteger>.CreateChecked(MinValue).Should()
.Be(BigInteger.Parse(
"-57896044618658097711785492504343953926634992332820282019728792003956564819968"));
NumberBaseHelper<double>.CreateChecked(MinValue).Should().Be(Int256MinValueAsDouble);
}
[Fact]
Expand All @@ -911,13 +942,19 @@ public static void CreateSaturatingFromInt256Test()
NumberBaseHelper<int>.CreateSaturating(Int32MaxValue).Should().Be(int.MaxValue);
NumberBaseHelper<long>.CreateSaturating(Int64MaxValue).Should().Be(long.MaxValue);
NumberBaseHelper<Int128>.CreateSaturating(Int128MaxValue).Should().Be(Int128.MaxValue);
NumberBaseHelper<BigInteger>.CreateSaturating(MaxValue).Should()
.Be(BigInteger.Parse(
"57896044618658097711785492504343953926634992332820282019728792003956564819967"));
NumberBaseHelper<double>.CreateSaturating(MaxValue).Should().Be(Int256MaxValueAsDouble);

NumberBaseHelper<byte>.CreateSaturating(Zero).Should().Be(byte.MinValue);
NumberBaseHelper<short>.CreateSaturating(Int16MinValue).Should().Be(short.MinValue);
NumberBaseHelper<int>.CreateSaturating(Int32MinValue).Should().Be(int.MinValue);
NumberBaseHelper<long>.CreateSaturating(Int64MinValue).Should().Be(long.MinValue);
NumberBaseHelper<Int128>.CreateSaturating(Int128MinValue).Should().Be(Int128.MinValue);
NumberBaseHelper<BigInteger>.CreateSaturating(MinValue).Should()
.Be(BigInteger.Parse(
"-57896044618658097711785492504343953926634992332820282019728792003956564819968"));
NumberBaseHelper<double>.CreateSaturating(MinValue).Should().Be(Int256MinValueAsDouble);
}
[Fact]
Expand All @@ -928,13 +965,19 @@ public static void CreateTruncatingFromInt256Test()
NumberBaseHelper<int>.CreateTruncating(Int32MaxValue).Should().Be(int.MaxValue);
NumberBaseHelper<long>.CreateTruncating(Int64MaxValue).Should().Be(long.MaxValue);
NumberBaseHelper<Int128>.CreateTruncating(Int128MaxValue).Should().Be(Int128.MaxValue);
NumberBaseHelper<BigInteger>.CreateTruncating(MaxValue).Should()
.Be(BigInteger.Parse(
"57896044618658097711785492504343953926634992332820282019728792003956564819967"));
NumberBaseHelper<double>.CreateTruncating(MaxValue).Should().Be(Int256MaxValueAsDouble);

NumberBaseHelper<byte>.CreateTruncating(Zero).Should().Be(byte.MinValue);
NumberBaseHelper<short>.CreateTruncating(Int16MinValue).Should().Be(short.MinValue);
NumberBaseHelper<int>.CreateTruncating(Int32MinValue).Should().Be(int.MinValue);
NumberBaseHelper<long>.CreateTruncating(Int64MinValue).Should().Be(long.MinValue);
NumberBaseHelper<Int128>.CreateTruncating(Int128MinValue).Should().Be(Int128.MinValue);
NumberBaseHelper<BigInteger>.CreateTruncating(MinValue).Should()
.Be(BigInteger.Parse(
"-57896044618658097711785492504343953926634992332820282019728792003956564819968"));
NumberBaseHelper<double>.CreateTruncating(MinValue).Should().Be(Int256MinValueAsDouble);
}

Expand Down Expand Up @@ -1180,5 +1223,25 @@ public static void NegativeOneTest()
MathConstantsHelper.NegativeOne<Int>().Should().Be(NegativeOne);
}
#endregion

#region IPowerFunctions
[Fact]
public void PowTest()
{
GenericFloatingPointFunctions.Pow(Zero, int.MaxValue).Should().Be(Zero);
GenericFloatingPointFunctions.Pow(One, int.MaxValue).Should().Be(One);
GenericFloatingPointFunctions.Pow(MaxValue, Zero).Should().Be(One);
GenericFloatingPointFunctions.Pow(MaxValue, One).Should().Be(MaxValue);
GenericFloatingPointFunctions.Pow(Two, Two).Should().Be(4);
GenericFloatingPointFunctions.Pow(Two, 4).Should().Be(16);
GenericFloatingPointFunctions.Pow(16, Two).Should().Be(256);
GenericFloatingPointFunctions.Pow(Two, 254)
.Should().Be(new Int(0x4000_0000_0000_0000, 0x0000_0000_0000_0000, 0x0000_0000_0000_0000, 0x0000_0000_0000_0000));

Assert.Throws<ArgumentOutOfRangeException>(() => GenericFloatingPointFunctions.Pow(Two, NegativeOne));
Assert.Throws<OverflowException>(() => GenericFloatingPointFunctions.Pow(Two, 255));
Assert.Throws<OverflowException>(() => GenericFloatingPointFunctions.Pow(Two + Two, 254));
}
#endregion
}
}
11 changes: 11 additions & 0 deletions src/MissingValues.Tests/Core/Int256Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.Json;
using System.Text.Unicode;
Expand Down Expand Up @@ -73,6 +74,16 @@ public void Cast_ToInt128()
Int128.MaxValue.Should().Be((Int128)Int128MaxValue);
}

[Fact]
public void Cast_ToBigInteger()
{
BigInteger.One.Should().Be((BigInteger)One);
BigInteger.Parse("57896044618658097711785492504343953926634992332820282019728792003956564819967")
.Should().Be((BigInteger)MaxValue);
BigInteger.Parse("-57896044618658097711785492504343953926634992332820282019728792003956564819968")
.Should().Be((BigInteger)MinValue);
}

[Fact]
public void Cast_ToDouble()
{
Expand Down
Loading

0 comments on commit 6465fbf

Please sign in to comment.