|
|
|
|
|
|
|
|
#include <catch.hpp> |
|
|
#include <catch.hpp> |
|
|
|
|
|
|
|
|
using Handle = int; |
|
|
using Handle = int; |
|
|
|
|
|
using PtrHandle = std::add_pointer_t<Handle>; |
|
|
|
|
|
|
|
|
TEST_CASE("deleter called on destruction", "[UniqueResource]") |
|
|
TEST_CASE("deleter called on destruction", "[UniqueResource]") |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
REQUIRE(guard.get() == 3); |
|
|
REQUIRE(guard.get() == 3); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
TEST_CASE("conversion operator accesses ressource", "[UniqueResource]") |
|
|
|
|
|
|
|
|
TEST_CASE("conversion operator", "[UniqueResource]") |
|
|
{ |
|
|
{ |
|
|
auto guard = sr::unique_resource(Handle{3}, [](auto) { }); |
|
|
auto guard = sr::unique_resource(Handle{3}, [](auto) { }); |
|
|
const auto& ref = guard; |
|
|
const auto& ref = guard; |
|
|
REQUIRE(ref == 3); |
|
|
REQUIRE(ref == 3); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// TODO: conditional access functions |
|
|
|
|
|
|
|
|
TEST_CASE("pointer access operator" "[UniqueResource]") |
|
|
|
|
|
{ |
|
|
|
|
|
const auto p = std::make_pair(3, 4); |
|
|
|
|
|
auto guard = sr::unique_resource(&p, [](auto) { }); |
|
|
|
|
|
const auto x = guard.operator->(); |
|
|
|
|
|
REQUIRE(x->first == 3); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TEST_CASE("dereference operator", "[UniqueResource]") |
|
|
|
|
|
{ |
|
|
|
|
|
Handle h{4}; |
|
|
|
|
|
auto guard = sr::unique_resource(PtrHandle{&h}, [](auto) { }); |
|
|
|
|
|
const auto x = guard.operator*(); |
|
|
|
|
|
REQUIRE(x == 4); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
TEST_CASE("deleter access", "[UniqueResource]") |
|
|
TEST_CASE("deleter access", "[UniqueResource]") |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
REQUIRE(value == 0); |
|
|
REQUIRE(value == 0); |
|
|
guard.get_deleter()(6); |
|
|
guard.get_deleter()(6); |
|
|
REQUIRE(value == 6); |
|
|
REQUIRE(value == 6); |
|
|
|
|
|
|
|
|
} |
|
|
} |