Add docs and tests for unary op on class (#1814)

This commit is contained in:
Ian Bell 2019-06-22 04:07:41 -06:00 committed by Wenzel Jakob
parent a1b71df137
commit 502ffe50a9
3 changed files with 12 additions and 7 deletions

View File

@ -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);
} }

View File

@ -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))
; ;

View File

@ -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