예제 #1
0
def test_abort_search__with_long_running_instance__returns_quickly(
        library_path):
    """ Performs a library call on a long running instance, and aborts after a short time.
    The time between the abort and the return should not exceed 100ms.
    To measure this time, the ExternalLibraryBinding is wrapped in a Thread which signals an Event
    once the computation has finished.
    """
    test_setup = (LONG_RUNNING_EXHSEARCH_INSTANCE, "NE", [(7, 6)], (3, 2))
    previous_shift_location = None
    board, piece = _create_board(test_setup)

    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)

    search_ended_event = threading.Event()
    concurrent_library_binding = ConcurrentExternalLibraryBinding(
        library_binding, search_ended_event)
    concurrent_library_binding.start()
    time.sleep(timedelta(milliseconds=10).total_seconds())
    assert not search_ended_event.is_set()
    start = time.time()
    library_binding.abort_search()
    search_ended_event.wait()
    stop = time.time()
    assert (stop - start) < 0.1
    if concurrent_library_binding.action:
        _assert_valid_action(concurrent_library_binding.action, board,
                             previous_shift_location, piece)
    else:
        assert concurrent_library_binding.action is None
예제 #2
0
def test_3by3_single_player_with_objective_on_leftover(library_path):
    test_setup = (MAZE_3BY3, "NE", [(0, 0)], "leftover")
    previous_shift_location = BoardLocation(1, 2)
    board, piece = _create_board(test_setup)

    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)
    action = library_binding.find_optimal_action()

    _assert_valid_action(action, board, previous_shift_location, piece)
예제 #3
0
def test_3by3_single_player_no_direct_path_without_previous_shift(
        library_path):
    test_setup = (MAZE_3BY3, "NE", [(0, 0)], (2, 2))
    previous_shift_location = None
    board, piece = _create_board(test_setup)

    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)
    action = library_binding.find_optimal_action()

    _assert_valid_action(action, board, previous_shift_location, piece)
예제 #4
0
def test_second_player_to_play__performs_valid_action(library_path):
    test_setup = (MAZE_3BY3, "NS", [(0, 0), (2, 2)], (2, 1))
    previous_shift_location = None
    board, _ = _create_board(test_setup)
    piece = board.pieces[1]

    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)
    action = library_binding.find_optimal_action()

    _assert_valid_action(action, board, previous_shift_location, piece)
    _assert_reaches_objective(action, board, piece)
예제 #5
0
def test_3by3_multiple_bindings_same_library(library_path):
    """ Binds three times to the same library """
    test_setup = (MAZE_3BY3, "NS", [(0, 0)], (0, 2))
    previous_shift_location = None

    boards = []
    for _ in range(3):
        board, piece = _create_board(test_setup)
        boards.append(board)

    bindings = [
        ExternalLibraryBinding(library_path,
                               board,
                               board.pieces[0],
                               previous_shift_location=previous_shift_location)
        for board in boards
    ]

    actions = [
        library_binding.find_optimal_action() for library_binding in bindings
    ]

    for i in range(3):
        _assert_valid_action(actions[i], boards[i], previous_shift_location,
                             boards[i].pieces[0])
예제 #6
0
def test_3by3_single_player_no_fresh_board(library_path):
    """ Creates a board and makes two shifts, so that the maze card ids
    are no longer regularly distributed row-first
    """
    test_setup = (MAZE_3BY3, "NS", [(0, 0)], (0, 2))
    previous_shift_location = BoardLocation(1, 2)
    board, piece = _create_board(test_setup)
    board.shift(BoardLocation(1, 2), 0)
    board.shift(BoardLocation(2, 1), 0)

    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)
    action = library_binding.find_optimal_action()

    _assert_valid_action(action, board, previous_shift_location, piece)
예제 #7
0
def test_3by3_single_player_no_pushback_rule(library_path):
    """ Performs two library calls: the first without previous shift,
    the second with a previous shift invalidating the shift result of the first call.
    """
    test_setup = (MAZE_3BY3, "NS", [(0, 0)], (0, 2))
    previous_shift_location = None
    board, piece = _create_board(test_setup)

    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)
    action_1 = library_binding.find_optimal_action()
    _assert_valid_action(action_1, board, previous_shift_location, piece)

    shift_location_1 = action_1[0][0]
    board, piece = _create_board(test_setup)
    previous_shift_location = board.opposing_border_location(shift_location_1)
    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)
    action_2 = library_binding.find_optimal_action()
    _assert_valid_action(action_2, board, previous_shift_location, piece)
    shift_location_2 = action_2[0][0]
    assert shift_location_2 != shift_location_1
예제 #8
0
def test_3by3_single_player_with_direct_path_concurrent(library_path):
    test_setup = (MAZE_3BY3, "NE", [(0, 0)], (0, 2))
    previous_shift_location = BoardLocation(0, 1)
    board, piece = _create_board(test_setup)

    library_binding = ExternalLibraryBinding(
        library_path,
        board,
        piece,
        previous_shift_location=previous_shift_location)

    search_ended_event = threading.Event()
    concurrent_library_binding = ConcurrentExternalLibraryBinding(
        library_binding, search_ended_event)
    concurrent_library_binding.start()
    search_ended_event.wait()
    if concurrent_library_binding.action:
        _assert_valid_action(concurrent_library_binding.action, board,
                             previous_shift_location, piece)
    else:
        assert concurrent_library_binding.action is None