diff --git a/extclass_demo.cpp b/extclass_demo.cpp index dd6c13f5..7b678120 100644 --- a/extclass_demo.cpp +++ b/extclass_demo.cpp @@ -290,6 +290,85 @@ long range_hash(const Range& r) return r.m_start * 123 + r.m_finish; } +/************************************************************/ +/* */ +/* some functions to test overloading */ +/* */ +/************************************************************/ + +static std::string testVoid() +{ + return std::string("Hello world!"); +} + +static int testInt(int i) +{ + return i; +} + +static std::string testString(std::string i) +{ + return i; +} + +static int test2(int i1, int i2) +{ + return i1+i2; +} + +static int test3(int i1, int i2, int i3) +{ + return i1+i2+i3; +} + +static int test4(int i1, int i2, int i3, int i4) +{ + return i1+i2+i3+i4; +} + +static int test5(int i1, int i2, int i3, int i4, int i5) +{ + return i1+i2+i3+i4+i5; +} + +/************************************************************/ +/* */ +/* a class to test overloading */ +/* */ +/************************************************************/ + +struct OverloadTest +{ + OverloadTest(): x_(1000) {} + OverloadTest(int x): x_(x) {} + OverloadTest(int x,int y): x_(x+y) { } + OverloadTest(int x,int y,int z): x_(x+y+z) {} + OverloadTest(int x,int y,int z, int a): x_(x+y+z+a) {} + OverloadTest(int x,int y,int z, int a, int b): x_(x+y+z+a+b) {} + + int x() const { return x_; } + void setX(int x) { x_ = x; } + + int p1(int x) { return x; } + int p2(int x, int y) { return x + y; } + int p3(int x, int y, int z) { return x + y + z; } + int p4(int x, int y, int z, int a) { return x + y + z + a; } + int p5(int x, int y, int z, int a, int b) { return x + y + z + a + b; } + private: + int x_; +}; + +static int getX(OverloadTest * u) +{ + return u->x(); +} + +/************************************************************/ +/* */ +/* init the module */ +/* */ +/************************************************************/ + void init_module(py::Module& m) { m.add(new Foo::PythonClass); @@ -321,6 +400,32 @@ void init_module(py::Module& m) range.def(&range_hash, "__hash__"); range.def_readonly(&Range::m_start, "start"); range.def_readonly(&Range::m_finish, "finish"); + + m.def(&testVoid, "overloaded"); + m.def(&testInt, "overloaded"); + m.def(&testString, "overloaded"); + m.def(&test2, "overloaded"); + m.def(&test3, "overloaded"); + m.def(&test4, "overloaded"); + m.def(&test5, "overloaded"); + + py::ClassWrapper over(m, "OverloadTest"); + over.def(py::Constructor<>()); + over.def(py::Constructor()); + over.def(py::Constructor()); + over.def(py::Constructor()); + over.def(py::Constructor()); + over.def(py::Constructor()); + over.def(py::Constructor()); + over.def(&getX, "getX"); + over.def(&OverloadTest::setX, "setX"); + over.def(&OverloadTest::x, "overloaded"); + over.def(&OverloadTest::p1, "overloaded"); + over.def(&OverloadTest::p2, "overloaded"); + over.def(&OverloadTest::p3, "overloaded"); + over.def(&OverloadTest::p4, "overloaded"); + over.def(&OverloadTest::p5, "overloaded"); + } void init_module() diff --git a/py_cpp_20001013.zip b/py_cpp_20001013.zip deleted file mode 100644 index 420d2bc4..00000000 Binary files a/py_cpp_20001013.zip and /dev/null differ diff --git a/test_extclass.py b/test_extclass.py index a458dbb0..138a7107 100644 --- a/test_extclass.py +++ b/test_extclass.py @@ -412,6 +412,80 @@ Testing __call__: 0 >>> comparator(couple2, couple) 1 + +Testing overloaded free functions + >>> overloaded() + 'Hello world!' + >>> overloaded(1) + 1 + >>> overloaded('foo') + 'foo' + >>> overloaded(1,2) + 3 + >>> overloaded(1,2,3) + 6 + >>> overloaded(1,2,3,4) + 10 + >>> overloaded(1,2,3,4,5) + 15 + >>> try: overloaded(1, 'foo') + ... except TypeError, err: + ... assert re.match("No overloaded functions match \(int, string\)\. Candidates are:", + ... str(err)) + ... else: + ... print 'no exception' + +Testing overloaded constructors + + >>> x = OverloadTest() + >>> x.getX() + 1000 + >>> x = OverloadTest(1) + >>> x.getX() + 1 + >>> x = OverloadTest(1,1) + >>> x.getX() + 2 + >>> x = OverloadTest(1,1,1) + >>> x.getX() + 3 + >>> x = OverloadTest(1,1,1,1) + >>> x.getX() + 4 + >>> x = OverloadTest(1,1,1,1,1) + >>> x.getX() + 5 + >>> x = OverloadTest(x) + >>> x.getX() + 5 + >>> try: x = OverloadTest(1, 'foo') + ... except TypeError, err: + ... assert re.match("No overloaded functions match \(OverloadTest, int, string\)\. Candidates are:", + ... str(err)) + ... else: + ... print 'no exception' + +Testing overloaded methods + + >>> x.setX(3) + >>> x.overloaded() + 3 + >>> x.overloaded(1) + 1 + >>> x.overloaded(1,1) + 2 + >>> x.overloaded(1,1,1) + 3 + >>> x.overloaded(1,1,1,1) + 4 + >>> x.overloaded(1,1,1,1,1) + 5 + >>> try: x.overloaded(1,'foo') + ... except TypeError, err: + ... assert re.match("No overloaded functions match \(OverloadTest, int, string\)\. Candidates are:", + ... str(err)) + ... else: + ... print 'no exception' ''' from demo import *