int test_reroot_2() { const char *test_name = "test_reroot_2"; /* A tree whose root has 3 children: * (A:3,B:3,(C:2,(D:1,E:1)f:1)g:1)h; */ struct rooted_tree tree = tree_5(); struct hash *map = create_label2node_map(tree.nodes_in_order); struct rnode *node_f = hash_get(map, "f"); const char *exp = "((D:1,E:1)f:0.5,(C:2,(A:3,B:3)h:1)g:0.5);"; reroot_tree(&tree, node_f); const char *obt = to_newick(tree.root); if (strcmp (exp, obt) != 0) { printf ("%s: expected '%s', got '%s'.\n", test_name, exp, obt); return 1; } printf ("%s: ok.\n", test_name); return 0; }
struct llist * get_outgroup_nodes(struct rooted_tree *tree, struct llist *labels) { struct hash *map; struct llist *outgroup_nodes; struct list_elem *el; map = create_label2node_map(tree->nodes_in_order); outgroup_nodes = create_llist(); if (NULL == outgroup_nodes) { perror(NULL); exit(EXIT_FAILURE); } for (el = labels->head; NULL != el; el = el->next) { struct rnode *desc; desc = hash_get(map, (char *) el->data); if (NULL == desc) { fprintf (stderr, "WARNING: label '%s' does not occur in tree\n", (char *) el->data); } else { if (! append_element(outgroup_nodes, desc)) { perror(NULL); exit(EXIT_FAILURE); } } } destroy_hash(map); return outgroup_nodes; }
int test_create_label2node_map() { const char *test_name = "test_create_label2node_map"; struct rnode *n1, *n2, *n3; struct llist *node_list; struct hash *map; n1 = create_rnode("n1", ""); n2 = create_rnode("n2", ""); n3 = create_rnode("n3", ""); node_list = create_llist(); append_element(node_list, n1); append_element(node_list, n2); append_element(node_list, n3); map = create_label2node_map(node_list); if (NULL == map) { printf ("%s: map must not be NULL.\n", test_name); return 1; } if (NULL != hash_get(map, "not there")) { printf ("%s: inexistent label should return NULL.\n", test_name); return 1; } if (n1 != hash_get(map, "n1")) { printf ("%s: node with label 'n1' should be %p, not %p.\n", test_name, n1, hash_get(map, "n1")); return 1; } if (n2 != hash_get(map, "n2")) { printf ("%s: node with label 'n2' should be %p, not %p.\n", test_name, n2, hash_get(map, "n2")); return 1; } if (n3 != hash_get(map, "n3")) { printf ("%s: node with label 'n3' should be %p, not %p.\n", test_name, n3, hash_get(map, "n3")); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_reroot() { const char *test_name = "test_reroot"; struct rooted_tree tree = tree_3(); /* ((A:1,B:1.0)f:2.0,(C:1,(D:1,E:1)g:2)h:3)i; */ struct hash *map = create_label2node_map(tree.nodes_in_order); struct rnode *node_g = hash_get(map, "g"); const char *exp = "((D:1,E:1)g:1,(C:1,(A:1,B:1.0)f:5)h:1);"; reroot_tree(&tree, node_g); const char *obt = to_newick(tree.root); if (strcmp (exp, obt) != 0) { printf ("%s: expected '%s', got '%s'.\n", test_name, exp, obt); return 1; } printf ("%s: ok.\n", test_name); return 0; }