Пример #1
0
    def test_solve(self):
        with open("solved.su", "r") as f:
            dat = f.read().strip()
            solved = sudoku.loads(dat)

        with open("unsolved.su", "r") as f:
            dat = f.read().strip()
            unsolved = sudoku.loads(dat)

        hopefully_solved = sudoku.solve(unsolved)
        self.assertEqual(hopefully_solved, solved)
Пример #2
0
 def test_loads_dumps(self):
     with open("solved.su", "r") as f:
         dat = f.read().strip()
     sudoku_arry = sudoku.loads(dat)
     self.assertEqual(len(sudoku_arry), 9)
     self.assertEqual(len(sudoku_arry[0]), 9)
     sudoku_string = sudoku.dumps(sudoku_arry)
     self.assertEqual(sudoku_string, dat)
Пример #3
0
    def test_validate(self):
        with open("solved.su", "r") as f:
            dat = f.read().strip()
        # This test sudoku is a valid one!
        sudoku_arry = sudoku.loads(dat)
        self.assertTrue(sudoku.validate(sudoku_arry))

        # Violate several rules...
        sudoku_arry[0][0] = 6
        sudoku_arry[1][1] = 6
        self.assertFalse(sudoku.validate(sudoku_arry))

        # Try using an unsolved sudoku!
        with open("unsolved.su", "r") as f:
            dat = f.read().strip()
        # This sudoku is not even solved!
        sudoku_arry = sudoku.loads(dat)
        self.assertFalse(sudoku.validate(sudoku_arry))
Пример #4
0
    def test_fill_spaces(self):
        with open("solved.su", "r") as f:
            dat = f.read().strip()
            solved = sudoku.loads(dat)

        unsolved = copy.copy(solved)
        unsolved[2][2] = 0  # mark a single space as unknown
        self.assertFalse(sudoku.validate(unsolved))
        self.assertEqual(sudoku.count_unknown(unsolved), 1)

        # Can we solve it?
        maybe_solved = sudoku.fill_spaces(unsolved)
        self.assertEqual(sudoku.count_unknown(maybe_solved), 0)
        self.assertEqual(maybe_solved, solved)
        self.assertTrue(sudoku.validate(maybe_solved))
Пример #5
0
    def test_column_to_psuedo_row(self):
        with open("solved.su", "r") as f:
            dat = f.read().strip()
        sudoku_arry = sudoku.loads(dat)
        self.assertEqual(
            sudoku.column_to_psuedo_row(sudoku_arry, 0),
            [1, 4, 7, 2, 5, 8, 3, 6, 9])

        self.assertEqual(
            sudoku.column_to_psuedo_row(sudoku_arry, 1),
            [2, 5, 8, 3, 6, 9, 1, 4, 7])

        self.assertEqual(
            sudoku.column_to_psuedo_row(sudoku_arry, 3),
            [4, 7, 1, 5, 8, 2, 6, 9, 3])

        self.assertEqual(
            sudoku.column_to_psuedo_row(sudoku_arry, 8),
            [9, 3, 6, 7, 1, 4, 8, 2, 5])
Пример #6
0
    def test_square_to_psuedo_row(self):
        with open("solved.su", "r") as f:
            dat = f.read().strip()
        sudoku_arry = sudoku.loads(dat)
        self.assertEqual(
            sudoku.square_to_psuedo_row(sudoku_arry, 0, 0),
            [1, 2, 3, 4, 5, 6, 7, 8, 9])

        self.assertEqual(
            sudoku.square_to_psuedo_row(sudoku_arry, 2, 2),
            [9, 7, 8, 3, 1, 2, 6, 4, 5])

        self.assertEqual(
            sudoku.square_to_psuedo_row(sudoku_arry, 1, 0),
            [4, 5, 6, 7, 8, 9, 1, 2, 3])

        self.assertEqual(
            sudoku.square_to_psuedo_row(sudoku_arry, 0, 1),
            [2, 3, 1, 5, 6, 4, 8, 9, 7])