Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

UniqueResourceTest.cpp 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Scope Guard
  3. * Copyright (C) 2017 offa
  4. *
  5. * This file is part of Scope Guard.
  6. *
  7. * Scope Guard is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Scope Guard is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Scope Guard. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include "unique_resource.h"
  21. #include <functional>
  22. #include <catch.hpp>
  23. using Handle = int;
  24. using PtrHandle = std::add_pointer_t<Handle>;
  25. TEST_CASE("deleter called on destruction", "[UniqueResource]")
  26. {
  27. std::size_t calls{0};
  28. {
  29. auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  30. static_cast<void>(guard);
  31. }
  32. REQUIRE(calls == 1);
  33. }
  34. TEST_CASE("deleter is not called if released", "[UniqueResource]")
  35. {
  36. std::size_t calls{0};
  37. {
  38. auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  39. guard.release();
  40. }
  41. REQUIRE(calls == 0);
  42. }
  43. TEST_CASE("deleter not called if checked valid", "[UniqueResource]")
  44. {
  45. std::size_t calls{0};
  46. {
  47. auto guard = sr::unique_resource_checked(Handle{3}, Handle{6}, [&calls](auto) { ++calls; });
  48. static_cast<void>(guard);
  49. }
  50. REQUIRE(calls == 1);
  51. }
  52. TEST_CASE("deleter not called if checked invalid", "[UniqueResource]")
  53. {
  54. std::size_t calls{0};
  55. {
  56. auto guard = sr::unique_resource_checked(Handle{3}, Handle{3}, [&calls](auto) { ++calls; });
  57. static_cast<void>(guard);
  58. }
  59. REQUIRE(calls == 0);
  60. }
  61. TEST_CASE("release returns reference to resource", "[UniqueResource]")
  62. {
  63. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  64. const auto result = guard.release();
  65. REQUIRE(3 == result);
  66. }
  67. TEST_CASE("move releases moved-from object", "[UniqueResource]")
  68. {
  69. std::size_t calls{0};
  70. {
  71. auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  72. auto guard = std::move(movedFrom);
  73. static_cast<void>(guard);
  74. }
  75. REQUIRE(calls == 1);
  76. }
  77. TEST_CASE("move transfers state", "[UniqueResource]")
  78. {
  79. std::size_t calls{0};
  80. {
  81. auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  82. auto guard = std::move(movedFrom);
  83. static_cast<void>(guard);
  84. }
  85. REQUIRE(calls == 1);
  86. }
  87. TEST_CASE("move transfers state if released", "[UniqueResource]")
  88. {
  89. std::size_t calls{0};
  90. {
  91. auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  92. movedFrom.release();
  93. auto guard = std::move(movedFrom);
  94. static_cast<void>(guard);
  95. }
  96. REQUIRE(calls == 0);
  97. }
  98. TEST_CASE("move assignment releases moved-from object", "[UniqueResource]")
  99. {
  100. std::size_t calls{0};
  101. std::function<void(Handle)> del = [&calls](auto) { ++calls; };
  102. {
  103. auto movedFrom = sr::unique_resource(Handle{3}, del);
  104. auto guard = sr::unique_resource(Handle{3}, del);
  105. guard = std::move(movedFrom);
  106. static_cast<void>(guard);
  107. }
  108. REQUIRE(calls == 2);
  109. }
  110. TEST_CASE("move assignment transfers state", "[UniqueResource]")
  111. {
  112. std::size_t calls{0};
  113. std::function<void(Handle)> del = [&calls](auto) { ++calls; };
  114. {
  115. auto movedFrom = sr::unique_resource(Handle{3}, del);
  116. auto guard = sr::unique_resource(Handle{3}, del);
  117. guard = std::move(movedFrom);
  118. static_cast<void>(guard);
  119. }
  120. REQUIRE(calls == 2);
  121. }
  122. TEST_CASE("move assignment transfers state if released", "[UniqueResource]")
  123. {
  124. std::size_t calls{0};
  125. std::function<void(Handle)> del = [&calls](auto) { ++calls; };
  126. {
  127. auto movedFrom = sr::unique_resource(Handle{3}, del);
  128. movedFrom.release();
  129. auto guard = sr::unique_resource(Handle{3}, del);
  130. guard = std::move(movedFrom);
  131. static_cast<void>(guard);
  132. }
  133. REQUIRE(calls == 1);
  134. }
  135. TEST_CASE("no exception propagation from deleter", "[UniqueResource]")
  136. {
  137. REQUIRE_NOTHROW([] {
  138. auto guard = sr::unique_resource(Handle{3}, [](auto) { throw "Don't propagate this!"; });
  139. static_cast<void>(guard);
  140. }());
  141. }
  142. TEST_CASE("invoke executes deleter on resource", "[UniqueResource]")
  143. {
  144. std::size_t calls{0};
  145. {
  146. auto guard = sr::unique_resource(Handle{3}, [&calls](auto h)
  147. {
  148. REQUIRE(h == 3);
  149. ++calls;
  150. });
  151. guard.invoke();
  152. }
  153. REQUIRE(calls == 1);
  154. }
  155. TEST_CASE("invoke executes only multiple times if again strategy", "[UniqueResource]")
  156. {
  157. std::size_t calls{0};
  158. {
  159. auto guard = sr::unique_resource(Handle{3}, [&calls](auto h) { if( h == 3 ) { ++calls;} });
  160. guard.invoke(sr::invoke_it::again);
  161. guard.invoke(sr::invoke_it::again);
  162. }
  163. REQUIRE(calls == 3);
  164. }
  165. TEST_CASE("invoke does nothing if released", "[UniqueResource]")
  166. {
  167. std::size_t calls{0};
  168. {
  169. auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  170. guard.release();
  171. guard.invoke(sr::invoke_it::once);
  172. }
  173. REQUIRE(calls == 0);
  174. }
  175. TEST_CASE("invoke executes after release if again strategy", "[UniqueResource]")
  176. {
  177. std::size_t calls{0};
  178. {
  179. auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  180. guard.release();
  181. guard.invoke(sr::invoke_it::again);
  182. }
  183. REQUIRE(calls == 1);
  184. }
  185. TEST_CASE("invoke does not propagate exception", "[UniqueResource]")
  186. {
  187. auto guard = sr::unique_resource(Handle{3}, [](auto) { throw "Don't propagate this!"; });
  188. REQUIRE_NOTHROW(guard.invoke());
  189. }
  190. TEST_CASE("reset releases old ressource", "[UniqueResource]")
  191. {
  192. std::size_t calls{0};
  193. {
  194. auto d = [&calls](auto v)
  195. {
  196. if( calls == 0 )
  197. {
  198. REQUIRE(v == 3);
  199. }
  200. else
  201. {
  202. REQUIRE(v == 7);
  203. }
  204. ++calls;
  205. };
  206. auto guard = sr::unique_resource(Handle{3}, d);
  207. guard.reset(Handle{7});
  208. }
  209. REQUIRE(calls == 2);
  210. }
  211. TEST_CASE("reset sets ressource", "[UniqueResource]")
  212. {
  213. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  214. guard.reset(Handle{7});
  215. REQUIRE(guard.get() == 7);
  216. }
  217. TEST_CASE("get accesses ressource", "[UniqueResource]")
  218. {
  219. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  220. REQUIRE(guard.get() == 3);
  221. }
  222. TEST_CASE("conversion operator", "[UniqueResource]")
  223. {
  224. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  225. const auto& ref = guard;
  226. REQUIRE(ref == 3);
  227. }
  228. TEST_CASE("pointer access operator" "[UniqueResource]")
  229. {
  230. const auto p = std::make_pair(3, 4);
  231. auto guard = sr::unique_resource(&p, [](auto) { });
  232. const auto x = guard.operator->();
  233. REQUIRE(x->first == 3);
  234. }
  235. TEST_CASE("dereference operator", "[UniqueResource]")
  236. {
  237. Handle h{4};
  238. auto guard = sr::unique_resource(PtrHandle{&h}, [](auto) { });
  239. const auto x = guard.operator*();
  240. REQUIRE(x == 4);
  241. }
  242. TEST_CASE("deleter access", "[UniqueResource]")
  243. {
  244. std::size_t value{0};
  245. auto guard = sr::unique_resource(Handle{3}, [&value](auto v) { value = v; });
  246. REQUIRE(value == 0);
  247. guard.get_deleter()(6);
  248. REQUIRE(value == 6);
  249. }