|
| 1 | +import typing |
1 | 2 | import unittest |
2 | 3 |
|
3 | 4 | import pygambit |
4 | 5 |
|
5 | 6 | from . import games |
6 | 7 |
|
7 | 8 |
|
| 9 | +# an auxiliary function used in `copy_tree` tests |
| 10 | +def subtrees_equal( |
| 11 | + n1: pygambit.Node, |
| 12 | + n2: pygambit.Node, |
| 13 | + recursion_stop_node: typing.Union[pygambit.Node, None] = None |
| 14 | + ) -> bool: |
| 15 | + if n1 == recursion_stop_node: |
| 16 | + return n2.is_terminal |
| 17 | + if n1.is_terminal and n2.is_terminal: |
| 18 | + return n1.outcome == n2.outcome |
| 19 | + if n1.is_terminal is not n2.is_terminal: |
| 20 | + return False |
| 21 | + # now, both n1 and n2 are non-terminal |
| 22 | + # check that they are in the same infosets |
| 23 | + if n1.infoset != n2.infoset: |
| 24 | + return False |
| 25 | + # check that they have the same number of children |
| 26 | + if len(n1.children) != len(n2.children): |
| 27 | + return False |
| 28 | + |
| 29 | + return all( |
| 30 | + subtrees_equal(c1, c2, recursion_stop_node) for (c1, c2) in zip(n1.children, n2.children) |
| 31 | + ) |
| 32 | + |
| 33 | + |
8 | 34 | class TestGambitNode(unittest.TestCase): |
9 | 35 | def setUp(self): |
10 | 36 | self.game = pygambit.Game.new_tree() |
@@ -205,6 +231,26 @@ def test_node_copy_across_games(self): |
205 | 231 | self.assertRaises(pygambit.MismatchError, self.game.copy_tree, |
206 | 232 | self.extensive_game.root, self.game.root) |
207 | 233 |
|
| 234 | + def test_copy_tree_onto_nondescendant_terminal_node(self): |
| 235 | + |
| 236 | + g = games.read_from_file("e01.efg") |
| 237 | + src_node = g.nodes()[3] # path=[1, 0] |
| 238 | + dest_node = g.nodes()[2] # path=[0, 0] |
| 239 | + |
| 240 | + g.copy_tree(src_node, dest_node) |
| 241 | + |
| 242 | + assert subtrees_equal(src_node, dest_node) |
| 243 | + |
| 244 | + def test_copy_tree_onto_descendant_terminal_node(self): |
| 245 | + |
| 246 | + g = games.read_from_file("e01.efg") |
| 247 | + src_node = g.nodes()[1] # path=[0] |
| 248 | + dest_node = g.nodes()[4] # path=[0, 1, 0] |
| 249 | + |
| 250 | + g.copy_tree(src_node, dest_node) |
| 251 | + |
| 252 | + assert subtrees_equal(src_node, dest_node, dest_node) |
| 253 | + |
208 | 254 | def test_node_move_nonterminal(self): |
209 | 255 | """Test on moving to a nonterminal node.""" |
210 | 256 | self.assertRaises(pygambit.UndefinedOperationError, self.extensive_game.move_tree, |
|
0 commit comments