|
| 1 | +#include <testthat.h> |
| 2 | + |
| 3 | +#include <deque> |
| 4 | +#include <list> |
| 5 | +#include <map> |
| 6 | +#include <unordered_map> |
| 7 | + |
| 8 | +context("as_advanced-C++") { |
| 9 | + test_that("as_cpp with int type") { |
| 10 | + SEXP x = PROTECT(Rf_ScalarInteger(42)); |
| 11 | + auto val = cpp4r::as_cpp<int>(x); |
| 12 | + expect_true(val == 42); |
| 13 | + UNPROTECT(1); |
| 14 | + } |
| 15 | + |
| 16 | + test_that("as_sexp with std::map<std::string, SEXP>") { |
| 17 | + std::map<std::string, SEXP> m; |
| 18 | + m["a"] = PROTECT(Rf_ScalarInteger(1)); |
| 19 | + m["b"] = PROTECT(Rf_ScalarInteger(2)); |
| 20 | + |
| 21 | + SEXP result = PROTECT(cpp4r::as_sexp(m)); |
| 22 | + expect_true(Rf_isVector(result)); |
| 23 | + expect_true(Rf_xlength(result) == 2); |
| 24 | + |
| 25 | + SEXP names = Rf_getAttrib(result, R_NamesSymbol); |
| 26 | + expect_true(names != R_NilValue); |
| 27 | + UNPROTECT(3); |
| 28 | + } |
| 29 | + |
| 30 | + test_that("as_sexp with std::map<double, int>") { |
| 31 | + std::map<double, int> m; |
| 32 | + m[1.5] = 10; |
| 33 | + m[2.5] = 20; |
| 34 | + |
| 35 | + SEXP result = PROTECT(cpp4r::as_sexp(m)); |
| 36 | + expect_true(Rf_isVector(result)); |
| 37 | + UNPROTECT(1); |
| 38 | + } |
| 39 | + |
| 40 | + test_that("as_sexp with std::unordered_map<std::string, SEXP>") { |
| 41 | + std::unordered_map<std::string, SEXP> m; |
| 42 | + m["x"] = PROTECT(Rf_ScalarReal(1.5)); |
| 43 | + m["y"] = PROTECT(Rf_ScalarReal(2.5)); |
| 44 | + |
| 45 | + SEXP result = PROTECT(cpp4r::as_sexp(m)); |
| 46 | + expect_true(Rf_isVector(result)); |
| 47 | + UNPROTECT(3); |
| 48 | + } |
| 49 | + |
| 50 | + test_that("as_sexp with std::list<double>") { |
| 51 | + std::list<double> l = {1.0, 2.0, 3.0}; |
| 52 | + SEXP result = PROTECT(cpp4r::as_sexp(l)); |
| 53 | + expect_true(Rf_isReal(result)); |
| 54 | + expect_true(Rf_xlength(result) == 3); |
| 55 | + UNPROTECT(1); |
| 56 | + } |
| 57 | + |
| 58 | + test_that("as_cpp with SEXP passthrough") { |
| 59 | + SEXP x = PROTECT(Rf_ScalarInteger(42)); |
| 60 | + SEXP y = cpp4r::as_cpp<SEXP>(x); |
| 61 | + expect_true(x == y); |
| 62 | + UNPROTECT(1); |
| 63 | + } |
| 64 | + |
| 65 | + test_that("as_cpp for std::complex<double>") { |
| 66 | + SEXP x = PROTECT(Rf_allocVector(CPLXSXP, 1)); |
| 67 | + COMPLEX(x)[0].r = 1.0; |
| 68 | + COMPLEX(x)[0].i = 2.0; |
| 69 | + |
| 70 | + std::complex<double> c = cpp4r::as_cpp(x); |
| 71 | + expect_true(c.real() == 1.0); |
| 72 | + expect_true(c.imag() == 2.0); |
| 73 | + UNPROTECT(1); |
| 74 | + } |
| 75 | + |
| 76 | + test_that("as_sexp for std::complex<double>") { |
| 77 | + std::complex<double> c(3.0, 4.0); |
| 78 | + SEXP result = PROTECT(cpp4r::as_sexp(c)); |
| 79 | + expect_true(Rf_isComplex(result)); |
| 80 | + expect_true(COMPLEX(result)[0].r == 3.0); |
| 81 | + expect_true(COMPLEX(result)[0].i == 4.0); |
| 82 | + UNPROTECT(1); |
| 83 | + } |
| 84 | +} |
0 commit comments