Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

UniqueResourceTest.cpp 2.8KB

7 лет назад
7 лет назад
7 лет назад
7 лет назад
7 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <catch.hpp>
  22. using Handle = int;
  23. TEST_CASE("deleter called on destruction", "[UniqueResource]")
  24. {
  25. std::size_t calls{0};
  26. constexpr Handle handle{3};
  27. {
  28. auto guard = sr::unique_resource(handle, [&calls] { ++calls; });
  29. static_cast<void>(guard);
  30. }
  31. REQUIRE(calls == 1);
  32. }
  33. TEST_CASE("deleter is not called if released", "[UniqueResource]")
  34. {
  35. std::size_t calls{0};
  36. constexpr Handle handle{3};
  37. {
  38. auto guard = sr::unique_resource(handle, [&calls] { ++calls; });
  39. guard.release();
  40. }
  41. REQUIRE(calls == 0);
  42. }
  43. TEST_CASE("release returns reference to resource", "[UniqueResource]")
  44. {
  45. constexpr Handle handle{3};
  46. auto guard = sr::unique_resource(handle, [] { });
  47. const auto result = guard.release();
  48. REQUIRE(handle == result);
  49. }
  50. TEST_CASE("move releases moved-from object", "[UniqueResource]")
  51. {
  52. std::size_t calls{0};
  53. constexpr Handle handle{3};
  54. {
  55. auto movedFrom = sr::unique_resource(handle, [&calls] { ++calls; });
  56. auto guard = std::move(movedFrom);
  57. static_cast<void>(guard);
  58. }
  59. REQUIRE(calls == 1);
  60. }
  61. TEST_CASE("move transfers state", "[UniqueResource]")
  62. {
  63. std::size_t calls{0};
  64. constexpr Handle handle{3};
  65. {
  66. auto movedFrom = sr::unique_resource(handle, [&calls] { ++calls; });
  67. auto guard = std::move(movedFrom);
  68. static_cast<void>(guard);
  69. }
  70. REQUIRE(calls == 1);
  71. }
  72. TEST_CASE("move transfers state if released", "[UniqueResource]")
  73. {
  74. std::size_t calls{0};
  75. constexpr Handle handle{3};
  76. {
  77. auto movedFrom = sr::unique_resource(handle, [&calls] { ++calls; });
  78. movedFrom.release();
  79. auto guard = std::move(movedFrom);
  80. static_cast<void>(guard);
  81. }
  82. REQUIRE(calls == 0);
  83. }
  84. TEST_CASE("no exception propagation from deleter", "[UniqueResource]")
  85. {
  86. constexpr Handle handle{3};
  87. REQUIRE_NOTHROW([] {
  88. auto guard = sr::unique_resource(handle, [] { throw "Don't propagate this!"; });
  89. static_cast<void>(guard);
  90. }());
  91. }