Suitable exceptions for setBoard with corresponding tests

This commit is contained in:
Imbus 2023-12-10 15:04:04 +01:00
parent f449d2343e
commit 175545d3d5
2 changed files with 14 additions and 3 deletions

View file

@ -12,7 +12,11 @@ public class Solver implements SudokuSolver {
/** {@inheritDoc} */
@Override
public void setBoard(int[][] board) {
public void setBoard(int[][] board) throws IllegalArgumentException, NullPointerException {
if (board == null)
throw new NullPointerException("Board cannot be null");
if (board.length != 9 || board[0].length != 9)
throw new IllegalArgumentException("Board must be 9x9");
this.board = board;
}
@ -107,7 +111,7 @@ public class Solver implements SudokuSolver {
}
// Recursively call randomizeBoard() until we get a solvable board
// This is expensive, and there should be some voodoo magic that computes this in n^2 time
// This is expensive, and there should be a better way to do this
if (!isSolvable()) {
randomizeBoard(difficulty);
}

View file

@ -106,4 +106,11 @@ class SolverTest {
solver.set(7, 1, 8);
assertFalse(solver.solve());
}
@Test
void setBoardInvalidInputThrowsTest() {
Solver solver = new Solver();
assertThrows(NullPointerException.class, () -> solver.setBoard(null));
assertThrows(IllegalArgumentException.class, () -> solver.setBoard(new int[8][8]));
}
}