def test_saved_cases(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ mapping = {'K': King, 'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight} for parameter_filename in iglob(os.path.join(TEST_DATA_PATH, 'params_*')): case_nro = parameter_filename.split('/')[-1].split('_')[1] with open(parameter_filename, 'r') as parameter_file: input_parameters = json.loads(parameter_file.read()) board = Board(int(input_parameters['n']), int(input_parameters['m'])) pieces = [] for piece_type in input_parameters['pieces']: pieces.append(mapping[piece_type]()) res = [] for board in backtracking(board, pieces.copy(), pieces, set()): res.append(board) solution_filename = 'solution_{0}'.format(case_nro) expected = [] with open(os.path.join(TEST_DATA_PATH, solution_filename), 'r') as solution_file: for solution in solution_file: expected.append(Board.from_json(solution)) # just in case we test that all solutions are unique. assert len(expected) == len(res) for expected_res in expected: assert expected_res in res
def main(): """ Main function that instanciates the problem and then calls backtracking solver """ mapping = {'K': King, 'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight} args = parse_args() board = Board(int(args.n), int(args.m)) pieces = [] for piece_type in args.pieces.split(','): pieces.append(mapping[piece_type]()) res = set() start = time.time() for board in backtracking(board, pieces.copy(), pieces, res, args.animation): if args.output: with open(args.output, 'a') as output_file: if args.output_format == 'json': output_file.write(board.to_json() + '\n') if args.output_format == 'text': output_file.write(draw_board(board) + '\n') elif not args.animation: print(draw_board(board)) end = time.time() print('Total unique configurations found {0}'.format(len(res))) print('Total time {0} seconds'.format(end - start))
def test_king_takes_rook(): """test_of_a_bug_it_was_possible_to_put_a_king_that_takes_a_rook""" king = King() board = Board(3, 3) board.put(king, 0, 0) positions_to_take = king.positions_to_take(board, 0, 0) assert (0, 1) in positions_to_take
def test_complete_with_empty_board(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ board = Board(3, 3) assert board.complete([])
def main(): """ Generates test cases data used in tests. """ for case_nro in range(0, 20): dimension_n = randint(3, 5) dimension_m = randint(3, 5) mapping = {'K': King, 'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight} board = Board(dimension_n, dimension_m) pieces = [] number_of_pieces = randint(0, 6) for _ in range(0, number_of_pieces): pieces.append(mapping[choice([key for key in mapping.keys()])]()) inputs = { 'n': dimension_n, 'm': dimension_m, 'pieces': [piece.piece_identification for piece in pieces] } print('Generate case with {0}'.format(inputs)) input_filename = os.path.join(TEST_DATA_PATH, 'params_{0}'.format(case_nro)) with open(input_filename, 'w') as input_params_file: input_params_file.write(json.dumps(inputs)) solution_filename = os.path.join(TEST_DATA_PATH, 'solution_{0}'.format(case_nro)) with open(solution_filename, 'a') as output_file: for board in backtracking(board, pieces.copy(), pieces, set()): output_file.write(board.to_json() + '\n')
def test_king_invalid_move(): """test_king_cant_move_more_than_one_step""" king_piece = King() small_board = Board(3, 3) small_board.put(king_piece, 0, 0) assert king_piece.takes(small_board, 0, 0, 2, 2) is False assert king_piece.takes(small_board, 0, 0, 0, 2) is False
def test_complete_returns_true(): """ test_complete_returns_true_when_all_pieces_are_in_the_board """ king = King() board = Board(3, 3) board.put(king, 0, 0) assert board.complete([King()]) is True
def test_rook_invalid_check(): """test_rook_cant_move_can_move_in_diagonal_direction""" rook_piece = Rook() small_board = Board(3, 3) small_board.put(rook_piece, 1, 2) assert rook_piece.takes(small_board, 0, 0, 1, 1) is False assert rook_piece.takes(small_board, 0, 0, 2, 2) is False # just in case assert rook_piece.takes(small_board, 0, 0, 1, 2) is False
def test_free_with_empty_board(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ board = Board(3, 3) for i in range(0, 3): for j in range(0, 3): assert (i, j) in board.free_positions()
def test_from_json_two_pieces(): """test_from_json_with_a_board_with_two_pieces""" king = King() rook = Rook() expected_board = Board(3, 4) expected_board.put(king, 1, 1) expected_board.put(rook, 2, 2) res = Board.from_json( '{"m": 4, "pieces": {"(2, 2)": "R", "(1, 1)": "K"}, "n": 3}') assert res == expected_board
def test_pieces_positions_check(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ king = King() board = Board(3, 3) board.put(king, 1, 1) assert [position for position in board.piece_positions()] == [(1, 1)]
def test_to_json_two_pieces(): """test_to_json_with_a_board_with_two_pieces""" king = King() rook = Rook() board = Board(3, 4) board.put(king, 1, 1) board.put(rook, 2, 2) res = json.loads(board.to_json()) expected = json.loads( '{"m": 4, "pieces": {"(2, 2)": "R", "(1, 1)": "K"}, "n": 3}') assert res == expected
def test_conflict_case(): """test_conflict_special_case_found""" board = Board(6, 4) rook = Rook() queen = Queen() board.put(rook, 2, 1) assert board.conflict(4, 0) is False assert board.conflict(5, 2) is False assert (5, 2) in board.free_positions() board.put(queen, 4, 0) assert (5, 2) in board.free_positions() assert board.conflict(5, 2) is False
def test_rook_valid_moves(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ rook_piece = Rook() small_board = Board(3, 3) small_board.put(rook_piece, 0, 0) assert rook_piece.takes(small_board, 0, 0, 0, 0) assert rook_piece.takes(small_board, 0, 0, 0, 1) assert rook_piece.takes(small_board, 0, 0, 0, 2) assert rook_piece.takes(small_board, 0, 0, 1, 0) assert rook_piece.takes(small_board, 0, 0, 2, 0)
def test_takess_diagonal(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ bishop = Bishop() small_board = Board(3, 3) small_board.put(bishop, 0, 0) valid_positions = [ (0, 0), (1, 1), (2, 2), ] verify_piece_movement(bishop, small_board, valid_positions, 0, 0)
def test_very_simple(): """ A board with one position available. test_very_simple_1x1_board_with_one_piece """ expected = [{'pieces': {'(0, 0)': 'K'}, 'n': 1, 'm': 1}] board = Board(1, 1) pieces = [King()] board.put(pieces[0], 0, 0) res = [] for board in backtracking(board, pieces, pieces, set()): res.append(json.loads(board.to_json())) assert res == expected
def test_draw_a_board_with_one_king(): """ test draw with one king. checks that the king is drawn in the correct pos """ expected = "┌──────┐\n" expected += "│K _ _ │\n" expected += "│_ _ _ │\n" expected += "│_ _ _ │\n" expected += "└──────┘\n\n" board = Board(3, 3) king = King() board.put(king, 0, 0) res = draw_board(board) assert res == expected
def test_king_right_corner(): """ Checks that the king is drawn in the corner. Edge case test. """ expected = "┌──────┐\n" expected += "│_ _ _ │\n" expected += "│_ _ _ │\n" expected += "│_ _ K │\n" expected += "└──────┘\n\n" board = Board(3, 3) king = King() board.put(king, 2, 2) res = draw_board(board) assert res == expected
def test_takes_valid_ones(): """test_takes_in_diagonal_vertical_and_horizontal""" queen = Queen() small_board = Board(3, 3) small_board.put(queen, 0, 0) valid_positions = [ (0, 0), (1, 1), (2, 2), (0, 1), (0, 2), (1, 0), (2, 0), ] verify_piece_movement(queen, small_board, valid_positions, 0, 0)
def test_example_test_case_given(): """ This test case was given as an example. The assert were done manually before the to_json method was done. To make checks easily see: test_with_data which uses a "fuzzer" case generator to verify results. """ expected = [{ 'pieces': { '(2, 0)': 'K', '(1, 2)': 'R', '(0, 0)': 'K' }, 'm': 3, 'n': 3 }, { 'pieces': { '(0, 2)': 'K', '(2, 1)': 'R', '(0, 0)': 'K' }, 'm': 3, 'n': 3 }, { 'pieces': { '(0, 1)': 'R', '(2, 0)': 'K', '(2, 2)': 'K' }, 'm': 3, 'n': 3 }, { 'pieces': { '(0, 2)': 'K', '(1, 0)': 'R', '(2, 2)': 'K' }, 'm': 3, 'n': 3 }] pieces = [King(), King(), Rook()] board = Board(3, 3) res = [] for board in backtracking(board, pieces.copy(), pieces, set()): res.append(json.loads(board.to_json())) assert len(expected) == len(res) for expected_res in expected: assert expected_res in res
def load_test_case_solution(solution_filename): res = [] with open(solution_filename, 'r') as solution_file: for solution in solution_file: if solution.strip('\n'): res.append(Board.from_json(solution)) return res
def test_equal_same_positions(): """ test_equal_with_the_same_pieces_at_same_positions """ king = King() board = Board(3, 3) board.put(king, 0, 0) another_board = Board(3, 3) another_board.put(king, 0, 0) assert another_board == board assert another_board == board assert hash(another_board) == hash(board)
def test_used_king_ok(): """test_positions_used_from_for_king_in_the_upper_corner_are_valid""" king_piece = King() small_board = Board(3, 3) assert (0, 0) in king_piece.positions_to_take(small_board, 0, 0) assert (1, 0) in king_piece.positions_to_take(small_board, 0, 0) assert (1, 1) in king_piece.positions_to_take(small_board, 0, 0) assert (0, 1) in king_piece.positions_to_take(small_board, 0, 0) assert (0, 2) not in king_piece.positions_to_take(small_board, 0, 0)
def test_king_takes_happy_cases(): """ The King can move anywhere but only by one step. This test asserts that the function returns True for all valid cases """ king_piece = King() small_board = Board(3, 3) small_board.put(king_piece, 1, 1) assert king_piece.takes(small_board, 1, 1, 0, 0) assert king_piece.takes(small_board, 1, 1, 0, 1) assert king_piece.takes(small_board, 1, 1, 0, 2) assert king_piece.takes(small_board, 1, 1, 1, 0) assert king_piece.takes(small_board, 1, 1, 1, 1) assert king_piece.takes(small_board, 1, 1, 1, 2) assert king_piece.takes(small_board, 1, 1, 2, 0) assert king_piece.takes(small_board, 1, 1, 2, 0) assert king_piece.takes(small_board, 1, 1, 2, 1) assert king_piece.takes(small_board, 1, 1, 2, 2)
def load_test_case_input(parameter_filename): mapping = {'K': King, 'Q': Queen, 'R': Rook, 'B': Bishop, 'N': Knight} with open(parameter_filename, 'r') as parameter_file: input_parameters = json.loads(parameter_file.read()) board = Board(int(input_parameters['n']), int(input_parameters['m'])) pieces = [] for piece_type in input_parameters['pieces']: pieces.append(mapping[piece_type]()) return board, pieces
def test_equals_different_board_2(): """ test_equals_on_different_board_different_piece_in_same_position """ king = King() bishop = Bishop() board = Board(3, 3) board.put(king, 0, 0) another_board = Board(3, 3) another_board.put(bishop, 0, 0) assert another_board != board assert hash(another_board) != hash(board)
def test_draw_an_empy_board(): """ test draw method of an empty board """ expected = "┌──────┐\n" expected += "│_ _ _ │\n" expected += "│_ _ _ │\n" expected += "│_ _ _ │\n" expected += "└──────┘\n\n" board = Board(3, 3) res = draw_board(board) assert res == expected
def test_clean_frees_place(): """test_clean_frees_places_in_the_board""" board = Board(4, 4) rook = Rook() board.put(rook, 2, 2) expected = [(0, 1), (0, 0), (1, 3), (3, 3), (3, 0), (3, 1), (1, 0), (1, 1), (0, 3)] assert set(expected) == set(board.free_positions()) expected_after_clean = [(0, 1), (0, 0), (1, 3), (3, 3), (3, 0), (3, 1), (1, 0), (1, 1), (0, 3), (0, 2), (1, 2), (2, 2), (3, 2), (2, 0), (2, 1), (2, 2), (2, 3)] board.clean(2, 2) assert set(expected_after_clean) == set(board.free_positions())
def test_takess(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ knight = Knight() small_board = Board(5, 5) small_board.put(knight, 2, 2) valid_positions = [ (0, 1), (1, 0), (0, 3), (2, 2), (1, 4), (3, 0), (4, 1), (3, 4), (4, 3), ] verify_piece_movement(knight, small_board, valid_positions, 2, 2)
def test_queen_positions_to_take(): """ docstrings should comply to pep257 for every public class and method and module function (except from tests) """ board = Board(3, 3) queen = Queen() board.put(queen, 1, 1) expected = [ (0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2), ] res = queen.positions_to_take(board, 1, 1) assert set([pos for pos in res]) == set(expected)