more test files for STL
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5759 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
359d862a49
commit
9e22799f04
6 changed files with 390 additions and 0 deletions
22
SWIG/Examples/test-suite/python/implicittest.i
Normal file
22
SWIG/Examples/test-suite/python/implicittest.i
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
%module implicittest
|
||||
%include implicit.i
|
||||
|
||||
%inline
|
||||
{
|
||||
struct B { };
|
||||
}
|
||||
|
||||
%implicit(A, int, double, B);
|
||||
|
||||
%inline
|
||||
{
|
||||
struct A
|
||||
{
|
||||
int ii;
|
||||
A(int i) { ii = 1; }
|
||||
A(double d) { ii = 2; }
|
||||
A(const B& b) { ii = 3; }
|
||||
};
|
||||
|
||||
int get(const A& a) { return a.ii; }
|
||||
}
|
||||
17
SWIG/Examples/test-suite/python/implicittest_runme.py
Normal file
17
SWIG/Examples/test-suite/python/implicittest_runme.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from implicittest import *
|
||||
b = B()
|
||||
ai = A(1)
|
||||
ad = A(2.0)
|
||||
ab = A(b)
|
||||
|
||||
print ai, get(ai)
|
||||
print ad, get(ad)
|
||||
print ab, get(ab)
|
||||
|
||||
if get(ai) != get(1):
|
||||
raise RuntimeError,"bad implicit type"
|
||||
if get(ad) != get(2.0):
|
||||
raise RuntimeError,"bad implicit type"
|
||||
if get(ab) != get(b):
|
||||
raise RuntimeError,"bad implicit type"
|
||||
|
||||
105
SWIG/Examples/test-suite/python/lib_std_pair.i
Normal file
105
SWIG/Examples/test-suite/python/lib_std_pair.i
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
%module lib_std_pair
|
||||
|
||||
%include "std_pair.i"
|
||||
|
||||
%{
|
||||
struct A
|
||||
{
|
||||
};
|
||||
struct B
|
||||
{
|
||||
};
|
||||
%}
|
||||
|
||||
|
||||
namespace std {
|
||||
%template(IntPair) pair<int, int>;
|
||||
%template(AIntPair) pair<A, int>;
|
||||
|
||||
%template(ABPair) pair<A, B>;
|
||||
%template(IntAPair) pair<int, A>;
|
||||
}
|
||||
|
||||
%inline %{
|
||||
|
||||
/* Test the "out" typemap for pair<T, U> */
|
||||
std::pair<int, int> makeIntPair(int a, int b) {
|
||||
return std::make_pair(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no "out" typemap for a pointer to a pair, so
|
||||
* this should return a wrapped instance of a std::pair
|
||||
* instead of the native "array" type for the target language.
|
||||
*/
|
||||
std::pair<int, int> * makeIntPairPtr(int a, int b) {
|
||||
static std::pair<int, int> p = std::make_pair(a, b);
|
||||
return &p;
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no "out" typemap for a non-const reference to a pair, so
|
||||
* this should return a wrapped instance of a std::pair instead of
|
||||
* the native "array" type for the target language.
|
||||
*/
|
||||
std::pair<int, int>& makeIntPairRef(int a, int b) {
|
||||
static std::pair<int, int> p = std::make_pair(a, b);
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* There is no "out" typemap for a const reference to a pair, so
|
||||
* this should return a wrapped instance of a std::pair
|
||||
* instead of the native "array" type for the target language.
|
||||
*/
|
||||
const std::pair<int, int> & makeIntPairConstRef(int a, int b) {
|
||||
static std::pair<int, int> p = std::make_pair(a, b);
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Test the "in" typemap for pair<T, U> */
|
||||
int product1(std::pair<int, int> p) {
|
||||
return p.first*p.second;
|
||||
}
|
||||
|
||||
/* Test the "in" typemap for const pair<T, U>& */
|
||||
int product2(const std::pair<int, int>& p) {
|
||||
return p.first*p.second;
|
||||
}
|
||||
|
||||
std::pair<int, int>
|
||||
p_ident(std::pair<int, int> p, const std::pair<int, int>& q) {
|
||||
return p;
|
||||
}
|
||||
|
||||
#if 0
|
||||
std::pair<char, char>
|
||||
p_ident(std::pair<char, char> p, const std::pair<char, char>& q) {
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Test the "in" typemap for const pair<T, U>* */
|
||||
std::pair<A, B>
|
||||
p_ident(std::pair<A, B> p, const std::pair<A, B>& q) {
|
||||
return q;
|
||||
}
|
||||
|
||||
/* Test the "in" typemap for const pair<T, U>* */
|
||||
std::pair<int, A>
|
||||
p_ident(std::pair<int, A> p, const std::pair<int, A>& q) {
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
std::pair<int, int>
|
||||
p_ident(std::pair<int, int> p, const std::pair<A, int>& q) {
|
||||
return p;
|
||||
}
|
||||
|
||||
std::pair<int, int>
|
||||
p_ident(std::pair<int, int> p, const std::pair<A, B>& q) {
|
||||
return p;
|
||||
}
|
||||
#endif
|
||||
%}
|
||||
|
||||
63
SWIG/Examples/test-suite/python/lib_std_vectora.i
Normal file
63
SWIG/Examples/test-suite/python/lib_std_vectora.i
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
%module lib_std_vectora
|
||||
|
||||
%include std_vectora.i
|
||||
|
||||
%{
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <numeric>
|
||||
%}
|
||||
|
||||
|
||||
%template(vector_i) std::vector<int, std::allocator<int> >;
|
||||
|
||||
%template(matrix_i) std::vector<std::vector<int,std::allocator<int> >,std::allocator<std::vector<int,std::allocator<int> > > >;
|
||||
|
||||
%inline
|
||||
{
|
||||
typedef
|
||||
std::vector<std::vector<int,std::allocator<int> >,
|
||||
std::allocator<std::vector<int,std::allocator<int> > > >
|
||||
imatrix;
|
||||
|
||||
std::vector<int> vident(const std::vector<int>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
imatrix mident(const imatrix& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
%template(DoubleVector) std::vector<double, std::allocator<double> >;
|
||||
|
||||
%inline %{
|
||||
typedef float Real;
|
||||
%}
|
||||
|
||||
namespace std {
|
||||
%template(RealVector) vector<Real, std::allocator<Real> >;
|
||||
}
|
||||
|
||||
%inline %{
|
||||
|
||||
double average(std::vector<int> v) {
|
||||
return std::accumulate(v.begin(),v.end(),0.0)/v.size();
|
||||
}
|
||||
|
||||
std::vector<float> half(const std::vector<float>& v) {
|
||||
std::vector<float> w(v);
|
||||
for (unsigned int i=0; i<w.size(); i++)
|
||||
w[i] /= 2.0;
|
||||
return w;
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
%template(IntPtrVector) std::vector<int *,std::allocator<int *> >;
|
||||
|
||||
|
||||
|
||||
130
SWIG/Examples/test-suite/python/std_containers.i
Normal file
130
SWIG/Examples/test-suite/python/std_containers.i
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
%module std_containers
|
||||
|
||||
%include std_vector.i
|
||||
%include std_string.i
|
||||
%include std_deque.i
|
||||
%include std_list.i
|
||||
%include std_set.i
|
||||
%include std_pair.i
|
||||
%include std_map.i
|
||||
%include std_complex.i
|
||||
|
||||
|
||||
%template() std::vector< std::vector<double > > ;
|
||||
%template(ccube) std::vector< std::vector< std::vector<double > > >;
|
||||
|
||||
%inline
|
||||
{
|
||||
typedef
|
||||
std::vector<std::vector<std::vector<double > > >
|
||||
ccube;
|
||||
|
||||
ccube cident(const ccube& c)
|
||||
{
|
||||
return c;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
%template(map_si) std::map<std::string, int>;
|
||||
%template(set_i) std::set<int>;
|
||||
%template(multiset_i) std::multiset<int>;
|
||||
%template(list_i) std::list<int>;
|
||||
%template(deque_i) std::deque<int>;
|
||||
|
||||
%template(vector_i) std::vector<int>;
|
||||
%template(vector_c) std::vector<std::complex<double> >;
|
||||
%template(vector_ui) std::vector<unsigned int>;
|
||||
|
||||
%template(imatrix) std::vector<std::vector<int> >;
|
||||
%template(cmatrix) std::vector<std::vector<std::complex<double> > >;
|
||||
|
||||
%inline
|
||||
{
|
||||
typedef std::vector<std::vector<int> > imatrix;
|
||||
imatrix mident(const imatrix& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
std::map<int,int> mapidenti(const std::map<int,int>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
std::map<std::string,int> mapident(const std::map<std::string,int>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<int> vident(const std::vector<int>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
std::set<int> sident(const std::set<int>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
std::vector<unsigned int> videntu(const std::vector<unsigned int>& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
int get_elem(const std::vector<int>& v, int index)
|
||||
{
|
||||
return v[index];
|
||||
}
|
||||
|
||||
std::pair<int,int> pident(const std::pair<int,int>& p)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
%{
|
||||
|
||||
template <class C> struct Param
|
||||
{
|
||||
};
|
||||
%}
|
||||
|
||||
|
||||
template <class C> struct Param
|
||||
{
|
||||
};
|
||||
|
||||
|
||||
%template(Param_c) Param<std::complex<double> >;
|
||||
%inline
|
||||
{
|
||||
int hello(Param<std::complex<double> > c)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
%inline
|
||||
{
|
||||
struct A
|
||||
{
|
||||
};
|
||||
}
|
||||
|
||||
%template() std::pair<A,int>;
|
||||
%template(pair_iA) std::pair<int,A>;
|
||||
|
||||
|
||||
%inline {
|
||||
std::pair<A,int> ident(std::pair<int,A> a, const std::pair<int,int>& b)
|
||||
{
|
||||
return std::pair<A,int>();
|
||||
}
|
||||
}
|
||||
|
||||
53
SWIG/Examples/test-suite/python/std_containers_runme.py
Normal file
53
SWIG/Examples/test-suite/python/std_containers_runme.py
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import sys
|
||||
import std_containers
|
||||
|
||||
|
||||
cube = (((1, 2), (3, 4)), ((5, 6), (7, 8)))
|
||||
|
||||
|
||||
if cube != std_containers.cident(cube):
|
||||
raise RuntimeError, "bad cident"
|
||||
|
||||
|
||||
p = (1,2)
|
||||
if p != std_containers.pident(p):
|
||||
raise RuntimeError, "bad pident"
|
||||
|
||||
v = (1,2,3,4,5,6)
|
||||
|
||||
if v != std_containers.vident(v):
|
||||
raise RuntimeError, "bad pident"
|
||||
|
||||
|
||||
if v != std_containers.videntu(v):
|
||||
raise RuntimeError, "bad videntu"
|
||||
|
||||
vu = std_containers.vector_ui(v)
|
||||
if vu[2] != std_containers.videntu(vu)[2]:
|
||||
raise RuntimeError, "bad videntu"
|
||||
|
||||
|
||||
if v[0:3] != vu[0:3]:
|
||||
raise RuntimeError, "bad getslice"
|
||||
|
||||
|
||||
m = ((1,2,3),(2,3),(3,4))
|
||||
if m != std_containers.mident(m):
|
||||
raise RuntimeError, "bad getslice"
|
||||
|
||||
|
||||
mi = std_containers.imatrix(m)
|
||||
mc = std_containers.cmatrix(m)
|
||||
if mi[0][1] != mc[0][1]:
|
||||
raise RuntimeError, "bad matrix"
|
||||
|
||||
|
||||
map ={}
|
||||
map['hello'] = 1
|
||||
map['hi'] = 2
|
||||
map['3'] = 2
|
||||
|
||||
if map != std_containers.mapident(map):
|
||||
raise RuntimeError, "bad map"
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue