aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/tests/test_operator_overloading.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/pybind11/tests/test_operator_overloading.cpp')
-rw-r--r--3rdparty/pybind11/tests/test_operator_overloading.cpp63
1 files changed, 59 insertions, 4 deletions
diff --git a/3rdparty/pybind11/tests/test_operator_overloading.cpp b/3rdparty/pybind11/tests/test_operator_overloading.cpp
index 7b111704..0a27bfd5 100644
--- a/3rdparty/pybind11/tests/test_operator_overloading.cpp
+++ b/3rdparty/pybind11/tests/test_operator_overloading.cpp
@@ -43,6 +43,13 @@ public:
friend Vector2 operator-(float f, const Vector2 &v) { return Vector2(f - v.x, f - v.y); }
friend Vector2 operator*(float f, const Vector2 &v) { return Vector2(f * v.x, f * v.y); }
friend Vector2 operator/(float f, const Vector2 &v) { return Vector2(f / v.x, f / v.y); }
+
+ bool operator==(const Vector2 &v) const {
+ return x == v.x && y == v.y;
+ }
+ bool operator!=(const Vector2 &v) const {
+ return x != v.x || y != v.y;
+ }
private:
float x, y;
};
@@ -55,12 +62,22 @@ int operator+(const C2 &, const C2 &) { return 22; }
int operator+(const C2 &, const C1 &) { return 21; }
int operator+(const C1 &, const C2 &) { return 12; }
+// Note: Specializing explicit within `namespace std { ... }` is done due to a
+// bug in GCC<7. If you are supporting compilers later than this, consider
+// specializing `using template<> struct std::hash<...>` in the global
+// namespace instead, per this recommendation:
+// https://en.cppreference.com/w/cpp/language/extending_std#Adding_template_specializations
namespace std {
template<>
struct hash<Vector2> {
// Not a good hash function, but easy to test
size_t operator()(const Vector2 &) { return 4; }
};
+} // namespace std
+
+// Not a good abs function, but easy to test.
+std::string abs(const Vector2&) {
+ return "abs(Vector2)";
}
// MSVC warns about unknown pragmas, and warnings are errors.
@@ -71,11 +88,11 @@ namespace std {
// Here, we suppress the warning using `#pragma diagnostic`.
// Taken from: https://github.com/RobotLocomotion/drake/commit/aaf84b46
// TODO(eric): This could be resolved using a function / functor (e.g. `py::self()`).
- #if (__APPLE__) && (__clang__)
- #if (__clang_major__ >= 10) && (__clang_minor__ >= 0) && (__clang_patchlevel__ >= 1)
+ #if defined(__APPLE__) && defined(__clang__)
+ #if (__clang_major__ >= 10)
#pragma GCC diagnostic ignored "-Wself-assign-overloaded"
#endif
- #elif (__clang__)
+ #elif defined(__clang__)
#if (__clang_major__ >= 7)
#pragma GCC diagnostic ignored "-Wself-assign-overloaded"
#endif
@@ -107,7 +124,13 @@ TEST_SUBMODULE(operators, m) {
.def(float() / py::self)
.def(-py::self)
.def("__str__", &Vector2::toString)
- .def(hash(py::self))
+ .def("__repr__", &Vector2::toString)
+ .def(py::self == py::self)
+ .def(py::self != py::self)
+ .def(py::hash(py::self))
+ // N.B. See warning about usage of `py::detail::abs(py::self)` in
+ // `operators.h`.
+ .def("__abs__", [](const Vector2& v) { return abs(v); })
;
m.attr("Vector") = m.attr("Vector2");
@@ -164,6 +187,38 @@ TEST_SUBMODULE(operators, m) {
.def(py::self *= int())
.def_readwrite("b", &NestC::b);
m.def("get_NestC", [](const NestC &c) { return c.value; });
+
+
+ // test_overriding_eq_reset_hash
+ // #2191 Overriding __eq__ should set __hash__ to None
+ struct Comparable {
+ int value;
+ bool operator==(const Comparable& rhs) const {return value == rhs.value;}
+ };
+
+ struct Hashable : Comparable {
+ explicit Hashable(int value): Comparable{value}{};
+ size_t hash() const { return static_cast<size_t>(value); }
+ };
+
+ struct Hashable2 : Hashable {
+ using Hashable::Hashable;
+ };
+
+ py::class_<Comparable>(m, "Comparable")
+ .def(py::init<int>())
+ .def(py::self == py::self);
+
+ py::class_<Hashable>(m, "Hashable")
+ .def(py::init<int>())
+ .def(py::self == py::self)
+ .def("__hash__", &Hashable::hash);
+
+ // define __hash__ before __eq__
+ py::class_<Hashable2>(m, "Hashable2")
+ .def("__hash__", &Hashable::hash)
+ .def(py::init<int>())
+ .def(py::self == py::self);
}
#ifndef _MSC_VER