Ejemplo n.º 1
0
    def testPrevNodeIdempotency(self, unused_update_cache):
        """Tests that the TreeNode is actually caching a PrevNode call."""
        sibling_a = add_node.AddNodeOp()(
            self.n, {
                'path': [(tree.TreeNode.LEFT, 'sibling-a')],
                'data': 'some-data',
            })
        sibling_b = add_node.AddNodeOp()(
            self.n, {
                'path': [(tree.TreeNode.LEFT, 'sibling-b')],
                'data': 'some-data',
            })

        prev = prev_node.PrevNodeOp()(sibling_b)
        self.assertEqual(prev, sibling_a)
        self.assertEqual(prev, prev_node.PrevNodeOp()(sibling_b))
Ejemplo n.º 2
0
    def testPrevNodeBegin(self, unused_update_cache):
        """Tests that getting prev node from start of a tree raises an error."""
        child = add_node.AddNodeOp()(self.n, {
            'path': [(tree.TreeNode.LEFT, 'child-id')],
            'data': 'some-data',
        })

        with self.assertRaises(StopIteration):
            prev_node.PrevNodeOp()(child)
Ejemplo n.º 3
0
    def testPrevNodeParent(self, unused_update_cache):
        """Tests in-order tree traversal when parent is prev node."""
        child = add_node.AddNodeOp()(
            self.n, {
                'path': [(tree.TreeNode.RIGHT, 'child-id')],
                'data': 'some-data',
            })

        prev = prev_node.PrevNodeOp()(child)
        self.assertEqual(prev, self.n)
Ejemplo n.º 4
0
    def testPrevNodeMaxSubtree(self, unused_update_cache):
        """Tests in-order tree traversal with a right subtree."""
        child = add_node.AddNodeOp()(self.n, {
            'path': [(tree.TreeNode.LEFT, 'child-id')],
            'data': 'some-data',
        })
        grandchild = add_node.AddNodeOp()(
            child, {
                'path': [(tree.TreeNode.RIGHT, 'grandchild-id')],
                'data': 'some-data',
            })

        prev = prev_node.PrevNodeOp()(self.n)
        self.assertEqual(prev, grandchild)
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def testPrevNodeSibling(self, unused_update_cache):
        """Tests getting a sibling node is implemented."""
        sibling_a = add_node.AddNodeOp()(
            self.n, {
                'path': [(tree.TreeNode.LEFT, 'sibling-a')],
                'data': 'some-data',
            })
        sibling_b = add_node.AddNodeOp()(
            self.n, {
                'path': [(tree.TreeNode.LEFT, 'sibling-b')],
                'data': 'some-data',
            })

        prev = prev_node.PrevNodeOp()(sibling_b)
        self.assertEqual(prev, sibling_a)
        self.assertEqual(sibling_b.metadata['prev'], sibling_a)

        self.assertNotIn('prev', sibling_a.metadata)