feat: add some tests

This commit is contained in:
NIHART, Jeremi 2024-09-10 09:36:54 +02:00
parent 0a70653bb2
commit c14d81928c
5 changed files with 180 additions and 5 deletions

View File

@ -4,6 +4,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{67AA5CD2-BE1
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EndMove.Sudoku.Solver.Core", "src\EndMove.Sudoku.Solver.Core\EndMove.Sudoku.Solver.Core.csproj", "{2A444DA7-B235-4419-8B31-F9A554496D8E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{F3DB57FC-878B-40EC-ABC3-0AC37DD4693F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EndMove.Sudoku.Solver.Test", "test\EndMove.Sudoku.Solver.Test\EndMove.Sudoku.Solver.Test.csproj", "{BA84417D-96D2-4307-B804-974DF6A715F2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -14,8 +18,13 @@ Global
{2A444DA7-B235-4419-8B31-F9A554496D8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A444DA7-B235-4419-8B31-F9A554496D8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A444DA7-B235-4419-8B31-F9A554496D8E}.Release|Any CPU.Build.0 = Release|Any CPU
{BA84417D-96D2-4307-B804-974DF6A715F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA84417D-96D2-4307-B804-974DF6A715F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA84417D-96D2-4307-B804-974DF6A715F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA84417D-96D2-4307-B804-974DF6A715F2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2A444DA7-B235-4419-8B31-F9A554496D8E} = {67AA5CD2-BE13-4741-8F70-4FE3638271E4}
{BA84417D-96D2-4307-B804-974DF6A715F2} = {F3DB57FC-878B-40EC-ABC3-0AC37DD4693F}
EndGlobalSection
EndGlobal

View File

@ -24,12 +24,12 @@ public abstract class SolverBase : ISolver
}
}
// Check Box
int xSize = (col / 3) * 3, ySize = (row / 3) * 3;
for (int y = 0; y < 3; y++)
int xSize = col - (col % 3), ySize = row - (row % 3);
for (int y = ySize; y < ySize + 3; y++)
{
for (int x = 0; x < 3; x++)
for (int x = xSize; x < xSize + 3; x++)
{
if (board[ySize + y, xSize + x] == n)
if (board[y, x] == n)
{
return false;
}

View File

@ -63,7 +63,7 @@ namespace EndMove.Sudoku.Solver.Core
// TODO Add time measurement
ISolver solver = new BackTrackingSolver(SudokuUtils.DisplayBoard);
var (status, lol) = solver.Solve(store[0]);
var (status, lol) = solver.Solve(store[1]);
Console.WriteLine(status);
}
}

View File

@ -0,0 +1,28 @@
<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="coverlet.collector" Version="6.0.0"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0"/>
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.9.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
</ItemGroup>
<ItemGroup>
<Using Include="NUnit.Framework"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\EndMove.Sudoku.Solver.Core\EndMove.Sudoku.Solver.Core.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,138 @@
using EndMove.Sudoku.Solver.Core.Algorithms;
namespace EndMove.Sudoku.Solver.Test
{
public class SolverBaseTests
{
// Mock implementation of SolverBase for testing
private class MockSolver : SolverBase
{
public MockSolver(Action<int[,]> displayBoard) : base(displayBoard) { }
public override (bool, int[,]) Solve(int[,] board)
{
throw new NotImplementedException();
}
public override (bool, int[]) Solve(int[] board)
{
throw new NotImplementedException();
}
}
// Test fixture
private SolverBase _solverBase;
[SetUp]
public void Setup()
{
// Create a mock display method (if necessary)
Action<int[,]> mockDisplay = (board) => { /* mock display implementation */ };
_solverBase = new MockSolver(mockDisplay);
}
[Test]
public void IsValid1D_ValidPlacement_ReturnsTrue()
{
// GIVEN
int row = 1, col = 4, n = 1;
int[,] board = {
//|--------|-------|--------|
{ 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 1, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
//|--------|-------|--------|
};
// WHEN
bool result = _solverBase.IsValid(board, row, col, n);
// THEN
Assert.That(result, Is.True, "Expected valid placement to return true.");
}
[Test]
public void IsValid1D_InvalidPlacementInColumn_ReturnsFalse()
{
// GIVEN
int row = 3, col = 2, n = 9;
int[,] board = {
//|--------|-------|--------|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 9, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
//|--------|-------|--------|
};
// WHEN
bool result = _solverBase.IsValid(board, row, col, n);
// THEN
Assert.That(result, Is.False, "Expected invalid placement in column to return false.");
}
[Test]
public void IsValid1D_InvalidPlacementInRow_ReturnsFalse()
{
// GIVEN
int row = 4, col = 2, n = 9;
int[,] board = {
//|--------|-------|--------|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 9, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
//|--------|-------|--------|
};
// WHEN
bool result = _solverBase.IsValid(board, row, col, n);
// THEN
Assert.That(result, Is.False, "Expected invalid placement in row to return false.");
}
[Test]
public void IsValid1D_InvalidPlacementInBox_ReturnsFalse()
{
// GIVEN
int row = 8, col = 5, n = 1;
int[,] board = {
//|--------|-------|--------|
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 9, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }
//|--------|-------|--------|
};
// WHEN
bool result = _solverBase.IsValid(board, row, col, n);
// THEN
Assert.That(result, Is.False, "Expected invalid placement in box to return false.");
}
}
}