"""All tests for the Sudoku Solver.""" import unittest from main import iterate_board from board import Board from square import Square class TestMain(unittest.TestCase): """TestCase for the main file.""" def test_iterate_over_various_board_difficulties(self): """Run the solver on different boards.""" strings = [( ".2..785..\n" "4...52...\n" "..1..3.2.\n" ".....1...\n" "7348.526.\n" "2.9.67..5\n" ".687..3.9\n" "342.1.7..\n" "19..86..2\n" ), ( "..1..6435\n" ".....1...\n" ".5.47.98.\n" "..2.8.7.9\n" "..87..612\n" ".64.1..5.\n" "91.3428.7\n" ".27....9.\n" "6.3.9.5..\n" ), ( ".....6...\n" "..2.35.8.\n" "....2..13\n" ".2....19.\n" "93....7..\n" "5....4...\n" "1......7.\n" ".7...142.\n" ".9.4.3...\n" ), ( "...82....\n" "...6.9..1\n" "9.4..1...\n" ".9.7..28.\n" ".......57\n" "7.5......\n" ".78962..5\n" ".2..8.7.3\n" ".6..74.2.\n" ), ( "4......1.\n" ".15.4.6..\n" ".....7...\n" "...21...8\n" "...734..2\n" ".3..8....\n" "8.19...45\n" ".4....7..\n" "2....3...\n" ), ( "3........\n" ".6...791.\n" ".....325.\n" ".19.645..\n" ".......6.\n" ".85......\n" "...8.....\n" "..1..97..\n" "47..5....\n" )] for string in strings[:-4]: while '.' in string: new_string = iterate_board(string) self.assertNotEqual(new_string, string) string = new_string class TestBoard(unittest.TestCase): """TestCase for Board.""" def setUp(self): self.string = ( ".2..785..\n" "4...52...\n" "..1..3.2.\n" ".....1...\n" "7348.526.\n" "2.9.67..5\n" ".687..3.9\n" "342.1.7..\n" "19..86..2\n" ) self.board = Board(self.string) def test_lines_are_correctly_initialized(self): """There should be 9 lines, each a set of its values.""" self.assertIsInstance(self.board.lines, list) self.assertEqual(len(self.board.lines), 9) for line in self.board.lines: self.assertIsInstance(line, set) self.assertNotEqual(len(line), 0) def test_columns_are_correctly_initialized(self): """There should be 9 columns, each a set of its values.""" self.assertIsInstance(self.board.columns, list) self.assertEqual(len(self.board.columns), 9) for column in self.board.columns: self.assertIsInstance(column, set) self.assertNotEqual(len(column), 0) def test_grids_are_correctly_initialized(self): """There should be 9 grids, each a set of its values.""" self.assertIsInstance(self.board.grids, list) self.assertEqual(len(self.board.grids), 9) for grid in self.board.grids: self.assertIsInstance(grid, set) self.assertNotEqual(len(grid), 0) def test_squares_are_correctly_initialized(self): """At instantiation, squares should be an empty list.""" self.assertIsInstance(self.board.squares, list) self.assertEqual(len(self.board.squares), 0) class TestSquare(unittest.TestCase): """TestCase for Square.""" def setUp(self): content = '.' self.board = [ Square(line, column, content) for line in range(9) for column in range(9) ] self.assertEqual(len(self.board), 81) def test_grid_is_correctly_computed(self): """Square.grid is correctly computed for all values.""" correct_grids = ( "000111222" "000111222" "000111222" "333444555" "333444555" "333444555" "666777888" "666777888" "666777888" ) for i, square in enumerate(self.board): self.assertEqual(square.grid_id, int(correct_grids[i])) if __name__ == '__main__': unittest.main()