scope_ut.cpp 882 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "scope.h"
  2. #include <util/generic/ptr.h>
  3. #include <library/cpp/testing/unittest/registar.h>
  4. Y_UNIT_TEST_SUITE(ScopeToolsTest) {
  5. Y_UNIT_TEST(OnScopeExitTest) {
  6. int i = 0;
  7. {
  8. Y_SCOPE_EXIT(&i) {
  9. i = i * 2;
  10. };
  11. Y_SCOPE_EXIT(&i) {
  12. i = i + 1;
  13. };
  14. }
  15. UNIT_ASSERT_VALUES_EQUAL(2, i);
  16. }
  17. Y_UNIT_TEST(OnScopeExitMoveTest) {
  18. THolder<int> i{new int{10}};
  19. int p = 0;
  20. {
  21. Y_SCOPE_EXIT(i = std::move(i), &p) {
  22. p = *i * 2;
  23. };
  24. }
  25. UNIT_ASSERT_VALUES_EQUAL(20, p);
  26. }
  27. Y_UNIT_TEST(TestDefer) {
  28. int i = 0;
  29. {
  30. Y_DEFER {
  31. i = 20;
  32. };
  33. }
  34. UNIT_ASSERT_VALUES_EQUAL(i, 20);
  35. }
  36. } // Y_UNIT_TEST_SUITE(ScopeToolsTest)