-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New tests/core/smart_holder_poc_test.cpp, using Catch2.
- Loading branch information
Ralf W. Grosse-Kunstleve
committed
Jan 7, 2021
1 parent
41b87c8
commit 4b40a7c
Showing
2 changed files
with
128 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include "pybind11/smart_holder_poc.h" | ||
|
||
#define CATCH_CONFIG_MAIN | ||
#include "catch.hpp" | ||
|
||
using pybindit::memory::smart_holder; | ||
|
||
namespace helpers { | ||
|
||
template <typename T> | ||
struct functor_builtin_delete { | ||
void operator()(T* ptr) { delete ptr; } | ||
}; | ||
|
||
} // namespace helpers | ||
|
||
TEST_CASE("from_raw_ptr_take_ownership=const_value_ref") { | ||
smart_holder hld; | ||
REQUIRE(!hld.has_pointee()); | ||
hld.from_raw_ptr_take_ownership(new int(19)); | ||
REQUIRE(hld.has_pointee()); | ||
REQUIRE(hld.const_value_ref<int>() == 19); | ||
} | ||
|
||
TEST_CASE("from_raw_ptr_unowned=const_value_ref") { | ||
static int value = 19; | ||
smart_holder hld; | ||
hld.from_raw_ptr_unowned(&value); | ||
REQUIRE(hld.const_value_ref<int>() == 19); | ||
} | ||
|
||
TEST_CASE("as_raw_ptr_release_ownership") { | ||
smart_holder hld; | ||
hld.from_raw_ptr_take_ownership(new int(19)); | ||
auto new_owner = | ||
std::unique_ptr<int>(hld.as_raw_ptr_release_ownership<int>()); | ||
REQUIRE(!hld.has_pointee()); | ||
} | ||
|
||
TEST_CASE("as_raw_ptr_unowned") { | ||
smart_holder hld; | ||
hld.from_raw_ptr_take_ownership(new int(19)); | ||
int* raw_ptr = hld.as_raw_ptr_unowned<int>(); | ||
REQUIRE(hld.has_pointee()); | ||
REQUIRE(*raw_ptr == 19); | ||
} | ||
|
||
TEST_CASE("from_unique_ptr=const_value_ref") { | ||
std::unique_ptr<int> orig_owner(new int(19)); | ||
smart_holder hld; | ||
hld.from_unique_ptr(std::move(orig_owner)); | ||
REQUIRE(orig_owner.get() == nullptr); | ||
REQUIRE(hld.const_value_ref<int>() == 19); | ||
} | ||
|
||
TEST_CASE("as_unique_ptr") { | ||
smart_holder hld; | ||
hld.from_raw_ptr_take_ownership(new int(19)); | ||
auto new_owner = hld.as_unique_ptr<int>(); | ||
REQUIRE(!hld.has_pointee()); | ||
REQUIRE(*new_owner == 19); | ||
} | ||
|
||
TEST_CASE("from_unique_ptr_with_deleter=const_value_ref") { | ||
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( | ||
new int(19)); | ||
smart_holder hld; | ||
hld.from_unique_ptr_with_deleter(std::move(orig_owner)); | ||
REQUIRE(orig_owner.get() == nullptr); | ||
REQUIRE(hld.const_value_ref<int>() == 19); | ||
} | ||
|
||
TEST_CASE("as_unique_ptr_with_deleter") { | ||
std::unique_ptr<int, helpers::functor_builtin_delete<int>> orig_owner( | ||
new int(19)); | ||
smart_holder hld; | ||
hld.from_unique_ptr_with_deleter(std::move(orig_owner)); | ||
auto new_owner = | ||
hld.as_unique_ptr_with_deleter<int, | ||
helpers::functor_builtin_delete<int>>(); | ||
REQUIRE(!hld.has_pointee()); | ||
REQUIRE(*new_owner == 19); | ||
} | ||
|
||
TEST_CASE("from_shared_ptr=const_value_ref") { | ||
std::shared_ptr<int> orig_owner(new int(19)); | ||
smart_holder hld; | ||
hld.from_shared_ptr(orig_owner); | ||
REQUIRE(orig_owner.get() != nullptr); | ||
REQUIRE(hld.const_value_ref<int>() == 19); | ||
} | ||
|
||
TEST_CASE("as_shared_ptr") { | ||
smart_holder hld; | ||
hld.from_raw_ptr_take_ownership(new int(19)); | ||
auto new_owner = hld.as_shared_ptr<int>(); | ||
REQUIRE(hld.has_pointee()); | ||
REQUIRE(*new_owner == 19); | ||
} |