def current_test(): inputstr = farach.str2int('1222') constructed_tree = farach.construct_suffix_tree(inputstr) expected_result = Node(aId='root') inner1 = Node(aId='inner', aStrLength=[2]) inner2 = Node(aId='inner', aStrLength=[2]) leaf1 = Node(aId=1, aStrLength=[1, 2, 2, 2, 3]) leaf2 = Node(aId=2, aStrLength=[2, 3]) leaf3 = Node(aId=3, aStrLength=[3]) leaf4 = Node(aId=4, aStrLength=[3]) leaf5 = Node(aId=5, aStrLength=[3]) expected_result.add_child(leaf1) expected_result.add_child(inner1) expected_result.add_child(leaf5) inner1.add_child(inner2) inner1.add_child(leaf4) inner2.add_child(leaf2) inner2.add_child(leaf3) # print('-'*80) # print('inputstr: %s' % inputstr) # print('expected:') # print(expected_result.fancyprint(inputstr)) # print('actual:') # print(constructed_tree.fancyprint(inputstr)) assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr)
def banana_test(): # banana # 123232 inputstr = farach.str2int('123232') root = Node(aId="root") root.add_child(Node(7, 1)) inner = Node(1, "inner") root.add_child(inner) inner2 = Node(3, "inner") inner2.add_child(Node(6, 2)) inner2.add_child(Node(4, 4)) inner.add_child(inner2) inner.add_child(Node(2, 6)) inner = Node(2, "inner") inner.add_child(Node(5, 3)) inner.add_child(Node(3, 5)) root.add_child(inner) root.add_child(Node(1, 7)) constructed_tree = farach.construct_suffix_tree(inputstr) root.update_leaf_list() # print(constructed_tree.fancyprint(inputstr)) # print(root.fancyprint(inputstr)) assert constructed_tree.fancyprint(inputstr) == root.fancyprint(inputstr)
def str12121(): string = '12121' string = str2int(string) constructed_tree = farach.construct_suffix_tree(string) actual_tree = Node(aId='root') inner1 = Node(1, 'inner') inner2 = Node(2, 'inner') inner3 = Node(3, 'inner') leaf1 = Node(6, 1) leaf2 = Node(5, 2) leaf3 = Node(4, 3) leaf4 = Node(3, 4) leaf5 = Node(2, 5) leaf6 = Node(1, 6) actual_tree.add_child(inner1) actual_tree.add_child(inner2) actual_tree.add_child(leaf6) inner1.add_child(inner3) inner1.add_child(leaf5) inner3.add_child(leaf1) inner3.add_child(leaf3) inner2.add_child(leaf2) inner2.add_child(leaf4) actual_tree.update_leaf_list() # print(actual_tree.fancyprint(string)) # print(constructed_tree.fancyprint(string)) check_correctness(constructed_tree, string) assert constructed_tree.fancyprint(string) == actual_tree.fancyprint( string)
def main(): #run_tests() # while True: for _ in range(1000): S = ''.join(random.choice(string.digits) for _ in range(1000)) S = str2int(S) tree1 = naive.construct_suffix_tree(S[:]) tree2 = farach.construct_suffix_tree(S[:]) tree3 = mccreight.construct_suffix_tree(S) sort_tree(tree1, S) sort_tree(tree3, S) assert tree_equality(tree1, tree2, S) assert tree_equality(tree1, tree3, S)
def check_correctness(string): string = farach.str2int(string) constructed_tree = farach.construct_suffix_tree(string) id2node = [] constructed_tree.traverse(lambda n: id2node.append((n.id, n)) if 'inner' not in str(n.id) else 'do nothing') id2node = dict(id2node) constructed_tree.update_leaf_list leaflist = constructed_tree.leaflist lca_al = lca.LCA() lca_al.preprocess(constructed_tree) for i in leaflist: for j in leaflist: assert farach.naive_lca(i, j, constructed_tree, id2node) == lca_al.query(i, j)
def test_tree_four(): string = 'mississippi' string = farach.str2int(string) constructed_tree = farach.construct_suffix_tree(string) constructed_tree.update_leaf_list leaflist = constructed_tree.leaflist lca_al = lca.LCA() lca_al.preprocess(constructed_tree) assert str(lca_al.query(leaflist[1], leaflist[2]).leaflist) == "[node2, node5]" assert str( lca_al.query(leaflist[0], leaflist[2]).leaflist ) == "[node1, node2, node5, node8, node11, node4, node7, node3, node6, node10, node9, node12]" assert str(lca_al.query( leaflist[6], leaflist[8]).leaflist) == "[node4, node7, node3, node6]" assert str(lca_al.query(leaflist[9], leaflist[10]).leaflist) == "[node10, node9]"
def check_speed(): for i in range(100, 500): S = ''.join(random.choice(string.digits) for _ in range(i * 50)) S = farach.str2int(S) constructed_tree = farach.construct_suffix_tree(S) id2node = [] constructed_tree.traverse(lambda n: id2node.append( (n.id, n)) if 'inner' not in str(n.id) else 'do nothing') id2node = dict(id2node) constructed_tree.update_leaf_list leaflist = constructed_tree.leaflist start = time.time() preprocess_and_one_query(constructed_tree, leaflist) end = time.time() print('%i,%f' % ((i * 50), (end - start)))
def test_tree_three(): string = '12121' string = farach.str2int(string) constructed_tree = farach.construct_suffix_tree(string) constructed_tree.update_leaf_list leaflist = constructed_tree.leaflist lca_al = lca.LCA() lca_al.preprocess(constructed_tree) assert str(lca_al.query(leaflist[0], leaflist[1]).leaflist) == "[node1, node3]" assert str(lca_al.query( leaflist[2], leaflist[3]).leaflist) == "[node1, node3, node5, node2, node4, node6]" assert str(lca_al.query(leaflist[0], leaflist[2]).leaflist) == "[node1, node3, node5]" assert str(lca_al.query(leaflist[3], leaflist[4]).leaflist) == "[node2, node4]" assert str(lca_al.query( leaflist[0], leaflist[5]).leaflist) == "[node1, node3, node5, node2, node4, node6]"
def run_tests(): inputstr = str2int("mississippi") tree = farach.construct_suffix_tree(inputstr) check_correctness(tree, inputstr) inputstr = str2int("121112212221") tree = farach.construct_suffix_tree(inputstr) check_correctness(tree, inputstr) inputstr = str2int("111222122121") tree = farach.construct_suffix_tree(inputstr) check_correctness(tree, inputstr) inputstr = str2int("12121212121") tree = farach.construct_suffix_tree(inputstr) check_correctness(tree, inputstr) inputstr = str2int("banana") tree = farach.construct_suffix_tree(inputstr) check_correctness(tree, inputstr) inputstr = str2int("mississippiisaniceplaceithink") tree = farach.construct_suffix_tree(inputstr) check_correctness(tree, inputstr) inputstr = str2int("12121") tree = farach.construct_suffix_tree(inputstr) check_correctness(tree, inputstr)
def run_tests(): inputstr = farach.str2int('1') constructed_tree = farach.construct_suffix_tree(inputstr) expected_result = Node(aId='root') expected_result.add_child(Node(aId=1, aStrLength=2)) expected_result.add_child(Node(aId=2, aStrLength=1)) constructed_tree.update_leaf_list() expected_result.update_leaf_list() # print('inputstr: %s' % inputstr) # print('expected:') # print(expected_result.fancyprint()) # print('actual:') # print(constructed_tree.fancyprint()) assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) inputstr = farach.str2int('12') constructed_tree = farach.construct_suffix_tree(inputstr) expected_result = Node(aId='root') expected_result.add_child(Node(aId=1, aStrLength=3)) expected_result.add_child(Node(aId=2, aStrLength=2)) expected_result.add_child(Node(aId=3, aStrLength=1)) constructed_tree.update_leaf_list() expected_result.update_leaf_list() # print('inputstr: %s' % inputstr) # print('expected:') # print(expected_result.fancyprint(inputstr)) # print('actual:') # print(constructed_tree.fancyprint(inputstr)) assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) inputstr = farach.str2int('11') constructed_tree = farach.construct_suffix_tree(inputstr) expected_result = Node(aId='root') innernode = Node(aId='inner', aStrLength=1) expected_result.add_child(innernode) innernode.add_child(Node(aId=1, aStrLength=3)) innernode.add_child(Node(aId=2, aStrLength=2)) expected_result.add_child(Node(aId=3, aStrLength=1)) constructed_tree.update_leaf_list() expected_result.update_leaf_list() # print('inputstr: %s' % inputstr) # print('expected:') # print(expected_result.fancyprint(inputstr)) # print('actual:') # print(constructed_tree.fancyprint(inputstr)) assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) inputstr = farach.str2int('111') constructed_tree = farach.construct_suffix_tree(inputstr) expected_result = Node(aId='root') inner1 = Node(aId='inner', aStrLength=1) inner2 = Node(aId='inner', aStrLength=2) leaf1 = Node(aId=1, aStrLength=4) leaf2 = Node(aId=2, aStrLength=3) leaf3 = Node(aId=3, aStrLength=2) leaf4 = Node(aId=4, aStrLength=1) expected_result.add_child(inner1) expected_result.add_child(leaf4) inner1.add_child(inner2) inner1.add_child(leaf3) inner2.add_child(leaf1) inner2.add_child(leaf2) constructed_tree.update_leaf_list() expected_result.update_leaf_list() # print('inputstr: %s' % inputstr) # print('expected:') # print(expected_result.fancyprint(inputstr)) # print('actual:') # print(constructed_tree.fancyprint(inputstr)) assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) # inputstr = farach.str2int('122') # constructed_tree = farach.construct_suffix_tree(inputstr) # expected_result = Node(aId='root') # expected_result.add_child(Node(aId=1, aStrLength=[12])) # assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) inputstr = farach.str2int('1222') constructed_tree = farach.construct_suffix_tree(inputstr) expected_result = Node(aId='root') inner1 = Node(aId='inner', aStrLength=1) inner2 = Node(aId='inner', aStrLength=2) leaf1 = Node(aId=1, aStrLength=5) leaf2 = Node(aId=2, aStrLength=4) leaf3 = Node(aId=3, aStrLength=3) leaf4 = Node(aId=4, aStrLength=2) leaf5 = Node(aId=5, aStrLength=1) expected_result.add_child(leaf1) expected_result.add_child(inner1) expected_result.add_child(leaf5) inner1.add_child(inner2) inner1.add_child(leaf4) inner2.add_child(leaf2) inner2.add_child(leaf3) expected_result.update_leaf_list() # print('inputstr: %s' % inputstr) # print('expected:') # print(expected_result.fancyprint(inputstr)) # print('actual:') # print(constructed_tree.fancyprint(inputstr)) assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) # inputstr = farach.str2int('1221') # constructed_tree = farach.construct_suffix_tree(inputstr) # expected_result = Node(aId='root') # expected_result.add_child(Node(aId=1, aStrLength=[12])) # assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) # inputstr = farach.str2int('2221') # constructed_tree = farach.construct_suffix_tree(inputstr) # expected_result = Node(aId='root') # expected_result.add_child(Node(aId=1, aStrLength=[12])) # assert constructed_tree.fancyprint(inputstr) == expected_result.fancyprint(inputstr) banana_test() print('tests succeeded!')