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
44805c16
Commit
44805c16
authored
Nov 25, 2020
by
Ralf W. Grosse-Kunstleve
Browse files
adding test_holder_{shared,unique}_ptr.{cpp,py}
parent
d96d16aa
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
208 additions
and
0 deletions
+208
-0
tests/test_holder_shared_ptr.cpp
tests/test_holder_shared_ptr.cpp
+61
-0
tests/test_holder_shared_ptr.py
tests/test_holder_shared_ptr.py
+43
-0
tests/test_holder_unique_ptr.cpp
tests/test_holder_unique_ptr.cpp
+61
-0
tests/test_holder_unique_ptr.py
tests/test_holder_unique_ptr.py
+43
-0
No files found.
tests/test_holder_shared_ptr.cpp
0 → 100644
View file @
44805c16
// KEEP IN SYNC WITH test_holder_unique_ptr.cpp
#include "pybind11_tests.h"
#include <iostream>
#include <memory>
namespace
pybind11_tests
{
namespace
holder_shared_ptr
{
inline
void
to_cout
(
std
::
string
text
)
{
std
::
cout
<<
text
<<
std
::
endl
;
}
class
pointee
{
// NOT copyable.
public:
pointee
()
{
to_cout
(
"pointee::pointee()"
);
}
int
get_int
()
const
{
to_cout
(
"pointee::get_int()"
);
return
213
;
}
~
pointee
()
{
to_cout
(
"~pointee()"
);
}
private:
pointee
(
const
pointee
&
)
=
delete
;
pointee
(
pointee
&&
)
=
delete
;
pointee
&
operator
=
(
const
pointee
&
)
=
delete
;
pointee
&
operator
=
(
pointee
&&
)
=
delete
;
};
inline
std
::
unique_ptr
<
pointee
>
make_unique_pointee
()
{
return
std
::
unique_ptr
<
pointee
>
(
new
pointee
);
}
inline
std
::
shared_ptr
<
pointee
>
make_shared_pointee
()
{
return
std
::
unique_ptr
<
pointee
>
(
new
pointee
);
}
inline
int
pass_unique_pointee
(
std
::
unique_ptr
<
pointee
>
ptr
)
{
return
4000
+
ptr
->
get_int
();
}
inline
int
pass_shared_pointee
(
std
::
shared_ptr
<
pointee
>
ptr
)
{
return
5000
+
ptr
->
get_int
();
}
TEST_SUBMODULE
(
holder_shared_ptr
,
m
)
{
m
.
def
(
"to_cout"
,
to_cout
);
py
::
class_
<
pointee
,
std
::
shared_ptr
<
pointee
>>
(
m
,
"pointee"
)
.
def
(
py
::
init
<>
())
.
def
(
"get_int"
,
&
pointee
::
get_int
);
m
.
def
(
"make_unique_pointee"
,
make_unique_pointee
);
m
.
def
(
"make_shared_pointee"
,
make_shared_pointee
);
// m.def("pass_unique_pointee", pass_unique_pointee);
m
.
def
(
"pass_shared_pointee"
,
pass_shared_pointee
);
}
}
// namespace holder_shared_ptr
}
// namespace pybind11_tests
tests/test_holder_shared_ptr.py
0 → 100644
View file @
44805c16
# KEEP IN SYNC WITH test_holder_unique_ptr.py
# -*- coding: utf-8 -*-
from
pybind11_tests
import
holder_shared_ptr
as
m
def
test_make_unique_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"make_unique_pointee"
)
obj
=
m
.
make_unique_pointee
()
assert
obj
.
get_int
()
==
213
m
.
to_cout
(
""
)
def
test_make_shared_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"make_shared_pointee"
)
obj
=
m
.
make_shared_pointee
()
assert
obj
.
get_int
()
==
213
m
.
to_cout
(
""
)
def
test_pass_unique_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"pass_unique_pointee"
)
obj
=
m
.
make_shared_pointee
()
assert
obj
.
get_int
()
==
213
i
=
m
.
pass_unique_pointee
(
obj
)
assert
i
==
4213
m
.
to_cout
(
""
)
def
test_pass_shared_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"pass_shared_pointee"
)
obj
=
m
.
make_shared_pointee
()
assert
obj
.
get_int
()
==
213
i
=
m
.
pass_shared_pointee
(
obj
)
assert
i
==
5213
m
.
to_cout
(
""
)
tests/test_holder_unique_ptr.cpp
0 → 100644
View file @
44805c16
// KEEP IN SYNC WITH test_holder_shared_ptr.cpp
#include "pybind11_tests.h"
#include <iostream>
#include <memory>
namespace
pybind11_tests
{
namespace
holder_unique_ptr
{
inline
void
to_cout
(
std
::
string
text
)
{
std
::
cout
<<
text
<<
std
::
endl
;
}
class
pointee
{
// NOT copyable.
public:
pointee
()
{
to_cout
(
"pointee::pointee()"
);
}
int
get_int
()
const
{
to_cout
(
"pointee::get_int()"
);
return
213
;
}
~
pointee
()
{
to_cout
(
"~pointee()"
);
}
private:
pointee
(
const
pointee
&
)
=
delete
;
pointee
(
pointee
&&
)
=
delete
;
pointee
&
operator
=
(
const
pointee
&
)
=
delete
;
pointee
&
operator
=
(
pointee
&&
)
=
delete
;
};
inline
std
::
unique_ptr
<
pointee
>
make_unique_pointee
()
{
return
std
::
unique_ptr
<
pointee
>
(
new
pointee
);
}
inline
std
::
shared_ptr
<
pointee
>
make_shared_pointee
()
{
return
std
::
unique_ptr
<
pointee
>
(
new
pointee
);
}
inline
int
pass_unique_pointee
(
std
::
unique_ptr
<
pointee
>
ptr
)
{
return
4000
+
ptr
->
get_int
();
}
inline
int
pass_shared_pointee
(
std
::
shared_ptr
<
pointee
>
ptr
)
{
return
5000
+
ptr
->
get_int
();
}
TEST_SUBMODULE
(
holder_unique_ptr
,
m
)
{
m
.
def
(
"to_cout"
,
to_cout
);
py
::
class_
<
pointee
>
(
m
,
"pointee"
)
.
def
(
py
::
init
<>
())
.
def
(
"get_int"
,
&
pointee
::
get_int
);
m
.
def
(
"make_unique_pointee"
,
make_unique_pointee
);
m
.
def
(
"make_shared_pointee"
,
make_shared_pointee
);
// m.def("pass_unique_pointee", pass_unique_pointee);
m
.
def
(
"pass_shared_pointee"
,
pass_shared_pointee
);
}
}
// namespace holder_unique_ptr
}
// namespace pybind11_tests
tests/test_holder_unique_ptr.py
0 → 100644
View file @
44805c16
# KEEP IN SYNC WITH test_holder_shared_ptr.py
# -*- coding: utf-8 -*-
from
pybind11_tests
import
holder_unique_ptr
as
m
def
test_make_unique_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"make_unique_pointee"
)
obj
=
m
.
make_unique_pointee
()
assert
obj
.
get_int
()
==
213
m
.
to_cout
(
""
)
def
test_make_shared_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"make_shared_pointee"
)
obj
=
m
.
make_shared_pointee
()
assert
obj
.
get_int
()
==
213
m
.
to_cout
(
""
)
def
test_pass_unique_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"pass_unique_pointee"
)
obj
=
m
.
make_unique_pointee
()
assert
obj
.
get_int
()
==
213
i
=
m
.
pass_unique_pointee
(
obj
)
assert
i
==
4213
m
.
to_cout
(
""
)
def
test_pass_shared_pointee
():
m
.
to_cout
(
""
)
m
.
to_cout
(
""
)
m
.
to_cout
(
"pass_shared_pointee"
)
obj
=
m
.
make_unique_pointee
()
assert
obj
.
get_int
()
==
213
i
=
m
.
pass_shared_pointee
(
obj
)
assert
i
==
5213
m
.
to_cout
(
""
)
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