def test_strategy(self): for action in [C, D]: m = MockPlayer(actions=[action]) p2 = Player() self.assertEqual(action, m.strategy(p2)) actions = [C, C, D, D, C, C] m = MockPlayer(actions=actions) p2 = Player() for action in actions: self.assertEqual(action, m.strategy(p2))
def test_update_history(self): player = Player() self.assertEqual(player.history, []) self.assertEqual(player.cooperations, 0) self.assertEqual(player.defections, 0) update_history(player, D) self.assertEqual(player.history, [D]) self.assertEqual(player.defections, 1) self.assertEqual(player.cooperations, 0) update_history(player, C) self.assertEqual(player.history, [D, C]) self.assertEqual(player.defections, 1) self.assertEqual(player.cooperations, 1)
def markov_test(self, responses, random_seed=None): """Test responses to the four possible one round histories. Input responses is simply the four responses to CC, CD, DC, and DD.""" # Construct the test lists histories = [[[C], [C]], [[C], [D]], [[D], [C]], [[D], [D]]] for i, history in enumerate(histories): # Needs to be in the inner loop in case player retains some state P1 = self.player() P2 = Player() test_responses(self, P1, P2, history[0], history[1], responses[i], random_seed=random_seed)
def responses_test(self, history_1, history_2, responses, random_seed=None, tournament_length=200): """Test responses to arbitrary histories. Input response_list is a list of lists, each of which consists of a list for the history of player 1, a list for the history of player 2, and a list for the subsequent moves by player one to test. """ P1 = self.player() P1.tournament_length = tournament_length P2 = Player() P2.tournament_length = tournament_length test_responses(self, P1, P2, history_1, history_2, responses, random_seed=random_seed)
def test_strategy(self): human = Human() expected_action = C actual_action = human.strategy(Player(), lambda: C) self.assertEqual(actual_action, expected_action)
def setUp(self): self.inspector = Player() self.game = Game()
def first_play_test(self, play, random_seed=None): """Tests first move of a strategy.""" P1 = self.player() P2 = Player() test_responses(self, P1, P2, [], [], [play], random_seed=random_seed)
def test_history_assignment(self): player = Player() with self.assertRaises(AttributeError): player.history = []