From d52f0c7d40ea7802714013534c865fc0d020ccc2 Mon Sep 17 00:00:00 2001 From: Bruno da Silva de Oliveira Date: Sat, 31 May 2003 21:18:09 +0000 Subject: [PATCH] - New tests [SVN r18629] --- pyste/tests/.cvsignore | 1 - pyste/tests/SConstruct | 59 ------ pyste/tests/_smart_tr.cpp | 83 ++++++++ pyste/tests/basic.cpp | 8 + pyste/tests/basic.h | 59 ++++++ pyste/tests/basic.pyste | 5 + .../tests/{example_basicUT.py => basicUT.py} | 2 +- pyste/tests/enums.h | 24 +++ pyste/tests/enums.pyste | 8 + .../tests/{example_enumsUT.py => enumsUT.py} | 2 +- pyste/tests/header_test.h | 38 ++++ pyste/tests/header_test.pyste | 4 + ...mple_header_testUT.py => header_testUT.py} | 2 +- pyste/tests/inherit.h | 18 ++ pyste/tests/inherit.pyste | 8 + .../{example_inheritUT.py => inheritUT.py} | 2 +- pyste/tests/nested.cpp | 4 + pyste/tests/nested.h | 26 +++ pyste/tests/nested.pyste | 1 + .../tests/{example_nested.py => nestedUT.py} | 2 +- pyste/tests/nt_all.bat | 7 - pyste/tests/nt_build_all.bat | 16 -- pyste/tests/nt_build_pyste.bat | 8 - pyste/tests/nt_clean.bat | 24 --- pyste/tests/opaque.h | 47 +++++ pyste/tests/opaque.pyste | 5 + .../{example_opaqueUT.py => opaqueUT.py} | 2 +- pyste/tests/operators.cpp | 3 + pyste/tests/operators.h | 49 +++++ pyste/tests/operators.pyste | 2 + ...{example_operatorsUT.py => operatorsUT.py} | 2 +- pyste/tests/smart_ptr.h | 44 +++++ pyste/tests/smart_ptr.pyste | 13 ++ ...{example_smart_ptrUT.py => smart_ptrUT.py} | 2 +- pyste/tests/templates.h | 10 + pyste/tests/templates.pyste | 8 + ...{example_templatesUT.py => templatesUT.py} | 2 +- pyste/tests/test_all.py | 179 ++++++++++++++++++ pyste/tests/test_all_linux.py | 88 --------- pyste/tests/unions.h | 16 ++ pyste/tests/unions.pyste | 2 + pyste/tests/vars.h | 19 ++ pyste/tests/vars.pyste | 1 + pyste/tests/{example_varsUT.py => varsUT.py} | 10 +- pyste/tests/virtual.cpp | 70 +++++++ pyste/tests/virtual.h | 28 +++ pyste/tests/virtual.pyste | 2 + pyste/tests/virtual2.h | 27 +++ pyste/tests/virtual2.pyste | 4 + .../{example_virtual2.py => virtual2UT.py} | 2 +- .../{example_virtual.py => virtualUT.py} | 2 +- pyste/tests/wrappertest.h | 46 +++++ pyste/tests/wrappertest.pyste | 21 ++ ...mple_wrappertestUT.py => wrappertestUT.py} | 2 +- pyste/tests/wrappertest_wrappers.h | 28 +++ 55 files changed, 927 insertions(+), 220 deletions(-) delete mode 100644 pyste/tests/SConstruct create mode 100644 pyste/tests/_smart_tr.cpp create mode 100644 pyste/tests/basic.cpp create mode 100644 pyste/tests/basic.h create mode 100644 pyste/tests/basic.pyste rename pyste/tests/{example_basicUT.py => basicUT.py} (98%) create mode 100644 pyste/tests/enums.h create mode 100644 pyste/tests/enums.pyste rename pyste/tests/{example_enumsUT.py => enumsUT.py} (95%) create mode 100644 pyste/tests/header_test.h create mode 100644 pyste/tests/header_test.pyste rename pyste/tests/{example_header_testUT.py => header_testUT.py} (96%) create mode 100644 pyste/tests/inherit.h create mode 100644 pyste/tests/inherit.pyste rename pyste/tests/{example_inheritUT.py => inheritUT.py} (94%) create mode 100644 pyste/tests/nested.cpp create mode 100644 pyste/tests/nested.h create mode 100644 pyste/tests/nested.pyste rename pyste/tests/{example_nested.py => nestedUT.py} (93%) delete mode 100644 pyste/tests/nt_all.bat delete mode 100644 pyste/tests/nt_build_all.bat delete mode 100644 pyste/tests/nt_build_pyste.bat delete mode 100644 pyste/tests/nt_clean.bat create mode 100644 pyste/tests/opaque.h create mode 100644 pyste/tests/opaque.pyste rename pyste/tests/{example_opaqueUT.py => opaqueUT.py} (93%) create mode 100644 pyste/tests/operators.cpp create mode 100644 pyste/tests/operators.h create mode 100644 pyste/tests/operators.pyste rename pyste/tests/{example_operatorsUT.py => operatorsUT.py} (96%) create mode 100644 pyste/tests/smart_ptr.h create mode 100644 pyste/tests/smart_ptr.pyste rename pyste/tests/{example_smart_ptrUT.py => smart_ptrUT.py} (93%) create mode 100644 pyste/tests/templates.h create mode 100644 pyste/tests/templates.pyste rename pyste/tests/{example_templatesUT.py => templatesUT.py} (95%) create mode 100644 pyste/tests/test_all.py delete mode 100644 pyste/tests/test_all_linux.py create mode 100644 pyste/tests/unions.h create mode 100644 pyste/tests/unions.pyste create mode 100644 pyste/tests/vars.h create mode 100644 pyste/tests/vars.pyste rename pyste/tests/{example_varsUT.py => varsUT.py} (59%) create mode 100644 pyste/tests/virtual.cpp create mode 100644 pyste/tests/virtual.h create mode 100644 pyste/tests/virtual.pyste create mode 100644 pyste/tests/virtual2.h create mode 100644 pyste/tests/virtual2.pyste rename pyste/tests/{example_virtual2.py => virtual2UT.py} (96%) rename pyste/tests/{example_virtual.py => virtualUT.py} (97%) create mode 100644 pyste/tests/wrappertest.h create mode 100644 pyste/tests/wrappertest.pyste rename pyste/tests/{example_wrappertestUT.py => wrappertestUT.py} (94%) create mode 100644 pyste/tests/wrappertest_wrappers.h diff --git a/pyste/tests/.cvsignore b/pyste/tests/.cvsignore index 6e8f4170..8fe21f06 100644 --- a/pyste/tests/.cvsignore +++ b/pyste/tests/.cvsignore @@ -4,5 +4,4 @@ *.obj *.arg *.dll -*.cpp .sconsign diff --git a/pyste/tests/SConstruct b/pyste/tests/SConstruct deleted file mode 100644 index b23dfb76..00000000 --- a/pyste/tests/SConstruct +++ /dev/null @@ -1,59 +0,0 @@ -import glob -import sys -import os - -# constants -if sys.platform == 'win32': - BOOST_ROOT = 'D:/Programming/Libraries/boost-cvs' - STLPORT_ROOT = 'D:/Programming/Libraries/stlport-4.5.3' - PYTHON_ROOT = 'C:/Python' - -if BOOST_ROOT: - BOOST_INCLUDE = BOOST_ROOT + '/boost' - BOOST_LIB = BOOST_ROOT + '/lib' - -if STLPORT_ROOT: - STLPORT_INCLUDE = STLPORT_ROOT + '/stlport' - STLPORT_LIB = STLPORT_ROOT + '/lib' - -if PYTHON_ROOT: - PYTHON_INCLUDE = PYTHON_ROOT + '/include' - PYTHON_LIB = PYTHON_ROOT + '/libs' - -LIBS = ['boost_python', 'python22'] - -INCLUDES = ['../example'] -if sys.platform == 'win32': - CXX = 'icl' - CXXFLAGS='/GR /GX /MD /nologo' - INCLUDES += [BOOST_INCLUDE, STLPORT_INCLUDE, PYTHON_INCLUDE] - LIBPATH = [STLPORT_LIB, PYTHON_LIB, BOOST_LIB] + os.environ['LIB'].split(os.pathsep) -else: - CXX = 'g++' - CXXFLAGS = '' - LIBPATH = [] - #INCLUDES = ['..'] - -# Create the environment -env = Environment( - CXX=CXX, - CXXFLAGS=CXXFLAGS, - CPPPATH=INCLUDES, - LIBS=LIBS, - LIBPATH=LIBPATH) - - -# Build all the cpp files -modules = [os.path.splitext(os.path.basename(x))[0] for x in glob.glob('../example/*.pyste')] -for module in modules: - multiple = ARGUMENTS.get('multiple', '') - example_cpp = '../example/%s.cpp' % module - if os.path.isfile(example_cpp): - sources = [example_cpp] - else: - sources = [] - if multiple: - env.SharedLibrary(target=module, source=sources + glob.glob('_%s/*.cpp'%module)) - else: - env.SharedLibrary(target=module, source=sources + ['_%s.cpp' % module]) - diff --git a/pyste/tests/_smart_tr.cpp b/pyste/tests/_smart_tr.cpp new file mode 100644 index 00000000..f5bc2844 --- /dev/null +++ b/pyste/tests/_smart_tr.cpp @@ -0,0 +1,83 @@ + +// Includes ==================================================================== +#include +#include + +// Using ======================================================================= +using namespace boost::python; + +// Declarations ================================================================ + + +namespace { + + +struct smart_ptr_A_Wrapper: smart_ptr::A +{ + smart_ptr_A_Wrapper(PyObject* self_, const smart_ptr::A & p0): + smart_ptr::A(p0), self(self_) {} + + smart_ptr_A_Wrapper(PyObject* self_): + smart_ptr::A(), self(self_) {} + + int f() { + return call_method< int >(self, "f"); + } + + PyObject* self; +}; + + + +}// namespace + + +// Module ====================================================================== +BOOST_PYTHON_MODULE(_smart_tr) +{ + scope* smart_ptr_A_scope = new scope( + class_< smart_ptr::A, boost::noncopyable, smart_ptr_A_Wrapper >("A", init< >()) + ); + // Temporary code for smart pointers + objects::class_value_wrapper< + boost::shared_ptr< smart_ptr::A >, objects::make_ptr_instance< + smart_ptr::A, objects::pointer_holder< + boost::shared_ptr< smart_ptr::A >, smart_ptr::A > + > + >(); + delete smart_ptr_A_scope; + + scope* smart_ptr_C_scope = new scope( + class_< smart_ptr::C >("C", init< >()) + .def(init< const smart_ptr::C & >()) + .def_readwrite("value", &smart_ptr::C::value) + ); + // Temporary code for smart pointers + objects::class_value_wrapper< + boost::shared_ptr< smart_ptr::C >, objects::make_ptr_instance< + smart_ptr::C, objects::pointer_holder< + boost::shared_ptr< smart_ptr::C >, smart_ptr::C > + > + >(); + delete smart_ptr_C_scope; + + scope* smart_ptr_D_scope = new scope( + class_< smart_ptr::D >("D", init< >()) + .def(init< const smart_ptr::D & >()) + .def("Get", &smart_ptr::D::Get) + .def("Set", &smart_ptr::D::Set) + ); + // Temporary code for smart pointers + objects::class_value_wrapper< + std::auto_ptr< smart_ptr::D >, objects::make_ptr_instance< + smart_ptr::D, objects::pointer_holder< + std::auto_ptr< smart_ptr::D >, smart_ptr::D > + > + >(); + delete smart_ptr_D_scope; + + def("GetA", &smart_ptr::GetA); + def("NewA", &smart_ptr::NewA); + def("NewC", &smart_ptr::NewC); + def("NewD", &smart_ptr::NewD); +} diff --git a/pyste/tests/basic.cpp b/pyste/tests/basic.cpp new file mode 100644 index 00000000..9e6ed996 --- /dev/null +++ b/pyste/tests/basic.cpp @@ -0,0 +1,8 @@ +#include "basic.h" + +namespace basic { + + int C::static_value = 3; + const int C::const_static_value = 100; + +} diff --git a/pyste/tests/basic.h b/pyste/tests/basic.h new file mode 100644 index 00000000..5ab6ca58 --- /dev/null +++ b/pyste/tests/basic.h @@ -0,0 +1,59 @@ +#ifndef BASIC_H +#define BASIC_H + + +#include + +namespace basic { + +struct C +{ + // test virtuallity + C(): value(1), const_value(0) {} + virtual int f(int x = 10) + { + return x*2; + } + + int foo(int x=1){ + return x+1; + } + + const std::string& name() { return _name; } + void set_name(const std::string& name) { _name = name; } + std::string _name; + + // test data members + static int static_value; + static const int const_static_value; + + int value; + const int const_value; + + // test static functions + static int mul(int x=2, int y=3) { return x*y; } +}; + +inline int call_f(C& c) +{ + return c.f(); +} + +inline int call_f(C& c, int x) +{ + return c.f(x); +} + +inline int get_static() +{ + return C::static_value; +} + +inline int get_value(C& c) +{ + return c.value; +} + +} + +#endif diff --git a/pyste/tests/basic.pyste b/pyste/tests/basic.pyste new file mode 100644 index 00000000..4fe0b5b3 --- /dev/null +++ b/pyste/tests/basic.pyste @@ -0,0 +1,5 @@ +Class('basic::C', 'basic.h') +Function('basic::call_f', 'basic.h') +Function('basic::get_static', 'basic.h') +Function('basic::get_value', 'basic.h') + diff --git a/pyste/tests/example_basicUT.py b/pyste/tests/basicUT.py similarity index 98% rename from pyste/tests/example_basicUT.py rename to pyste/tests/basicUT.py index 3b6f1d5c..23cd8655 100644 --- a/pyste/tests/example_basicUT.py +++ b/pyste/tests/basicUT.py @@ -1,5 +1,5 @@ import unittest -from basic import * +from _basic import * class BasicExampleTest(unittest.TestCase): diff --git a/pyste/tests/enums.h b/pyste/tests/enums.h new file mode 100644 index 00000000..207001e0 --- /dev/null +++ b/pyste/tests/enums.h @@ -0,0 +1,24 @@ +#ifndef ENUMS_H +#define ENUMS_H + +namespace enums { + +enum color { red, blue }; + +struct X +{ + enum choices + { + good = 1, + bad = 2 + }; + + int set(choices c) + { + return (int)c; + } +}; + +} + +#endif diff --git a/pyste/tests/enums.pyste b/pyste/tests/enums.pyste new file mode 100644 index 00000000..a6cc740a --- /dev/null +++ b/pyste/tests/enums.pyste @@ -0,0 +1,8 @@ +color = Enum('enums::color', 'enums.h') +rename(color.red, 'Red') +rename(color.blue, 'Blue') +X = Class('enums::X', 'enums.h') +rename(X.choices.bad, 'Bad') +rename(X.choices.good, 'Good') +rename(X.choices, 'Choices') + diff --git a/pyste/tests/example_enumsUT.py b/pyste/tests/enumsUT.py similarity index 95% rename from pyste/tests/example_enumsUT.py rename to pyste/tests/enumsUT.py index 01e3bdd8..ce29050d 100644 --- a/pyste/tests/example_enumsUT.py +++ b/pyste/tests/enumsUT.py @@ -1,5 +1,5 @@ import unittest -from enums import * +from _enums import * class EnumsTest(unittest.TestCase): diff --git a/pyste/tests/header_test.h b/pyste/tests/header_test.h new file mode 100644 index 00000000..ada44ac0 --- /dev/null +++ b/pyste/tests/header_test.h @@ -0,0 +1,38 @@ +#ifndef HEADER_TEST_H +#define HEADER_TEST_H + +#include +#include + +namespace header_test { + +enum choice { red, blue }; + +inline std::string choice_str(choice c) +{ + std::map choice_map; + choice_map[red] = "red"; + choice_map[blue] = "blue"; + return choice_map[c]; +} + +struct C +{ + choice c; + + std::string get() + { + return choice_str(c); + } +}; + +// test the exclusion of the following + +struct ForwardDeclared; // should be excluded automatically +struct A {}; +void foo(); +enum bar { value }; + +} + +#endif diff --git a/pyste/tests/header_test.pyste b/pyste/tests/header_test.pyste new file mode 100644 index 00000000..3bd55501 --- /dev/null +++ b/pyste/tests/header_test.pyste @@ -0,0 +1,4 @@ +h = AllFromHeader('header_test.h') +exclude(h.A) +exclude(h.foo) +exclude(h.bar) diff --git a/pyste/tests/example_header_testUT.py b/pyste/tests/header_testUT.py similarity index 96% rename from pyste/tests/example_header_testUT.py rename to pyste/tests/header_testUT.py index 549fcb43..4e879429 100644 --- a/pyste/tests/example_header_testUT.py +++ b/pyste/tests/header_testUT.py @@ -1,5 +1,5 @@ import unittest -from header_test import * +from _header_test import * class HeaderTest(unittest.TestCase): diff --git a/pyste/tests/inherit.h b/pyste/tests/inherit.h new file mode 100644 index 00000000..04caddae --- /dev/null +++ b/pyste/tests/inherit.h @@ -0,0 +1,18 @@ +template +class A +{ +public: + void set(T v) { mData = v; } + + T get() const { return mData; } + +private: + T mData; +}; + + +class B : public A +{ +public: + int go() { return get(); } +}; diff --git a/pyste/tests/inherit.pyste b/pyste/tests/inherit.pyste new file mode 100644 index 00000000..2531ba83 --- /dev/null +++ b/pyste/tests/inherit.pyste @@ -0,0 +1,8 @@ +# Doesn't work: +A = Template('A', 'inherit.h') +A_int = A('int') + +Class('B', 'inherit.h') + +# Does work: +#AllFromHeader('inherit.h') diff --git a/pyste/tests/example_inheritUT.py b/pyste/tests/inheritUT.py similarity index 94% rename from pyste/tests/example_inheritUT.py rename to pyste/tests/inheritUT.py index 43d7bb8f..67be4fa4 100644 --- a/pyste/tests/example_inheritUT.py +++ b/pyste/tests/inheritUT.py @@ -1,5 +1,5 @@ import unittest -from inherit import * +from _inherit import * class InheritExampleTest(unittest.TestCase): diff --git a/pyste/tests/nested.cpp b/pyste/tests/nested.cpp new file mode 100644 index 00000000..9a37a226 --- /dev/null +++ b/pyste/tests/nested.cpp @@ -0,0 +1,4 @@ +#include "nested.h" + +int nested::X::staticXValue = 10; +int nested::X::Y::staticYValue = 20; diff --git a/pyste/tests/nested.h b/pyste/tests/nested.h new file mode 100644 index 00000000..e62adab5 --- /dev/null +++ b/pyste/tests/nested.h @@ -0,0 +1,26 @@ +#ifndef NESTED_H +#define NESTED_H + +namespace nested { + +struct X +{ + struct Y + { + int valueY; + static int staticYValue; + struct Z + { + int valueZ; + }; + }; + + static int staticXValue; + int valueX; +}; + +typedef X Root; + +} + +#endif diff --git a/pyste/tests/nested.pyste b/pyste/tests/nested.pyste new file mode 100644 index 00000000..48bb26b5 --- /dev/null +++ b/pyste/tests/nested.pyste @@ -0,0 +1 @@ +Class('nested::Root', 'nested.h') diff --git a/pyste/tests/example_nested.py b/pyste/tests/nestedUT.py similarity index 93% rename from pyste/tests/example_nested.py rename to pyste/tests/nestedUT.py index 7c1685bc..43f3a346 100644 --- a/pyste/tests/example_nested.py +++ b/pyste/tests/nestedUT.py @@ -1,5 +1,5 @@ import unittest -from nested import * +from _nested import * class NestedTest(unittest.TestCase): diff --git a/pyste/tests/nt_all.bat b/pyste/tests/nt_all.bat deleted file mode 100644 index 68c4e197..00000000 --- a/pyste/tests/nt_all.bat +++ /dev/null @@ -1,7 +0,0 @@ -@echo off -call nt_build_all.bat -runtests.py -call nt_clean.bat -call nt_build_all.bat --multiple -runtests.py -call nt_clean.bat diff --git a/pyste/tests/nt_build_all.bat b/pyste/tests/nt_build_all.bat deleted file mode 100644 index 60f9abef..00000000 --- a/pyste/tests/nt_build_all.bat +++ /dev/null @@ -1,16 +0,0 @@ -@echo off - -call nt_build_pyste.bat basic %1 -call nt_build_pyste.bat enums %1 -call nt_build_pyste.bat header_test %1 -call nt_build_pyste.bat nested %1 -call nt_build_pyste.bat operators %1 -call nt_build_pyste.bat smart_ptr %1 -call nt_build_pyste.bat templates %1 -call nt_build_pyste.bat unions %1 -call nt_build_pyste.bat virtual %1 -call nt_build_pyste.bat virtual2 %1 -call nt_build_pyste.bat wrappertest %1 -call nt_build_pyste.bat opaque %1 -call nt_build_pyste.bat inherit %1 -call nt_build_pyste.bat vars %1 diff --git a/pyste/tests/nt_build_pyste.bat b/pyste/tests/nt_build_pyste.bat deleted file mode 100644 index 33023650..00000000 --- a/pyste/tests/nt_build_pyste.bat +++ /dev/null @@ -1,8 +0,0 @@ -@echo off -set BOOST_INCLUDE=D:\Programming\Libraries\boost-cvs\boost -set out=_%1.cpp -if "%2" == "--multiple" set out=_%1 -rem python ../src/pyste.py %2 -I%BOOST_INCLUDE -I../example --module=%1 --out=%out ../example/%1.pyste -pyste %2 -I%BOOST_INCLUDE -I../example --module=%1 --out=%out ../example/%1.pyste - -scons --quiet multiple=%2 %1.dll diff --git a/pyste/tests/nt_clean.bat b/pyste/tests/nt_clean.bat deleted file mode 100644 index b305b02b..00000000 --- a/pyste/tests/nt_clean.bat +++ /dev/null @@ -1,24 +0,0 @@ -@echo off - -rm -Rf _basic -rm -Rf _enums -rm -Rf _header_test -rm -Rf _nested -rm -Rf _operators -rm -Rf _smart_ptr -rm -Rf _templates -rm -Rf _unions -rm -Rf _virtual -rm -Rf _virtual2 -rm -Rf _wrappertest -rm -Rf _opaque -rm -Rf _inherit - -rm -f *.cpp -rm -f *.obj -rm -f *.exp -rm -f *.arg -rm -f *.dll -rm -f *.pyc -rm -f *.lib -rm -f ../example/*.obj diff --git a/pyste/tests/opaque.h b/pyste/tests/opaque.h new file mode 100644 index 00000000..50ebf508 --- /dev/null +++ b/pyste/tests/opaque.h @@ -0,0 +1,47 @@ +#ifndef OPAQUE_H +#define OPAQUE_H + +#include + +namespace opaque { + + +struct C { + C(int v): value(v) {} + int value; +}; + + +inline C* new_C() +{ + return new C(10); +} + +inline int get(C* c) +{ + return c->value; +} + +struct D { + D(double v): value(v) {} + double value; +}; + +struct A +{ + D* new_handle() + { + return new D(3.0); + } + + double get(D* d) + { + return d->value; + } + + int f(int x=0) { return x; } +}; + +} + +#endif diff --git a/pyste/tests/opaque.pyste b/pyste/tests/opaque.pyste new file mode 100644 index 00000000..2e6a3bc5 --- /dev/null +++ b/pyste/tests/opaque.pyste @@ -0,0 +1,5 @@ +foo = Function('opaque::new_C', 'opaque.h') +set_policy(foo, return_value_policy(return_opaque_pointer)) +Function('opaque::get', 'opaque.h' ) +A = Class('opaque::A', 'opaque.h') +set_policy(A.new_handle, return_value_policy(return_opaque_pointer)) diff --git a/pyste/tests/example_opaqueUT.py b/pyste/tests/opaqueUT.py similarity index 93% rename from pyste/tests/example_opaqueUT.py rename to pyste/tests/opaqueUT.py index a16dad52..1c0ae845 100644 --- a/pyste/tests/example_opaqueUT.py +++ b/pyste/tests/opaqueUT.py @@ -1,5 +1,5 @@ import unittest -from opaque import * +from _opaque import * class OpaqueTest(unittest.TestCase): diff --git a/pyste/tests/operators.cpp b/pyste/tests/operators.cpp new file mode 100644 index 00000000..cc8e881d --- /dev/null +++ b/pyste/tests/operators.cpp @@ -0,0 +1,3 @@ +#include "operators.h" + +double operators::C::x = 10; diff --git a/pyste/tests/operators.h b/pyste/tests/operators.h new file mode 100644 index 00000000..a9d3759e --- /dev/null +++ b/pyste/tests/operators.h @@ -0,0 +1,49 @@ +#ifndef OPERATORS_H +#define OPERATORS_H + + +#include + +namespace operators { + +struct C +{ + static double x; + double value; + + const C operator+(const C other) const + { + C c; + c.value = value + other.value; + return c; + } + operator int() const + { + return (int)value; + } + + double operator()() + { + return C::x; + } + + double operator()(double other) + { + return C::x + other; + } + + operator const char*() { return "C"; } +}; + +inline const C operator*(const C& lhs, const C& rhs) +{ + C c; + c.value = lhs.value * rhs.value; + return c; +} + + +} + + +#endif diff --git a/pyste/tests/operators.pyste b/pyste/tests/operators.pyste new file mode 100644 index 00000000..4ab7a370 --- /dev/null +++ b/pyste/tests/operators.pyste @@ -0,0 +1,2 @@ +C = Class('operators::C', 'operators.h') +#exclude(C.operator['+']) diff --git a/pyste/tests/example_operatorsUT.py b/pyste/tests/operatorsUT.py similarity index 96% rename from pyste/tests/example_operatorsUT.py rename to pyste/tests/operatorsUT.py index 06585d05..b0bdfa13 100644 --- a/pyste/tests/example_operatorsUT.py +++ b/pyste/tests/operatorsUT.py @@ -1,5 +1,5 @@ import unittest -from operators import * +from _operators import * class OperatorTest(unittest.TestCase): diff --git a/pyste/tests/smart_ptr.h b/pyste/tests/smart_ptr.h new file mode 100644 index 00000000..05b9149c --- /dev/null +++ b/pyste/tests/smart_ptr.h @@ -0,0 +1,44 @@ +#ifndef SMART_PTR_H +#define SMART_PTR_H + + +#include +#include + +namespace smart_ptr { + +struct C +{ + int value; +}; + +inline boost::shared_ptr NewC() { return boost::shared_ptr( new C() ); } + +struct D +{ + boost::shared_ptr Get() { return ptr; } + void Set( boost::shared_ptr c ) { ptr = c; } +private: + boost::shared_ptr ptr; +}; + +inline std::auto_ptr NewD() { return std::auto_ptr( new D() ); } + + +// test an abstract class +struct A +{ + virtual int f() = 0; +}; + +struct B: A +{ + virtual int f(){ return 1; } +}; + +inline boost::shared_ptr NewA() { return boost::shared_ptr(new B()); } +inline int GetA(boost::shared_ptr a) { return a->f(); } + +} + +#endif diff --git a/pyste/tests/smart_ptr.pyste b/pyste/tests/smart_ptr.pyste new file mode 100644 index 00000000..cfbdd81a --- /dev/null +++ b/pyste/tests/smart_ptr.pyste @@ -0,0 +1,13 @@ +C = Class('smart_ptr::C', 'smart_ptr.h') +use_shared_ptr(C) + +D = Class('smart_ptr::D', 'smart_ptr.h') +use_auto_ptr(D) + +A = Class('smart_ptr::A', 'smart_ptr.h') +use_shared_ptr(A) + +Function('smart_ptr::NewC', 'smart_ptr.h') +Function('smart_ptr::NewD', 'smart_ptr.h') +Function('smart_ptr::NewA', 'smart_ptr.h') +Function('smart_ptr::GetA', 'smart_ptr.h') diff --git a/pyste/tests/example_smart_ptrUT.py b/pyste/tests/smart_ptrUT.py similarity index 93% rename from pyste/tests/example_smart_ptrUT.py rename to pyste/tests/smart_ptrUT.py index 04fc9caa..477c919a 100644 --- a/pyste/tests/example_smart_ptrUT.py +++ b/pyste/tests/smart_ptrUT.py @@ -1,5 +1,5 @@ import unittest -from smart_ptr import * +from _smart_ptr import * class BasicExampleTest(unittest.TestCase): diff --git a/pyste/tests/templates.h b/pyste/tests/templates.h new file mode 100644 index 00000000..c34f4a81 --- /dev/null +++ b/pyste/tests/templates.h @@ -0,0 +1,10 @@ +namespace templates { + +template +struct Point +{ + T x; + T y; +}; + +} diff --git a/pyste/tests/templates.pyste b/pyste/tests/templates.pyste new file mode 100644 index 00000000..77eaceaa --- /dev/null +++ b/pyste/tests/templates.pyste @@ -0,0 +1,8 @@ +Point = Template('templates::Point', 'templates.h') +rename(Point.x, 'i') +rename(Point.y, 'j') +IPoint = Point('int') +FPoint = Point('double', 'FPoint') +rename(IPoint, 'IPoint') +rename(IPoint.x, 'x') +rename(IPoint.y, 'y') diff --git a/pyste/tests/example_templatesUT.py b/pyste/tests/templatesUT.py similarity index 95% rename from pyste/tests/example_templatesUT.py rename to pyste/tests/templatesUT.py index 5afbd6e8..af908369 100644 --- a/pyste/tests/example_templatesUT.py +++ b/pyste/tests/templatesUT.py @@ -1,5 +1,5 @@ import unittest -from templates import * +from _templates import * class TemplatesTest(unittest.TestCase): diff --git a/pyste/tests/test_all.py b/pyste/tests/test_all.py new file mode 100644 index 00000000..23ab4ff0 --- /dev/null +++ b/pyste/tests/test_all.py @@ -0,0 +1,179 @@ +#!/usr/bin/python + +import os +import glob +import shutil +import sys + +# 3 functions are needed for each plataform: +# build_pyste(multiple, module) +# compile_single(module) +# compile_multiple(module) +# +if sys.platform == 'win32': + + includes = '-ID:/programming/libraries/boost-cvs/boost -IC:/Python/include' + lib_dirs = '/libpath:D:/programming/libraries/boost-cvs/lib /libpath:C:/Python/libs' + libs = 'boost_python.lib python22.lib' + + def build_pyste(multiple, module): + cmd = 'python ../src/pyste.py %s %s --module=%s %s.pyste' + execute(cmd % (multiple, includes, '_' + module, module)) + + + def compile_single(module): + start_building(module) + cmd = 'icl /nologo /GR /GX -c %s -I.' % includes + cmd += ' %s' + module_obj = '' + if os.path.isfile(module+'.cpp'): + execute(cmd % (module+'.cpp')) + module_obj = module + '.obj' + execute(cmd % '_%s.cpp' % module) + execute('link /nologo /DLL /out:_%s.dll %s %s %s %s' % \ + (module, lib_dirs, '_%s.obj' % module, module_obj, libs)) + end_building(module) + + + def compile_multiple(module): + start_building(module) + cmd = 'icl /nologo /GR /GX -c %s -I.' % includes + cmd += ' %s' + module_obj = '' + if os.path.isfile(module+'.cpp'): + execute(cmd % (module+'.cpp')) + module_obj = module + '.obj' + files = glob.glob('_%s/*.cpp' % module) + for f in files: + execute(cmd % f) + objs = [os.path.split(os.path.splitext(x)[0])[1] + '.obj' for x in files] + objs.append(module_obj) + execute('link /nologo /DLL /out:_%s.dll %s %s %s' % \ + (module, lib_dirs, ' '.join(objs), libs)) + end_building(module) + + + def start_building(module): + #print 'Building module %s...' % module, + pass + + + def end_building(module): + pass + #if os.path.isfile('_%s.dll' % module): + # print ' done.' + #else: + # print 'FAILED!' + #print + + +elif sys.platform == 'posix': + + def build_pyste(multiple, module): + cmd = 'python ../src/pyste.py %s --module=%s %s.pyste' + execute(cmd % (multiple, module)) + + +def execute(cmd): + #output = os.popen(cmd).read() + #f = file('build.log', 'a') + #f.write(output) + #f.close() + os.system(cmd) + + + + +def compile_pyste_files(multiple): + pass + #if not multiple: + # ## compile each cpp into a shared library + # #for cpp in glob.glob('*.cpp'): + # # print + # # print 'compiling', cpp + # # out = os.path.splitext(cpp)[0] + '.so' + # # cmdline = 'g++ -shared -o %s -I../example ' \ + # # '-I/usr/include/python2.2 -lboost_python %s' % (out, cpp) + # # os.system(cmdline) + # + #else: + # modules = get_modules() + # # list cpp files in each module directory + # print + # for module in modules: + # # compile each + # for file in glob.glob(module+'/*.cpp'): + # print 'compiling', file + # out = os.path.splitext(file)[0] + '.obj' + # cmdline = 'g++ -shared -c -o %s -I../example ' \ + # '-I/usr/include/python2.2 %s' % (out, file) + # os.system(cmdline) + # # generate a dynamic library + # print 'linking' + # objs = ' '.join([x for x in glob.glob(module+'/*.obj')]) + # out = module + '.so' + # cmdline = 'g++ -shared -o %s -lboost_python %s' % (out, objs) + # os.system(cmdline) + + +def compile_file_posix(filename, outfilename): + cmdline = 'g++ -shared -o %s -I../example ' \ + '-I/usr/include/python2.2 -lboost_python %s' % (outfilename, filename) + execute(cmdline) + + +def run_tests(): + if os.system('python runtests.py') != 0: + raise RuntimeError, 'tests failed' + + +def cleanup(): + modules = get_modules() + extensions = '*.dll *.pyc *.obj *.exp *.lib' + files = [] + for module in modules: + files.append('_' + module + '.cpp') + for ext in extensions.split(): + files += glob.glob(ext) + files.append('build.log') + for file in files: + try: + os.remove(file) + except OSError: pass + + for module in modules: + try: + shutil.rmtree('_' + module) + except OSError: pass + + +def main(multiple, module=None): + if module is None: + modules = get_modules() + else: + modules = [module] + for module in modules: + build_pyste(multiple, module) + if multiple: + compile_multiple(module) + else: + compile_single(module) + run_tests() + cleanup() + + +def get_modules(): + def getname(file): + return os.path.splitext(os.path.basename(file))[0] + return [getname(x) for x in glob.glob('*.pyste')] + +if __name__ == '__main__': + if len(sys.argv) > 1: + module = sys.argv[1] + else: + module = None + try: + main('--multiple', module) + main('', module) + except RuntimeError, e: + print e diff --git a/pyste/tests/test_all_linux.py b/pyste/tests/test_all_linux.py deleted file mode 100644 index 7c536926..00000000 --- a/pyste/tests/test_all_linux.py +++ /dev/null @@ -1,88 +0,0 @@ -#!/usr/bin/python - -import os -import glob -import shutil - -def build_pyste_files(multiple): - # list all pyste files in the example directory - examples = glob.glob('../example/*.pyste') - # generate the cpp file for each example - for example in examples: - path, filename = os.path.split(example) - module = os.path.splitext(filename)[0] - os.system('python ../src/pyste.py %s -I%s --module=%s %s' % \ - (multiple, path, module, example)) - - -def compile_pyste_files(multiple): - if not multiple: - # compile each cpp into a shared library - for cpp in glob.glob('*.cpp'): - print - print 'compiling', cpp - out = os.path.splitext(cpp)[0] + '.so' - cmdline = 'g++ -shared -o %s -I../example ' \ - '-I/usr/include/python2.2 -lboost_python %s' % (out, cpp) - os.system(cmdline) - else: - modules = get_modules() - # list cpp files in each module directory - print - for module in modules: - # compile each - for file in glob.glob(module+'/*.cpp'): - print 'compiling', file - out = os.path.splitext(file)[0] + '.obj' - cmdline = 'g++ -shared -c -o %s -I../example ' \ - '-I/usr/include/python2.2 %s' % (out, file) - os.system(cmdline) - # generate a dynamic library - print 'linking' - objs = ' '.join([x for x in glob.glob(module+'/*.obj')]) - out = module + '.so' - cmdline = 'g++ -shared -o %s -lboost_python %s' % (out, objs) - os.system(cmdline) - - - - -def run_tests(): - if os.system('python runtests.py') != 0: - raise RuntimeError, 'tests failed' - - -def cleanup(): - extensions = '*.cpp *.so *.pyc' - files = [] - for ext in extensions.split(): - files += glob.glob(ext) - for file in files: - try: - os.remove(file) - except OSError: pass - - modules = get_modules() - for module in modules: - try: - shutil.rmtree(module) - except OSError: pass - - -def main(multiple): - build_pyste_files(multiple) - compile_pyste_files(multiple) - run_tests() - cleanup() - -def get_modules(): - def getname(file): - return os.path.splitext(os.path.basename(file))[0] - return [getname(x) for x in glob.glob('../example/*.pyste')] - -if __name__ == '__main__': - try: - main('--multiple') - main('') - except RuntimeError, e: - print e diff --git a/pyste/tests/unions.h b/pyste/tests/unions.h new file mode 100644 index 00000000..06287dec --- /dev/null +++ b/pyste/tests/unions.h @@ -0,0 +1,16 @@ +namespace unions { + +class UnionTest +{ +public: + union // unions are not supported for now + { + int i; + short s1; + short s2; + } mBad; + + int mGood; +}; + +} diff --git a/pyste/tests/unions.pyste b/pyste/tests/unions.pyste new file mode 100644 index 00000000..726ab6a2 --- /dev/null +++ b/pyste/tests/unions.pyste @@ -0,0 +1,2 @@ +UnionTest = Class('unions::UnionTest', 'unions.h') +exclude(UnionTest.mBad) diff --git a/pyste/tests/vars.h b/pyste/tests/vars.h new file mode 100644 index 00000000..23de92b5 --- /dev/null +++ b/pyste/tests/vars.h @@ -0,0 +1,19 @@ + +struct Color +{ + Color(int r_ = 0, int g_ = 0, int b_ = 0): + r(r_), g(g_), b(b_) + {} + Color( const Color &c): + r(c.r), g(c.g), b(c.b) + {} + int r; + int g; + int b; +}; + +const Color black = Color(0, 0, 0); +const Color red = Color(255, 0, 0); +const Color green = Color(0, 255, 0); +const Color blue = Color(0, 0, 255); +Color in_use = black; diff --git a/pyste/tests/vars.pyste b/pyste/tests/vars.pyste new file mode 100644 index 00000000..3fd9d689 --- /dev/null +++ b/pyste/tests/vars.pyste @@ -0,0 +1 @@ +AllFromHeader('vars.h') diff --git a/pyste/tests/example_varsUT.py b/pyste/tests/varsUT.py similarity index 59% rename from pyste/tests/example_varsUT.py rename to pyste/tests/varsUT.py index 226b0118..86fba68b 100644 --- a/pyste/tests/example_varsUT.py +++ b/pyste/tests/varsUT.py @@ -1,5 +1,5 @@ import unittest -import vars +import _vars class VarsTest(unittest.TestCase): @@ -9,9 +9,9 @@ class VarsTest(unittest.TestCase): self.assertEqual(c.r, r) self.assertEqual(c.g, g) self.assertEqual(c.b, b) - testColor(vars.black, 0, 0, 0) - testColor(vars.red, 255, 0, 0) - testColor(vars.green, 0, 255, 0) - testColor(vars.blue, 0, 0, 255) + testColor(_vars.black, 0, 0, 0) + testColor(_vars.red, 255, 0, 0) + testColor(_vars.green, 0, 255, 0) + testColor(_vars.blue, 0, 0, 255) diff --git a/pyste/tests/virtual.cpp b/pyste/tests/virtual.cpp new file mode 100644 index 00000000..a1baa6db --- /dev/null +++ b/pyste/tests/virtual.cpp @@ -0,0 +1,70 @@ + +// Includes ==================================================================== +#include +#include + +// Using ======================================================================= +using namespace boost::python; + +// Declarations ================================================================ + + +namespace { + + +struct virtual_C_Wrapper: virtual_::C +{ + virtual_C_Wrapper(PyObject* self_, const virtual_::C & p0): + virtual_::C(p0), self(self_) {} + + virtual_C_Wrapper(PyObject* self_): + virtual_::C(), self(self_) {} + + int f() { + return call_method< int >(self, "f"); + } + + int default_f() { + return virtual_::C::f(); + } + + void bar(int p0) { + call_method< void >(self, "bar", p0); + } + + void default_bar(int p0) { + virtual_::C::bar(p0); + } + + void bar(char * p0) { + call_method< void >(self, "bar", p0); + } + + void default_bar(char * p0) { + virtual_::C::bar(p0); + } + + int f_abs() { + return call_method< int >(self, "f_abs"); + } + + PyObject* self; +}; + + + +}// namespace + + +// Module ====================================================================== +BOOST_PYTHON_MODULE(virtual) +{ + class_< virtual_::C, boost::noncopyable, virtual_C_Wrapper >("C", init< >()) + .def("get_name", &virtual_::C::get_name) + .def("f", &virtual_::C::f, &virtual_C_Wrapper::default_f) + .def("bar", (void (virtual_::C::*)(int) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(int))&virtual_C_Wrapper::default_bar) + .def("bar", (void (virtual_::C::*)(char *) )&virtual_::C::bar, (void (virtual_C_Wrapper::*)(char *))&virtual_C_Wrapper::default_bar) + ; + + def("call_f", &virtual_::call_f); +} diff --git a/pyste/tests/virtual.h b/pyste/tests/virtual.h new file mode 100644 index 00000000..cd194315 --- /dev/null +++ b/pyste/tests/virtual.h @@ -0,0 +1,28 @@ +namespace virtual_ { + +struct C +{ +public: + virtual int f() + { + return f_abs(); + } + + virtual void bar(int) {} + virtual void bar(char*) {} + + const char* get_name() + { + return name(); + } + +protected: + virtual int f_abs() = 0; + +private: + virtual const char* name() { return "C"; } +}; + +inline int call_f(C& c) { return c.f(); } + +} diff --git a/pyste/tests/virtual.pyste b/pyste/tests/virtual.pyste new file mode 100644 index 00000000..3fa072f8 --- /dev/null +++ b/pyste/tests/virtual.pyste @@ -0,0 +1,2 @@ +Class('virtual_::C', 'virtual.h') +Function('virtual_::call_f', 'virtual.h') diff --git a/pyste/tests/virtual2.h b/pyste/tests/virtual2.h new file mode 100644 index 00000000..0f5aab32 --- /dev/null +++ b/pyste/tests/virtual2.h @@ -0,0 +1,27 @@ + +namespace virtual2 { + +struct A +{ + virtual int f() { return 0; } + virtual int f1() { return 10; } +}; + +struct B: A +{ + virtual int f() { return 1; } + virtual int f2() { return 20; } +}; + +inline int call_fs(A*a) +{ + int r = a->f1(); + B* b = dynamic_cast(a); + return r + b->f2(); +} + +inline int call_f(A* a) +{ + return a->f(); +} +} diff --git a/pyste/tests/virtual2.pyste b/pyste/tests/virtual2.pyste new file mode 100644 index 00000000..2ca567e6 --- /dev/null +++ b/pyste/tests/virtual2.pyste @@ -0,0 +1,4 @@ +Class('virtual2::A', 'virtual2.h') +Class('virtual2::B', 'virtual2.h') +Function('virtual2::call_fs', 'virtual2.h') +Function('virtual2::call_f', 'virtual2.h') diff --git a/pyste/tests/example_virtual2.py b/pyste/tests/virtual2UT.py similarity index 96% rename from pyste/tests/example_virtual2.py rename to pyste/tests/virtual2UT.py index 6bf6f47f..25dd81fe 100644 --- a/pyste/tests/example_virtual2.py +++ b/pyste/tests/virtual2UT.py @@ -1,5 +1,5 @@ import unittest -from virtual2 import * +from _virtual2 import * class Virtual2Test(unittest.TestCase): diff --git a/pyste/tests/example_virtual.py b/pyste/tests/virtualUT.py similarity index 97% rename from pyste/tests/example_virtual.py rename to pyste/tests/virtualUT.py index 2aba0f9b..a3f39661 100644 --- a/pyste/tests/example_virtual.py +++ b/pyste/tests/virtualUT.py @@ -1,5 +1,5 @@ import unittest -from virtual import * +from _virtual import * class VirtualTest(unittest.TestCase): diff --git a/pyste/tests/wrappertest.h b/pyste/tests/wrappertest.h new file mode 100644 index 00000000..4cce28ee --- /dev/null +++ b/pyste/tests/wrappertest.h @@ -0,0 +1,46 @@ +#ifndef WRAPPER_TEST +#define WRAPPER_TEST + + +#include + +namespace wrappertest { + +inline std::vector Range(int count) +{ + std::vector v; + v.reserve(count); + for (int i = 0; i < count; ++i){ + v.push_back(i); + } + return v; +} + + +struct C +{ + C() {} + + std::vector Mul(int value) + { + std::vector res; + res.reserve(value); + std::vector::const_iterator it; + std::vector v(Range(value)); + for (it = v.begin(); it != v.end(); ++it){ + res.push_back(*it * value); + } + return res; + } +}; + + +struct A +{ + virtual int f() { return 1; }; +}; + +inline int call_foo(A* a){ return a->f(); } +} +#endif + diff --git a/pyste/tests/wrappertest.pyste b/pyste/tests/wrappertest.pyste new file mode 100644 index 00000000..12ba47b6 --- /dev/null +++ b/pyste/tests/wrappertest.pyste @@ -0,0 +1,21 @@ +Include('wrappertest_wrappers.h') + +f = Function('wrappertest::Range', 'wrappertest.h') +set_wrapper(f, 'RangeWrapper') + +mul = Wrapper('MulWrapper', +''' +list MulWrapper(wrappertest::C& c, int value){ + return VectorToList(c.Mul(value)); +} +''' +) + +C = Class('wrappertest::C', 'wrappertest.h') +set_wrapper(C.Mul, mul) + + +A = Class('wrappertest::A', 'wrappertest.h') +set_wrapper(A.f, 'f_wrapper') + +Function('wrappertest::call_foo', 'wrappertest.h') diff --git a/pyste/tests/example_wrappertestUT.py b/pyste/tests/wrappertestUT.py similarity index 94% rename from pyste/tests/example_wrappertestUT.py rename to pyste/tests/wrappertestUT.py index fa3c4236..5831dd76 100644 --- a/pyste/tests/example_wrappertestUT.py +++ b/pyste/tests/wrappertestUT.py @@ -1,5 +1,5 @@ import unittest -from wrappertest import * +from _wrappertest import * class WrapperTest(unittest.TestCase): diff --git a/pyste/tests/wrappertest_wrappers.h b/pyste/tests/wrappertest_wrappers.h new file mode 100644 index 00000000..a0b5d946 --- /dev/null +++ b/pyste/tests/wrappertest_wrappers.h @@ -0,0 +1,28 @@ +#ifndef WRAPPER_TEST_WRAPPERS +#define WRAPPER_TEST_WRAPPERS + +#include +#include +#include "wrappertest.h" + +using namespace boost::python; + +template +list VectorToList(const std::vector & v) +{ + list res; + typename std::vector::const_iterator it; + for(it = v.begin(); it != v.end(); ++it){ + res.append(*it); + } + Py_XINCREF(res.ptr()); + return res; +} + +inline list RangeWrapper(int count){ + return VectorToList(wrappertest::Range(count)); +} + +inline int f_wrapper(wrappertest::A*) { return 10; } + +#endif