Skip to content

Commit 31ed308

Browse files
author
gaffney2010
committed
Updated tests for new caching approach.
1 parent 9a40888 commit 31ed308

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

axelrod/tests/unit/test_deterministic_cache.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
class TestDeterministicCache(unittest.TestCase):
1111
@classmethod
1212
def setUpClass(cls):
13-
cls.test_key = (TitForTat(), Defector(), 3)
13+
cls.test_key = (TitForTat(), Defector())
1414
cls.test_value = [(C, D), (D, D), (D, D)]
1515
cls.test_save_file = "test_cache_save.txt"
1616
cls.test_load_file = "test_cache_load.txt"
17-
test_data_to_pickle = {("Tit For Tat", "Defector", 3): [(C, D), (D, D), (D, D)]}
17+
test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]}
1818
cls.test_pickle = pickle.dumps(test_data_to_pickle)
1919

2020
with open(cls.test_load_file, "wb") as f:
@@ -44,40 +44,30 @@ def test_setitem_invalid_key_not_tuple(self):
4444
with self.assertRaises(ValueError):
4545
self.cache[invalid_key] = self.test_value
4646

47-
def test_setitem_invalid_key_too_short(self):
48-
invalid_key = self.test_key + (4,)
49-
with self.assertRaises(ValueError):
50-
self.cache[invalid_key] = self.test_value
51-
52-
def test_setitem_invalid_key_too_long(self):
53-
invalid_key = self.test_key[:2]
54-
with self.assertRaises(ValueError):
55-
self.cache[invalid_key] = self.test_value
56-
5747
def test_setitem_invalid_key_first_two_elements_not_player(self):
58-
invalid_key = ("test", "test", 2)
48+
invalid_key = ("test", "test")
5949
with self.assertRaises(ValueError):
6050
self.cache[invalid_key] = self.test_value
6151

62-
invalid_key = (TitForTat(), "test", 2)
52+
invalid_key = (TitForTat(), "test")
6353
with self.assertRaises(ValueError):
6454
self.cache[invalid_key] = self.test_value
6555

66-
invalid_key = ("test", TitForTat(), 2)
56+
invalid_key = ("test", TitForTat())
6757
with self.assertRaises(ValueError):
6858
self.cache[invalid_key] = self.test_value
6959

70-
def test_setitem_invalid_key_last_element_not_integer(self):
60+
def test_setitem_invalid_key_too_many_players(self):
7161
invalid_key = (TitForTat(), TitForTat(), TitForTat())
7262
with self.assertRaises(ValueError):
7363
self.cache[invalid_key] = self.test_value
7464

7565
def test_setitem_invalid_key_stochastic_player(self):
76-
invalid_key = (Random(), TitForTat(), 2)
66+
invalid_key = (Random(), TitForTat())
7767
with self.assertRaises(ValueError):
7868
self.cache[invalid_key] = self.test_value
7969

80-
invalid_key = (TitForTat(), Random(), 2)
70+
invalid_key = (TitForTat(), Random())
8171
with self.assertRaises(ValueError):
8272
self.cache[invalid_key] = self.test_value
8373

axelrod/tests/unit/test_match.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,8 @@ def test_example_prob_end(self):
8989
self.assertEqual(len(match.play()), expected_length)
9090
self.assertEqual(match.noise, 0)
9191
self.assertEqual(match.game.RPST(), (3, 1, 0, 5))
92-
self.assertEqual(len(match._cache), 3)
93-
94-
for expected_length in expected_lengths:
95-
self.assertEqual(
96-
match._cache[(p1, p2, expected_length)], [(C, C)] * expected_length
97-
)
92+
self.assertEqual(len(match._cache), 1)
93+
self.assertEqual(match._cache[(p1, p2)], [(C, C)] * 5)
9894

9995
@given(turns=integers(min_value=1, max_value=200), game=games())
10096
@example(turns=5, game=axelrod.DefaultGame)
@@ -167,14 +163,29 @@ def test_play(self):
167163
expected_result = [(C, D), (C, D), (C, D)]
168164
self.assertEqual(match.play(), expected_result)
169165
self.assertEqual(
170-
cache[(axelrod.Cooperator(), axelrod.Defector(), 3)], expected_result
166+
cache[(axelrod.Cooperator(), axelrod.Defector())], expected_result
171167
)
172168

173169
# a deliberately incorrect result so we can tell it came from the cache
174-
expected_result = [(C, C), (D, D), (D, C)]
175-
cache[(axelrod.Cooperator(), axelrod.Defector(), 3)] = expected_result
170+
expected_result = [(C, C), (D, D), (D, C), (C, C), (C, D)]
171+
cache[(axelrod.Cooperator(), axelrod.Defector())] = expected_result
176172
match = axelrod.Match(players, 3, deterministic_cache=cache)
177-
self.assertEqual(match.play(), expected_result)
173+
self.assertEqual(match.play(), expected_result[:3])
174+
175+
def test_long_cache(self):
176+
cache = DeterministicCache()
177+
players = (axelrod.Cooperator(), axelrod.Defector())
178+
match = axelrod.Match(players, 5, deterministic_cache=cache)
179+
expected_result_5_turn = [(C, D), (C, D), (C, D), (C, D), (C, D)]
180+
expected_result_3_turn = [(C, D), (C, D), (C, D)]
181+
self.assertEqual(match.play(), expected_result_5_turn)
182+
match.turns = 3
183+
self.assertEqual(match.play(), expected_result_3_turn)
184+
# The cache should still hold the 5.
185+
self.assertEqual(
186+
cache[(axelrod.Cooperator(), axelrod.Defector())],
187+
expected_result_5_turn
188+
)
178189

179190
def test_scores(self):
180191
player1 = axelrod.TitForTat()

0 commit comments

Comments
 (0)