Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a test helper to define the expected C++ error class (fixes #15) #17

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})