Skip to content

Commit ba83b85

Browse files
committed
Add C tests
1 parent 151ff70 commit ba83b85

2 files changed

Lines changed: 118 additions & 0 deletions

File tree

c/tests/test_tables.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10576,6 +10576,60 @@ test_table_collection_check_integrity_bad_indexes(void)
1057610576
tsk_table_collection_free(&tables);
1057710577
}
1057810578

10579+
static void
10580+
test_check_integrity_bad_mutation_parent_topology(void)
10581+
{
10582+
int ret;
10583+
tsk_table_collection_t tables;
10584+
const char *sites = "0 0\n";
10585+
/* Make a mutation on a parallel branch the parent*/
10586+
const char *bad_mutations = "0 0 1 -1\n"
10587+
"0 1 1 0\n";
10588+
10589+
/* A mutation above is set as child*/
10590+
const char *reverse_mutations = "0 0 1 -1\n"
10591+
"0 4 1 0\n";
10592+
10593+
const char *reverse_sites = "0.5 0\n"
10594+
"0 0\n";
10595+
10596+
ret = tsk_table_collection_init(&tables, 0);
10597+
CU_ASSERT_EQUAL_FATAL(ret, 0);
10598+
10599+
tables.sequence_length = 1;
10600+
parse_nodes(single_tree_ex_nodes, &tables.nodes);
10601+
CU_ASSERT_EQUAL_FATAL(tables.nodes.num_rows, 7);
10602+
parse_edges(single_tree_ex_edges, &tables.edges);
10603+
CU_ASSERT_EQUAL_FATAL(tables.edges.num_rows, 6);
10604+
parse_sites(sites, &tables.sites);
10605+
CU_ASSERT_EQUAL_FATAL(tables.sites.num_rows, 1);
10606+
parse_mutations(bad_mutations, &tables.mutations);
10607+
CU_ASSERT_EQUAL_FATAL(tables.mutations.num_rows, 2);
10608+
tables.sequence_length = 1.0;
10609+
10610+
ret = tsk_table_collection_build_index(&tables, 0);
10611+
CU_ASSERT_EQUAL_FATAL(ret, 0);
10612+
10613+
ret = tsk_table_collection_check_integrity(&tables, TSK_CHECK_TREES);
10614+
CU_ASSERT_EQUAL_FATAL(ret, 1);
10615+
ret = tsk_table_collection_check_integrity(&tables, TSK_CHECK_MUTATION_PARENTS);
10616+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_MUTATION_PARENT);
10617+
10618+
parse_mutations(reverse_mutations, &tables.mutations);
10619+
ret = tsk_table_collection_check_integrity(&tables, TSK_CHECK_TREES);
10620+
CU_ASSERT_EQUAL_FATAL(ret, 1);
10621+
ret = tsk_table_collection_check_integrity(&tables, TSK_CHECK_MUTATION_PARENTS);
10622+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_MUTATION_PARENT_AFTER_CHILD);
10623+
10624+
/* Now check that TSK_CHECK_MUTATION_PARENTS implies TSK_CHECK_TREES
10625+
by triggering an error with reversed sites */
10626+
parse_sites(reverse_sites, &tables.sites);
10627+
ret = tsk_table_collection_check_integrity(&tables, TSK_CHECK_MUTATION_PARENTS);
10628+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_UNSORTED_SITES);
10629+
10630+
tsk_table_collection_free(&tables);
10631+
}
10632+
1057910633
static void
1058010634
test_table_collection_subset_with_options(tsk_flags_t options)
1058110635
{
@@ -11658,6 +11712,8 @@ main(int argc, char **argv)
1165811712
test_table_collection_check_integrity_bad_indexes_example },
1165911713
{ "test_table_collection_check_integrity_bad_indexes",
1166011714
test_table_collection_check_integrity_bad_indexes },
11715+
{ "test_check_integrity_bad_mutation_parent_topology",
11716+
test_check_integrity_bad_mutation_parent_topology },
1166111717
{ "test_table_collection_subset", test_table_collection_subset },
1166211718
{ "test_table_collection_subset_unsorted",
1166311719
test_table_collection_subset_unsorted },

c/tests/test_trees.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8715,6 +8715,67 @@ test_init_take_ownership_no_edge_metadata(void)
87158715
tsk_treeseq_free(&ts);
87168716
}
87178717

8718+
static void
8719+
test_init_compute_mutation_parents(void)
8720+
{
8721+
int ret;
8722+
tsk_table_collection_t *tables, *tables2;
8723+
tsk_treeseq_t ts;
8724+
const char *sites = "0 0\n";
8725+
/* Make a mutation on a parallel branch the parent*/
8726+
const char *bad_mutations = "0 0 1 -1\n"
8727+
"0 1 1 0\n";
8728+
8729+
tables = tsk_malloc(sizeof(tsk_table_collection_t));
8730+
CU_ASSERT_NOT_EQUAL_FATAL(tables, NULL);
8731+
tables2 = tsk_malloc(sizeof(tsk_table_collection_t));
8732+
CU_ASSERT_NOT_EQUAL_FATAL(tables2, NULL);
8733+
8734+
CU_ASSERT_FATAL(tables != NULL);
8735+
ret = tsk_table_collection_init(tables, 0);
8736+
CU_ASSERT_EQUAL_FATAL(ret, 0);
8737+
8738+
tables->sequence_length = 1;
8739+
parse_nodes(single_tree_ex_nodes, &tables->nodes);
8740+
CU_ASSERT_EQUAL_FATAL(tables->nodes.num_rows, 7);
8741+
parse_edges(single_tree_ex_edges, &tables->edges);
8742+
CU_ASSERT_EQUAL_FATAL(tables->edges.num_rows, 6);
8743+
parse_sites(sites, &tables->sites);
8744+
CU_ASSERT_EQUAL_FATAL(tables->sites.num_rows, 1);
8745+
parse_mutations(bad_mutations, &tables->mutations);
8746+
CU_ASSERT_EQUAL_FATAL(tables->mutations.num_rows, 2);
8747+
tables->sequence_length = 1.0;
8748+
ret = tsk_table_collection_copy(tables, tables2, 0);
8749+
CU_ASSERT_EQUAL_FATAL(ret, 0);
8750+
8751+
ret = tsk_treeseq_init(&ts, tables, TSK_TS_INIT_BUILD_INDEXES);
8752+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_MUTATION_PARENT);
8753+
tsk_treeseq_free(&ts);
8754+
8755+
ret = tsk_treeseq_init(
8756+
&ts, tables, TSK_TS_INIT_BUILD_INDEXES | TSK_TS_INIT_COMPUTE_MUTATION_PARENTS);
8757+
CU_ASSERT_EQUAL_FATAL(ret, 0);
8758+
tsk_treeseq_free(&ts);
8759+
8760+
/* When we use take ownership, the check of parents shouldn't overwrite them*/
8761+
ret = tsk_treeseq_init(&ts, tables, TSK_TAKE_OWNERSHIP | TSK_TS_INIT_BUILD_INDEXES);
8762+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_MUTATION_PARENT);
8763+
CU_ASSERT_EQUAL(tables->mutations.parent[0], -1);
8764+
CU_ASSERT_EQUAL(tables->mutations.parent[1], 0);
8765+
tsk_treeseq_free(&ts);
8766+
8767+
/* When we use take ownership and compute, the tables are overwritten*/
8768+
ret = tsk_treeseq_init(&ts, tables2,
8769+
TSK_TAKE_OWNERSHIP | TSK_TS_INIT_BUILD_INDEXES
8770+
| TSK_TS_INIT_COMPUTE_MUTATION_PARENTS);
8771+
CU_ASSERT_EQUAL_FATAL(ret, 0);
8772+
CU_ASSERT_EQUAL(tables2->mutations.parent[0], -1);
8773+
CU_ASSERT_EQUAL(tables2->mutations.parent[1], -1);
8774+
8775+
/* Don't need to free tables as we took ownership */
8776+
tsk_treeseq_free(&ts);
8777+
}
8778+
87188779
int
87198780
main(int argc, char **argv)
87208781
{
@@ -8923,6 +8984,7 @@ main(int argc, char **argv)
89238984
test_extend_haplotypes_conflicting_times },
89248985
{ "test_init_take_ownership_no_edge_metadata",
89258986
test_init_take_ownership_no_edge_metadata },
8987+
{ "test_init_compute_mutation_parents", test_init_compute_mutation_parents },
89268988
{ NULL, NULL },
89278989
};
89288990

0 commit comments

Comments
 (0)