From c14d81928c04d53a0fb17b93e16858ed82afb521 Mon Sep 17 00:00:00 2001 From: "NIHART, Jeremi" Date: Tue, 10 Sep 2024 09:36:54 +0200 Subject: [PATCH] feat: add some tests --- EndMove.Sudoku.Solver.sln | 9 ++ .../Algorithms/SolverBase.cs | 8 +- src/EndMove.Sudoku.Solver.Core/Program.cs | 2 +- .../EndMove.Sudoku.Solver.Test.csproj | 28 ++++ .../SolverBaseTest.cs | 138 ++++++++++++++++++ 5 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 test/EndMove.Sudoku.Solver.Test/EndMove.Sudoku.Solver.Test.csproj create mode 100644 test/EndMove.Sudoku.Solver.Test/SolverBaseTest.cs diff --git a/EndMove.Sudoku.Solver.sln b/EndMove.Sudoku.Solver.sln index 1d1fd2b..e66fa3b 100644 --- a/EndMove.Sudoku.Solver.sln +++ b/EndMove.Sudoku.Solver.sln @@ -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 diff --git a/src/EndMove.Sudoku.Solver.Core/Algorithms/SolverBase.cs b/src/EndMove.Sudoku.Solver.Core/Algorithms/SolverBase.cs index 358a373..d3b0ef1 100644 --- a/src/EndMove.Sudoku.Solver.Core/Algorithms/SolverBase.cs +++ b/src/EndMove.Sudoku.Solver.Core/Algorithms/SolverBase.cs @@ -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; } diff --git a/src/EndMove.Sudoku.Solver.Core/Program.cs b/src/EndMove.Sudoku.Solver.Core/Program.cs index 03c8bc2..cb28471 100644 --- a/src/EndMove.Sudoku.Solver.Core/Program.cs +++ b/src/EndMove.Sudoku.Solver.Core/Program.cs @@ -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); } } diff --git a/test/EndMove.Sudoku.Solver.Test/EndMove.Sudoku.Solver.Test.csproj b/test/EndMove.Sudoku.Solver.Test/EndMove.Sudoku.Solver.Test.csproj new file mode 100644 index 0000000..ab77444 --- /dev/null +++ b/test/EndMove.Sudoku.Solver.Test/EndMove.Sudoku.Solver.Test.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/test/EndMove.Sudoku.Solver.Test/SolverBaseTest.cs b/test/EndMove.Sudoku.Solver.Test/SolverBaseTest.cs new file mode 100644 index 0000000..ec928ee --- /dev/null +++ b/test/EndMove.Sudoku.Solver.Test/SolverBaseTest.cs @@ -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 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 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."); + } + } +}