Skip to content

Commit d577744

Browse files
committed
Address review comments
1 parent 7b33b49 commit d577744

7 files changed

Lines changed: 119 additions & 9 deletions

File tree

RcppTskit/NEWS.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ and releases adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html
3535
to append node rows from \code{R}, mirroring `tsk_node_table_add_row()`.
3636
- Added `rtsk_edge_table_add_row()` and `TableCollection$edge_table_add_row()`
3737
to append edge rows from \code{R}, mirroring `tsk_edge_table_add_row()`.
38-
- `TableCollection$node_table_add_row()` now maps `population = NULL` and
39-
`individual = NULL` to `-1` (`TSK_NULL`) for R-level convenience.
40-
- `rtsk_edge_table_add_row()` and `TableCollection$edge_table_add_row()` now
41-
validate `left`/`right` more explicitly (non-`NA`, finite, and `left < right`)
42-
and require scalar `parent`/`child` IDs.
4338
- TODO
4439

4540
### Changed

RcppTskit/R/Class-TableCollection.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ TableCollection <- R6Class(
213213
#' a raw vector, or a character of length 1.
214214
#' @details See the \code{tskit Python} equivalent at
215215
#' \url{https://tskit.dev/tskit/docs/stable/python-api.html#tskit.NodeTable.add_row}.
216-
#' The function casts inputs to the expected class.
216+
#' The function casts inputs to the expected class. For convenience,
217+
#' \code{population = NULL} and \code{individual = NULL} are mapped to
218+
#' \code{-1} (\code{TSK_NULL}).
217219
#' @return Integer row ID (0-based) of the newly added node.
218220
#' @examples
219221
#' ts_file <- system.file("examples/test.trees", package = "RcppTskit")
@@ -275,7 +277,10 @@ TableCollection <- R6Class(
275277
#' a raw vector, or a character of length 1.
276278
#' @details See the \code{tskit Python} equivalent at
277279
#' \url{https://tskit.dev/tskit/docs/stable/python-api.html#tskit.EdgeTable.add_row}.
278-
#' The function casts inputs to the expected class.
280+
#' The function casts inputs to the expected class. Inputs are validated:
281+
#' \code{left} and \code{right} must be finite numeric scalars with
282+
#' \code{left < right}, and \code{parent} and \code{child} must be
283+
#' non-\code{NA} integer scalars.
279284
#' @return Integer row ID (0-based) of the newly added edge.
280285
#' @examples
281286
#' ts_file <- system.file("examples/test.trees", package = "RcppTskit")

RcppTskit/R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,11 @@ test_rtsk_individual_table_add_row_forced_error <- function(tc) {
251251
invisible(.Call(`_RcppTskit_test_rtsk_individual_table_add_row_forced_error`, tc))
252252
}
253253

254+
test_rtsk_node_table_add_row_forced_error <- function(tc) {
255+
invisible(.Call(`_RcppTskit_test_rtsk_node_table_add_row_forced_error`, tc))
256+
}
257+
258+
test_rtsk_edge_table_add_row_forced_error <- function(tc) {
259+
invisible(.Call(`_RcppTskit_test_rtsk_edge_table_add_row_forced_error`, tc))
260+
}
261+

RcppTskit/man/TableCollection.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RcppTskit/src/RcppExports.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,26 @@ BEGIN_RCPP
669669
return R_NilValue;
670670
END_RCPP
671671
}
672+
// test_rtsk_node_table_add_row_forced_error
673+
void test_rtsk_node_table_add_row_forced_error(const SEXP tc);
674+
RcppExport SEXP _RcppTskit_test_rtsk_node_table_add_row_forced_error(SEXP tcSEXP) {
675+
BEGIN_RCPP
676+
Rcpp::RNGScope rcpp_rngScope_gen;
677+
Rcpp::traits::input_parameter< const SEXP >::type tc(tcSEXP);
678+
test_rtsk_node_table_add_row_forced_error(tc);
679+
return R_NilValue;
680+
END_RCPP
681+
}
682+
// test_rtsk_edge_table_add_row_forced_error
683+
void test_rtsk_edge_table_add_row_forced_error(const SEXP tc);
684+
RcppExport SEXP _RcppTskit_test_rtsk_edge_table_add_row_forced_error(SEXP tcSEXP) {
685+
BEGIN_RCPP
686+
Rcpp::RNGScope rcpp_rngScope_gen;
687+
Rcpp::traits::input_parameter< const SEXP >::type tc(tcSEXP);
688+
test_rtsk_edge_table_add_row_forced_error(tc);
689+
return R_NilValue;
690+
END_RCPP
691+
}
672692

673693
static const R_CallMethodDef CallEntries[] = {
674694
{"_RcppTskit_test_validate_options", (DL_FUNC) &_RcppTskit_test_validate_options, 2},
@@ -730,6 +750,8 @@ static const R_CallMethodDef CallEntries[] = {
730750
{"_RcppTskit_test_rtsk_treeseq_init_forced_error", (DL_FUNC) &_RcppTskit_test_rtsk_treeseq_init_forced_error, 1},
731751
{"_RcppTskit_test_rtsk_table_collection_build_index_forced_error", (DL_FUNC) &_RcppTskit_test_rtsk_table_collection_build_index_forced_error, 1},
732752
{"_RcppTskit_test_rtsk_individual_table_add_row_forced_error", (DL_FUNC) &_RcppTskit_test_rtsk_individual_table_add_row_forced_error, 1},
753+
{"_RcppTskit_test_rtsk_node_table_add_row_forced_error", (DL_FUNC) &_RcppTskit_test_rtsk_node_table_add_row_forced_error, 1},
754+
{"_RcppTskit_test_rtsk_edge_table_add_row_forced_error", (DL_FUNC) &_RcppTskit_test_rtsk_edge_table_add_row_forced_error, 1},
733755
{NULL, NULL, 0}
734756
};
735757

RcppTskit/src/tests.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,60 @@ void test_rtsk_individual_table_add_row_forced_error(const SEXP tc) {
149149
throw;
150150
}
151151
}
152+
153+
// TEST-ONLY
154+
// @title Force tskit-level error path in \code{rtsk_node_table_add_row}
155+
// @param tc an external pointer to table collection as a
156+
// \code{tsk_table_collection_t} object.
157+
// @return No return value; called for side effects - testing.
158+
// [[Rcpp::export]]
159+
void test_rtsk_node_table_add_row_forced_error(const SEXP tc) {
160+
rtsk_table_collection_t tc_xptr(tc);
161+
tsk_node_table_t &nodes = tc_xptr->nodes;
162+
tsk_size_t saved_max_rows = nodes.max_rows;
163+
tsk_size_t saved_max_rows_increment = nodes.max_rows_increment;
164+
nodes.max_rows = 1;
165+
nodes.max_rows_increment = static_cast<tsk_size_t>(TSK_MAX_ID) + 1;
166+
try {
167+
(void)rtsk_node_table_add_row(tc);
168+
// Lines below not hit by tests because rtsk_node_table_add_row()
169+
// throws error # nocov start
170+
nodes.max_rows = saved_max_rows;
171+
nodes.max_rows_increment = saved_max_rows_increment;
172+
return;
173+
// # nocov end
174+
} catch (...) {
175+
nodes.max_rows = saved_max_rows;
176+
nodes.max_rows_increment = saved_max_rows_increment;
177+
throw;
178+
}
179+
}
180+
181+
// TEST-ONLY
182+
// @title Force tskit-level error path in \code{rtsk_edge_table_add_row}
183+
// @param tc an external pointer to table collection as a
184+
// \code{tsk_table_collection_t} object.
185+
// @return No return value; called for side effects - testing.
186+
// [[Rcpp::export]]
187+
void test_rtsk_edge_table_add_row_forced_error(const SEXP tc) {
188+
rtsk_table_collection_t tc_xptr(tc);
189+
tsk_edge_table_t &edges = tc_xptr->edges;
190+
tsk_size_t saved_max_rows = edges.max_rows;
191+
tsk_size_t saved_max_rows_increment = edges.max_rows_increment;
192+
edges.max_rows = 1;
193+
edges.max_rows_increment = static_cast<tsk_size_t>(TSK_MAX_ID) + 1;
194+
try {
195+
(void)rtsk_edge_table_add_row(tc, 0, 1, static_cast<int>(edges.parent[0]),
196+
static_cast<int>(edges.child[0]), R_NilValue);
197+
// Lines below not hit by tests because rtsk_edge_table_add_row()
198+
// throws error # nocov start
199+
edges.max_rows = saved_max_rows;
200+
edges.max_rows_increment = saved_max_rows_increment;
201+
return;
202+
// # nocov end
203+
} catch (...) {
204+
edges.max_rows = saved_max_rows;
205+
edges.max_rows_increment = saved_max_rows_increment;
206+
throw;
207+
}
208+
}

RcppTskit/tests/testthat/test_TableCollection.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,10 @@ test_that("node_table_add_row wrapper expands the table collection and handles i
549549
tc$node_table_add_row(metadata = 1L),
550550
regexp = "metadata must be NULL, a raw vector, or a length-1 non-NA character string!"
551551
)
552+
expect_error(
553+
test_rtsk_node_table_add_row_forced_error(tc$xptr),
554+
regexp = "TSK_ERR_TABLE_OVERFLOW"
555+
)
552556
})
553557

554558
test_that("edge_table_add_row wrapper expands the table collection and handles inputs", {
@@ -649,6 +653,16 @@ test_that("edge_table_add_row wrapper expands the table collection and handles i
649653
),
650654
regexp = "left must be finite in rtsk_edge_table_add_row"
651655
)
656+
expect_error(
657+
rtsk_edge_table_add_row(
658+
tc = tc_xptr,
659+
left = 0,
660+
right = Inf,
661+
parent = parent,
662+
child = child
663+
),
664+
regexp = "right must be finite in rtsk_edge_table_add_row"
665+
)
652666
expect_error(
653667
rtsk_edge_table_add_row(
654668
tc = tc_xptr,
@@ -827,4 +841,8 @@ test_that("edge_table_add_row wrapper expands the table collection and handles i
827841
),
828842
regexp = "metadata must be NULL, a raw vector, or a length-1 non-NA character string!"
829843
)
844+
expect_error(
845+
test_rtsk_edge_table_add_row_forced_error(tc$xptr),
846+
regexp = "TSK_ERR_TABLE_OVERFLOW"
847+
)
830848
})

0 commit comments

Comments
 (0)