2016-08-29 01:41:05 +00:00
|
|
|
/*
|
|
|
|
tests/test_numpy_array.cpp -- test core array functionality
|
|
|
|
|
|
|
|
Copyright (c) 2016 Ivan Smirnov <i.s.smirnov@gmail.com>
|
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <pybind11/numpy.h>
|
|
|
|
#include <pybind11/stl.h>
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2016-09-08 22:03:35 +00:00
|
|
|
#include <cstdint>
|
2021-06-22 16:11:54 +00:00
|
|
|
#include <utility>
|
2016-09-08 22:03:35 +00:00
|
|
|
|
2018-03-20 20:55:29 +00:00
|
|
|
// Size / dtype checks.
|
|
|
|
struct DtypeCheck {
|
|
|
|
py::dtype numpy{};
|
|
|
|
py::dtype pybind11{};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2022-02-10 20:17:07 +00:00
|
|
|
DtypeCheck get_dtype_check(const char *name) {
|
2020-10-03 17:38:03 +00:00
|
|
|
py::module_ np = py::module_::import("numpy");
|
2018-03-20 20:55:29 +00:00
|
|
|
DtypeCheck check{};
|
|
|
|
check.numpy = np.attr("dtype")(np.attr(name));
|
|
|
|
check.pybind11 = py::dtype::of<T>();
|
|
|
|
return check;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<DtypeCheck> get_concrete_dtype_checks() {
|
2022-02-10 20:17:07 +00:00
|
|
|
return {// Normalization
|
|
|
|
get_dtype_check<std::int8_t>("int8"),
|
|
|
|
get_dtype_check<std::uint8_t>("uint8"),
|
|
|
|
get_dtype_check<std::int16_t>("int16"),
|
|
|
|
get_dtype_check<std::uint16_t>("uint16"),
|
|
|
|
get_dtype_check<std::int32_t>("int32"),
|
|
|
|
get_dtype_check<std::uint32_t>("uint32"),
|
|
|
|
get_dtype_check<std::int64_t>("int64"),
|
|
|
|
get_dtype_check<std::uint64_t>("uint64")};
|
2018-03-20 20:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct DtypeSizeCheck {
|
|
|
|
std::string name{};
|
|
|
|
int size_cpp{};
|
|
|
|
int size_numpy{};
|
|
|
|
// For debugging.
|
|
|
|
py::dtype dtype{};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
DtypeSizeCheck get_dtype_size_check() {
|
|
|
|
DtypeSizeCheck check{};
|
|
|
|
check.name = py::type_id<T>();
|
|
|
|
check.size_cpp = sizeof(T);
|
|
|
|
check.dtype = py::dtype::of<T>();
|
|
|
|
check.size_numpy = check.dtype.attr("itemsize").template cast<int>();
|
|
|
|
return check;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<DtypeSizeCheck> get_platform_dtype_size_checks() {
|
|
|
|
return {
|
|
|
|
get_dtype_size_check<short>(),
|
|
|
|
get_dtype_size_check<unsigned short>(),
|
|
|
|
get_dtype_size_check<int>(),
|
|
|
|
get_dtype_size_check<unsigned int>(),
|
|
|
|
get_dtype_size_check<long>(),
|
|
|
|
get_dtype_size_check<unsigned long>(),
|
|
|
|
get_dtype_size_check<long long>(),
|
|
|
|
get_dtype_size_check<unsigned long long>(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arrays.
|
2016-09-08 22:03:35 +00:00
|
|
|
using arr = py::array;
|
|
|
|
using arr_t = py::array_t<uint16_t, 0>;
|
2017-03-13 18:17:18 +00:00
|
|
|
static_assert(std::is_same<arr_t::value_type, uint16_t>::value, "");
|
2016-09-08 22:03:35 +00:00
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename... Ix>
|
|
|
|
arr data(const arr &a, Ix... index) {
|
2017-04-14 20:33:44 +00:00
|
|
|
return arr(a.nbytes() - a.offset_at(index...), (const uint8_t *) a.data(index...));
|
2016-09-08 22:03:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename... Ix>
|
|
|
|
arr data_t(const arr_t &a, Ix... index) {
|
2017-04-14 20:33:44 +00:00
|
|
|
return arr(a.size() - a.index_at(index...), a.data(index...));
|
2016-09-08 22:03:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename... Ix>
|
|
|
|
arr &mutate_data(arr &a, Ix... index) {
|
2022-02-09 14:24:57 +00:00
|
|
|
auto *ptr = (uint8_t *) a.mutable_data(index...);
|
2022-02-08 00:23:20 +00:00
|
|
|
for (py::ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++) {
|
2016-09-08 22:03:35 +00:00
|
|
|
ptr[i] = (uint8_t) (ptr[i] * 2);
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-09-08 22:03:35 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename... Ix>
|
|
|
|
arr_t &mutate_data_t(arr_t &a, Ix... index) {
|
2016-09-08 22:03:35 +00:00
|
|
|
auto ptr = a.mutable_data(index...);
|
2022-02-08 00:23:20 +00:00
|
|
|
for (py::ssize_t i = 0; i < a.size() - a.index_at(index...); i++) {
|
2016-09-08 22:03:35 +00:00
|
|
|
ptr[i]++;
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2016-09-08 22:03:35 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename... Ix>
|
|
|
|
py::ssize_t index_at(const arr &a, Ix... idx) {
|
|
|
|
return a.index_at(idx...);
|
|
|
|
}
|
|
|
|
template <typename... Ix>
|
|
|
|
py::ssize_t index_at_t(const arr_t &a, Ix... idx) {
|
|
|
|
return a.index_at(idx...);
|
|
|
|
}
|
|
|
|
template <typename... Ix>
|
|
|
|
py::ssize_t offset_at(const arr &a, Ix... idx) {
|
|
|
|
return a.offset_at(idx...);
|
|
|
|
}
|
|
|
|
template <typename... Ix>
|
|
|
|
py::ssize_t offset_at_t(const arr_t &a, Ix... idx) {
|
|
|
|
return a.offset_at(idx...);
|
|
|
|
}
|
|
|
|
template <typename... Ix>
|
|
|
|
py::ssize_t at_t(const arr_t &a, Ix... idx) {
|
|
|
|
return a.at(idx...);
|
|
|
|
}
|
|
|
|
template <typename... Ix>
|
|
|
|
arr_t &mutate_at_t(arr_t &a, Ix... idx) {
|
|
|
|
a.mutable_at(idx...)++;
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define def_index_fn(name, type) \
|
|
|
|
sm.def(#name, [](type a) { return name(a); }); \
|
|
|
|
sm.def(#name, [](type a, int i) { return name(a, i); }); \
|
|
|
|
sm.def(#name, [](type a, int i, int j) { return name(a, i, j); }); \
|
2016-09-08 22:03:35 +00:00
|
|
|
sm.def(#name, [](type a, int i, int j, int k) { return name(a, i, j, k); });
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
template <typename T, typename T2>
|
|
|
|
py::handle auxiliaries(T &&r, T2 &&r2) {
|
2022-02-08 00:23:20 +00:00
|
|
|
if (r.ndim() != 2) {
|
|
|
|
throw std::domain_error("error: ndim != 2");
|
|
|
|
}
|
2017-03-20 20:48:38 +00:00
|
|
|
py::list l;
|
|
|
|
l.append(*r.data(0, 0));
|
|
|
|
l.append(*r2.mutable_data(0, 0));
|
|
|
|
l.append(r.data(0, 1) == r2.mutable_data(0, 1));
|
|
|
|
l.append(r.ndim());
|
|
|
|
l.append(r.itemsize());
|
|
|
|
l.append(r.shape(0));
|
|
|
|
l.append(r.shape(1));
|
|
|
|
l.append(r.size());
|
|
|
|
l.append(r.nbytes());
|
|
|
|
return l.release();
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:00:05 +00:00
|
|
|
// note: declaration at local scope would create a dangling reference!
|
|
|
|
static int data_i = 42;
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
TEST_SUBMODULE(numpy_array, sm) {
|
2022-02-10 20:17:07 +00:00
|
|
|
try {
|
|
|
|
py::module_::import("numpy");
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-08 22:03:35 +00:00
|
|
|
|
2018-03-20 20:55:29 +00:00
|
|
|
// test_dtypes
|
|
|
|
py::class_<DtypeCheck>(sm, "DtypeCheck")
|
|
|
|
.def_readonly("numpy", &DtypeCheck::numpy)
|
|
|
|
.def_readonly("pybind11", &DtypeCheck::pybind11)
|
2022-02-10 20:17:07 +00:00
|
|
|
.def("__repr__", [](const DtypeCheck &self) {
|
|
|
|
return py::str("<DtypeCheck numpy={} pybind11={}>").format(self.numpy, self.pybind11);
|
2018-03-20 20:55:29 +00:00
|
|
|
});
|
|
|
|
sm.def("get_concrete_dtype_checks", &get_concrete_dtype_checks);
|
|
|
|
|
|
|
|
py::class_<DtypeSizeCheck>(sm, "DtypeSizeCheck")
|
|
|
|
.def_readonly("name", &DtypeSizeCheck::name)
|
|
|
|
.def_readonly("size_cpp", &DtypeSizeCheck::size_cpp)
|
|
|
|
.def_readonly("size_numpy", &DtypeSizeCheck::size_numpy)
|
2022-02-10 20:17:07 +00:00
|
|
|
.def("__repr__", [](const DtypeSizeCheck &self) {
|
|
|
|
return py::str("<DtypeSizeCheck name='{}' size_cpp={} size_numpy={} dtype={}>")
|
|
|
|
.format(self.name, self.size_cpp, self.size_numpy, self.dtype);
|
2018-03-20 20:55:29 +00:00
|
|
|
});
|
|
|
|
sm.def("get_platform_dtype_size_checks", &get_platform_dtype_size_checks);
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_array_attributes
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def("ndim", [](const arr &a) { return a.ndim(); });
|
|
|
|
sm.def("shape", [](const arr &a) { return arr(a.ndim(), a.shape()); });
|
|
|
|
sm.def("shape", [](const arr &a, py::ssize_t dim) { return a.shape(dim); });
|
|
|
|
sm.def("strides", [](const arr &a) { return arr(a.ndim(), a.strides()); });
|
|
|
|
sm.def("strides", [](const arr &a, py::ssize_t dim) { return a.strides(dim); });
|
|
|
|
sm.def("writeable", [](const arr &a) { return a.writeable(); });
|
|
|
|
sm.def("size", [](const arr &a) { return a.size(); });
|
|
|
|
sm.def("itemsize", [](const arr &a) { return a.itemsize(); });
|
|
|
|
sm.def("nbytes", [](const arr &a) { return a.nbytes(); });
|
|
|
|
sm.def("owndata", [](const arr &a) { return a.owndata(); });
|
2016-09-08 22:03:35 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_index_offset
|
2022-02-10 20:17:07 +00:00
|
|
|
def_index_fn(index_at, const arr &);
|
|
|
|
def_index_fn(index_at_t, const arr_t &);
|
|
|
|
def_index_fn(offset_at, const arr &);
|
|
|
|
def_index_fn(offset_at_t, const arr_t &);
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_data
|
2022-02-10 20:17:07 +00:00
|
|
|
def_index_fn(data, const arr &);
|
|
|
|
def_index_fn(data_t, const arr_t &);
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_mutate_data, test_mutate_readonly
|
2022-02-10 20:17:07 +00:00
|
|
|
def_index_fn(mutate_data, arr &);
|
|
|
|
def_index_fn(mutate_data_t, arr_t &);
|
|
|
|
def_index_fn(at_t, const arr_t &);
|
|
|
|
def_index_fn(mutate_at_t, arr_t &);
|
2016-10-12 21:34:06 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_make_c_f_array
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def("make_f_array", [] { return py::array_t<float>({2, 2}, {4, 8}); });
|
|
|
|
sm.def("make_c_array", [] { return py::array_t<float>({2, 2}, {8, 4}); });
|
2016-10-12 22:57:42 +00:00
|
|
|
|
2018-05-06 13:59:25 +00:00
|
|
|
// test_empty_shaped_array
|
|
|
|
sm.def("make_empty_shaped_array", [] { return py::array(py::dtype("f"), {}, {}); });
|
2019-06-11 12:00:05 +00:00
|
|
|
// test numpy scalars (empty shape, ndim==0)
|
|
|
|
sm.def("scalar_int", []() { return py::array(py::dtype("i"), {}, {}, &data_i); });
|
2018-05-06 13:59:25 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_wrap
|
2021-06-22 16:11:54 +00:00
|
|
|
sm.def("wrap", [](const py::array &a) {
|
2022-02-10 20:17:07 +00:00
|
|
|
return py::array(a.dtype(),
|
|
|
|
{a.shape(), a.shape() + a.ndim()},
|
|
|
|
{a.strides(), a.strides() + a.ndim()},
|
|
|
|
a.data(),
|
|
|
|
a);
|
2016-10-12 22:57:42 +00:00
|
|
|
});
|
2016-10-13 08:37:52 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_numpy_view
|
2016-10-13 08:37:52 +00:00
|
|
|
struct ArrayClass {
|
2022-02-10 20:17:07 +00:00
|
|
|
int data[2] = {1, 2};
|
2016-10-13 08:37:52 +00:00
|
|
|
ArrayClass() { py::print("ArrayClass()"); }
|
|
|
|
~ArrayClass() { py::print("~ArrayClass()"); }
|
|
|
|
};
|
|
|
|
py::class_<ArrayClass>(sm, "ArrayClass")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def("numpy_view", [](py::object &obj) {
|
|
|
|
py::print("ArrayClass::numpy_view()");
|
2022-02-10 20:17:07 +00:00
|
|
|
auto &a = obj.cast<ArrayClass &>();
|
2016-10-13 08:37:52 +00:00
|
|
|
return py::array_t<int>({2}, {4}, a.data, obj);
|
2022-02-10 20:17:07 +00:00
|
|
|
});
|
2016-10-27 22:37:07 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_cast_numpy_int64_to_uint64
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def("function_taking_uint64", [](uint64_t) {});
|
2016-11-16 00:35:22 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_isinstance
|
2016-11-16 00:35:22 +00:00
|
|
|
sm.def("isinstance_untyped", [](py::object yes, py::object no) {
|
2021-06-22 16:11:54 +00:00
|
|
|
return py::isinstance<py::array>(std::move(yes))
|
|
|
|
&& !py::isinstance<py::array>(std::move(no));
|
2016-11-16 00:35:22 +00:00
|
|
|
});
|
2021-07-12 20:10:28 +00:00
|
|
|
sm.def("isinstance_typed", [](const py::object &o) {
|
2016-11-16 00:35:22 +00:00
|
|
|
return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
|
|
|
|
});
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_constructors
|
2016-11-16 00:35:22 +00:00
|
|
|
sm.def("default_constructors", []() {
|
2022-02-10 20:17:07 +00:00
|
|
|
return py::dict("array"_a = py::array(),
|
|
|
|
"array_t<int32>"_a = py::array_t<std::int32_t>(),
|
|
|
|
"array_t<double>"_a = py::array_t<double>());
|
2016-11-16 00:35:22 +00:00
|
|
|
});
|
2021-06-22 16:11:54 +00:00
|
|
|
sm.def("converting_constructors", [](const py::object &o) {
|
2022-02-10 20:17:07 +00:00
|
|
|
return py::dict("array"_a = py::array(o),
|
|
|
|
"array_t<int32>"_a = py::array_t<std::int32_t>(o),
|
|
|
|
"array_t<double>"_a = py::array_t<double>(o));
|
2016-11-16 00:35:22 +00:00
|
|
|
});
|
2017-02-24 10:33:31 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_overload_resolution
|
2021-07-12 20:10:28 +00:00
|
|
|
sm.def("overloaded", [](const py::array_t<double> &) { return "double"; });
|
|
|
|
sm.def("overloaded", [](const py::array_t<float> &) { return "float"; });
|
|
|
|
sm.def("overloaded", [](const py::array_t<int> &) { return "int"; });
|
|
|
|
sm.def("overloaded", [](const py::array_t<unsigned short> &) { return "unsigned short"; });
|
|
|
|
sm.def("overloaded", [](const py::array_t<long long> &) { return "long long"; });
|
|
|
|
sm.def("overloaded",
|
|
|
|
[](const py::array_t<std::complex<double>> &) { return "double complex"; });
|
|
|
|
sm.def("overloaded", [](const py::array_t<std::complex<float>> &) { return "float complex"; });
|
|
|
|
|
|
|
|
sm.def("overloaded2",
|
|
|
|
[](const py::array_t<std::complex<double>> &) { return "double complex"; });
|
|
|
|
sm.def("overloaded2", [](const py::array_t<double> &) { return "double"; });
|
|
|
|
sm.def("overloaded2",
|
|
|
|
[](const py::array_t<std::complex<float>> &) { return "float complex"; });
|
|
|
|
sm.def("overloaded2", [](const py::array_t<float> &) { return "float"; });
|
2017-02-26 23:03:00 +00:00
|
|
|
|
2021-01-15 20:59:47 +00:00
|
|
|
// [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.
|
|
|
|
|
2017-02-26 23:03:00 +00:00
|
|
|
// Only accept the exact types:
|
2021-07-12 20:10:28 +00:00
|
|
|
sm.def(
|
|
|
|
"overloaded3", [](const py::array_t<int> &) { return "int"; }, py::arg{}.noconvert());
|
|
|
|
sm.def(
|
|
|
|
"overloaded3",
|
|
|
|
[](const py::array_t<double> &) { return "double"; },
|
|
|
|
py::arg{}.noconvert());
|
2017-02-26 23:03:00 +00:00
|
|
|
|
|
|
|
// Make sure we don't do unsafe coercion (e.g. float to int) when not using forcecast, but
|
|
|
|
// rather that float gets converted via the safe (conversion to double) overload:
|
2021-07-12 20:10:28 +00:00
|
|
|
sm.def("overloaded4", [](const py::array_t<long long, 0> &) { return "long long"; });
|
|
|
|
sm.def("overloaded4", [](const py::array_t<double, 0> &) { return "double"; });
|
2017-02-26 23:03:00 +00:00
|
|
|
|
|
|
|
// But we do allow conversion to int if forcecast is enabled (but only if no overload matches
|
|
|
|
// without conversion)
|
2021-07-12 20:10:28 +00:00
|
|
|
sm.def("overloaded5", [](const py::array_t<unsigned int> &) { return "unsigned int"; });
|
|
|
|
sm.def("overloaded5", [](const py::array_t<double> &) { return "double"; });
|
2017-02-26 23:03:00 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_greedy_string_overload
|
2017-02-24 10:33:31 +00:00
|
|
|
// Issue 685: ndarray shouldn't go to std::string overload
|
2021-07-12 20:10:28 +00:00
|
|
|
sm.def("issue685", [](const std::string &) { return "string"; });
|
|
|
|
sm.def("issue685", [](const py::array &) { return "array"; });
|
|
|
|
sm.def("issue685", [](const py::object &) { return "other"; });
|
2017-03-19 04:14:23 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_array_unchecked_fixed_dims
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def(
|
|
|
|
"proxy_add2",
|
|
|
|
[](py::array_t<double> a, double v) {
|
|
|
|
auto r = a.mutable_unchecked<2>();
|
|
|
|
for (py::ssize_t i = 0; i < r.shape(0); i++) {
|
|
|
|
for (py::ssize_t j = 0; j < r.shape(1); j++) {
|
|
|
|
r(i, j) += v;
|
|
|
|
}
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
},
|
|
|
|
py::arg{}.noconvert(),
|
|
|
|
py::arg());
|
2017-03-20 20:48:38 +00:00
|
|
|
|
2017-03-19 04:14:23 +00:00
|
|
|
sm.def("proxy_init3", [](double start) {
|
2022-02-10 20:17:07 +00:00
|
|
|
py::array_t<double, py::array::c_style> a({3, 3, 3});
|
2017-03-19 04:14:23 +00:00
|
|
|
auto r = a.mutable_unchecked<3>();
|
2022-02-08 00:23:20 +00:00
|
|
|
for (py::ssize_t i = 0; i < r.shape(0); i++) {
|
|
|
|
for (py::ssize_t j = 0; j < r.shape(1); j++) {
|
|
|
|
for (py::ssize_t k = 0; k < r.shape(2); k++) {
|
|
|
|
r(i, j, k) = start++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-19 04:14:23 +00:00
|
|
|
return a;
|
|
|
|
});
|
|
|
|
sm.def("proxy_init3F", [](double start) {
|
2022-02-10 20:17:07 +00:00
|
|
|
py::array_t<double, py::array::f_style> a({3, 3, 3});
|
2017-03-19 04:14:23 +00:00
|
|
|
auto r = a.mutable_unchecked<3>();
|
2022-02-08 00:23:20 +00:00
|
|
|
for (py::ssize_t k = 0; k < r.shape(2); k++) {
|
|
|
|
for (py::ssize_t j = 0; j < r.shape(1); j++) {
|
|
|
|
for (py::ssize_t i = 0; i < r.shape(0); i++) {
|
|
|
|
r(i, j, k) = start++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-19 04:14:23 +00:00
|
|
|
return a;
|
|
|
|
});
|
2021-06-22 16:11:54 +00:00
|
|
|
sm.def("proxy_squared_L2_norm", [](const py::array_t<double> &a) {
|
2017-03-19 04:14:23 +00:00
|
|
|
auto r = a.unchecked<1>();
|
|
|
|
double sumsq = 0;
|
2022-02-08 00:23:20 +00:00
|
|
|
for (py::ssize_t i = 0; i < r.shape(0); i++) {
|
2017-03-19 04:14:23 +00:00
|
|
|
sumsq += r[i] * r(i); // Either notation works for a 1D array
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2017-03-19 04:14:23 +00:00
|
|
|
return sumsq;
|
|
|
|
});
|
2017-03-20 20:48:38 +00:00
|
|
|
|
|
|
|
sm.def("proxy_auxiliaries2", [](py::array_t<double> a) {
|
|
|
|
auto r = a.unchecked<2>();
|
|
|
|
auto r2 = a.mutable_unchecked<2>();
|
|
|
|
return auxiliaries(r, r2);
|
|
|
|
});
|
|
|
|
|
2020-10-02 17:07:04 +00:00
|
|
|
sm.def("proxy_auxiliaries1_const_ref", [](py::array_t<double> a) {
|
|
|
|
const auto &r = a.unchecked<1>();
|
|
|
|
const auto &r2 = a.mutable_unchecked<1>();
|
|
|
|
return r(0) == r2(0) && r[0] == r2[0];
|
|
|
|
});
|
|
|
|
|
|
|
|
sm.def("proxy_auxiliaries2_const_ref", [](py::array_t<double> a) {
|
|
|
|
const auto &r = a.unchecked<2>();
|
|
|
|
const auto &r2 = a.mutable_unchecked<2>();
|
|
|
|
return r(0, 0) == r2(0, 0);
|
|
|
|
});
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_array_unchecked_dyn_dims
|
2017-03-20 20:48:38 +00:00
|
|
|
// Same as the above, but without a compile-time dimensions specification:
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def(
|
|
|
|
"proxy_add2_dyn",
|
|
|
|
[](py::array_t<double> a, double v) {
|
|
|
|
auto r = a.mutable_unchecked();
|
|
|
|
if (r.ndim() != 2) {
|
|
|
|
throw std::domain_error("error: ndim != 2");
|
2022-02-08 00:23:20 +00:00
|
|
|
}
|
2022-02-10 20:17:07 +00:00
|
|
|
for (py::ssize_t i = 0; i < r.shape(0); i++) {
|
|
|
|
for (py::ssize_t j = 0; j < r.shape(1); j++) {
|
|
|
|
r(i, j) += v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
py::arg{}.noconvert(),
|
|
|
|
py::arg());
|
2017-03-20 20:48:38 +00:00
|
|
|
sm.def("proxy_init3_dyn", [](double start) {
|
2022-02-10 20:17:07 +00:00
|
|
|
py::array_t<double, py::array::c_style> a({3, 3, 3});
|
2017-03-20 20:48:38 +00:00
|
|
|
auto r = a.mutable_unchecked();
|
2022-02-08 00:23:20 +00:00
|
|
|
if (r.ndim() != 3) {
|
|
|
|
throw std::domain_error("error: ndim != 3");
|
|
|
|
}
|
|
|
|
for (py::ssize_t i = 0; i < r.shape(0); i++) {
|
|
|
|
for (py::ssize_t j = 0; j < r.shape(1); j++) {
|
|
|
|
for (py::ssize_t k = 0; k < r.shape(2); k++) {
|
|
|
|
r(i, j, k) = start++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-03-20 20:48:38 +00:00
|
|
|
return a;
|
|
|
|
});
|
|
|
|
sm.def("proxy_auxiliaries2_dyn", [](py::array_t<double> a) {
|
|
|
|
return auxiliaries(a.unchecked(), a.mutable_unchecked());
|
|
|
|
});
|
|
|
|
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def("array_auxiliaries2", [](py::array_t<double> a) { return auxiliaries(a, a); });
|
2017-04-10 15:05:26 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_array_failures
|
2022-02-10 20:17:07 +00:00
|
|
|
// Issue #785: Uninformative "Unknown internal error" exception when constructing array from
|
|
|
|
// empty object:
|
2017-04-10 15:05:26 +00:00
|
|
|
sm.def("array_fail_test", []() { return py::array(py::object()); });
|
|
|
|
sm.def("array_t_fail_test", []() { return py::array_t<double>(py::object()); });
|
2017-04-14 20:33:44 +00:00
|
|
|
// Make sure the error from numpy is being passed through:
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def("array_fail_test_negative_size", []() {
|
|
|
|
int c = 0;
|
|
|
|
return py::array(-1, &c);
|
|
|
|
});
|
2017-04-14 20:33:44 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_initializer_list
|
2017-04-28 15:06:16 +00:00
|
|
|
// Issue (unnumbered; reported in #788): regression: initializer lists can be ambiguous
|
2022-02-10 19:42:03 +00:00
|
|
|
sm.def("array_initializer_list1", []() { return py::array_t<float>(1); });
|
|
|
|
// { 1 } also works for the above, but clang warns about it
|
2022-02-10 20:17:07 +00:00
|
|
|
sm.def("array_initializer_list2", []() { return py::array_t<float>({1, 2}); });
|
|
|
|
sm.def("array_initializer_list3", []() { return py::array_t<float>({1, 2, 3}); });
|
|
|
|
sm.def("array_initializer_list4", []() { return py::array_t<float>({1, 2, 3, 4}); });
|
2017-04-13 16:41:55 +00:00
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_array_resize
|
2017-04-13 16:41:55 +00:00
|
|
|
// reshape array to 2D without changing size
|
|
|
|
sm.def("array_reshape2", [](py::array_t<double> a) {
|
2022-02-10 20:17:07 +00:00
|
|
|
const auto dim_sz = (py::ssize_t) std::sqrt(a.size());
|
2022-02-08 00:23:20 +00:00
|
|
|
if (dim_sz * dim_sz != a.size()) {
|
|
|
|
throw std::domain_error(
|
|
|
|
"array_reshape2: input array total size is not a squared integer");
|
|
|
|
}
|
2017-04-13 16:41:55 +00:00
|
|
|
a.resize({dim_sz, dim_sz});
|
|
|
|
});
|
|
|
|
|
|
|
|
// resize to 3D array with each dimension = N
|
|
|
|
sm.def("array_resize3", [](py::array_t<double> a, size_t N, bool refcheck) {
|
|
|
|
a.resize({N, N, N}, refcheck);
|
|
|
|
});
|
|
|
|
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
// test_array_create_and_resize
|
2017-04-13 16:41:55 +00:00
|
|
|
// return 2D array with Nrows = Ncols = N
|
|
|
|
sm.def("create_and_resize", [](size_t N) {
|
|
|
|
py::array_t<double> a;
|
|
|
|
a.resize({N, N});
|
|
|
|
std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
|
|
|
|
return a;
|
|
|
|
});
|
2018-08-27 22:23:59 +00:00
|
|
|
|
2021-08-26 21:11:01 +00:00
|
|
|
sm.def("array_view",
|
|
|
|
[](py::array_t<uint8_t> a, const std::string &dtype) { return a.view(dtype); });
|
|
|
|
|
2021-08-26 15:12:35 +00:00
|
|
|
sm.def("reshape_initializer_list", [](py::array_t<int> a, size_t N, size_t M, size_t O) {
|
|
|
|
return a.reshape({N, M, O});
|
|
|
|
});
|
|
|
|
sm.def("reshape_tuple", [](py::array_t<int> a, const std::vector<int> &new_shape) {
|
|
|
|
return a.reshape(new_shape);
|
|
|
|
});
|
|
|
|
|
2021-06-22 16:11:54 +00:00
|
|
|
sm.def("index_using_ellipsis",
|
|
|
|
[](const py::array &a) { return a[py::make_tuple(0, py::ellipsis(), 0)]; });
|
2020-09-15 12:50:51 +00:00
|
|
|
|
|
|
|
// test_argument_conversions
|
2021-06-22 16:11:54 +00:00
|
|
|
sm.def(
|
2021-07-12 20:10:28 +00:00
|
|
|
"accept_double", [](const py::array_t<double, 0> &) {}, py::arg("a"));
|
2021-06-22 16:11:54 +00:00
|
|
|
sm.def(
|
|
|
|
"accept_double_forcecast",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::forcecast> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
py::arg("a"));
|
|
|
|
sm.def(
|
|
|
|
"accept_double_c_style",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::c_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
py::arg("a"));
|
|
|
|
sm.def(
|
|
|
|
"accept_double_c_style_forcecast",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::forcecast | py::array::c_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
py::arg("a"));
|
|
|
|
sm.def(
|
|
|
|
"accept_double_f_style",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::f_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
py::arg("a"));
|
|
|
|
sm.def(
|
|
|
|
"accept_double_f_style_forcecast",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::forcecast | py::array::f_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
py::arg("a"));
|
|
|
|
sm.def(
|
2021-07-12 20:10:28 +00:00
|
|
|
"accept_double_noconvert", [](const py::array_t<double, 0> &) {}, "a"_a.noconvert());
|
2021-06-22 16:11:54 +00:00
|
|
|
sm.def(
|
|
|
|
"accept_double_forcecast_noconvert",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::forcecast> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
"a"_a.noconvert());
|
|
|
|
sm.def(
|
|
|
|
"accept_double_c_style_noconvert",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::c_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
"a"_a.noconvert());
|
|
|
|
sm.def(
|
|
|
|
"accept_double_c_style_forcecast_noconvert",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::forcecast | py::array::c_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
"a"_a.noconvert());
|
|
|
|
sm.def(
|
|
|
|
"accept_double_f_style_noconvert",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::f_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
"a"_a.noconvert());
|
|
|
|
sm.def(
|
|
|
|
"accept_double_f_style_forcecast_noconvert",
|
2021-07-12 20:10:28 +00:00
|
|
|
[](const py::array_t<double, py::array::forcecast | py::array::f_style> &) {},
|
2021-06-22 16:11:54 +00:00
|
|
|
"a"_a.noconvert());
|
2021-06-08 18:56:45 +00:00
|
|
|
|
|
|
|
// Check that types returns correct npy format descriptor
|
2021-07-12 20:10:28 +00:00
|
|
|
sm.def("test_fmt_desc_float", [](const py::array_t<float> &) {});
|
|
|
|
sm.def("test_fmt_desc_double", [](const py::array_t<double> &) {});
|
|
|
|
sm.def("test_fmt_desc_const_float", [](const py::array_t<const float> &) {});
|
|
|
|
sm.def("test_fmt_desc_const_double", [](const py::array_t<const double> &) {});
|
Update all remaining tests to new test styles
This udpates all the remaining tests to the new test suite code and
comment styles started in #898. For the most part, the test coverage
here is unchanged, with a few minor exceptions as noted below.
- test_constants_and_functions: this adds more overload tests with
overloads with different number of arguments for more comprehensive
overload_cast testing. The test style conversion broke the overload
tests under MSVC 2015, prompting the additional tests while looking
for a workaround.
- test_eigen: this dropped the unused functions `get_cm_corners` and
`get_cm_corners_const`--these same tests were duplicates of the same
things provided (and used) via ReturnTester methods.
- test_opaque_types: this test had a hidden dependence on ExampleMandA
which is now fixed by using the global UserType which suffices for the
relevant test.
- test_methods_and_attributes: this required some additions to UserType
to make it usable as a replacement for the test's previous SimpleType:
UserType gained a value mutator, and the `value` property is not
mutable (it was previously readonly). Some overload tests were also
added to better test overload_cast (as described above).
- test_numpy_array: removed the untemplated mutate_data/mutate_data_t:
the templated versions with an empty parameter pack expand to the same
thing.
- test_stl: this was already mostly in the new style; this just tweaks
things a bit, localizing a class, and adding some missing
`// test_whatever` comments.
- test_virtual_functions: like `test_stl`, this was mostly in the new
test style already, but needed some `// test_whatever` comments.
This commit also moves the inherited virtual example code to the end
of the file, after the main set of tests (since it is less important
than the other tests, and rather length); it also got renamed to
`test_inherited_virtuals` (from `test_inheriting_repeat`) because it
tests both inherited virtual approaches, not just the repeat approach.
2017-07-25 20:47:36 +00:00
|
|
|
}
|