Exemplo n.º 1
0
def test_memory_leaks_setters():

    root = Node("root")

    node = Node("node")

    refc_before_link = node_ctype._get_reference_counts(node)

    root._add_child(node)

    # Adding a node adds 1 reference

    assert node_ctype._get_reference_counts(node) == refc_before_link + 1

    # Remove the node and verify that the reference counts goes back to what it was
    root._remove_child("node")

    # Now we should go back to the original

    assert node_ctype._get_reference_counts(node) == refc_before_link

    # Now add a second node nested under the first one (root.node.node2)
    node2 = Node("node2")

    refc_before_link2 = node_ctype._get_reference_counts(node2)

    root._add_child(node)  # +1 for node

    assert node_ctype._get_reference_counts(node) == refc_before_link + 1

    node._add_child(node2)  # +1 for node2 and +1 for node

    assert node_ctype._get_reference_counts(node2) == refc_before_link2 + 1

    # "node" is now also parent of node2, so its reference are now 2 more than the original
    assert node_ctype._get_reference_counts(node) == refc_before_link + 2

    # Clean up and verify that we go back to the original number of references
    node._remove_child("node2")  # -1 for node2, -1 for node

    assert node_ctype._get_reference_counts(node2) == refc_before_link2

    assert node_ctype._get_reference_counts(node) == refc_before_link + 1

    root._remove_child("node")  # -1 for node

    assert node_ctype._get_reference_counts(node) == refc_before_link

    # Now test add_children
    node3 = Node("node3")
    node4 = Node("node4")

    refc_before_link3 = node_ctype._get_reference_counts(node3)
    refc_before_link4 = node_ctype._get_reference_counts(node4)

    root._add_children([node3, node4])  # +1 for both nodes

    assert node_ctype._get_reference_counts(node3) == refc_before_link3 + 1
    assert node_ctype._get_reference_counts(node3) == refc_before_link4 + 1
Exemplo n.º 2
0
def test_memory_leaks_destructors():

    print("\n\n\n\n\n")

    for i in range(1000):

        print("\n\nRound %i" % (i + 1))

        # Test destructors
        root = Node("root")

        node1 = Node("node1")
        node2 = Node("node2")
        node3 = Node("node3")

        refc_before_link_root = node_ctype._get_reference_counts(root)

        refc_before_link1 = node_ctype._get_reference_counts(node1)
        refc_before_link2 = node_ctype._get_reference_counts(node2)
        refc_before_link3 = node_ctype._get_reference_counts(node3)

        # root._add_children([node1, node2, node3])  # node1 +1, node2 + 1, node3 + 1, root +3

        root._add_child(node1)
        root._add_child(node2)
        root._add_child(node3)

        assert node_ctype._get_reference_counts(
            root) == refc_before_link_root + 3
        assert node_ctype._get_reference_counts(node1) == refc_before_link1 + 1
        assert node_ctype._get_reference_counts(node2) == refc_before_link2 + 1
        assert node_ctype._get_reference_counts(node3) == refc_before_link3 + 1

        #
        # Let's destroy the tree
        root._remove_child("node1")

        root._remove_child("node2")

        root._remove_child("node3")