|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- /*
- * Scope Guard
- * Copyright (C) 2017 offa
- *
- * This file is part of Scope Guard.
- *
- * Scope Guard is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Scope Guard is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Scope Guard. If not, see <http://www.gnu.org/licenses/>.
- */
-
- #include "unique_resource.h"
- #include <functional>
- #include <catch.hpp>
-
- using Handle = int;
- using PtrHandle = std::add_pointer_t<Handle>;
-
- TEST_CASE("deleter called on destruction", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 1);
- }
-
- TEST_CASE("deleter is not called if released", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
- guard.release();
- }
-
- REQUIRE(calls == 0);
- }
-
- TEST_CASE("deleter not called if checked valid", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource_checked(Handle{3}, Handle{6}, [&calls](auto) { ++calls; });
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 1);
- }
-
- TEST_CASE("deleter not called if checked invalid", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource_checked(Handle{3}, Handle{3}, [&calls](auto) { ++calls; });
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 0);
- }
-
- TEST_CASE("release returns reference to resource", "[UniqueResource]")
- {
- auto guard = sr::unique_resource(Handle{3}, [](auto) { });
- const auto result = guard.release();
-
- REQUIRE(3 == result);
- }
-
- TEST_CASE("move releases moved-from object", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
- auto guard = std::move(movedFrom);
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 1);
- }
-
- TEST_CASE("move transfers state", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
- auto guard = std::move(movedFrom);
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 1);
- }
-
- TEST_CASE("move transfers state if released", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
- movedFrom.release();
- auto guard = std::move(movedFrom);
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 0);
- }
-
- TEST_CASE("move assignment releases moved-from object", "[UniqueResource]")
- {
- std::size_t calls{0};
- std::function<void(Handle)> del = [&calls](auto) { ++calls; };
-
- {
- auto movedFrom = sr::unique_resource(Handle{3}, del);
- auto guard = sr::unique_resource(Handle{3}, del);
- guard = std::move(movedFrom);
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 2);
- }
-
- TEST_CASE("move assignment transfers state", "[UniqueResource]")
- {
- std::size_t calls{0};
- std::function<void(Handle)> del = [&calls](auto) { ++calls; };
-
- {
- auto movedFrom = sr::unique_resource(Handle{3}, del);
- auto guard = sr::unique_resource(Handle{3}, del);
- guard = std::move(movedFrom);
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 2);
- }
-
- TEST_CASE("move assignment transfers state if released", "[UniqueResource]")
- {
- std::size_t calls{0};
- std::function<void(Handle)> del = [&calls](auto) { ++calls; };
-
- {
- auto movedFrom = sr::unique_resource(Handle{3}, del);
- movedFrom.release();
- auto guard = sr::unique_resource(Handle{3}, del);
- guard = std::move(movedFrom);
- static_cast<void>(guard);
- }
-
- REQUIRE(calls == 1);
- }
-
- TEST_CASE("no exception propagation from deleter", "[UniqueResource]")
- {
- REQUIRE_NOTHROW([] {
- auto guard = sr::unique_resource(Handle{3}, [](auto) { throw "Don't propagate this!"; });
- static_cast<void>(guard);
- }());
- }
-
- TEST_CASE("invoke executes deleter on resource", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource(Handle{3}, [&calls](auto h)
- {
- REQUIRE(h == 3);
- ++calls;
- });
- guard.invoke();
- }
-
- REQUIRE(calls == 1);
- }
-
- TEST_CASE("invoke executes only multiple times if again strategy", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource(Handle{3}, [&calls](auto h) { if( h == 3 ) { ++calls;} });
- guard.invoke(sr::invoke_it::again);
- guard.invoke(sr::invoke_it::again);
- }
-
- REQUIRE(calls == 3);
- }
-
- TEST_CASE("invoke does nothing if released", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
- guard.release();
- guard.invoke(sr::invoke_it::once);
- }
-
- REQUIRE(calls == 0);
- }
-
- TEST_CASE("invoke executes after release if again strategy", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
- guard.release();
- guard.invoke(sr::invoke_it::again);
- }
-
- REQUIRE(calls == 1);
- }
-
- TEST_CASE("invoke does not propagate exception", "[UniqueResource]")
- {
- auto guard = sr::unique_resource(Handle{3}, [](auto) { throw "Don't propagate this!"; });
- REQUIRE_NOTHROW(guard.invoke());
- }
-
- TEST_CASE("reset releases old ressource", "[UniqueResource]")
- {
- std::size_t calls{0};
-
- {
- auto d = [&calls](auto v)
- {
- if( calls == 0 )
- {
- REQUIRE(v == 3);
- }
- else
- {
- REQUIRE(v == 7);
- }
- ++calls;
- };
-
- auto guard = sr::unique_resource(Handle{3}, d);
- guard.reset(Handle{7});
- }
-
- REQUIRE(calls == 2);
- }
-
- TEST_CASE("reset sets ressource", "[UniqueResource]")
- {
- auto guard = sr::unique_resource(Handle{3}, [](auto) { });
- guard.reset(Handle{7});
- REQUIRE(guard.get() == 7);
- }
-
- TEST_CASE("get accesses ressource", "[UniqueResource]")
- {
- auto guard = sr::unique_resource(Handle{3}, [](auto) { });
- REQUIRE(guard.get() == 3);
- }
-
- TEST_CASE("conversion operator", "[UniqueResource]")
- {
- auto guard = sr::unique_resource(Handle{3}, [](auto) { });
- const auto& ref = guard;
- REQUIRE(ref == 3);
- }
-
- 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]")
- {
- std::size_t value{0};
- auto guard = sr::unique_resource(Handle{3}, [&value](auto v) { value = v; });
- REQUIRE(value == 0);
- guard.get_deleter()(6);
- REQUIRE(value == 6);
- }
-
|