Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

284 rindas
6.9KB

  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("release returns reference to resource", "[UniqueResource]")
  44. {
  45. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  46. const auto result = guard.release();
  47. REQUIRE(3 == result);
  48. }
  49. TEST_CASE("move releases moved-from object", "[UniqueResource]")
  50. {
  51. std::size_t calls{0};
  52. {
  53. auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  54. auto guard = std::move(movedFrom);
  55. static_cast<void>(guard);
  56. }
  57. REQUIRE(calls == 1);
  58. }
  59. TEST_CASE("move transfers state", "[UniqueResource]")
  60. {
  61. std::size_t calls{0};
  62. {
  63. auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  64. auto guard = std::move(movedFrom);
  65. static_cast<void>(guard);
  66. }
  67. REQUIRE(calls == 1);
  68. }
  69. TEST_CASE("move transfers state if released", "[UniqueResource]")
  70. {
  71. std::size_t calls{0};
  72. {
  73. auto movedFrom = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  74. movedFrom.release();
  75. auto guard = std::move(movedFrom);
  76. static_cast<void>(guard);
  77. }
  78. REQUIRE(calls == 0);
  79. }
  80. TEST_CASE("move assignment releases moved-from object", "[UniqueResource]")
  81. {
  82. std::size_t calls{0};
  83. std::function<void(Handle)> del = [&calls](auto) { ++calls; };
  84. {
  85. auto movedFrom = sr::unique_resource(Handle{3}, del);
  86. auto guard = sr::unique_resource(Handle{3}, del);
  87. guard = std::move(movedFrom);
  88. static_cast<void>(guard);
  89. }
  90. REQUIRE(calls == 2);
  91. }
  92. TEST_CASE("move assignment transfers state", "[UniqueResource]")
  93. {
  94. std::size_t calls{0};
  95. std::function<void(Handle)> del = [&calls](auto) { ++calls; };
  96. {
  97. auto movedFrom = sr::unique_resource(Handle{3}, del);
  98. auto guard = sr::unique_resource(Handle{3}, del);
  99. guard = std::move(movedFrom);
  100. static_cast<void>(guard);
  101. }
  102. REQUIRE(calls == 2);
  103. }
  104. TEST_CASE("move assignment transfers state if released", "[UniqueResource]")
  105. {
  106. std::size_t calls{0};
  107. std::function<void(Handle)> del = [&calls](auto) { ++calls; };
  108. {
  109. auto movedFrom = sr::unique_resource(Handle{3}, del);
  110. movedFrom.release();
  111. auto guard = sr::unique_resource(Handle{3}, del);
  112. guard = std::move(movedFrom);
  113. static_cast<void>(guard);
  114. }
  115. REQUIRE(calls == 1);
  116. }
  117. TEST_CASE("no exception propagation from deleter", "[UniqueResource]")
  118. {
  119. REQUIRE_NOTHROW([] {
  120. auto guard = sr::unique_resource(Handle{3}, [](auto) { throw "Don't propagate this!"; });
  121. static_cast<void>(guard);
  122. }());
  123. }
  124. TEST_CASE("invoke executes deleter on resource", "[UniqueResource]")
  125. {
  126. std::size_t calls{0};
  127. {
  128. auto guard = sr::unique_resource(Handle{3}, [&calls](auto h)
  129. {
  130. REQUIRE(h == 3);
  131. ++calls;
  132. });
  133. guard.invoke();
  134. }
  135. REQUIRE(calls == 1);
  136. }
  137. TEST_CASE("invoke executes only multiple times if again strategy", "[UniqueResource]")
  138. {
  139. std::size_t calls{0};
  140. {
  141. auto guard = sr::unique_resource(Handle{3}, [&calls](auto h) { if( h == 3 ) { ++calls;} });
  142. guard.invoke(sr::invoke_it::again);
  143. guard.invoke(sr::invoke_it::again);
  144. }
  145. REQUIRE(calls == 3);
  146. }
  147. TEST_CASE("invoke does nothing if released", "[UniqueResource]")
  148. {
  149. std::size_t calls{0};
  150. {
  151. auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  152. guard.release();
  153. guard.invoke(sr::invoke_it::once);
  154. }
  155. REQUIRE(calls == 0);
  156. }
  157. TEST_CASE("invoke executes after release if again strategy", "[UniqueResource]")
  158. {
  159. std::size_t calls{0};
  160. {
  161. auto guard = sr::unique_resource(Handle{3}, [&calls](auto) { ++calls; });
  162. guard.release();
  163. guard.invoke(sr::invoke_it::again);
  164. }
  165. REQUIRE(calls == 1);
  166. }
  167. TEST_CASE("invoke does not propagate exception", "[UniqueResource]")
  168. {
  169. auto guard = sr::unique_resource(Handle{3}, [](auto) { throw "Don't propagate this!"; });
  170. REQUIRE_NOTHROW(guard.invoke());
  171. }
  172. TEST_CASE("reset releases old ressource", "[UniqueResource]")
  173. {
  174. std::size_t calls{0};
  175. {
  176. auto d = [&calls](auto v)
  177. {
  178. if( calls == 0 )
  179. {
  180. REQUIRE(v == 3);
  181. }
  182. else
  183. {
  184. REQUIRE(v == 7);
  185. }
  186. ++calls;
  187. };
  188. auto guard = sr::unique_resource(Handle{3}, d);
  189. guard.reset(Handle{7});
  190. }
  191. REQUIRE(calls == 2);
  192. }
  193. TEST_CASE("reset sets ressource", "[UniqueResource]")
  194. {
  195. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  196. guard.reset(Handle{7});
  197. REQUIRE(guard.get() == 7);
  198. }
  199. TEST_CASE("get accesses ressource", "[UniqueResource]")
  200. {
  201. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  202. REQUIRE(guard.get() == 3);
  203. }
  204. TEST_CASE("conversion operator", "[UniqueResource]")
  205. {
  206. auto guard = sr::unique_resource(Handle{3}, [](auto) { });
  207. const auto& ref = guard;
  208. REQUIRE(ref == 3);
  209. }
  210. TEST_CASE("pointer access operator" "[UniqueResource]")
  211. {
  212. const auto p = std::make_pair(3, 4);
  213. auto guard = sr::unique_resource(&p, [](auto) { });
  214. const auto x = guard.operator->();
  215. REQUIRE(x->first == 3);
  216. }
  217. TEST_CASE("dereference operator", "[UniqueResource]")
  218. {
  219. Handle h{4};
  220. auto guard = sr::unique_resource(PtrHandle{&h}, [](auto) { });
  221. const auto x = guard.operator*();
  222. REQUIRE(x == 4);
  223. }
  224. TEST_CASE("deleter access", "[UniqueResource]")
  225. {
  226. std::size_t value{0};
  227. auto guard = sr::unique_resource(Handle{3}, [&value](auto v) { value = v; });
  228. REQUIRE(value == 0);
  229. guard.get_deleter()(6);
  230. REQUIRE(value == 6);
  231. }