示例#1
0
def test_get_sudoku_combinations_works_properly():
    sample_array = np.array([
        [0, 0, 0],
        [0, 5, 0],
        [1, 0, 0],
    ])
    sudoku = Sudoku(sample_array)
    row = 0
    col = 0
    values = np.array([1, 2])

    expected_sudoku_combinations = [
        Sudoku(np.array([
            [1, 0, 0],
            [0, 5, 0],
            [1, 0, 0],
        ])),
        Sudoku(np.array([
            [2, 0, 0],
            [0, 5, 0],
            [1, 0, 0],
        ])),
    ]

    # WHEN
    sudoku_combinations = sudoku.get_sudoku_combinations(row, col, values)

    # THEN
    for expected, generated in zip(expected_sudoku_combinations,
                                   sudoku_combinations):
        assert np.all(expected.array == generated.array)
示例#2
0
def test_copy_funkction_works_properly():
    # GIVEN
    sample_array = np.array([
        [0, 0, 0],
        [0, 5, 0],
        [1, 0, 0],
    ])
    sample_sudoku = Sudoku(sample_array)

    # WHEN
    copied_sudoku = sample_sudoku.copy()

    # THEN
    assert np.all(copied_sudoku.array == sample_sudoku.array)
    assert np.all(
        copied_sudoku._possibles.matrix == sample_sudoku._possibles.matrix)
示例#3
0
def test_copy_function_return_independent_copy():
    # GIVEN
    sample_array = np.array([
        [0, 0, 0],
        [0, 5, 0],
        [1, 0, 0],
    ])
    sample_sudoku = Sudoku(sample_array)

    # WHEN
    copied_sudoku = sample_sudoku.copy()
    copied_sudoku[0, 0] = 5
    copied_sudoku.update()

    # THEN
    assert np.any(copied_sudoku.array != sample_sudoku.array)
    assert np.any(
        copied_sudoku._possibles.matrix != sample_sudoku._possibles.matrix)
示例#4
0
def test_recursive_solver_works_for_simples_cases(probe):
    # GIVEN
    sudoku = Sudoku(load_sample_unsolved_sudoku(probe))

    # WHEN
    solved_sudoku = recursive_solver(sudoku)

    # THEN
    assert solved_sudoku.is_solved == True
    assert sudoku_is_valid(solved_sudoku.array) == True
示例#5
0
def test_get_possible_numbers_works_properly(indices, expected_numbers):
    # GIVEN
    sample_array = np.array([
        [1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 2, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 3, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 4, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 5, 8, 0, 0, 0],
        [0, 0, 0, 0, 0, 6, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 8, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 7, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 9],
    ])

    # WHEN
    sudoku = Sudoku(sample_array)
    sudoku.update()
    indices = sudoku.get_possible_numbers(*indices)

    # THEN
    assert np.all(indices == expected_numbers)
示例#6
0
def test_get_most_promising_cell_works_properly():
    # GIVEN
    sample_array = np.array([
        [1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 2, 0, 0, 0, 0, 0, 0, 1],
        [0, 0, 3, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 4, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 5, 8, 0, 0, 0],
        [0, 0, 0, 0, 0, 6, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 8, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 7, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 9],
    ])
    expected_indices = (1, 5)

    # WHEN
    sudoku = Sudoku(sample_array)
    sudoku.update()
    indices = sudoku.get_most_promising_cell()

    # THEN
    assert indices == expected_indices
示例#7
0
def test_draw_sudoku_works_properly_for_nonsolved_sudokus(probe):
    # GIVEN
    sample_path = f"./tests/app/images/sample_{probe}.png"
    sudoku = Sudoku(load_sample_unsolved_sudoku(probe))
    expected_suffix = ".png"

    # WHEN
    draw_sudoku(sudoku=sudoku, path=sample_path)

    # THEN
    assert Path(sample_path).suffix == expected_suffix

    # cleaning up!
    remove(sample_path)
示例#8
0
def test_filled_rows_property_works_properly():
    # GIVEN
    sample_array = np.array([
        [0, 0, 0],
        [0, 2, 0],
        [1, 0, 0],
    ])
    expected_filled_rows = np.array([1, 2])

    # WHEN
    sudoku = Sudoku(sample_array)

    # THEN
    assert np.all(sudoku.filled_rows == expected_filled_rows)
示例#9
0
def test_where_is_solved_property_works_properly_for_solved_sudoku():
    # GIVEN
    sample_array = np.array([
        [1, 2, 3],
        [3, 1, 2],
        [2, 3, 1],
    ])
    expected_result = True

    # WHEN
    sudoku = Sudoku(sample_array)

    # THEN
    assert sudoku.is_solved == expected_result
示例#10
0
def test_where_is_solved_property_works_properly_for_not_solved_sudoku():
    # GIVEN
    sample_array = np.array([
        [0, 0, 0],
        [0, 2, 0],
        [1, 0, 0],
    ])
    expected_result = False

    # WHEN
    sudoku = Sudoku(sample_array)

    # THEN
    assert sudoku.is_solved == expected_result
示例#11
0
def test_where_is_filled_property_works_properly():
    # GIVEN
    sample_array = np.array([
        [0, 0, 0],
        [0, 2, 0],
        [1, 0, 0],
    ])
    expected_where_is_empty = np.array([[1, 1], [2, 0]])

    # WHEN
    sudoku = Sudoku(sample_array)

    # THEN
    assert np.all(sudoku.where_is_filled == expected_where_is_empty)
示例#12
0
def test_filled_numbers_property_works_properly():
    # GIVEN
    sample_array = np.array([
        [0, 0, 0],
        [0, 5, 0],
        [1, 0, 0],
    ])
    expected_filled_numbers = np.array([5, 1])

    # WHEN
    sudoku = Sudoku(sample_array)

    # THEN
    assert np.all(sudoku.filled_numbers + 1 == expected_filled_numbers)
示例#13
0
def test_initialization_of_notempty_sudoku_object_returns_proper_object():
    # GIVEN
    sample_array = np.random.randint(low=0, high=10, size=((9, 9)))
    expected_dtype = np.uint8
    expected_params = ["array", "_possibles"]
    expected_shape = (9, 9)

    # WHEN
    sudoku_object = Sudoku(sample_array)

    # THEN
    for param in expected_params:
        assert hasattr(sudoku_object, param)

    assert sudoku_object.array.dtype == expected_dtype
    assert sudoku_object.array.shape == expected_shape