blob: ab11dff8ce483ffafc4dc4c990cd07a8d85b0ebd [file] [log] [blame]
Tim Shend8f58682016-08-10 17:52:09 +00001//===- llvm/unittest/ADT/ScopeExit.cpp - Scope exit unit tests --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/ScopeExit.h"
11#include "gtest/gtest.h"
12
13using namespace llvm;
14
15namespace {
16
17TEST(ScopeExitTest, Basic) {
18 struct Callable {
19 bool &Called;
20 Callable(bool &Called) : Called(Called) {}
21 Callable(Callable &&RHS) : Called(RHS.Called) {}
22 void operator()() { Called = true; }
23 };
24 bool Called = false;
25 {
26 auto g = make_scope_exit(Callable(Called));
27 EXPECT_FALSE(Called);
28 }
29 EXPECT_TRUE(Called);
30}
31
Sam McCall1e9dc042018-01-25 16:55:48 +000032TEST(ScopeExitTest, Release) {
33 int Count = 0;
34 auto Increment = [&] { ++Count; };
35 {
36 auto G = make_scope_exit(Increment);
37 auto H = std::move(G);
38 auto I = std::move(G);
39 EXPECT_EQ(0, Count);
40 }
41 EXPECT_EQ(1, Count);
42 {
43 auto G = make_scope_exit(Increment);
44 G.release();
45 }
46 EXPECT_EQ(1, Count);
47}
48
Tim Shend8f58682016-08-10 17:52:09 +000049} // end anonymous namespace