defer_test.cc 880 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "port/port.h"
  6. #include "port/stack_trace.h"
  7. #include "test_util/testharness.h"
  8. #include "util/defer.h"
  9. namespace ROCKSDB_NAMESPACE {
  10. class DeferTest {};
  11. TEST(DeferTest, BlockScope) {
  12. int v = 1;
  13. {
  14. Defer defer([&v]() { v *= 2; });
  15. }
  16. ASSERT_EQ(2, v);
  17. }
  18. TEST(DeferTest, FunctionScope) {
  19. int v = 1;
  20. auto f = [&v]() {
  21. Defer defer([&v]() { v *= 2; });
  22. v = 2;
  23. };
  24. f();
  25. ASSERT_EQ(4, v);
  26. }
  27. } // namespace ROCKSDB_NAMESPACE
  28. int main(int argc, char** argv) {
  29. ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
  30. ::testing::InitGoogleTest(&argc, argv);
  31. return RUN_ALL_TESTS();
  32. }