R - Add support for std::vector<std::vector<std::string>>

Closes #2385
This commit is contained in:
William S Fulton 2022-10-17 20:13:32 +01:00
commit d8a0286012
6 changed files with 128 additions and 5 deletions

View file

@ -714,6 +714,7 @@ CPP_STD_TEST_CASES += \
li_std_vector_enum \
li_std_vector_member_var\
li_std_vector_ptr \
li_std_vector_vector \
li_std_wstring \
smart_pointer_inherit \
template_typedef_fnc \

View file

@ -0,0 +1,50 @@
%module li_std_vector_vector
%include <std_string.i>
%include <std_vector.i>
namespace std {
%template(VectorInt) vector<int>;
%template(VectorString) vector<string>;
%template(VectorVectorInt) vector<vector<int>>;
%template(VectorVectorString) vector<vector<string>>;
};
%inline %{
std::vector<std::string> make_vector_int() {
std::vector<std::string> v = {"1", "2", "3", "4", "5"};
return v;
}
std::vector<std::vector<int> > make_vector_vector_int() {
std::vector<std::vector<int> > vv;
std::vector<int> v = {1, 2, 3, 4, 5};
vv.push_back(v);
return vv;
}
std::vector<bool> make_vector_bool() {
std::vector<bool> v = {true, false, false, false, true};
return v;
}
std::vector<std::vector<bool> > make_vector_vector_bool() {
std::vector<std::vector<bool> > vv;
std::vector<bool> v = {false, true, true, true, false};
vv.push_back(v);
return vv;
}
std::vector<std::string> make_vector_string() {
std::vector<std::string> v = {"aa", "bb", "cc", "dd", "ee"};
return v;
}
std::vector<std::vector<std::string> > make_vector_vector_string() {
std::vector<std::vector<std::string> > vv;
std::vector<std::string> v = {"1", "2", "3", "4", "5"};
vv.push_back(v);
return vv;
}
%}

View file

@ -0,0 +1,28 @@
clargs <- commandArgs(trailing=TRUE)
source(file.path(clargs[1], "unittest.R"))
dyn.load(paste("li_std_vector_vector", .Platform$dynlib.ext, sep=""))
source("li_std_vector_vector.R")
cacheMetaData(1)
v = make_vector_int()
unittest_sequence(v, list(1, 2, 3, 4, 5))
v = make_vector_vector_int()
unittest(length(v), 1)
unittest_sequence(v[[1]], list(1, 2, 3, 4, 5))
v = make_vector_bool()
unittest_sequence(v, list(TRUE, FALSE, FALSE, FALSE, TRUE))
print(v)
v = make_vector_vector_bool()
unittest(length(v), 1)
print(v)
unittest_sequence(v[[1]], list(FALSE, TRUE, TRUE, TRUE, FALSE)) # Does not actually fail if no match, arg, needs fixing
v = make_vector_string()
unittest_sequence(v, list("aa", "bb", "cc", "dd", "ee"))
v = make_vector_vector_string()
unittest(length(v), 1)
unittest_sequence(v[[1]], list("1", "2", "3", "4", "5"))
q(save="no")

View file

@ -1,8 +1,8 @@
unittest <- function (x,y) {
if (all(x==y)) {
print("PASS")
print("PASS")
} else {
print("FAIL")
print("FAIL")
stop("Test failed")
}
}
@ -15,3 +15,12 @@ unittesttol <- function(x,y,z) {
stop("Test failed")
}
}
unittest_sequence <- function (x,y) {
if (setequal(x, y)) {
print("PASS")
} else {
print("FAIL")
stop("Test failed")
}
}