Scilab: std_vector_as_argument example converted to test
This commit is contained in:
parent
1a9c86673b
commit
84f1e3d3e2
7 changed files with 136 additions and 189 deletions
|
|
@ -1,15 +0,0 @@
|
||||||
TOP = ../../..
|
|
||||||
SWIG = $(TOP)/../preinst-swig
|
|
||||||
SRCS = example.cpp
|
|
||||||
TARGET = example_wrap.cxx
|
|
||||||
INTERFACE = example.i
|
|
||||||
|
|
||||||
check: build
|
|
||||||
$(MAKE) -f $(TOP)/Makefile scilab_run
|
|
||||||
|
|
||||||
build:
|
|
||||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
|
||||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
|
|
||||||
|
|
||||||
clean:
|
|
||||||
$(MAKE) -f $(TOP)/Makefile scilab_clean
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
||||||
/* File : example.hxx */
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
|
|
||||||
// double vectors
|
|
||||||
std::vector<double> create_double_vector(const int size, const double value);
|
|
||||||
double sum_double_vector(const std::vector<double>& vector);
|
|
||||||
std::vector<double> concat_double_vector(const std::vector<double> vector, const std::vector<double> other_vector);
|
|
||||||
|
|
||||||
// integer vectors
|
|
||||||
std::vector<int> create_integer_vector(const int size, const int value);
|
|
||||||
int sum_integer_vector(const std::vector<int>& vector);
|
|
||||||
std::vector<int> concat_integer_vector(const std::vector<int> vector, const std::vector<int> other_vector);
|
|
||||||
|
|
||||||
// string vectors
|
|
||||||
std::vector<std::string> create_string_vector(const int size, const char* value);
|
|
||||||
std::vector<std::string> concat_string_vector(const std::vector<std::string> vector, const std::vector<std::string> other_vector);
|
|
||||||
|
|
||||||
// bool vectors
|
|
||||||
std::vector<bool> create_bool_vector(const int size, const bool value);
|
|
||||||
std::vector<bool> concat_bool_vector(const std::vector<bool> vector, const std::vector<bool> other_vector);
|
|
||||||
|
|
||||||
// pointer (on object) vectors
|
|
||||||
class classA
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
classA() : a(0) {}
|
|
||||||
classA(int _a) : a(_a) {}
|
|
||||||
classA(const classA& c) : a(c.a) {}
|
|
||||||
int a;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<classA*> create_classAPtr_vector(const int size, const int value);
|
|
||||||
void print_classAPtr_vector(const std::vector<classA*>& pvector);
|
|
||||||
std::vector<classA*> concat_classAPtr_vector(const std::vector<classA*> vector, const std::vector<classA*> other_vector);
|
|
||||||
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
/* File : example.i */
|
|
||||||
|
|
||||||
%module example
|
|
||||||
|
|
||||||
%{
|
|
||||||
#include "example.hxx"
|
|
||||||
%}
|
|
||||||
|
|
||||||
%include stl.i
|
|
||||||
|
|
||||||
/* instantiate the required template specializations */
|
|
||||||
namespace std
|
|
||||||
{
|
|
||||||
%template(IntVector) vector<int>;
|
|
||||||
%template(DoubleVector) vector<double>;
|
|
||||||
%template(StringVector) vector<std::string>;
|
|
||||||
%template(BoolVector) vector<bool>;
|
|
||||||
%template(ClassAPtrVector) vector<classA*>;
|
|
||||||
}
|
|
||||||
|
|
||||||
%include "example.hxx"
|
|
||||||
|
|
@ -1,70 +0,0 @@
|
||||||
lines(0);
|
|
||||||
exec loader.sce;
|
|
||||||
SWIG_Init();
|
|
||||||
|
|
||||||
// This example shows how to use C++ fonctions with STL vectors arguments
|
|
||||||
// Here, STL vectors are converted from/to Scilab matrices (SWIG_SCILAB_EXTRA_NATIVE_CONTAINERS is not defined)
|
|
||||||
|
|
||||||
// double vectors
|
|
||||||
|
|
||||||
disp("Example of passing matrices of double as vector arguments of C++ functions.");
|
|
||||||
disp("get a vector of double {2.0, 2.0, 2.0, 2.0} from create_double_vector():");
|
|
||||||
dv = create_double_vector(4, 2.0);
|
|
||||||
disp(dv);
|
|
||||||
disp("get the sum of this vector elements with sum_double_vector():")
|
|
||||||
ds = sum_double_vector(dv);
|
|
||||||
disp(ds);
|
|
||||||
dv2 = create_double_vector(2, 5.0);
|
|
||||||
disp("concat this vector with the vector of double {5.0, 5.0} with concat_double_vector():");
|
|
||||||
dv3 = concat_double_vector(dv, dv2);
|
|
||||||
disp(dv3);
|
|
||||||
|
|
||||||
// integer vectors
|
|
||||||
|
|
||||||
disp("Example of passing matrices of int as vector arguments of C++ functions.");
|
|
||||||
disp("get a vector of int {3, 3, 3, 3} from create_integer_vector():");
|
|
||||||
iv = create_integer_vector(3, 3);
|
|
||||||
disp(iv);
|
|
||||||
disp("get the sum of this vector elements with sum_integer_vector():")
|
|
||||||
is = sum_integer_vector(iv);
|
|
||||||
disp(is);
|
|
||||||
iv2 = create_integer_vector(2, 1);
|
|
||||||
disp("concat this vector with the vector of int {1, 1} with concat_integer_vector():");
|
|
||||||
iv3 = concat_integer_vector(iv, iv2);
|
|
||||||
disp(iv3);
|
|
||||||
|
|
||||||
// string vectors
|
|
||||||
|
|
||||||
disp("Example of passing matrices of string as vector arguments of C++ functions.");
|
|
||||||
disp("get a vector of string {''aa'', ''aa''} with create_string_vector():");
|
|
||||||
sv = create_string_vector(2, "aa");
|
|
||||||
disp(sv);
|
|
||||||
sv2 = create_string_vector(2, "bb");
|
|
||||||
disp("concat this vector with the vector of string {''bb'', ''bb''} with concat_string_vector():");
|
|
||||||
sv3 = concat_string_vector(sv, sv2);
|
|
||||||
disp(sv3);
|
|
||||||
|
|
||||||
// bool vectors
|
|
||||||
|
|
||||||
disp("Example of passing matrices of bool as vector arguments of C++ functions.");
|
|
||||||
disp("get a vector of bool {true, true} with create_bool_vector():");
|
|
||||||
bv = create_bool_vector(2, %T);
|
|
||||||
disp(bv);
|
|
||||||
bv2 = create_bool_vector(3, %F);
|
|
||||||
disp("concat this vector with the vector of bool {false, false, false} with concat_bool_vector():");
|
|
||||||
bv3 = concat_bool_vector(bv, bv2);
|
|
||||||
disp(bv3);
|
|
||||||
|
|
||||||
// pointer (on object) vectors
|
|
||||||
|
|
||||||
disp("Example of passing lists of pointers on object as vector of pointers on objects arguments of C++ functions.");
|
|
||||||
disp("get a vector of pointers on object {<classA* a:1>, <classA* a:1>, <classA* a:1>} with create_classAPtr_vector():");
|
|
||||||
pv = create_classAPtr_vector(3, 1);
|
|
||||||
print_classAPtr_vector(pv);
|
|
||||||
pv2 = create_classAPtr_vector(2, 5);
|
|
||||||
disp("concat this vector with the vector of pointers on object {<classA* a:5>, <classA* a:5>} with concat_classAPtr_vector():");
|
|
||||||
pv3 = concat_classAPtr_vector(pv, pv2);
|
|
||||||
print_classAPtr_vector(pv3);
|
|
||||||
|
|
||||||
exit
|
|
||||||
|
|
||||||
|
|
@ -1,17 +1,50 @@
|
||||||
/* File : example.cpp */
|
%module li_std_vector_as_argument
|
||||||
|
|
||||||
#include "example.hxx"
|
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
|
|
||||||
|
%}
|
||||||
|
|
||||||
|
%{
|
||||||
template <typename T>
|
class classA
|
||||||
std::vector<T> concat_vector(const std::vector<T> vector, const std::vector<T> other_vector)
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
classA() : a(0) {}
|
||||||
|
classA(int _a) : a(_a) {}
|
||||||
|
classA(const classA& c) : a(c.a) {}
|
||||||
|
int a;
|
||||||
|
};
|
||||||
|
%}
|
||||||
|
|
||||||
|
%include stl.i
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
%template(IntVector) vector<int>;
|
||||||
|
%template(DoubleVector) vector<double>;
|
||||||
|
%template(StringVector) vector<std::string>;
|
||||||
|
%template(BoolVector) vector<bool>;
|
||||||
|
%template(ClassAPtrVector) vector<classA*>;
|
||||||
|
}
|
||||||
|
|
||||||
|
%ignore concat_vector;
|
||||||
|
|
||||||
|
class classA
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
classA() : a(0) {}
|
||||||
|
classA(int _a) : a(_a) {}
|
||||||
|
classA(const classA& c) : a(c.a) {}
|
||||||
|
int a;
|
||||||
|
};
|
||||||
|
|
||||||
|
%inline %{
|
||||||
|
template <typename T>
|
||||||
|
std::vector<T> concat_vector(const std::vector<T> vector, const std::vector<T> other_vector) {
|
||||||
std::vector<T> out_vector(vector);
|
std::vector<T> out_vector(vector);
|
||||||
out_vector.insert(out_vector.end(), other_vector.begin(), other_vector.end());
|
out_vector.insert(out_vector.end(), other_vector.begin(), other_vector.end());
|
||||||
return out_vector;
|
return out_vector;
|
||||||
|
|
@ -19,87 +52,64 @@ std::vector<T> concat_vector(const std::vector<T> vector, const std::vector<T> o
|
||||||
|
|
||||||
// double vectors
|
// double vectors
|
||||||
|
|
||||||
std::vector<double> create_double_vector(const int size, const double value)
|
std::vector<double> create_double_vector(const int size, const double value) {
|
||||||
{
|
|
||||||
return std::vector<double>(size, value);
|
return std::vector<double>(size, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
double sum_double_vector(const std::vector<double>& vector)
|
double sum_double_vector(const std::vector<double>& vector) {
|
||||||
{
|
|
||||||
return std::accumulate(vector.begin(), vector.end(), 0);
|
return std::accumulate(vector.begin(), vector.end(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<double> concat_double_vector(const std::vector<double> vector, const std::vector<double> other_vector)
|
std::vector<double> concat_double_vector(const std::vector<double> vector, const std::vector<double> other_vector) {
|
||||||
{
|
|
||||||
return concat_vector<double>(vector, other_vector);
|
return concat_vector<double>(vector, other_vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
// int vectors
|
// int vectors
|
||||||
|
|
||||||
std::vector<int> create_integer_vector(const int size, const int value)
|
std::vector<int> create_integer_vector(const int size, const int value) {
|
||||||
{
|
|
||||||
return std::vector<int>(size, value);
|
return std::vector<int>(size, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sum_integer_vector(const std::vector<int>& vector)
|
int sum_integer_vector(const std::vector<int>& vector) {
|
||||||
{
|
|
||||||
return std::accumulate(vector.begin(), vector.end(), 0);
|
return std::accumulate(vector.begin(), vector.end(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<int> concat_integer_vector(const std::vector<int> vector, const std::vector<int> other_vector)
|
std::vector<int> concat_integer_vector(const std::vector<int> vector, const std::vector<int> other_vector) {
|
||||||
{
|
|
||||||
return concat_vector<int>(vector, other_vector);
|
return concat_vector<int>(vector, other_vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
// string vectors
|
// string vectors
|
||||||
|
|
||||||
std::vector<std::string> create_string_vector(const int size, const char* value)
|
std::vector<std::string> create_string_vector(const int size, const char *value) {
|
||||||
{
|
|
||||||
return std::vector<std::string>(size, value);
|
return std::vector<std::string>(size, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> concat_string_vector(const std::vector<std::string> vector, const std::vector<std::string> other_vector)
|
std::vector<std::string> concat_string_vector(const std::vector<std::string> vector, const std::vector<std::string> other_vector) {
|
||||||
{
|
|
||||||
return concat_vector<std::string>(vector, other_vector);
|
return concat_vector<std::string>(vector, other_vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
// bool vectors
|
// bool vectors
|
||||||
|
|
||||||
std::vector<bool> create_bool_vector(const int size, const bool value)
|
std::vector<bool> create_bool_vector(const int size, const bool value) {
|
||||||
{
|
|
||||||
return std::vector<bool>(size, value);
|
return std::vector<bool>(size, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<bool> concat_bool_vector(const std::vector<bool> vector, const std::vector<bool> other_vector)
|
std::vector<bool> concat_bool_vector(const std::vector<bool> vector, const std::vector<bool> other_vector) {
|
||||||
{
|
|
||||||
return concat_vector<bool>(vector, other_vector);
|
return concat_vector<bool>(vector, other_vector);
|
||||||
}
|
}
|
||||||
|
|
||||||
// pointer (on objects) vectors
|
// pointer (on object) vectors
|
||||||
|
|
||||||
std::vector<classA*> create_classAPtr_vector(const int size, const int value)
|
std::vector<classA*> create_classAPtr_vector(const int size, classA *aClassAPtr) {
|
||||||
{
|
|
||||||
std::vector<classA*> out_vector;
|
std::vector<classA*> out_vector;
|
||||||
for (int i=0; i<size; i++)
|
for (int i=0; i<size; i++) {
|
||||||
{
|
out_vector.push_back(aClassAPtr);
|
||||||
classA* objA = new classA(value);
|
|
||||||
out_vector.push_back(objA);
|
|
||||||
}
|
}
|
||||||
return out_vector;
|
return out_vector;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_classAPtr_vector(const std::vector<classA*>& vector)
|
std::vector<classA*> concat_classAPtr_vector(const std::vector<classA*> vector, const std::vector<classA*> other_vector) {
|
||||||
{
|
|
||||||
std::vector<classA*>::const_iterator it;
|
|
||||||
std::cout << std::endl;
|
|
||||||
for (it = vector.begin(); it != vector.end(); ++it)
|
|
||||||
{
|
|
||||||
std::cout << "<classA* a:" << (*it)->a << ">" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<classA*> concat_classAPtr_vector(const std::vector<classA*> vector, const std::vector<classA*> other_vector)
|
|
||||||
{
|
|
||||||
return concat_vector<classA*>(vector, other_vector);
|
return concat_vector<classA*>(vector, other_vector);
|
||||||
}
|
}
|
||||||
|
%}
|
||||||
|
|
||||||
|
|
@ -25,6 +25,9 @@ define get_runme_script
|
||||||
RUNME_SCRIPT := $(srcdir)/$(SCRIPTPREFIX)$(1)$(SCRIPTSUFFIX)
|
RUNME_SCRIPT := $(srcdir)/$(SCRIPTPREFIX)$(1)$(SCRIPTSUFFIX)
|
||||||
endef
|
endef
|
||||||
|
|
||||||
|
CPP_STD_TEST_CASES += \
|
||||||
|
li_std_vector_as_argument
|
||||||
|
|
||||||
include $(srcdir)/../common.mk
|
include $(srcdir)/../common.mk
|
||||||
|
|
||||||
# Override make commands to specify OUTDIR
|
# Override make commands to specify OUTDIR
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
// Tests C++ fonctions with STL vectors as arguments
|
||||||
|
|
||||||
|
exec("swigtest.start", -1);
|
||||||
|
|
||||||
|
// double vectors
|
||||||
|
|
||||||
|
// Test of using vector<double> arguments of C++ functions
|
||||||
|
// Get a vector of double {2.0, 2.0, 2.0, 2.0} from create_double_vector()
|
||||||
|
dv = create_double_vector(4, 2.0);
|
||||||
|
if ~exists("dv") | (dv <> [2. 2. 2. 2.]) then swigtesterror(); end
|
||||||
|
// Get sum this vector elements with sum_double_vector()
|
||||||
|
ds = sum_double_vector(dv);
|
||||||
|
if ~exists("ds") | (ds <> 8.) then swigtesterror(); end
|
||||||
|
// Get a vector of double {5.0, 5.0} from create_double_vector()
|
||||||
|
dv2 = create_double_vector(2, 5.0);
|
||||||
|
if ~exists("dv2") | (dv2 <> [5. 5.]) then swigtesterror(); end
|
||||||
|
// Concat the two vectors with concat_double_vector()
|
||||||
|
dv3 = concat_double_vector(dv, dv2);
|
||||||
|
if ~exists("dv3") | (dv3 <> [dv dv2]) then swigtesterror(); end
|
||||||
|
|
||||||
|
// integer vectors
|
||||||
|
|
||||||
|
// Test of using vector<int> arguments of C++ functions.");
|
||||||
|
// Get a vector of int {3, 3, 3, 3} from create_integer_vector()
|
||||||
|
iv = create_integer_vector(3, 3);
|
||||||
|
if ~exists("iv") | (iv <> int32([3 3 3])) then swigtesterror(); end
|
||||||
|
// Get the sum of this vector elements with sum_integer_vector()
|
||||||
|
is = sum_integer_vector(iv);
|
||||||
|
if ~exists("is") | (is <> int32(9)) then swigtesterror(); end
|
||||||
|
// Get a vector of int {1, 1} from create_integer_vector()
|
||||||
|
iv2 = create_integer_vector(2, 1);
|
||||||
|
// Concat the two vectors with concat_integer_vector()
|
||||||
|
iv3 = concat_integer_vector(iv, iv2);
|
||||||
|
if ~exists("iv3") | (iv3 <> int32([iv iv2])) then swigtesterror(); end
|
||||||
|
|
||||||
|
// std::string vectors
|
||||||
|
|
||||||
|
// Test of using vector<std::string> arguments of C++ functions.
|
||||||
|
// Get a vector of string {''aa'', ''aa''} with create_string_vector()
|
||||||
|
sv = create_string_vector(2, "aa");
|
||||||
|
if ~exists("sv") | (sv <> ["aa"; "aa"]) then swigtesterror(); end
|
||||||
|
// Get a vector of string {''bb'', ''bb''} with create_string_vector()
|
||||||
|
sv2 = create_string_vector(2, "bb");
|
||||||
|
if ~exists("sv2") | (sv2 <> ["bb"; "bb"]) then swigtesterror(); end
|
||||||
|
// Concat the two vectors with concat_string_vector()
|
||||||
|
sv3 = concat_string_vector(sv, sv2);
|
||||||
|
if ~exists("sv3") | (sv3 <> [sv; sv2]) then swigtesterror(); end
|
||||||
|
|
||||||
|
// bool vectors
|
||||||
|
|
||||||
|
// Test of using vector<bool> arguments of C++ functions.
|
||||||
|
// Get a vector of bool {true, true} with create_bool_vector()
|
||||||
|
bv = create_bool_vector(2, %T);
|
||||||
|
if ~exists("bv") | (bv <> [%T %T]) then swigtesterror(); end
|
||||||
|
// Get a vector of bool {false, false, false} with create_bool_vector()
|
||||||
|
bv2 = create_bool_vector(3, %F);
|
||||||
|
if ~exists("bv2") | (bv2 <> [%F %F %F]) then swigtesterror(); end
|
||||||
|
// Concat the two vectors with concat_bool_vector()
|
||||||
|
bv3 = concat_bool_vector(bv, bv2);
|
||||||
|
if ~exists("bv3") | (bv3 <> [bv bv2]) then swigtesterror(); end
|
||||||
|
|
||||||
|
// Pointer (on object) vectors
|
||||||
|
|
||||||
|
// Test of using vector of pointers (on object) arguments of C++ functions.
|
||||||
|
// Get a vector of pointers on object {<classA* a:10>, <classA* a:10>, <classA* a:10>} with create_classAPtr_vector()
|
||||||
|
classA_1 = new_classA(10);
|
||||||
|
pv = create_classAPtr_vector(3, classA_1);
|
||||||
|
if ~exists("pv") | (size(pv) <> 3) | (classA_a_get(pv(1)) <> 10) then swigtesterror(); end
|
||||||
|
// Get a vector of pointers on object {<classA* a:5>, <classA* a:5>} with create_classAPtr_vector()
|
||||||
|
classA_2 = new_classA(5);
|
||||||
|
pv2 = create_classAPtr_vector(2, classA_2);
|
||||||
|
if ~exists("pv2") | (size(pv2) <> 2) | (classA_a_get(pv2(1)) <> 5) then swigtesterror(); end
|
||||||
|
// Concat the two vectors with concat_classAPtr_vector()
|
||||||
|
pv3 = concat_classAPtr_vector(pv, pv2);
|
||||||
|
if ~exists("pv3") | (size(pv3) <> 5) | (classA_a_get(pv3(1)) <> 10) | (classA_a_get(pv3(4)) <> 5) then swigtesterror(); end
|
||||||
|
|
||||||
|
exec("swigtest.quit", -1);
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue