Skip to content

Commit acb1bd0

Browse files
committed
fix sum rcpp error on Unix systems (cpp11test) - replaces r-lib#439
1 parent b63d96c commit acb1bd0

File tree

12 files changed

+31
-304
lines changed

12 files changed

+31
-304
lines changed

cpp11test/DESCRIPTION

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ Authors@R:
1212
Description: Provides a test suite and benchmarking code for the 'cpp11' package.
1313
License: MIT + file LICENSE
1414
Encoding: UTF-8
15-
LinkingTo: cpp11, Rcpp, testthat
16-
Imports: cpp11, Rcpp
15+
LinkingTo: Rcpp, cpp11, testthat
1716
Suggests:
1817
covr,
19-
testthat (>= 3.0.0),
18+
testthat,
2019
pkgload,
2120
xml2
2221
LazyData: true
2322
Roxygen: list(markdown = TRUE)
24-
RoxygenNote: 7.3.2
25-
Config/testthat/edition: 3
23+
RoxygenNote: 7.1.1

cpp11test/NAMESPACE

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3-
export(roxcpp2_)
4-
export(roxcpp3_)
5-
export(roxcpp4_)
6-
export(roxcpp5_)
7-
export(roxcpp7_)
83
export(run_tests)
94
exportPattern("_$")
105
importFrom(Rcpp,sourceCpp)
11-
importFrom(cpp11,cpp_source)
126
useDynLib(cpp11test, .registration = TRUE)

cpp11test/R/cpp11.R

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -160,57 +160,28 @@ notroxcpp1_ <- function(x) {
160160
.Call(`_cpp11test_notroxcpp1_`, x)
161161
}
162162

163-
#' @title Roxygenise C++ function II
164-
#' @param x numeric value
165-
#' @description Dummy function to test roxygen2. It adds 2.0 to a double.
166-
#' @export
167-
#' @examples roxcpp2_(1.0)
168163
roxcpp2_ <- function(x) {
169-
.Call(`_cpp11test_roxcpp2_`, x)
164+
.Call(`_cpp11test_roxcpp2_`, x)
170165
}
171166

172-
#' @title Roxygenise C++ function III
173-
#' @param x numeric value
174-
#' @description Dummy function to test roxygen2. It adds 3.0 to a double.
175-
#' @export
176-
#' @examples roxcpp3_(1.0)
177167
roxcpp3_ <- function(x) {
178-
.Call(`_cpp11test_roxcpp3_`, x)
168+
.Call(`_cpp11test_roxcpp3_`, x)
179169
}
180170

181-
#' @title Roxygenise C++ function IV
182-
#' @param x numeric value
183-
#' @description Dummy function to test roxygen2. It adds 4.0 to a double.
184-
#' @export
185-
#' @examples roxcpp4_(1.0)
186171
roxcpp4_ <- function(x) {
187-
.Call(`_cpp11test_roxcpp4_`, x)
172+
.Call(`_cpp11test_roxcpp4_`, x)
188173
}
189174

190-
#' @title Roxygenise C++ function V
191-
#' @param x numeric value
192-
#' @description Dummy function to test roxygen2. It adds 5.0 to a double.
193-
#' @export
194-
#' @examples roxcpp5_(1.0)
195175
roxcpp5_ <- function(x) {
196-
.Call(`_cpp11test_roxcpp5_`, x)
176+
.Call(`_cpp11test_roxcpp5_`, x)
197177
}
198178

199179
notroxcpp6_ <- function(x) {
200180
.Call(`_cpp11test_notroxcpp6_`, x)
201181
}
202182

203-
#' @title Roxygenise C++ function VII
204-
#' @param x numeric value
205-
#' @description Dummy function to test roxygen2. It adds 7.0 to a double.
206-
#' @export
207-
#' @examples
208-
#' my_fun <- function(x) {
209-
#' roxcpp7_(x)
210-
#' }
211-
#' @seealso \code{\link{roxcpp1_}}
212183
roxcpp7_ <- function(x) {
213-
.Call(`_cpp11test_roxcpp7_`, x)
184+
.Call(`_cpp11test_roxcpp7_`, x)
214185
}
215186

216187
cpp11_safe_ <- function(x_sxp) {

cpp11test/R/cpp11test-package.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#' @keywords internal
22
#' @exportPattern "_$"
3-
#' @importFrom cpp11 cpp_source
43
#' @importFrom Rcpp sourceCpp
5-
NULL
64
"_PACKAGE"
75

86
# The following block is used by usethis to automatically manage

cpp11test/R/test.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ run_tests <- function(reporter = testthat::default_reporter()) {
2222
on.exit(setwd(old))
2323
setwd(system.file("tests", package = "cpp11test"))
2424

25-
testthat::test_check("cpp11test", reporter = reporter)
25+
library(testthat)
26+
test_check("cpp11test", reporter = reporter)
2627
}

cpp11test/man/cpp11test-package.Rd

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cpp11test/src/cpp11.cpp

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -303,55 +303,6 @@ extern "C" SEXP _cpp11test_rcpp_release_(SEXP n) {
303303
return R_NilValue;
304304
END_CPP11
305305
}
306-
// roxygen1.cpp
307-
double notroxcpp1_(double x);
308-
extern "C" SEXP _cpp11test_notroxcpp1_(SEXP x) {
309-
BEGIN_CPP11
310-
return cpp11::as_sexp(notroxcpp1_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
311-
END_CPP11
312-
}
313-
// roxygen1.cpp
314-
double roxcpp2_(double x);
315-
extern "C" SEXP _cpp11test_roxcpp2_(SEXP x) {
316-
BEGIN_CPP11
317-
return cpp11::as_sexp(roxcpp2_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
318-
END_CPP11
319-
}
320-
// roxygen2.cpp
321-
double roxcpp3_(double x);
322-
extern "C" SEXP _cpp11test_roxcpp3_(SEXP x) {
323-
BEGIN_CPP11
324-
return cpp11::as_sexp(roxcpp3_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
325-
END_CPP11
326-
}
327-
// roxygen2.cpp
328-
double roxcpp4_(double x);
329-
extern "C" SEXP _cpp11test_roxcpp4_(SEXP x) {
330-
BEGIN_CPP11
331-
return cpp11::as_sexp(roxcpp4_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
332-
END_CPP11
333-
}
334-
// roxygen3.cpp
335-
double roxcpp5_(double x);
336-
extern "C" SEXP _cpp11test_roxcpp5_(SEXP x) {
337-
BEGIN_CPP11
338-
return cpp11::as_sexp(roxcpp5_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
339-
END_CPP11
340-
}
341-
// roxygen3.cpp
342-
double notroxcpp6_(double x);
343-
extern "C" SEXP _cpp11test_notroxcpp6_(SEXP x) {
344-
BEGIN_CPP11
345-
return cpp11::as_sexp(notroxcpp6_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
346-
END_CPP11
347-
}
348-
// roxygen3.cpp
349-
double roxcpp7_(double x);
350-
extern "C" SEXP _cpp11test_roxcpp7_(SEXP x) {
351-
BEGIN_CPP11
352-
return cpp11::as_sexp(roxcpp7_(cpp11::as_cpp<cpp11::decay_t<double>>(x)));
353-
END_CPP11
354-
}
355306
// safe.cpp
356307
SEXP cpp11_safe_(SEXP x_sxp);
357308
extern "C" SEXP _cpp11test_cpp11_safe_(SEXP x_sxp) {
@@ -373,41 +324,6 @@ extern "C" SEXP _cpp11test_string_push_back_() {
373324
return cpp11::as_sexp(string_push_back_());
374325
END_CPP11
375326
}
376-
// strings.cpp
377-
cpp11::strings grow_strings_cpp11_(size_t n, int seed);
378-
extern "C" SEXP _cpp11test_grow_strings_cpp11_(SEXP n, SEXP seed) {
379-
BEGIN_CPP11
380-
return cpp11::as_sexp(grow_strings_cpp11_(cpp11::as_cpp<cpp11::decay_t<size_t>>(n), cpp11::as_cpp<cpp11::decay_t<int>>(seed)));
381-
END_CPP11
382-
}
383-
// strings.cpp
384-
Rcpp::CharacterVector grow_strings_rcpp_(size_t n, int seed);
385-
extern "C" SEXP _cpp11test_grow_strings_rcpp_(SEXP n, SEXP seed) {
386-
BEGIN_CPP11
387-
return cpp11::as_sexp(grow_strings_rcpp_(cpp11::as_cpp<cpp11::decay_t<size_t>>(n), cpp11::as_cpp<cpp11::decay_t<int>>(seed)));
388-
END_CPP11
389-
}
390-
// strings.cpp
391-
SEXP grow_strings_manual_(size_t n, int seed);
392-
extern "C" SEXP _cpp11test_grow_strings_manual_(SEXP n, SEXP seed) {
393-
BEGIN_CPP11
394-
return cpp11::as_sexp(grow_strings_manual_(cpp11::as_cpp<cpp11::decay_t<size_t>>(n), cpp11::as_cpp<cpp11::decay_t<int>>(seed)));
395-
END_CPP11
396-
}
397-
// strings.cpp
398-
cpp11::strings assign_cpp11_(size_t n, int seed);
399-
extern "C" SEXP _cpp11test_assign_cpp11_(SEXP n, SEXP seed) {
400-
BEGIN_CPP11
401-
return cpp11::as_sexp(assign_cpp11_(cpp11::as_cpp<cpp11::decay_t<size_t>>(n), cpp11::as_cpp<cpp11::decay_t<int>>(seed)));
402-
END_CPP11
403-
}
404-
// strings.cpp
405-
Rcpp::CharacterVector assign_rcpp_(size_t n, int seed);
406-
extern "C" SEXP _cpp11test_assign_rcpp_(SEXP n, SEXP seed) {
407-
BEGIN_CPP11
408-
return cpp11::as_sexp(assign_rcpp_(cpp11::as_cpp<cpp11::decay_t<size_t>>(n), cpp11::as_cpp<cpp11::decay_t<int>>(seed)));
409-
END_CPP11
410-
}
411327
// sum.cpp
412328
double sum_dbl_for_(cpp11::doubles x);
413329
extern "C" SEXP _cpp11test_sum_dbl_for_(SEXP x) {
@@ -527,20 +443,6 @@ extern "C" SEXP _cpp11test_rcpp_push_and_truncate_(SEXP size_sxp) {
527443
return cpp11::as_sexp(rcpp_push_and_truncate_(cpp11::as_cpp<cpp11::decay_t<SEXP>>(size_sxp)));
528444
END_CPP11
529445
}
530-
// test-external_pointer.cpp
531-
cpp11::external_pointer<int> nullable_extptr_1();
532-
extern "C" SEXP _cpp11test_nullable_extptr_1() {
533-
BEGIN_CPP11
534-
return cpp11::as_sexp(nullable_extptr_1());
535-
END_CPP11
536-
}
537-
// test-external_pointer.cpp
538-
cpp11::external_pointer<int> nullable_extptr_2();
539-
extern "C" SEXP _cpp11test_nullable_extptr_2() {
540-
BEGIN_CPP11
541-
return cpp11::as_sexp(nullable_extptr_2());
542-
END_CPP11
543-
}
544446
// test-protect-nested.cpp
545447
void test_destruction_inner();
546448
extern "C" SEXP _cpp11test_test_destruction_inner() {
@@ -570,8 +472,6 @@ extern "C" {
570472
extern SEXP run_testthat_tests(SEXP);
571473

572474
static const R_CallMethodDef CallEntries[] = {
573-
{"_cpp11test_assign_cpp11_", (DL_FUNC) &_cpp11test_assign_cpp11_, 2},
574-
{"_cpp11test_assign_rcpp_", (DL_FUNC) &_cpp11test_assign_rcpp_, 2},
575475
{"_cpp11test_col_sums", (DL_FUNC) &_cpp11test_col_sums, 1},
576476
{"_cpp11test_cpp11_add_vec_for_", (DL_FUNC) &_cpp11test_cpp11_add_vec_for_, 2},
577477
{"_cpp11test_cpp11_insert_", (DL_FUNC) &_cpp11test_cpp11_insert_, 1},
@@ -588,9 +488,6 @@ static const R_CallMethodDef CallEntries[] = {
588488
{"_cpp11test_gibbs_rcpp", (DL_FUNC) &_cpp11test_gibbs_rcpp, 2},
589489
{"_cpp11test_gibbs_rcpp2", (DL_FUNC) &_cpp11test_gibbs_rcpp2, 2},
590490
{"_cpp11test_grow_", (DL_FUNC) &_cpp11test_grow_, 1},
591-
{"_cpp11test_grow_strings_cpp11_", (DL_FUNC) &_cpp11test_grow_strings_cpp11_, 2},
592-
{"_cpp11test_grow_strings_manual_", (DL_FUNC) &_cpp11test_grow_strings_manual_, 2},
593-
{"_cpp11test_grow_strings_rcpp_", (DL_FUNC) &_cpp11test_grow_strings_rcpp_, 2},
594491
{"_cpp11test_my_message", (DL_FUNC) &_cpp11test_my_message, 2},
595492
{"_cpp11test_my_message_n1", (DL_FUNC) &_cpp11test_my_message_n1, 1},
596493
{"_cpp11test_my_message_n1fmt", (DL_FUNC) &_cpp11test_my_message_n1fmt, 1},
@@ -603,8 +500,6 @@ static const R_CallMethodDef CallEntries[] = {
603500
{"_cpp11test_my_warning_n1", (DL_FUNC) &_cpp11test_my_warning_n1, 1},
604501
{"_cpp11test_my_warning_n1fmt", (DL_FUNC) &_cpp11test_my_warning_n1fmt, 1},
605502
{"_cpp11test_my_warning_n2fmt", (DL_FUNC) &_cpp11test_my_warning_n2fmt, 2},
606-
{"_cpp11test_nullable_extptr_1", (DL_FUNC) &_cpp11test_nullable_extptr_1, 0},
607-
{"_cpp11test_nullable_extptr_2", (DL_FUNC) &_cpp11test_nullable_extptr_2, 0},
608503
{"_cpp11test_protect_many_", (DL_FUNC) &_cpp11test_protect_many_, 1},
609504
{"_cpp11test_protect_many_cpp11_", (DL_FUNC) &_cpp11test_protect_many_cpp11_, 1},
610505
{"_cpp11test_protect_many_preserve_", (DL_FUNC) &_cpp11test_protect_many_preserve_, 1},
@@ -623,11 +518,6 @@ static const R_CallMethodDef CallEntries[] = {
623518
{"_cpp11test_rcpp_sum_int_for_", (DL_FUNC) &_cpp11test_rcpp_sum_int_for_, 1},
624519
{"_cpp11test_remove_altrep", (DL_FUNC) &_cpp11test_remove_altrep, 1},
625520
{"_cpp11test_row_sums", (DL_FUNC) &_cpp11test_row_sums, 1},
626-
{"_cpp11test_roxcpp2_", (DL_FUNC) &_cpp11test_roxcpp2_, 1},
627-
{"_cpp11test_roxcpp3_", (DL_FUNC) &_cpp11test_roxcpp3_, 1},
628-
{"_cpp11test_roxcpp4_", (DL_FUNC) &_cpp11test_roxcpp4_, 1},
629-
{"_cpp11test_roxcpp5_", (DL_FUNC) &_cpp11test_roxcpp5_, 1},
630-
{"_cpp11test_roxcpp7_", (DL_FUNC) &_cpp11test_roxcpp7_, 1},
631521
{"_cpp11test_string_proxy_assignment_", (DL_FUNC) &_cpp11test_string_proxy_assignment_, 0},
632522
{"_cpp11test_string_push_back_", (DL_FUNC) &_cpp11test_string_push_back_, 0},
633523
{"_cpp11test_sum_dbl_accumulate2_", (DL_FUNC) &_cpp11test_sum_dbl_accumulate2_, 1},

cpp11test/src/strings.cpp

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#include "cpp11/strings.hpp"
2-
#include <random>
3-
#include <vector>
4-
5-
#include <Rcpp.h>
62

73
// Test benchmark for string proxy assignment performance.
84
// We don't unwind_protect() before each `SET_STRING_ELT()` call,
@@ -37,93 +33,3 @@
3733

3834
return x;
3935
}
40-
41-
// issue 406
42-
43-
std::random_device rd;
44-
std::mt19937 gen(rd());
45-
46-
double random_double() {
47-
std::uniform_real_distribution<double> dist(0.0, 1.0);
48-
return dist(gen);
49-
}
50-
51-
int random_int(int min, int max) {
52-
std::uniform_int_distribution<int> dist(min, max);
53-
return dist(gen);
54-
}
55-
56-
std::string random_string() {
57-
std::string s(10, '\0');
58-
for (size_t i = 0; i < 10; i++) {
59-
s[i] = random_int(0, 25) + 'a';
60-
}
61-
return s;
62-
}
63-
64-
[[cpp11::register]] cpp11::strings grow_strings_cpp11_(size_t n, int seed) {
65-
gen.seed(seed);
66-
cpp11::writable::strings x;
67-
for (size_t i = 0; i < n; ++i) {
68-
x.push_back(random_string());
69-
}
70-
return x;
71-
}
72-
73-
[[cpp11::register]] Rcpp::CharacterVector grow_strings_rcpp_(size_t n, int seed) {
74-
gen.seed(seed);
75-
Rcpp::CharacterVector x(n);
76-
for (size_t i = 0; i < n; ++i) {
77-
x[i] = random_string();
78-
}
79-
return x;
80-
}
81-
82-
[[cpp11::register]] SEXP grow_strings_manual_(size_t n, int seed) {
83-
gen.seed(seed);
84-
SEXP data_ = PROTECT(Rf_allocVector(STRSXP, 0));
85-
size_t size_ = 0;
86-
size_t capacity_ = 0;
87-
for (size_t i = 0; i < n; ++i) {
88-
if (size_ == capacity_) {
89-
capacity_ = capacity_ == 0 ? 1 : capacity_ * 2;
90-
SEXP new_data_ = PROTECT(Rf_allocVector(STRSXP, capacity_));
91-
for (size_t j = 0; j < size_; ++j) {
92-
SET_STRING_ELT(new_data_, j, STRING_ELT(data_, j));
93-
}
94-
UNPROTECT(2);
95-
data_ = PROTECT(new_data_);
96-
}
97-
SET_STRING_ELT(data_, size_++, Rf_mkChar(random_string().c_str()));
98-
}
99-
// copy back down to size
100-
if (size_ < capacity_) {
101-
SEXP new_data_ = PROTECT(Rf_allocVector(STRSXP, size_));
102-
for (size_t j = 0; j < size_; ++j) {
103-
SET_STRING_ELT(new_data_, j, STRING_ELT(data_, j));
104-
}
105-
UNPROTECT(2);
106-
return new_data_;
107-
} else {
108-
UNPROTECT(1);
109-
return data_;
110-
}
111-
}
112-
113-
[[cpp11::register]] cpp11::strings assign_cpp11_(size_t n, int seed) {
114-
gen.seed(seed);
115-
cpp11::writable::strings x(n);
116-
for (size_t i = 0; i < n; ++i) {
117-
x[i] = random_string();
118-
}
119-
return x;
120-
}
121-
122-
[[cpp11::register]] Rcpp::CharacterVector assign_rcpp_(size_t n, int seed) {
123-
gen.seed(seed);
124-
Rcpp::CharacterVector x(n);
125-
for (size_t i = 0; i < n; ++i) {
126-
x[i] = random_string();
127-
}
128-
return x;
129-
}

0 commit comments

Comments
 (0)