Skip to content

Commit fcf3293

Browse files
committed
Merge pull request #454 from Axelrod-Python/198
198 - Implements some hypothesis testing
2 parents 0e4c4d1 + 21298cb commit fcf3293

File tree

6 files changed

+73
-9
lines changed

6 files changed

+73
-9
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.hypothesis/examples.db merge=hypothesisdb

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ _build/
66
dist/
77
MANIFEST
88
Axelrod.egg-info
9+
.hypothesis/eval_source/

.hypothesis/examples.db

7 KB
Binary file not shown.

axelrod/tests/unit/test_game.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import unittest
2+
from hypothesis import given
3+
from hypothesis.strategies import integers, tuples
24

35
import axelrod
46

@@ -25,3 +27,26 @@ def test_score(self):
2527
self.assertEqual(self.game.score((D, D)), (1, 1))
2628
self.assertEqual(self.game.score((C, D)), (0, 5))
2729
self.assertEqual(self.game.score((D, C)), (5, 0))
30+
31+
@given(r=integers(), p=integers(), s=integers(), t=integers())
32+
def test_property_init(self, r, p, s, t):
33+
"""Use the hypothesis library to test init"""
34+
expected_scores = {(C, D): (s, t), (D, C): (t, s),
35+
(D, D): (p, p), (C, C): (r, r)}
36+
game = axelrod.Game(r, s, t, p)
37+
self.assertEqual(game.scores, expected_scores)
38+
39+
@given(r=integers(), p=integers(), s=integers(), t=integers())
40+
def test_property_RPST(self, r, p, s, t):
41+
"""Use the hypothesis library to test RPST"""
42+
game = axelrod.Game(r, s, t, p)
43+
self.assertEqual(game.RPST(), (r, p, s, t))
44+
45+
@given(r=integers(), p=integers(), s=integers(), t=integers())
46+
def test_property_score(self, r, p, s, t):
47+
"""Use the hypothesis library to test score"""
48+
game = axelrod.Game(r, s, t, p)
49+
self.assertEqual(game.score((C, C)), (r, r))
50+
self.assertEqual(game.score((D, D)), (p, p))
51+
self.assertEqual(game.score((C, D)), (s, t))
52+
self.assertEqual(game.score((D, C)), (t, s))

axelrod/tests/unit/test_tournament.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
"""Tests for the main tournament class."""
22

3-
import unittest
43
import axelrod
54
import logging
65
import multiprocessing
6+
import unittest
7+
import random
8+
9+
from hypothesis import given, example
10+
from hypothesis.strategies import (integers, lists,
11+
sampled_from, random_module,
12+
Settings)
713

814
try:
915
# Python 3
@@ -12,21 +18,24 @@
1218
# Python 2
1319
from mock import MagicMock
1420

21+
test_strategies = [axelrod.Cooperator,
22+
axelrod.TitForTat,
23+
axelrod.Defector,
24+
axelrod.Grudger,
25+
axelrod.GoByMajority]
26+
test_repetitions = 5
27+
test_turns = 100
28+
1529

1630
class TestTournament(unittest.TestCase):
1731

1832
@classmethod
1933
def setUpClass(cls):
2034
cls.game = axelrod.Game()
21-
cls.players = [
22-
axelrod.Cooperator(),
23-
axelrod.TitForTat(),
24-
axelrod.Defector(),
25-
axelrod.Grudger(),
26-
axelrod.GoByMajority()]
35+
cls.players = [s() for s in test_strategies]
2736
cls.test_name = 'test'
28-
cls.test_repetitions = 5
29-
cls.test_turns = 100
37+
cls.test_repetitions = test_repetitions
38+
cls.test_turns = test_turns
3039

3140
cls.expected_payoff = [
3241
[600, 600, 0, 600, 600],
@@ -99,6 +108,33 @@ def test_serial_play(self):
99108
{'cooperation': [], 'payoff': []})
100109
self.assertFalse(tournament._run_parallel_repetitions.called)
101110

111+
@given(s=lists(sampled_from(axelrod.strategies),
112+
min_size=2, # Errors are returned if less than 2 strategies
113+
max_size=5, unique=True),
114+
turns=integers(min_value=2, max_value=50),
115+
repetitions=integers(min_value=2, max_value=4),
116+
settings=Settings(max_examples=50,
117+
timeout=0),
118+
rm=random_module())
119+
@example(s=test_strategies, turns=test_turns, repetitions=test_repetitions,
120+
rm=random.seed(0))
121+
def test_property_serial_play(self, s, turns, repetitions, rm):
122+
"""Test serial play using hypothesis"""
123+
# Test that we get an instance of ResultSet
124+
players = [strat() for strat in s]
125+
126+
tournament = axelrod.Tournament(
127+
name=self.test_name,
128+
players=players,
129+
game=self.game,
130+
turns=turns,
131+
repetitions=repetitions)
132+
results = tournament.play()
133+
self.assertIsInstance(results, axelrod.ResultSet)
134+
self.assertEqual(len(results.cooperation), len(players))
135+
self.assertEqual(results.nplayers, len(players))
136+
self.assertEqual(results.players, players)
137+
102138
def test_parallel_play(self):
103139
# Test that we get an instance of ResultSet
104140
tournament = axelrod.Tournament(

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
numpy>=1.9.2
22
matplotlib>=1.4.2
33
cloudpickle==0.1.1
4+
hypothesis==1.18

0 commit comments

Comments
 (0)