test_operator_overloading.py revision 11986
1 2def test_operator_overloading(): 3 from pybind11_tests import Vector2, Vector, ConstructorStats 4 5 v1 = Vector2(1, 2) 6 v2 = Vector(3, -1) 7 assert str(v1) == "[1.000000, 2.000000]" 8 assert str(v2) == "[3.000000, -1.000000]" 9 10 assert str(v1 + v2) == "[4.000000, 1.000000]" 11 assert str(v1 - v2) == "[-2.000000, 3.000000]" 12 assert str(v1 - 8) == "[-7.000000, -6.000000]" 13 assert str(v1 + 8) == "[9.000000, 10.000000]" 14 assert str(v1 * 8) == "[8.000000, 16.000000]" 15 assert str(v1 / 8) == "[0.125000, 0.250000]" 16 assert str(8 - v1) == "[7.000000, 6.000000]" 17 assert str(8 + v1) == "[9.000000, 10.000000]" 18 assert str(8 * v1) == "[8.000000, 16.000000]" 19 assert str(8 / v1) == "[8.000000, 4.000000]" 20 21 v1 += v2 22 v1 *= 2 23 assert str(v1) == "[8.000000, 2.000000]" 24 25 cstats = ConstructorStats.get(Vector2) 26 assert cstats.alive() == 2 27 del v1 28 assert cstats.alive() == 1 29 del v2 30 assert cstats.alive() == 0 31 assert cstats.values() == ['[1.000000, 2.000000]', '[3.000000, -1.000000]', 32 '[4.000000, 1.000000]', '[-2.000000, 3.000000]', 33 '[-7.000000, -6.000000]', '[9.000000, 10.000000]', 34 '[8.000000, 16.000000]', '[0.125000, 0.250000]', 35 '[7.000000, 6.000000]', '[9.000000, 10.000000]', 36 '[8.000000, 16.000000]', '[8.000000, 4.000000]'] 37 assert cstats.default_constructions == 0 38 assert cstats.copy_constructions == 0 39 assert cstats.move_constructions >= 10 40 assert cstats.copy_assignments == 0 41 assert cstats.move_assignments == 0 42