Exemplo n.º 1
0
    def test_get_all_from_empty(self):
        # создаем дерево
        parent_node = None
        tree = SimpleTree(parent_node)

        # получаем список значений узлов
        nodes = tree.GetAllNodes()

        # проверяем, что список пуст
        self.assertEqual(len(nodes), 0)
Exemplo n.º 2
0
    def test_add_child_to_empty_tree(self):
        # создаем дерево
        parent_node = None
        tree = SimpleTree(parent_node)

        # создаем и добавляем дочерний узел
        child_node = SimpleTreeNode(1, None)
        tree.AddChild(parent_node, child_node)

        # получаем список узлов
        nodes = [x.NodeValue for x in tree.GetAllNodes()]

        # проверяем, что список пуст
        self.assertEqual(len(nodes), 0)
Exemplo n.º 3
0
    def test_add_child_for_existing_parent(self):
        # создаем дерево
        parent_node = SimpleTreeNode(0, None)
        tree = SimpleTree(parent_node)

        # создаем и добавляем дочерний узел
        child_node = SimpleTreeNode(1, None)
        tree.AddChild(parent_node, child_node)

        # получаем список значений узлов
        nodes = [x.NodeValue for x in tree.GetAllNodes()]
        nodes = tuple(sorted(nodes))

        # проверяем, что список узлов равен (0, 1)
        self.assertTupleEqual(nodes, (0, 1))
Exemplo n.º 4
0
    def test_remove_child_non_existing(self):
        # создаем дерево
        parent_node = SimpleTreeNode(0, None)
        tree = SimpleTree(parent_node)

        # создаем узел, но не добавляем
        child_node = SimpleTreeNode(1, None)

        # удаляем не принадлежащий к дереву узел
        tree.DeleteNode(child_node)

        # получаем список значений узлов
        nodes = [x.NodeValue for x in tree.GetAllNodes()]
        nodes = tuple(sorted(nodes))

        # проверяем, что список узлов равен (0,)
        self.assertTupleEqual(nodes, (0, ))
Exemplo n.º 5
0
    def test_get_all(self):
        # создаем дерево
        parent_node = SimpleTreeNode(0, None)
        tree = SimpleTree(parent_node)

        # создаем и добавляем дочерние узлы в цикле
        for i in range(1, 5):
            child_node = SimpleTreeNode(i, None)
            tree.AddChild(parent_node, child_node)

        # получаем список значений узлов
        nodes = [x.NodeValue for x in tree.GetAllNodes()]
        nodes = tuple(sorted(nodes))

        # проверяем, что список узлов равен (0,1,2,3,4,)
        self.assertTupleEqual(nodes, (
            0,
            1,
            2,
            3,
            4,
        ))
class TestUM(unittest.TestCase):
    def setUp(self):
        self.parent_node = SimpleTreeNode(1, None)
        self.tree = SimpleTree(self.parent_node)
        self.assertEqual(self.parent_node, self.tree.Root)

    def test_add_child(self):
        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)
        self.assertEqual(child2, self.tree.Root.Children[0])

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)
        self.assertEqual(child3, self.tree.Root.Children[1])

    def test_delete_child(self):
        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)

        self.tree.DeleteNode(child2)
        self.assertEqual(child3, self.tree.Root.Children[0])

        child4 = SimpleTreeNode(4, child3)
        self.tree.AddChild(child3, child4)

        child5 = SimpleTreeNode(5, child3)
        self.tree.AddChild(child3, child5)

        self.tree.DeleteNode(child4)
        self.assertEqual(child5, self.tree.Root.Children[0].Children[0])

    def test_get_all_nodes(self):
        self.assertEqual([self.parent_node], self.tree.GetAllNodes())

        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)

        self.assertEqual([self.parent_node, child2, child3],
                         self.tree.GetAllNodes())

        child4 = SimpleTreeNode(4, child2)
        self.tree.AddChild(child2, child4)

        child5 = SimpleTreeNode(5, child2)
        self.tree.AddChild(child2, child5)

        self.assertEqual([self.parent_node, child2, child4, child5, child3],
                         self.tree.GetAllNodes())

        child6 = SimpleTreeNode(6, child4)
        self.tree.AddChild(child4, child6)

        child7 = SimpleTreeNode(7, child4)
        self.tree.AddChild(child4, child7)

        self.assertEqual(
            [self.parent_node, child2, child4, child6, child7, child5, child3],
            self.tree.GetAllNodes())

    def test_find_nodes(self):
        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)

        child4 = SimpleTreeNode(4, child2)
        self.tree.AddChild(child2, child4)

        child5 = SimpleTreeNode(5, child2)
        self.tree.AddChild(child2, child5)

        child2_again = SimpleTreeNode(2, child4)
        self.tree.AddChild(child4, child2_again)

        child2_another_one = SimpleTreeNode(2, child4)
        self.tree.AddChild(child4, child2_another_one)

        self.assertEqual([], self.tree.FindNodesByValue(0))

        self.assertEqual([child5], self.tree.FindNodesByValue(5))

        self.assertEqual([child2, child2_again, child2_another_one],
                         self.tree.FindNodesByValue(2))

    def test_count_all_nodes(self):

        self.assertEqual(1, self.tree.Count())

        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)

        child4 = SimpleTreeNode(4, child2)
        self.tree.AddChild(child2, child4)

        self.assertEqual(4, self.tree.Count())

        child5 = SimpleTreeNode(5, child2)
        self.tree.AddChild(child2, child5)

        child6 = SimpleTreeNode(6, child4)
        self.tree.AddChild(child4, child6)

        child7 = SimpleTreeNode(7, child4)
        self.tree.AddChild(child4, child7)

        self.assertEqual(7, self.tree.Count())

    def test_count_all_leaves(self):

        self.assertEqual(1, self.tree.LeafCount())

        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)

        self.assertEqual(2, self.tree.LeafCount())

        child4 = SimpleTreeNode(4, child2)
        self.tree.AddChild(child2, child4)

        child5 = SimpleTreeNode(5, child2)
        self.tree.AddChild(child2, child5)

        self.assertEqual(3, self.tree.LeafCount())

        child6 = SimpleTreeNode(6, child4)
        self.tree.AddChild(child4, child6)

        child7 = SimpleTreeNode(7, child4)
        self.tree.AddChild(child4, child7)

        self.assertEqual(4, self.tree.LeafCount())

    def test_move_node(self):
        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)

        child4 = SimpleTreeNode(4, child2)
        self.tree.AddChild(child2, child4)

        child5 = SimpleTreeNode(5, child2)
        self.tree.AddChild(child2, child5)

        child6 = SimpleTreeNode(6, child4)
        self.tree.AddChild(child4, child6)

        child7 = SimpleTreeNode(7, child4)
        self.tree.AddChild(child4, child7)

        self.assertEqual(0, len(child3.Children))

        self.tree.MoveNode(child4, child3)

        self.assertEqual(1, len(child2.Children))
        self.assertEqual(child5, child2.Children[0])

        self.assertEqual(1, len(child3.Children))
        self.assertEqual(child4, child3.Children[0])
        self.assertEqual(2, len(child4.Children))
        self.assertEqual([child6, child7], child4.Children)

    def test_set_levels(self):
        self.tree.set_levels()
        self.assertEqual(0, self.tree.Root.level)

        child2 = SimpleTreeNode(2, self.parent_node)
        self.tree.AddChild(self.parent_node, child2)

        child3 = SimpleTreeNode(3, self.parent_node)
        self.tree.AddChild(self.parent_node, child3)

        self.tree.set_levels()
        self.assertEqual(1, child3.level)

        child4 = SimpleTreeNode(4, child2)
        self.tree.AddChild(child2, child4)

        child5 = SimpleTreeNode(5, child2)
        self.tree.AddChild(child2, child5)

        self.tree.set_levels()
        self.assertEqual(2, child4.level)

        child6 = SimpleTreeNode(6, child4)
        self.tree.AddChild(child4, child6)

        child7 = SimpleTreeNode(7, child4)
        self.tree.AddChild(child4, child7)

        self.tree.set_levels()
        self.assertEqual(3, child7.level)