Skip to content

Commit ef0879c

Browse files
committed
Rework basic tests and update a number of explicit tests
1 parent 0fa8fe3 commit ef0879c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1042
-1387
lines changed

axelrod/strategies/apavlov.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class APavlov2006(Player):
1414
uncooperative opponents.
1515
"""
1616

17-
name = "Adapative Pavlov 2006"
17+
name = "Adaptive Pavlov 2006"
1818
classifier = {
1919
'memory_depth': float('inf'),
2020
'stochastic': False,
@@ -81,7 +81,7 @@ class APavlov2011(Player):
8181
uncooperative opponents.
8282
"""
8383

84-
name = "Adapative Pavlov 2011"
84+
name = "Adaptive Pavlov 2011"
8585
classifier = {
8686
'memory_depth': float('inf'),
8787
'stochastic': False,

axelrod/strategies/doubler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Doubler(Player):
1010
1111
Names:
1212
13-
- doubler: [PRISON1998]_
13+
- Doubler: [PRISON1998]_
1414
"""
1515

1616
name = 'Doubler'

axelrod/strategies/grumpy.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def __init__(self, starting_state='Nice', grumpy_threshold=10,
3434
nice
3535
"""
3636
super().__init__()
37-
self.history = []
3837
self.state = starting_state
3938
self.starting_state = starting_state
4039
self.grumpy_threshold = grumpy_threshold
@@ -44,25 +43,27 @@ def strategy(self, opponent):
4443
"""A player that gets grumpier the more the opposition defects,
4544
and nicer the more they cooperate.
4645
47-
Starts off Nice, but becomes grumpy once the grumpiness threshold is hit.
48-
Won't become nice once that grumpy threshold is hit, but must reach a much lower threshold before it becomes nice again.
46+
Starts off Nice, but becomes grumpy once the grumpiness threshold is
47+
hit. Won't become nice once that grumpy threshold is hit, but must
48+
reach a much lower threshold before it becomes nice again.
4949
"""
5050

51-
self.grumpiness = opponent.defections - opponent.cooperations
51+
grumpiness = opponent.defections - opponent.cooperations
5252

5353
if self.state == 'Nice':
54-
if self.grumpiness > self.grumpy_threshold:
54+
if grumpiness > self.grumpy_threshold:
5555
self.state = 'Grumpy'
5656
return D
5757
return C
5858

5959
if self.state == 'Grumpy':
60-
if self.grumpiness < self.nice_threshold:
60+
if grumpiness < self.nice_threshold:
6161
self.state = 'Nice'
6262
return C
6363
return D
6464

6565
def reset(self):
66-
"""Resets score, history and state for the next round of the tournement."""
66+
"""Resets score, history and state for the next round of the
67+
tournament."""
6768
super().reset()
6869
self.state = self.starting_state

axelrod/strategies/negation.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import random
2-
from axelrod import Actions, Player, random_choice, flip_action, init_args
3-
from axelrod.strategy_transformers import TrackHistoryTransformer
1+
from axelrod import Actions, Player, random_choice, flip_action
42

53
C, D = Actions.C, Actions.D
64

5+
76
class Negation(Player):
87
"""
98
A player starts by cooperating or defecting randomly if it's their first move,
@@ -28,8 +27,7 @@ class Negation(Player):
2827
def strategy(self, opponent):
2928
# Random first move
3029
if not self.history:
31-
return random_choice();
32-
30+
return random_choice()
3331
# Act opposite of opponent otherwise
3432
return flip_action(opponent.history[-1])
35-
33+

axelrod/tests/unit/test_actions.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
import unittest
22
from axelrod import Actions, flip_action
33

4+
5+
C, D = Actions.C, Actions.D
6+
7+
48
class TestAction(unittest.TestCase):
59

610
def test_action_values(self):
7-
self.assertEqual(Actions.C, 'C')
8-
self.assertEqual(Actions.D, 'D')
9-
self.assertNotEqual(Actions.D, 'd')
10-
self.assertNotEqual(Actions.C, 'c')
11-
self.assertNotEqual(Actions.C, Actions.D)
12-
11+
self.assertEqual(C, 'C')
12+
self.assertEqual(D, 'D')
13+
self.assertNotEqual(D, 'd')
14+
self.assertNotEqual(C, 'c')
15+
self.assertNotEqual(C, D)
1316

1417
def test_flip_action(self):
15-
self.assertEqual(flip_action(Actions.D), Actions.C)
16-
self.assertEqual(flip_action(Actions.C), Actions.D)
18+
self.assertEqual(flip_action(D), C)
19+
self.assertEqual(flip_action(C), D)
1720

1821
with self.assertRaises(ValueError):
19-
flip_action('R')
22+
flip_action('R')

axelrod/tests/unit/test_adaptive.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
"""Test for the Adaptive strategy."""
1+
"""Tests for the Adaptive strategy."""
22

33
import axelrod
4-
54
from .test_player import TestHeadsUp, TestPlayer
65

76
C, D = axelrod.Actions.C, axelrod.Actions.D
@@ -23,7 +22,7 @@ class TestAdaptive(TestPlayer):
2322

2423
def test_strategy(self):
2524
# Test initial play sequence
26-
self.responses_test([], [], [C] * 6 + [D] * 5)
25+
self.responses_test([C] * 6 + [D] * 5, [], [])
2726

2827
def test_scoring(self):
2928
player = axelrod.Adaptive()

axelrod/tests/unit/test_alternator.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
"""Test for the alternator strategy."""
1+
"""Tests for the Alternator strategy."""
22

33
import axelrod
4-
54
from .test_player import TestPlayer
65

76
C, D = axelrod.Actions.C, axelrod.Actions.D
@@ -22,13 +21,11 @@ class TestAlternator(TestPlayer):
2221
}
2322

2423
def test_strategy(self):
25-
"""Starts by cooperating."""
24+
# Starts by cooperating.
2625
self.first_play_test(C)
27-
28-
def test_effect_of_strategy(self):
29-
"""Simply does the opposite to what the strategy did last time."""
30-
self.markov_test([D, D, C, C])
26+
# Simply does the opposite to what the strategy did last time.
27+
self.second_play_test(D, D, C, C)
3128
for i in range(10):
32-
self.responses_test([], [], [C, D] * i)
33-
self.responses_test([C, D, D, D], [C, C, C, C], [C])
34-
self.responses_test([C, C, D, D, C], [C, D, C, C, C], [D])
29+
self.responses_test([C, D] * i)
30+
self.responses_test([C], [C, D, D, D], [C, C, C, C])
31+
self.responses_test([D], [C, C, D, D, C], [C, D, C, C, C])

axelrod/tests/unit/test_ann.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
"""Test for the Adaptive strategy."""
1+
"""Tests for the Adaptive strategy."""
22
import unittest
33

44
import axelrod
5-
from .test_player import TestHeadsUp, TestPlayer
65
from axelrod.strategies.ann import split_weights
6+
from .test_player import TestHeadsUp, TestPlayer
7+
78

89
C, D = axelrod.Actions.C, axelrod.Actions.D
910

@@ -74,7 +75,6 @@ def test_strategy(self):
7475
self.first_play_test(C)
7576

7677

77-
7878
class TestEvolvedANNvsCooperator(TestHeadsUp):
7979
def test_rounds(self):
8080
self.versus_test(axelrod.EvolvedANN(), axelrod.Cooperator(),

axelrod/tests/unit/test_apavlov.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
class TestAPavlov2006(TestPlayer):
10-
name = "Adapative Pavlov 2006"
10+
name = "Adaptive Pavlov 2006"
1111
player = axelrod.APavlov2006
1212

1313
expected_classifier = {
@@ -22,19 +22,19 @@ class TestAPavlov2006(TestPlayer):
2222

2323
def test_strategy(self):
2424
self.first_play_test(C)
25-
self.responses_test([C] * 6, [C] * 6, [C],
25+
self.responses_test([C], [C] * 6, [C] * 6,
2626
attrs={"opponent_class": "Cooperative"})
27-
self.responses_test([C, D, D, D, D, D], [D] * 6, [D],
27+
self.responses_test([D], [C, D, D, D, D, D], [D] * 6,
2828
attrs={"opponent_class": "ALLD"})
29-
self.responses_test([C, D, C, D, C, D], [D, C, D, C, D, C], [C, C],
29+
self.responses_test([C, C], [C, D, C, D, C, D], [D, C, D, C, D, C],
3030
attrs={"opponent_class": "STFT"})
31-
self.responses_test([C, D, D, C, D, D], [D, D, C, D, D, C], [D],
31+
self.responses_test([D], [C, D, D, C, D, D], [D, D, C, D, D, C],
3232
attrs={"opponent_class": "PavlovD"})
33-
self.responses_test([C, D, D, C, D, D, C], [D, D, C, D, D, C, D], [C],
33+
self.responses_test([C], [C, D, D, C, D, D, C], [D, D, C, D, D, C, D],
3434
attrs={"opponent_class": "PavlovD"})
35-
self.responses_test([C, D, D, C, D, D], [C, C, C, D, D, D], [D],
35+
self.responses_test([D], [C, D, D, C, D, D], [C, C, C, D, D, D],
3636
attrs={"opponent_class": "Random"})
37-
self.responses_test([C, D, D, D, C, C], [D, D, D, C, C, C], [D],
37+
self.responses_test([D], [C, D, D, D, C, C], [D, D, D, C, C, C],
3838
attrs={"opponent_class": "Random"})
3939

4040
def test_reset(self):
@@ -44,8 +44,9 @@ def test_reset(self):
4444
player.reset()
4545
self.assertEqual(player.opponent_class, None)
4646

47+
4748
class TestAPavlov2011(TestPlayer):
48-
name = "Adapative Pavlov 2011"
49+
name = "Adaptive Pavlov 2011"
4950
player = axelrod.APavlov2011
5051

5152
expected_classifier = {
@@ -60,24 +61,24 @@ class TestAPavlov2011(TestPlayer):
6061

6162
def test_strategy(self):
6263
self.first_play_test(C)
63-
self.responses_test([C] * 6, [C] * 6, [C],
64+
self.responses_test([C], [C] * 6, [C] * 6,
6465
attrs={"opponent_class": "Cooperative"})
65-
self.responses_test([C, D, D, D, D, D], [D] * 6, [D],
66+
self.responses_test([D], [C, D, D, D, D, D], [D] * 6,
6667
attrs={"opponent_class": "ALLD"})
67-
self.responses_test([C, C, D, D, D, D], [C] + [D] * 5, [D],
68+
self.responses_test([D], [C, C, D, D, D, D], [C] + [D] * 5,
6869
attrs={"opponent_class": "ALLD"})
69-
self.responses_test([C, C, C, D, D, D], [C, C] + [D] * 4, [D],
70+
self.responses_test([D], [C, C, C, D, D, D], [C, C] + [D] * 4,
7071
attrs={"opponent_class": "ALLD"})
71-
self.responses_test([C, C, D, D, C, D], [C, D, D, C, D, D], [D],
72+
self.responses_test([D], [C, C, D, D, C, D], [C, D, D, C, D, D],
7273
attrs={"opponent_class": "ALLD"})
7374

74-
self.responses_test([C, C, D, D, D, C], [C, D, D, C, C, D], [C],
75+
self.responses_test([C], [C, C, D, D, D, C], [C, D, D, C, C, D],
7576
attrs={"opponent_class": "STFT"})
76-
self.responses_test([C, C, D, C, D, C], [C, D, C, D, C, D], [C],
77+
self.responses_test([C], [C, C, D, C, D, C], [C, D, C, D, C, D],
7778
attrs={"opponent_class": "STFT"})
78-
self.responses_test([C, D, D, D, C, C], [D, D, D, C, C, C], [C],
79+
self.responses_test([C], [C, D, D, D, C, C], [D, D, D, C, C, C],
7980
attrs={"opponent_class": "STFT"})
80-
self.responses_test([C, D, D, D, C, C], [D, D, D, C, C, C], [C],
81+
self.responses_test([C], [C, D, D, D, C, C], [D, D, D, C, C, C],
8182
attrs={"opponent_class": "STFT"})
8283

8384
# Specific case for STFT when responding with TFT
@@ -90,9 +91,9 @@ def test_strategy(self):
9091
opponent.history.append(C)
9192
self.assertEqual(player.strategy(opponent), C)
9293

93-
self.responses_test([C, C, C, C, C, D], [C, C, C, C, D, D], [D],
94+
self.responses_test([D], [C, C, C, C, C, D], [C, C, C, C, D, D],
9495
attrs={"opponent_class": "Random"})
95-
self.responses_test([C, D, D, C, C, C], [D, D, C, C, C, C], [D],
96+
self.responses_test([D], [C, D, D, C, C, C], [D, D, C, C, C, C],
9697
attrs={"opponent_class": "Random"})
9798

9899
def test_reset(self):

axelrod/tests/unit/test_appeaser.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
"""Test for the appeaser strategy."""
1+
"""Tests for the Appeaser strategy."""
22

33
import axelrod
4-
54
from .test_player import TestPlayer
65

76
C, D = axelrod.Actions.C, axelrod.Actions.D
@@ -22,16 +21,10 @@ class TestAppeaser(TestPlayer):
2221
}
2322

2423
def test_strategy(self):
25-
"""Starts by cooperating."""
24+
# Starts by cooperating.
2625
self.first_play_test(C)
27-
28-
def test_effect_of_strategy(self):
29-
P1 = axelrod.Appeaser()
30-
P2 = axelrod.Cooperator()
31-
self.assertEqual(P1.strategy(P2), C)
32-
33-
self.responses_test([C], [C], [C, C, C])
34-
self.responses_test([C, D, C, D], [C, C, D], [D])
35-
self.responses_test([C, D, C, D, C], [C, C, D, D], [C])
36-
self.responses_test([C, D, C, D, C, D], [C, C, D, D, D], [D])
26+
self.responses_test([C, C, C], [C], [C])
27+
self.responses_test([D], [C, D, C, D], [C, C, D])
28+
self.responses_test([C], [C, D, C, D, C], [C, C, D, D])
29+
self.responses_test([D], [C, D, C, D, C, D], [C, C, D, D, D])
3730

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
"""Test for the average_copier strategy."""
1+
"""Tests for the AverageCopier strategies."""
22

33
import axelrod
4-
54
from .test_player import TestPlayer
65

76
C, D = axelrod.Actions.C, axelrod.Actions.D
@@ -22,19 +21,13 @@ class TestAverageCopier(TestPlayer):
2221
}
2322

2423
def test_strategy(self):
25-
"""Test that the first strategy is picked randomly."""
26-
self.responses_test([], [], [C], random_seed=1)
27-
self.responses_test([], [], [D], random_seed=2)
28-
29-
def test_when_oppenent_all_Cs(self):
30-
"""Tests that if opponent has played all C then player chooses C."""
31-
self.responses_test([C, C, C, C], [C, C, C, C], [C, C, C],
32-
random_seed=5)
33-
34-
def test_when_opponent_all_Ds(self):
35-
"""Tests that if opponent has played all D then player chooses D."""
36-
self.responses_test([C, C, C, C], [D, D, D, D], [D, D, D],
37-
random_seed=5)
24+
# Test that the first strategy is picked randomly.
25+
self.responses_test([C], seed=1)
26+
self.responses_test([D], seed=2)
27+
# Tests that if opponent has played all C then player chooses C.
28+
self.responses_test([C, C, C], [C, C, C, C], [C, C, C, C], seed=5)
29+
# Tests that if opponent has played all D then player chooses D.
30+
self.responses_test([D, D, D], [C, C, C, C], [D, D, D, D], seed=5)
3831

3932

4033
class TestNiceAverageCopier(TestPlayer):
@@ -52,19 +45,9 @@ class TestNiceAverageCopier(TestPlayer):
5245
}
5346

5447
def test_strategy(self):
55-
"""Test that the first strategy is cooperation."""
48+
# Cooperates initially.
5649
self.first_play_test(C)
57-
58-
def test_when_oppenent_all_Cs(self):
59-
"""
60-
Tests that if opponent has played all C then player chooses C
61-
"""
62-
self.responses_test([C, C, C, C], [C, C, C, C], [C, C, C],
63-
random_seed=5)
64-
65-
def test_when_opponent_all_Ds(self):
66-
"""
67-
Tests that if opponent has played all D then player chooses D
68-
"""
69-
self.responses_test([D, D, D, D], [D, D, D, D], [D, D, D],
70-
random_seed=5)
50+
# If opponent has played all C then player chooses C.
51+
self.responses_test([C, C, C], [C, C, C, C], [C, C, C, C], seed=5)
52+
# If opponent has played all D then player chooses D.
53+
self.responses_test([D, D, D], [D, D, D, D], [D, D, D, D], seed=5)

0 commit comments

Comments
 (0)