new test directory for test modules that wrap C/C++ source files
authorStefan Behnel <scoder@users.berlios.de>
Tue, 9 Feb 2010 09:06:03 +0000 (10:06 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Tue, 9 Feb 2010 09:06:03 +0000 (10:06 +0100)
naming convention in test runner to search for related source files in the test directory
tests for wrapping C++ code

runtests.py
tests/wrappers/cpp_overload_wrapper.pyx [new file with mode: 0644]
tests/wrappers/cpp_overload_wrapper_lib.cpp [new file with mode: 0644]
tests/wrappers/cpp_overload_wrapper_lib.h [new file with mode: 0644]
tests/wrappers/cpp_overload_wrapper_lib.pxd [new file with mode: 0644]
tests/wrappers/cppwrap.pyx [new file with mode: 0644]
tests/wrappers/cppwrap_lib.cpp [new file with mode: 0644]
tests/wrappers/cppwrap_lib.h [new file with mode: 0644]
tests/wrappers/cppwrap_lib.pxd [new file with mode: 0644]

index c22fc5010cbe83b803dab1b7903df7510da67ee8..1b2ef2e328861b272a88cca7c30de96233a547e7 100644 (file)
@@ -28,8 +28,8 @@ from distutils.core import Extension
 from distutils.command.build_ext import build_ext as _build_ext
 distutils_distro = Distribution()
 
-TEST_DIRS = ['compile', 'errors', 'run', 'pyregr']
-TEST_RUN_DIRS = ['run', 'pyregr']
+TEST_DIRS = ['compile', 'errors', 'run', 'wrappers', 'pyregr']
+TEST_RUN_DIRS = ['run', 'wrappers', 'pyregr']
 
 # Lists external modules, and a matcher matching tests
 # which should be excluded if the module is not present.
@@ -200,10 +200,10 @@ class TestBuilder(object):
                           fork=self.fork)
 
 class CythonCompileTestCase(unittest.TestCase):
-    def __init__(self, directory, workdir, module, language='c',
+    def __init__(self, test_directory, workdir, module, language='c',
                  expect_errors=False, annotate=False, cleanup_workdir=True,
                  cleanup_sharedlibs=True, cython_only=False, fork=True):
-        self.directory = directory
+        self.test_directory = test_directory
         self.workdir = workdir
         self.module = module
         self.language = language
@@ -257,8 +257,8 @@ class CythonCompileTestCase(unittest.TestCase):
         self.runCompileTest()
 
     def runCompileTest(self):
-        self.compile(self.directory, self.module, self.workdir,
-                     self.directory, self.expect_errors, self.annotate)
+        self.compile(self.test_directory, self.module, self.workdir,
+                     self.test_directory, self.expect_errors, self.annotate)
 
     def find_module_source_file(self, source_file):
         if not os.path.exists(source_file):
@@ -269,8 +269,15 @@ class CythonCompileTestCase(unittest.TestCase):
         target = '%s.%s' % (module_name, self.language)
         return target
 
-    def split_source_and_output(self, directory, module, workdir):
-        source_file = os.path.join(directory, module) + '.pyx'
+    def find_source_files(self, test_directory, module_name):
+        is_related = re.compile('%s_.*[.]%s' % (module_name, self.language)).match
+        return [self.build_target_filename(module_name)] + [
+            os.path.join(test_directory, filename)
+            for filename in os.listdir(test_directory)
+            if is_related(filename) and os.path.isfile(os.path.join(test_directory, filename)) ]
+
+    def split_source_and_output(self, test_directory, module, workdir):
+        source_file = os.path.join(test_directory, module) + '.pyx'
         source_and_output = codecs.open(
             self.find_module_source_file(source_file), 'rU', 'ISO-8859-1')
         out = codecs.open(os.path.join(workdir, module + '.pyx'),
@@ -289,12 +296,12 @@ class CythonCompileTestCase(unittest.TestCase):
         else:
             return geterrors()
 
-    def run_cython(self, directory, module, targetdir, incdir, annotate):
+    def run_cython(self, test_directory, module, targetdir, incdir, annotate):
         include_dirs = INCLUDE_DIRS[:]
         if incdir:
             include_dirs.append(incdir)
         source = self.find_module_source_file(
-            os.path.join(directory, module + '.pyx'))
+            os.path.join(test_directory, module + '.pyx'))
         target = os.path.join(targetdir, self.build_target_filename(module))
         options = CompilationOptions(
             pyrex_default_options,
@@ -309,7 +316,7 @@ class CythonCompileTestCase(unittest.TestCase):
         cython_compile(source, options=options,
                        full_module_name=module)
 
-    def run_distutils(self, module, workdir, incdir):
+    def run_distutils(self, test_directory, module, workdir, incdir):
         cwd = os.getcwd()
         os.chdir(workdir)
         try:
@@ -324,7 +331,7 @@ class CythonCompileTestCase(unittest.TestCase):
                     ext_include_dirs += get_additional_include_dirs()
             extension = Extension(
                 module,
-                sources = [self.build_target_filename(module)],
+                sources = self.find_source_files(test_directory, module),
                 include_dirs = ext_include_dirs,
                 extra_compile_args = CFLAGS,
                 )
@@ -337,19 +344,19 @@ class CythonCompileTestCase(unittest.TestCase):
         finally:
             os.chdir(cwd)
 
-    def compile(self, directory, module, workdir, incdir,
+    def compile(self, test_directory, module, workdir, incdir,
                 expect_errors, annotate):
         expected_errors = errors = ()
         if expect_errors:
             expected_errors = self.split_source_and_output(
-                directory, module, workdir)
-            directory = workdir
+                test_directory, module, workdir)
+            test_directory = workdir
 
         if WITH_CYTHON:
             old_stderr = sys.stderr
             try:
                 sys.stderr = ErrorWriter()
-                self.run_cython(directory, module, workdir, incdir, annotate)
+                self.run_cython(test_directory, module, workdir, incdir, annotate)
                 errors = sys.stderr.geterrors()
             finally:
                 sys.stderr = old_stderr
@@ -373,7 +380,7 @@ class CythonCompileTestCase(unittest.TestCase):
                 raise
         else:
             if not self.cython_only:
-                self.run_distutils(module, workdir, incdir)
+                self.run_distutils(test_directory, module, workdir, incdir)
 
 class CythonRunTestCase(CythonCompileTestCase):
     def shortDescription(self):
diff --git a/tests/wrappers/cpp_overload_wrapper.pyx b/tests/wrappers/cpp_overload_wrapper.pyx
new file mode 100644 (file)
index 0000000..ccf5f02
--- /dev/null
@@ -0,0 +1,36 @@
+
+cimport cppwrap_lib
+
+cdef class DoubleKeeper:
+    cdef cppwrap_lib.DoubleKeeper* keeper
+
+    def __cinit__(self, factor=None):
+        if factor is None:
+            self.keeper = new cppwrap_lib.DoubleKeeper()
+        else:
+            self.keeper = new cppwrap_lib.DoubleKeeper(<double>factor)
+
+    def __dealloc__(self):
+        del self.keeper
+
+    def set_number(self, factor=None):
+        if factor is None:
+            self.keeper.set_number()
+        else:
+            self.keeper.set_number(<double>factor)
+
+    def get_number(self):
+        return self.keeper.get_number()
+
+    def transmogrify(self, double value):
+        return self.keeper.transmogrify(value)
+
+
+def voidfunc():
+    cppwrap_lib.voidfunc()
+
+def doublefunc(double x, double y, double z):
+    return cppwrap_lib.doublefunc(x, y, z)
+
+def transmogrify_from_cpp(DoubleKeeper obj not None, double value):
+    return cppwrap_lib.transmogrify_from_cpp(obj.keeper, value)
diff --git a/tests/wrappers/cpp_overload_wrapper_lib.cpp b/tests/wrappers/cpp_overload_wrapper_lib.cpp
new file mode 100644 (file)
index 0000000..3668120
--- /dev/null
@@ -0,0 +1,55 @@
+
+#include "cppwrap_lib.h"
+
+void voidfunc (void)
+{
+}
+
+double doublefunc (double a, double b, double c)
+{
+    return a + b + c;
+}
+
+
+DoubleKeeper::DoubleKeeper ()
+    : number (1.0) 
+{
+}
+
+DoubleKeeper::DoubleKeeper (double factor)
+    : number (factor)
+{
+}
+
+DoubleKeeper::~DoubleKeeper ()
+{
+}
+
+double DoubleKeeper::get_number () const
+{
+    return number;
+}
+
+void DoubleKeeper::set_number (double f)
+{
+    number = f;
+}
+
+void DoubleKeeper::set_number ()
+{
+    number = 1.0;
+}
+
+double
+DoubleKeeper::transmogrify (double value) const
+{
+    return value*number;
+}
+
+
+double
+transmogrify_from_cpp (DoubleKeeper const *obj, double value)
+{
+    return obj->transmogrify (value);
+}
+
diff --git a/tests/wrappers/cpp_overload_wrapper_lib.h b/tests/wrappers/cpp_overload_wrapper_lib.h
new file mode 100644 (file)
index 0000000..ca34afd
--- /dev/null
@@ -0,0 +1,22 @@
+
+void voidfunc(void);
+
+double doublefunc (double a, double b, double c);
+
+
+class DoubleKeeper
+{
+    double number;
+    
+public:
+    DoubleKeeper ();
+    DoubleKeeper (double number);
+    virtual ~DoubleKeeper ();
+    
+    void set_number (double num);
+    void set_number (void);
+    double get_number () const;
+    virtual double transmogrify (double value) const;
+};
+
+double transmogrify_from_cpp (DoubleKeeper const *obj, double value);
diff --git a/tests/wrappers/cpp_overload_wrapper_lib.pxd b/tests/wrappers/cpp_overload_wrapper_lib.pxd
new file mode 100644 (file)
index 0000000..d92ca89
--- /dev/null
@@ -0,0 +1,14 @@
+
+cdef extern from "testapi.h":
+    void voidfunc()
+    double doublefunc(double a, double b, double c)
+
+    cdef cppclass DoubleKeeper:
+        DoubleKeeper()
+        DoubleKeeper(double factor)
+        void set_number()
+        void set_number(double f)
+        double get_number()
+        double transmogrify(double value)
+
+    double transmogrify_from_cpp (DoubleKeeper *obj, double value)
diff --git a/tests/wrappers/cppwrap.pyx b/tests/wrappers/cppwrap.pyx
new file mode 100644 (file)
index 0000000..1dde30a
--- /dev/null
@@ -0,0 +1,30 @@
+
+cimport cppwrap_lib
+
+cdef class DoubleKeeper:
+    cdef cppwrap_lib.DoubleKeeper* keeper
+
+    def __cinit__(self, double number):
+        self.keeper = new cppwrap_lib.DoubleKeeper(number)
+
+    def __dealloc__(self):
+        del self.keeper
+
+    def set_number(self, double number):
+        self.keeper.set_number(number)
+
+    def get_number(self):
+        return self.keeper.get_number()
+
+    def transmogrify(self, double value):
+        return self.keeper.transmogrify(value)
+
+
+def voidfunc():
+    cppwrap_lib.voidfunc()
+
+def doublefunc(double x, double y, double z):
+    return cppwrap_lib.doublefunc(x, y, z)
+
+def transmogrify_from_cpp(DoubleKeeper obj not None, double value):
+    return cppwrap_lib.transmogrify_from_cpp(obj.keeper, value)
diff --git a/tests/wrappers/cppwrap_lib.cpp b/tests/wrappers/cppwrap_lib.cpp
new file mode 100644 (file)
index 0000000..3668120
--- /dev/null
@@ -0,0 +1,55 @@
+
+#include "cppwrap_lib.h"
+
+void voidfunc (void)
+{
+}
+
+double doublefunc (double a, double b, double c)
+{
+    return a + b + c;
+}
+
+
+DoubleKeeper::DoubleKeeper ()
+    : number (1.0) 
+{
+}
+
+DoubleKeeper::DoubleKeeper (double factor)
+    : number (factor)
+{
+}
+
+DoubleKeeper::~DoubleKeeper ()
+{
+}
+
+double DoubleKeeper::get_number () const
+{
+    return number;
+}
+
+void DoubleKeeper::set_number (double f)
+{
+    number = f;
+}
+
+void DoubleKeeper::set_number ()
+{
+    number = 1.0;
+}
+
+double
+DoubleKeeper::transmogrify (double value) const
+{
+    return value*number;
+}
+
+
+double
+transmogrify_from_cpp (DoubleKeeper const *obj, double value)
+{
+    return obj->transmogrify (value);
+}
+
diff --git a/tests/wrappers/cppwrap_lib.h b/tests/wrappers/cppwrap_lib.h
new file mode 100644 (file)
index 0000000..94145d6
--- /dev/null
@@ -0,0 +1,20 @@
+
+void voidfunc(void);
+
+double doublefunc (double a, double b, double c);
+
+
+class DoubleKeeper
+{
+    double number;
+    
+public:
+    DoubleKeeper (double number);
+    virtual ~DoubleKeeper ();
+    
+    void set_number (double num);
+    double get_number () const;
+    virtual double transmogrify (double value) const;
+};
+
+double transmogrify_from_cpp (DoubleKeeper const *obj, double value);
diff --git a/tests/wrappers/cppwrap_lib.pxd b/tests/wrappers/cppwrap_lib.pxd
new file mode 100644 (file)
index 0000000..405c808
--- /dev/null
@@ -0,0 +1,12 @@
+
+cdef extern from "testapi.h":
+    void voidfunc()
+    double doublefunc(double a, double b, double c)
+
+    cdef cppclass DoubleKeeper:
+        DoubleKeeper(double factor)
+        void set_number(double f)
+        double get_number()
+        double transmogrify(double value)
+
+    double transmogrify_from_cpp (DoubleKeeper *obj, double value)