Initial commit.

This commit is contained in:
Marek Lemańczyk committed 2025-09-09 21:15:57 +02:00
1 parent e5b0fea18c
commit 9f35f6c558
94 files changed
+12857

No files matched your search

@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="../EvenPerfectBitScanner/EvenPerfectBitScanner.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,92 @@
using FluentAssertions;
using Xunit;
using PerfectNumbers.Core;
using System.Reflection;
namespace EvenPerfectBitScanner.Tests;
[Trait("Category", "Fast")]
public class ProgramTests
{
[Fact]
public void CountOnes_returns_correct_count()
{
Program.CountOnes(0b101010UL).Should().Be(3UL);
}
[Theory]
[InlineData(2UL, true)]
[InlineData(4UL, false)]
[InlineData(9UL, false)]
[InlineData(11UL, true)]
[InlineData(81UL, false)]
public void IsPrime_identifies_primes_correctly(ulong n, bool expected)
{
new PrimeTester().IsPrime(n, CancellationToken.None).Should().Be(expected);
}
[Fact]
public void TransformPAdd_moves_to_next_candidate()
{
ulong remainder = 3UL;
Program.TransformPAdd(3UL, ref remainder).Should().Be(5UL);
Program.TransformPAdd(5UL, ref remainder).Should().Be(7UL);
Program.TransformPAdd(7UL, ref remainder).Should().Be(11UL);
}
[Fact]
public void TransformPBit_appends_one_bit_and_skips_to_candidate()
{
ulong remainder = 5UL;
Program.TransformPBit(5UL, ref remainder).Should().Be(13UL);
}
[Fact]
public void TransformPBit_detects_overflow_and_stops()
{
typeof(Program).GetField("_limitReached", BindingFlags.NonPublic | BindingFlags.Static)!
.SetValue(null, false);
ulong start = (ulong.MaxValue >> 1) + 1UL;
ulong remainder = start % 6UL;
Program.TransformPBit(start, ref remainder);
bool limit = (bool)typeof(Program).GetField("_limitReached", BindingFlags.NonPublic | BindingFlags.Static)!
.GetValue(null)!;
limit.Should().BeTrue();
typeof(Program).GetField("_limitReached", BindingFlags.NonPublic | BindingFlags.Static)!
.SetValue(null, false);
}
[Fact]
public void Gcd_filter_detects_some_composites()
{
Program.IsCompositeByGcd(15UL).Should().BeTrue();
Program.IsCompositeByGcd(5UL).Should().BeFalse();
}
[Fact]
[Trait("Category", "Fast")]
public void Main_displays_help_when_requested()
{
var main = typeof(Program).GetMethod("Main", BindingFlags.NonPublic | BindingFlags.Static)!;
using var writer = new StringWriter();
TextWriter original = Console.Out;
Console.SetOut(writer);
try
{
main.Invoke(null, [new[] { "--help" }]);
}
finally
{
Console.SetOut(original);
}
string output = writer.ToString();
output.Should().Contain("Usage:");
output.Should().Contain("--mersenne-device=cpu|gpu");
output.Should().Contain("--primes-device=cpu|gpu");
}
}
@@ -0,0 +1,54 @@
using System.Reflection;
using FluentAssertions;
using PerfectNumbers.Core.Gpu;
using Xunit;
namespace EvenPerfectBitScanner.Tests;
[Trait("Category", "Fast")]
public class ResultsFileNameTests
{
[Fact]
public void BuildResultsFileName_contains_reduction_and_devices()
{
var programType = typeof(global::EvenPerfectBitScanner.Program);
var method = programType.GetMethod(
"BuildResultsFileName",
BindingFlags.NonPublic | BindingFlags.Static,
binder: null,
types: new[]
{
typeof(bool), typeof(int), typeof(int), typeof(global::PerfectNumbers.Core.GpuKernelType),
typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(bool), typeof(NttBackend), typeof(int), typeof(int), typeof(int), typeof(ulong), typeof(ModReductionMode), typeof(string), typeof(string), typeof(string)
},
modifiers: null)!;
string name = (string)method.Invoke(null, new object[]
{
true, // bitInc
8, // threads
1, // block
global::PerfectNumbers.Core.GpuKernelType.Incremental,
false, // useLucasFlag
true, // mersenneOnGpu (unused inside name content, devices passed explicitly below)
false, // useOrder
false, // useModWorkaround
false, // useGcd
NttBackend.Staged,
1, // gpuPrimeThreads
32, // llSlice
1024, // gpuScanBatch
1000UL, // warmupLimit
ModReductionMode.Barrett128,
"gpu", // mersenneDevice
"gpu", // primesDevice
"gpu" // orderDevice
})!;
name.Should().Contain("ntt-staged");
name.Should().Contain("red-barrett128");
name.Should().Contain("mersdev-gpu");
name.Should().Contain("primesdev-gpu");
name.Should().Contain("orderdev-gpu");
}
}