diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/multiarray_tests.c.src | 16 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 65 |
2 files changed, 63 insertions, 18 deletions
diff --git a/numpy/core/src/multiarray/multiarray_tests.c.src b/numpy/core/src/multiarray/multiarray_tests.c.src index 1f225d5ec..040026f2c 100644 --- a/numpy/core/src/multiarray/multiarray_tests.c.src +++ b/numpy/core/src/multiarray/multiarray_tests.c.src @@ -28,7 +28,7 @@ static int copy_@type@(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *ni PyArrayNeighborhoodIter_Reset(niterx); for(j = 0; j < itx->ao->nd; ++j) { - odims[0] = bounds[2 * j + 1] - bounds[2 * j] + 1; + odims[j] = bounds[2 * j + 1] - bounds[2 * j] + 1; } aout = (PyArrayObject*)PyArray_SimpleNew(itx->ao->nd, odims, @typenum@); if (aout == NULL) { @@ -69,7 +69,7 @@ static int copy_object(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *ni PyArrayNeighborhoodIter_Reset(niterx); for(j = 0; j < itx->ao->nd; ++j) { - odims[0] = bounds[2 * j + 1] - bounds[2 * j] + 1; + odims[j] = bounds[2 * j + 1] - bounds[2 * j] + 1; } aout = (PyArrayObject*)PyArray_SimpleNew(itx->ao->nd, odims, NPY_OBJECT); if (aout == NULL) { @@ -95,7 +95,7 @@ test_neighborhood_iterator(PyObject* NPY_UNUSED(self), PyObject* args) PyObject *x, *fill, *out, *b; PyArrayObject *ax, *afill; PyArrayIterObject *itx; - int i, typenum, imode; + int i, typenum, imode, st; npy_intp bounds[NPY_MAXDIMS*2]; PyArrayNeighborhoodIterObject *niterx; PyArrayNeighborhoodIterMode mode; @@ -167,19 +167,23 @@ test_neighborhood_iterator(PyObject* NPY_UNUSED(self), PyObject* args) switch (typenum) { case NPY_OBJECT: - copy_object(itx, niterx, bounds, &out); + st = copy_object(itx, niterx, bounds, &out); break; case NPY_INT: - copy_int(itx, niterx, bounds, &out); + st = copy_int(itx, niterx, bounds, &out); break; case NPY_DOUBLE: - copy_double(itx, niterx, bounds, &out); + st = copy_double(itx, niterx, bounds, &out); break; default: PyErr_SetString(PyExc_ValueError, "Type not supported"); goto clean_niterx; } + if (st) { + goto clean_niterx; + } + Py_DECREF(niterx); Py_XDECREF(afill); Py_DECREF(itx); diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index f6102bef2..0e96deab2 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1069,18 +1069,59 @@ def can_use_decimal(): # TODO: test for multidimensional NEIGH_MODE = {'zero': 0, 'one': 1, 'constant': 2, 'circular': 3, 'mirror': 4} class TestNeighborhoodIter(TestCase): - ## Simple, 2d tests - #def test_simple2d(self): - # # Test zero and one padding for simple data type - # x = np.array([[0, 1], [2, 3]], dtype=np.float) - # r = [np.array([[0, 0], [0, 0]], dtype=np.float), - # np.array([[0, 0], [0, 1]], dtype=np.float), - # np.array([[0, 0], [1, 0]], dtype=np.float), - # np.array([[0, 0], [0, 2]], dtype=np.float), - # np.array([[0, 1], [2, 3]], dtype=np.float),] - # l = test_neighborhood_iterator(x, [-1, 1, -1, 1], x[0], NEIGH_MODE['zero']) - # print l - # #assert_array_equal(l, r) + # Simple, 2d tests + def _test_simple2d(self, dt): + # Test zero and one padding for simple data type + x = np.array([[0, 1], [2, 3]], dtype=dt) + r = [np.array([[0, 0, 0], [0, 0, 1]], dtype=dt), + np.array([[0, 0, 0], [0, 1, 0]], dtype=dt), + np.array([[0, 0, 1], [0, 2, 3]], dtype=dt), + np.array([[0, 1, 0], [2, 3, 0]], dtype=dt)] + l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], NEIGH_MODE['zero']) + assert_array_equal(l, r) + + r = [np.array([[1, 1, 1], [1, 0, 1]], dtype=dt), + np.array([[1, 1, 1], [0, 1, 1]], dtype=dt), + np.array([[1, 0, 1], [1, 2, 3]], dtype=dt), + np.array([[0, 1, 1], [2, 3, 1]], dtype=dt)] + l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], NEIGH_MODE['one']) + assert_array_equal(l, r) + + r = [np.array([[4, 4, 4], [4, 0, 1]], dtype=dt), + np.array([[4, 4, 4], [0, 1, 4]], dtype=dt), + np.array([[4, 0, 1], [4, 2, 3]], dtype=dt), + np.array([[0, 1, 4], [2, 3, 4]], dtype=dt)] + l = test_neighborhood_iterator(x, [-1, 0, -1, 1], 4, NEIGH_MODE['constant']) + assert_array_equal(l, r) + + def test_simple2d(self): + self._test_simple2d(np.float) + + @dec.skipif(not can_use_decimal(), + "Skip neighborhood iterator tests for decimal objects " \ + "(decimal module not available") + def test_simple2d_object(self): + from decimal import Decimal + self._test_simple2d(Decimal) + + def _test_mirror2d(self, dt): + x = np.array([[0, 1], [2, 3]], dtype=dt) + r = [np.array([[0, 0, 1], [0, 0, 1]], dtype=dt), + np.array([[0, 1, 1], [0, 1, 1]], dtype=dt), + np.array([[0, 0, 1], [2, 2, 3]], dtype=dt), + np.array([[0, 1, 1], [2, 3, 3]], dtype=dt)] + l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], NEIGH_MODE['mirror']) + assert_array_equal(l, r) + + def test_mirror2d(self): + self._test_mirror2d(np.float) + + @dec.skipif(not can_use_decimal(), + "Skip neighborhood iterator tests for decimal objects " \ + "(decimal module not available") + def test_mirror2d_object(self): + from decimal import Decimal + self._test_mirror2d(Decimal) # Simple, 1d tests def _test_simple(self, dt): |