您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

74 行
1.5KB

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