Skip to content

Commit 8581380

Browse files
committed
Refactor tests for retaliate.
Also adding specific test for some of the parametrized versions. Contributes towards #884
1 parent 4702455 commit 8581380

File tree

1 file changed

+88
-38
lines changed

1 file changed

+88
-38
lines changed

axelrod/tests/strategies/test_retaliate.py

Lines changed: 88 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,59 @@ def test_strategy(self):
2424
# Starts by cooperating.
2525
self.first_play_test(C)
2626
# If opponent has defected more than 10 percent of the time, defect.
27-
P1 = axelrod.Retaliate()
28-
P2 = axelrod.Player()
29-
self.responses_test([C], [C] * 4, [C] * 4)
30-
self.responses_test([D], [C, C, C, C, D], [C, C, C, D, C])
31-
self.responses_test([D], [C] * 6, [C] * 5 + [D])
27+
opponent = axelrod.Cooperator()
28+
actions = [(C, C)] * 5
29+
self.versus_test(opponent=opponent, expected_actions=actions)
30+
31+
opponent = axelrod.MockPlayer([C, C, C, D, C])
32+
actions = [(C, C), (C, C), (C, C), (C, D), (D, C), (D, C)]
33+
self.versus_test(opponent=opponent, expected_actions=actions)
34+
35+
36+
class TestRetaliate2(TestPlayer):
37+
38+
name = "Retaliate 2: 0.08"
39+
player = axelrod.Retaliate2
40+
expected_classifier = {
41+
'memory_depth': float('inf'), # Long memory
42+
'stochastic': False,
43+
'makes_use_of': set(),
44+
'long_run_time': False,
45+
'inspects_source': False,
46+
'manipulates_source': False,
47+
'manipulates_state': False
48+
}
49+
50+
def test_strategy(self):
51+
# Starts by cooperating.
52+
self.first_play_test(C)
53+
# If opponent has defected more than 8 percent of the time, defect.
54+
opponent = axelrod.MockPlayer([C] * 13 + [D])
55+
actions = [(C, C)] * 13 + [(C, D), (D, C)]
56+
self.versus_test(opponent=opponent, expected_actions=actions)
57+
58+
59+
class TestRetaliate3(TestPlayer):
60+
61+
name = "Retaliate 3: 0.05"
62+
player = axelrod.Retaliate3
63+
expected_classifier = {
64+
'memory_depth': float('inf'), # Long memory
65+
'stochastic': False,
66+
'makes_use_of': set(),
67+
'long_run_time': False,
68+
'inspects_source': False,
69+
'manipulates_source': False,
70+
'manipulates_state': False
71+
}
72+
73+
def test_strategy(self):
74+
# Starts by cooperating.
75+
self.first_play_test(C)
76+
# If opponent has defected more than 5 percent of the time, defect.
77+
opponent = axelrod.MockPlayer([C] * 19 + [D])
78+
actions = [(C, C)] * 19 + [(C, D), (D, C)]
79+
self.versus_test(opponent=opponent, expected_actions=actions)
3280

3381

3482
class TestLimitedRetaliate(TestPlayer):
@@ -50,38 +98,40 @@ def test_strategy(self):
5098
self.first_play_test(C)
5199

52100
# If opponent has never defected, co-operate
53-
self.responses_test([C], [C] * 4, [C] * 4, attrs={"retaliating": False})
101+
opponent = axelrod.Cooperator()
102+
actions = [(C, C)] * 5
103+
self.versus_test(opponent=opponent, expected_actions=actions,
104+
attrs={"retaliating": False})
54105

55106
# Retaliate after a (C, D) round.
56-
self.responses_test([D], [C, C, C, C, D], [C, C, C, D, C],
57-
attrs={"retaliating": True})
58-
self.responses_test([D], [C, C, C, C, C, C], [C, C, C, C, C, D],
59-
attrs={"retaliating": True})
60-
61-
# Case were retaliation count is less than limit: cooperate, reset
62-
# retaliation count and be not retaliating
63-
P1 = self.player()
64-
P2 = TestOpponent()
65-
P1.history = [C, C, C, D, C]
66-
P2.history = [D, D, D, C, D]
67-
P1.retaliation_count = 1
68-
P1.retaliation_limit = 0
69-
self.assertEqual(P1.strategy(P2), C)
70-
self.assertEqual(P1.retaliation_count, 0)
71-
self.assertFalse(P1.retaliating)
72-
73-
# If I've hit the limit for retaliation attempts, co-operate
74-
P1.history = [C, C, C, C, D]
75-
P2.history = [C, C, C, D, C]
76-
P1.retaliation_count = 20
77-
self.assertEqual(P1.strategy(P2), C)
78-
self.assertFalse(P1.retaliating)
79-
80-
def test_reset(self):
81-
P1 = axelrod.LimitedRetaliate()
82-
P1.history = [C, C, C, C, D]
83-
P1.retaliating = True
84-
P1.retaliation_count = 4
85-
P1.reset()
86-
self.assertFalse(P1.retaliating)
87-
self.assertEqual(P1.retaliation_count, 0)
107+
opponent = axelrod.MockPlayer([C, C, C, D, C])
108+
actions = [(C, C), (C, C), (C, C), (C, D), (D, C), (D, C)]
109+
self.versus_test(opponent=opponent, expected_actions=actions,
110+
attrs={"retaliating": True})
111+
112+
opponent = axelrod.Alternator()
113+
114+
# Count retaliations
115+
actions = [(C, C), (C, D), (D, C), (D, D), (D, C)]
116+
self.versus_test(opponent=opponent, expected_actions=actions,
117+
attrs={"retaliation_count": 3})
118+
opponent = axelrod.Alternator()
119+
120+
# Cooperate if we hit the retaliation limit
121+
actions = [(C, C), (C, D), (D, C), (D, D), (C, C)]
122+
self.versus_test(opponent=opponent, expected_actions=actions,
123+
attrs={"retaliation_count": 0},
124+
init_kwargs={"retaliation_limit": 2})
125+
126+
# Defect again after cooperating
127+
actions = [(C, C), (C, D), (D, C), (D, D), (C, C), (D, D), (D, C)]
128+
self.versus_test(opponent=opponent, expected_actions=actions,
129+
attrs={"retaliation_count": 2},
130+
init_kwargs={"retaliation_limit": 2})
131+
132+
# Different behaviour with different retaliation threshold
133+
actions = [(C, C), (C, D), (D, C), (C, D), (C, C), (C, D), (C, C)]
134+
self.versus_test(opponent=opponent, expected_actions=actions,
135+
attrs={"retaliation_count": 0},
136+
init_kwargs={"retaliation_limit": 2,
137+
"retaliation_threshold": 9})

0 commit comments

Comments
 (0)