Exemplo n.º 1
0
def test_get_child_from_path():
    # Make a small tree

    node1 = Node('node1')
    node2 = Node('node2')
    node3 = Node('node3')

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

    assert node1._get_child_from_path("node2.node3") == node3

    clean()
Exemplo n.º 2
0
def test_pickle():

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

    import pickle

    root = Node("root")
    node = Node("node")

    root._add_child(node)

    d = pickle.dumps(root)

    root2 = pickle.loads(d)

    assert root2.node.path == 'root.node'
    assert root2.node.name == 'node'

    # Now test pickling a subclass of Node

    root = _SimpleInheritance("root")

    root._placeholder = 5.3

    node = _SimpleInheritance("node")

    root._add_child(node)

    d = pickle.dumps(root)

    root2 = pickle.loads(d)

    assert root2.node.path == 'root.node'
    assert root2.node.name == 'node'

    print root.placeholder

    assert root2.placeholder == root.placeholder

    rootc = _ComplexInheritance("root", -1.0, 1.0)
    nodec = _ComplexInheritance("node", -5.0, 5.0)

    rootc._add_child(nodec)

    d = pickle.dumps(rootc)

    root2c = pickle.loads(d)

    print root2c.min_value

    clean()
Exemplo n.º 3
0
def test_get_path():
    # Make a small tree

    node1 = Node('node1')
    node2 = Node('node2')
    node3 = Node('node3')

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

    print node3.path

    assert node3.path == "node1.node2.node3"

    clean()
Exemplo n.º 4
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")
Exemplo n.º 5
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.º 6
0
def test_get_children():
    node1 = Node('node1')
    node2 = Node('node2')
    node22 = Node('node22')
    node3 = Node('node3')

    node1._add_child(node2)
    node1._add_child(node22)
    node2._add_child(node3)

    assert node1._get_children() == (node2, node22)
    assert node2._get_children() == (node3, )

    clean()
Exemplo n.º 7
0
def test_memory_leaks_getters():

    # Now test the getters

    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)

    None_counts_before = node_ctype._get_reference_counts(None)

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

    node_again = root._get_child("node1")  # node1 +1

    assert node_ctype._get_reference_counts(node1) == refc_before_link1 + 2

    del node_again  # node1 -1

    assert node_ctype._get_reference_counts(node1) == refc_before_link1 + 1

    children = root._get_children()  #node1 +1, node2 +1, node3 +1

    assert len(children) == 3

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

    # test get_parent

    root_again = node1._get_parent()  # root +1

    assert node_ctype._get_reference_counts(root) == refc_before_link_root + 4

    del root_again  # root -1

    assert node_ctype._get_reference_counts(root) == refc_before_link_root + 3

    # test _get_path

    path = node2._get_path()  # this shouldn't change any ref count

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

    # test _get_child_from_path

    node4 = Node("node4")
    refc_before_link4 = node_ctype._get_reference_counts(node4)

    node3._add_child(node4)  # node3 +1, node4 + 1

    node4_again = root._get_child_from_path("node3.node4")  # node4 +1

    assert node_ctype._get_reference_counts(node4) == refc_before_link4 + 2
    assert node_ctype._get_reference_counts(node3) == refc_before_link3 + 3

    del node4_again  # node4 -1

    assert node_ctype._get_reference_counts(node4) == refc_before_link4 + 1