|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | +import io |
| 4 | +import os |
| 5 | +import importlib.util |
| 6 | + |
| 7 | +# Absolute path to Sudoku-Game.py |
| 8 | +file_path = os.path.join( |
| 9 | + os.path.dirname(__file__), "..", |
| 10 | + "games", "Sudoku-Game", "Sudoku-Game.py" |
| 11 | +) |
| 12 | +file_path = os.path.abspath(file_path) |
| 13 | + |
| 14 | +# Load module dynamically from file path |
| 15 | +spec = importlib.util.spec_from_file_location("sudoku_game", file_path) |
| 16 | +sudoku_module = importlib.util.module_from_spec(spec) |
| 17 | +spec.loader.exec_module(sudoku_module) |
| 18 | + |
| 19 | +is_valid = sudoku_module.is_valid |
| 20 | +find_empty = sudoku_module.find_empty |
| 21 | +solve_sudoku_backtracking = sudoku_module.solve_sudoku_backtracking |
| 22 | +generate_sudoku_puzzle = sudoku_module.generate_sudoku_puzzle |
| 23 | + |
| 24 | +class TestSudokuGame(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + # A simple solvable Sudoku grid |
| 27 | + self.grid = [ |
| 28 | + [5, 3, 0, 0, 7, 0, 0, 0, 0], |
| 29 | + [6, 0, 0, 1, 9, 5, 0, 0, 0], |
| 30 | + [0, 9, 8, 0, 0, 0, 0, 6, 0], |
| 31 | + [8, 0, 0, 0, 6, 0, 0, 0, 3], |
| 32 | + [4, 0, 0, 8, 0, 3, 0, 0, 1], |
| 33 | + [7, 0, 0, 0, 2, 0, 0, 0, 6], |
| 34 | + [0, 6, 0, 0, 0, 0, 2, 8, 0], |
| 35 | + [0, 0, 0, 4, 1, 9, 0, 0, 5], |
| 36 | + [0, 0, 0, 0, 8, 0, 0, 7, 9] |
| 37 | + ] |
| 38 | + |
| 39 | + def test_find_empty(self): |
| 40 | + # First empty should be at row 0, col 2 |
| 41 | + self.assertEqual(find_empty(self.grid), (0, 2)) |
| 42 | + |
| 43 | + # A full grid should return None |
| 44 | + full_grid = [[1] * 9 for _ in range(9)] |
| 45 | + self.assertIsNone(find_empty(full_grid)) |
| 46 | + |
| 47 | + def test_is_valid(self): |
| 48 | + # Placing 1 in grid at (0, 2) is valid |
| 49 | + self.assertTrue(is_valid(self.grid, 0, 2, 1)) |
| 50 | + # Placing 5 in grid at (0, 2) is invalid (already in row) |
| 51 | + self.assertFalse(is_valid(self.grid, 0, 2, 5)) |
| 52 | + # Placing 9 in grid at (0, 2) is invalid (already in 3x3 box) |
| 53 | + self.assertFalse(is_valid(self.grid, 0, 2, 9)) |
| 54 | + |
| 55 | + def test_solve_sudoku_backtracking(self): |
| 56 | + grid_copy = [row[:] for row in self.grid] |
| 57 | + stats = {"steps": 0} |
| 58 | + solved = solve_sudoku_backtracking(grid_copy, stats=stats) |
| 59 | + self.assertTrue(solved) |
| 60 | + self.assertIsNone(find_empty(grid_copy)) |
| 61 | + self.assertGreater(stats["steps"], 0) |
| 62 | + |
| 63 | + def test_generate_sudoku_puzzle(self): |
| 64 | + for diff in ["easy", "medium", "hard"]: |
| 65 | + puzzle, solution = generate_sudoku_puzzle(diff) |
| 66 | + # Board size should be 9x9 |
| 67 | + self.assertEqual(len(puzzle), 9) |
| 68 | + self.assertEqual(len(puzzle[0]), 9) |
| 69 | + self.assertEqual(len(solution), 9) |
| 70 | + self.assertEqual(len(solution[0]), 9) |
| 71 | + |
| 72 | + # Count empty cells |
| 73 | + empty_count = sum(row.count(0) for row in puzzle) |
| 74 | + if diff == "easy": |
| 75 | + self.assertEqual(empty_count, 46) |
| 76 | + elif diff == "medium": |
| 77 | + self.assertEqual(empty_count, 53) |
| 78 | + elif diff == "hard": |
| 79 | + self.assertEqual(empty_count, 61) |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + unittest.main() |
0 commit comments