def test_adjustment(): goal_pos = 20 ladders = [(2, 10), (9, 13), (12, 18)] chutes = [(4, 1), (7, 3), (17, 8)] test_cases = { 0: 0, 1: 0, 2: 8, 3: 0, 4: -3, 5: 0, 6: 0, 7: -4, 8: 0, 9: 4, 10: 0, 11: 0, 12: 6, 13: 0, 14: 0, 15: 0, 16: 0, 17: -9, 18: 0, 19: 0 } brd = Board(ladders=ladders, chutes=chutes, goal=goal_pos) for pos, change in test_cases.items(): assert brd.position_adjustment(pos) == change
def test_adjust_empty_board(): """"No position adjustment on empty board.""" goal_pos = 20 brd = Board(ladders=[], chutes=[], goal=goal_pos) for pos in range(goal_pos): assert brd.position_adjustment(pos) == 0
def test_multi_step_three(mocker): ''' Use mocked randint to test that player makes multiple moves of given length. ''' n_steps = 5 randint_value = 3 # Patch random.randint to always return randint_value mocker.patch('random.randint', return_value=randint_value) # Wrap a "spy" around random.randint to collect information about # how often and with which arguments it is called. randint_spy = mocker.spy(random, 'randint') b = Board(chutes=[], ladders=[]) pl = Player(b) for _ in range(n_steps): pl.move() # Check that player is in correct position assert pl.position == n_steps * randint_value # Check that random.randint was called once per call to move() assert randint_spy.call_count == n_steps # Check that random.randint was always called with arguments 1, 6 randint_spy.assert_has_calls(n_steps * [mocker.call(1, 6)])
def test_bad_boards(): """Test that bad board specifications are not accepted.""" with pytest.raises(ValueError): Board(ladders=[(10, 10)]) with pytest.raises(ValueError): Board(ladders=[(10, 9)]) with pytest.raises(ValueError): Board(chutes=[(10, 10)]) with pytest.raises(ValueError): Board(chutes=[(10, 12)]) with pytest.raises(ValueError): Board(goal=0)
def test_single_step_one(mocker): ''' Use mocked randint to test that a player makes a correct initial step of length 1. ''' # the next line replaces random.randint with a mock function # returning 1 for the rest of this test. mocker.patch('random.randint', return_value=1) b = Board(chutes=[], ladders=[]) pl = Player(b) pl.move() assert pl.position == 1
def test_default_board(): """Some tests on default board.""" brd = Board() assert brd.position_adjustment(1) == 39 assert brd.position_adjustment(2) == 0 assert brd.position_adjustment(33) == -30 assert not brd.goal_reached(89) assert brd.goal_reached(90) assert brd.goal_reached(91)
def test_multi_step_three(mocker): ''' Use mocked randint to test that player makes multiple moves of given length. ''' n_steps = 5 randint_value = 3 # the next line replaces random.randint with a mock function # returning randint_value for the rest of this test. mocker.patch('random.randint', return_value=randint_value) b = Board(chutes=[], ladders=[]) pl = Player(b) for _ in range(n_steps): pl.move() assert pl.position == n_steps * randint_value # Check that random.randint was called once per call to move() # with arguments 1 and 6. assert random.randint.mock_calls == n_steps * [mock.call(1, 6)]
sim.run_simulation(10) print(type_to_name(sim.players_per_type())) print(type_to_name(sim.winners_per_type())) print(type_to_name(sim.durations_per_type())) print('\n**** Second Simulation: Four players, standard board ****') sim = Simulation([Player, Player, LazyPlayer, ResilientPlayer]) print(type_to_name(sim.single_game())) sim.run_simulation(10) print(type_to_name(sim.players_per_type())) print(type_to_name(sim.winners_per_type())) print(type_to_name(sim.durations_per_type())) print('\n**** Third Simulation: Four players, small board ****') my_board = Board(ladders=[(3, 10), (5, 8)], chutes=[(9, 2)], goal=20) sim = Simulation([Player, Player, LazyPlayer, ResilientPlayer], board=my_board) print(type_to_name(sim.single_game())) sim.run_simulation(10) print(type_to_name(sim.players_per_type())) print(type_to_name(sim.winners_per_type())) print(type_to_name(sim.durations_per_type())) print('\n**** Fourth Simulation: Modified players, standard board ****') board = Board() sim = Simulation([ Player(board), LazyPlayer(board, dropped_steps=5), ResilientPlayer(board, extra_steps=5) ],
def test_goal_reached(): """Ensure goal_reached() does not yield false negatives.""" goal_pos = 20 brd = Board(ladders=[], chutes=[], goal=goal_pos) for pos in range(goal_pos, goal_pos + 10): assert brd.goal_reached(pos)