Skip to content

Commit

Permalink
Merge pull request #1238 from Axelrod-Python/warnings
Browse files Browse the repository at this point in the history
Hypothesis warnings and speed up some tests
  • Loading branch information
meatballs committed Feb 11, 2019
2 parents aab4622 + dc30880 commit 09b92e5
Show file tree
Hide file tree
Showing 20 changed files with 147 additions and 100 deletions.
2 changes: 1 addition & 1 deletion axelrod/strategies/shortmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ShortMem(Player):

name = "ShortMem"
classifier = {
"memory_depth": 10,
"memory_depth": float('inf'),
"stochastic": False,
"makes_use_of": set(),
"long_run_time": False,
Expand Down
44 changes: 26 additions & 18 deletions axelrod/tests/integration/test_filtering.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest

from axelrod import all_strategies, filtered_strategies, seed

from axelrod import filtered_strategies, seed, short_run_time_strategies
from axelrod.tests.property import strategy_lists
from hypothesis import example, given, settings
from hypothesis.strategies import integers

Expand All @@ -12,7 +12,8 @@ class TestFiltersAgainstComprehensions(unittest.TestCase):
match the results from using a list comprehension.
"""

def test_boolean_filtering(self):
@given(strategies=strategy_lists(min_size=20, max_size=20))
def test_boolean_filtering(self, strategies):

classifiers = [
"stochastic",
Expand All @@ -23,62 +24,69 @@ 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(
min_memory_depth=integers(min_value=1, max_value=10),
max_memory_depth=integers(min_value=1, max_value=10),
memory_depth=integers(min_value=1, max_value=10),
strategies=strategy_lists(min_size=20, max_size=20),
)
@example(
min_memory_depth=float("inf"),
max_memory_depth=float("inf"),
memory_depth=float("inf"),
strategies=short_run_time_strategies,
)
@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
self, min_memory_depth, max_memory_depth, memory_depth,
strategies
):

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)
def test_makes_use_of_filtering(self, seed_):
@given(seed_=integers(min_value=0, max_value=4294967295),
strategies=strategy_lists(min_size=20, max_size=20),
)
@settings(max_examples=5)
def test_makes_use_of_filtering(self, seed_, strategies):
"""
Test equivalent filtering using two approaches.
Expand All @@ -91,14 +99,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=210,
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
36 changes: 33 additions & 3 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 @@ -339,7 +369,7 @@ class TestMetaMajorityFiniteMemory(TestMetaPlayer):
}

def test_strategy(self):
actions = [(C, C), (C, D), (D, C), (C, D), (C, C)]
actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions)


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
9 changes: 5 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 @@ -490,6 +491,7 @@ def test_memory_depth_upper_bound(self, strategies, seed, turns):
memory = player.classifier["memory_depth"]
if memory < float("inf"):
for strategy in strategies:
player.reset()
opponent = strategy()
self.assertTrue(
test_memory(
Expand All @@ -508,7 +510,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/strategies/test_shortmem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
C, D = axelrod.Action.C, axelrod.Action.D


class TestCooperator(TestPlayer):
class TestShortMem(TestPlayer):

name = "ShortMem"
player = axelrod.ShortMem
expected_classifier = {
"memory_depth": 10,
"memory_depth": float('inf'),
"stochastic": False,
"makes_use_of": set(),
"inspects_source": False,
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
Loading

0 comments on commit 09b92e5

Please sign in to comment.