Skip to content
Closed
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
64 changes: 64 additions & 0 deletions cpp11test/R/cpp11.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ cpp11_insert_ <- function(num_sxp) {
.Call(`_cpp11test_cpp11_insert_`, num_sxp)
}

ordered_map_to_list_ <- function(x) {
.Call(`_cpp11test_ordered_map_to_list_`, x)
}

unordered_map_to_list_ <- function(x) {
.Call(`_cpp11test_unordered_map_to_list_`, x)
}

gibbs_cpp <- function(N, thin) {
.Call(`_cpp11test_gibbs_cpp`, N, thin)
}
Expand Down Expand Up @@ -156,6 +164,34 @@ rcpp_release_ <- function(n) {
invisible(.Call(`_cpp11test_rcpp_release_`, n))
}

notroxcpp1_ <- function(x) {
.Call(`_cpp11test_notroxcpp1_`, x)
}

roxcpp2_ <- function(x) {
.Call(`_cpp11test_roxcpp2_`, x)
}

roxcpp3_ <- function(x) {
.Call(`_cpp11test_roxcpp3_`, x)
}

roxcpp4_ <- function(x) {
.Call(`_cpp11test_roxcpp4_`, x)
}

roxcpp5_ <- function(x) {
.Call(`_cpp11test_roxcpp5_`, x)
}

notroxcpp6_ <- function(x) {
.Call(`_cpp11test_notroxcpp6_`, x)
}

roxcpp7_ <- function(x) {
.Call(`_cpp11test_roxcpp7_`, x)
}

cpp11_safe_ <- function(x_sxp) {
.Call(`_cpp11test_cpp11_safe_`, x_sxp)
}
Expand All @@ -168,6 +204,26 @@ string_push_back_ <- function() {
.Call(`_cpp11test_string_push_back_`)
}

grow_strings_cpp11_ <- function(n, seed) {
.Call(`_cpp11test_grow_strings_cpp11_`, n, seed)
}

grow_strings_rcpp_ <- function(n, seed) {
.Call(`_cpp11test_grow_strings_rcpp_`, n, seed)
}

grow_strings_manual_ <- function(n, seed) {
.Call(`_cpp11test_grow_strings_manual_`, n, seed)
}

assign_cpp11_ <- function(n, seed) {
.Call(`_cpp11test_assign_cpp11_`, n, seed)
}

assign_rcpp_ <- function(n, seed) {
.Call(`_cpp11test_assign_rcpp_`, n, seed)
}

sum_dbl_for_ <- function(x) {
.Call(`_cpp11test_sum_dbl_for_`, x)
}
Expand Down Expand Up @@ -236,6 +292,14 @@ rcpp_push_and_truncate_ <- function(size_sxp) {
.Call(`_cpp11test_rcpp_push_and_truncate_`, size_sxp)
}

nullable_extptr_1 <- function() {
.Call(`_cpp11test_nullable_extptr_1`)
}

nullable_extptr_2 <- function() {
.Call(`_cpp11test_nullable_extptr_2`)
}

test_destruction_inner <- function() {
invisible(.Call(`_cpp11test_test_destruction_inner`))
}
Expand Down
17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp2_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp3_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp4_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions cpp11test/man/roxcpp5_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions cpp11test/man/roxcpp7_.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions cpp11test/src/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ extern "C" SEXP _cpp11test_cpp11_insert_(SEXP num_sxp) {
return cpp11::as_sexp(cpp11_insert_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(num_sxp)));
END_CPP11
}
// map.cpp
SEXP ordered_map_to_list_(cpp11::doubles x);
extern "C" SEXP _cpp11test_ordered_map_to_list_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(ordered_map_to_list_(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles>>(x)));
END_CPP11
}
// map.cpp
SEXP unordered_map_to_list_(cpp11::doubles x);
extern "C" SEXP _cpp11test_unordered_map_to_list_(SEXP x) {
BEGIN_CPP11
return cpp11::as_sexp(unordered_map_to_list_(cpp11::as_cpp<cpp11::decay_t<cpp11::doubles>>(x)));
END_CPP11
}
// matrix.cpp
SEXP gibbs_cpp(int N, int thin);
extern "C" SEXP _cpp11test_gibbs_cpp(SEXP N, SEXP thin) {
Expand Down Expand Up @@ -500,6 +514,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11test_my_warning_n1", (DL_FUNC) &_cpp11test_my_warning_n1, 1},
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
{"_cpp11test_ordered_map_to_list_", (DL_FUNC) &_cpp11test_ordered_map_to_list_, 1},
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},
Expand Down Expand Up @@ -533,6 +548,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_cpp11test_sum_int_foreach_", (DL_FUNC) &_cpp11test_sum_int_foreach_, 1},
{"_cpp11test_test_destruction_inner", (DL_FUNC) &_cpp11test_test_destruction_inner, 0},
{"_cpp11test_test_destruction_outer", (DL_FUNC) &_cpp11test_test_destruction_outer, 0},
{"_cpp11test_unordered_map_to_list_", (DL_FUNC) &_cpp11test_unordered_map_to_list_, 1},
{"_cpp11test_upper_bound", (DL_FUNC) &_cpp11test_upper_bound, 2},
{"run_testthat_tests", (DL_FUNC) &run_testthat_tests, 1},
{NULL, NULL, 0}
Expand Down
20 changes: 20 additions & 0 deletions cpp11test/src/map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "cpp11/as.hpp"
#include "cpp11/doubles.hpp"

[[cpp11::register]] SEXP ordered_map_to_list_(cpp11::doubles x) {
std::map<double, int> counts;
int n = x.size();
for (int i = 0; i < n; i++) {
counts[x[i]]++;
}
return cpp11::as_sexp(counts);
}

[[cpp11::register]] SEXP unordered_map_to_list_(cpp11::doubles x) {
std::unordered_map<double, int> counts;
int n = x.size();
for (int i = 0; i < n; i++) {
counts[x[i]]++;
}
return cpp11::as_sexp(counts);
}
22 changes: 22 additions & 0 deletions cpp11test/src/roxygen1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "cpp11/doubles.hpp"
using namespace cpp11;

// Test: not documented + documented

// Not Roxygenised C++ function I
[[cpp11::register]] double notroxcpp1_(double x) {
double y = x + 1.0;
return y;
}

/* roxygen start
@title Roxygenise C++ function II
@param x numeric value
@description Dummy function to test roxygen2. It adds 2.0 to a double.
@export
@examples roxcpp2_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp2_(double x) {
double y = x + 2.0;
return y;
}
28 changes: 28 additions & 0 deletions cpp11test/src/roxygen2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "cpp11/doubles.hpp"
using namespace cpp11;

// Test: documented + documented

/* roxygen start
@title Roxygenise C++ function III
@param x numeric value
@description Dummy function to test roxygen2. It adds 3.0 to a double.
@export
@examples roxcpp3_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp3_(double x) {
double y = x + 3.0;
return y;
}

/* roxygen start
@title Roxygenise C++ function IV
@param x numeric value
@description Dummy function to test roxygen2. It adds 4.0 to a double.
@export
@examples roxcpp4_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp4_(double x) {
double y = x + 4.0;
return y;
}
38 changes: 38 additions & 0 deletions cpp11test/src/roxygen3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "cpp11/doubles.hpp"
using namespace cpp11;

// Test: documented + not documented + documented

/* roxygen start
@title Roxygenise C++ function V
@param x numeric value
@description Dummy function to test roxygen2. It adds 5.0 to a double.
@export
@examples roxcpp5_(1.0)
roxygen end */
[[cpp11::register]] double roxcpp5_(double x) {
double y = x + 5.0;
return y;
}

// Not Roxygenised C++ function VI
[[cpp11::register]] double notroxcpp6_(double x) {
double y = x + 6.0;
return y;
}

/* roxygen start
@title Roxygenise C++ function VII
@param x numeric value
@description Dummy function to test roxygen2. It adds 7.0 to a double.
@export
@examples
my_fun <- function(x) {
roxcpp7_(x)
}
@seealso \code{\link{roxcpp1_}}
roxygen end */
[[cpp11::register]] double roxcpp7_(double x) {
double y = x + 7.0;
return y;
}
18 changes: 18 additions & 0 deletions cpp11test/tests/testthat/test-map-to-list.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
test_that("ordered and unordered C++ maps are converted to R lists", {
set.seed(42L)
x <- rnorm(10L)
xprime <- c(x, x[1])

om <- ordered_map_to_list_(x)
expect_type(om, "list")

om_doubles <- as.double(names(om))
expect_equal(om_doubles, sort(om_doubles))

omprime <- ordered_map_to_list_(xprime)
expect_equal(unlist(unique(omprime)), 1:2)

um <- unordered_map_to_list_(xprime)
expect_type(um, "list")
expect_equal(unlist(unique(um)), 1:2)
})
Loading
Loading