[llvm-objdump] - Cleanup the error reporting.
[llvm-complete.git] / unittests / ADT / ScopeExitTest.cpp
blob14376529766cf22b0e2fb26ec18a0f42520444b6
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 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/ScopeExit.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
14 namespace {
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; }
23 bool Called = false;
25 auto g = make_scope_exit(Callable(Called));
26 EXPECT_FALSE(Called);
28 EXPECT_TRUE(Called);
31 TEST(ScopeExitTest, Release) {
32 int Count = 0;
33 auto Increment = [&] { ++Count; };
35 auto G = make_scope_exit(Increment);
36 auto H = std::move(G);
37 auto I = std::move(G);
38 EXPECT_EQ(0, Count);
40 EXPECT_EQ(1, Count);
42 auto G = make_scope_exit(Increment);
43 G.release();
45 EXPECT_EQ(1, Count);
48 } // end anonymous namespace