def test_same_winner_single_game_with_given_seed(self): """ Test if the seed works, giving the same winners """ players = [cs.Player, cs.LazyPlayer] sim = cs.Simulation(player_field=players, seed=1) winner = sim.single_game() sim2 = cs.Simulation(player_field=players, seed=1) test_winner = sim2.single_game() assert winner == test_winner
def test_randomize_players(self): """ Tests if the shuffle works, and gives different result with same seed """ p = [ cs.Player, cs.Player, cs.ResilientPlayer, cs.ResilientPlayer, cs.ResilientPlayer, cs.LazyPlayer ] sim1 = cs.Simulation(p, randomize_players=True) sim2 = cs.Simulation(p, randomize_players=False) sim1.run_simulation(10) sim2.run_simulation(10) assert sim1.get_results() is not sim2.get_results()
def test_winner_get_result(): """ Tests that the winner is correct """ sim = sa.Simulation([sa.LazyPlayer]) sim.run_simulation(1) assert (sim.get_results()[0][1]) == sa.LazyPlayer.__name__
def test_players_per_type(self): """player_per_type() returns dict mapping names to non-neg numbers.""" s = cs.Simulation([cs.Player, cs.LazyPlayer, cs.ResilientPlayer]) p = s.players_per_type() assert all(k in ['Player', 'LazyPlayer', 'ResilientPlayer'] for k in p.keys()) assert all(v >= 0 for v in p.values())
def test_single_game(self): """ Last part of the output is a tuple. """ s = cs.Simulation([cs.Player, cs.Player]) end = s.single_game() assert type(end) == tuple
def test_winners_per_type(self): """ Returns the right number of wins to the player. """ s = cs.Simulation([cs.Player]) s.run_simulation(6) w = s.winners_per_type() assert w['Player'] == 6
def test_steps_taken(): """ Test to assure that no one can win without taking any steps """ sim = sa.Simulation([sa.LazyPlayer, sa.Player, sa.ResilientPlayer]) sim.run_simulation(1) assert sim.get_results()[0][0] > 0
def test_durations_per_type(self): """ Checking if the output type is correct. """ s = cs.Simulation([cs.LazyPlayer, cs.Player]) s.run_simulation(10) w = s.durations_per_type() assert w['Player'] or w['LazyPlayer'] == int
def test_players_per_type(self): """ Checking if the output type is correct. """ s = cs.Simulation([cs.Player, cs.LazyPlayer, cs.ResilientPlayer]) s.run_simulation(10) w = s.durations_per_type() assert type(w['Player']) == list
def test_winners_per_type(self): """winners_per_type() returns dict mapping names to non-neg numbers.""" s = cs.Simulation([cs.Player, cs.LazyPlayer, cs.ResilientPlayer]) s.run_simulation(10) w = s.winners_per_type() assert all(k in ['Player', 'LazyPlayer', 'ResilientPlayer'] for k in w.keys()) assert all(v >= 0 for v in w.values())
def test_constructor_named(self): """Constructor with kw args works.""" b = cs.Board() s = cs.Simulation(player_field=[cs.Player, cs.Player], board=b, seed=123, randomize_players=True) assert isinstance(s, cs.Simulation)
def test_amount_of_winners_single_game(): """ Test that there is only one winner per game """ sim = sa.Simulation([sa.LazyPlayer, sa.Player, sa.ResilientPlayer]) sim.run_simulation(1) assert not (len(sim.get_results()) == 2) # Assert not to check that the # value is not higher than one. assert len(sim.get_results()) == 1
def test_player_per_type(): """ Tests if amount of player per type gives correct output """ player_list = [sa.LazyPlayer, sa.LazyPlayer, sa.Player, sa.ResilientPlayer] sim = sa.Simulation(player_list) sim.run_simulation(1) assert sim.players_per_type()["LazyPlayer"] == 2 assert sim.players_per_type()["Player"] == 1 assert not sim.players_per_type()["ResilientPlayer"] == 2
def test_shuffle(): """ Tests if player list actually gets shuffled if told so """ player_list = [sa.LazyPlayer, sa.Player, sa.ResilientPlayer] sim = sa.Simulation( [sa.LazyPlayer, sa.Player, sa.ResilientPlayer], randomize_players=True ) sim.single_game() assert player_list != sim.player_classes
def test_players_per_type_is_one(self): """ Tests if we have one of each players """ player_types = {'Player': 1, 'LazyPlayer': 1, 'ResilientPlayer': 1} p = [cs.Player, cs.LazyPlayer, cs.ResilientPlayer] sim = cs.Simulation(p) assert sim.players_per_type() == player_types
def test_players_per_type_is_zero(self): """ Tests if no players, gives a dictionary without players in the counter """ player_types = {'Player': 0, 'LazyPlayer': 0, 'ResilientPlayer': 0} p = [] sim = cs.Simulation(p) assert sim.players_per_type() == player_types
def test_known_winner_in_single_game_with_given_seed(self): """ Tests if the winner is same as a given winner, with given seed """ players = [cs.Player, cs.LazyPlayer] sim = cs.Simulation(player_field=players, seed=1) winner = sim.single_game() test_winner = (29, 'Player') assert winner == test_winner
def test_simulations_single_game(self): """Test if single_game returns the right kind of value""" sim = cs.Simulation([ cs.Player, cs.Player, cs.LazyPlayer, cs.LazyPlayer, cs.ResilientPlayer, cs.ResilientPlayer ], randomize_players=False) run = sim.single_game() assert run == (15, 'LazyPlayer')
def test_durations_per_type(self): """ durations_per_type() returns dict mapping names to list of non-neg numbers. """ s = cs.Simulation([cs.Player, cs.LazyPlayer, cs.ResilientPlayer]) s.run_simulation(10) w = s.durations_per_type() assert all(k in ['Player', 'LazyPlayer', 'ResilientPlayer'] for k in w.keys()) assert all(len(v) >= 0 for v in w.values()) assert all(n >= 0 for v in w.values() for n in v)
def test_simulation_results(self): """ - Multiple calls to run_simulation() aggregate results - get_results() returns list of result tuples """ s = cs.Simulation([cs.Player, cs.Player]) s.run_simulation(2) r = s.get_results() assert len(r) == 2 s.run_simulation(1) r = s.get_results() assert len(r) == 3 assert all(s > 0 and t == 'Player' for s, t in r)
def test_resilient_player_wins(): """ From how Resilient player is coded, he should win given a higher amount of simulations and the same seed """ player_list = [sa.LazyPlayer, sa.Player, sa.ResilientPlayer] sim = sa.Simulation(player_list) sim.run_simulation(1) winner_types = sim.winners_per_type() assert ( winner_types["ResilientPlayer"] > winner_types["LazyPlayer"] and winner_types["ResilientPlayer"] > winner_types["Player"] )
def test_simulations_players_per_type(self): """Test if players_per_type returns the right kind of value""" sim = cs.Simulation([ cs.Player, cs.Player, cs.LazyPlayer, cs.LazyPlayer, cs.ResilientPlayer, cs.ResilientPlayer ], randomize_players=False) sim.run_simulation(5) assert sim.players_per_type() == { 'ResilientPlayer': 2, 'LazyPlayer': 2, 'Player': 2 }
def test_simulations_get_results(self): """Test if get_results returns right kind of value""" sim = cs.Simulation([ cs.Player, cs.Player, cs.LazyPlayer, cs.LazyPlayer, cs.ResilientPlayer, cs.ResilientPlayer ], randomize_players=False) sim.run_simulation(5) results = sim.get_results() assert results == [(15, 'LazyPlayer'), (6, 'LazyPlayer'), (21, 'ResilientPlayer'), (13, 'Player'), (5, 'Player')]
def test_simulations_durations_per_type(self): """Test if durations_per_type returns the right value""" sim = cs.Simulation([ cs.Player, cs.Player, cs.LazyPlayer, cs.LazyPlayer, cs.ResilientPlayer, cs.ResilientPlayer ], randomize_players=False) sim.run_simulation(5) durations = sim.durations_per_type() assert durations == { 'Player': [13, 5], 'ResilientPlayer': [21], 'LazyPlayer': [15, 6] }
def test_constructor_default(self): """Default constructor works.""" s = cs.Simulation([cs.Player, cs.Player]) assert isinstance(s, cs.Simulation)
def test_run_simulation(self): """run_simulation() can be called""" s = cs.Simulation([cs.Player, cs.Player]) s.run_simulation(2)
def test_single_game(self): """single_game() returns non-negative number and class name""" s = cs.Simulation([cs.Player, cs.Player]) nos, wc = s.single_game() assert nos > 0 assert wc == 'Player'
def test_single_game(self): b = cs.Board() s = cs.Simulation([cs.Player, cs.LazyPlayer, cs.ResilientPlayer]) assert type(s.single_game()) == tuple
def test_run_simulation(self): b = cs.Board() s = cs.Simulation([cs.Player, cs.LazyPlayer, cs.ResilientPlayer]) s.run_simulation(5) assert len(s.winning_list) == 5