Commit 502ffe50 by Ian Bell Committed by Wenzel Jakob

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

parent a1b71df1
......@@ -662,6 +662,7 @@ to Python.
.def(py::self *= float())
.def(float() * py::self)
.def(py::self * float())
.def(-py::self)
.def("__repr__", &Vector2::toString);
}
......
......@@ -23,6 +23,7 @@ public:
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-(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(-py::self)
.def("__str__", &Vector2::toString)
.def(hash(py::self))
;
......
......@@ -9,6 +9,8 @@ def test_operator_overloading():
assert str(v1) == "[1.000000, 2.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) == "[-2.000000, 3.000000]"
assert str(v1 - 8) == "[-7.000000, -6.000000]"
......@@ -44,13 +46,13 @@ def test_operator_overloading():
del v2
assert cstats.alive() == 0
assert cstats.values() == ['[1.000000, 2.000000]', '[3.000000, -1.000000]',
'[4.000000, 1.000000]', '[-2.000000, 3.000000]',
'[-7.000000, -6.000000]', '[9.000000, 10.000000]',
'[8.000000, 16.000000]', '[0.125000, 0.250000]',
'[7.000000, 6.000000]', '[9.000000, 10.000000]',
'[8.000000, 16.000000]', '[8.000000, 4.000000]',
'[3.000000, -2.000000]', '[3.000000, -0.500000]',
'[6.000000, -2.000000]']
'[-3.000000, 1.000000]', '[4.000000, 1.000000]',
'[-2.000000, 3.000000]', '[-7.000000, -6.000000]',
'[9.000000, 10.000000]', '[8.000000, 16.000000]',
'[0.125000, 0.250000]', '[7.000000, 6.000000]',
'[9.000000, 10.000000]', '[8.000000, 16.000000]',
'[8.000000, 4.000000]', '[3.000000, -2.000000]',
'[3.000000, -0.500000]', '[6.000000, -2.000000]']
assert cstats.default_constructions == 0
assert cstats.copy_constructions == 0
assert cstats.move_constructions >= 10
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment