forked from r-lib/cpp11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-function.cpp
More file actions
43 lines (31 loc) · 1.18 KB
/
test-function.cpp
File metadata and controls
43 lines (31 loc) · 1.18 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
#include "cpp11/function.hpp"
#include <testthat.h>
context("function-C++") {
test_that("functions can be called") {
auto median = cpp11::package("stats")["median"];
double res = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), true);
expect_true(res == 2.);
double res2 = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), false);
expect_true(ISNAN(res2));
}
test_that("functions can be called with named arguments") {
using namespace cpp11::literals;
auto median = cpp11::package("stats")["median"];
double res = median("x"_nm = {1., 2., 3., NA_REAL}, "na.rm"_nm = true);
expect_true(res == 2.);
double res2 = median(cpp11::as_sexp({1., 2., 3., NA_REAL}), false);
expect_true(ISNAN(res2));
}
test_that("base functions can be called") {
auto file = cpp11::package("base")["file"];
auto isOpen = cpp11::package("base")["isOpen"];
auto close = cpp11::package("base")["close"];
cpp11::sexp con = file("foo");
bool res = isOpen(con);
expect_true(res == false);
close(con);
}
test_that("unknown packages cause an error (#317)") {
expect_error_as(cpp11::package("definitely_not_a_package"), cpp11::unwind_exception);
}
}