sudoku-solver/test/EndMove.Sudoku.Solver.Test/SolverBaseTest.cs
2024-09-10 09:36:54 +02:00

139 lines
4.6 KiB
C#

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.");
}
}
}