Skip to content

Commit

Permalink
Use a test helper to define the expected C++ error class (fixes #15)
Browse files Browse the repository at this point in the history
* This works around the fact that, in certain circumstances, we cannot rely on errors propagating with proper `std::invalid_argument` class / error message. See also RcppCore/Rcpp#972
  • Loading branch information
riccardoporreca committed Jan 26, 2021
1 parent 6a0e4d2 commit b292e3a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
34 changes: 34 additions & 0 deletions tests/testthat/helper-invalid_argument.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Work around the fact that, in certain circumstances (architectures /
# compilers), we cannot rely on errors propagating with proper
# `std::invalid_argument` class / error message.
#
# See https://github.com/miraisolutions/rTRNG/issues/15,
# https://github.com/RcppCore/Rcpp/issues/972
#
# Approach: Use a minimal example to test whether "std::invalid_argument" is
# correctly detected, otherwise just fall-back on "error" (the "c++ exception
# (unknown reason)" error inherits from it).

invalid_argument_thrower <- cppFunction('
void invalid_argument_thrower() {
throw std::invalid_argument("Invalid argument test");
}
')

invalid_argument_error_class <- function() {
error_class <- tryCatch(
invalid_argument_thrower(),
error = function(e) class(e)
)
error_class
}

supported_invalid_argument_class <- function() {
if ("std::invalid_argument" %in% invalid_argument_error_class()) {
"std::invalid_argument"
} else {
"error"
}
}

expected_invalid_argument_class <- supported_invalid_argument_class()
4 changes: 2 additions & 2 deletions tests/testthat/test-TRNG.Engine.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ test_that("$split errors for out-of-range subsequence indices", {
p <- 5L
if (!grepl("(lagfib|mt)", engineClass)) {
expect_error(
e$split(p, 0L), class = "std::invalid_argument", # 1-base indexing
e$split(p, 0L), class = expected_invalid_argument_class, # 1-base indexing
info = .name(engineClass)
)
expect_error(
e$split(p, p + 1L), class = "std::invalid_argument",
e$split(p, p + 1L), class = expected_invalid_argument_class,
info = .name(engineClass)
)
expect_error(
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-TRNG.Random.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ test_that("TRNGsplit errors for out-of-range subsequence indices", {
TRNGkind(KIND)
TRNGseed(SEED)
p <- 5L
expect_error(TRNGsplit(p, 0L), class = "std::invalid_argument") # 1-base indexing
expect_error(TRNGsplit(p, p + 1L), class = "std::invalid_argument")
expect_error(TRNGsplit(p, 0L), class = expected_invalid_argument_class) # 1-base indexing
expect_error(TRNGsplit(p, p + 1L), class = expected_invalid_argument_class)
expect_error(TRNGsplit(p, -1L), "negative")
expect_error(TRNGsplit(-1L, 1L), "negative")
})

0 comments on commit b292e3a

Please sign in to comment.