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
6 #include "port/port.h"
7 #include "port/stack_trace.h"
8 #include "test_util/testharness.h"
9 #include "util/defer.h"
10
11 namespace ROCKSDB_NAMESPACE {
12
13 class DeferTest {};
14
TEST(DeferTest,BlockScope)15 TEST(DeferTest, BlockScope) {
16 int v = 1;
17 {
18 Defer defer([&v]() { v *= 2; });
19 }
20 ASSERT_EQ(2, v);
21 }
22
TEST(DeferTest,FunctionScope)23 TEST(DeferTest, FunctionScope) {
24 int v = 1;
25 auto f = [&v]() {
26 Defer defer([&v]() { v *= 2; });
27 v = 2;
28 };
29 f();
30 ASSERT_EQ(4, v);
31 }
32
33 } // namespace ROCKSDB_NAMESPACE
34
main(int argc,char ** argv)35 int main(int argc, char** argv) {
36 ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
37 ::testing::InitGoogleTest(&argc, argv);
38 return RUN_ALL_TESTS();
39 }
40