xref: /llvm-project/llvm/unittests/ADT/ScopeExitTest.cpp (revision 2946cd701067404b99c39fb29dc9c74bd7193eb3)
1 //===- llvm/unittest/ADT/ScopeExit.cpp - Scope exit unit tests --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ADT/ScopeExit.h"
10 #include "gtest/gtest.h"
11 
12 using namespace llvm;
13 
14 namespace {
15 
TEST(ScopeExitTest,Basic)16 TEST(ScopeExitTest, Basic) {
17   struct Callable {
18     bool &Called;
19     Callable(bool &Called) : Called(Called) {}
20     Callable(Callable &&RHS) : Called(RHS.Called) {}
21     void operator()() { Called = true; }
22   };
23   bool Called = false;
24   {
25     auto g = make_scope_exit(Callable(Called));
26     EXPECT_FALSE(Called);
27   }
28   EXPECT_TRUE(Called);
29 }
30 
TEST(ScopeExitTest,Release)31 TEST(ScopeExitTest, Release) {
32   int Count = 0;
33   auto Increment = [&] { ++Count; };
34   {
35     auto G = make_scope_exit(Increment);
36     auto H = std::move(G);
37     auto I = std::move(G);
38     EXPECT_EQ(0, Count);
39   }
40   EXPECT_EQ(1, Count);
41   {
42     auto G = make_scope_exit(Increment);
43     G.release();
44   }
45   EXPECT_EQ(1, Count);
46 }
47 
48 } // end anonymous namespace
49