Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ScopeGuardTest.cpp 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 "scope_guard.h"
  21. #include <catch.hpp>
  22. TEST_CASE("deleter called on destruction", "[ScopeGuard]")
  23. {
  24. std::size_t calls{0};
  25. {
  26. auto guard = sr::scope_guard([&calls] { ++calls; });
  27. static_cast<void>(guard);
  28. }
  29. REQUIRE(calls == 1);
  30. }
  31. TEST_CASE("deleter is not called if released", "[ScopeGuard]")
  32. {
  33. std::size_t calls{0};
  34. {
  35. auto guard = sr::scope_guard([&calls] { ++calls; });
  36. guard.release();
  37. }
  38. REQUIRE(calls == 0);
  39. }
  40. TEST_CASE("move releases moved-from object", "[ScopeGuard]")
  41. {
  42. std::size_t calls{0};
  43. {
  44. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  45. auto guard = std::move(movedFrom);
  46. static_cast<void>(guard);
  47. }
  48. REQUIRE(calls == 1);
  49. }
  50. TEST_CASE("move transfers state", "[ScopeGuard]")
  51. {
  52. std::size_t calls{0};
  53. {
  54. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  55. auto guard = std::move(movedFrom);
  56. static_cast<void>(guard);
  57. }
  58. REQUIRE(calls == 1);
  59. }
  60. TEST_CASE("move transfers state if released", "[ScopeGuard]")
  61. {
  62. std::size_t calls{0};
  63. {
  64. auto movedFrom = sr::scope_guard([&calls] { ++calls; });
  65. movedFrom.release();
  66. auto guard = std::move(movedFrom);
  67. static_cast<void>(guard);
  68. }
  69. REQUIRE(calls == 0);
  70. }
  71. TEST_CASE("no exception propagation from deleter", "[ScopeGuard]")
  72. {
  73. REQUIRE_NOTHROW([] {
  74. auto guard = sr::scope_guard([] { throw "Don't propagate this!"; });
  75. static_cast<void>(guard);
  76. }());
  77. }