29
29
#include <unistd.h>
30
30
#include <stdlib.h>
31
31
32
+ static void
33
+ reverse_migrations (tsk_table_collection_t * tables )
34
+ {
35
+ int ret ;
36
+ tsk_migration_table_t migrations ;
37
+ tsk_migration_t migration ;
38
+ tsk_id_t j ;
39
+
40
+ /* Easy way to copy the metadata schema */
41
+ ret = tsk_migration_table_copy (& tables -> migrations , & migrations , 0 );
42
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
43
+ ret = tsk_migration_table_clear (& migrations );
44
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
45
+
46
+ for (j = (tsk_id_t ) tables -> migrations .num_rows - 1 ; j >= 0 ; j -- ) {
47
+ ret = tsk_migration_table_get_row (& tables -> migrations , j , & migration );
48
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
49
+ ret = tsk_migration_table_add_row (& migrations , migration .left , migration .right ,
50
+ migration .node , migration .source , migration .dest , migration .time ,
51
+ migration .metadata , migration .metadata_length );
52
+ CU_ASSERT_FATAL (ret >= 0 );
53
+ }
54
+
55
+ ret = tsk_migration_table_copy (& migrations , & tables -> migrations , TSK_NO_INIT );
56
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
57
+
58
+ tsk_migration_table_free (& migrations );
59
+ }
60
+
32
61
static void
33
62
reverse_edges (tsk_table_collection_t * tables )
34
63
{
@@ -37,7 +66,10 @@ reverse_edges(tsk_table_collection_t *tables)
37
66
tsk_edge_t edge ;
38
67
tsk_id_t j ;
39
68
40
- ret = tsk_edge_table_init (& edges , tables -> edges .options );
69
+ /* Easy way to copy the metadata schema */
70
+ ret = tsk_edge_table_copy (& tables -> edges , & edges , 0 );
71
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
72
+ ret = tsk_edge_table_clear (& edges );
41
73
CU_ASSERT_EQUAL_FATAL (ret , 0 );
42
74
43
75
for (j = (tsk_id_t ) tables -> edges .num_rows - 1 ; j >= 0 ; j -- ) {
@@ -3756,13 +3788,8 @@ test_sort_tables_offsets(void)
3756
3788
ret = tsk_treeseq_copy_tables (ts , & tables , 0 );
3757
3789
CU_ASSERT_EQUAL_FATAL (ret , 0 );
3758
3790
3759
- ret = tsk_table_collection_sort (& tables , NULL , 0 );
3760
- CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_SORT_MIGRATIONS_NOT_SUPPORTED );
3761
-
3762
- tsk_migration_table_clear (& tables .migrations );
3763
3791
ret = tsk_table_collection_sort (& tables , NULL , 0 );
3764
3792
CU_ASSERT_EQUAL_FATAL (ret , 0 );
3765
-
3766
3793
/* Check that setting edge offset = len(edges) does nothing */
3767
3794
reverse_edges (& tables );
3768
3795
ret = tsk_table_collection_copy (& tables , & copy , 0 );
@@ -3773,6 +3800,18 @@ test_sort_tables_offsets(void)
3773
3800
CU_ASSERT_EQUAL_FATAL (ret , 0 );
3774
3801
CU_ASSERT_FATAL (tsk_table_collection_equals (& tables , & copy , 0 ));
3775
3802
3803
+ ret = tsk_table_collection_sort (& tables , NULL , 0 );
3804
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3805
+ /* Check that setting migration offset = len(migrations) does nothing */
3806
+ reverse_migrations (& tables );
3807
+ ret = tsk_table_collection_copy (& tables , & copy , TSK_NO_INIT );
3808
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3809
+ memset (& bookmark , 0 , sizeof (bookmark ));
3810
+ bookmark .migrations = tables .migrations .num_rows ;
3811
+ ret = tsk_table_collection_sort (& tables , & bookmark , 0 );
3812
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
3813
+ CU_ASSERT_FATAL (tsk_table_collection_equals (& tables , & copy , 0 ));
3814
+
3776
3815
ret = tsk_table_collection_sort (& tables , NULL , 0 );
3777
3816
CU_ASSERT_EQUAL_FATAL (ret , 0 );
3778
3817
CU_ASSERT_FATAL (tables .sites .num_rows > 2 );
@@ -3942,6 +3981,15 @@ test_sort_tables_errors(void)
3942
3981
ret = tsk_table_collection_sort (& tables , & pos , 0 );
3943
3982
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_EDGE_OUT_OF_BOUNDS );
3944
3983
3984
+ memset (& pos , 0 , sizeof (pos ));
3985
+ pos .migrations = (tsk_size_t ) - 1 ;
3986
+ ret = tsk_table_collection_sort (& tables , & pos , 0 );
3987
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_MIGRATION_OUT_OF_BOUNDS );
3988
+
3989
+ pos .migrations = tables .migrations .num_rows + 1 ;
3990
+ ret = tsk_table_collection_sort (& tables , & pos , 0 );
3991
+ CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_MIGRATION_OUT_OF_BOUNDS );
3992
+
3945
3993
/* Individual, node, population and provenance positions are ignored */
3946
3994
memset (& pos , 0 , sizeof (pos ));
3947
3995
pos .individuals = 1 ;
@@ -3963,13 +4011,8 @@ test_sort_tables_errors(void)
3963
4011
ret = tsk_table_collection_sort (& tables , & pos , 0 );
3964
4012
CU_ASSERT_EQUAL_FATAL (ret , 0 );
3965
4013
3966
- /* Setting migrations, sites or mutations gives a BAD_PARAM. See
4014
+ /* Setting sites or mutations gives a BAD_PARAM. See
3967
4015
* github.com/tskit-dev/tskit/issues/101 */
3968
- memset (& pos , 0 , sizeof (pos ));
3969
- pos .migrations = 1 ;
3970
- ret = tsk_table_collection_sort (& tables , & pos , 0 );
3971
- CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_MIGRATIONS_NOT_SUPPORTED );
3972
-
3973
4016
memset (& pos , 0 , sizeof (pos ));
3974
4017
pos .sites = 1 ;
3975
4018
ret = tsk_table_collection_sort (& tables , & pos , 0 );
@@ -3980,12 +4023,6 @@ test_sort_tables_errors(void)
3980
4023
ret = tsk_table_collection_sort (& tables , & pos , 0 );
3981
4024
CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_SORT_OFFSET_NOT_SUPPORTED );
3982
4025
3983
- /* Migrations are not supported */
3984
- tsk_migration_table_add_row (& tables .migrations , 0 , 1 , 0 , 0 , 0 , 0 , NULL , 0 );
3985
- CU_ASSERT_EQUAL_FATAL (tables .migrations .num_rows , 1 );
3986
- ret = tsk_table_collection_sort (& tables , NULL , 0 );
3987
- CU_ASSERT_EQUAL_FATAL (ret , TSK_ERR_SORT_MIGRATIONS_NOT_SUPPORTED );
3988
-
3989
4026
tsk_table_collection_free (& tables );
3990
4027
tsk_treeseq_free (& ts );
3991
4028
}
@@ -4179,6 +4216,51 @@ test_sort_tables_canonical(void)
4179
4216
tsk_table_collection_free (& t1 );
4180
4217
}
4181
4218
4219
+ static void
4220
+ test_sort_tables_migrations (void )
4221
+ {
4222
+ int ret ;
4223
+ tsk_treeseq_t * ts ;
4224
+ tsk_table_collection_t tables , copy ;
4225
+
4226
+ ts = caterpillar_tree (13 , 1 , 1 );
4227
+ ret = tsk_treeseq_copy_tables (ts , & tables , 0 );
4228
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
4229
+ CU_ASSERT_FATAL (tables .migrations .num_rows > 0 );
4230
+
4231
+ ret = tsk_table_collection_copy (& tables , & copy , 0 );
4232
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
4233
+ CU_ASSERT_FATAL (tsk_table_collection_equals (& tables , & copy , 0 ));
4234
+
4235
+ reverse_migrations (& tables );
4236
+ CU_ASSERT_FATAL (!tsk_table_collection_equals (& tables , & copy , 0 ));
4237
+ ret = tsk_table_collection_sort (& tables , NULL , 0 );
4238
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
4239
+ CU_ASSERT_FATAL (tsk_migration_table_equals (& tables .migrations , & copy .migrations , 0 ));
4240
+ CU_ASSERT_FATAL (tsk_table_collection_equals (& tables , & copy , 0 ));
4241
+
4242
+ /* Make sure we test the deeper comparison keys. The full key is
4243
+ * (time, source, dest, left, node) */
4244
+ tsk_migration_table_clear (& tables .migrations );
4245
+
4246
+ /* params = left, right, node, source, dest, time */
4247
+ tsk_migration_table_add_row (& tables .migrations , 0 , 1 , 0 , 0 , 1 , 0 , NULL , 0 );
4248
+ tsk_migration_table_add_row (& tables .migrations , 0 , 1 , 1 , 0 , 1 , 0 , NULL , 0 );
4249
+ ret = tsk_migration_table_copy (& tables .migrations , & copy .migrations , TSK_NO_INIT );
4250
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
4251
+
4252
+ reverse_migrations (& tables );
4253
+ CU_ASSERT_FATAL (!tsk_table_collection_equals (& tables , & copy , 0 ));
4254
+ ret = tsk_table_collection_sort (& tables , NULL , 0 );
4255
+ CU_ASSERT_EQUAL_FATAL (ret , 0 );
4256
+ CU_ASSERT_FATAL (tsk_migration_table_equals (& tables .migrations , & copy .migrations , 0 ));
4257
+
4258
+ tsk_table_collection_free (& tables );
4259
+ tsk_table_collection_free (& copy );
4260
+ tsk_treeseq_free (ts );
4261
+ free (ts );
4262
+ }
4263
+
4182
4264
static void
4183
4265
test_sorter_interface (void )
4184
4266
{
@@ -5942,6 +6024,7 @@ main(int argc, char **argv)
5942
6024
{ "test_sort_tables_edge_metadata" , test_sort_tables_edge_metadata },
5943
6025
{ "test_sort_tables_no_edge_metadata" , test_sort_tables_no_edge_metadata },
5944
6026
{ "test_sort_tables_mutation_times" , test_sort_tables_mutation_times },
6027
+ { "test_sort_tables_migrations" , test_sort_tables_migrations },
5945
6028
{ "test_edge_update_invalidates_index" , test_edge_update_invalidates_index },
5946
6029
{ "test_copy_table_collection" , test_copy_table_collection },
5947
6030
{ "test_sort_tables_errors" , test_sort_tables_errors },
0 commit comments