Skip to content

Commit 29d4e8d

Browse files
committed
Add tests Sudoku
1 parent 90d20b3 commit 29d4e8d

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
public class SudokuTest {
11+
12+
@Test
13+
void testIsSafe2() {
14+
int[][] board = {
15+
{3, 0, 6, 5, 0, 8, 4, 0, 0},
16+
{5, 2, 0, 0, 0, 0, 0, 0, 0},
17+
{0, 8, 7, 0, 0, 0, 0, 3, 1},
18+
{0, 0, 3, 0, 1, 0, 0, 8, 0},
19+
{9, 0, 0, 8, 6, 3, 0, 0, 5},
20+
{0, 5, 0, 0, 9, 0, 6, 0, 0},
21+
{1, 3, 0, 0, 0, 0, 2, 5, 0},
22+
{0, 0, 0, 0, 0, 0, 0, 7, 4},
23+
{0, 0, 5, 2, 0, 6, 3, 0, 0}
24+
};
25+
26+
assertFalse(Sudoku.isSafe(board, 0, 1, 3));
27+
assertTrue(Sudoku.isSafe(board, 1, 2, 1));
28+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
29+
Sudoku.isSafe(board, 10, 10, 5);
30+
});
31+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
32+
Sudoku.isSafe(board, -1, 0, 5);
33+
});
34+
}
35+
36+
@Test
37+
void testSolveSudoku() {
38+
int[][] board = {
39+
{3, 0, 6, 5, 0, 8, 4, 0, 0},
40+
{5, 2, 0, 0, 0, 0, 0, 0, 0},
41+
{0, 8, 7, 0, 0, 0, 0, 3, 1},
42+
{0, 0, 3, 0, 1, 0, 0, 8, 0},
43+
{9, 0, 0, 8, 6, 3, 0, 0, 5},
44+
{0, 5, 0, 0, 9, 0, 6, 0, 0},
45+
{1, 3, 0, 0, 0, 0, 2, 5, 0},
46+
{0, 0, 0, 0, 0, 0, 0, 7, 4},
47+
{0, 0, 5, 2, 0, 6, 3, 0, 0}
48+
};
49+
50+
assertTrue(Sudoku.solveSudoku(board, board.length));
51+
assertEquals(1, board[0][1]);
52+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> {
53+
Sudoku.solveSudoku(board, 10);
54+
});
55+
assertTrue(Sudoku.solveSudoku(board, -1));
56+
}
57+
58+
@Test
59+
void testUnsolvableSudoku() {
60+
int[][] unsolvableBoard = {
61+
{5, 1, 6, 8, 4, 9, 7, 3, 2},
62+
{3, 0, 7, 6, 0, 5, 0, 0, 0},
63+
{8, 0, 9, 7, 0, 0, 0, 6, 5},
64+
{1, 3, 5, 0, 6, 0, 9, 0, 7},
65+
{4, 7, 2, 5, 9, 1, 0, 0, 6},
66+
{9, 6, 8, 3, 7, 0, 0, 5, 0},
67+
{2, 5, 3, 1, 8, 6, 0, 7, 4},
68+
{6, 8, 4, 2, 5, 7, 3, 9, 0},
69+
{7, 9, 1, 4, 3, 0, 5, 0, 0}
70+
};
71+
72+
assertFalse(Sudoku.solveSudoku(unsolvableBoard, unsolvableBoard.length));
73+
}
74+
}

0 commit comments

Comments
 (0)