Skip to content

Commit ac00851

Browse files
authored
Merge pull request #640 from Axelrod-Python/604
Removing the random module for hypothesis
2 parents e934a98 + fad9582 commit ac00851

File tree

7 files changed

+90
-131
lines changed

7 files changed

+90
-131
lines changed

axelrod/tests/property.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
A module for creating hypothesis based strategies for property based testing
33
"""
44
import axelrod
5-
from hypothesis.strategies import composite, tuples, sampled_from, integers, floats, random_module, lists
5+
from hypothesis.strategies import (composite, sampled_from, integers,
6+
floats, lists)
67

78

89
@composite
@@ -45,15 +46,15 @@ def matches(draw, strategies=axelrod.strategies,
4546
4647
Returns
4748
-------
48-
tuple : a random match as well as a random seed
49+
match : a random match
4950
"""
50-
seed = draw(random_module())
5151
strategies = draw(strategy_lists(min_size=2, max_size=2))
5252
players = [s() for s in strategies]
5353
turns = draw(integers(min_value=min_turns, max_value=max_turns))
5454
noise = draw(floats(min_value=min_noise, max_value=max_noise))
5555
match = axelrod.Match(players, turns=turns, noise=noise)
56-
return match, seed
56+
return match
57+
5758

5859
@composite
5960
def tournaments(draw, strategies=axelrod.strategies,
@@ -85,7 +86,6 @@ def tournaments(draw, strategies=axelrod.strategies,
8586
max_repetitions : integer
8687
The maximum number of repetitions
8788
"""
88-
seed = draw(random_module())
8989
strategies = draw(strategy_lists(strategies=strategies,
9090
min_size=min_size,
9191
max_size=max_size))
@@ -97,7 +97,7 @@ def tournaments(draw, strategies=axelrod.strategies,
9797

9898
tournament = axelrod.Tournament(players, turns=turns,
9999
repetitions=repetitions, noise=noise)
100-
return tournament, seed
100+
return tournament
101101

102102

103103
@composite
@@ -130,7 +130,6 @@ def prob_end_tournaments(draw, strategies=axelrod.strategies,
130130
max_repetitions : integer
131131
The maximum number of repetitions
132132
"""
133-
seed = draw(random_module())
134133
strategies = draw(strategy_lists(strategies=strategies,
135134
min_size=min_size,
136135
max_size=max_size))
@@ -142,7 +141,7 @@ def prob_end_tournaments(draw, strategies=axelrod.strategies,
142141

143142
tournament = axelrod.ProbEndTournament(players, prob_end=prob_end,
144143
repetitions=repetitions, noise=noise)
145-
return tournament, seed
144+
return tournament
146145

147146

148147
@composite

axelrod/tests/unit/test_match.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from axelrod.deterministic_cache import DeterministicCache
66

77
from hypothesis import given, example
8-
from hypothesis.strategies import integers, floats, random_module, assume
8+
from hypothesis.strategies import integers, floats, assume
99

1010
from axelrod.tests.property import games
1111

@@ -52,9 +52,8 @@ def test_len(self, turns):
5252
match = axelrod.Match((p1, p2), turns)
5353
self.assertEqual(len(match), turns)
5454

55-
@given(p=floats(min_value=0, max_value=1),
56-
rm=random_module())
57-
def test_stochastic(self, p, rm):
55+
@given(p=floats(min_value=0, max_value=1))
56+
def test_stochastic(self, p):
5857

5958
assume(0 < p < 1)
6059

@@ -69,9 +68,8 @@ def test_stochastic(self, p, rm):
6968
match = axelrod.Match((p1, p2), 5)
7069
self.assertTrue(match._stochastic)
7170

72-
@given(p=floats(min_value=0, max_value=1),
73-
rm=random_module())
74-
def test_cache_update_required(self, p, rm):
71+
@given(p=floats(min_value=0, max_value=1))
72+
def test_cache_update_required(self, p):
7573

7674
assume(0 < p < 1)
7775

axelrod/tests/unit/test_match_generator.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import unittest
33

44
from hypothesis import given, example
5-
from hypothesis.strategies import floats, random_module, integers
5+
from hypothesis.strategies import floats, integers
66

77
import axelrod
88

@@ -123,9 +123,9 @@ def setUpClass(cls):
123123
cls.players = [s() for s in test_strategies]
124124

125125
@given(repetitions=integers(min_value=1, max_value=test_repetitions),
126-
prob_end=floats(min_value=0, max_value=1), rm=random_module())
127-
@example(repetitions=test_repetitions, prob_end=.5, rm=random_module())
128-
def test_build_match_chunks(self, repetitions, prob_end, rm):
126+
prob_end=floats(min_value=0, max_value=1))
127+
@example(repetitions=test_repetitions, prob_end=.5)
128+
def test_build_match_chunks(self, repetitions, prob_end):
129129
rr = axelrod.ProbEndRoundRobinMatches(
130130
self.players, prob_end, test_game, repetitions)
131131
chunks = list(rr.build_match_chunks())
@@ -134,8 +134,8 @@ def test_build_match_chunks(self, repetitions, prob_end, rm):
134134

135135
self.assertEqual(sorted(match_definitions), sorted(expected_match_definitions))
136136

137-
@given(prob_end=floats(min_value=0.1, max_value=0.5), rm=random_module())
138-
def test_build_matches_different_length(self, prob_end, rm):
137+
@given(prob_end=floats(min_value=0.1, max_value=0.5))
138+
def test_build_matches_different_length(self, prob_end):
139139
"""
140140
If prob end is not 0 or 1 then the matches should all have different
141141
length
@@ -149,8 +149,8 @@ def test_build_matches_different_length(self, prob_end, rm):
149149
match_lengths = [match_params[0] for (index_pair, match_params, repetitions) in chunks]
150150
self.assertNotEqual(min(match_lengths), max(match_lengths))
151151

152-
@given(prob_end=floats(min_value=0, max_value=1), rm=random_module())
153-
def test_sample_length(self, prob_end, rm):
152+
@given(prob_end=floats(min_value=0, max_value=1))
153+
def test_sample_length(self, prob_end):
154154
rr = axelrod.ProbEndRoundRobinMatches(
155155
self.players, prob_end, test_game, test_repetitions)
156156
self.assertGreaterEqual(rr.sample_length(prob_end), 1)
@@ -159,8 +159,8 @@ def test_sample_length(self, prob_end, rm):
159159
except AssertionError:
160160
self.assertEqual(rr.sample_length(prob_end), float("inf"))
161161

162-
@given(prob_end=floats(min_value=.1, max_value=1), rm=random_module())
163-
def test_build_single_match_params(self, prob_end, rm):
162+
@given(prob_end=floats(min_value=.1, max_value=1))
163+
def test_build_single_match_params(self, prob_end):
164164
rr = axelrod.ProbEndRoundRobinMatches(
165165
self.players, prob_end, test_game, test_repetitions)
166166
match_params = rr.build_single_match_params()
@@ -200,8 +200,8 @@ def test_build_single_match_params(self, prob_end, rm):
200200
self.assertLess(len(match), float('inf'))
201201
self.assertGreater(len(match), 0)
202202

203-
@given(prob_end=floats(min_value=.1, max_value=1), rm=random_module())
204-
def test_len(self, prob_end, rm):
203+
@given(prob_end=floats(min_value=.1, max_value=1))
204+
def test_len(self, prob_end):
205205
repetitions = 10
206206
rr = axelrod.ProbEndRoundRobinMatches(
207207
self.players, prob_end, game=None, repetitions=repetitions)

axelrod/tests/unit/test_moran.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from axelrod.moran import fitness_proportionate_selection
88

99
from hypothesis import given, example, settings
10-
from hypothesis.strategies import random_module
1110

1211
from axelrod.tests.property import strategy_lists
1312

@@ -69,16 +68,13 @@ def test_four_players(self):
6968
self.assertEqual(populations, mp.populations)
7069
self.assertEqual(mp.winning_strategy_name, str(axelrod.Defector()))
7170

72-
@given(strategies=strategy_lists(min_size=2, max_size=5),
73-
rm=random_module())
71+
@given(strategies=strategy_lists(min_size=2, max_size=5))
7472
@settings(max_examples=5, timeout=0) # Very low number of examples
7573

7674
# Two specific examples relating to cloning of strategies
77-
@example(strategies=[axelrod.BackStabber, axelrod.MindReader],
78-
rm=random.seed(0))
79-
@example(strategies=[axelrod.ThueMorse, axelrod.MindReader],
80-
rm=random.seed(0))
81-
def test_property_players(self, strategies, rm):
75+
@example(strategies=[axelrod.BackStabber, axelrod.MindReader])
76+
@example(strategies=[axelrod.ThueMorse, axelrod.MindReader])
77+
def test_property_players(self, strategies):
8278
"""Hypothesis test that randomly checks players"""
8379
players = [s() for s in strategies]
8480
mp = MoranProcess(players)

axelrod/tests/unit/test_property.py

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
prob_end_tournaments, games)
77

88
from hypothesis import given, settings
9-
from hypothesis.strategies import random_module
109

1110
stochastic_strategies = [s for s in axelrod.strategies if
1211
s().classifier['stochastic']]
@@ -20,31 +19,28 @@ def test_call(self):
2019
for p in strategies:
2120
self.assertIsInstance(p(), axelrod.Player)
2221

23-
@given(strategies=strategy_lists(min_size=1, max_size=50),
24-
rm=random_module())
22+
@given(strategies=strategy_lists(min_size=1, max_size=50))
2523
@settings(max_examples=10, timeout=0)
26-
def test_decorator(self, strategies, rm):
24+
def test_decorator(self, strategies):
2725
self.assertIsInstance(strategies, list)
2826
self.assertGreaterEqual(len(strategies), 1)
2927
self.assertLessEqual(len(strategies), 50)
3028
for strategy in strategies:
3129
self.assertIsInstance(strategy(), axelrod.Player)
3230

33-
@given(strategies=strategy_lists(strategies=axelrod.basic_strategies),
34-
rm=random_module())
31+
@given(strategies=strategy_lists(strategies=axelrod.basic_strategies))
3532
@settings(max_examples=10, timeout=0)
36-
def test_decorator_with_given_strategies(self, strategies, rm):
33+
def test_decorator_with_given_strategies(self, strategies):
3734
self.assertIsInstance(strategies, list)
3835
basic_player_names = [str(s()) for s in axelrod.basic_strategies]
3936
for strategy in strategies:
4037
player = strategy()
4138
self.assertIsInstance(player, axelrod.Player)
4239
self.assertIn(str(player), basic_player_names)
4340

44-
@given(strategies=strategy_lists(strategies=stochastic_strategies),
45-
rm=random_module())
41+
@given(strategies=strategy_lists(strategies=stochastic_strategies))
4642
@settings(max_examples=10, timeout=0)
47-
def test_decorator_with_stochastic_strategies(self, strategies, rm):
43+
def test_decorator_with_stochastic_strategies(self, strategies):
4844
self.assertIsInstance(strategies, list)
4945
stochastic_player_names = [str(s()) for s in stochastic_strategies]
5046
for strategy in strategies:
@@ -59,29 +55,24 @@ class TestMatch(unittest.TestCase):
5955
"""
6056

6157
def test_call(self):
62-
match, seed = matches().example()
63-
self.assertTrue(str(seed).startswith('random.seed'))
58+
match = matches().example()
6459
self.assertIsInstance(match, axelrod.Match)
6560

66-
@given(match_and_seed=matches(min_turns=10, max_turns=50,
67-
min_noise=0, max_noise=1))
61+
@given(match=matches(min_turns=10, max_turns=50,
62+
min_noise=0, max_noise=1))
6863
@settings(max_examples=10, timeout=0)
69-
def test_decorator(self, match_and_seed):
70-
match, seed = match_and_seed
71-
self.assertTrue(str(seed).startswith('random.seed'))
64+
def test_decorator(self, match):
7265

7366
self.assertIsInstance(match, axelrod.Match)
7467
self.assertGreaterEqual(len(match), 10)
7568
self.assertLessEqual(len(match), 50)
7669
self.assertGreaterEqual(match.noise, 0)
7770
self.assertLessEqual(match.noise, 1)
7871

79-
@given(match_and_seed=matches(min_turns=10, max_turns=50,
80-
min_noise=0, max_noise=0))
72+
@given(match=matches(min_turns=10, max_turns=50,
73+
min_noise=0, max_noise=0))
8174
@settings(max_examples=10, timeout=0)
82-
def test_decorator_with_no_noise(self, match_and_seed):
83-
match, seed = match_and_seed
84-
self.assertTrue(str(seed).startswith('random.seed'))
75+
def test_decorator_with_no_noise(self, match):
8576

8677
self.assertIsInstance(match, axelrod.Match)
8778
self.assertGreaterEqual(len(match), 10)
@@ -92,19 +83,15 @@ def test_decorator_with_no_noise(self, match_and_seed):
9283
class TestTournament(unittest.TestCase):
9384

9485
def test_call(self):
95-
tournament, seed = tournaments().example()
96-
self.assertTrue(str(seed).startswith('random.seed'))
86+
tournament = tournaments().example()
9787
self.assertIsInstance(tournament, axelrod.Tournament)
9888

99-
@given(tournament_and_seed=tournaments(min_turns=2, max_turns=50, min_noise=0,
100-
max_noise=1, min_repetitions=2,
101-
max_repetitions=50,
102-
max_size=3))
89+
@given(tournament=tournaments(min_turns=2, max_turns=50, min_noise=0,
90+
max_noise=1, min_repetitions=2,
91+
max_repetitions=50,
92+
max_size=3))
10393
@settings(max_examples=10, timeout=0)
104-
def test_decorator(self, tournament_and_seed):
105-
tournament, seed = tournament_and_seed
106-
self.assertTrue(str(seed).startswith('random.seed'))
107-
94+
def test_decorator(self, tournament):
10895
self.assertIsInstance(tournament, axelrod.Tournament)
10996
self.assertLessEqual(tournament.turns, 50)
11097
self.assertGreaterEqual(tournament.turns, 2)
@@ -113,25 +100,19 @@ def test_decorator(self, tournament_and_seed):
113100
self.assertLessEqual(tournament.repetitions, 50)
114101
self.assertGreaterEqual(tournament.repetitions, 2)
115102

116-
@given(tournament_and_seed=tournaments(strategies=axelrod.basic_strategies,
117-
max_size=3))
103+
@given(tournament=tournaments(strategies=axelrod.basic_strategies,
104+
max_size=3))
118105
@settings(max_examples=10, timeout=0)
119-
def test_decorator_with_given_strategies(self, tournament_and_seed):
120-
tournament, seed = tournament_and_seed
121-
self.assertTrue(str(seed).startswith('random.seed'))
122-
106+
def test_decorator_with_given_strategies(self, tournament):
123107
self.assertIsInstance(tournament, axelrod.Tournament)
124108
basic_player_names = [str(s()) for s in axelrod.basic_strategies]
125109
for p in tournament.players:
126110
self.assertIn(str(p), basic_player_names)
127111

128-
@given(tournament_and_seed=tournaments(strategies=stochastic_strategies,
129-
max_size=3))
112+
@given(tournament=tournaments(strategies=stochastic_strategies,
113+
max_size=3))
130114
@settings(max_examples=10, timeout=0)
131-
def test_decorator_with_stochastic_strategies(self, tournament_and_seed):
132-
tournament, seed = tournament_and_seed
133-
self.assertTrue(str(seed).startswith('random.seed'))
134-
115+
def test_decorator_with_stochastic_strategies(self, tournament):
135116
self.assertIsInstance(tournament, axelrod.Tournament)
136117
stochastic_player_names = [str(s()) for s in stochastic_strategies]
137118
for p in tournament.players:
@@ -141,21 +122,17 @@ def test_decorator_with_stochastic_strategies(self, tournament_and_seed):
141122
class TestProbEndTournament(unittest.TestCase):
142123

143124
def test_call(self):
144-
tournament, seed = prob_end_tournaments().example()
145-
self.assertTrue(str(seed).startswith('random.seed'))
125+
tournament = tournaments().example()
146126
self.assertIsInstance(tournament, axelrod.Tournament)
147127

148-
@given(tournament_and_seed=prob_end_tournaments(min_prob_end=0,
149-
max_prob_end=1,
150-
min_noise=0, max_noise=1,
151-
min_repetitions=2,
152-
max_repetitions=50,
153-
max_size=3))
128+
@given(tournament=prob_end_tournaments(min_prob_end=0,
129+
max_prob_end=1,
130+
min_noise=0, max_noise=1,
131+
min_repetitions=2,
132+
max_repetitions=50,
133+
max_size=3))
154134
@settings(max_examples=10, timeout=0)
155-
def test_decorator(self, tournament_and_seed):
156-
tournament, seed = tournament_and_seed
157-
self.assertTrue(str(seed).startswith('random.seed'))
158-
135+
def test_decorator(self, tournament):
159136
self.assertIsInstance(tournament, axelrod.ProbEndTournament)
160137
self.assertLessEqual(tournament.prob_end, 1)
161138
self.assertGreaterEqual(tournament.prob_end, 0)
@@ -164,25 +141,19 @@ def test_decorator(self, tournament_and_seed):
164141
self.assertLessEqual(tournament.repetitions, 50)
165142
self.assertGreaterEqual(tournament.repetitions, 2)
166143

167-
@given(tournament_and_seed=prob_end_tournaments(
168-
strategies=axelrod.basic_strategies, max_size=3))
144+
@given(tournament=prob_end_tournaments(strategies=axelrod.basic_strategies,
145+
max_size=3))
169146
@settings(max_examples=10, timeout=0)
170-
def test_decorator_with_given_strategies(self, tournament_and_seed):
171-
tournament, seed = tournament_and_seed
172-
self.assertTrue(str(seed).startswith('random.seed'))
173-
147+
def test_decorator_with_given_strategies(self, tournament):
174148
self.assertIsInstance(tournament, axelrod.ProbEndTournament)
175149
basic_player_names = [str(s()) for s in axelrod.basic_strategies]
176150
for p in tournament.players:
177151
self.assertIn(str(p), basic_player_names)
178152

179-
@given(tournament_and_seed=prob_end_tournaments(
180-
strategies=stochastic_strategies, max_size=3))
153+
@given(tournament=prob_end_tournaments(strategies=stochastic_strategies,
154+
max_size=3))
181155
@settings(max_examples=10, timeout=0)
182-
def test_decorator_with_stochastic_strategies(self, tournament_and_seed):
183-
tournament, seed = tournament_and_seed
184-
self.assertTrue(str(seed).startswith('random.seed'))
185-
156+
def test_decorator_with_stochastic_strategies(self, tournament):
186157
self.assertIsInstance(tournament, axelrod.ProbEndTournament)
187158
stochastic_player_names = [str(s()) for s in stochastic_strategies]
188159
for p in tournament.players:

0 commit comments

Comments
 (0)