def testNextNodeIdempotency(self, unused_update_cache): """Tests that the TreeNode is actually caching a NextNode call.""" sibling_a = add_node.AddNodeOp()( self.n, { 'path': [(tree.TreeNode.RIGHT, 'sibling-a')], 'data': 'some-data', }) sibling_b = add_node.AddNodeOp()( self.n, { 'path': [(tree.TreeNode.RIGHT, 'sibling-b')], 'data': 'some-data', }) next = next_node.NextNodeOp()(sibling_a) self.assertEqual(next, sibling_b) self.assertEqual(next, next_node.NextNodeOp()(sibling_a))
def testNextNodeParent(self, unused_update_cache): """Tests in-order tree traversal when parent is next node.""" child = add_node.AddNodeOp()(self.n, { 'path': [(tree.TreeNode.LEFT, 'child-id')], 'data': 'some-data', }) next = next_node.NextNodeOp()(child) self.assertEqual(next, self.n)
def testNextNodeEnd(self, unused_update_cache): """Tests that getting next node from end of a tree raises an error.""" child = add_node.AddNodeOp()( self.n, { 'path': [(tree.TreeNode.RIGHT, 'child-id')], 'data': 'some-data', }) with self.assertRaises(StopIteration): next_node.NextNodeOp()(child)
def _update_cache(self, node): """Updates node neighbors' cache with newly inserted node.""" try: prev = prev_node.PrevNodeOp()(node) prev.metadata['next'] = node except StopIteration: pass try: next = next_node.NextNodeOp()(node) next.metadata['prev'] = node except StopIteration: pass
def testNextNodeMinSubtree(self, unused_update_cache): """Tests in-order tree traversal with left subtree.""" child = add_node.AddNodeOp()( self.n, { 'path': [(tree.TreeNode.RIGHT, 'child-id')], 'data': 'some-data', }) grandchild = add_node.AddNodeOp()( child, { 'path': [(tree.TreeNode.LEFT, 'grandchild-id')], 'data': 'some-data', }) next = next_node.NextNodeOp()(self.n) self.assertEqual(next, grandchild)
def testNextNodeSibling(self, unused_update_cache): """Tests getting a sibling node is implemented.""" sibling_a = add_node.AddNodeOp()( self.n, { 'path': [(tree.TreeNode.RIGHT, 'sibling-a')], 'data': 'some-data', }) sibling_b = add_node.AddNodeOp()( self.n, { 'path': [(tree.TreeNode.RIGHT, 'sibling-b')], 'data': 'some-data', }) next = next_node.NextNodeOp()(sibling_a) self.assertEqual(next, sibling_b) self.assertEqual(sibling_a.metadata['next'], sibling_b) self.assertNotIn('next', sibling_b.metadata)