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 test_even_tree_for_string_2(): # aaaaabbabb # 1111122122 inputstr = farach.str2int('1111122122') t_odd = farach.T_odd(inputstr) t_even = farach.T_even(t_odd, inputstr) root = utils.Node(aId="root") inner = utils.Node(1, "inner") inner2 = utils.Node(2, "inner") inner.add_child(inner2) inner.add_child(utils.Node(3, 8)) inner2.add_child(utils.Node(9, 2)) inner2.add_child(utils.Node(7, 4)) root.add_child(inner) root.add_child(utils.Node(5, 6)) root.add_child(utils.Node(1, 10)) root.update_leaf_list() # print("t_even:") # print(t_even.fancyprint(inputstr)) # print("root:") # print(root.fancyprint(inputstr)) assert (t_even.fancyprint(inputstr) == root.fancyprint(inputstr))
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 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 = 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!')
def faked_tree_article(): t_odd = utils.Node(0, "root") inner1 = utils.Node(1, "inner") t_odd.add_child(inner1) inner2 = utils.Node(13, 1) inner1.add_child(inner2) inner2 = utils.Node(3, "inner") inner1.add_child(inner2) inner3 = utils.Node(7, 7) inner2.add_child(inner3) inner3 = utils.Node(11, 3) inner2.add_child(inner3) inner1 = utils.Node(1, "inner") t_odd.add_child(inner1) inner2 = utils.Node(2, "inner") inner1.add_child(inner2) inner3 = utils.Node(5, 9) inner2.add_child(inner3) inner3 = utils.Node(3, 11) inner2.add_child(inner3) inner2 = utils.Node(9, 5) inner1.add_child(inner2) inner1 = utils.Node(1, 13) t_odd.add_child(inner1) t_even = utils.Node(0, "root") inner1 = utils.Node(1, "inner") t_even.add_child(inner1) inner2 = utils.Node(12, 2) inner1.add_child(inner2) inner2 = utils.Node(4, 10) inner1.add_child(inner2) inner2 = utils.Node(2, 12) inner1.add_child(inner2) inner1 = utils.Node(1, "inner") t_even.add_child(inner1) inner2 = utils.Node(8, 6) inner1.add_child(inner2) inner2 = utils.Node(2, "inner2") inner1.add_child(inner2) inner3 = utils.Node(6, 8) inner2.add_child(inner3) inner3 = utils.Node(10, 4) inner2.add_child(inner3) t_overmerged_result = utils.Node(0, "root") inner1 = utils.Node(1, "inner") t_overmerged_result.add_child(inner1) inner2 = utils.Node(12, 2) inner1.add_child(inner2) inner3 = utils.Node(13, 1) inner2.add_child(inner3) inner2 = utils.Node(3, "inner") inner1.add_child(inner2) inner3 = utils.Node(7, 7) inner2.add_child(inner3) inner3 = utils.Node(11, 3) inner2.add_child(inner3) inner3 = utils.Node(4, 10) inner2.add_child(inner3) inner2 = utils.Node(2, 12) inner1.add_child(inner2) inner1 = utils.Node(1, "inner") t_overmerged_result.add_child(inner1) inner2 = utils.Node(2, "inner") inner1.add_child(inner2) inner3 = utils.Node(5, 9) inner2.add_child(inner3) inner4 = utils.Node(8, 6) inner3.add_child(inner4) inner3 = utils.Node(3, 11) inner2.add_child(inner3) inner2 = utils.Node(2, "inner2") inner1.add_child(inner2) inner3 = utils.Node(6, 8) inner2.add_child(inner3) inner4 = utils.Node(9, 5) inner3.add_child(inner4) inner3 = utils.Node(10, 4) inner2.add_child(inner3) inner1 = utils.Node(1, 13) t_overmerged_result.add_child(inner1) inputstr = farach.str2int("1112221221213") t_even.update_leaf_list() t_odd.update_leaf_list() t_overmerged = farach.overmerge(t_even, t_odd, inputstr) t_overmerged_result.update_leaf_list() assert t_overmerged.fancyprint(inputstr) == t_overmerged_result.fancyprint( inputstr)