<commit_before>import random
import unittest
import axelrod

C, D = 'C', 'D'


def test_four_vector(test_class, expected_dictionary):
    P1 = test_class.player()
    for key in sorted(expected_dictionary.keys()):
        test_class.assertAlmostEqual(
            P1._four_vector[key], expected_dictionary[key])

def test_responses(test_class, P1, P2, history_1, history_2, responses, random_seed=None):
    """Test responses to arbitrary histories. Used for the the following tests
    in TestPlayer: first_play_test, markov_test, and responses_test.
    Works for arbitrary players as well. Input response_lists 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."""
    if random_seed:
        random.seed(random_seed)
    P1.history, P2.history = history_1, history_2
    for response in responses:
        test_class.assertEqual(P1.strategy(P2), response)


class TestPlayer(unittest.TestCase):

    name = "Player"
    player = axelrod.Player
    stochastic = False

    def test_initialisation(self):
        """Test that the player initiates correctly."""
        self.assertEqual(self.player().history, [])
        self.assertEqual(self.player().stochastic, self.stochastic)

    def test_repr(self):
        """Test that the representation is correct."""
        self.assertEquals(str(self.player()), self.name)

    def test_reset(self):
        """Make sure reseting works correctly."""
        p = self.player()
        p.history = [C, C]
        p.reset()
        self.assertEquals(p.history, [])

    def test_strategy(self):
        """Test that strategy method."""
        self.assertEquals(self.player().strategy(self.player()), None)

    def first_play_test(self, play, random_seed=None):
        """Tests first move of a strategy."""
        P1 = self.player()
        P2 = axelrod.Player()
        test_responses(
            self, P1, P2, [], [], [play],
            random_seed=random_seed)

    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."""
        P1 = self.player()
        P2 = axelrod.Player()
        # Construct the test lists
        histories = [
            [[C], [C]], [[C], [D]], [[D], [C]],
            [[D], [D]]]
        response_lists = []
        for i, history in enumerate(histories):
            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):
        """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()
        P2 = axelrod.Player()
        test_responses(
            self, P1, P2, history_1, history_2, responses, random_seed=random_seed)<commit_msg>Heads Up Testing of various strategies<commit_after>import random
import unittest
import axelrod

C, D = 'C', 'D'


def test_four_vector(test_class, expected_dictionary):
    P1 = test_class.player()
    for key in sorted(expected_dictionary.keys()):
        test_class.assertAlmostEqual(
            P1._four_vector[key], expected_dictionary[key])

def test_responses(test_class, P1, P2, history_1, history_2, responses, random_seed=None):
    """Test responses to arbitrary histories. Used for the the following tests
    in TestPlayer: first_play_test, markov_test, and responses_test.
    Works for arbitrary players as well. Input response_lists 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."""
    if random_seed:
        random.seed(random_seed)
    P1.history, P2.history = history_1, history_2
    for response in responses:
        test_class.assertEqual(P1.strategy(P2), response)

class TestPlayer(unittest.TestCase):

    name = "Player"
    player = axelrod.Player
    stochastic = False

    def test_initialisation(self):
        """Test that the player initiates correctly."""
        self.assertEqual(self.player().history, [])
        self.assertEqual(self.player().stochastic, self.stochastic)

    def test_repr(self):
        """Test that the representation is correct."""
        self.assertEquals(str(self.player()), self.name)

    def test_reset(self):
        """Make sure reseting works correctly."""
        p = self.player()
        p.history = [C, C]
        p.reset()
        self.assertEquals(p.history, [])

    def test_strategy(self):
        """Test that strategy method."""
        self.assertEquals(self.player().strategy(self.player()), None)

    def first_play_test(self, play, random_seed=None):
        """Tests first move of a strategy."""
        P1 = self.player()
        P2 = axelrod.Player()
        test_responses(
            self, P1, P2, [], [], [play],
            random_seed=random_seed)

    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."""
        P1 = self.player()
        P2 = axelrod.Player()
        # Construct the test lists
        histories = [
            [[C], [C]], [[C], [D]], [[D], [C]],
            [[D], [D]]]
        response_lists = []
        for i, history in enumerate(histories):
            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):
        """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()
        P2 = axelrod.Player()
        test_responses(
            self, P1, P2, history_1, history_2, responses, random_seed=random_seed)

class TestHeadsUp(unittest.TestCase):
    
    def versus_test(self, player_1_class, player_2_class, outcomes, player_1_history=None, player_2_history=None, random_seed=None):
        if random_seed:
            random.seed(random_seed)
        player_1 = player_1_class()
        player_2 = player_2_class()
        if player_1.history is not None:
            player_1.history = player_1_history
        else:
            player_1.history = []
        if player_2.history is not None:
            player_2.history = player_2_history
        else:
            player_2.history = []
        for outcome_1, outcome_2 in outcomes:
            player_1.play(player_2)
            self.assertEqual(player_1_history[-1], outcome_1)
            self.assertEqual(player_2_history[-1], outcome_2)
{'fraction_non_alphanumeric': 0.10393415178571429, 'fraction_numerical': 0.013113839285714286, 'mean_word_length': 2.7164333851736653, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7121709', 'n_tokens_mistral': 2097, 'n_tokens_neox': 2045, 'n_words': 641}