Suitable exceptions for setBoard with corresponding tests
This commit is contained in:
parent
f449d2343e
commit
175545d3d5
2 changed files with 14 additions and 3 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -28,7 +32,7 @@ public class Solver implements SudokuSolver {
|
|||
board = new int[9][9];
|
||||
}
|
||||
|
||||
/*{@inheritDoc} */
|
||||
/* {@inheritDoc} */
|
||||
@Override
|
||||
public boolean solve() {
|
||||
return solve(0, 0);
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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]));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue