Esempio n. 1
0
def test_node_set_attributes():
    root = Node(1)
    assert root.left is None
    assert root.right is None
    assert root.value == 1
    assert repr(root) == 'Node(1)'

    left_child = Node(2)
    root.left = left_child
    assert root.left is left_child
    assert root.right is None
    assert root.value == 1
    assert root.left.left is None
    assert root.left.right is None
    assert root.left.value == 2
    assert repr(left_child) == 'Node(2)'

    right_child = Node(3)
    root.right = right_child
    assert root.left is left_child
    assert root.right is right_child
    assert root.value == 1
    assert root.right.left is None
    assert root.right.right is None
    assert root.right.value == 3
    assert repr(right_child) == 'Node(3)'

    last_node = Node(4)
    left_child.right = last_node
    assert root.left.right is last_node
    assert root.left.right.value == 4
    assert repr(last_node) == 'Node(4)'

    with pytest.raises(InvalidNodeValueError):
        # noinspection PyTypeChecker
        Node('this_is_not_an_integer')

    with pytest.raises(InvalidNodeTypeError):
        # noinspection PyTypeChecker
        Node(1, 'this_is_not_a_node')

    with pytest.raises(InvalidNodeTypeError):
        # noinspection PyTypeChecker
        Node(1, Node(1), 'this_is_not_a_node')

    with pytest.raises(InvalidNodeValueError):
        root.value = 'this_is_not_an_integer'
    assert root.value == 1

    with pytest.raises(InvalidNodeTypeError):
        root.left = 'this_is_not_a_node'
    assert root.left is left_child

    with pytest.raises(InvalidNodeTypeError):
        root.right = 'this_is_not_a_node'
    assert root.right is right_child
Esempio n. 2
0
def test_inspect():
    for invalid_argument in [None, 1, 'foo']:
        with pytest.raises(ValueError) as err:
            inspect(invalid_argument)
        assert str(err.value) == 'Expecting a list or a node'

    def convert_inspect(target):
        return inspect(convert(target))

    def self_inspect(target):
        return target.inspect()

    for inspect_func in [inspect, convert_inspect, self_inspect]:

        root = Node(1)
        assert inspect_func(root) == {
            'is_height_balanced': True,
            'is_weight_balanced': True,
            'is_max_heap': True,
            'is_min_heap': True,
            'is_bst': True,
            'height': 0,
            'max_value': 1,
            'min_value': 1,
            'leaf_count': 1,
            'node_count': 1,
            'max_leaf_depth': 0,
            'min_leaf_depth': 0,
        }
        root.left = Node(2)
        assert inspect_func(root) == {
            'is_height_balanced': True,
            'is_weight_balanced': True,
            'is_max_heap': False,
            'is_min_heap': True,
            'is_bst': False,
            'height': 1,
            'max_value': 2,
            'min_value': 1,
            'node_count': 2,
            'leaf_count': 1,
            'max_leaf_depth': 1,
            'min_leaf_depth': 1,
        }
        root.right = Node(3)
        assert inspect_func(root) == {
            'is_height_balanced': True,
            'is_weight_balanced': True,
            'is_max_heap': False,
            'is_min_heap': True,
            'is_bst': False,
            'height': 1,
            'max_value': 3,
            'min_value': 1,
            'leaf_count': 2,
            'node_count': 3,
            'max_leaf_depth': 1,
            'min_leaf_depth': 1,
        }
        root.value = 2
        root.left.value = 1
        root.right.value = 3
        assert inspect_func(root) == {
            'is_height_balanced': True,
            'is_weight_balanced': True,
            'is_max_heap': False,
            'is_min_heap': False,
            'is_bst': True,
            'height': 1,
            'max_value': 3,
            'min_value': 1,
            'leaf_count': 2,
            'node_count': 3,
            'max_leaf_depth': 1,
            'min_leaf_depth': 1,
        }
        root.value = 1
        root.left.value = 2
        root.right.value = 3
        root.left.right = Node(4)
        assert inspect_func(root) == {
            'is_height_balanced': True,
            'is_weight_balanced': True,
            'is_max_heap': False,
            'is_min_heap': False,
            'is_bst': False,
            'height': 2,
            'max_value': 4,
            'min_value': 1,
            'leaf_count': 2,
            'node_count': 4,
            'max_leaf_depth': 2,
            'min_leaf_depth': 1,
        }
        root.left.left = Node(5)
        assert inspect_func(root) == {
            'is_height_balanced': True,
            'is_weight_balanced': True,
            'is_max_heap': False,
            'is_min_heap': True,
            'is_bst': False,
            'height': 2,
            'max_value': 5,
            'min_value': 1,
            'leaf_count': 3,
            'node_count': 5,
            'max_leaf_depth': 2,
            'min_leaf_depth': 1,
        }
        root.right.right = Node(6)
        assert inspect_func(root) == {
            'is_height_balanced': True,
            'is_weight_balanced': True,
            'is_max_heap': False,
            'is_min_heap': False,
            'is_bst': False,
            'height': 2,
            'max_value': 6,
            'min_value': 1,
            'leaf_count': 3,
            'node_count': 6,
            'max_leaf_depth': 2,
            'min_leaf_depth': 2,
        }

        root.right.right = Node(None)
        with pytest.raises(ValueError) as err:
            assert inspect_func(root)
        assert str(err.value) == 'A node cannot have a null value'

        root.right.right = {}
        with pytest.raises(ValueError) as err:
            assert inspect_func(root)
        assert str(err.value) == 'Found an invalid node in the tree'
Esempio n. 3
0
def test_node_set_attributes():
    root = Node(1)
    assert root.left is None
    assert root.right is None
    assert root.val == 1
    assert root.value == 1
    assert repr(root) == 'Node(1)'

    root.value = 2
    assert root.value == 2
    assert root.val == 2
    assert repr(root) == 'Node(2)'

    root.val = 1
    assert root.value == 1
    assert root.val == 1
    assert repr(root) == 'Node(1)'

    left_child = Node(2)
    root.left = left_child
    assert root.left is left_child
    assert root.right is None
    assert root.val == 1
    assert root.left.left is None
    assert root.left.right is None
    assert root.left.val == 2
    assert repr(left_child) == 'Node(2)'

    right_child = Node(3)
    root.right = right_child
    assert root.left is left_child
    assert root.right is right_child
    assert root.val == 1
    assert root.right.left is None
    assert root.right.right is None
    assert root.right.val == 3
    assert repr(right_child) == 'Node(3)'

    last_node = Node(4)
    left_child.right = last_node
    assert root.left.right is last_node
    assert repr(root.left.right) == 'Node(4)'

    with pytest.raises(NodeValueError) as err:
        # noinspection PyTypeChecker
        Node('this_is_not_an_integer')
    assert str(err.value) == 'node value must be a number'

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, 'this_is_not_a_node')
    assert str(err.value) == 'left child must be a Node instance'

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, Node(1), 'this_is_not_a_node')
    assert str(err.value) == 'right child must be a Node instance'

    with pytest.raises(NodeValueError) as err:
        root.val = 'this_is_not_an_integer'
    assert root.val == 1
    assert str(err.value) == 'node value must be a number'

    with pytest.raises(NodeValueError) as err:
        root.value = 'this_is_not_an_integer'
    assert root.value == 1
    assert str(err.value) == 'node value must be a number'

    with pytest.raises(NodeTypeError) as err:
        root.left = 'this_is_not_a_node'
    assert root.left is left_child
    assert str(err.value) == 'left child must be a Node instance'

    with pytest.raises(NodeTypeError) as err:
        root.right = 'this_is_not_a_node'
    assert root.right is right_child
    assert str(err.value) == 'right child must be a Node instance'
Esempio n. 4
0
def test_node_set_attributes():
    root = Node(1)
    assert root.left is None
    assert root.right is None
    assert root.val == 1
    assert root.value == 1
    assert repr(root) == "Node(1)"

    root.value = 2
    assert root.value == 2
    assert root.val == 2
    assert repr(root) == "Node(2)"

    root.val = 1
    assert root.value == 1
    assert root.val == 1
    assert repr(root) == "Node(1)"

    left_child = Node(2)
    root.left = left_child
    assert root.left is left_child
    assert root.right is None
    assert root.val == 1
    assert root.left.left is None
    assert root.left.right is None
    assert root.left.val == 2
    assert repr(left_child) == "Node(2)"

    right_child = Node(3)
    root.right = right_child
    assert root.left is left_child
    assert root.right is right_child
    assert root.val == 1
    assert root.right.left is None
    assert root.right.right is None
    assert root.right.val == 3
    assert repr(right_child) == "Node(3)"

    last_node = Node(4)
    left_child.right = last_node
    assert root.left.right is last_node
    assert repr(root.left.right) == "Node(4)"

    with pytest.raises(NodeValueError) as err:
        # noinspection PyTypeChecker
        Node("this_is_not_an_integer")
    assert str(err.value) == "node value must be a float or int"

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, "this_is_not_a_node")
    assert str(err.value) == "left child must be a Node instance"

    with pytest.raises(NodeTypeError) as err:
        # noinspection PyTypeChecker
        Node(1, Node(1), "this_is_not_a_node")
    assert str(err.value) == "right child must be a Node instance"

    with pytest.raises(NodeValueError) as err:
        root.val = "this_is_not_an_integer"
    assert root.val == 1
    assert str(err.value) == "node value must be a float or int"

    with pytest.raises(NodeValueError) as err:
        root.value = "this_is_not_an_integer"
    assert root.value == 1
    assert str(err.value) == "node value must be a float or int"

    with pytest.raises(NodeTypeError) as err:
        root.left = "this_is_not_a_node"
    assert root.left is left_child
    assert str(err.value) == "left child must be a Node instance"

    with pytest.raises(NodeTypeError) as err:
        root.right = "this_is_not_a_node"
    assert root.right is right_child
    assert str(err.value) == "right child must be a Node instance"