Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
502ffe50
Commit
502ffe50
authored
Jun 22, 2019
by
Ian Bell
Committed by
Wenzel Jakob
Jun 22, 2019
Browse files
Add docs and tests for unary op on class (#1814)
parent
a1b71df1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
7 deletions
+12
-7
docs/advanced/classes.rst
docs/advanced/classes.rst
+1
-0
tests/test_operator_overloading.cpp
tests/test_operator_overloading.cpp
+2
-0
tests/test_operator_overloading.py
tests/test_operator_overloading.py
+9
-7
No files found.
docs/advanced/classes.rst
View file @
502ffe50
...
@@ -662,6 +662,7 @@ to Python.
...
@@ -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);
}
}
...
...
tests/test_operator_overloading.cpp
View file @
502ffe50
...
@@ -23,6 +23,7 @@ public:
...
@@ -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) {
...
@@ -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
))
;
;
...
...
tests/test_operator_overloading.py
View file @
502ffe50
...
@@ -9,6 +9,8 @@ def test_operator_overloading():
...
@@ -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():
...
@@ -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, 1
6
.000000]'
,
'[
0.125000, 0.25
0000]'
,
'[
9
.000000, 1
0
.000000]'
,
'[
8.000000, 16.00
0000]'
,
'[
7.000
000,
6.00
0000]'
,
'[
9
.000000,
10
.000000]'
,
'[
0.125
000,
0.25
0000]'
,
'[
7
.000000,
6
.000000]'
,
'[
8
.000000, 1
6
.000000]'
,
'[8.000000,
4
.000000]'
,
'[
9
.000000, 1
0
.000000]'
,
'[8.000000,
16
.000000]'
,
'[
3
.000000,
-2
.000000]'
,
'[3.000000, -
0.5
00000]'
,
'[
8
.000000,
4
.000000]'
,
'[3.000000, -
2.0
00000]'
,
'[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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment