mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-28 16:11:59 +00:00
Add docs and tests for unary op on class (#1814)
This commit is contained in:
parent
a1b71df137
commit
502ffe50a9
@ -662,6 +662,7 @@ to Python.
|
|||||||
.def(py::self *= float())
|
.def(py::self *= float())
|
||||||
.def(float() * py::self)
|
.def(float() * py::self)
|
||||||
.def(py::self * float())
|
.def(py::self * float())
|
||||||
|
.def(-py::self)
|
||||||
.def("__repr__", &Vector2::toString);
|
.def("__repr__", &Vector2::toString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,7 @@ public:
|
|||||||
|
|
||||||
std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
|
std::string toString() const { return "[" + std::to_string(x) + ", " + std::to_string(y) + "]"; }
|
||||||
|
|
||||||
|
Vector2 operator-() const { return Vector2(-x, -y); }
|
||||||
Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
|
Vector2 operator+(const Vector2 &v) const { return Vector2(x + v.x, y + v.y); }
|
||||||
Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
|
Vector2 operator-(const Vector2 &v) const { return Vector2(x - v.x, y - v.y); }
|
||||||
Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
|
Vector2 operator-(float value) const { return Vector2(x - value, y - value); }
|
||||||
@ -104,6 +105,7 @@ TEST_SUBMODULE(operators, m) {
|
|||||||
.def(float() - py::self)
|
.def(float() - py::self)
|
||||||
.def(float() * py::self)
|
.def(float() * py::self)
|
||||||
.def(float() / py::self)
|
.def(float() / py::self)
|
||||||
|
.def(-py::self)
|
||||||
.def("__str__", &Vector2::toString)
|
.def("__str__", &Vector2::toString)
|
||||||
.def(hash(py::self))
|
.def(hash(py::self))
|
||||||
;
|
;
|
||||||
|
@ -9,6 +9,8 @@ def test_operator_overloading():
|
|||||||
assert str(v1) == "[1.000000, 2.000000]"
|
assert str(v1) == "[1.000000, 2.000000]"
|
||||||
assert str(v2) == "[3.000000, -1.000000]"
|
assert str(v2) == "[3.000000, -1.000000]"
|
||||||
|
|
||||||
|
assert str(-v2) == "[-3.000000, 1.000000]"
|
||||||
|
|
||||||
assert str(v1 + v2) == "[4.000000, 1.000000]"
|
assert str(v1 + v2) == "[4.000000, 1.000000]"
|
||||||
assert str(v1 - v2) == "[-2.000000, 3.000000]"
|
assert str(v1 - v2) == "[-2.000000, 3.000000]"
|
||||||
assert str(v1 - 8) == "[-7.000000, -6.000000]"
|
assert str(v1 - 8) == "[-7.000000, -6.000000]"
|
||||||
@ -44,13 +46,13 @@ def test_operator_overloading():
|
|||||||
del v2
|
del v2
|
||||||
assert cstats.alive() == 0
|
assert cstats.alive() == 0
|
||||||
assert cstats.values() == ['[1.000000, 2.000000]', '[3.000000, -1.000000]',
|
assert cstats.values() == ['[1.000000, 2.000000]', '[3.000000, -1.000000]',
|
||||||
'[4.000000, 1.000000]', '[-2.000000, 3.000000]',
|
'[-3.000000, 1.000000]', '[4.000000, 1.000000]',
|
||||||
'[-7.000000, -6.000000]', '[9.000000, 10.000000]',
|
'[-2.000000, 3.000000]', '[-7.000000, -6.000000]',
|
||||||
'[8.000000, 16.000000]', '[0.125000, 0.250000]',
|
'[9.000000, 10.000000]', '[8.000000, 16.000000]',
|
||||||
'[7.000000, 6.000000]', '[9.000000, 10.000000]',
|
'[0.125000, 0.250000]', '[7.000000, 6.000000]',
|
||||||
'[8.000000, 16.000000]', '[8.000000, 4.000000]',
|
'[9.000000, 10.000000]', '[8.000000, 16.000000]',
|
||||||
'[3.000000, -2.000000]', '[3.000000, -0.500000]',
|
'[8.000000, 4.000000]', '[3.000000, -2.000000]',
|
||||||
'[6.000000, -2.000000]']
|
'[3.000000, -0.500000]', '[6.000000, -2.000000]']
|
||||||
assert cstats.default_constructions == 0
|
assert cstats.default_constructions == 0
|
||||||
assert cstats.copy_constructions == 0
|
assert cstats.copy_constructions == 0
|
||||||
assert cstats.move_constructions >= 10
|
assert cstats.move_constructions >= 10
|
||||||
|
Loading…
Reference in New Issue
Block a user