Skip to content

Commit c7b50fb

Browse files
authored
Merge pull request #1028 from Axelrod-Python/884-meta
Refactor tests for meta
2 parents 9edd676 + 7a828a7 commit c7b50fb

File tree

1 file changed

+66
-49
lines changed

1 file changed

+66
-49
lines changed

axelrod/tests/strategies/test_meta.py

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
"""Tests for the various Meta strategies."""
2-
3-
import random
4-
52
import axelrod
6-
from .test_player import TestPlayer, test_responses
3+
from .test_player import TestPlayer
74

85
C, D = axelrod.Actions.C, axelrod.Actions.D
96

@@ -180,13 +177,16 @@ class TestNiceMetaWinnerEnsemble(TestMetaPlayer):
180177
def test_strategy(self):
181178
self.first_play_test(C)
182179

183-
P1 = axelrod.NiceMetaWinner(team=[axelrod.Cooperator, axelrod.Defector])
184-
P2 = axelrod.Cooperator()
185-
test_responses(self, P1, P2, [C] * 4, [C] * 4, [C] * 4)
186-
187-
P1 = axelrod.NiceMetaWinner(team=[axelrod.Cooperator, axelrod.Defector])
188-
P2 = axelrod.Defector()
189-
test_responses(self, P1, P2, [D] * 4, [C] * 4, [D] * 4)
180+
actions = [(C, C)] * 8
181+
self.versus_test(opponent=axelrod.Cooperator(),
182+
expected_actions=actions,
183+
init_kwargs={"team": [axelrod.Cooperator,
184+
axelrod.Defector]})
185+
actions = [(C, D)] + [(D, D)] * 7
186+
self.versus_test(opponent=axelrod.Defector(),
187+
expected_actions=actions,
188+
init_kwargs={"team": [axelrod.Cooperator,
189+
axelrod.Defector]})
190190

191191

192192
class TestMetaHunter(TestMetaPlayer):
@@ -208,19 +208,30 @@ def test_strategy(self):
208208

209209
# We are not using the Cooperator Hunter here, so this should lead to
210210
# cooperation.
211-
self.responses_test([C], [C, C, C, C], [C, C, C, C])
211+
actions = [(C, C)] * 5
212+
self.versus_test(opponent=axelrod.Cooperator(),
213+
expected_actions=actions)
212214

213215
# After long histories tit-for-tat should come into play.
214-
self.responses_test([D], [C] * 101, [C] * 100 + [D])
216+
opponent = axelrod.MockPlayer([C] * 100 + [D])
217+
actions = [(C, C)] * 100 + [(C, D)] + [(D, C)]
218+
self.versus_test(opponent=opponent, expected_actions=actions)
219+
220+
actions = [(C, C)] * 102
221+
self.versus_test(opponent=axelrod.Cooperator(),
222+
expected_actions=actions)
215223

216224
# All these others, however, should trigger a defection for the hunter.
217-
self.responses_test([D], [C] * 4, [D] * 4)
218-
self.responses_test([D], [C] * 6, [C, D] * 3)
219-
self.responses_test([D], [C] * 8, [C, C, C, D, C, C, C, D])
220-
# Test post 100 rounds responses
221-
self.responses_test([C], [C] * 101, [C] * 101)
222-
self.responses_test([D], [C] * 101, [C] * 100 + [D])
225+
actions = [(C, D), (C, D), (C, D), (C, D), (D, D)]
226+
self.versus_test(opponent=axelrod.Defector(), expected_actions=actions)
227+
228+
actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (D, C)]
229+
self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions)
223230

231+
actions = [(C, C), (C, C), (C, C), (C, D),
232+
(C, C), (C, C), (C, C), (C, D), (D, C)]
233+
self.versus_test(opponent=axelrod.CyclerCCCD(),
234+
expected_actions=actions)
224235

225236
class TestMetaHunterAggressive(TestMetaPlayer):
226237

@@ -241,15 +252,21 @@ def test_strategy(self):
241252

242253
# We are using CooperatorHunter here, so this should lead to
243254
# defection
244-
self.responses_test([D], [C, C, C, C], [C, C, C, C])
255+
actions = [(C, C)] * 4 + [(D, C)]
256+
self.versus_test(opponent=axelrod.Cooperator(),
257+
expected_actions=actions)
245258

246259
# All these others, however, should trigger a defection for the hunter.
247-
self.responses_test([D], [C] * 4, [D] * 4)
248-
self.responses_test([D], [C] * 6, [C, D] * 3)
249-
self.responses_test([D], [C] * 8, [C, C, C, D, C, C, C, D])
250-
# Test post 100 rounds responses
251-
self.responses_test([D], [C] * 101, [C] * 101)
252-
self.responses_test([D], [C] * 101, [C] * 100 + [D])
260+
actions = [(C, D), (C, D), (C, D), (C, D), (D, D)]
261+
self.versus_test(opponent=axelrod.Defector(), expected_actions=actions)
262+
263+
actions = [(C, C), (C, D), (C, C), (C, D), (C, C), (C, D), (D, C)]
264+
self.versus_test(opponent=axelrod.Alternator(), expected_actions=actions)
265+
266+
actions = [(C, C), (C, C), (C, C), (C, D),
267+
(C, C), (C, C), (C, C), (C, D), (D, C)]
268+
self.versus_test(opponent=axelrod.CyclerCCCD(),
269+
expected_actions=actions)
253270

254271
# To test the TFT action of the strategy after 100 turns, we need to
255272
# remove two of the hunters from its team.
@@ -260,13 +277,13 @@ def test_strategy(self):
260277
axelrod.DefectorHunter,
261278
axelrod.AlternatorHunter,
262279
axelrod.RandomHunter,
263-
# axelrod.MathConstantHunter,
264280
axelrod.CycleHunter,
265-
axelrod.EventualCycleHunter,
266-
# axelrod.CooperatorHunter
281+
axelrod.EventualCycleHunter
267282
]
268-
self.responses_test(
269-
[D], [C] * 101, [C] * 100 + [D], init_kwargs={'team': team})
283+
opponent = axelrod.MockPlayer([C] * 100 + [D])
284+
actions = [(C, C)] * 100 + [(C, D), (D, C)]
285+
self.versus_test(opponent=opponent, expected_actions=actions,
286+
init_kwargs={'team': team})
270287

271288

272289
class TestMetaMajorityMemoryOne(TestMetaPlayer):
@@ -426,36 +443,36 @@ def test_strategy(self):
426443

427444
P1 = axelrod.MetaMixer(team=team, distribution=distribution)
428445
P2 = axelrod.Cooperator()
446+
actions = [(C, C)] * 20
447+
self.versus_test(opponent=axelrod.Cooperator(),
448+
expected_actions=actions,
449+
init_kwargs={"team": team,
450+
"distribution": distribution})
451+
429452

430-
for k in range(100):
431-
P1.play(P2)
432-
self.assertEqual(P1.history[-1], C)
433453

434454
team.append(axelrod.Defector)
435455
distribution = [.2, .5, .3, 0] # If add a defector but does not occur
436-
437-
P1 = axelrod.MetaMixer(team=team, distribution=distribution)
438-
439-
for k in range(100):
440-
P1.play(P2)
441-
self.assertEqual(P1.history[-1], C)
456+
self.versus_test(opponent=axelrod.Cooperator(),
457+
expected_actions=actions,
458+
init_kwargs={"team": team,
459+
"distribution": distribution})
442460

443461
distribution = [0, 0, 0, 1] # If defector is only one that is played
444-
445-
P1 = axelrod.MetaMixer(team=team, distribution=distribution)
446-
447-
for k in range(100):
448-
P1.play(P2)
449-
self.assertEqual(P1.history[-1], D)
462+
actions = [(D, C)] * 20
463+
self.versus_test(opponent=axelrod.Cooperator(),
464+
expected_actions=actions,
465+
init_kwargs={"team": team,
466+
"distribution": distribution})
450467

451468
def test_raise_error_in_distribution(self):
452469
team = [axelrod.TitForTat, axelrod.Cooperator, axelrod.Grudger]
453470
distribution = [.2, .5, .5] # Not a valid probability distribution
454471

455-
P1 = axelrod.MetaMixer(team=team, distribution=distribution)
456-
P2 = axelrod.Cooperator()
472+
player = axelrod.MetaMixer(team=team, distribution=distribution)
473+
opponent = axelrod.Cooperator()
457474

458-
self.assertRaises(ValueError, P1.strategy, P2)
475+
self.assertRaises(ValueError, player.strategy, opponent)
459476

460477

461478
class TestNMWEDeterministic(TestMetaPlayer):

0 commit comments

Comments
 (0)