Skip to content

Commit eb612f5

Browse files
committed
Add eq for very strange edge case.
Check equality of players when players have attributes that point at each other. Also included some more tests for basic attribute equality.
1 parent c5e95d6 commit eb612f5

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

axelrod/player.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,12 @@ def __eq__(self, other):
164164
if not (all(next(generator) == next(other_generator)
165165
for _ in range(200))):
166166
return False
167+
168+
# Code for a strange edge case where each strategy points at each
169+
# other
170+
elif (value is other and other_value is self):
171+
pass
172+
167173
else:
168174
if value != other_value:
169175
return False

axelrod/tests/strategies/test_player.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,20 @@ def test_equality(self):
172172
p2.test = "29"
173173
self.assertNotEqual(p1, p2)
174174

175+
p1.test = "29"
176+
self.assertEqual(p1, p2)
177+
178+
# Check that attributes of both players are tested.
179+
p1.another_attribute = [1, 2, 3]
180+
self.assertNotEqual(p1, p2)
181+
p2.another_attribute = [1, 2, 3]
182+
self.assertEqual(p1, p2)
183+
184+
p2.another_attribute_2 = {1: 2}
185+
self.assertNotEqual(p1, p2)
186+
p1.another_attribute_2 = {1: 2}
187+
self.assertEqual(p1, p2)
188+
175189
def test_equality_for_numpy_array(self):
176190
# Check numpy array attribute (a special case)
177191
p1 = axelrod.Cooperator()
@@ -246,6 +260,40 @@ def test_equaity_on_init(self):
246260
self.assertEqual(p1, p2)
247261
self.assertEqual(p1, p2)
248262

263+
def test_equaity_on_with_player_attributes(self):
264+
"""Test for a strange edge case where players have pointers to each
265+
other"""
266+
p1 = axelrod.Cooperator()
267+
p2 = axelrod.Cooperator()
268+
269+
# If pointing at each other
270+
p1.player = p2
271+
p2.player = p1
272+
self.assertEqual(p1, p2)
273+
274+
# Still checking other attributes.
275+
p1.test_attribute = "29"
276+
self.assertNotEqual(p1, p2)
277+
278+
# If pointing at same strategy instances
279+
p1.player = axelrod.Cooperator()
280+
p2.player = axelrod.Cooperator()
281+
p2.test_attribute = "29"
282+
self.assertEqual(p1, p2)
283+
284+
285+
# If pointing at different strategy instances
286+
p1.player = axelrod.Cooperator()
287+
p2.player = axelrod.Defector()
288+
self.assertNotEqual(p1, p2)
289+
290+
# If different strategies pointing at same strategy instances
291+
p3 = axelrod.Defector()
292+
p1.player = axelrod.Cooperator()
293+
p3.player = axelrod.Cooperator()
294+
self.assertNotEqual(p1, p3)
295+
296+
249297
def test_init_params(self):
250298
"""Tests player correct parameters signature detection."""
251299
self.assertEqual(self.player.init_params(), {})

0 commit comments

Comments
 (0)