def test_bst(): for invalid_height in ['foo', -1]: with pytest.raises(ValueError) as err: bst(height=invalid_height) assert str(err.value) == 'Height must be a non-negative integer' for _ in range(repetitions): root = bst(height=0) assert attr(root, 'left') is None assert attr(root, 'right') is None assert isinstance(attr(root, 'value'), int) assert inspect(root)['height'] == 0 assert inspect(root)['is_bst'] is True for _ in range(repetitions): height = randint(1, 10) root = bst(height) nodes_to_visit = [root] while nodes_to_visit: node = nodes_to_visit.pop() assert isinstance(node, Node) assert isinstance(attr(node, 'value'), int) if attr(node, 'left') is not None: nodes_to_visit.append(attr(node, 'left')) if attr(node, 'right') is not None: nodes_to_visit.append(attr(node, 'right')) assert inspect(root)['height'] == height assert inspect(root)['is_bst'] is True
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'
def convert_inspect(target): return inspect(convert(target))
def inspect(self): return inspect(self)