8000 TST: added test for PyArray_AsCArray #5313 · numpy/numpy@4762bde · GitHub
[go: up one dir, main page]

Skip to content

Commit 4762bde

Browse files
committed
TST: added test for PyArray_AsCArray #5313
1 parent a4163c4 commit 4762bde

File tree

2 files changed

+93
-1
lines changed

2 files changed

+93
-1
lines changed

numpy/core/src/multiarray/multiarray_tests.c.src

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,78 @@ array_indexing(PyObject *NPY_UNUSED(self), PyObject *args)
719719
return NULL;
720720
}
721721

722+
/*
723+
* Test C-api PyArray_AsCArray item getter
724+
*/
725+
static PyObject *
726+
test_as_c_array(PyObject *NPY_UNUSED(self), PyObject *args)
727+
{
728+
PyArrayObject *array_obj;
729+
npy_intp i=0, j=0, k=0;
730+
if (!PyArg_ParseTuple(args, "O!l|ll",
731+
&PyArray_Type, &array_obj,
732+
&i, &j, &k)) {
733+
return NULL;
734+
}
735+
736+
if (NULL == array_obj) {
737+
return NULL;
738+
}
739+
740+
npy_intp dims[3]; // max 3-dim
741+
npy_intp num_dims = PyArray_NDIM(array_obj);
742+
PyArray_Descr *descr = PyArray_DESCR(array_obj);
743+
744+
double *array1 = NULL;
745+
double **array2 = NULL;
746+
double ***array3 = NULL;
747+
double temp;
748+
749+
switch (num_dims) {
750+
case 1:
751+
if (PyArray_AsCArray(
752+
(PyObject **) &array_obj,
753+
(void *) &array1,
754+
dims,
755+
1,
756+
descr) < 0) {
757+
PyErr_SetString(PyExc_RuntimeError, "error converting 1D array");
758+
return NULL;
759+
}
760+
temp = array1[i];
761+
PyArray_Free((PyObject *) array_obj, (void *) array1);
762+
break;
763+
case 2:
764+
if (PyArray_AsCArray(
765+
(PyObject **) &array_obj,
766+
(void **) &array2,
767+
dims,
768+
2,
769+
descr) < 0) {
770+
PyErr_SetString(PyExc_RuntimeError, "error converting 2D array");
771+
return NULL;
772+
}
773+
temp = array2[i][j];
774+
PyArray_Free((PyObject *) array_obj, (void *) array2);
775+
break;
776+
case 3:
777+
if (PyArray_AsCArray(
778+
(PyObject **) &array_obj,
779+
(void ***) &array3,
780+
dims,
781+
3,
782+
descr) < 0) {
783+
PyErr_SetString(PyExc_RuntimeError, "error converting 3D array");
784+
return NULL;
785+
}
786+
temp = array3[i][j][k];
787+
PyArray_Free((PyObject *) array_obj, (void *) array3);
788+
break;
789+
default:
790+
PyErr_SetString(PyExc_ValueError, "array.ndim not in [1, 3]");
791+
}
792+
return Py_BuildValue("f", temp);
793+
}
722794

723795
/*
724796
* Test nditer of too large arrays using remove axis, etc.
@@ -850,6 +922,9 @@ static PyMethodDef Multiarray_TestsMethods[] = {
850922
{"array_indexing",
851923
array_indexing,
852924
METH_VARARGS, NULL},
925+
{"test_as_c_array",
926+
test_as_c_array,
927+
METH_VARARGS, NULL},
853928
{"test_nditer_too_large",
854929
test_nditer_too_large,
855930
METH_VARARGS, NULL},

numpy/core/tests/test_multiarray.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from numpy.core.multiarray_tests import (
2323
test_neighborhood_iterator, test_neighborhood_iterator_oob,
2424
test_pydatamem_seteventhook_start, test_pydatamem_seteventhook_end,
25-
test_inplace_increment, get_buffer_info
25+
test_inplace_increment, get_buffer_info, test_as_c_array
2626
)
2727
from numpy.testing import (
2828
TestCase, run_module_suite, assert_, assert_raises,
@@ -4201,6 +4201,23 @@ def test_mapiter(self):
42014201
assert_equal(b, [ 100.1, 51., 6., 3., 4., 5. ])
42024202

42034203

4204+
class TestAsCArray(TestCase):
4205+
def test_1darray(self):
4206+
array = np.arange(24, dtype=np.double)
4207+
from_c = test_as_c_array(array, 3)
4208+
assert_equal(array[3], from_c)
4209+
4210+
def test_2darray(self):
4211+
< 6D47 span class=pl-s1>array = np.arange(24, dtype=np.double).reshape(3, 8)
4212+
from_c = test_as_c_array(array, 2, 4)
4213+
assert_equal(array[2, 4], from_c)
4214+
4215+
def test_3darray(self):
4216+
array = np.arange(24, dtype=np.double).reshape(2, 3, 4)
4217+
from_c = test_as_c_array(array, 1, 2, 3)
4218+
assert_equal(array[1, 2, 3], from_c)
4219+
4220+
42044221
class PriorityNdarray():
42054222
__array_priority__ = 1000
42064223

0 commit comments

Comments
 (0)
0