forked from r-lib/cpp11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-protect.cpp
More file actions
50 lines (42 loc) · 1.54 KB
/
test-protect.cpp
File metadata and controls
50 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define CPP11_USE_FMT
#include "cpp11/protect.hpp"
#include "testthat.h"
context("unwind_protect-C++") {
test_that("unwind_protect works if there is no error") {
SEXP out = PROTECT(cpp11::unwind_protect([&] {
out = PROTECT(Rf_allocVector(REALSXP, 1));
REAL(out)[0] = 1;
UNPROTECT(1);
return out;
}));
expect_true(Rf_xlength(out) == 1);
expect_true(REAL(out)[0] == 1);
UNPROTECT(1);
}
test_that("unwind_protect throws a C++ exception if there is an R error") {
SEXP out;
expect_error_as(cpp11::unwind_protect([&] {
out = PROTECT(Rf_allocVector(REALSXP, -1));
REAL(out)[0] = 1;
UNPROTECT(1);
return out;
}),
cpp11::unwind_exception);
}
test_that("safe wraps R functions and works if there is no error") {
SEXP out = PROTECT(cpp11::safe[Rf_allocVector](REALSXP, 1));
REAL(out)[0] = 1;
expect_true(Rf_xlength(out) == 1);
expect_true(REAL(out)[0] == 1);
UNPROTECT(1);
}
test_that("stop throws an unwind_exception") {
expect_error_as(cpp11::stop("error"), cpp11::unwind_exception);
expect_error_as(cpp11::stop("error {}", "message"), cpp11::unwind_exception);
expect_error_as(cpp11::stop("error {a}", fmt::arg("a", "message")),
cpp11::unwind_exception);
}
test_that("safe wraps R functions and works if there is an R error") {
expect_error_as(cpp11::safe[Rf_allocVector](REALSXP, -1), cpp11::unwind_exception);
}
}