No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

76 líneas
1.5KB

  1. #include "scope_guard.h"
  2. #include <catch.hpp>
  3. TEST_CASE("deleter called on destruction", "[ScopeGuard]")
  4. {
  5. std::size_t calls{0};
  6. {
  7. auto guard = sr::scope_guard([&calls] { ++calls; });
  8. static_cast<void>(guard);
  9. }
  10. REQUIRE(calls == 1);
  11. }
  12. TEST_CASE("deleter is not called if released", "[ScopeGuard]")
  13. {
  14. std::size_t calls{0};
  15. {
  16. auto guard = sr::scope_guard([&calls] { ++calls; });
  17. guard.release();
  18. }
  19. REQUIRE(calls == 0);
  20. }
  21. TEST_CASE("move releases moved-from object", "[ScopeGuard]")
  22. {
  23. std::size_t calls{0};
  24. {
  25. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  26. auto guard = std::move(movedFrom);
  27. static_cast<void>(guard);
  28. }
  29. REQUIRE(calls == 1);
  30. }
  31. TEST_CASE("move transfers state", "[ScopeGuard]")
  32. {
  33. std::size_t calls{0};
  34. {
  35. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  36. auto guard = std::move(movedFrom);
  37. static_cast<void>(guard);
  38. }
  39. REQUIRE(calls == 1);
  40. }
  41. TEST_CASE("move transfers state if released", "[ScopeGuard]")
  42. {
  43. std::size_t calls{0};
  44. {
  45. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  46. movedFrom.release();
  47. auto guard = std::move(movedFrom);
  48. static_cast<void>(guard);
  49. }
  50. REQUIRE(calls == 0);
  51. }
  52. TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")
  53. {
  54. REQUIRE_NOTHROW([] {
  55. auto guard = sr::scope_guard([] { throw "Don't propagate this!"; });
  56. static_cast<void>(guard);
  57. }());
  58. }