Commits

Antonio Cuni committed 3038043

test both numpy and fakenumpy at the same time

  • Participants
  • Parent commits 5edff58

Comments (0)

Files changed (3)

 #include <stdlib.h>
 #include <assert.h>
 
-#ifdef PYPY_VERSION
-// if we are testing on pypy, use the fake C API
-#include "fakenumpy.h"
+#ifdef USE_NUMPY
+     // test using the actual numpy implementation
+#    define INIT initfakenumpy_test_direct
+#    define MODNAME "fakenumpy_test_direct"
+#    include <numpy/arrayobject.h>
 #else
-// else, use the real numpy
-#include <numpy/arrayobject.h>
+#    define INIT initfakenumpy_test
+#    define MODNAME "fakenumpy_test"
+#    include "fakenumpy.h"
 #endif
 
 #define py_assert(e) {                                                  \
     npy_intp* dims2 = PyArray_DIMS(array);
     py_assert(dims2[0] == 2);
     py_assert(dims2[1] == 2);
-
+    Py_XDECREF(array);
     Py_RETURN_NONE;
 }
 
     PyArrayObject* array = (PyArrayObject*)PyArray_SimpleNewFromData(2, dims, PyArray_FLOAT64, data);
     //
     py_assert(PyArray_Return(array) == (void*)array);
+    Py_XDECREF(array);
     Py_RETURN_NONE;
 }
 
     //
     void* data2 = PyArray_DATA(array);
     py_assert(data2 == (void*)(data));
+    Py_XDECREF(array);
+    Py_RETURN_NONE;
+}
+
+static PyObject*
+_test_STRIDES(PyObject* self, PyObject* args) {
+    /* double* data = (double*)0x01; // a non-NULL pointer */
+    /* npy_intp dims[3] = {3, 5, 7}; */
+    /* PyArrayObject* array = (PyArrayObject*)PyArray_SimpleNewFromData(3, dims,  */
+    /*                                                                  PyArray_FLOAT64, data); */
+    /* npy_intp* strides = PyArray_STRIDES(array); */
+    /* py_assert(strides[0] == 7*5*sizeof(double)); */
+    /* py_assert(strides[1] == 7*sizeof(double)); */
+    /* py_assert(strides[2] == sizeof(double)); */
+    /* Py_XDECREF(array); */
+
+    /* npy_intp dims2[2] = {4, 2}; */
+    /* array = (PyArrayObject*)PyArray_SimpleNewFromData(2, dims2, PyArray_FLOAT64, data); */
+    /* strides = PyArray_STRIDES(array); */
+    /* py_assert(strides[0] == 2*sizeof(double)); */
+    /* py_assert(strides[1] == sizeof(double)); */
+    /* Py_XDECREF(array); */
     Py_RETURN_NONE;
 }
 
     {"_test_DIMS", _test_DIMS, METH_NOARGS, "..."},
     {"_test_Return", _test_Return, METH_NOARGS, "..."},
     {"_test_DATA", _test_DATA, METH_NOARGS, "..."},
+    {"_test_STRIDES", _test_STRIDES, METH_NOARGS, "..."},
     {NULL}  /* Sentinel */
 };
 
 #define PyMODINIT_FUNC void
 #endif
 PyMODINIT_FUNC
-initfakenumpy_test(void) 
+INIT(void) 
 {
     PyObject* m;
 
-    m = Py_InitModule3("fakenumpy_test", fakenumpy_test_methods,
+    m = Py_InitModule3(MODNAME, fakenumpy_test_methods,
                        "C tests for fakenumpy");
     import_array();
 }
+import sys
+is_pypy = hasattr(sys, 'pypy_version_info')
 from distutils.core import setup, Extension
 
 fakenumpy = Extension('fakenumpy',
                            depends = ['fakenumpy.h'],
                            extra_compile_args=['-g'])
 
+fakenumpy_test_direct = Extension('fakenumpy_test_direct',
+                                  sources = ['fakenumpy_test.c'],
+                                  depends = ['fakenumpy.h'],
+                                  extra_compile_args=['-g'],
+                                  define_macros=[('USE_NUMPY', None)])
+
+ext_modules = [fakenumpy, fakenumpy_test]
+if not is_pypy:
+    ext_modules.append(fakenumpy_test_direct)
+
 setup(name = 'fakenumpy',
       version = '0.1',
       description = 'fakenumpy',
-      ext_modules = [fakenumpy, fakenumpy_test])
+      ext_modules = ext_modules)

test_fakenumpy.py

 import py
 import ctypes
 import fakenumpy
-import fakenumpy_test
 try:
     import numpypy as np
     is_pypy = True
     is_pypy = False
 
 
+def _import_c_tests(mod):
+    glob = globals()
+    for name, value in mod.__dict__.iteritems():
+        if name.startswith('_test'):
+            fn_name = name[1:]
+            if 'direct' in mod.__name__:
+                fn_name += '_direct'
+            def fn(test=value):
+                test()
+            fn.__name__ = fn_name
+            glob[fn_name] = fn
+
+import fakenumpy_test
+_import_c_tests(fakenumpy_test)
+
+if not is_pypy:
+    import fakenumpy_test_direct
+    _import_c_tests(fakenumpy_test_direct)
+
+
 if is_pypy:
     def build_typedict():
         d = {}
     array[0, 0] = 42
     assert buf[0] == 42
 
-def _import_c_tests():
-    glob = globals()
-    for name, value in fakenumpy_test.__dict__.iteritems():
-        if name.startswith('_test'):
-            def fn(test=value):
-                test()
-            fn.__name__ = name[1:]
-            glob[name[1:]] = fn
-
-_import_c_tests()