Skip to content

Commit 721fc91

Browse files
committed
Add new tests and update some existing tests
1 parent 26eee39 commit 721fc91

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

src/axelrod_fortran/player.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self, original_name):
4545
self.index, self.shared_library_filename = \
4646
shared_library_manager.get_filename_for_player(original_name)
4747
self.shared_library = load_library(self.shared_library_filename)
48-
self.original_name = original_name
48+
self.__original_name = original_name
4949
self.original_function = self.original_name
5050
is_stochastic = characteristics[self.original_name]['stochastic']
5151
if is_stochastic is not None:

src/axelrod_fortran/shared_library_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def create_library_copy(self):
5959
# Copy the library file to a new (temp) location.
6060
temp_directory = tempfile.gettempdir()
6161
copy_number = len(self.filenames)
62-
filename = "{}-{}-{}".format(
62+
filename = "{}-{}-{}".format(
6363
self.prefix,
6464
str(copy_number),
6565
self.shared_library_name)

src/axelrod_fortran/strategies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@
348348
'original_rank': None},
349349
}
350350

351-
all_strategies = characteristics.keys()
351+
all_strategies = list(characteristics.keys())
352352

353353
# Players from Axelrod's second tournament.
354354
second_tournament_strategies = [

tests/test_player.py

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from axelrod_fortran import Player, characteristics, all_strategies
77
from axelrod import (Alternator, Cooperator, Defector, Match, MoranProcess,
8-
Game, basic_strategies, seed)
8+
Game, RandomGenerator, Tournament, basic_strategies)
99
from axelrod.action import Action
1010

1111

@@ -22,7 +22,7 @@ def test_init():
2222
POINTER(c_int), POINTER(c_int), POINTER(c_int), POINTER(c_int),
2323
POINTER(c_float))
2424
assert player.original_function.restype == c_int
25-
with pytest.raises(ValueError):
25+
with pytest.raises(AttributeError):
2626
player = Player('test')
2727

2828

@@ -122,16 +122,22 @@ def test_implemented_strategies():
122122
"""
123123
for strategy, dictionary in characteristics.items():
124124
axelrod_class = dictionary["axelrod-python_class"]
125-
player = Player(strategy)
126-
if (axelrod_class is not None and
127-
player.classifier["stochastic"] is False):
128-
axl_player = axelrod_class()
125+
stochastic = Player(strategy).classifier["stochastic"]
126+
if axelrod_class is not None and not stochastic:
129127
for opponent_strategy in basic_strategies:
128+
player = Player(strategy)
130129
opponent = opponent_strategy()
131130
match = Match((player, opponent))
132131
interactions = match.play()
132+
133+
axl_player = axelrod_class()
134+
opponent = opponent_strategy()
133135
axl_match = Match((axl_player, opponent))
134-
assert interactions == axl_match.play(), (player, opponent)
136+
axl_interactions = axl_match.play()
137+
print(player, axl_player, opponent)
138+
print(interactions)
139+
print(axl_interactions)
140+
assert interactions == axl_interactions
135141

136142

137143
def test_champion_v_alternator():
@@ -140,15 +146,12 @@ def test_champion_v_alternator():
140146
"""
141147
player = Player("k61r")
142148
opponent = Alternator()
143-
144149
match = Match((player, opponent))
145150

146-
seed(0)
147-
interactions = match.play()
151+
seed = 0
152+
interactions = match.play(seed=seed)
148153
assert interactions[25:30] == [(C, D), (C, C), (C, D), (D, C), (C, D)]
149-
150-
seed(0)
151-
assert interactions == match.play()
154+
assert interactions == match.play(seed=seed)
152155

153156

154157
def test_warning_for_self_interaction(recwarn):
@@ -157,9 +160,7 @@ def test_warning_for_self_interaction(recwarn):
157160
"""
158161
player = Player("k42r")
159162
opponent = player
160-
161163
match = Match((player, opponent))
162-
163164
interactions = match.play()
164165
assert len(recwarn) == 1
165166

@@ -168,13 +169,10 @@ def test_no_warning_for_normal_interaction(recwarn):
168169
"""
169170
Test that a warning is not given for a normal interaction
170171
"""
171-
player = Player("k42r")
172-
opponent = Alternator()
173172
for players in [(Player("k42r"), Alternator()),
174173
(Player("k42r"), Player("k41r"))]:
175174

176175
match = Match(players)
177-
178176
interactions = match.play()
179177
assert len(recwarn) == 0
180178

@@ -185,3 +183,44 @@ def test_multiple_copies(recwarn):
185183
mp = MoranProcess(players)
186184
mp.play()
187185
mp.populations_plot()
186+
187+
188+
def test_match_reproducibility():
189+
for _ in range(10):
190+
rng = RandomGenerator()
191+
seed = rng.random_seed_int()
192+
strategies = rng.choice(all_strategies, size=2)
193+
print(strategies)
194+
players1 = [Player(strategy) for strategy in strategies]
195+
# players1 = (p() for p in strategies)
196+
match1 = Match(players1, turns=200, noise=0.1, seed=seed)
197+
results1 = match1.play()
198+
players2 = [Player(strategy) for strategy in strategies]
199+
# players2 = (p() for p in strategies)
200+
match2 = Match(players2, turns=200, noise=0.1, seed=seed)
201+
results2 = match2.play()
202+
assert (results1 == results2)
203+
204+
205+
def test_tournament_reproducibility():
206+
rng = RandomGenerator()
207+
seed = rng.random_seed_int()
208+
strategies = rng.choice(all_strategies, size=2)
209+
players1 = [Player(strategy) for strategy in strategies]
210+
tournament1 = Tournament(players1, seed=seed, repetitions=2)
211+
results1 = tournament1.play(processes=2)
212+
213+
players2 = [Player(strategy) for strategy in strategies]
214+
tournament2 = Tournament(players2, seed=seed, repetitions=2)
215+
results2 = tournament2.play(processes=2)
216+
217+
assert (results1.ranked_names == results2.ranked_names)
218+
219+
220+
if __name__ == "__main__":
221+
test_init()
222+
test_matches()
223+
test_noisy_matches()
224+
test_implemented_strategies()
225+
test_match_reproducibility()
226+
test_tournament_reproducibility()

0 commit comments

Comments
 (0)