add callback and inout tests
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@6281 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
6b9fdc6c86
commit
9ae33f3c4d
5 changed files with 171 additions and 1 deletions
|
|
@ -12,17 +12,19 @@ top_builddir = @top_builddir@
|
|||
CPP_TEST_CASES += \
|
||||
argcargvtest \
|
||||
attributetest \
|
||||
callback \
|
||||
complextest \
|
||||
director_stl \
|
||||
file_test \
|
||||
implicittest \
|
||||
inout \
|
||||
inplaceadd \
|
||||
lib_std_except \
|
||||
lib_std_vectora \
|
||||
lib_std_map \
|
||||
lib_std_wstring \
|
||||
primitive_types \
|
||||
nondynamic \
|
||||
primitive_types \
|
||||
std_containers \
|
||||
|
||||
C_TEST_CASES += \
|
||||
|
|
|
|||
72
SWIG/Examples/test-suite/python/callback.i
Normal file
72
SWIG/Examples/test-suite/python/callback.i
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
%module callback
|
||||
|
||||
%pythoncallback(1);
|
||||
%pythoncallback(1) foo;
|
||||
%pythoncallback(1) A::bar;
|
||||
%pythoncallback(1) A::foom;
|
||||
%pythoncallback(1) foo_T;
|
||||
|
||||
%inline %{
|
||||
|
||||
|
||||
int foo(int a) {
|
||||
return a;
|
||||
}
|
||||
|
||||
struct A
|
||||
{
|
||||
static int bar(int a) {
|
||||
return 2*a;
|
||||
}
|
||||
|
||||
int foom(int a)
|
||||
{
|
||||
return -a;
|
||||
}
|
||||
|
||||
friend int foof(int a)
|
||||
{
|
||||
return 3*a;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int foobar(int a, int (*pf)(int a)) {
|
||||
return pf(a);
|
||||
}
|
||||
|
||||
int foobarm(int a, A ap, int (A::*pf)(int a)) {
|
||||
return (ap.*pf)(a);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T foo_T(T a)
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T foo_T(T a, T b)
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
|
||||
template <class T>
|
||||
T foobar_T(T a, T (*pf)(T a)) {
|
||||
return pf(a);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const T& ident(const T& x) {
|
||||
return x;
|
||||
}
|
||||
%}
|
||||
|
||||
%template(foo_i) foo_T<int>;
|
||||
%template(foobar_i) foobar_T<int>;
|
||||
|
||||
%template(foo_d) foo_T<double>;
|
||||
%template(foobar_d) foobar_T<double>;
|
||||
|
||||
%template(ident_d) ident<double>;
|
||||
31
SWIG/Examples/test-suite/python/callback_runme.py
Normal file
31
SWIG/Examples/test-suite/python/callback_runme.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import _callback
|
||||
from callback import *
|
||||
|
||||
if foo(2) != 2:
|
||||
raise RuntimeError
|
||||
|
||||
if A.bar(2) != 4:
|
||||
raise RuntimeError
|
||||
|
||||
if foobar(3, _callback.foo) != foo(3):
|
||||
raise RuntimeError
|
||||
|
||||
if foobar(3, foo) != foo(3):
|
||||
raise RuntimeError
|
||||
|
||||
if foobar(3, A.bar) != A.bar(3):
|
||||
raise RuntimeError
|
||||
|
||||
if foobar(3, foof) != foof(3):
|
||||
raise RuntimeError
|
||||
|
||||
if foobar_i(3, foo_i) != foo_i(3):
|
||||
raise RuntimeError
|
||||
|
||||
|
||||
if foobar_d(3.5, foo_d) != foo_d(3.5):
|
||||
raise RuntimeError
|
||||
|
||||
a = A()
|
||||
if foobarm(3, a, A.foom_cb_ptr) != a.foom(3):
|
||||
raise RuntimeError
|
||||
43
SWIG/Examples/test-suite/python/inout.i
Normal file
43
SWIG/Examples/test-suite/python/inout.i
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
%module inout
|
||||
|
||||
%include "typemaps.i"
|
||||
%include "std_pair.i"
|
||||
|
||||
%{
|
||||
inline void AddOne3(double* a, double* b, double* c) {
|
||||
*a += 1;
|
||||
*b += 1;
|
||||
*c += 1;
|
||||
}
|
||||
|
||||
inline void AddOne1(double* a) {
|
||||
*a += 1;
|
||||
}
|
||||
|
||||
inline void AddOne1p(std::pair<double, double>* p) {
|
||||
p->first += 1;
|
||||
p->second += 1;
|
||||
}
|
||||
|
||||
inline void AddOne2p(std::pair<double, double>* p,double* a) {
|
||||
*a += 1;
|
||||
p->first += 1;
|
||||
p->second += 1;
|
||||
}
|
||||
|
||||
inline void AddOne3p(double* a, std::pair<double, double>* p,double* b) {
|
||||
*a += 1;
|
||||
*b += 1;
|
||||
p->first += 1;
|
||||
p->second += 1;
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
%template() std::pair<double, double>;
|
||||
|
||||
void AddOne1(double* INOUT);
|
||||
void AddOne3(double* INOUT, double* INOUT, double* INOUT);
|
||||
void AddOne1p(std::pair<double, double>* INOUT);
|
||||
void AddOne2p(std::pair<double, double>* INOUT, double* INOUT);
|
||||
void AddOne3p(double* INOUT, std::pair<double, double>* INOUT, double* INOUT);
|
||||
22
SWIG/Examples/test-suite/python/inout_runme.py
Normal file
22
SWIG/Examples/test-suite/python/inout_runme.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import inout
|
||||
|
||||
a = inout.AddOne1(1)
|
||||
if a != 2:
|
||||
raise RuntimeError
|
||||
|
||||
a = inout.AddOne3(1,1,1)
|
||||
if a != [2,2,2]:
|
||||
raise RuntimeError
|
||||
|
||||
a = inout.AddOne1p((1,1))
|
||||
if a != (2,2):
|
||||
raise RuntimeError
|
||||
|
||||
a = inout.AddOne2p((1,1),1)
|
||||
if a != [(2,2),2]:
|
||||
raise RuntimeError
|
||||
|
||||
a = inout.AddOne3p(1,(1,1),1)
|
||||
if a != [2,(2,2),2]:
|
||||
raise RuntimeError
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue