new tests for string and directors+containers
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5772 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
a672ff61ef
commit
c8d7e4d971
4 changed files with 189 additions and 0 deletions
66
SWIG/Examples/test-suite/python/director_stl.i
Normal file
66
SWIG/Examples/test-suite/python/director_stl.i
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
%module(directors="1") director_stl
|
||||
|
||||
%include "std_string.i"
|
||||
%include "std_pair.i"
|
||||
%include "std_vector.i"
|
||||
|
||||
%feature("director") Foo;
|
||||
|
||||
%feature("director:except") {
|
||||
if ($error != NULL) {
|
||||
throw Swig::DirectorMethodException();
|
||||
}
|
||||
}
|
||||
|
||||
%exception {
|
||||
try { $action }
|
||||
catch (...) { SWIG_fail; }
|
||||
}
|
||||
|
||||
%inline
|
||||
{
|
||||
class Foo {
|
||||
public:
|
||||
virtual ~Foo() {}
|
||||
|
||||
virtual std::string& bar(std::string& s)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
virtual std::string ping(std::string s) = 0;
|
||||
virtual std::string pong(const std::string& s)
|
||||
{ return std::string("Foo::pong:") + s + ":" + ping(s); }
|
||||
|
||||
std::string tping(std::string s) { return ping(s); }
|
||||
std::string tpong(const std::string& s) { return pong(s); }
|
||||
|
||||
virtual std::pair<double, int>
|
||||
pident(const std::pair<double, int>& p) { return p; }
|
||||
|
||||
virtual std::vector<int>
|
||||
vident(const std::vector<int>& p) { return p; }
|
||||
|
||||
virtual std::vector<int>
|
||||
vsecond(const std::vector<int>& p, const std::vector<int>& s) { return s; }
|
||||
|
||||
std::pair<double, int>
|
||||
tpident(const std::pair<double, int>& p) { return pident(p); }
|
||||
|
||||
std::vector<int>
|
||||
tvident(const std::vector<int>& p) { return vident(p); }
|
||||
|
||||
virtual std::vector<int>
|
||||
tvsecond(const std::vector<int>& p, const std::vector<int>& s) { return vsecond(p,s); }
|
||||
|
||||
|
||||
virtual std::vector<std::string>
|
||||
vidents(const std::vector<std::string>& p) { return p; }
|
||||
|
||||
std::vector<std::string>
|
||||
tvidents(const std::vector<std::string>& p) { return vidents(p); }
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
39
SWIG/Examples/test-suite/python/director_stl_runme.py
Normal file
39
SWIG/Examples/test-suite/python/director_stl_runme.py
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import director_stl
|
||||
|
||||
class MyFoo(director_stl.Foo):
|
||||
def ping(self, s):
|
||||
return "MyFoo::ping():" + s
|
||||
|
||||
def pident(self, arg):
|
||||
return arg
|
||||
|
||||
def vident(self,v):
|
||||
return v
|
||||
|
||||
def vidents(self,v):
|
||||
return v
|
||||
|
||||
def vsecond(self,v1,v2):
|
||||
return v2
|
||||
|
||||
|
||||
a = MyFoo()
|
||||
|
||||
a.tping("hello")
|
||||
a.tpong("hello")
|
||||
|
||||
p = (1,2)
|
||||
a.pident(p)
|
||||
v = (3,4)
|
||||
a.vident(v)
|
||||
|
||||
a.tpident(p)
|
||||
a.tvident(v)
|
||||
|
||||
v1 = (3,4)
|
||||
v2 = (5,6)
|
||||
a.tvsecond(v1,v2)
|
||||
|
||||
vs=("hi", "hello")
|
||||
vs
|
||||
a.tvidents(vs)
|
||||
57
SWIG/Examples/test-suite/python/lib_std_string.i
Normal file
57
SWIG/Examples/test-suite/python/lib_std_string.i
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
%module lib_std_string
|
||||
%include "std_string.i"
|
||||
|
||||
%template(string) std::basic_string<char>;
|
||||
%inline %{
|
||||
|
||||
struct A : std::string
|
||||
{
|
||||
A(const std::string& s) : std::string(s)
|
||||
{
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
std::string test_value(std::string x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
const std::string& test_const_reference(const std::string &x) {
|
||||
return x;
|
||||
}
|
||||
|
||||
void test_pointer(std::string *x) {
|
||||
}
|
||||
|
||||
std::string *test_pointer_out() {
|
||||
static std::string x = "x";
|
||||
return &x;
|
||||
}
|
||||
|
||||
void test_const_pointer(const std::string *x) {
|
||||
}
|
||||
|
||||
const std::string *test_const_pointer_out() {
|
||||
static std::string x = "x";
|
||||
return &x;
|
||||
}
|
||||
|
||||
void test_reference(std::string &x) {
|
||||
}
|
||||
|
||||
std::string& test_reference_out() {
|
||||
static std::string x = "x";
|
||||
return x;
|
||||
}
|
||||
|
||||
void test_throw() throw(std::string){
|
||||
static std::string x = "x";
|
||||
|
||||
throw x;
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
|
||||
27
SWIG/Examples/test-suite/python/lib_std_string_runme.py
Normal file
27
SWIG/Examples/test-suite/python/lib_std_string_runme.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import lib_std_string
|
||||
|
||||
x="hello"
|
||||
|
||||
if lib_std_string.test_value(x) != x:
|
||||
raise RuntimeError, "bad string mapping"
|
||||
|
||||
if lib_std_string.test_const_reference(x) != x:
|
||||
raise RuntimeError, "bad string mapping"
|
||||
|
||||
|
||||
s = lib_std_string.string("he")
|
||||
s.append("llo")
|
||||
|
||||
if lib_std_string.test_value(s) != x:
|
||||
raise RuntimeError, "bad string mapping"
|
||||
|
||||
if lib_std_string.test_const_reference(s) != x:
|
||||
raise RuntimeError, "bad string mapping"
|
||||
|
||||
a = lib_std_string.A("hello")
|
||||
|
||||
if lib_std_string.test_value(a) != x:
|
||||
raise RuntimeError, "bad string mapping"
|
||||
|
||||
if lib_std_string.test_const_reference(a) != x:
|
||||
raise RuntimeError, "bad string mapping"
|
||||
Loading…
Add table
Add a link
Reference in a new issue