|
| 1 | +package com.thealgorithms.backtracking; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 6 | + |
| 7 | +import org.junit.jupiter.api.Test; |
| 8 | + |
| 9 | +/** |
| 10 | + * Test class for SudokuSolver using JUnit 5. |
| 11 | + * This class verifies the correctness of the Sudoku solver algorithm. |
| 12 | + */ |
| 13 | +class SudokuSolverTest { |
| 14 | + |
| 15 | + /** |
| 16 | + * Test case for solving a solvable Sudoku puzzle. |
| 17 | + */ |
| 18 | + @Test |
| 19 | + void testForValidSudoku() { |
| 20 | + int[][] board = { |
| 21 | + { 5, 3, 0, 0, 7, 0, 0, 0, 0 }, |
| 22 | + { 6, 0, 0, 1, 9, 5, 0, 0, 0 }, |
| 23 | + { 0, 9, 8, 0, 0, 0, 0, 6, 0 }, |
| 24 | + { 8, 0, 0, 0, 6, 0, 0, 0, 3 }, |
| 25 | + { 4, 0, 0, 8, 0, 3, 0, 0, 1 }, |
| 26 | + { 7, 0, 0, 0, 2, 0, 0, 0, 6 }, |
| 27 | + { 0, 6, 0, 0, 0, 0, 2, 8, 0 }, |
| 28 | + { 0, 0, 0, 4, 1, 9, 0, 0, 5 }, |
| 29 | + { 0, 0, 0, 0, 8, 0, 0, 7, 9 } |
| 30 | + }; |
| 31 | + |
| 32 | + // Solve the Sudoku puzzle |
| 33 | + boolean solved = SudokuSolver.solveSudoku(board); |
| 34 | + |
| 35 | + // Check that the puzzle is solvable |
| 36 | + assertTrue(solved, "The Sudoku puzzle should be solvable."); |
| 37 | + |
| 38 | + // Expected output for a solved puzzle |
| 39 | + int[][] expected = { |
| 40 | + { 5, 3, 4, 6, 7, 8, 9, 1, 2 }, |
| 41 | + { 6, 7, 2, 1, 9, 5, 3, 4, 8 }, |
| 42 | + { 1, 9, 8, 3, 4, 2, 5, 6, 7 }, |
| 43 | + { 8, 5, 9, 7, 6, 1, 4, 2, 3 }, |
| 44 | + { 4, 2, 6, 8, 5, 3, 7, 9, 1 }, |
| 45 | + { 7, 1, 3, 9, 2, 4, 8, 5, 6 }, |
| 46 | + { 9, 6, 1, 5, 3, 7, 2, 8, 4 }, |
| 47 | + { 2, 8, 7, 4, 1, 9, 6, 3, 5 }, |
| 48 | + { 3, 4, 5, 2, 8, 6, 1, 7, 9 } |
| 49 | + }; |
| 50 | + |
| 51 | + // Verify the solved board matches the expected result |
| 52 | + assertArrayEquals(expected, board); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Test case for unsolvable Sudoku puzzle. |
| 57 | + */ |
| 58 | + @Test |
| 59 | + void testForUnsolvableSudoku() { |
| 60 | + int[][] board = { |
| 61 | + { 5, 3, 0, 0, 7, 0, 0, 0, 0 }, |
| 62 | + { 6, 0, 0, 1, 9, 5, 0, 0, 0 }, |
| 63 | + { 0, 9, 8, 0, 0, 0, 0, 6, 0 }, |
| 64 | + { 8, 0, 0, 0, 6, 0, 0, 0, 3 }, |
| 65 | + { 4, 0, 0, 8, 0, 3, 0, 0, 1 }, |
| 66 | + { 7, 0, 0, 0, 2, 0, 0, 0, 6 }, |
| 67 | + { 0, 6, 0, 0, 0, 0, 2, 8, 0 }, |
| 68 | + { 0, 0, 0, 4, 1, 9, 0, 0, 5 }, |
| 69 | + { 0, 0, 0, 0, 8, 0, 0, 7, 9 } |
| 70 | + }; |
| 71 | + |
| 72 | + // Modify the puzzle to make it unsolvable (e.g., repeat numbers) |
| 73 | + board[0][0] = 5; |
| 74 | + |
| 75 | + // Attempt to solve the unsolvable puzzle |
| 76 | + boolean solved = SudokuSolver.solveSudoku(board); |
| 77 | + |
| 78 | + // Check that the puzzle is not solvable |
| 79 | + assertFalse(solved, "The Sudoku puzzle should be unsolvable."); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Test case for an already solved Sudoku puzzle. |
| 84 | + */ |
| 85 | + @Test |
| 86 | + void testForAlreadySolvedSudoku() { |
| 87 | + int[][] board = { |
| 88 | + { 5, 3, 4, 6, 7, 8, 9, 1, 2 }, |
| 89 | + { 6, 7, 2, 1, 9, 5, 3, 4, 8 }, |
| 90 | + { 1, 9, 8, 3, 4, 2, 5, 6, 7 }, |
| 91 | + { 8, 5, 9, 7, 6, 1, 4, 2, 3 }, |
| 92 | + { 4, 2, 6, 8, 5, 3, 7, 9, 1 }, |
| 93 | + { 7, 1, 3, 9, 2, 4, 8, 5, 6 }, |
| 94 | + { 9, 6, 1, 5, 3, 7, 2, 8, 4 }, |
| 95 | + { 2, 8, 7, 4, 1, 9, 6, 3, 5 }, |
| 96 | + { 3, 4, 5, 2, 8, 6, 1, 7, 9 } |
| 97 | + }; |
| 98 | + |
| 99 | + // Check that the board is already solved |
| 100 | + boolean solved = SudokuSolver.solveSudoku(board); |
| 101 | + |
| 102 | + // Check that no changes are made to the already solved board |
| 103 | + assertTrue(solved, "The Sudoku board should be already solved."); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Test case for solving an empty Sudoku puzzle (no empty cells). |
| 108 | + */ |
| 109 | + @Test |
| 110 | + void testForEmptySudoku() { |
| 111 | + int[][] board = { |
| 112 | + { 5, 3, 4, 6, 7, 8, 9, 1, 2 }, |
| 113 | + { 6, 7, 2, 1, 9, 5, 3, 4, 8 }, |
| 114 | + { 1, 9, 8, 3, 4, 2, 5, 6, 7 }, |
| 115 | + { 8, 5, 9, 7, 6, 1, 4, 2, 3 }, |
| 116 | + { 4, 2, 6, 8, 5, 3, 7, 9, 1 }, |
| 117 | + { 7, 1, 3, 9, 2, 4, 8, 5, 6 }, |
| 118 | + { 9, 6, 1, 5, 3, 7, 2, 8, 4 }, |
| 119 | + { 2, 8, 7, 4, 1, 9, 6, 3, 5 }, |
| 120 | + { 3, 4, 5, 2, 8, 6, 1, 7, 9 } |
| 121 | + }; |
| 122 | + |
| 123 | + // Solve the "already filled" puzzle |
| 124 | + boolean solved = SudokuSolver.solveSudoku(board); |
| 125 | + |
| 126 | + // Check that the board is already solved |
| 127 | + assertTrue(solved, "The Sudoku board should be solved (no empty cells)."); |
| 128 | + } |
| 129 | +} |
0 commit comments