Skip to content

Commit 09b92e5

Browse files
authored
Merge pull request #1238 from Axelrod-Python/warnings
Hypothesis warnings and speed up some tests
2 parents aab4622 + dc30880 commit 09b92e5

20 files changed

+147
-100
lines changed

axelrod/strategies/shortmem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ShortMem(Player):
2222

2323
name = "ShortMem"
2424
classifier = {
25-
"memory_depth": 10,
25+
"memory_depth": float('inf'),
2626
"stochastic": False,
2727
"makes_use_of": set(),
2828
"long_run_time": False,

axelrod/tests/integration/test_filtering.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22

3-
from axelrod import all_strategies, filtered_strategies, seed
4-
3+
from axelrod import filtered_strategies, seed, short_run_time_strategies
4+
from axelrod.tests.property import strategy_lists
55
from hypothesis import example, given, settings
66
from hypothesis.strategies import integers
77

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

15-
def test_boolean_filtering(self):
15+
@given(strategies=strategy_lists(min_size=20, max_size=20))
16+
def test_boolean_filtering(self, strategies):
1617

1718
classifiers = [
1819
"stochastic",
@@ -23,62 +24,69 @@ def test_boolean_filtering(self):
2324
]
2425

2526
for classifier in classifiers:
26-
comprehension = set([s for s in all_strategies if s.classifier[classifier]])
27+
comprehension = set([s for s in strategies if s.classifier[classifier]])
2728
filterset = {classifier: True}
28-
filtered = set(filtered_strategies(filterset))
29+
filtered = set(filtered_strategies(filterset, strategies=strategies))
2930
self.assertEqual(comprehension, filtered)
3031

3132
@given(
3233
min_memory_depth=integers(min_value=1, max_value=10),
3334
max_memory_depth=integers(min_value=1, max_value=10),
3435
memory_depth=integers(min_value=1, max_value=10),
36+
strategies=strategy_lists(min_size=20, max_size=20),
3537
)
3638
@example(
3739
min_memory_depth=float("inf"),
3840
max_memory_depth=float("inf"),
3941
memory_depth=float("inf"),
42+
strategies=short_run_time_strategies,
4043
)
41-
@settings(max_examples=5, max_iterations=20)
44+
@settings(max_examples=5)
4245
def test_memory_depth_filtering(
43-
self, min_memory_depth, max_memory_depth, memory_depth
46+
self, min_memory_depth, max_memory_depth, memory_depth,
47+
strategies
4448
):
4549

4650
min_comprehension = set(
4751
[
4852
s
49-
for s in all_strategies
53+
for s in strategies
5054
if s().classifier["memory_depth"] >= min_memory_depth
5155
]
5256
)
5357
min_filterset = {"min_memory_depth": min_memory_depth}
54-
min_filtered = set(filtered_strategies(min_filterset))
58+
min_filtered = set(filtered_strategies(min_filterset,
59+
strategies=strategies))
5560
self.assertEqual(min_comprehension, min_filtered)
5661

5762
max_comprehension = set(
5863
[
5964
s
60-
for s in all_strategies
65+
for s in strategies
6166
if s().classifier["memory_depth"] <= max_memory_depth
6267
]
6368
)
6469
max_filterset = {"max_memory_depth": max_memory_depth}
65-
max_filtered = set(filtered_strategies(max_filterset))
70+
max_filtered = set(filtered_strategies(max_filterset,
71+
strategies=strategies))
6672
self.assertEqual(max_comprehension, max_filtered)
6773

6874
comprehension = set(
6975
[
7076
s
71-
for s in all_strategies
77+
for s in strategies
7278
if s().classifier["memory_depth"] == memory_depth
7379
]
7480
)
7581
filterset = {"memory_depth": memory_depth}
76-
filtered = set(filtered_strategies(filterset))
82+
filtered = set(filtered_strategies(filterset, strategies=strategies))
7783
self.assertEqual(comprehension, filtered)
7884

79-
@given(seed_=integers(min_value=0, max_value=4294967295))
80-
@settings(max_examples=5, max_iterations=20)
81-
def test_makes_use_of_filtering(self, seed_):
85+
@given(seed_=integers(min_value=0, max_value=4294967295),
86+
strategies=strategy_lists(min_size=20, max_size=20),
87+
)
88+
@settings(max_examples=5)
89+
def test_makes_use_of_filtering(self, seed_, strategies):
8290
"""
8391
Test equivalent filtering using two approaches.
8492
@@ -91,14 +99,14 @@ def test_makes_use_of_filtering(self, seed_):
9199
comprehension = set(
92100
[
93101
s
94-
for s in all_strategies
102+
for s in strategies
95103
if set(classifier).issubset(set(s().classifier["makes_use_of"]))
96104
]
97105
)
98106

99107
seed(seed_)
100108
filterset = {"makes_use_of": classifier}
101-
filtered = set(filtered_strategies(filterset))
109+
filtered = set(filtered_strategies(filterset, strategies=strategies))
102110

103111
self.assertEqual(
104112
comprehension, filtered, msg="classifier: {}".format(classifier)

axelrod/tests/integration/test_matches.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TestMatchOutcomes(unittest.TestCase):
2424
),
2525
turns=integers(min_value=1, max_value=20),
2626
)
27-
@settings(max_examples=5, max_iterations=20)
27+
@settings(max_examples=5)
2828
def test_outcome_repeats(self, strategies, turns):
2929
"""A test that if we repeat 3 matches with deterministic and well
3030
behaved strategies then we get the same result"""
@@ -40,7 +40,7 @@ def test_outcome_repeats(self, strategies, turns):
4040
turns=integers(min_value=1, max_value=20),
4141
seed=integers(min_value=0, max_value=4294967295),
4242
)
43-
@settings(max_examples=5, max_iterations=20)
43+
@settings(max_examples=5)
4444
def test_outcome_repeats_stochastic(self, strategies, turns, seed):
4545
"""a test to check that if a seed is set stochastic strategies give the
4646
same result"""

axelrod/tests/integration/test_tournament.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import filecmp
22
import unittest
33

4+
from hypothesis import given, settings
5+
46
import axelrod
57
from axelrod.strategy_transformers import FinalTransformer
8+
from axelrod.tests.property import tournaments
69

710

811
class TestTournament(unittest.TestCase):
@@ -29,12 +32,19 @@ def setUpClass(cls):
2932
]
3033
cls.expected_outcome.sort()
3134

32-
def test_full_tournament(self):
33-
"""A test to check that tournament runs with all non cheating strategies."""
34-
strategies = [strategy() for strategy in axelrod.strategies]
35-
tournament = axelrod.Tournament(
36-
name="test", players=strategies, game=self.game, turns=2, repetitions=1
37-
)
35+
@given(tournaments(
36+
strategies=axelrod.short_run_time_strategies,
37+
min_size=10,
38+
max_size=30,
39+
min_turns=2,
40+
max_turns=210,
41+
min_repetitions=1,
42+
max_repetitions=4,
43+
))
44+
@settings(max_examples=1)
45+
def test_big_tournaments(self, tournament):
46+
"""A test to check that tournament runs with a sample of non-cheating
47+
strategies."""
3848
filename = "test_outputs/test_tournament.csv"
3949
self.assertIsNone(
4050
tournament.play(progress_bar=False, filename=filename, build_results=False)

axelrod/tests/strategies/test_ann.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import axelrod
55
from axelrod.strategies.ann import split_weights
66

7-
from .test_player import TestMatch, TestPlayer
7+
from .test_player import TestPlayer
88

99
C, D = axelrod.Action.C, axelrod.Action.D
1010

axelrod/tests/strategies/test_meta.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Tests for the various Meta strategies."""
2-
import axelrod
2+
from hypothesis import given, settings
3+
from hypothesis.strategies import integers
34

5+
import axelrod
46
from .test_player import TestPlayer
57

68
C, D = axelrod.Action.C, axelrod.Action.D
@@ -59,6 +61,34 @@ def test_repr(self):
5961
),
6062
)
6163

64+
@given(seed=integers(min_value=1, max_value=20000000))
65+
@settings(max_examples=1)
66+
def test_clone(self, seed):
67+
# Test that the cloned player produces identical play
68+
player1 = self.player()
69+
player2 = player1.clone()
70+
self.assertEqual(len(player2.history), 0)
71+
self.assertEqual(player2.cooperations, 0)
72+
self.assertEqual(player2.defections, 0)
73+
self.assertEqual(player2.state_distribution, {})
74+
self.assertEqual(player2.classifier, player1.classifier)
75+
self.assertEqual(player2.match_attributes, player1.match_attributes)
76+
77+
turns = 10
78+
for op in [
79+
axelrod.Cooperator(),
80+
axelrod.Defector(),
81+
axelrod.TitForTat(),
82+
]:
83+
player1.reset()
84+
player2.reset()
85+
for p in [player1, player2]:
86+
axelrod.seed(seed)
87+
m = axelrod.Match((p, op), turns=turns)
88+
m.play()
89+
self.assertEqual(len(player1.history), turns)
90+
self.assertEqual(player1.history, player2.history)
91+
6292

6393
class TestMetaMajority(TestMetaPlayer):
6494

@@ -339,7 +369,7 @@ class TestMetaMajorityFiniteMemory(TestMetaPlayer):
339369
}
340370

341371
def test_strategy(self):
342-
actions = [(C, C), (C, D), (D, C), (C, D), (C, C)]
372+
actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
343373
self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions)
344374

345375

@@ -692,7 +722,7 @@ def test_strategy(self):
692722
)
693723

694724
opponent = axelrod.Defector()
695-
actions = [(C, D)] * 7 + [((D, D))]
725+
actions = [(C, D)] * 7 + [(D, D)]
696726
self.versus_test(
697727
opponent,
698728
expected_actions=actions,

axelrod/tests/strategies/test_player.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ def test_reset_clone(self):
444444
clone = player.clone()
445445
self.assertEqual(player, clone)
446446

447-
def test_clone(self):
447+
@given(seed=integers(min_value=1, max_value=20000000))
448+
@settings(max_examples=1)
449+
def test_clone(self, seed):
448450
# Test that the cloned player produces identical play
449451
player1 = self.player()
450452
if player1.name in ["Darwin", "Human"]:
@@ -468,7 +470,6 @@ def test_clone(self):
468470
]:
469471
player1.reset()
470472
player2.reset()
471-
seed = random.randint(0, 10 ** 6)
472473
for p in [player1, player2]:
473474
axelrod.seed(seed)
474475
m = axelrod.Match((p, op), turns=turns)
@@ -481,7 +482,7 @@ def test_clone(self):
481482
seed=integers(min_value=1, max_value=200),
482483
turns=integers(min_value=1, max_value=200),
483484
)
484-
@settings(max_examples=1, max_iterations=1)
485+
@settings(max_examples=1)
485486
def test_memory_depth_upper_bound(self, strategies, seed, turns):
486487
"""
487488
Test that the memory depth is indeed an upper bound.
@@ -490,6 +491,7 @@ def test_memory_depth_upper_bound(self, strategies, seed, turns):
490491
memory = player.classifier["memory_depth"]
491492
if memory < float("inf"):
492493
for strategy in strategies:
494+
player.reset()
493495
opponent = strategy()
494496
self.assertTrue(
495497
test_memory(
@@ -508,7 +510,6 @@ def versus_test(
508510
expected_actions,
509511
noise=None,
510512
seed=None,
511-
turns=10,
512513
match_attributes=None,
513514
attrs=None,
514515
init_kwargs=None,

axelrod/tests/strategies/test_shortmem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
C, D = axelrod.Action.C, axelrod.Action.D
88

99

10-
class TestCooperator(TestPlayer):
10+
class TestShortMem(TestPlayer):
1111

1212
name = "ShortMem"
1313
player = axelrod.ShortMem
1414
expected_classifier = {
15-
"memory_depth": 10,
15+
"memory_depth": float('inf'),
1616
"stochastic": False,
1717
"makes_use_of": set(),
1818
"inspects_source": False,

axelrod/tests/unit/test_filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_equality_filter(self):
3939
larger=integers(min_value=11, max_value=100),
4040
)
4141
@example(smaller=0, larger=float("inf"))
42-
@settings(max_examples=5, max_iterations=20)
42+
@settings(max_examples=5)
4343
def test_inequality_filter(self, smaller, larger):
4444
self.assertTrue(
4545
passes_operator_filter(
@@ -81,7 +81,7 @@ def test_list_filter(self):
8181
larger=integers(min_value=11, max_value=100),
8282
)
8383
@example(smaller=0, larger=float("inf"))
84-
@settings(max_examples=5, max_iterations=20)
84+
@settings(max_examples=5)
8585
def test_passes_filterset(self, smaller, larger):
8686

8787
full_passing_filterset_1 = {

axelrod/tests/unit/test_fingerprint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def test_majority_fingerprint(self):
373373
self.assertAlmostEqual(value, test_data[key], places=2)
374374

375375
@given(strategy_pair=strategy_lists(min_size=2, max_size=2))
376-
@settings(max_examples=5, max_iterations=20)
376+
@settings(max_examples=5)
377377
def test_pair_fingerprints(self, strategy_pair):
378378
"""
379379
A test to check that we can fingerprint

0 commit comments

Comments
 (0)