Skip to content

Commit

Permalink
Input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ms609 committed Sep 15, 2022
1 parent c61c92f commit 847a10d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
13 changes: 11 additions & 2 deletions src/int_to_tree.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <Rcpp/Lightest>
#include <Rcpp/Lighter> /* for is_na */
#include <random>
#include <stdexcept> /* for errors */
#include "../inst/include/TreeTools.h"
Expand All @@ -9,8 +9,17 @@ const intx MB_MAX_TIP = 32768, MB_MAX_NODE = MB_MAX_TIP + MB_MAX_TIP - 1;

// [[Rcpp::export]]
IntegerVector num_to_parent(const IntegerVector n, const IntegerVector nTip) {
if (Rcpp::is_true(Rcpp::any(Rcpp::is_na(n)))) {
Rcpp::stop("`n` may not contain NA values");
}
if (Rcpp::is_true(Rcpp::any(n < 0))) {
Rcpp::stop("`n` may not be negative");
}
if (nTip[0] < 2) {
Rcpp::stop("nTip must be > 1");
Rcpp::stop("`nTip` must be > 1");
}
if (nTip.length() > 1) {
Rcpp::warning("`nTip` should be a single integer");
}
const intx
n_tip = nTip[0],
Expand Down
24 changes: 15 additions & 9 deletions tests/testthat/test-int_to_tree.cpp.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
test_that("Failures are graceful", {
expect_error(num_to_parent(10, 1))
expect_error(num_to_parent(10, -1))
expect_error(mixed_base_to_parent(10, 1))
expect_error(mixed_base_to_parent(10, -1))
expect_error(edge_to_num(1:10, 1:11, 6))
expect_error(edge_to_num(1:10, 1:10, 5))
expect_error(edge_to_mixed_base(1:10, 1:11, 6))
expect_error(edge_to_mixed_base(1:10, 1:10, 5))
expect_error(as.phylo(0, 0))
expect_error(num_to_parent(10, 1), "`nTip` must be > 1")
expect_error(num_to_parent(10, -1), "`nTip` must be > 1")
expect_warning(num_to_parent(10, c(2, 3)),
"`nTip` should be a single integer")
expect_error(num_to_parent(NA, 10), "`n` may not contain NA")
expect_error(num_to_parent(-1, 10), "`n` may not be negative")
expect_error(mixed_base_to_parent(10, 1), "nTip must be > 1")
expect_error(mixed_base_to_parent(10, -1), "nTip must be > 1")
expect_error(edge_to_num(1:10, 1:11, 6), "Parent and child .* same length")
expect_error(edge_to_num(1:10, 1:10, 5), "nEdge must == nTip . nTip - 2")
expect_error(edge_to_mixed_base(1:10, 1:11, 6),
"Parent and child .* same length")
expect_error(edge_to_mixed_base(1:10, 1:10, 5),
"nEdge must == nTip . nTip - 2")
expect_error(as.phylo(0, 0), "nTip must be > 1")
})

test_that("Edge cases handled", {
Expand Down

0 comments on commit 847a10d

Please sign in to comment.