@@ -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,54 @@ 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_cache_grows (self ):
176+ """
177+ We want to make sure that if we try to use the cache for more turns than
178+ what is stored, then it will instead regenerate the result and overwrite
179+ the cache.
180+ """
181+ cache = DeterministicCache ()
182+ players = (axelrod .Cooperator (), axelrod .Defector ())
183+ match = axelrod .Match (players , 3 , deterministic_cache = cache )
184+ expected_result_5_turn = [(C , D ), (C , D ), (C , D ), (C , D ), (C , D )]
185+ expected_result_3_turn = [(C , D ), (C , D ), (C , D )]
186+ self .assertEqual (match .play (), expected_result_3_turn )
187+ match .turns = 5
188+ self .assertEqual (match .play (), expected_result_5_turn )
189+ # The cache should now hold the 5-turn result..
190+ self .assertEqual (
191+ cache [(axelrod .Cooperator (), axelrod .Defector ())],
192+ expected_result_5_turn
193+ )
194+
195+ def test_cache_doesnt_shrink (self ):
196+ """
197+ We want to make sure that when we access the cache looking for fewer
198+ turns than what is stored, then it will not overwrite the cache with the
199+ shorter result.
200+ """
201+ cache = DeterministicCache ()
202+ players = (axelrod .Cooperator (), axelrod .Defector ())
203+ match = axelrod .Match (players , 5 , deterministic_cache = cache )
204+ expected_result_5_turn = [(C , D ), (C , D ), (C , D ), (C , D ), (C , D )]
205+ expected_result_3_turn = [(C , D ), (C , D ), (C , D )]
206+ self .assertEqual (match .play (), expected_result_5_turn )
207+ match .turns = 3
208+ self .assertEqual (match .play (), expected_result_3_turn )
209+ # The cache should still hold the 5.
210+ self .assertEqual (
211+ cache [(axelrod .Cooperator (), axelrod .Defector ())],
212+ expected_result_5_turn
213+ )
178214
179215 def test_scores (self ):
180216 player1 = axelrod .TitForTat ()
0 commit comments