From 08e16bed8edb401021b67d558c772efa8d047e8a Mon Sep 17 00:00:00 2001 From: Mark Rose Date: Fri, 7 Mar 2003 10:30:11 +0000 Subject: [PATCH] four new director tests git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4449 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/test-suite/common.mk | 4 ++ Examples/test-suite/director_basic.i | 26 +++++++++ Examples/test-suite/director_exception.i | 54 +++++++++++++++++++ Examples/test-suite/director_finalizer.i | 42 +++++++++++++++ Examples/test-suite/director_unroll.i | 42 +++++++++++++++ .../test-suite/python/director_basic_runme.py | 24 +++++++++ .../python/director_exception_runme.py | 43 +++++++++++++++ .../python/director_finalizer_runme.py | 47 ++++++++++++++++ .../python/director_unroll_runme.py | 17 ++++++ 9 files changed, 299 insertions(+) create mode 100644 Examples/test-suite/director_basic.i create mode 100644 Examples/test-suite/director_exception.i create mode 100644 Examples/test-suite/director_finalizer.i create mode 100644 Examples/test-suite/director_unroll.i create mode 100644 Examples/test-suite/python/director_basic_runme.py create mode 100644 Examples/test-suite/python/director_exception_runme.py create mode 100644 Examples/test-suite/python/director_finalizer_runme.py create mode 100644 Examples/test-suite/python/director_unroll_runme.py diff --git a/Examples/test-suite/common.mk b/Examples/test-suite/common.mk index b0e5e2b15..e6c5b632d 100644 --- a/Examples/test-suite/common.mk +++ b/Examples/test-suite/common.mk @@ -97,6 +97,10 @@ CPP_TEST_CASES += \ default_constructor \ default_ns \ default_ref \ + director_basic \ + director_exception \ + director_finalizer \ + director_unroll \ dynamic_cast \ enum_plus \ enum_scope \ diff --git a/Examples/test-suite/director_basic.i b/Examples/test-suite/director_basic.i new file mode 100644 index 000000000..630218b3d --- /dev/null +++ b/Examples/test-suite/director_basic.i @@ -0,0 +1,26 @@ +%module(directors="1") director_basic +%{ +#include + +class Foo { +public: + virtual ~Foo() {} + virtual std::string ping() { return "Foo::ping()"; } + virtual std::string pong() { return "Foo::pong();" + ping(); } +}; + +%} + +%include "typemaps.i" +%include "exception.i" +%include "std_string.i" + +%feature("director") Foo; + +class Foo { +public: + virtual ~Foo() {} + virtual std::string ping() { return "Foo::ping()"; } + virtual std::string pong() { return "Foo::pong();" + ping(); } +}; + diff --git a/Examples/test-suite/director_exception.i b/Examples/test-suite/director_exception.i new file mode 100644 index 000000000..5107654c9 --- /dev/null +++ b/Examples/test-suite/director_exception.i @@ -0,0 +1,54 @@ +%module(directors="1") director_exception +%{ + +#include + +class Foo { +public: + virtual ~Foo() {} + virtual std::string ping() { return "Foo::ping()"; } + virtual std::string pong() { return "Foo::pong();" + ping(); } +}; + +Foo *launder(Foo *f) { + return f; +} + +// define dummy director exception classes to prevent spurious errors +// in target languages that do not support directors. + +#ifndef SWIG_DIRECTORS +class SWIG_DIRECTOR_EXCEPTION {}; +class SWIG_DIRECTOR_METHOD_EXCEPTION: public SWIG_DIRECTOR_EXCEPTION {}; +#endif + +%} + +%include "typemaps.i" +%include "exception.i" +%include "std_string.i" + + +%feature("director:except") { + if ($error != NULL) { + throw SWIG_DIRECTOR_METHOD_EXCEPTION(); + } +} + +%exception { + try { $action } + catch (SWIG_DIRECTOR_EXCEPTION &e) { SWIG_fail; } +} + + +%feature("director") Foo; + +class Foo { +public: + virtual ~Foo() {} + virtual std::string ping() { return "Foo::ping()"; } + virtual std::string pong() { return "Foo::pong();" + ping(); } +}; + +Foo *launder(Foo *f); + diff --git a/Examples/test-suite/director_finalizer.i b/Examples/test-suite/director_finalizer.i new file mode 100644 index 000000000..52cdb1f52 --- /dev/null +++ b/Examples/test-suite/director_finalizer.i @@ -0,0 +1,42 @@ +%module(directors="1") director_finalizer +%{ + +int status = 0; + +class Foo { +public: + virtual ~Foo() { orStatus(1); } + virtual void orStatus(int x) { status |= x; } +}; + +void deleteFoo(Foo *f) { + delete f; +} + +Foo *launder(Foo *f) { + return f; +} + +int getStatus() { + return status; +} + +void resetStatus() { + status = 0; +} + +%} + +%feature("director") Foo; + +class Foo { +public: + virtual ~Foo(); + virtual void orStatus(int x); +}; + +void deleteFoo(Foo *f); +int getStatus(); +Foo *launder(Foo *f); +void resetStatus(); + diff --git a/Examples/test-suite/director_unroll.i b/Examples/test-suite/director_unroll.i new file mode 100644 index 000000000..f5f153308 --- /dev/null +++ b/Examples/test-suite/director_unroll.i @@ -0,0 +1,42 @@ +%module(directors="1") director_unroll +%{ +#include + +class Foo { +public: + virtual ~Foo() {} + virtual std::string ping() { return "Foo::ping()"; } + virtual std::string pong() { return "Foo::pong();" + ping(); } +}; + +class Bar { +private: + Foo *foo; +public: + void set(Foo *x) { foo = x; } + Foo *get() { return foo; } +}; + +%} + +%include "typemaps.i" +%include "exception.i" +%include "std_string.i" + +%feature("director") Foo; + +class Foo { +public: + virtual ~Foo() {} + virtual std::string ping() { return "Foo::ping()"; } + virtual std::string pong() { return "Foo::pong();" + ping(); } +}; + +class Bar { +private: + Foo *foo; +public: + void set(Foo *x) { foo = x; } + Foo *get() { return foo; } +}; + diff --git a/Examples/test-suite/python/director_basic_runme.py b/Examples/test-suite/python/director_basic_runme.py new file mode 100644 index 000000000..7a8d3f59c --- /dev/null +++ b/Examples/test-suite/python/director_basic_runme.py @@ -0,0 +1,24 @@ +import director_basic + +class MyFoo(director_basic.Foo): + def ping(self): + return "MyFoo::ping()" + + +a = MyFoo() + +if a.ping() != "MyFoo::ping()": + raise RuntimeError, a.ping() + +if a.pong() != "Foo::pong();MyFoo::ping()": + raise RuntimeError, a.pong() + +b = director_basic.Foo() + +if b.ping() != "Foo::ping()": + raise RuntimeError, b.ping() + +if b.pong() != "Foo::pong();Foo::ping()": + raise RuntimeError, b.pong() + + diff --git a/Examples/test-suite/python/director_exception_runme.py b/Examples/test-suite/python/director_exception_runme.py new file mode 100644 index 000000000..76f159da7 --- /dev/null +++ b/Examples/test-suite/python/director_exception_runme.py @@ -0,0 +1,43 @@ +from director_exception import * +from exceptions import * + +class MyFoo(Foo): + def ping(self): + raise NotImplementedError, "MyFoo::ping() EXCEPTION" + +class MyFoo2(Foo): + def ping(self): + pass # error: should return a string + +ok = 0 + +a = MyFoo() +b = launder(a) + +try: + b.pong() +except NotImplementedError, e: + ok = 1 +except: + pass + +if not ok: + raise RuntimeError + +ok = 0 + +a = MyFoo2() +b = launder(a) + +try: + b.pong() +except TypeError, e: + ok = 1 +except: + pass + +if not ok: + raise RuntimeError + + + diff --git a/Examples/test-suite/python/director_finalizer_runme.py b/Examples/test-suite/python/director_finalizer_runme.py new file mode 100644 index 000000000..0be55793c --- /dev/null +++ b/Examples/test-suite/python/director_finalizer_runme.py @@ -0,0 +1,47 @@ +from director_finalizer import * + +class MyFoo(Foo): + def __del__(self): + self.orStatus(2) + Foo.__del__(self) + + +resetStatus() + +a = MyFoo() +del a + +if getStatus() != 3: + raise RuntimeError + +resetStatus() + +a = MyFoo() +launder(a) + +if getStatus() != 0: + raise RuntimeError + +del a + +if getStatus() != 3: + raise RuntimeError + +resetStatus() + +a = MyFoo().__disown__() +deleteFoo(a) + +if getStatus() != 3: + raise RuntimeError + +resetStatus() + +a = MyFoo().__disown__() +deleteFoo(launder(a)) + +if getStatus() != 3: + raise RuntimeError + +resetStatus() + diff --git a/Examples/test-suite/python/director_unroll_runme.py b/Examples/test-suite/python/director_unroll_runme.py new file mode 100644 index 000000000..5279d6fc5 --- /dev/null +++ b/Examples/test-suite/python/director_unroll_runme.py @@ -0,0 +1,17 @@ +import director_unroll + +class MyFoo(director_unroll.Foo): + def ping(self): + return "MyFoo::ping()" + + +a = MyFoo() + +b = director_unroll.Bar() + +b.set(a) +c = b.get() + +if not (a is c): + raise RuntimeError +