Skip to content

Commit

Permalink
Merge 3c10c95 into aab4622
Browse files Browse the repository at this point in the history
  • Loading branch information
marcharper committed Feb 6, 2019
2 parents aab4622 + 3c10c95 commit 01fd43f
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ script:
- cd docs; make clean; make html
# Run the test suit with coverage
- cd ..
- travis_wait 60 coverage run --source=axelrod -m unittest discover
- travis_wait 60 coverage run --source=axelrod -m unittest discover -v
- coverage report -m
# Run the doctests
- python doctests.py
Expand Down
31 changes: 18 additions & 13 deletions axelrod/tests/integration/test_filtering.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import random
import unittest

from axelrod import all_strategies, filtered_strategies, seed
from axelrod import short_run_time_strategies, filtered_strategies, seed

from hypothesis import example, given, settings
from hypothesis.strategies import integers

strategies = random.sample(short_run_time_strategies, 20)


class TestFiltersAgainstComprehensions(unittest.TestCase):
"""
Expand All @@ -23,9 +26,9 @@ def test_boolean_filtering(self):
]

for classifier in classifiers:
comprehension = set([s for s in all_strategies if s.classifier[classifier]])
comprehension = set([s for s in strategies if s.classifier[classifier]])
filterset = {classifier: True}
filtered = set(filtered_strategies(filterset))
filtered = set(filtered_strategies(filterset, strategies=strategies))
self.assertEqual(comprehension, filtered)

@given(
Expand All @@ -38,46 +41,48 @@ def test_boolean_filtering(self):
max_memory_depth=float("inf"),
memory_depth=float("inf"),
)
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_memory_depth_filtering(
self, min_memory_depth, max_memory_depth, memory_depth
):

min_comprehension = set(
[
s
for s in all_strategies
for s in strategies
if s().classifier["memory_depth"] >= min_memory_depth
]
)
min_filterset = {"min_memory_depth": min_memory_depth}
min_filtered = set(filtered_strategies(min_filterset))
min_filtered = set(filtered_strategies(min_filterset,
strategies=strategies))
self.assertEqual(min_comprehension, min_filtered)

max_comprehension = set(
[
s
for s in all_strategies
for s in strategies
if s().classifier["memory_depth"] <= max_memory_depth
]
)
max_filterset = {"max_memory_depth": max_memory_depth}
max_filtered = set(filtered_strategies(max_filterset))
max_filtered = set(filtered_strategies(max_filterset,
strategies=strategies))
self.assertEqual(max_comprehension, max_filtered)

comprehension = set(
[
s
for s in all_strategies
for s in strategies
if s().classifier["memory_depth"] == memory_depth
]
)
filterset = {"memory_depth": memory_depth}
filtered = set(filtered_strategies(filterset))
filtered = set(filtered_strategies(filterset, strategies=strategies))
self.assertEqual(comprehension, filtered)

@given(seed_=integers(min_value=0, max_value=4294967295))
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_makes_use_of_filtering(self, seed_):
"""
Test equivalent filtering using two approaches.
Expand All @@ -91,14 +96,14 @@ def test_makes_use_of_filtering(self, seed_):
comprehension = set(
[
s
for s in all_strategies
for s in strategies
if set(classifier).issubset(set(s().classifier["makes_use_of"]))
]
)

seed(seed_)
filterset = {"makes_use_of": classifier}
filtered = set(filtered_strategies(filterset))
filtered = set(filtered_strategies(filterset, strategies=strategies))

self.assertEqual(
comprehension, filtered, msg="classifier: {}".format(classifier)
Expand Down
4 changes: 2 additions & 2 deletions axelrod/tests/integration/test_matches.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TestMatchOutcomes(unittest.TestCase):
),
turns=integers(min_value=1, max_value=20),
)
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_outcome_repeats(self, strategies, turns):
"""A test that if we repeat 3 matches with deterministic and well
behaved strategies then we get the same result"""
Expand All @@ -40,7 +40,7 @@ def test_outcome_repeats(self, strategies, turns):
turns=integers(min_value=1, max_value=20),
seed=integers(min_value=0, max_value=4294967295),
)
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_outcome_repeats_stochastic(self, strategies, turns, seed):
"""a test to check that if a seed is set stochastic strategies give the
same result"""
Expand Down
22 changes: 16 additions & 6 deletions axelrod/tests/integration/test_tournament.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import filecmp
import unittest

from hypothesis import given, settings

import axelrod
from axelrod.strategy_transformers import FinalTransformer
from axelrod.tests.property import tournaments


class TestTournament(unittest.TestCase):
Expand All @@ -29,12 +32,19 @@ def setUpClass(cls):
]
cls.expected_outcome.sort()

def test_full_tournament(self):
"""A test to check that tournament runs with all non cheating strategies."""
strategies = [strategy() for strategy in axelrod.strategies]
tournament = axelrod.Tournament(
name="test", players=strategies, game=self.game, turns=2, repetitions=1
)
@given(tournaments(
strategies=axelrod.short_run_time_strategies,
min_size=10,
max_size=30,
min_turns=2,
max_turns=40,
min_repetitions=1,
max_repetitions=4,
))
@settings(max_examples=1)
def test_big_tournaments(self, tournament):
"""A test to check that tournament runs with a sample of non-cheating
strategies."""
filename = "test_outputs/test_tournament.csv"
self.assertIsNone(
tournament.play(progress_bar=False, filename=filename, build_results=False)
Expand Down
2 changes: 1 addition & 1 deletion axelrod/tests/strategies/test_ann.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import axelrod
from axelrod.strategies.ann import split_weights

from .test_player import TestMatch, TestPlayer
from .test_player import TestPlayer

C, D = axelrod.Action.C, axelrod.Action.D

Expand Down
34 changes: 32 additions & 2 deletions axelrod/tests/strategies/test_meta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Tests for the various Meta strategies."""
import axelrod
from hypothesis import given, settings
from hypothesis.strategies import integers

import axelrod
from .test_player import TestPlayer

C, D = axelrod.Action.C, axelrod.Action.D
Expand Down Expand Up @@ -59,6 +61,34 @@ def test_repr(self):
),
)

@given(seed=integers(min_value=1, max_value=20000000))
@settings(max_examples=1)
def test_clone(self, seed):
# Test that the cloned player produces identical play
player1 = self.player()
player2 = player1.clone()
self.assertEqual(len(player2.history), 0)
self.assertEqual(player2.cooperations, 0)
self.assertEqual(player2.defections, 0)
self.assertEqual(player2.state_distribution, {})
self.assertEqual(player2.classifier, player1.classifier)
self.assertEqual(player2.match_attributes, player1.match_attributes)

turns = 10
for op in [
axelrod.Cooperator(),
axelrod.Defector(),
axelrod.TitForTat(),
]:
player1.reset()
player2.reset()
for p in [player1, player2]:
axelrod.seed(seed)
m = axelrod.Match((p, op), turns=turns)
m.play()
self.assertEqual(len(player1.history), turns)
self.assertEqual(player1.history, player2.history)


class TestMetaMajority(TestMetaPlayer):

Expand Down Expand Up @@ -692,7 +722,7 @@ def test_strategy(self):
)

opponent = axelrod.Defector()
actions = [(C, D)] * 7 + [((D, D))]
actions = [(C, D)] * 7 + [(D, D)]
self.versus_test(
opponent,
expected_actions=actions,
Expand Down
8 changes: 4 additions & 4 deletions axelrod/tests/strategies/test_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ def test_reset_clone(self):
clone = player.clone()
self.assertEqual(player, clone)

def test_clone(self):
@given(seed=integers(min_value=1, max_value=20000000))
@settings(max_examples=1)
def test_clone(self, seed):
# Test that the cloned player produces identical play
player1 = self.player()
if player1.name in ["Darwin", "Human"]:
Expand All @@ -468,7 +470,6 @@ def test_clone(self):
]:
player1.reset()
player2.reset()
seed = random.randint(0, 10 ** 6)
for p in [player1, player2]:
axelrod.seed(seed)
m = axelrod.Match((p, op), turns=turns)
Expand All @@ -481,7 +482,7 @@ def test_clone(self):
seed=integers(min_value=1, max_value=200),
turns=integers(min_value=1, max_value=200),
)
@settings(max_examples=1, max_iterations=1)
@settings(max_examples=1)
def test_memory_depth_upper_bound(self, strategies, seed, turns):
"""
Test that the memory depth is indeed an upper bound.
Expand All @@ -508,7 +509,6 @@ def versus_test(
expected_actions,
noise=None,
seed=None,
turns=10,
match_attributes=None,
attrs=None,
init_kwargs=None,
Expand Down
4 changes: 2 additions & 2 deletions axelrod/tests/unit/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_equality_filter(self):
larger=integers(min_value=11, max_value=100),
)
@example(smaller=0, larger=float("inf"))
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_inequality_filter(self, smaller, larger):
self.assertTrue(
passes_operator_filter(
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_list_filter(self):
larger=integers(min_value=11, max_value=100),
)
@example(smaller=0, larger=float("inf"))
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_passes_filterset(self, smaller, larger):

full_passing_filterset_1 = {
Expand Down
2 changes: 1 addition & 1 deletion axelrod/tests/unit/test_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def test_majority_fingerprint(self):
self.assertAlmostEqual(value, test_data[key], places=2)

@given(strategy_pair=strategy_lists(min_size=2, max_size=2))
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_pair_fingerprints(self, strategy_pair):
"""
A test to check that we can fingerprint
Expand Down
8 changes: 4 additions & 4 deletions axelrod/tests/unit/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_wrong_class_equality(self):
self.assertNotEqual(Game(), "wrong class")

@given(r=integers(), p=integers(), s=integers(), t=integers())
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_random_init(self, r, p, s, t):
"""Test init with random scores using the hypothesis library."""
expected_scores = {
Expand All @@ -55,14 +55,14 @@ def test_random_init(self, r, p, s, t):
self.assertEqual(game.scores, expected_scores)

@given(r=integers(), p=integers(), s=integers(), t=integers())
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_random_RPST(self, r, p, s, t):
"""Test RPST method with random scores using the hypothesis library."""
game = Game(r, s, t, p)
self.assertEqual(game.RPST(), (r, p, s, t))

@given(r=integers(), p=integers(), s=integers(), t=integers())
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_random_score(self, r, p, s, t):
"""Test score method with random scores using the hypothesis library."""
game = Game(r, s, t, p)
Expand All @@ -72,7 +72,7 @@ def test_random_score(self, r, p, s, t):
self.assertEqual(game.score((D, C)), (t, s))

@given(game=games())
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
def test_random_repr(self, game):
"""Test repr with random scores using the hypothesis library."""
expected_repr = "Axelrod game: (R,P,S,T) = {}".format(game.RPST())
Expand Down
4 changes: 2 additions & 2 deletions axelrod/tests/unit/test_match_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def test_build_single_match_params_with_fixed_length_unknown(self):
self.assertEqual(match.match_attributes, {"length": float("inf")})

@given(repetitions=integers(min_value=1, max_value=test_repetitions))
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
@example(repetitions=test_repetitions)
def test_build_match_chunks(self, repetitions):
rr = axelrod.MatchGenerator(
Expand All @@ -181,7 +181,7 @@ def test_build_match_chunks(self, repetitions):
self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions))

@given(repetitions=integers(min_value=1, max_value=test_repetitions))
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)
@example(repetitions=test_repetitions)
def test_spatial_build_match_chunks(self, repetitions):
cycle = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 1)]
Expand Down
2 changes: 1 addition & 1 deletion axelrod/tests/unit/test_moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def test_four_players(self):
self.assertEqual(mp.winning_strategy_name, str(axelrod.Defector()))

@given(strategies=strategy_lists(min_size=2, max_size=4))
@settings(max_examples=5, max_iterations=20)
@settings(max_examples=5)

# Two specific examples relating to cloning of strategies
@example(strategies=[axelrod.BackStabber, axelrod.MindReader])
Expand Down
Loading

0 comments on commit 01fd43f

Please sign in to comment.