def test_remove(): # Test validity first tree = Tree() for elem in range(2, 1999 * 2): tree.insert(elem) sequence = [2 * pow(997, i, 1999) for i in range(1, 1999)] for elem in sequence: tree.remove(elem + 1) # if flatten(tree) != sorted(sequence): # print(elem+1) # print(flatten(tree)) # print(sorted(sequence)) assert flatten(tree) == sorted( sequence), "Incorrect tree after a sequence of removes" # Test speed tree = Tree() for elem in range(0, 10000, 2): tree.insert(elem) # Non-existing elements for elem in range(1, 10000, 2): for _ in range(10): tree.remove(elem) # Existing elements for elem in range(2, 10000, 2): tree.remove(elem)
def test_insert(): # Test validity first tree = Tree() sequence = [pow(997, i, 1999) for i in range(1, 1999)] for elem in sequence: tree.insert(elem) assert flatten(tree) == sorted( sequence), "Incorrect tree after a sequence of inserts" # Test speed tree = Tree() for elem in range(200000): for _ in range(10): tree.insert(elem)
def test_lookup(): tree = Tree() for elem in range(0, 1000, 2): tree.insert(elem) # Find non-existing for elem in range(1, 1000, 2): for _ in range(10): assert tree.lookup(elem) is None, "Non-existing element was found" # Find existing for elem in range(0, 1000, 2): for _ in range(10): assert tree.lookup( elem) is not None, "Existing element was not found"