2015-08-15 07:30:09 +08:00
|
|
|
from nose.tools import assert_equal
|
2016-06-26 10:08:54 +08:00
|
|
|
from nose.tools import raises
|
2015-08-15 07:30:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestBstValidate(object):
|
|
|
|
|
2016-06-26 10:08:54 +08:00
|
|
|
@raises(Exception)
|
|
|
|
def test_bst_validate_empty(self):
|
|
|
|
validate_bst(None)
|
|
|
|
|
2015-08-15 07:30:09 +08:00
|
|
|
def test_bst_validate(self):
|
2016-08-30 18:06:58 +08:00
|
|
|
bst = BstValidate(Node(5))
|
|
|
|
bst.insert(8)
|
|
|
|
bst.insert(5)
|
|
|
|
bst.insert(6)
|
|
|
|
bst.insert(4)
|
|
|
|
bst.insert(7)
|
|
|
|
assert_equal(bst.validate(), True)
|
|
|
|
|
|
|
|
bst = BstValidate(Node(5))
|
2015-08-15 07:30:09 +08:00
|
|
|
left = Node(5)
|
|
|
|
right = Node(8)
|
|
|
|
invalid = Node(20)
|
2016-08-30 18:06:58 +08:00
|
|
|
bst.root.left = left
|
|
|
|
bst.root.right = right
|
|
|
|
bst.root.left.right = invalid
|
|
|
|
assert_equal(bst.validate(), False)
|
2015-08-15 07:30:09 +08:00
|
|
|
|
|
|
|
print('Success: test_bst_validate')
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
test = TestBstValidate()
|
2016-06-26 10:08:54 +08:00
|
|
|
test.test_bst_validate_empty()
|
2015-08-15 07:30:09 +08:00
|
|
|
test.test_bst_validate()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|