make changes as per review comments

This commit is contained in:
Francesco Rizzi 2023-06-28 20:51:17 +02:00
parent 136c664b5a
commit d2ea386ef7
2 changed files with 36 additions and 16 deletions

View File

@ -206,6 +206,13 @@ TEST_SUBMODULE(numpy_array, sm) {
sm.def("nbytes", [](const arr &a) { return a.nbytes(); }); sm.def("nbytes", [](const arr &a) { return a.nbytes(); });
sm.def("owndata", [](const arr &a) { return a.owndata(); }); sm.def("owndata", [](const arr &a) { return a.owndata(); });
sm.attr("defined_NDEBUG") =
#ifdef NDEBUG
true;
#else
false;
#endif
// test_index_offset // test_index_offset
def_index_fn(index_at, const arr &); def_index_fn(index_at, const arr &);
def_index_fn(index_at_t, const arr_t &); def_index_fn(index_at_t, const arr_t &);

View File

@ -109,27 +109,40 @@ def test_data(arr, args, ret):
assert all(m.data(arr, *args)[(1 if byteorder == "little" else 0) :: 2] == 0) assert all(m.data(arr, *args)[(1 if byteorder == "little" else 0) :: 2] == 0)
@pytest.mark.parametrize(
"func",
[
m.at_t,
m.mutate_at_t,
m.const_subscript_via_call_operator_t,
m.subscript_via_call_operator_t,
][: 2 if m.defined_NDEBUG else 99],
)
@pytest.mark.parametrize("dim", [0, 1, 3]) @pytest.mark.parametrize("dim", [0, 1, 3])
def test_at_fail(arr, dim): def test_elem_reference(arr, func, dim):
for func in m.at_t, m.mutate_at_t: with pytest.raises(IndexError) as excinfo:
with pytest.raises(IndexError) as excinfo: func(arr, *([0] * dim))
func(arr, *([0] * dim)) assert str(excinfo.value) == f"index dimension mismatch: {dim} (ndim = 2)"
assert str(excinfo.value) == f"index dimension mismatch: {dim} (ndim = 2)"
def test_at(arr): # @pytest.mark.parametrize("dim", [0, 1, 3])
assert m.at_t(arr, 0, 2) == 3 # def test_at_fail(arr, dim):
assert m.at_t(arr, 1, 0) == 4 # for func in m.at_t, m.mutate_at_t:
# with pytest.raises(IndexError) as excinfo:
assert all(m.mutate_at_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6]) # func(arr, *([0] * dim))
assert all(m.mutate_at_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6]) # assert str(excinfo.value) == f"index dimension mismatch: {dim} (ndim = 2)"
def test_subscript_via_call_operator(arr): @pytest.mark.parametrize("func", [m.at_t, m.const_subscript_via_call_operator_t])
assert m.const_subscript_via_call_operator_t(arr, 0, 2) == 3 def test_const_elem_reference(arr, func):
assert m.const_subscript_via_call_operator_t(arr, 1, 0) == 4 assert func(arr, 0, 2) == 3
assert all(m.subscript_via_call_operator_t(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6]) assert func(arr, 1, 0) == 4
assert all(m.subscript_via_call_operator_t(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
@pytest.mark.parametrize("func", [m.mutate_at_t, m.subscript_via_call_operator_t])
def test_mutable_elem_reference(arr, func):
assert all(func(arr, 0, 2).ravel() == [1, 2, 4, 4, 5, 6])
assert all(func(arr, 1, 0).ravel() == [1, 2, 4, 5, 5, 6])
def test_mutate_readonly(arr): def test_mutate_readonly(arr):