scilab: new generic test li_std_sequence_container_typemaps (for STL vector, list, ...)
This commit is contained in:
parent
15f0624216
commit
a1a055f0f0
3 changed files with 220 additions and 0 deletions
118
Examples/test-suite/li_std_sequence_container_typemaps.i
Normal file
118
Examples/test-suite/li_std_sequence_container_typemaps.i
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
%module li_std_sequence_container_typemaps
|
||||
|
||||
%include stl.i
|
||||
|
||||
%{
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include <numeric>
|
||||
%}
|
||||
|
||||
%inline %{
|
||||
class ClassA
|
||||
{
|
||||
public:
|
||||
ClassA() : a(0) {}
|
||||
ClassA(int _a) : a(_a) {}
|
||||
ClassA(const ClassA& c) : a(c.a) {}
|
||||
int a;
|
||||
};
|
||||
|
||||
typedef ClassA* ClassAPtr;
|
||||
|
||||
namespace std {
|
||||
template<typename T> T binaryOperation(T x, T y) {
|
||||
return x + y;
|
||||
}
|
||||
|
||||
template<> bool binaryOperation(bool x, bool y) {
|
||||
return x | y;
|
||||
}
|
||||
|
||||
template<> ClassAPtr binaryOperation(ClassAPtr x, ClassAPtr y) {
|
||||
x->a += y->a;
|
||||
return x;
|
||||
}
|
||||
|
||||
template<typename SeqCont>
|
||||
struct sequence_container {
|
||||
typedef typename SeqCont::value_type value_type;
|
||||
|
||||
static SeqCont ret_container(const int size, const value_type value) {
|
||||
return SeqCont(size, value);
|
||||
}
|
||||
|
||||
static value_type val_container(const SeqCont container) {
|
||||
return std::accumulate(container.begin(), container.end(), container.front(),
|
||||
binaryOperation<value_type>);
|
||||
}
|
||||
|
||||
static value_type ref_container(const SeqCont& container) {
|
||||
return std::accumulate(container.begin(), container.end(), container.front(),
|
||||
binaryOperation<value_type>);
|
||||
}
|
||||
|
||||
/*SeqCont ret_val_containers(const SeqCont container,
|
||||
const SeqCont other_container) {
|
||||
SeqCont out_container(container);
|
||||
out_container.insert(out_container.end(), other_container.begin(),
|
||||
other_container.end());
|
||||
return out_container;
|
||||
}*/
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
std::vector<T> ret_vector(const int size, const T value) {
|
||||
return sequence_container<std::vector<T> >::ret_container(size, value);
|
||||
}
|
||||
template<typename T>
|
||||
T val_vector(const std::vector<T> container) {
|
||||
return sequence_container<std::vector<T> >::val_container(container);
|
||||
}
|
||||
template<typename T>
|
||||
T ref_vector(const std::vector<T>& container) {
|
||||
return sequence_container<std::vector<T> >::ref_container(container);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::list<T> ret_list(const int size, const T value) {
|
||||
return sequence_container<std::list<T> >::ret_container(size, value);
|
||||
}
|
||||
template<typename T>
|
||||
T val_list(const std::list<T> container) {
|
||||
return sequence_container<std::list<T> >::val_container(container);
|
||||
}
|
||||
template<typename T>
|
||||
T ref_list(const std::list<T>& container) {
|
||||
return sequence_container<std::list<T> >::ref_container(container);
|
||||
}
|
||||
}
|
||||
%}
|
||||
|
||||
%define instantiate_containers_templates(TYPE...)
|
||||
namespace std
|
||||
{
|
||||
%template(TYPE ##_## vector) std::vector<TYPE >;
|
||||
%template(ret_ ## TYPE ##_## vector) ret_vector<TYPE>;
|
||||
%template(val_ ## TYPE ##_## vector) val_vector<TYPE>;
|
||||
%template(ref_ ## TYPE ##_## vector) ref_vector<TYPE>;
|
||||
|
||||
%template(TYPE ##_## list) std::list<TYPE >;
|
||||
%template(ret_ ## TYPE ##_## list) ret_list<TYPE>;
|
||||
%template(val_ ## TYPE ##_## list) val_list<TYPE>;
|
||||
%template(ref_ ## TYPE ##_## list) ref_list<TYPE>;
|
||||
}
|
||||
%enddef
|
||||
|
||||
|
||||
instantiate_containers_templates(int)
|
||||
instantiate_containers_templates(double)
|
||||
instantiate_containers_templates(bool)
|
||||
instantiate_containers_templates(string)
|
||||
instantiate_containers_templates(ClassAPtr)
|
||||
|
||||
|
||||
|
|
@ -17,6 +17,7 @@ C_TEST_CASES += \
|
|||
CPP_STD_TEST_CASES += \
|
||||
primitive_types \
|
||||
inout \
|
||||
li_std_sequence_container_typemaps \
|
||||
|
||||
TEST_DIR = $*.dir
|
||||
RUNME_SCRIPT = $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,101 @@
|
|||
// test STL sequence containers typemaps
|
||||
|
||||
exec("swigtest.start", -1);
|
||||
|
||||
// test sequence containers of pointers
|
||||
// -container: type of container: "vector", "list"...
|
||||
// -value, count: value to store count times in container
|
||||
// -expected_accumulate_value: expected value of an accumulation function
|
||||
// computed on the container
|
||||
function testSequenceContainerPtr(container, count, value, expected_accumulate_value)
|
||||
// test sequence container returned from fonction (expected a list)
|
||||
classAPtr = new_ClassA(value);
|
||||
cmd = msprintf("l = ret_ClassAPtr_%s(count, classAPtr);", container);
|
||||
ierr = execstr(cmd, "errcatch");
|
||||
if ierr <> 0 then swigtesterror(); end
|
||||
if ~exists('l') | (size(l) <> count) | (ClassA_a_get(l(1)) <> value) then swigtesterror(); end
|
||||
l2 = l;
|
||||
|
||||
// test sequence container passed as value of function
|
||||
cmd = msprintf("classAPtr = val_ClassAPtr_%s(l);", container);
|
||||
ierr = execstr(cmd, "errcatch");
|
||||
if ierr <> 0 then swigtesterror(); end
|
||||
if ClassA_a_get(classAPtr) <> expected_accumulate_value then swigtesterror(); end
|
||||
|
||||
// recreate a container
|
||||
classAPtr = new_ClassA(value);
|
||||
cmd = msprintf("l = ret_ClassAPtr_%s(count, classAPtr);", container);
|
||||
ierr = execstr(cmd, "errcatch");
|
||||
if ierr <> 0 then swigtesterror(); end
|
||||
if ~exists('l') | (size(l) <> count) | (ClassA_a_get(l(1)) <> value) then swigtesterror(); end
|
||||
|
||||
// test sequence container passed as refererence of function
|
||||
cmd = msprintf("classAPtr = ref_ClassAPtr_%s(l);", container);
|
||||
ierr = execstr(cmd, "errcatch");
|
||||
if ierr <> 0 then swigtesterror(); end
|
||||
if ClassA_a_get(classAPtr) <> expected_accumulate_value then swigtesterror(); end
|
||||
endfunction
|
||||
|
||||
// test sequence containers of primitive type
|
||||
// -container: type of container: "vector", "list"...
|
||||
// -value_type: type of element stored in container: "int", ...
|
||||
// -value, count: value to store count times in container
|
||||
// -expected_accumulate_value: expected value of an accumulation function
|
||||
// computed on the container
|
||||
function testSequenceContainer(container, value_type, value, count, expected_accumulate_value)
|
||||
// test sequence container returned from fonction (expect a row matrix)
|
||||
if value_type = "string"
|
||||
cmd = msprintf("c = ret_%s_%s(count, ''%s'');", value_type, container, value);
|
||||
elseif value_type = "bool" then
|
||||
cmd = msprintf("c = ret_%s_%s(count, %s);", value_type, container, "%"+string(value));
|
||||
else
|
||||
cmd = msprintf("c = ret_%s_%s(count, %d);", value_type, container, value);
|
||||
end
|
||||
ierr = execstr(cmd, "errcatch");
|
||||
if ierr <> 0 then swigtesterror(); end
|
||||
if ~isdef('c') | c <> repmat(value, 1, count) then swigtesterror(); end
|
||||
|
||||
// test sequence container passed as value of function
|
||||
cmd = msprintf("s = val_%s_%s(c);", value_type, container);
|
||||
ierr = execstr(cmd, "errcatch");
|
||||
if ierr <> 0 then swigtesterror(); end
|
||||
if s <> expected_accumulate_value then swigtesterror(); end
|
||||
|
||||
// test sequence container passed as matrix as value of function
|
||||
//m = repmat(value, 1, count);
|
||||
//cmd = msprintf("s = val_%s_%s(m);", value_type, container);
|
||||
//ierr = execstr(cmd, "errcatch");
|
||||
//if ierr <> 0 then swigtesterror(); end
|
||||
//if s <> expected_accumulate_value then swigtesterror(); end
|
||||
|
||||
// test sequence container passed as reference of function
|
||||
cmd = msprintf("s = ref_%s_%s(c);", value_type, container);
|
||||
ierr = execstr(cmd, "errcatch");
|
||||
if ierr <> 0 then swigtesterror(); end
|
||||
if s <> expected_accumulate_value then swigtesterror(); end
|
||||
|
||||
// test sequence container passed as matrix as reference of function
|
||||
//m = repmat(value, 1, count);
|
||||
//cmd = msprintf("s = val_%s_%s(m);", value_type, container);
|
||||
//ierr = execstr(cmd, "errcatch");
|
||||
//if ierr <> 0 then swigtesterror(); end
|
||||
//if s <> expected_accumulate_value then swigtesterror(); end
|
||||
endfunction
|
||||
|
||||
// test vector
|
||||
testSequenceContainer("vector", "int", 2, 4, 10);
|
||||
testSequenceContainer("vector", "double", 2., 3., 8.);
|
||||
testSequenceContainer("vector", "string", "a", 4, "aaaaa");
|
||||
testSequenceContainer("vector", "bool", %T, 2, %T);
|
||||
testSequenceContainerPtr("vector", 1, 3, 6.0);
|
||||
|
||||
// test list
|
||||
testSequenceContainer("list", "int", 2, 3, 8);
|
||||
testSequenceContainer("list", "double", 2., 4., 10.);
|
||||
testSequenceContainer("list", "string", "a", 4, "aaaaa");
|
||||
testSequenceContainer("list", "bool", %T, 2, %T);
|
||||
testSequenceContainerPtr("list", 1, 3, 6.0);
|
||||
|
||||
exec("swigtest.quit", -1);
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue