|
9 | 9 |
|
10 | 10 |
|
11 | 11 | class TestPickle(unittest.TestCase):
|
| 12 | + |
| 13 | + def assert_original_acts_same_as_pickled(self, player, turns=100): |
| 14 | + copy = pickle.loads(pickle.dumps(player)) |
| 15 | + opponent_1 = axl.CyclerCCCDCD() |
| 16 | + opponent_2 = axl.CyclerCCCDCD() |
| 17 | + axl.seed(0) |
| 18 | + match_1 = axl.Match((player, opponent_1), turns=turns) |
| 19 | + result_1 = match_1.play() |
| 20 | + |
| 21 | + axl.seed(0) |
| 22 | + match_2 = axl.Match((copy, opponent_2), turns=turns) |
| 23 | + result_2 = match_2.play() |
| 24 | + |
| 25 | + self.assertEqual(result_1, result_2) |
| 26 | + |
12 | 27 | def test_parameterized_player(self):
|
13 | 28 | p1 = axl.Cooperator()
|
14 | 29 | p2 = axl.Cycler('DDCCDD')
|
@@ -37,21 +52,11 @@ def test_final_transformer_called(self):
|
37 | 52 | results = match.play()
|
38 | 53 | self.assertEqual(results, [(C, C), (C, C), (D, D)])
|
39 | 54 |
|
| 55 | + self.assert_original_acts_same_as_pickled(axl.Alexei(), turns=10) |
| 56 | + |
40 | 57 | def test_nice_transformer_called(self):
|
41 | 58 | player = axl.NMWEDeterministic()
|
42 |
| - copy = pickle.loads(pickle.dumps(player)) |
43 |
| - opponent_1 = axl.CyclerCCCDCD() |
44 |
| - opponent_2 = axl.CyclerCCCDCD() |
45 |
| - axl.seed(0) |
46 |
| - match_1 = axl.Match((player, opponent_1), turns=100) |
47 |
| - result_1 = match_1.play() |
48 |
| - |
49 |
| - axl.seed(0) |
50 |
| - match_2 = axl.Match((copy, opponent_2), turns=100) |
51 |
| - result_2 = match_2.play() |
52 |
| - |
53 |
| - |
54 |
| - self.assertEqual(result_1, result_2) |
| 59 | + self.assert_original_acts_same_as_pickled(player, turns=10) |
55 | 60 |
|
56 | 61 | def test_all(self):
|
57 | 62 | for s in axl.strategies:
|
|
0 commit comments