mirror of
https://github.com/pybind/pybind11.git
synced 2026-03-14 20:27:47 +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.
This commit is contained in:
@@ -26,20 +26,6 @@ template<typename... Ix> arr data_t(const arr_t& a, Ix... index) {
|
||||
return arr(a.size() - a.index_at(index...), a.data(index...));
|
||||
}
|
||||
|
||||
arr& mutate_data(arr& a) {
|
||||
auto ptr = (uint8_t *) a.mutable_data();
|
||||
for (ssize_t i = 0; i < a.nbytes(); i++)
|
||||
ptr[i] = (uint8_t) (ptr[i] * 2);
|
||||
return a;
|
||||
}
|
||||
|
||||
arr_t& mutate_data_t(arr_t& a) {
|
||||
auto ptr = a.mutable_data();
|
||||
for (ssize_t i = 0; i < a.size(); i++)
|
||||
ptr[i]++;
|
||||
return a;
|
||||
}
|
||||
|
||||
template<typename... Ix> arr& mutate_data(arr& a, Ix... index) {
|
||||
auto ptr = (uint8_t *) a.mutable_data(index...);
|
||||
for (ssize_t i = 0; i < a.nbytes() - a.offset_at(index...); i++)
|
||||
@@ -82,9 +68,11 @@ template <typename T, typename T2> py::handle auxiliaries(T &&r, T2 &&r2) {
|
||||
return l.release();
|
||||
}
|
||||
|
||||
test_initializer numpy_array([](py::module &m) {
|
||||
auto sm = m.def_submodule("array");
|
||||
TEST_SUBMODULE(numpy_array, sm) {
|
||||
try { py::module::import("numpy"); }
|
||||
catch (...) { return; }
|
||||
|
||||
// test_array_attributes
|
||||
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, ssize_t dim) { return a.shape(dim); });
|
||||
@@ -96,25 +84,25 @@ test_initializer numpy_array([](py::module &m) {
|
||||
sm.def("nbytes", [](const arr& a) { return a.nbytes(); });
|
||||
sm.def("owndata", [](const arr& a) { return a.owndata(); });
|
||||
|
||||
def_index_fn(data, const arr&);
|
||||
def_index_fn(data_t, const arr_t&);
|
||||
// test_index_offset
|
||||
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&);
|
||||
// test_data
|
||||
def_index_fn(data, const arr&);
|
||||
def_index_fn(data_t, const arr_t&);
|
||||
// test_mutate_data, test_mutate_readonly
|
||||
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&);
|
||||
|
||||
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 });
|
||||
});
|
||||
// test_make_c_f_array
|
||||
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 }); });
|
||||
|
||||
// test_wrap
|
||||
sm.def("wrap", [](py::array a) {
|
||||
return py::array(
|
||||
a.dtype(),
|
||||
@@ -125,12 +113,12 @@ test_initializer numpy_array([](py::module &m) {
|
||||
);
|
||||
});
|
||||
|
||||
// test_numpy_view
|
||||
struct ArrayClass {
|
||||
int data[2] = { 1, 2 };
|
||||
ArrayClass() { py::print("ArrayClass()"); }
|
||||
~ArrayClass() { py::print("~ArrayClass()"); }
|
||||
};
|
||||
|
||||
py::class_<ArrayClass>(sm, "ArrayClass")
|
||||
.def(py::init<>())
|
||||
.def("numpy_view", [](py::object &obj) {
|
||||
@@ -140,16 +128,18 @@ test_initializer numpy_array([](py::module &m) {
|
||||
}
|
||||
);
|
||||
|
||||
// test_cast_numpy_int64_to_uint64
|
||||
sm.def("function_taking_uint64", [](uint64_t) { });
|
||||
|
||||
// test_isinstance
|
||||
sm.def("isinstance_untyped", [](py::object yes, py::object no) {
|
||||
return py::isinstance<py::array>(yes) && !py::isinstance<py::array>(no);
|
||||
});
|
||||
|
||||
sm.def("isinstance_typed", [](py::object o) {
|
||||
return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
|
||||
});
|
||||
|
||||
// test_constructors
|
||||
sm.def("default_constructors", []() {
|
||||
return py::dict(
|
||||
"array"_a=py::array(),
|
||||
@@ -157,7 +147,6 @@ test_initializer numpy_array([](py::module &m) {
|
||||
"array_t<double>"_a=py::array_t<double>()
|
||||
);
|
||||
});
|
||||
|
||||
sm.def("converting_constructors", [](py::object o) {
|
||||
return py::dict(
|
||||
"array"_a=py::array(o),
|
||||
@@ -166,7 +155,7 @@ test_initializer numpy_array([](py::module &m) {
|
||||
);
|
||||
});
|
||||
|
||||
// Overload resolution tests:
|
||||
// test_overload_resolution
|
||||
sm.def("overloaded", [](py::array_t<double>) { return "double"; });
|
||||
sm.def("overloaded", [](py::array_t<float>) { return "float"; });
|
||||
sm.def("overloaded", [](py::array_t<int>) { return "int"; });
|
||||
@@ -194,11 +183,13 @@ test_initializer numpy_array([](py::module &m) {
|
||||
sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; });
|
||||
sm.def("overloaded5", [](py::array_t<double>) { return "double"; });
|
||||
|
||||
// test_greedy_string_overload
|
||||
// Issue 685: ndarray shouldn't go to std::string overload
|
||||
sm.def("issue685", [](std::string) { return "string"; });
|
||||
sm.def("issue685", [](py::array) { return "array"; });
|
||||
sm.def("issue685", [](py::object) { return "other"; });
|
||||
|
||||
// test_array_unchecked_fixed_dims
|
||||
sm.def("proxy_add2", [](py::array_t<double> a, double v) {
|
||||
auto r = a.mutable_unchecked<2>();
|
||||
for (ssize_t i = 0; i < r.shape(0); i++)
|
||||
@@ -238,6 +229,7 @@ test_initializer numpy_array([](py::module &m) {
|
||||
return auxiliaries(r, r2);
|
||||
});
|
||||
|
||||
// test_array_unchecked_dyn_dims
|
||||
// Same as the above, but without a compile-time dimensions specification:
|
||||
sm.def("proxy_add2_dyn", [](py::array_t<double> a, double v) {
|
||||
auto r = a.mutable_unchecked();
|
||||
@@ -264,19 +256,21 @@ test_initializer numpy_array([](py::module &m) {
|
||||
return auxiliaries(a, a);
|
||||
});
|
||||
|
||||
// test_array_failures
|
||||
// Issue #785: Uninformative "Unknown internal error" exception when constructing array from empty object:
|
||||
sm.def("array_fail_test", []() { return py::array(py::object()); });
|
||||
sm.def("array_t_fail_test", []() { return py::array_t<double>(py::object()); });
|
||||
|
||||
// Make sure the error from numpy is being passed through:
|
||||
sm.def("array_fail_test_negative_size", []() { int c = 0; return py::array(-1, &c); });
|
||||
|
||||
// test_initializer_list
|
||||
// Issue (unnumbered; reported in #788): regression: initializer lists can be ambiguous
|
||||
sm.def("array_initializer_list", []() { return py::array_t<float>(1); }); // { 1 } also works, but clang warns about it
|
||||
sm.def("array_initializer_list", []() { return py::array_t<float>({ 1, 2 }); });
|
||||
sm.def("array_initializer_list", []() { return py::array_t<float>({ 1, 2, 3 }); });
|
||||
sm.def("array_initializer_list", []() { return py::array_t<float>({ 1, 2, 3, 4 }); });
|
||||
sm.def("array_initializer_list1", []() { return py::array_t<float>(1); }); // { 1 } also works, but clang warns about it
|
||||
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 }); });
|
||||
|
||||
// test_array_resize
|
||||
// reshape array to 2D without changing size
|
||||
sm.def("array_reshape2", [](py::array_t<double> a) {
|
||||
const ssize_t dim_sz = (ssize_t)std::sqrt(a.size());
|
||||
@@ -290,6 +284,7 @@ test_initializer numpy_array([](py::module &m) {
|
||||
a.resize({N, N, N}, refcheck);
|
||||
});
|
||||
|
||||
// test_array_create_and_resize
|
||||
// return 2D array with Nrows = Ncols = N
|
||||
sm.def("create_and_resize", [](size_t N) {
|
||||
py::array_t<double> a;
|
||||
@@ -297,4 +292,4 @@ test_initializer numpy_array([](py::module &m) {
|
||||
std::fill(a.mutable_data(), a.mutable_data() + a.size(), 42.);
|
||||
return a;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user