Skip to content

Commit

Permalink
Copying from prework_no_rst branch (PR pybind#3087): test_numpy_array…
Browse files Browse the repository at this point in the history
….cpp, test_stl.cpp
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jul 11, 2021
1 parent 0f4761b commit 711847e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 87 deletions.
124 changes: 45 additions & 79 deletions tests/test_numpy_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ TEST_SUBMODULE(numpy_array, sm) {
return py::isinstance<py::array>(std::move(yes))
&& !py::isinstance<py::array>(std::move(no));
});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("isinstance_typed", [](py::object o) {
sm.def("isinstance_typed", [](const py::object &o) {
return py::isinstance<py::array_t<double>>(o) && !py::isinstance<py::array_t<int>>(o);
});

Expand All @@ -248,60 +247,47 @@ TEST_SUBMODULE(numpy_array, sm) {
});

// test_overload_resolution
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<double>) { return "double"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<float>) { return "float"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<int>) { return "int"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<unsigned short>) { return "unsigned short"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<long long>) { return "long long"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<std::complex<double>>) { return "double complex"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded", [](py::array_t<std::complex<float>>) { return "float complex"; });

// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<std::complex<double>>) { return "double complex"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<double>) { return "double"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<std::complex<float>>) { return "float complex"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded2", [](py::array_t<float>) { return "float"; });
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"; });

// [workaround(intel)] ICC 20/21 breaks with py::arg().stuff, using py::arg{}.stuff works.

// Only accept the exact types:
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded3", [](py::array_t<int>) { return "int"; }, py::arg{}.noconvert());
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded3", [](py::array_t<double>) { return "double"; }, py::arg{}.noconvert());
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());

// 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:
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded4", [](py::array_t<long long, 0>) { return "long long"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded4", [](py::array_t<double, 0>) { return "double"; });
sm.def("overloaded4", [](const py::array_t<long long, 0> &) { return "long long"; });
sm.def("overloaded4", [](const py::array_t<double, 0> &) { return "double"; });

// But we do allow conversion to int if forcecast is enabled (but only if no overload matches
// without conversion)
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded5", [](py::array_t<unsigned int>) { return "unsigned int"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("overloaded5", [](py::array_t<double>) { return "double"; });
sm.def("overloaded5", [](const py::array_t<unsigned int> &) { return "unsigned int"; });
sm.def("overloaded5", [](const py::array_t<double> &) { return "double"; });

// test_greedy_string_overload
// Issue 685: ndarray shouldn't go to std::string overload
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("issue685", [](std::string) { return "string"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("issue685", [](py::array) { return "array"; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("issue685", [](py::object) { return "other"; });
sm.def("issue685", [](const std::string &) { return "string"; });
sm.def("issue685", [](const py::array &) { return "array"; });
sm.def("issue685", [](const py::object &) { return "other"; });

// test_array_unchecked_fixed_dims
sm.def("proxy_add2", [](py::array_t<double> a, double v) {
Expand Down Expand Up @@ -424,73 +410,53 @@ TEST_SUBMODULE(numpy_array, sm) {

// test_argument_conversions
sm.def(
"accept_double",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, 0>) {},
py::arg("a"));
"accept_double", [](const py::array_t<double, 0> &) {}, py::arg("a"));
sm.def(
"accept_double_forcecast",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast>) {},
[](const py::array_t<double, py::array::forcecast> &) {},
py::arg("a"));
sm.def(
"accept_double_c_style",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::c_style>) {},
[](const py::array_t<double, py::array::c_style> &) {},
py::arg("a"));
sm.def(
"accept_double_c_style_forcecast",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
[](const py::array_t<double, py::array::forcecast | py::array::c_style> &) {},
py::arg("a"));
sm.def(
"accept_double_f_style",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::f_style>) {},
[](const py::array_t<double, py::array::f_style> &) {},
py::arg("a"));
sm.def(
"accept_double_f_style_forcecast",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
[](const py::array_t<double, py::array::forcecast | py::array::f_style> &) {},
py::arg("a"));
sm.def(
"accept_double_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, 0>) {},
"a"_a.noconvert());
"accept_double_noconvert", [](const py::array_t<double, 0> &) {}, "a"_a.noconvert());
sm.def(
"accept_double_forcecast_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast>) {},
[](const py::array_t<double, py::array::forcecast> &) {},
"a"_a.noconvert());
sm.def(
"accept_double_c_style_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::c_style>) {},
[](const py::array_t<double, py::array::c_style> &) {},
"a"_a.noconvert());
sm.def(
"accept_double_c_style_forcecast_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::c_style>) {},
[](const py::array_t<double, py::array::forcecast | py::array::c_style> &) {},
"a"_a.noconvert());
sm.def(
"accept_double_f_style_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::f_style>) {},
[](const py::array_t<double, py::array::f_style> &) {},
"a"_a.noconvert());
sm.def(
"accept_double_f_style_forcecast_noconvert",
// NOLINTNEXTLINE(performance-unnecessary-value-param)
[](py::array_t<double, py::array::forcecast | py::array::f_style>) {},
[](const py::array_t<double, py::array::forcecast | py::array::f_style> &) {},
"a"_a.noconvert());

// Check that types returns correct npy format descriptor
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_float", [](py::array_t<float>) {});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_double", [](py::array_t<double>) {});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_const_float", [](py::array_t<const float>) {});
// NOLINTNEXTLINE(performance-unnecessary-value-param)
sm.def("test_fmt_desc_const_double", [](py::array_t<const double>) {});
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> &) {});
}
14 changes: 6 additions & 8 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ TEST_SUBMODULE(stl, m) {
}, py::arg_v("x", std::nullopt, "None"));

m.def("nodefer_none_optional", [](std::optional<int>) { return true; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
m.def("nodefer_none_optional", [](py::none) { return false; });
m.def("nodefer_none_optional", [](const py::none &) { return false; });

using opt_holder = OptionalHolder<std::optional, MoveOutDetector>;
py::class_<opt_holder>(m, "OptionalHolder", "Class with optional member")
Expand Down Expand Up @@ -299,12 +298,11 @@ TEST_SUBMODULE(stl, m) {
m.def("stl_pass_by_pointer", [](std::vector<int>* v) { return *v; }, "v"_a=nullptr);

// #1258: pybind11/stl.h converts string to vector<string>
// NOLINTNEXTLINE(performance-unnecessary-value-param)
m.def("func_with_string_or_vector_string_arg_overload", [](std::vector<std::string>) { return 1; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
m.def("func_with_string_or_vector_string_arg_overload", [](std::list<std::string>) { return 2; });
// NOLINTNEXTLINE(performance-unnecessary-value-param)
m.def("func_with_string_or_vector_string_arg_overload", [](std::string) { return 3; });
m.def("func_with_string_or_vector_string_arg_overload",
[](const std::vector<std::string> &) { return 1; });
m.def("func_with_string_or_vector_string_arg_overload",
[](const std::list<std::string> &) { return 2; });
m.def("func_with_string_or_vector_string_arg_overload", [](const std::string &) { return 3; });

class Placeholder {
public:
Expand Down

0 comments on commit 711847e

Please sign in to comment.