Example #1
0
 def test_solved_puzzle_is_solved(self):
     """example taken from http://www.sudokugame.com/rules.php"""
     tmp = Sudoku()
     tmp[0] = [2,4,8,3,9,5,7,1,6]
     tmp[1] = [5,7,1,6,2,8,3,4,9]
     tmp[2] = [9,3,6,7,4,1,5,8,2]
     tmp[3] = [6,8,2,5,3,9,1,7,4]
     tmp[4] = [3,5,9,1,7,4,6,2,8]
     tmp[5] = [7,1,4,8,6,2,9,5,3]
     tmp[6] = [8,6,3,4,1,7,2,9,5]
     tmp[7] = [1,9,5,2,8,6,4,3,7]
     tmp[8] = [4,2,7,9,5,3,8,6,1]
     self.assertTrue(tmp.solved())
     self.assertFalse(tmp.has_zero())
     self.assertTrue(tmp.consistent())
Example #2
0
class SudokuTest(unittest.TestCase):
    def setUp(self):
        self.puzzle = Sudoku()
        self.empty = Sudoku()
        for row in range(9):
            for col in range(9):
                self.puzzle[row][col] = 1 + (row + col) % 9
    def test_puzzle_initialized(self):
        self.assertEqual([1,2,3,4,5,6,7,8,9], self.puzzle[0])
        self.assertEqual([1,2,3,4,5,6,7,8,9], self.puzzle.col(0))
    def test_empty_initialized(self):
        self.assertEqual([0,0,0,0,0,0,0,0,0], self.empty[0])
        self.assertEqual([0,0,0,0,0,0,0,0,0], self.empty.col(0))
    def test_puzzle_rows_are_consistent(self):
        for row in range(9):
            self.assertTrue(self.puzzle.row_consistent(row))
    def test_puzzle_cols_are_consisten(self):
        for col in range(9):
            self.assertTrue(self.puzzle.col_consistent(col))
    def test_consistent_ignores_zero_entries(self):
        for index in range(9):
            self.assertTrue(self.empty.row_consistent(index))
            self.assertTrue(self.empty.col_consistent(index))
    def test_consistent_returns_false(self):
        self.puzzle[0][0] = 8
        self.assertFalse(self.puzzle.row_consistent(0))
        self.assertFalse(self.puzzle.col_consistent(0))
        self.puzzle[0][0] = 1
    def test_consistent_plays_nice_with_index(self):
        "this test doesn't check what it needs to"
        tmp = self.puzzle[8][8]
        self.puzzle[8][8] = 1
        self.assertFalse(self.puzzle.row_consistent(8))
        self.assertFalse(self.puzzle.col_consistent(8))
        self.puzzle[8][8] = tmp
    def test_squares_are_all_inconsistent(self):
        for i in range(9):
            self.assertFalse(self.puzzle.square_consistent(i))
    def test_squares_of_empty_are_consistent(self):
        for i in range(9):
            self.assertTrue(self.empty.square_consistent(i))
    def test_square_can_be_consistent(self):
        tmp = Sudoku()
        self.assertTrue(tmp.square_consistent(0))
        tmp[0][0] = 1
        tmp[0][1] = 2
        tmp[0][2] = 3
        self.assertTrue(tmp.square_consistent(0))
        tmp[1][0] = 4
        tmp[1][1] = 5
        tmp[1][2] = 3
        self.assertFalse(tmp.square_consistent(0))
        tmp[1][2] = 6
        self.assertTrue(tmp.square_consistent(0))
        tmp[2][0] = 7
        tmp[2][0] = 8
        self.assertTrue(tmp.square_consistent(0))
        tmp[2][2] = 5
        self.assertFalse(tmp.square_consistent(0))
    def test_puzzle_inconsistent(self):
        self.assertFalse(self.puzzle.consistent())
    def test_empty_consistent(self):
        self.assertTrue(self.empty.consistent())
    def test_puzzle_has_no_zero(self):
        self.assertFalse(self.puzzle.has_zero())
    def test_empty_has_zero(self):
        self.assertTrue(self.empty.has_zero())
    def test_puzzle_not_solved(self):
        self.assertFalse(self.puzzle.solved())
    def test_empty_not_solved(self):
        self.assertFalse(self.empty.solved())
    def test_solved_puzzle_is_solved(self):
        """example taken from http://www.sudokugame.com/rules.php"""
        tmp = Sudoku()
        tmp[0] = [2,4,8,3,9,5,7,1,6]
        tmp[1] = [5,7,1,6,2,8,3,4,9]
        tmp[2] = [9,3,6,7,4,1,5,8,2]
        tmp[3] = [6,8,2,5,3,9,1,7,4]
        tmp[4] = [3,5,9,1,7,4,6,2,8]
        tmp[5] = [7,1,4,8,6,2,9,5,3]
        tmp[6] = [8,6,3,4,1,7,2,9,5]
        tmp[7] = [1,9,5,2,8,6,4,3,7]
        tmp[8] = [4,2,7,9,5,3,8,6,1]
        self.assertTrue(tmp.solved())
        self.assertFalse(tmp.has_zero())
        self.assertTrue(tmp.consistent())
    def test_zeros(self):
        self.assertEqual(len(self.empty.zeros()), 81)
    def test_print(self):
        self.assertTrue(len(str(self.empty)) > 50)